Skip to content

Native Library loading IOS Frameworks in .NET 10 #129798

Closed

Description

@smoogipoo

Apple platform

iOS

Framework version

net10.0-*

Affected platform version

SDK 10.0.300, Manifest 36.1.69/10.0.100

Description

Previous issue on this: dotnet/macios#24266
Relevant issue: realm/realm-dotnet#3711

We're trying to update our app from .NET 8 to .NET 10 and encountering issues because our apps can no longer load their native libraries.

The breaking change is documented here: https://learn.microsoft.com/en-us/dotnet/core/compatibility/interop/10.0/native-library-search

I could be wrong, but it looks to be tracked back to this PR on dotnet/runtime: #115236

The breaking (no longer functional) code looks like this:

if (OperatingSystem.IsIOS())
    NativeLibrary.SetDllImportResolver(typeof(VeldridSpirvNative).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/veldrid-spirv.framework/veldrid-spirv", assembly, path));

With the suggested change being (as far as I understand):

if (OperatingSystem.IsIOS())
    NativeLibrary.SetDllImportResolver(typeof(VeldridSpirvNative).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/veldrid-spirv.framework/veldrid-spirv", assembly, path | DllImportSearchPath.ApplicationDirectory));

Before I go and update about 5 different projects and begin a cycle of dependency updates, I want to ask whether this is really intended behaviour?

Steps to Reproduce

I was going to provide some steps here, but I'm uncovering even more issues that makes this very annoying to create a reproduction for. It's just a question anyway.

Did you find any workaround?

As mentioned in the description.

Relevant logs

No response

Activity

  1. rolfbjarne commented on Jun 24, 2026

    @rolfbjarne
    Member

    Thanks for the report!

    I can reproduce, here's a sample app: ReproApp-6b5f1fa.zip

    Copilot's analysis

    Analysis: #129798 — native framework @rpath loading broken in .NET 10

    Summary

    Loading an embedded iOS native framework via
    NativeLibrary.Load("@rpath/<Name>.framework/<Name>", assembly, searchPath) from a
    DllImportResolver worked on .NET 8/9 but throws DllNotFoundException on .NET 10.

    The root cause is not in dlopen or in dyld's @rpath resolution. dlopen is still
    called and dyld still resolves @rpath correctly. The regression is in CoreCLR's managed
    native-library search algorithm
    (coreclr/vm/nativelibrary.cpp,
    LoadNativeLibraryBySearch): for the default / AssemblyDirectory search semantics it no
    longer hands the raw "@rpath/…" string to dlopen, so dyld never gets a chance to resolve it.

    Related:

    Reproduction

    A self-contained reproduction was built in /Users/rolf/test/bugs/issue25792:

    • native/mynative.c — one function int my_native_add(int, int).
    • native/build-framework.sh — builds a fat (x86_64 + arm64) iOS-simulator
      MyNative.framework whose install name is @rpath/MyNative.framework/MyNative (exactly like
      the real-world SDL3 / veldrid-spirv / realm frameworks in the issue).
    • ReproApp/ — a net10.0-ios app that embeds the framework via:
      <NativeReference Include="MyNative.framework">
        <Kind>Framework</Kind>
      </NativeReference>
      macios embeds it at ReproApp.app/Frameworks/MyNative.framework/MyNative and adds an
      @executable_path/Frameworks LC_RPATH, so @rpath/MyNative.framework/MyNative resolves to the
      embedded framework.
    • ReproApp/NativeRepro.cs — runs probes A–F at startup (from AppDelegate.FinishedLaunching).

    Environment: macOS (Intel/x86_64 host), .NET SDK 10.0.107, Microsoft.iOS workload
    26.2.10233/10.0.100, iPhone 15 Pro simulator (iOS 26.2). Built with
    dotnet build -c Debug -r iossimulator-x64, installed/launched with simctl, output captured
    from the unified log.

    Measured results (on-device, iOS simulator, .NET 10)

    Probe Call Result
    (A) NativeLibrary.Load("@rpath/…", asm, searchPath) (resolver) FAILED DllNotFoundException
    (B) NativeLibrary.Load("@rpath/…", asm, ApplicationDirectory) SUCCESS
    (C) NativeLibrary.Load("@rpath/…", asm, AssemblyDirectory) FAILED DllNotFoundException
    (D) NativeLibrary.Load("@rpath/…") (single-arg, direct) SUCCESS
    (E) raw dlopen("@rpath/MyNative.framework/MyNative", RTLD_NOW) SUCCESS
    (F) raw dlopen("<app>/Frameworks/MyNative.framework/MyNative") SUCCESS
    • (A) is the exact .NET 8/9 pattern — it regressed on .NET 10.
    • (E) is decisive: the OS loader resolves @rpath fine. dlopen/dyld are not the problem.

    Mechanism (code walk-through)

    Relevant function: LoadNativeLibraryBySearch in src/coreclr/vm/nativelibrary.cpp
    (release/10.0). On Apple/Unix, LocalLoadLibraryHelper(name) → PAL_LoadLibraryDirect(name) →
    dlopen(name, RTLD_LAZY).

    For libName = "@rpath/MyNative.framework/MyNative":

    1. libNameIsRelativePath = Path::IsRelative(libName) is true — the string does not start
      with /. (dyld's @rpath/@executable_path/@loader_path prefixes are not understood by
      CoreCLR's path classifier; they are treated as ordinary relative paths.)

    2. For each name variation the loop tries, in order:
      a. LoadFromNativeDllSearchDirectories(name) — prepends every
      NATIVE_DLL_SEARCH_DIRECTORIES entry: dlopen("<dir>/@rpath/MyNative.framework/MyNative").
      These are nonsensical paths (a literal @rpath path segment) and always fail.
      b. if (!libNameIsRelativePath) { LocalLoadLibraryHelper(name, …) } — a bare
      dlopen("@rpath/…"). Skipped here because the name is classified as relative.
      c. else if (callingAssembly != nullptr && searchAssemblyDirectory)
      → LoadFromPInvokeAssemblyDirectory(...): dlopen("<assemblyDir>/@rpath/…") — for a
      bundled iOS assembly the assembly directory is the .app bundle dir, so this is again a
      nonsensical concatenated path and fails.
      d. Final fallback:

      if (!userSpecifiedSearchFlags || !searchAssemblyDirectory || dllImportSearchPathFlags != 0)
          hmod = LocalLoadLibraryHelper(currLibNameVariation, dllImportSearchPathFlags); // bare dlopen("@rpath/…")

      This bare dlopen("@rpath/…") is the only attempt that lets dyld resolve @rpath
      (proven by probe E). Whether it runs depends entirely on the gate.

    3. Evaluating the gate:

      • Default P/Invoke semantics / AssemblyDirectory: userSpecifiedSearchFlags == true,
        searchAssemblyDirectory == true, dllImportSearchPathFlags == 0 → gate is false →
        bare dlopen is skipped → DllNotFoundException. This is probes (A) and (C).
      • ApplicationDirectory: maps to a non-zero dllImportSearchPathFlags (it is not the
        assembly-directory bit) → dllImportSearchPathFlags != 0 → gate is true → bare
        dlopen("@rpath/…") runs → dyld resolves it → SUCCESS. This is probe (B).
    4. The single-argument NativeLibrary.Load(path) (probe D) bypasses the search entirely:
      it is a direct LocalLoadLibraryHelper(path, LOAD_WITH_ALTERED_SEARCH_PATH) = a bare
      dlopen, so it also succeeds.

    Key takeaways

    • dlopen is still called. It is not bypassed, and dyld's @rpath handling is intact.
    • The regression is that CoreCLR feeds dlopen directory-prefixed variants
      (<dir>/@rpath/…, which can never succeed) and skips the one bare-string dlopen call
      that dyld could resolve — unless a non-AssemblyDirectory search flag is set.
    • DllImportSearchPath.ApplicationDirectory "fixes" it only as a side effect of changing
      control flow
      (it makes dllImportSearchPathFlags != 0), not because it points anywhere near
      the framework. Any code relying on this is relying on a fallthrough, not on documented
      ApplicationDirectory semantics.

    Tie-in to #115236

    The PR's stated change: stop always adding the app directory (and single-file extraction
    directory) to NATIVE_DLL_SEARCH_DIRECTORIES, and treat a bundled assembly's directory as the
    bundle/app directory. iOS apps are single-file/bundled, so they are directly affected.

    Relevant hunks:

    • hostpolicy/deps_resolver.cpp: removed the block that, for single-file bundles, added
      bundle->base_path() (and the extraction path) to the native search directories.
    • vm/nativelibrary.cpp LoadFromPInvokeAssemblyDirectory: now resolves a bundled assembly's
      path to the bundle path (previously empty → early return NULL).

    On .NET 8/9 the bundle/app directory was always present in the probe list and the control flow
    reached the bare-string dlopen, so the @rpath/… form resolved. The .NET 10 changes shifted
    that flow so the default / AssemblyDirectory path no longer reaches the bare dlopen,
    producing the regression.

    Recommended fixes / workarounds for app authors

    1. Documented workaround: OR-in DllImportSearchPath.ApplicationDirectory in the resolver:
      NativeLibrary.Load("@rpath/Foo.framework/Foo", assembly, (searchPath ?? 0) | DllImportSearchPath.ApplicationDirectory);
    2. Equivalent alternative: use the single-argument NativeLibrary.Load("@rpath/Foo.framework/Foo")
      (direct dlopen).
    3. Or load by resolved absolute path:
      Path.Combine(AppContext.BaseDirectory, "Frameworks/Foo.framework/Foo") (probe F).

    Whether the .NET 10 behavior is intended for Apple platforms (where @rpath/@loader_path
    are first-class loader concepts, unlike on Linux) is the open question raised in the issue.

    TLDR; it seems NativeLibrary.Load ("@rpath/...", assembly) will prepend search paths, end up trying to load non-sensical paths like "/search/path@rpath/...").

    This seems like an issue in dotnet/runtime, so I'm moving this issue there. IMHO "@rpath/..." paths should be treated like absolute paths and loaded as-is without prepending any search paths.

    As a workaround, the simplest solution is to call NativeLibrary.Load ("@rpath/..") (i.e. no second assembly argument).

  2. transferred this issue fromdotnet/macioson Jun 24, 2026
  3. added
    untriaged
    on Jun 24, 2026
  4. dotnet-policy-service commented on Jun 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @steveisok, @akoeplinger
    See info in area-owners.md if you want to be subscribed.

  5. dotnet-policy-service commented on Jun 24, 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.

  6. AaronRobinsonMSFT commented on Jun 24, 2026

    @AaronRobinsonMSFT
    Member

    @elinor-fung Can you please confirm our intent here.

  7. elinor-fung commented on Jun 24, 2026

    @elinor-fung
    Member

    This is a regression from https://learn.microsoft.com/dotnet/core/compatibility/interop/10.0/native-library-search (#114756). This specifically affects Mono resolving via DllImportResolver with default search path behaviour (so no DllImportSearchPath specified on the p/invoke or the assembly). When calling the DllImportResolver when no search paths were explicitly specified, Mono passes DllImportSearchPath.AssemblyDirectory instead of null - #114756 missed updating that. That means the NativeLibrary.Load("...", assembly, path) call in your resolver ends up having only DllImportSearchPath.AssemblyDirectory specified and we don't do the default load.

    Before I go and update about 5 different projects and begin a cycle of dependency updates, I want to ask whether this is really intended behaviour?

    Decidedly not intended. Thanks for reporting this.

  8. added this to the 10.0.x milestone on Jun 24, 2026
  9. removed
    untriaged
    on Jun 24, 2026
  10. MichaelLHerman commented on Aug 20, 2026

    @MichaelLHerman

    which release version of .NET MAUI will have the runtime with the fix? Is this fixed in MAUI 10.0.100?

  11. akoeplinger commented on Aug 21, 2026

    @akoeplinger
    Member

    @MichaelLHerman the fix from #129862 shipped with 10.0.11 of the runtime which was released a short while ago. You should be able to get it by updating to the latest version.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions