dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.56k stars 4.54k forks source link

Complete warnings for AOT unsafe interop #74697

Open jkotas opened 1 year ago

jkotas commented 1 year ago

Warnings for AOT unsafe interop are incomplete best effort currently: https://github.com/dotnet/runtime/pull/74630#discussion_r955966809

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

MichalStrehovsky commented 1 year ago

Steer people towards P/Invoke source generator by the warning text

If we tell people to use the source generator for AsAny/LayoutClass/System.Delegate marshalling, the source generator will then tell them it's unsupported. It will shift the blame, but the end result for the user is the same if we don't send them on a hike first.

jkotas commented 1 year ago

Yes, it is the case with retrofitting existing AOT unsafe code.

Steering people towards source generated interop is more interesting for new code. Source generated interop has natural AOT safety guiderails and prevents the AOT unsafe code to be written in the first place.

hez2010 commented 1 year ago

towards P/Invoke source generator

Do source generated P/Invokes support direct P/Invoke and static linking?

If not then I think it's not a replacement of current P/Invokes.

jkotas commented 1 year ago

Do source generated P/Invokes support direct P/Invoke and static linking?

Yes. This aspect is not different between source generated and built-in marshalling.

All P/Invokes (except a few outliers) in this repo have been switched to be source generated, and native aot publishing uses direct P/Invokes and static linking extensively.

MichalStrehovsky commented 1 year ago

Use actual marshaller kind to produce the warning instead of the approximation at

This logic is shared with IL Linker and IL Linker doesn't have access to this unfortunately.