Skip to content

Conversation

BigWingBeat
Copy link
Contributor

CppSharp currently DllImports the dlopen and dlsym functions from the library "dl". This is incorrect however, as described in dotnet/runtime#53291 (comment). Due to the way that DllImport searches for libraries, described here, specifying the library name as just "dl" causes it to find libdl.so, which comes from the libc6-dev package, but not the correct version of the library, libdl.so.2. The reason this crashes is because libc6-dev is not guranteed to be installed everywhere, so on Linux installations where it isn't, such as mine, the DllImport fails and throws an exception.

This PR fixes the crash by naming the correct version of the library in the DllImport, which should always exist.

@BigWingBeat
Copy link
Contributor Author

@dotnet-policy-service agree

@tritao
Copy link
Collaborator

tritao commented Sep 24, 2023

Hello @Pixelstormer, this is failing the macOS build, I guess we need to either ifdef or do the lookup at runtime.

Can you look into it?

@BigWingBeat
Copy link
Contributor Author

So, the problem is that on macOS, libraries use the .dylib or .bundle extensions, and not the .so extension like on Linux. Unfortunately, DllImport has no way of specifying multiple possible library names, and the recommended workaround of NativeLibrary.SetDllImportResolver isn't available on netstandard. I've pushed a fix that just chooses between Linux and macOS versions of the functions, similarly to how it was already choosing between Unix and Windows versions. This isn't the nicest fix, given that the functions are completely identical besides the name of the library they're imported from, but it does work.

On an unrelated note, I noticed that the PlatformID enum that was being used to distinguish between platforms apparently shouldn't be used any more, as amongst other things, it can't actually distinguish between macOS and Linux, instead grouping them both together as Unix. To solve this, I replaced it with the recommended alternative of OSPlatform. There's several other places in the codebase that also use PlatformID, but changing those are out of the scope of this PR.

@tritao
Copy link
Collaborator

tritao commented Sep 24, 2023

Cool, that makes sense. Thanks for the contribution.

@tritao tritao merged commit 25d6325 into mono:main Sep 24, 2023
JordanL8 pushed a commit to MoonCollider/CppSharp that referenced this pull request Oct 2, 2023
)

* DllImport correct version of libdl

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants