Skip to content

FixedAddressValueType is silently ignored when not supported #129664

Open

Description

@MichalPetryka

Given a class like

static class C
{
    [FixedAddressValueType]
    public static int I;
    [FixedAddressValueType]
    public static object O;
}

Since CoreCLR ignores FixedAddressValueType on non ValueType fields, this will silently allocate the fields normally.

This can cause potential GC holes in code unaware of this.

The runtime should either make FixedAddressValueType work in all cases (like POH arrays do) or throw TypeLoadException when not supported.

cc @jkotas what do you think?

Activity

  1. added
    untriaged
    on Jun 21, 2026
  2. hamarb123 commented on Jun 21, 2026

    @hamarb123
    Contributor

    Btw, I added this to hamarb123.Analyzers to ensure I didn't run into this unintuitive behaviour by accident: https://github.com/hamarb123/hamarb123.Analyzers#fixedaddressvaluetype-analyzer

    throw TypeLoadException when not supported

    I'm personally not convinced about this alternative idea, but working around it is always relatively easy ig (just put in a wrapper struct & use pointer to field instead).

  3. MichalPetryka commented on Jun 21, 2026

    @MichalPetryka
    ContributorAuthor

    Looking at usages on GitHub, I saw:

    [FixedAddressValueType] private readonly byte _value;

    The runtime should definitely throw TypeLoadException for non static usages.

  4. MichalPetryka commented on Jun 21, 2026

    @MichalPetryka
    ContributorAuthor

    There is one such GC hole in this repo:

    [FixedAddressValueType]
    private static readonly unsafe IntPtr s_globalHostServices = (IntPtr)Unsafe.AsPointer(in HostServices.Vftbl);

  5. jkotas commented on Jun 21, 2026

    @jkotas
    Member

    CoreCLR ignores FixedAddressValueType on non ValueType fields

    The same is true for other attributes that are meant to be only applicable in specific situations. For example, marking instance fields with [ThreadStatic] attribute does nothing as well.

    You are suggesting that we should read all these attributes eagerly in all cases just so we can tell the user that the attribute is ignored. I am not sure it is worth the startup performance overhead. I think it is a job for an analyzer or IL verify.

    There is one such GC hole in this repo

    I think the primary source of this GC hole is poor documentation and naming. The documentation says that the attribute applies to static value type fields. IntPtr is a value type, so one can expect it will work just fine - but that's not actually the case. We should fix the documentation to say that the attribute does not apply to primitive value type and enum fields.

  6. jkotas commented on Jun 21, 2026

    @jkotas
    Member

    System.Private.CoreLib/src/System/Runtime/InteropServices/ReferenceTrackerHost.cs

    This is not a real GC hole currently since CoreLib non-GC statics are allocated in non-GC memory that does not move. I agree we should fix it.

  7. teo-tsirpanis commented on Jun 21, 2026

    @teo-tsirpanis
    Contributor

    the attribute does not apply to primitive value type and enum fields

    That's surprising; why should it matter here that the type is primitive? How about we support this attribute on all value types?

  8. jkotas commented on Jun 21, 2026

    @jkotas
    Member

    why should it matter here that the type is primitive?

    It makes the runtime implementation simpler and more efficient. For example, the runtime looks up the attribute for non-primitive value types only currently.

    How about we support this attribute on all value types?

    It is certainly doable. The question is whether overheads and complications that would come with it are worth it.

  9. tannergooding commented on Jun 22, 2026

    @tannergooding
    Member

    The trivial workaround here is AllocTypeAssociatedMemory. It requires the use of unsafe somewhere, but so did all the code using FAVT in the other scenarios anyways

  10. hamarb123 commented on Jun 22, 2026

    @hamarb123
    Contributor

    The more trivial workaround is to just use ValueTuple<MyType> & access myStaticField.Item1 instead of myStaticField. It works for all types (except pointers, ofc), including object, etc.

  11. jkotas commented on Jun 23, 2026

    @jkotas
    Member

    Docs clarified by dotnet/dotnet-api-docs#12770
    Potential bug in ReferenceTrackerHost fixed by #129669

  12. removed
    untriaged
    on Jun 23, 2026
  13. added this to the Future milestone on Jun 23, 2026
  14. MichalPetryka commented on Jun 23, 2026

    @MichalPetryka
    ContributorAuthor

    Docs clarified by dotnet/dotnet-api-docs#12770 Potential bug in ReferenceTrackerHost fixed by #129669

    If the current behaviour is to stay, could we get builtin analyzers that warn on invalid FAVT (and ThreadStatic too ideally) usages? Since looking at github there are tons of projects with bugs like this, even the Stride3D game engine had one.

  15. MichalStrehovsky commented on Jul 14, 2026

    @MichalStrehovsky
    Member

    Interesting, does this cause GC holes for /clr:pure because MSVC does assume FixedAddressValueType is valid on primitives?

    It doesn't seem to be a problem for /clr because MSVC will make the field an RVA static and map it to the data section, but I might just not know enough C++/CLI.

    int myfield;
    
    public ref class Hello
    {
    public:
      int* GetStorage() { return &myfield; }
    };

    Compiles to:

    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
      .hash = (7F CE 86 E4 C2 B6 03 E9 D5 55 01 23 E7 03 D8 3A   // .........U.#...:
               18 46 75 F6 )                                     // .Fu.
      .ver 4:0:0:0
    }
    .assembly test
    {
      .permissionset reqmin
                 = {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module test.exe
    // MVID: {3A913CD0-1ED3-4409-BAB2-32328C94C8C9}
    .imagebase 0x0000000180000000
    .file alignment 0x00000200
    .stackreserve 0x0000000000100000
    .subsystem 0x0002       // WINDOWS_GUI
    .corflags 0x00000001    //  ILONLY
    // Image base: 0x000001D633000000
    
    
    // ================== GLOBAL FIELDS ==========================
    
    .field static assembly int32 myfield
    .custom instance void [mscorlib]System.Runtime.CompilerServices.FixedAddressValueTypeAttribute::.ctor() = ( 01 00 00 00 ) 
    
    // =============================================================
    
    
    // =============== CLASS MEMBERS DECLARATION ===================
    
    .class public auto ansi beforefieldinit Hello
           extends [mscorlib]System.Object
    {
      .method public hidebysig instance int32* 
              GetStorage() cil managed
      {
        // Code size       8 (0x8)
        .maxstack  1
        .locals (int32* V_0)
        IL_0000:  ldsflda    int32 myfield
        IL_0005:  stloc.0
        IL_0006:  ldloc.0
        IL_0007:  ret
      } // end of method Hello::GetStorage
    
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // Code size       7 (0x7)
        .maxstack  1
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
        IL_0006:  ret
      } // end of method Hello::.ctor
    
    } // end of class Hello
    
    
    // =============================================================
    
    .custom ([mscorlib]System.Runtime.CompilerServices.AssemblyAttributesGoHereSM) instance void [mscorlib]System.Security.Permissions.SecurityPermissionAttribute::.ctor(valuetype [mscorlib]System.Security.Permissions.SecurityAction) = ( 01 00 08 00 00 00 01 00 54 02 10 53 6B 69 70 56   // ........T..SkipV
                                                                                                                                                                                                                                              65 72 69 66 69 63 61 74 69 6F 6E 01 )             // erification.
    // *********** DISASSEMBLY COMPLETE ***********************
    
    
  16. jkotas commented on Jul 14, 2026

    @jkotas
    Member

    FixedAddressValueType is valid on primitives?

    On .NET Framework, primitives happen to be stored in unmanaged memory (except for ref-emitted collectible assemblies), And /clr:pure is not supported on modern .NET, so we do not have the problem here either.

  17. hamarb123 commented on Jul 14, 2026

    @hamarb123
    Contributor

    Yeah, looks like a GC hole to me (unless C++/CLI uses some special thing somehow, but it doesn't seem like it to me). Although I think /clr:pure has been deprecated for like 10 years now, right?

  18. hamarb123 commented on Jul 14, 2026

    @hamarb123
    Contributor

    @MichalStrehovsky what does /clr do for generic type parameter w/ static field of that type that you return by pointer (assuming it allows that), as that could be a problem potentially.

  19. MichalStrehovsky commented on Jul 14, 2026

    @MichalStrehovsky
    Member

    @MichalStrehovsky what does /clr do for generic type parameter w/ static field of that type that you return by pointer (assuming it allows that), as that could be a problem potentially.

    It doesn't allow it because then it's a managed class and one cannot cast addresses of statics on managed classes to pointers.

    Defining the field as __declspec(appdomain) int myfield; is probably the only way to make /clr (without pure) emit the GC hole-y code.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions