Skip to content
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

[NativeAOT] Unexpected behaviour of GetCustomAttribute in reflection-free mode #1790

Closed
tomvanenckevort opened this issue Dec 19, 2021 · 1 comment
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation

Comments

@tomvanenckevort
Copy link

I'm trying to build an app that uses a third-party library with Native AOT in reflection free mode (IlcDisableReflection set to true).
Since that library uses the GetCustomAttribute method in a few places I turned on shimming for the attributes (Switch.System.Reflection.Disabled.DoNotThrowForAttributes set to true).
Since that shims GetCustomAttributes to return an empty array, I assumed that GetCustomAttribute would return null (as it normally calls GetCustomAttributes internally and returns the first matching attribute or null if not found).

However, instead I get a Reflection_Disabled exception as it seems to be calling the RuntimeTypeInfo.GetAttributeFlagsImpl method first. Here's the final part of the stacktrace:

   at Internal.Reflection.RuntimeTypeInfo.GetAttributeFlagsImpl() + 0x33
   at Internal.Reflection.Extensions.NonPortable.CustomAttributeSearcher`1.GetMatchingCustomAttributes(E, Type, Boolean, Boolean) + 0xa1
   at Internal.Reflection.Extensions.NonPortable.CustomAttributeInheritanceRules.GetMatchingCustomAttributes(MemberInfo, Type, Boolean, Boolean) + 0x50
   at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo) + 0x2d

Is there a particular reason why it would take this path?
Or is it the case that Native AOT needs a similar override to GetCustomAttributes for GetCustomAttribute that will return null when no reflection is used and the DoNotThrowForAttributes setting is turned on?

@MichalStrehovsky MichalStrehovsky added the area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation label Dec 21, 2021
@MichalStrehovsky
Copy link
Member

This will be fixed when dotnet/runtime#67193 is fixed. We could introduce yet another shim for this (we can compute some of the AttributeFlags, but not all), but I think it will be better to look into dotnet/runtime#67193 at some point.

@jkotas jkotas closed this as completed Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation
Projects
None yet
Development

No branches or pull requests

3 participants