Skip to content

[Linux] DllImport should invoke with RTLD_GLOBAL flag #11901

Closed

Description

@ghost

The CoreCLR behavior seems to be behaving as if library is loaded as RTLD_LOCAL despite the fact that the actual code resolves them with RTLD_LAZY flag when using dlopen. This causes some difficulty of using CoreCLR for P/Invoke code that since current P/Invoke API currently cannot access global library variables (at least cleanly), we would normally use dlopen/dlsym to access the global library variables. The problem is that it behaves in a way that it seems as if there are 2 instances of the same library. It has persisted for more than 2 years now.

I've written a minimal reproduction code that can be tested on Mono and CoreCLR:
https://github.com/BlackCentipede/PinvokeBug

To test, simply run dotnet run and clang -std=c99 -shared -fPIC -olibTest.so Test.c && mcs Program.cs && mono Program.exe under TestPInvoke directory.

Dotnet Core would print the following:

dotnetcorebug

And Mono Runtime would print the following:
mono

This is tested on Dotnet Core 2.2.102 and Mono 5.16.0.

Activity

  1. jkotas commented on Jan 26, 2019

    @jkotas
    Member

    This issue was discussed in dotnet/coreclr#18628 .

    RTLD_GLOBAL will make some libraries work, but it will break others. There is no one right default. We have chosen RTLD_LOCAL to be the default since others (e.g. Python) made the same choice. Also, it matches the behavior on Windows - there is not even concept of global on Windows.

    We have introduced a callback in .NET Core 3.0 that allows customizing how the libraries are loaded for DllImport: NativeLibrary.SetDllImportResolver. More details are in https://github.com/dotnet/corefx/issues/32015.

    @swaroop-sridhar Do you happen to have a sample how one can use NativeLibrary.SetDllImportResolver to load with RTLD_GLOBAL ?

  2. tannergooding commented on Jan 26, 2019

    @tannergooding
    Member

    Also, it matches the behavior on Windows - there is not even concept of global on Windows.

    @jkotas, this is not the case.

    LoadLibrary (and LoadLibraryEx, by extension) ref counts modules on a per-process basis and Windows (see https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibraryw#remarks for more details), using the same code will return (as it does on Mono for dlopen):

    1
    2
    3
    

    Here is an end-to-end sample showing the behavior on Windows: repro.zip

  3. tannergooding commented on Jan 26, 2019

    @tannergooding
    Member

    Additionally, when using LoadLibraryEx, it is possible to explicitly load multiple instances of a given module by specifying it is a datafile or image resource. In this scenario, it does not call DllMain and you must do that (and similar setup) yourself (this is similar to the LOCAL loading state in dlopen).

  4. jkotas commented on Jan 26, 2019

    @jkotas
    Member

    ref counts modules on a per-process basis

    @tannergooding This has not much to do with refcounting or loading multiple instance of the same .dlls.

    On Unix, there is basically a process global hashtable of all exported symbols. When a shared library is loaded using RTLD_GLOBAL, its symbols are added to this hashtable and other shared libraries can resolve them, without specifying where the symbol should come from in its import tables. There is no such a thing on Windows.

  5. swaroop-sridhar commented on Jan 28, 2019

    @swaroop-sridhar
    Contributor

    Do you happen to have a sample how one can use NativeLibrary.SetDllImportResolver to load with RTLD_GLOBAL ?

    Here's a sample:

    using System.Runtime.InteropServices;
    
    void Test()
    {
        DllImportResolver resolver =
            (string libraryName, Assembly asm, DllImportSearchPath? dllImportSearchPath) =>
                 Interop.Libdl.dlopen(libraryName, RTLD_GLOBAL);
    
        NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly();, resolver);
    
       // .. Then try the pInvoke ... 
    }
  6. ghost closed this as completedon Jan 29, 2019
  7. transferred this issue fromdotnet/coreclron Jan 31, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 14, 2020
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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions