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

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 <[email protected]>
@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

@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)

@github-project-automation github-project-automation Bot moved this to Done in AppModel Jun 25, 2026
@elinor-fung elinor-fung added this to the 11.0.0 milestone Jun 25, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jun 26, 2026
JulieLeeMSFT pushed a commit that referenced this pull request Jun 29, 2026
…ult flags on Mono (#129862)

Backport of #129831 to release/10.0

/cc @elinor-fung

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Customer reported in #129798.

On Mono, when no search flags are user-specified, the runtime ends up
passing an explicit `DllImportSeachPath.AssemblyDirectory` value was
passed to any registered `DllImportResolver` instead of null. The
expected DllImportResolver contract is that search path is the attribute
value, or null when neither the P/Invoke nor the assembly has the
attribute.

Before #114756, specifying only
`DllImportSearchPath.AssemblyDirectory` would still do the default
search, so the Mono behaviour of explicitly passing `AssemblyDirectory`
instead of null didn't really matter. After that change, specifying only
`DllImportSeachPath.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.

This change propagates the tracking of whether flags were user-specified
to the resolver invocation, so the resolver is given the attribute value
or null for non-user-specified.

Fixes #129798

## Regression

- [x] Yes
- [ ] No

#114756

## Testing

Automated tests added.

## Risk

Low. Mono-only, propagating a boolean value correctly.

Co-authored-by: Elinor Fung <[email protected]>
Co-authored-by: Copilot <[email protected]>
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…no (#129831)

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.

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants