-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
System.Text.Json Access Violation Exception (Attempted to read or write protected memory) #40878
Comments
Found the stack trace (edited it into the issue) - VS didn't show it in the exception dialog but it was in the Visual Studio Debug Console - hope it helps! |
With netcoreapp3.1 and net5.0 preview 8 on macOS, it throws Unhandled exception. System.EntryPointNotFoundException: Entry point was not found.
at Schema.NET.IValues.get_Count()
at Schema.NET.ValuesJsonConverter.Write(Utf8JsonWriter writer, IValues value, JsonSerializerOptions options) in /Users/am11/projects/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson/Schema.NET/ValuesJsonConverter.cs:line 111
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCoreAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at Schema.NET.SchemaSerializer.SerializeObject(Object value, JsonSerializerOptions options) in /Users/am11/projects/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson/Schema.NET/SchemaSerializer.cs:line 79
at Schema.NET.SchemaSerializer.SerializeObject(Object value) in /Users/am11/projects/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson/Schema.NET/SchemaSerializer.cs:line 62
at SchemaNet_SystemTextJson.Program.Main(String[] args) in /Users/am11/projects/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson/Program.cs:line 30 The runtime type of |
Interesting find @am11 - If I understand you right, you're saying that it didn't implicitly convert to On my machine, that EDIT: Oh, I just realised you were testing with Preview 8 - may explain a difference in errors. |
I am getting diff --git a/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson.csproj b/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson.csproj
index 68fd6b4..db1358e 100644
--- a/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson.csproj
+++ b/SchemaNet_SystemTextJson/SchemaNet_SystemTextJson.csproj
@@ -2,11 +2,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
- <TargetFramework>netcoreapp3.1</TargetFramework>
+ <TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
- <ItemGroup>
- <PackageReference Include="System.Text.Json" Version="5.0.0-preview.7.20364.11" />
- </ItemGroup>
</Project> This is because the runtime type of I have not looked into internal mechanics on S.T.J, so I am not sure; but looks like there are two issues:
|
Maybe because we are using different operating system (Windows, macOS)?
Btw, this mysterious EntryPointNotFoundException due to usage of generic structs has also been reported to roslyn repo few times: https://github.com/dotnet/roslyn/search?q=EntryPointNotFoundException&type=Issues. A self-contained repro is bit tricky but would be great to nib the bug. |
I wonder if this occurs for other types then like public class CustomType : Thing, IThing
{
public override string Type => "CustomType";
[JsonPropertyName("number")]
[JsonConverter(typeof(ValuesJsonConverter))]
public OneOrMany<int?> Number { get; set; }
[JsonPropertyName("uri")]
[JsonConverter(typeof(ValuesJsonConverter))]
public Values<string, Uri> Uri { get; set; }
}
class Program
{
static void Main(string[] args)
{
// {"@context":"https://schema.org","@type":"CustomType","Number":123}
var inputObj = new CustomType
{
Number = 123
};
var json = SchemaSerializer.SerializeObject(inputObj);
var outputObj = SchemaSerializer.DeserializeObject<CustomType>(json);
}
} For me, it is still the same error for the same configuration as in the issue description (.NET Core 3.1, S.T.J v5 Preview 7).
Interesting - this is the first I think Ive seen of an
|
Tested with few alternatives (value types included) and it is throwing EntryPointNotFoundException on macOS for all T in |
I had a look into the |
Might explain why @am11 had |
Yes, makes sense. |
Hi @layomia, I have a test and fix for this ready. Are you interested in a PR? |
Hi @Turnerj For performance reasons, STJ generates getter/setter in plain IL and explicitly without boxing. As a result, in this case the generated IL is invalid because the values (say an Int32) are treated as pointer to an object. In #40914 the missing box/unbox IL is emitted, which fixes the problem, but I assume there are performance concerns and it will not be merged - at least in .NET 5. As a workaround you can create a @layomia Please let me know what you think about the boxing in #40914. If that is not possible, there should a |
Re-opening until the fix is back-ported to .NET 5 - #41366. |
Fixed for 5.0 in #41366. |
Thanks @Turnerj. Yes, the fix will come in 5.0 RC1. Please let us know how things go with the fix. |
Description
First just a quick bit of background: as I've raised in comments in #36621, I've hit a variety of
InvalidProgramException
andAccessViolationException
while trying to bring System.Text.Json (Previews 4 through 7) to Schema.NET (RehanSaeed/Schema.NET#100). Prior to trying System.Text.Json v5 previews, I had tried v4 which worked with my converter without this exception (though I really wanted the setting to not serialize default values which is coming in v5).Jumping to now with Preview 7, I'm hitting issues with an
AccessViolationException
with the error message:I've created a custom solution with as little code as I reasonably could while still using a custom version of the core from Schema.NET: https://github.com/Turnerj/SchemaNet_SystemTextJson
Things to note:
OneOrMany
andValues
are both structs which will serialize to a single item or an arrayValues
is special because it can convert to and from any of the generic types specified on itValuesJsonConverter
does the guts of the converting. In debugging, theCanConvert
method is hit howeverWrite
is never actually being called on it.ValuesJsonConverter
'sCanConvert
is:public override bool CanConvert(Type typeToConvert) => typeof(IValues).IsAssignableFrom(typeToConvert);
Configuration
Regression?
As mentioned, I didn't have this issue with System.Text.Json v4 (likely the last minor version) though I needed v5 as I needed the "don't serialize default values" configuration.
Other information
Full Exception
Additionally, when I first went to Preview 4, while I didn't hit this exception, I did hit a CLR exception ("Common Language Runtime detected an invalid program").
While this issue is happening with serialization, I have experienced similar issues with deserialization too when running CI builds of the Schema.NET PR I've linked to a few times. I'm thinking the fix for serialization might fix the deserialization issue too.
The text was updated successfully, but these errors were encountered: