Skip to content

Commit 0fe3bde

Browse files
committed
Perform the same treatment to dlopen() for consistency's sake
This changes `dlopen()` to mimic `dlsym()` so that a negative result returns `nothing`. While not necessary in the same way as in the `dlsym()` case (there are no valid `NULL` `dlopen()` results) the consistency is worth it.
1 parent 6369ee9 commit 0fe3bde

File tree

11 files changed

+46
-54
lines changed

11 files changed

+46
-54
lines changed

src/dlload.c

+1-11
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ JL_DLLEXPORT int jl_dlclose(void *handle)
117117
#endif
118118
}
119119

120-
static void *jl_load_dynamic_library_(const char *modname, unsigned flags, int throw_err)
120+
JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags, int throw_err)
121121
{
122122
char path[PATHBUF];
123123
int i;
@@ -202,16 +202,6 @@ static void *jl_load_dynamic_library_(const char *modname, unsigned flags, int t
202202
return handle;
203203
}
204204

205-
JL_DLLEXPORT void *jl_load_dynamic_library_e(const char *modname, unsigned flags)
206-
{
207-
return jl_load_dynamic_library_(modname, flags, 0);
208-
}
209-
210-
JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags)
211-
{
212-
return jl_load_dynamic_library_(modname, flags, 1);
213-
}
214-
215205
JL_DLLEXPORT int jl_dlsym(void *handle, const char *symbol, void ** value, int throw_err)
216206
{
217207
int symbol_found = 0;

src/init.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ void _julia_init(JL_IMAGE_SEARCH rel)
643643
}
644644
jl_arr_xtralloc_limit = total_mem / 100; // Extra allocation limited to 1% of total RAM
645645
jl_find_stack_bottom();
646-
jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT);
646+
jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT, 0);
647647
#ifdef _OS_WINDOWS_
648648
jl_ntdll_handle = jl_dlopen("ntdll.dll", 0); // bypass julia's pathchecking for system dlls
649649
jl_kernel32_handle = jl_dlopen("kernel32.dll", 0);

src/jitlayers.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ static uint64_t resolve_atomic(const char *name)
247247
#elif defined(_OS_WINDOWS_)
248248
static const char *const libatomic = "libatomic-1.dll";
249249
#endif
250-
static void *atomic_hdl = jl_load_dynamic_library_e(libatomic,
251-
JL_RTLD_LOCAL);
250+
static void *atomic_hdl = jl_load_dynamic_library(libatomic,
251+
JL_RTLD_LOCAL, 0);
252252
static const char *const atomic_prefix = "__atomic_";
253253
if (!atomic_hdl)
254254
return 0;

src/jlapi.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ JL_DLLEXPORT void jl_init(void)
5858
{
5959
char *libbindir = NULL;
6060
#ifdef _OS_WINDOWS_
61-
void *hdl = (void*)jl_load_dynamic_library_e(NULL, JL_RTLD_DEFAULT);
61+
void *hdl = (void*)jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT, 0);
6262
if (hdl) {
6363
char *to_free = (char*)jl_pathname_for_handle(hdl);
6464
if (to_free) {

src/julia.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,7 @@ enum JL_RTLD_CONSTANT {
14981498
#define JL_RTLD_DEFAULT (JL_RTLD_LAZY | JL_RTLD_DEEPBIND)
14991499

15001500
typedef void *jl_uv_libhandle; // compatible with dlopen (void*) / LoadLibrary (HMODULE)
1501-
JL_DLLEXPORT jl_uv_libhandle jl_load_dynamic_library(const char *fname, unsigned flags);
1502-
JL_DLLEXPORT jl_uv_libhandle jl_load_dynamic_library_e(const char *fname, unsigned flags);
1501+
JL_DLLEXPORT jl_uv_libhandle jl_load_dynamic_library(const char *fname, unsigned flags, int throw_err);
15031502
JL_DLLEXPORT jl_uv_libhandle jl_dlopen(const char *filename, unsigned flags);
15041503
JL_DLLEXPORT int jl_dlclose(jl_uv_libhandle handle);
15051504
JL_DLLEXPORT int jl_dlsym(jl_uv_libhandle handle, const char *symbol, void ** value, int throw_err);

src/runtime_ccall.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void *jl_get_library(const char *f_lib)
4545
if (hnd != NULL)
4646
return hnd;
4747
// We might run this concurrently on two threads but it doesn't matter.
48-
hnd = jl_load_dynamic_library(f_lib, JL_RTLD_DEFAULT);
48+
hnd = jl_load_dynamic_library(f_lib, JL_RTLD_DEFAULT, 0);
4949
if (hnd != NULL)
5050
jl_atomic_store_release(map_slot, hnd);
5151
return hnd;

src/staticdata.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ JL_DLLEXPORT void jl_preload_sysimg_so(const char *fname)
14081408

14091409
// Get handle to sys.so
14101410
if (!is_ji) // .ji extension => load .ji file only
1411-
jl_set_sysimg_so(jl_load_dynamic_library(fname, JL_RTLD_LOCAL | JL_RTLD_NOW));
1411+
jl_set_sysimg_so(jl_load_dynamic_library(fname, JL_RTLD_LOCAL | JL_RTLD_NOW, 1));
14121412
}
14131413

14141414
// Allow passing in a module handle directly, rather than a path

src/sys.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ JL_DLLEXPORT const char *jl_pathname_for_handle(void *handle)
534534
for (int32_t i = _dyld_image_count() - 1; i >= 0 ; i--) {
535535
// dlopen() each image, check handle
536536
const char *image_name = _dyld_get_image_name(i);
537-
void *probe_lib = jl_load_dynamic_library(image_name, JL_RTLD_DEFAULT);
537+
void *probe_lib = jl_load_dynamic_library(image_name, JL_RTLD_DEFAULT, 0);
538538
jl_dlclose(probe_lib);
539539

540540
// If the handle is the same as what was passed in (modulo mode bits), return this image name

stdlib/Libdl/src/Libdl.jl

+15-14
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,28 @@ exported symbols and if the bound references are put into process local or globa
9595
instance `RTLD_LAZY|RTLD_DEEPBIND|RTLD_GLOBAL` allows the library's symbols to be available
9696
for usage in other shared libraries, addressing situations where there are dependencies
9797
between shared libraries.
98+
99+
If the library cannot be found, this method returns `nothing`.
98100
"""
99101
function dlopen end
100102

101103
dlopen(s::Symbol, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
102104
dlopen(string(s), flags)
103105

104-
dlopen(s::AbstractString, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
105-
ccall(:jl_load_dynamic_library, Ptr{Cvoid}, (Cstring,UInt32), s, flags)
106+
function dlopen(s::AbstractString, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND; throw_error::Bool = true)
107+
ret = ccall(:jl_load_dynamic_library, Ptr{Cvoid}, (Cstring,UInt32), s, flags)
108+
if ret == C_NULL
109+
return nothing
110+
end
111+
return ret
112+
end
106113

107114
"""
108115
dlopen_e(libfile::AbstractString [, flags::Integer])
109116
110-
Similar to [`dlopen`](@ref), except returns a `NULL` pointer instead of raising errors.
117+
Similar to [`dlopen`](@ref), except returns a `NULL` pointer instead of raising errors. It is preferred to directly call dlopen(libfile, flags; throw_error=false)`
111118
"""
112-
function dlopen_e end
113-
114-
dlopen_e(s::Symbol, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
115-
dlopen_e(string(s), flags)
116-
117-
dlopen_e(s::AbstractString, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
118-
ccall(:jl_load_dynamic_library_e, Ptr{Cvoid}, (Cstring,UInt32), s, flags)
119+
dlopen_e(args...) = dlopen(args...; throw_error=false)
119120

120121
"""
121122
dlclose(handle)
@@ -139,14 +140,14 @@ function find_library(libnames, extrapaths=String[])
139140
for lib in libnames
140141
for path in extrapaths
141142
l = joinpath(path, lib)
142-
p = dlopen_e(l, RTLD_LAZY)
143-
if p != C_NULL
143+
p = dlopen(l, RTLD_LAZY; throw_error=false)
144+
if p !== nothing
144145
dlclose(p)
145146
return l
146147
end
147148
end
148-
p = dlopen_e(lib, RTLD_LAZY)
149-
if p != C_NULL
149+
p = dlopen(lib, RTLD_LAZY; throw_error=false)
150+
if p !== nothing
150151
dlclose(p)
151152
return lib
152153
end

stdlib/Libdl/test/runtests.jl

+19-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dlls = Libdl.dllist()
1010
@test length(dlls) > 3 # at a bare minimum, probably have some version of libstdc, libgcc, libjulia, ...
1111
if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
1212
for dl in dlls
13-
if isfile(dl) && (Libdl.dlopen_e(dl) != C_NULL)
13+
if isfile(dl) && (Libdl.dlopen(dl; throw_error=false) != C_NULL)
1414
@test Base.samefile(Libdl.dlpath(dl), dl)
1515
end
1616
end
@@ -45,44 +45,44 @@ end
4545
# dlopen should be able to handle absolute and relative paths, with and without dlext
4646
let dl = C_NULL
4747
try
48-
dl = Libdl.dlopen_e(abspath(joinpath(private_libdir, "libccalltest")))
49-
@test dl != C_NULL
48+
dl = Libdl.dlopen(abspath(joinpath(private_libdir, "libccalltest")); throw_error=false)
49+
@test dl !== nothing
5050
finally
5151
Libdl.dlclose(dl)
5252
end
5353
end
5454

5555
let dl = C_NULL
5656
try
57-
dl = Libdl.dlopen_e(abspath(joinpath(private_libdir, "libccalltest.$(Libdl.dlext)")))
58-
@test dl != C_NULL
57+
dl = Libdl.dlopen(abspath(joinpath(private_libdir, "libccalltest.$(Libdl.dlext)")); throw_error=false)
58+
@test dl !== nothing
5959
finally
6060
Libdl.dlclose(dl)
6161
end
6262
end
6363

6464
let dl = C_NULL
6565
try
66-
dl = Libdl.dlopen_e(relpath(joinpath(private_libdir, "libccalltest")))
67-
@test dl != C_NULL
66+
dl = Libdl.dlopen(relpath(joinpath(private_libdir, "libccalltest")); throw_error=false)
67+
@test dl !== nothing
6868
finally
6969
Libdl.dlclose(dl)
7070
end
7171
end
7272

7373
let dl = C_NULL
7474
try
75-
dl = Libdl.dlopen_e(relpath(joinpath(private_libdir, "libccalltest.$(Libdl.dlext)")))
76-
@test dl != C_NULL
75+
dl = Libdl.dlopen(relpath(joinpath(private_libdir, "libccalltest.$(Libdl.dlext)")); throw_error=false)
76+
@test dl !== nothing
7777
finally
7878
Libdl.dlclose(dl)
7979
end
8080
end
8181

8282
let dl = C_NULL
8383
try
84-
dl = Libdl.dlopen_e("./foo")
85-
@test dl == C_NULL
84+
dl = Libdl.dlopen("./foo"; throw_error=false)
85+
@test dl === nothing
8686
finally
8787
Libdl.dlclose(dl)
8888
end
@@ -91,17 +91,17 @@ end
9191
# unqualified names present in DL_LOAD_PATH
9292
let dl = C_NULL
9393
try
94-
dl = Libdl.dlopen_e("libccalltest")
95-
@test dl != C_NULL
94+
dl = Libdl.dlopen("libccalltest"; throw_error=false)
95+
@test dl !== nothing
9696
finally
9797
Libdl.dlclose(dl)
9898
end
9999
end
100100

101101
let dl = C_NULL
102102
try
103-
dl = Libdl.dlopen_e(string("libccalltest",".",Libdl.dlext))
104-
@test dl != C_NULL
103+
dl = Libdl.dlopen(string("libccalltest",".",Libdl.dlext); throw_error=false)
104+
@test dl !== nothing
105105
finally
106106
Libdl.dlclose(dl)
107107
end
@@ -149,8 +149,8 @@ end
149149
let dl = C_NULL
150150
try
151151
path = abspath(joinpath(private_libdir, "libccalltest"))
152-
dl = Libdl.dlopen(path)
153-
@test dl != C_NULL
152+
dl = Libdl.dlopen(path; throw_error=false)
153+
@test dl !== nothing
154154
@test Base.samefile(abspath(Libdl.dlpath(dl)),
155155
abspath(Libdl.dlpath(path)))
156156
@test Base.samefile(abspath(Libdl.dlpath(dl)),
@@ -193,8 +193,8 @@ let dl = C_NULL
193193
@test -1 == ccall(:jl_dlclose, Cint, (Ptr{Cvoid},), dl)
194194
@test !Libdl.dlclose(dl)
195195

196-
dl = Libdl.dlopen_e("libccalltest")
197-
@test dl != C_NULL
196+
dl = Libdl.dlopen("libccalltest"; throw_error=false)
197+
@test dl !== nothing
198198

199199
@test Libdl.dlclose(dl)
200200
@test_skip !Libdl.dlclose(dl) # Syscall doesn't fail on Win32

stdlib/LinearAlgebra/src/blas.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ import Libdl
7272
# utility routines
7373
let lib = C_NULL
7474
global function determine_vendor()
75-
lib == C_NULL && (lib = Libdl.dlopen_e(Base.libblas_name))
75+
if lib == C_NULL
76+
lib = Libdl.dlopen(Base.libblas_name; throw_error=false)
77+
end
7678
vend = :unknown
7779
if lib != C_NULL
7880
if Libdl.dlsym(lib, :openblas_set_num_threads; throw_error=false) !== nothing

0 commit comments

Comments
 (0)