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

Try LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_SEARCH_USER_DIRS for DLL loading on Windows #47775

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mkitti
Copy link
Contributor

@mkitti mkitti commented Dec 2, 2022

Currently, we use LOAD_WITH_ALTERED_SEARCH_PATH to load DLL libraries on Windows. With this pull request, if loading with the above fails, we use LOAD_LIBRARY_SEARCH_USER_DIRS.

This is a minimalist version of the idea proposed on Discourse. Rather than replacing LOAD_WITH_ALTERED_SEARCH_PATH with LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, we only use LOAD_LIBRARY_SEARCH_USER_DIRS if loading with ``LOAD_WITH_ALTERED_SEARCH_PATH` fails.

This allows the user to manually build a search list of directories via AddDllDirectory.

@ccall "kernel32".AddDllDirectory(library::Cwstring)::Ptr{Nothing}

https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw

@mkitti
Copy link
Contributor Author

mkitti commented Dec 2, 2022

The use of SetDefaultDllDirectories has the potential to make LoadLibrary* calls with LOAD_WITH_ALTERED_SEARCH_PATH fail.

For example,

julia> @ccall "kernel32".SetDefaultDllDirectories(0x1000::UInt32)::UInt8
0x01

julia> using Random

julia> MersenneTwister(1234)
ERROR: could not load library "libdSFMT"
The parameter is incorrect.

This pull request does not use SetDefaultDllDirectories. Rather it allows one to use AddDllDirectory to resolve the situation if it is used.

With this pull request, one can recover with AddDllDirectory in the following way.

julia> @ccall "kernel32".SetDefaultDllDirectories(0x1000::UInt32)::UInt8 # 0x1000 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
0x01

julia> using Random

julia> MersenneTwister(1234)
ERROR: could not load library "libdSFMT"
The parameter is incorrect.
Stacktrace:
 [1] dsfmt_init_by_array
   @ C:\Users\kittisopikulm\.julia\juliaup\julia-1.8.3+0.x64\share\julia\stdlib\v1.8\Random\src\DSFMT.jl:69 [inlined]
 [2] seed!(r::MersenneTwister, seed::Vector{UInt32})
   @ Random C:\Users\kittisopikulm\.julia\juliaup\julia-1.8.3+0.x64\share\julia\stdlib\v1.8\Random\src\RNGs.jl:313
 [3] seed!
   @ C:\Users\kittisopikulm\.julia\juliaup\julia-1.8.3+0.x64\share\julia\stdlib\v1.8\Random\src\RNGs.jl:321 [inlined]
 [4] MersenneTwister(seed::Int64)
   @ Random C:\Users\kittisopikulm\.julia\juliaup\julia-1.8.3+0.x64\share\julia\stdlib\v1.8\Random\src\RNGs.jl:113
 [5] top-level scope
   @ REPL[11]:1

julia> @ccall "kernel32".AddDllDirectory(raw"D:\repos\julia\usr\bin"::Cwstring)::Ptr{Nothing}
Ptr{Nothing} @0x000001b9dc2c5ec0

julia> MersenneTwister(1234)
MersenneTwister(1234)

This overall will permit Julia to be embedded in a wider range of applications where SetDefaultDllDirectories may be in use.

@mkitti
Copy link
Contributor Author

mkitti commented Dec 2, 2022

cc: @vtjnash

src/dlload.c Outdated Show resolved Hide resolved
mkitti and others added 2 commits December 12, 2022 19:40
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
@mkitti mkitti changed the title Add LOAD_LIBRARY_SEARCH_USER_DIRS for DLL loading Try LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_SEARCH_USER_DIRS for DLL loading on Windows Jan 5, 2023
@topolarity
Copy link
Member

Some notes:

  • This change would have been useful for resolving segfault with Matlab 2022a on Windows JuliaInterop/MATLAB.jl#200 (comment) without changing the PATH
  • Libdl.dlopen() currently fails for SuiteSparse_jll (and other stdlib JLL's) on Windows when Julia is used as a library, since Julia's bindir does not end up anywhere on the DLL search path
  • Our use of LOAD_WITH_ALTERED_SEARCH_PATH with relative paths is technically undefined behavior: "If this value is used and lpFileName specifies a relative path, the behavior is undefined." In practice, it's probably benign though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
system:windows Affects only Windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants