Skip to content

System.Text.Json fails to build on .NET Native due to unconstrained raw pointer uses #84895

Closed

Description

@Sergio0694

Description

I'm dogfooding .NET 8 builds of System.Text.Json in the Microsoft Store, now that we've fully migrated to it, and noticed that since the .NET 8 Preview 1 release, .NET Native has starting failing to build. We've investigated this and @AntonLapounov managed to narrow this down to some uses of unconstrained raw pointers, which is a known issue with .NET Native.

Specifically, the build failure is caused by some changes in 4deccc02588990626180eaa812e63603d6c2790e:

case TypeCode.Int32:
if (reader.TryGetInt32(out int int32))
{
return *(T*)&int32;
}
break;
case TypeCode.UInt32:
if (reader.TryGetUInt32(out uint uint32))
{
return *(T*)&uint32;
}
break;
case TypeCode.UInt64:
if (reader.TryGetUInt64(out ulong uint64))
{
return *(T*)&uint64;
}
break;
case TypeCode.Int64:
if (reader.TryGetInt64(out long int64))
{
return *(T*)&int64;
}
break;
case TypeCode.SByte:
if (reader.TryGetSByte(out sbyte byte8))
{
return *(T*)&byte8;
}
break;
case TypeCode.Byte:
if (reader.TryGetByte(out byte ubyte8))
{
return *(T*)&ubyte8;
}
break;
case TypeCode.Int16:
if (reader.TryGetInt16(out short int16))
{
return *(T*)&int16;
}
break;
case TypeCode.UInt16:
if (reader.TryGetUInt16(out ushort uint16))
{
return *(T*)&uint16;
}
break;

case TypeCode.Int32:
writer.WriteNumberValue(*(int*)&value);
break;
case TypeCode.UInt32:
writer.WriteNumberValue(*(uint*)&value);
break;
case TypeCode.UInt64:
writer.WriteNumberValue(*(ulong*)&value);
break;
case TypeCode.Int64:
writer.WriteNumberValue(*(long*)&value);
break;
case TypeCode.Int16:
writer.WriteNumberValue(*(short*)&value);
break;
case TypeCode.UInt16:
writer.WriteNumberValue(*(ushort*)&value);
break;
case TypeCode.Byte:
writer.WriteNumberValue(*(byte*)&value);
break;
case TypeCode.SByte:
writer.WriteNumberValue(*(sbyte*)&value);
break;
default:
ThrowHelper.ThrowJsonException();
break;

This is a complete blocker for us, as we cannot update to System.Text.Json 8.0 at all.

Could we revert these changes in EnumConverter<T> and just keep using Unsafe.As<TFrom, TTo>? Since it's an intrinsic it wouldn't really have any performance impacts on .NET 8, and it'd at least fix the compile issues on .NET Native we're hitting.

cc. @eiriktsarpalis, also could we mark this as partner impact? We're completely stuck on 7.0 as of right now 🥲

Reproduction Steps

  • Create a blank UWP app
  • Reference System.Text.Json 8.0.0 Preview 1 or greater
  • Add this code
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum MyEnum
{
    A,
    B,
    C
}

public sealed class MyClass
{
    public MyEnum Value { get; set; }
}

[JsonSerializable(typeof(MyClass))]
public sealed partial class MyContext : JsonSerializerContext
{
}

Compile in Release.

Expected behavior

The app should build correctly.

Actual behavior

The build fails with an error like this:

"Error ILT0005: 'C:\Users\sergiopedri.nuget\packages\runtime.win10-x64.microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\x64\ilc\Tools\nutc_driver.exe @"C:\Users\sergiopedri\source\repos\SystemTextJsonILCRepro\obj\x64\Release\ilc\intermediate\MDIL\SystemTextJsonILCRepro.rsp"' returned exit code -1073741819"

Specifically, the real issue is here:

"System.Text.Json.Serialization.Converters.EnumConverter`1[[System.__UniversalCanon,System.Private.CoreLib]],System.Text.Json" "Read" /singleMethodParam "System.__UniversalCanon,System.Private.CoreLib" /singleMethodParam "System.Text.Json.Utf8JsonReader&,System.Text.Json" /singleMethodParam "System.Type,System.Private.CoreLib" /singleMethodParam "System.Text.Json.JsonSerializerOptions,System.Text.Json""

Regression?

Yes, it works just fine with System.Text.Json 7.0.2.

Activity

  1. ghost added
    untriaged
    on Apr 16, 2023
  2. Sergio0694 commented on Apr 16, 2023

    @Sergio0694
    ContributorAuthor

    Thinking about this more, and since @tannergooding mentioned that the previous use of Unsafe.As<TFrom, TTo> had some concerns around additional generic instantiations, could we not just stop using pointers altogether and just box/unbox? The JIT/AOT compilers should recognize things anyway and elide the boxing anyway. That is, in both directions:

    // Read
    if (reader.TryGetInt32(out int int32))
    {
        return (T)(object)int32;
    }
    // Write
    writer.WriteNumberValue((int)(object)value);

    This would both solve the issue on .NET Native and also potentially gives even better codegen due to no address taken?

  3. Sergio0694 commented on Apr 16, 2023

    @Sergio0694
    ContributorAuthor

    "won't work for .NET Framework"

    Just to clarify, do you mean it'll work slowly, and not that it won't work at all? Because ai tried this in the NETFX C# interactive window and it seems to work fine on NETFX too.

    If performance on NETFX and Mono is an issue, it seems we could:

    • Revert the Unsafe.As use assuming that's fine with respect to additional generics.
    • Use a few #ifdef-s to keep raw pointers on .NET 8+, and revert only on downlevel targets? If it's just this couple methods it seems manageable?
  4. EgorBo commented on Apr 16, 2023

    @EgorBo
    Member

    it'll work slowly

    Yes, it boxes (allocates)

  5. jkotas commented on Apr 16, 2023

    @jkotas
    Member

    it's an intrinsic it wouldn't really have any performance impacts on .NET 8

    The generic intrinsic references contribute to static footprint that is also a performance metric. Reducing the static footprint was my motivation for the change.

    Revert the Unsafe.As use assuming that's fine with respect to additional generics

    I think it is fine to revert the change for System.Text.Json. System.Text.Json EnumConverter has large static footprint (e.g. it instantiates ConcurrentDictionary<string, T> - that is 10's kB of static footprint right there for each serialized enum type). A few extra generic instantiations from Unsafe.As are not a big deal.

  6. ghost added
    in-pr
    on Apr 16, 2023
  7. Sergio0694 commented on Apr 16, 2023

    @Sergio0694
    ContributorAuthor

    "The generic intrinsic references contribute to static footprint that is also a performance metric. Reducing the static footprint was my motivation for the change."

    Yup, no that makes perfect sense. I should've clarified, I only meant to revert those changes in EnumConverter<T> specifically, I do understand the motivation for the change in all other places in general to reduce generic instantiations 🙂


    Was about to say feel free to assign me to this but I see you already have a PR up ahah awesome! 😄

  8. added a commit that references this issue on Apr 17, 2023
    0099ffa
  9. ghost removed
    in-pr
    on Apr 17, 2023
  10. ghost removed
    untriaged
    on Apr 17, 2023
  11. ghost locked as resolved and limited conversation to collaborators on May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions