Skip to content

System.Text.Json Access Violation Exception (Attempted to read or write protected memory) #40878

@Turnerj

Description

@Turnerj

Description

First just a quick bit of background: as I've raised in comments in #36621, I've hit a variety of InvalidProgramException and AccessViolationException 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:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

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

public class CustomType : Thing, IThing
{
	public override string Type => "CustomType";

	[JsonPropertyName("name")]
	[JsonConverter(typeof(ValuesJsonConverter))]
	public OneOrMany<string> Name { get; set; }

	[JsonPropertyName("uri")]
	[JsonConverter(typeof(ValuesJsonConverter))]
	public Values<string, Uri> Uri { get; set; }
}

class Program
{
	static void Main(string[] args)
	{
		// This is the JSON of the object below
		// {"@context":"https://schema.org","@type":"CustomType","name":"Hello World"}
		var inputObj = new CustomType
		{
			Name = "Hello World"
		};

		var json = SchemaSerializer.SerializeObject(inputObj); // Exception occurs on serialization

		var outputObj = SchemaSerializer.DeserializeObject<CustomType>(json);
	}
}

Things to note:

  • OneOrMany and Values are both structs which will serialize to a single item or an array
  • Values is special because it can convert to and from any of the generic types specified on it
  • ValuesJsonConverter does the guts of the converting. In debugging, the CanConvert method is hit however Write is never actually being called on it.
  • The ValuesJsonConverter's CanConvert is: public override bool CanConvert(Type typeToConvert) => typeof(IValues).IsAssignableFrom(typeToConvert);

Configuration

  • .NET Core 3.1
  • System.Text.Json 5.0.0-preview.7.20364.11
  • Windows 10 Home (10.0.18362 Build 18362)
  • I've hit this issue in CI builds on this pull request for Ubuntu, Mac and Windows

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

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
at DynamicClass.get_Name(System.Object)
at System.Text.Json.JsonPropertyInfo1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetMemberAndWriteJson(System.Object, System.Text.Json.WriteStack ByRef, System.Text.Json.Utf8JsonWriter) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnTryWrite(System.Text.Json.Utf8JsonWriter, System.__Canon, System.Text.Json.JsonSerializerOptions, System.Text.Json.WriteStack ByRef)
at System.Text.Json.Serialization.JsonConverter1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryWrite(System.Text.Json.Utf8JsonWriter, System.__Canon ByRef, System.Text.Json.JsonSerializerOptions, System.Text.Json.WriteStack ByRef) at System.Text.Json.Serialization.JsonConverter1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].WriteCore(System.Text.Json.Utf8JsonWriter, System.__Canon ByRef, System.Text.Json.JsonSerializerOptions, System.Text.Json.WriteStack ByRef)
at System.Text.Json.Serialization.JsonConverter`1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].WriteCoreAsObject(System.Text.Json.Utf8JsonWriter, System.Object, System.Text.Json.JsonSerializerOptions, System.Text.Json.WriteStack ByRef)
at System.Text.Json.JsonSerializer.WriteCore[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Text.Json.Serialization.JsonConverter, System.Text.Json.Utf8JsonWriter, System.__Canon ByRef, System.Text.Json.JsonSerializerOptions, System.Text.Json.WriteStack ByRef)
at System.Text.Json.JsonSerializer.WriteCore[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Text.Json.Utf8JsonWriter, System.__Canon ByRef, System.Type, System.Text.Json.JsonSerializerOptions)
at System.Text.Json.JsonSerializer.Serialize[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef, System.Type, System.Text.Json.JsonSerializerOptions)
at System.Text.Json.JsonSerializer.Serialize[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon, System.Text.Json.JsonSerializerOptions)
at Schema.NET.SchemaSerializer.SerializeObject(System.Object, System.Text.Json.JsonSerializerOptions)
at Schema.NET.SchemaSerializer.SerializeObject(System.Object)
at SchemaNet_SystemTextJson.Program.Main(System.String[])

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions