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

Determine whether running in NativeAOT at runtime #94380

Closed
vaind opened this issue Nov 5, 2023 · 6 comments
Closed

Determine whether running in NativeAOT at runtime #94380

vaind opened this issue Nov 5, 2023 · 6 comments

Comments

@vaind
Copy link

vaind commented Nov 5, 2023

Is there any API that can be used to determine at runtime, from a normal, not AOT compiled library, whether the application that includes this library has been published as Native AOT? The only thing I'm aware of is an indirect approach of checking stack trace & the features available there

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 5, 2023
@ghost
Copy link

ghost commented Nov 5, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Is there any API that can be used to determine at runtime, from a normal, not AOT compiled library, whether the application that includes this library has been published as Native AOT? The only thing I'm aware of is an indirect approach of checking stack trace & the features available there

Author: vaind
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr

Milestone: -

@KeterSCP
Copy link

KeterSCP commented Nov 5, 2023

See #76739

@vaind vaind closed this as completed Nov 5, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 5, 2023
@MichalStrehovsky
Copy link
Member

See #76739

To be clear that issue was not about Native AOT (it's for Mono AOT). RuntimeFeature.IsDynamicCodeSupported will return false if dynamic code is not supported by the runtime, like the docs say. It can return false on runtimes that are not Native AOT: even CoreCLR with JIT can report false here if the app disables Reflection.Emit through an AppContext switch. There's no API to check for native AOT specifically. If native AOT ever gets an IL interpreter, this API could return true on Native AOT as well. What's the scenario you need this for?

@vaind
Copy link
Author

vaind commented Nov 6, 2023

What's the scenario you need this for?

Different stack trace handling in Sentry based on the library being included in a NativeAOT-compiled vs "standard" app. We were considering shipping two versions of the library but that would be complicating and we'd likely need to add a new nuget package which is terrible for library consumers.

@MichalStrehovsky
Copy link
Member

MichalStrehovsky commented Nov 6, 2023

It would be more reliable to tie it to the difference in stack trace behavior. IsDynamicCodeSupported will work in .NET 8, but you have to accept it is going to report false even in Mono scenarios (that have different problems for stack traces on Blazor-WASM-AOT) and also when running on top of CoreCLR with JIT sometimes (e.g. adding <PublishAot>true</PublishAot> to a project will report IsDynamicCodeSupported=false even for dotnet run with no AOT compilation in sight).

@vaind
Copy link
Author

vaind commented Nov 6, 2023

It would be more reliable to tie it to the difference in stack trace behavior

I've done just that, thanks for confirmation that's the right approach for now.

#if NET6_0_OR_GREATER   // TODO NET7 once we target it
        var stackTrace = new StackTrace(false);
        IsAot = stackTrace.GetFrame(0)?.GetMethod() is null;
#endif

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants