Skip to content

LibraryImport methods should be implicitly RequiresUnsafe #125801

Closed

Description

@jkoritzinsky

Methods attributed with LibraryImportAttribute are implicitly unsafe. Although the marshalling logic could be analyzed to be considered memory-safe, the compiler cannot validate that the underlying extern DllImport is targeting a method with a valid signature.

Today, the stubs generated by LibraryImportGenerator will not generate the RequiresUnsafeAttribute on the produced stubs. This causes inconsistency with stubs that have no marshalling (they are just a raw DllImport, which is extern).

The LibraryImportGenerator should add the RequiresUnsafeAttribute to all generated stubs to provide a consistent experience.

Activity

  1. added this to the 11.0.0 milestone on Mar 19, 2026
  2. dotnet-policy-service commented on Mar 19, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  3. dotnet-policy-service commented on Apr 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  4. AaronRobinsonMSFT commented on Jul 15, 2026

    @AaronRobinsonMSFT
    Member

    Moved this to .NET 12. @jkoritzinsky if you can do this in .NET 11 that is also fine.

  5. jkoritzinsky commented on Jul 20, 2026

    @jkoritzinsky
    MemberAuthor

    Closing this as we're taking a different approach (see dotnet/roslyn#84555)

  6. locked and limited conversation to collaborators on Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions