Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against (de)serializing pointers & ref structs #34777

Merged
merged 12 commits into from
Apr 27, 2020

Conversation

layomia
Copy link
Contributor

@layomia layomia commented Apr 9, 2020

Fixes #34644.

Ref structs are explicitly not supported for deserialization following this PR; created an issue to consider support in the future - #34779. Prior to this change we leak a TypeLoadException:

 typeof(System.TypeLoadException): The generic type 'System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1' was used with an invalid instantiation in assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
        ---- System.TypeLoadException : The generic type 'System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1' was used with an invalid instantiation in assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
        Stack Trace:
             at System.RuntimeTypeHandle.Instantiate(QCallTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
          D:\repos\dotnet_runtime_2\src\coreclr\src\System.Private.CoreLib\src\System\RuntimeHandles.cs(486,0): at System.RuntimeTypeHandle.Instantiate(Type[] inst)
          D:\repos\dotnet_runtime_2\src\coreclr\src\System.Private.CoreLib\src\System\RuntimeType.CoreCLR.cs(3226,0): at System.RuntimeType.MakeGenericType(Type[] instantiation)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Object\ObjectConverterFactory.cs(38,0): at System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(Type typeToConvert, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonConverterFactory.cs(60,0): at System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializerOptions.Converters.cs(192,0): at System.Text.Json.JsonSerializerOptions.GetConverter(Type typeToConvert)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializerOptions.Converters.cs(105,0): at System.Text.Json.JsonSerializerOptions.DetermineConverter(Type parentClassType, Type runtimePropertyType, PropertyInfo propertyInfo)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonClassInfo.cs(343,0): at System.Text.Json.JsonClassInfo.GetConverter(Type type, Type parentClassType, PropertyInfo propertyInfo, Type& runtimeType, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonClassInfo.Cache.cs(88,0): at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonClassInfo.cs(119,0): at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializerOptions.cs(340,0): at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\WriteStack.cs(71,0): at System.Text.Json.WriteStack.Initialize(Type type, JsonSerializerOptions options, Boolean supportContinuation)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Helpers.cs(23,0): at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue value, Type inputType, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.ByteArray.cs(67,0): at System.Text.Json.JsonSerializer.WriteCoreBytes[TValue](TValue value, Type inputType, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.ByteArray.cs(53,0): at System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(Object value, Type inputType, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\tests\Serialization\SerializationWrapper.cs(32,0): at System.Text.Json.Serialization.Tests.SerializationWrapper.SpanSerializerWrapper.Serialize(Object value, Type inputType, JsonSerializerOptions options)
          D:\repos\dotnet_runtime_2\src\libraries\System.Text.Json\tests\Serialization\InvalidTypeTests.cs(67,0): at System.Text.Json.Serialization.Tests.InvalidTypeTests.<>c__DisplayClass5_0.<TypeWithInvalidMember>b__1()

@layomia layomia added this to the 5.0 milestone Apr 9, 2020
@layomia layomia self-assigned this Apr 9, 2020
@ahsonkhan
Copy link
Member

Ref structs are explicitly not supported for deserialization following this PR; created an issue to consider support in the future - #34777.

I think you meant #34779.

@layomia
Copy link
Contributor Author

layomia commented Apr 27, 2020

Test failure unrelated.

@layomia layomia merged commit 6f4abe4 into dotnet:master Apr 27, 2020
@layomia layomia deleted the invalid_type branch April 27, 2020 19:29
@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System.Text.Json unsafe and ref struct serialization exceptions
5 participants