You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FixedAddressValueType is silently ignored when not supported #129664
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).
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.
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.
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
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.
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.
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 classHello
{
public:int* GetStorage() { return &myfield; }
};
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.
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?
@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.
@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.
Activity
hamarb123 commentedon Jun 21, 2026
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
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).
MichalPetryka commentedon Jun 21, 2026
Looking at usages on GitHub, I saw:
The runtime should definitely throw
TypeLoadExceptionfor non static usages.MichalPetryka commentedon Jun 21, 2026
There is one such GC hole in this repo:
runtime/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ReferenceTrackerHost.cs
Lines 14 to 15 in e556d5a
jkotas commentedon Jun 21, 2026
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.
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.
jkotas commentedon Jun 21, 2026
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.
teo-tsirpanis commentedon Jun 21, 2026
That's surprising; why should it matter here that the type is primitive? How about we support this attribute on all value types?
jkotas commentedon Jun 21, 2026
It makes the runtime implementation simpler and more efficient. For example, the runtime looks up the attribute for non-primitive value types only currently.
It is certainly doable. The question is whether overheads and complications that would come with it are worth it.
tannergooding commentedon Jun 22, 2026
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 anywayshamarb123 commentedon Jun 22, 2026
The more trivial workaround is to just use
ValueTuple<MyType>& accessmyStaticField.Item1instead ofmyStaticField. It works for all types (except pointers, ofc), includingobject, etc.jkotas commentedon Jun 23, 2026
Docs clarified by dotnet/dotnet-api-docs#12770
Potential bug in ReferenceTrackerHost fixed by #129669
MichalPetryka commentedon Jun 23, 2026
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.
MichalStrehovsky commentedon Jul 14, 2026
Interesting, does this cause GC holes for
/clr:purebecause MSVC does assumeFixedAddressValueTypeis valid on primitives?It doesn't seem to be a problem for
/clrbecause MSVC will make the field an RVA static and map it to the data section, but I might just not know enough C++/CLI.Compiles to:
jkotas commentedon Jul 14, 2026
On .NET Framework, primitives happen to be stored in unmanaged memory (except for ref-emitted collectible assemblies), And
/clr:pureis not supported on modern .NET, so we do not have the problem here either.hamarb123 commentedon Jul 14, 2026
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:purehas been deprecated for like 10 years now, right?hamarb123 commentedon Jul 14, 2026
@MichalStrehovsky what does
/clrdo 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.MichalStrehovsky commentedon Jul 14, 2026
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(withoutpure) emit the GC hole-y code.