Skip to content

Commit 165da4c

Browse files
authored
[windows] Fix self-path lookup on Windows (#103)
Store handle to self in `DllMain` so that we don't try to figure out a handle to ourselves (incorrectly) anymore on Windows. Add test for LBT path printing when `LBT_VERBOSE=1`
1 parent 8b8c2e3 commit 165da4c

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/dl_utils.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ void * lookup_symbol(const void * handle, const char * symbol_name) {
8282
* Work around protected symbol visibility and GCC/ld.bfd bug:
8383
* https://sourceware.org/bugzilla/show_bug.cgi?id=26815
8484
*/
85+
extern void * _win32_self_handle;
8586
void * lookup_self_symbol(const char * symbol_name) {
8687
void * self_handle = NULL;
8788
#if defined(_OS_WINDOWS_)
88-
self_handle = GetModuleHandle(NULL);
89+
self_handle = _win32_self_handle;
8990
#elif defined(_OS_DARWIN_)
9091
self_handle = RTLD_SELF;
9192
#elif defined(RTLD_DEFAULT)
@@ -104,7 +105,7 @@ const char * lookup_self_path()
104105
return self_path;
105106
}
106107
#if defined(_OS_WINDOWS_)
107-
if (!GetModuleFileNameA(GetModuleHandle(NULL), self_path, PATH_MAX)) {
108+
if (!GetModuleFileNameA(_win32_self_handle, self_path, PATH_MAX)) {
108109
fprintf(stderr, "ERROR: GetModuleFileName() failed\n");
109110
strcpy(self_path, "<unknown>");
110111
return self_path;

src/libblastrampoline.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,19 @@ LBT_DLLEXPORT int32_t lbt_forward(const char * libname, int32_t clear, int32_t v
395395
return nforwards;
396396
}
397397

398-
398+
/*
399+
* On windows it's surprisingly difficult to get a handle to ourselves,
400+
* and that's because they give it to you in `DllMain()`. ;)
401+
*/
402+
#ifdef _OS_WINDOWS_
403+
void * _win32_self_handle;
404+
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD code, void *) {
405+
if (code == DLL_PROCESS_ATTACH) {
406+
_win32_self_handle = (void *)hModule;
407+
}
408+
#else
399409
__attribute__((constructor)) void init(void) {
410+
#endif
400411
// Initialize config structures
401412
init_config();
402413

@@ -457,4 +468,8 @@ __attribute__((constructor)) void init(void) {
457468
clear = 0;
458469
}
459470
}
471+
472+
#ifdef _OS_WINDOWS_
473+
return TRUE;
474+
#endif
460475
}

src/win_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ int utf8_to_wchar(const char * str, wchar_t * wstr, size_t maxlen) {
3131
if (!MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, len))
3232
return 0;
3333
return 1;
34-
}
34+
}

test/runtests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ function run_test((test_name, test_expected_outputs, expect_success), libblas_na
6161
end
6262
@test expected_return_value
6363

64+
# Expect to see the path to `libblastrampoline` within the output,
65+
# since we have `LBT_VERBOSE=1` and at startup, it announces its own path:
66+
if startswith(libblas_name, "blastrampoline")
67+
lbt_libdir = first(libdirs)
68+
@test occursin(lbt_libdir, output)
69+
end
70+
6471
# Test to make sure the test ran properly
6572
has_expected_output = all(occursin(expected, output) for expected in test_expected_outputs)
6673
if !has_expected_output
@@ -187,7 +194,7 @@ end
187194
if openblas_interface == :ILP64
188195
inconsolable = ("inconsolable_test", ("||C||^2 is: 24.3384", "||b||^2 is: 3.0000"), true)
189196
@testset "LBT -> OpenBLAS 32 + 64 (LP64 + ILP64)" begin
190-
libdirs = unique(vcat(OpenBLAS32_jll.LIBPATH_list..., OpenBLAS_jll.LIBPATH_list..., CompilerSupportLibraries_jll.LIBPATH_list..., lbt_dir))
197+
libdirs = unique(vcat(lbt_dir, OpenBLAS32_jll.LIBPATH_list..., OpenBLAS_jll.LIBPATH_list..., CompilerSupportLibraries_jll.LIBPATH_list...))
191198
run_test(inconsolable, lbt_link_name, libdirs, :wild_sobbing, "$(OpenBLAS32_jll.libopenblas_path);$(OpenBLAS_jll.libopenblas_path)")
192199
end
193200
end

0 commit comments

Comments
 (0)