oneclick / rubyinstaller2

MSYS2 based RubyInstaller for Windows
https://rubyinstaller.org
BSD 3-Clause "New" or "Revised" License
646 stars 248 forks source link

Fiddle library load error "Fiddle::DLError: The parameter is incorrect." #274

Closed larskanis closed 2 years ago

larskanis commented 2 years ago

What problems are you experiencing?

Fiddle is unable to load DLLs from a path set by RubyInstaller::Runtime.add_dll_directory, when fiddle is built with libdl from the MSYS2-package mingw-w64-x86_64-dlfcn. Only the x64-mingw32 platform is affected. Neither the x86 nor the UCRT build suffer from this issue. It fails with:

  1) Error:
TestModule#test_add_remove_dll_directory:
Fiddle::DLError: "libtest.dll": The parameter is incorrect.
    C:/Ruby30-x64/lib/ruby/3.0.0/fiddle.rb:61:in `initialize'

Debugging the DLL loader gives the following output:

warning: 12f0:1004 @ 16767265 - LdrLoadDll - ENTER: DLL name: libtest.dll
warning: 12f0:1004 @ 16767265 - LdrpLoadDllInternal - ENTER: DLL name: libtest.dll
warning: 12f0:1004 @ 16767265 - LdrpFindKnownDll - ENTER: DLL name: libtest.dll
warning: 12f0:1004 @ 16767281 - LdrpFindKnownDll - RETURN: Status: 0xc0000135
warning: 12f0:1004 @ 16767281 - LdrpSearchPath - ENTER: DLL name: libtest.dll
warning: 12f0:1004 @ 16767281 - LdrpComputeLazyDllPath - ERROR: Lazy DLL search path computation failed with status: 0xc000000d.
warning: 12f0:1004 @ 16767296 - LdrpSearchPath - RETURN: Status: 0xc000000d
warning: 12f0:1004 @ 16767296 - LdrpProcessWork - ERROR: Unable to load DLL: "libtest.dll", Parent Module: "(null)", Status: 0xc000000d
warning: 12f0:1004 @ 16767312 - LdrpLoadDllInternal - RETURN: Status: 0xc000000d
warning: 12f0:1004 @ 16767312 - LdrLoadDll - RETURN: Status: 0xc000000d

... while a successful load looks like so:

warning: 1224:0ea4 @ 16922781 - LdrLoadDll - ENTER: DLL name: libtest.dll
warning: 1224:0ea4 @ 16922781 - LdrpLoadDllInternal - ENTER: DLL name: libtest.dll
warning: 1224:0ea4 @ 16922796 - LdrpFindKnownDll - ENTER: DLL name: libtest.dll
warning: 1224:0ea4 @ 16922796 - LdrpFindKnownDll - RETURN: Status: 0xc0000135
warning: 1224:0ea4 @ 16922796 - LdrpSearchPath - ENTER: DLL name: libtest.dll
warning: 1224:0ea4 @ 16922812 - LdrpComputeLazyDllPath - INFO: DLL search path computed: C:\Ruby30-x64\bin;C:/Users/Administrator/rubyinstaller2/test/helper;C:/msys64/mingw64/bin;C:\Windows\SYSTEM32
warning: 1224:0ea4 @ 16922828 - LdrpResolveDllName - ENTER: DLL name: C:\Ruby30-x64\bin\libtest.dll
warning: 1224:0ea4 @ 16922828 - LdrpResolveDllName - RETURN: Status: 0xc0000135
warning: 1224:0ea4 @ 16922828 - LdrpResolveDllName - ENTER: DLL name: C:/Users/Administrator/rubyinstaller2/test/helper\libtest.dll
warning: 1224:0ea4 @ 16922843 - LdrpResolveDllName - RETURN: Status: 0x00000000
warning: 1224:0ea4 @ 16922843 - LdrpSearchPath - RETURN: Status: 0x00000000
warning: 1224:0ea4 @ 16922859 - LdrpMapViewOfSection - ENTER: DLL name: C:\Users\Administrator\rubyinstaller2\test\helper\libtest.dll
warning: 1224:0ea4 @ 16922875 - LdrpMapViewOfSection - RETURN: Status: 0x00000000
warning: 1224:0ea4 @ 16922875 - LdrpFindDllActivationContext - INFO: Probing for the manifest of DLL "C:\Users\Administrator\rubyinstaller2\test\helper\libtest.dll" failed with status 0xc0000089
warning: 1224:0ea4 @ 16922890 - LdrpGetProcedureAddress - INFO: Locating procedure "RtlDeleteCriticalSection" by name
warning: 1224:0ea4 @ 16922890 - LdrpGetProcedureAddress - INFO: Locating procedure "RtlEnterCriticalSection" by name
warning: 1224:0ea4 @ 16922906 - LdrpGetProcedureAddress - INFO: Locating procedure "RtlInitializeCriticalSection" by name
warning: 1224:0ea4 @ 16922906 - LdrpGetProcedureAddress - INFO: Locating procedure "RtlLeaveCriticalSection" by name
warning: 1224:0ea4 @ 16922921 - LdrpInitializeNode - INFO: Calling init routine 00007FFE6D871350 for DLL "C:\Users\Administrator\rubyinstaller2\test\helper\libtest.dll"
warning: 1224:0ea4 @ 16922921 - LdrpCallTlsInitializers - INFO: Calling TLS callback 00007FFE6D8714C0 for DLL "C:\Users\Administrator\rubyinstaller2\test\helper\libtest.dll" at 00007FFE6D870000
warning: 1224:0ea4 @ 16922937 - LdrpCallTlsInitializers - INFO: Calling TLS callback 00007FFE6D871490 for DLL "C:\Users\Administrator\rubyinstaller2\test\helper\libtest.dll" at 00007FFE6D870000
warning: 1224:0ea4 @ 16922953 - LdrpLoadDllInternal - RETURN: Status: 0x00000000
warning: 1224:0ea4 @ 16922953 - LdrLoadDll - RETURN: Status: 0x00000000

The only similar issue found by google is this one in our same repository: https://github.com/oneclick/rubyinstaller2/issues/243 So obviously the .net DLL loading mechanism is similar to the loading mechanism of libdl, since it fails with the same error in a similar situation.

Steps to reproduce

Build fiddle with package mingw-w64-x86_64-dlfcn installed, so that fiddle is linked against libdl and the DLL is loaded through the DL library instead of direct Windows API calls (LoadLibrary etc).

What's the output from ridk version?

---
ruby:
  path: C:/Ruby30-x64
  version: 3.0.4
  platform: x64-mingw32
ruby_installer:
  package_version: 3.0.4-1
  git_commit: 9ed8d2e
msys2:
  path: C:\msys64
cc: gcc (Rev10, Built by MSYS2 project) 11.2.0
sh: GNU bash, version 5.1.16(1)-release (x86_64-pc-msys)
os: Microsoft Windows [Version 10.0.14393]
larskanis commented 2 years ago

This issue is fixed by https://github.com/oneclick/rubyinstaller2-packages/commit/08db12ba39bea1b72080c118b19107b6a1bcd325 .

MSP-Greg commented 2 years ago

Neither the x86 nor the UCRT build suffer from this issue.

Thanks for working on this. Would you consider this a MSYS2 bug, a Ruby bug, a dlfcn-win32 bug, or none of the above?

larskanis commented 2 years ago

This is pretty sure another instance of this issue https://github.com/ffi/ffi/pull/553 , but now in dlfcn. It uses LOAD_WITH_ALTERED_SEARCH_PATH with relative paths, where the behavior is undefined.

MSP-Greg commented 2 years ago

Thanks.

Currently the dlfcn package is installed by setup-ruby when using any ucrt or a mingw build with windows-2022 image.

Question I should have asked, is there any benefit to using dlfcn with ucrt64 builds?

Or, should setup-ruby install dlfcn at all?

Note that all packages required by Ruby are installed when using windows-2022.

MSP-Greg commented 2 years ago

Not sure if you're on vacation. Would you mind if I opened an issue in dlfcn linking to your explanation in

https://github.com/ffi/ffi/pull/553/commits/b3a0a876067c976ccc659985fed7cb70b60dbb0d

larskanis commented 2 years ago

Yes, @MSP-Greg that's nice! Thank you for your help!

larskanis commented 2 years ago

Discussion about the change to dlfcn-win32 is here: https://github.com/dlfcn-win32/dlfcn-win32/issues/104