Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle returned GetMainProgramHandle() not sufficient for resolving statically linked native symbols on iOS #77985

Closed
stanger opened this issue Nov 7, 2022 · 9 comments · Fixed by #78004
Milestone

Comments

@stanger
Copy link

stanger commented Nov 7, 2022

Description

Based on the discussion in #57610 we were under the impression that using GetMainProgramHandle() in tandem with NativeLibrary.SetDllImportResolver() would be a replacement for having to modify third party packages to use [DllImport("_Internal")] on iOS. So far in testing on .NET 7 this has not been the case.

I created a sample native iOS library (libsayhello) and a wrapping C# lib which handles [DllImport("libsayhello')]. Then within a sample Maui app I added Interop code to allow my lib to be statically linked into the main application at compile time, then attempted to catch the resolution of 'libsayhello' in the import resolver and return the handle returned by GetMainProgramHandle().

Reproduction Steps

A test app showing the behavior is located at stanger/ResolverTest.

Expected behavior

DllImport calls for 'libsayhello' should be able to resolved to their statically linked symbols in the main binary.

Actual behavior

DllNotFoundException

Regression?

No response

Known Workarounds

No response

Configuration

.NET: 7.0.100-rc.2.22477.23
iOS 16 and MacOS 13 (originally discovered on iOS)
Tested on x64 and ARM64

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 7, 2022
@ghost
Copy link

ghost commented Nov 7, 2022

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

Issue Details

Description

Based on the discussion in #57610 we were under the impression that using GetMainProgramHandle() in tandem with NativeLibrary.SetDllImportResolver() would be a replacement for having to modify third party packages to use [DllImport("_Internal")] on iOS. So far in testing on .NET 7 this has not been the case.

I created a sample native iOS library (libsayhello) and a wrapping C# lib which handles [DllImport("libsayhello')]. Then within a sample Maui app I added Interop code to allow my lib to be statically linked into the main application at compile time, then attempted to catch the resolution of 'libsayhello' in the import resolver and return the handle returned by GetMainProgramHandle().

Reproduction Steps

A test app showing the behavior is located at stanger/ResolverTest.

Expected behavior

DllImport calls for 'libsayhello' should be able to resolved to their statically linked symbols in the main binary.

Actual behavior

DllNotFoundException

Regression?

No response

Known Workarounds

No response

Configuration

.NET: 7.0.100-rc.2.22477.23
iOS 16 and MacOS 13 (originally discovered on iOS)
Tested on x64 and ARM64

Other information

No response

Author: stanger
Assignees: -
Labels:

area-System.Runtime.InteropServices, untriaged

Milestone: -

@jkoritzinsky

This comment was marked as outdated.

@am11
Copy link
Member

am11 commented Nov 7, 2022

Sometimes, the value returned by GetMainProgramHandle() is IntPtr.Zero

Going by:

it throws an exception instead of returning IntPtr.Zero.

@jkoritzinsky
Copy link
Member

In which case, I'm not sure what would be causing a DllNotFoundException specifically.

@stranger, can you confirm what GetMainProgramHandle is returning?

@stanger
Copy link
Author

stanger commented Nov 7, 2022

This is the value I see in the debugger is:

handle 0xfffffffffffffffe

Screenshot 2022-11-07 at 3 11 57 PM

@jkoritzinsky
Copy link
Member

Hmm. In that case, I'm not sure the issue off the top of my head.

Can you get the stack trace for where the exception is thrown from? I don't have a Mac handy at the moment.

Also, can you try adding __attribute__((visibility(default))) to the native definition of your say_hello method?

@stanger
Copy link
Author

stanger commented Nov 7, 2022

The stack trace isn't overly verbose and the inner exception was null.

System.DllNotFoundException: libsayhello at ResolverSample.MainPage.OnCounterClicked(Object sender, EventArgs e) in /Users/stanger/Workspaces/Enclave/ResolverSample/src/ResolverSample/MainPage.xaml.cs:line 17

I have also updated the sample lib definition with __attribute__((visibility("default")))

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 8, 2022
@marek-safar marek-safar added area-VM-meta-mono and removed area-System.Runtime.InteropServices untriaged New issue has not been triaged by the area owner labels Nov 8, 2022
@marek-safar marek-safar added this to the 8.0.0 milestone Nov 8, 2022
@marek-safar marek-safar added the os-ios Apple iOS label Nov 8, 2022
@ghost
Copy link

ghost commented Nov 8, 2022

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

Issue Details

Description

Based on the discussion in #57610 we were under the impression that using GetMainProgramHandle() in tandem with NativeLibrary.SetDllImportResolver() would be a replacement for having to modify third party packages to use [DllImport("_Internal")] on iOS. So far in testing on .NET 7 this has not been the case.

I created a sample native iOS library (libsayhello) and a wrapping C# lib which handles [DllImport("libsayhello')]. Then within a sample Maui app I added Interop code to allow my lib to be statically linked into the main application at compile time, then attempted to catch the resolution of 'libsayhello' in the import resolver and return the handle returned by GetMainProgramHandle().

Reproduction Steps

A test app showing the behavior is located at stanger/ResolverTest.

Expected behavior

DllImport calls for 'libsayhello' should be able to resolved to their statically linked symbols in the main binary.

Actual behavior

DllNotFoundException

Regression?

No response

Known Workarounds

No response

Configuration

.NET: 7.0.100-rc.2.22477.23
iOS 16 and MacOS 13 (originally discovered on iOS)
Tested on x64 and ARM64

Other information

No response

Author: stanger
Assignees: -
Labels:

area-VM-meta-mono, os-ios, in-pr

Milestone: 8.0.0

lambdageek pushed a commit that referenced this issue Nov 23, 2022
When user-defined resolvers return self handle via `GetMainProgramHandle()`, we lookup the cached handle in mono and fail to find one because `internal_module` is not cached in `native_library_module_map`.

Fix is to test the resolver returned handle against self; before the map lookup (which happens under the lock).

Close #77985.

* Allow interop resolvers to return self handle

* Disable new test on windows and monointerpreter

* Add new test to monointerpreter ExcludeList
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 23, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Dec 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants