Skip to content

Two CLR-based apps cannot coexist on Apple platforms #128867

Closed

Description

@MichalStrehovsky

The duplicate is the Apple crypto native library ending up in the same process twice — once dynamically (the running .NET host) and once statically baked into our AOT dylib.

  1. Host already has it (dynamically): the normal dotnet host loads the shared framework, which includes libSystem.Security.Cryptography.Native.Apple.dylib. That dylib contains pal_swiftbindings.o, whose Swift interop registers ObjC classes (HashBox, X25519KeyBox) with the process-wide ObjC runtime at load time.

  2. NativeAOT bakes a second copy in (statically): in Microsoft.NETCore.Native.Unix.targets (ILCompiler 11.0.0-preview.5.26261.101), line ~150:

    <NetCoreAppNativeLibrary Include="System.Security.Cryptography.Native.Apple"
                             Condition="'$(_IsApplePlatform)' == 'true'" />

    The only condition is "is this an Apple platform" — there is no '$(NativeLib)' != 'Shared' guard. So our NativeLib=Shared output (libdotnet-aot.dylib) statically links the .a version of the same crypto library, including the same pal_swiftbindings.o and its HashBox/X25519KeyBox classes.

  3. ObjC class names are process-global → collision: our dylib is loaded into the host process that already registered those classes from the framework copy. The dynamic loader then prints to stderr:

    objc[...]: Class HashBox is implemented in both .../libSystem.Security.Cryptography.Native.Apple.dylib and .../libdotnet-aot.dylib. One of the two will be used. Which one is undefined.

    That stderr line is what trips the StdErr.Should().BeEmpty() assertions in our tests.

Root flaw: the NativeAOT targets assume the output is a self-contained app that needs crypto statically baked in. For a NativeLib=Shared library loaded into an already-running .NET process, that static copy is redundant and collides with the framework's dynamic copy. The proper upstream fix looks like guarding line ~150 (and the matching DirectPInvoke) with '$(NativeLib)' != 'Shared' so shared-library AOT outputs don't re-embed framework-provided native libs.

Our RemoveAppleCryptoFromNativeAotLink target is essentially doing that guard manually from the csproj after the fact. Does guarding these NetCoreAppNativeLibrary/DirectPInvoke items on NativeLib != Shared sound like the right runtime-side fix, or is there a preferred way to handle native-lib linking for shared AOT outputs that load into an existing runtime?

Originally posted by @NikolaMilosavljevic in dotnet/sdk#54384 (comment)

Activity

  1. added
    untriaged
    on Jun 1, 2026
  2. dotnet-policy-service commented on Jun 1, 2026

    @dotnet-policy-service
    Contributor

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

  3. MichalStrehovsky commented on Jun 1, 2026

    @MichalStrehovsky
    MemberAuthor

    Looks like it is possible to work around: realm/SwiftLint#4901

    For dotnet specifically, we could use libSystem.*.Native.dylib/.so/.dll that are already there because we also have a matching CoreCLR VM. We'd need to research whether they have any global state that assumes only one consumer per process.

  4. added a commit that references this issue on Jun 1, 2026
  5. dotnet-policy-service commented on Jun 2, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
    See info in area-owners.md if you want to be subscribed.

  6. MichalStrehovsky commented on Jun 2, 2026

    @MichalStrehovsky
    MemberAuthor

    Looks like it is possible to work around: realm/SwiftLint#4901

    For dotnet specifically, we could use libSystem.*.Native.dylib/.so/.dll that are already there because we also have a matching CoreCLR VM. We'd need to research whether they have any global state that assumes only one consumer per process.

    There is a bunch of global state in most of the libSystem.*.Native.dylib/.so/.dll, so I don't think we'd want to try sharing these.

    For the immediate issue on Apple platforms (objc[...]: Class HashBox is implemented in both .../libSystem.Security.Cryptography.Native.Apple.dylib and .../libdotnet-aot.dylib. One of the two will be used. Which one is undefined.), I wonder if we could name these classes differently when building the static library libSystem.Security.Cryptography.Native.Apple.a (used for native AOT and single file deployments) vs when building the shared library libSystem.Security.Cryptography.Native.Apple.dylib (used otherwise).

    That way we'd at least allow CoreCLR-JIT and native AOT coexist in the same app. It would still not allow two native AOT libraries to coexist in the same app, but Apple being a limited platform is par for the course.

  7. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    Can you help me understand the urgency of this? Is this "we need it fixed for the next preview", "this needs to be fixed for .NET 11" or "it would be nice if this scenario worked" ?

  8. MichalStrehovsky commented on Jun 2, 2026

    @MichalStrehovsky
    MemberAuthor

    Partially nativeAOT'd and partially JITted dotnet CLI is a .NET 11 deliverable (https://github.com/dotnet/sdk/blob/main/src/Cli/dotnet-aot/DESIGN.md).

    Components that can be moved to AOT are moving to AOT. Looks like the first component that needs some crypto somewhere is moving to AOT and it ran into this: dotnet/sdk#54384 (comment).

    Cc @NikolaMilosavljevic @baronfel @JeremyKuhne on urgency. I think we need at least some workaround. The workaround in the linked PR likely has problems.

  9. baronfel commented on Jun 2, 2026

    @baronfel
    Member

    I think this likely blocks AOT on macos entirely for us, because our telemetry relies on crypto due to needing to submit to AppInsights via https (if nothing else).

  10. self-assigned this
    on Jun 2, 2026
  11. added this to the 11.0.0 milestone on Jun 2, 2026
  12. removed
    untriaged
    on Jun 2, 2026
  13. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    I have a pull request up over at #128902 that is essentially "we'd at least allow CoreCLR-JIT and native AOT coexist in the same app. ". Is that enough to unblock "Partially nativeAOT'd and partially JITted dotnet CLI is a .NET 11 deliverable"? Or is the CLI expected to load multiple NAOT libraries?

  14. baronfel commented on Jun 2, 2026

    @baronfel
    Member

    I think coreCLR-JIT + native AOT solves our need! We don't currently plan to load any other NAOT-compiled libraries at this time. Thanks for diving into this!

  15. am11 commented on Jun 2, 2026

    @am11
    Member

    If it’s really just those two types in pal_swiftbindings.swift, could this be a case where we’re paying for ObjC global class metadata unnecessarily? Would making them structs avoid the runtime class registration and sidestep the collision?

  16. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    If it’s really just those two types in pal_swiftbindings.swift, could this be a case where we’re paying for ObjC global class metadata unnecessarily? Would making them structs avoid the runtime class registration and sidestep the collision?

    We need to control the lifetime of these objects with Unmanaged.passRetained and friends. That's why they are classes that box structs - we need explicit lifetime management - you cannot do that with Swift structs.

  17. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    And while there are only two Classes right now, there will likely be more as we push more of Crypto on CryptoKit because Apple is no longer adding things to CommonCrypto, and CryptoKit is Swift-only.

  18. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    The X25519 one we could get rid of by making it more of a one-shot like we do with ChaCha and Aes.Gcm, but that comes at a non-trivial performance penalty. Ideally we would have more things "survive" p/invoke boundaries, not less.

    For the Digest / SHA cases, we need statefulness, so the hash types I wouldn't know how to actually get rid of entirely.

  19. jkotas commented on Jun 2, 2026

    @jkotas
    Member

    This sounds like another case where it would be beneficial to build the native runtime pieces during NAOT publish. It would allow us to build the ObjectiveC parts with library identity and prevent collisions between different apps/dylibs.

  20. am11 commented on Jun 2, 2026

    @am11
    Member

    you cannot do that with Swift structs.

    How about UnsafeMutablePointer.allocate, initialize, pass raw pointer through FFI boundary, and explicitly destroy it on demand? It would only skip (implicit) automatic reference counting mechanism.

  21. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    UnsafeMutablePointer.allocate

    This... might work actually.

  22. vcsjones commented on Jun 2, 2026

    @vcsjones
    Member

    Okay, I think we can get rid of the classes and use structs. The box happened to deal with the existentialism of the types already so it's largely just replacing one allocation pattern with another. Thanks for making re-think this through.

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

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions