Repository navigation
Description
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.
-
Host already has it (dynamically): the normal
dotnethost loads the shared framework, which includeslibSystem.Security.Cryptography.Native.Apple.dylib. That dylib containspal_swiftbindings.o, whose Swift interop registers ObjC classes (HashBox,X25519KeyBox) with the process-wide ObjC runtime at load time. -
NativeAOT bakes a second copy in (statically): in
Microsoft.NETCore.Native.Unix.targets(ILCompiler11.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 ourNativeLib=Sharedoutput (libdotnet-aot.dylib) statically links the.aversion of the same crypto library, including the samepal_swiftbindings.oand itsHashBox/X25519KeyBoxclasses. -
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)
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Jun 1, 2026
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.
MichalStrehovsky commentedon Jun 1, 2026
Looks like it is possible to work around: realm/SwiftLint#4901
For
dotnetspecifically, 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.Reference dotnet/runtime#128867 in Apple crypto AOT workaround
dotnet-policy-service commentedon Jun 2, 2026
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.
MichalStrehovsky commentedon Jun 2, 2026
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 librarylibSystem.Security.Cryptography.Native.Apple.a(used for native AOT and single file deployments) vs when building the shared librarylibSystem.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.
vcsjones commentedon Jun 2, 2026
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" ?
MichalStrehovsky commentedon Jun 2, 2026
Partially nativeAOT'd and partially JITted
dotnetCLI 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.
baronfel commentedon Jun 2, 2026
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).
vcsjones commentedon Jun 2, 2026
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?
baronfel commentedon Jun 2, 2026
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!
am11 commentedon Jun 2, 2026
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?vcsjones commentedon Jun 2, 2026
We need to control the lifetime of these objects with
Unmanaged.passRetainedand friends. That's why they are classes that box structs - we need explicit lifetime management - you cannot do that with Swift structs.vcsjones commentedon Jun 2, 2026
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.
vcsjones commentedon Jun 2, 2026
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.
jkotas commentedon Jun 2, 2026
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.
am11 commentedon Jun 2, 2026
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.vcsjones commentedon Jun 2, 2026
This... might work actually.
vcsjones commentedon Jun 2, 2026
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.