Skip to content

Pass correct search path to DllImportResolver for default flags on Mono#129831

Merged
elinor-fung merged 1 commit into
dotnet:mainfrom
elinor-fung:fix/mono-dllimportresolver-searchpath
Jun 25, 2026
Merged

Pass correct search path to DllImportResolver for default flags on Mono#129831
elinor-fung merged 1 commit into
dotnet:mainfrom
elinor-fung:fix/mono-dllimportresolver-searchpath

Conversation

@elinor-fung

@elinor-fung elinor-fung commented Jun 25, 2026

Copy link
Copy Markdown
Member

On Mono, when no search flags are user-specified, lookup_pinvoke_call_impl defaults the flags to DLLIMPORTSEARCHPATH_ASSEMBLY_DIRECTORY for the built-in probing logic. netcore_resolve_with_dll_import_resolver then derived whether to report search flags to the resolver from flags != 0, so the explicit AssemblyDirectory value was passed to the resolver instead of null. The expected DllImportResolver contract is that searchPath is the attribute value, or null when neither the P/Invoke nor the assembly has the attribute.

After #114756, specifying only AssemblyDirectory meant only assembly directory. This means that when a resolver that forwards the (wrongly non-null) AssemblyDirectory to NativeLibrary.Load, it no longer does the default search (bare dlopen/LoadLibrary).

This change passes the user_specified_flags value through to the resolver invocation and uses it for "has search flags", so the resolver receives the attribute value or null for non-user-specified.

cc @dotnet/appmodel @AaronRobinsonMSFT

See #129798. We should backport to 10.

Note

This pull request was created with GitHub Copilot.

When neither the P/Invoke nor the assembly has a DefaultDllImportSearchPaths
attribute, Mono passed DllImportSearchPath.AssemblyDirectory to a registered
DllImportResolver instead of null. lookup_pinvoke_call_impl defaults the flags
to ASSEMBLY_DIRECTORY for the built-in probing logic, and
netcore_resolve_with_dll_import_resolver derived "has search flags" from
(flags != 0), so the defaulted value leaked to the resolver.

Combined with dotnet#114756 - which made an explicit AssemblyDirectory no longer fall
back to the OS search - this broke resolvers that forward the search path to
NativeLibrary.Load, such as loading iOS @rpath frameworks.

Thread the existing user_specified_flags signal through to the resolver so it
receives the attribute value, or null when none was specified, matching CoreCLR
and NativeAOT.

Fixes dotnet#129798

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Mono’s DllImportResolver searchPath reporting so that the resolver receives null when no DefaultDllImportSearchPaths attribute is present, instead of receiving Mono’s internal default (AssemblyDirectory). This brings Mono behavior in line with the documented DllImportResolver contract and with CoreCLR/NativeAOT.

Changes:

  • Mono: thread through and use user_specified_flags to decide whether to pass search flags to the resolver (instead of flags != 0).
  • Tests: add an interop test validating that the resolver observes AssemblyDirectory, System32, LegacyBehavior, and null based on attribute presence/value.
Show a summary per file
File Description
src/mono/mono/metadata/native-library.c Uses the existing user_specified_flags signal to decide whether the resolver should receive a non-null searchPath.
src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs Adds coverage to assert the resolver’s observed searchPath matches DefaultDllImportSearchPaths (or null when absent).

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment thread src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs
Comment thread src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs

@AaronRobinsonMSFT AaronRobinsonMSFT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@elinor-fung elinor-fung merged commit d08d3ac into dotnet:main Jun 25, 2026
136 checks passed
@elinor-fung

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants