Skip to content

Commit 8b8c2e3

Browse files
authored
Eliminate exit(1) calls in LBT (#104)
It is rather rude of us to call `exit(1)` in our code, no matter how rare it is. Instead, we do our best to set ourselves into stable states when these situations (many of which should be impossible) occur.
1 parent d00e6ca commit 8b8c2e3

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

src/autodetection.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,19 @@ int32_t autodetect_blas_interface(void * isamax_addr) {
7272

7373
// Override `lsame_` to point to our `fake_lsame` if we're unable to `RTLD_DEEPBIND`
7474
if (use_deepbind == 0) {
75-
push_fake_lsame();
75+
if (!push_fake_lsame()) {
76+
// Bad LBT compile?
77+
return LBT_INTERFACE_UNKNOWN;
78+
}
7679
}
7780

7881
int64_t max_idx = isamax(&n, X, &incx);
7982

8083
if (use_deepbind == 0) {
81-
pop_fake_lsame();
84+
if (!pop_fake_lsame()) {
85+
// Bad LBT compile?
86+
return LBT_INTERFACE_UNKNOWN;
87+
}
8288
}
8389

8490
// Although we declare that `isamax` returns an `int64_t`, it may not actually do so,

src/deepbindless.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ LBT_DLLEXPORT uint8_t lbt_get_use_deepbind() {
1818

1919
int lsame_idx = -1;
2020
const void *old_lsame32 = NULL, *old_lsame64 = NULL;
21-
void push_fake_lsame() {
21+
uint8_t push_fake_lsame() {
2222
// Find `lsame_` in our symbol list (if we haven't done so before)
2323
if (lsame_idx == -1) {
2424
lsame_idx = find_symbol_idx("lsame_");
2525
if (lsame_idx == -1) {
26-
// This is fatal as it signifies a configuration error in our trampoline symbol list
27-
fprintf(stderr, "Error: Unable to find lsame_ in our symbol list?!\n");
28-
exit(1);
26+
fprintf(stderr, "Error: Unable to find lsame_ in our symbol list. "
27+
"This signifies a libblastrampoline compilation failure. "
28+
"Aborting interface autodetection.\n");
29+
return 0;
2930
}
3031
}
3132

@@ -36,20 +37,23 @@ void push_fake_lsame() {
3637
// Insert our "fake" lsame in so that we always have a half-functional copy
3738
(*exported_func32_addrs[lsame_idx]) = &fake_lsame;
3839
(*exported_func64_addrs[lsame_idx]) = &fake_lsame;
40+
return 1;
3941
}
4042

41-
void pop_fake_lsame() {
43+
uint8_t pop_fake_lsame() {
4244
if (lsame_idx == -1) {
4345
// Did you call `pop_fake_lsame()` without calling `push_fake_lsame()` first?!
44-
fprintf(stderr, "pop_fake_lsame() called with invalid `lsame_idx`!\n");
45-
exit(1);
46+
fprintf(stderr, "Error: Invalid lsame_ index. This is an internal libblastrampoline error. "
47+
"Aborting interface autodetection.\n");
48+
return 0;
4649
}
4750

4851
(*exported_func32_addrs[lsame_idx]) = old_lsame32;
4952
(*exported_func64_addrs[lsame_idx]) = old_lsame64;
5053

5154
old_lsame32 = NULL;
5255
old_lsame64 = NULL;
56+
return 1;
5357
}
5458

5559

src/dl_utils.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void * load_library(const char * path) {
3131
wchar_t wpath[2*PATH_MAX + 1] = {0};
3232
if (!utf8_to_wchar(path, wpath, 2*PATH_MAX)) {
3333
fprintf(stderr, "ERROR: Unable to convert path %s to wide string!\n", path);
34-
exit(1);
34+
return NULL;
3535
}
3636
new_handle = (void *)LoadLibraryExW(wpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
3737
#else
@@ -106,14 +106,16 @@ const char * lookup_self_path()
106106
#if defined(_OS_WINDOWS_)
107107
if (!GetModuleFileNameA(GetModuleHandle(NULL), self_path, PATH_MAX)) {
108108
fprintf(stderr, "ERROR: GetModuleFileName() failed\n");
109-
exit(1);
109+
strcpy(self_path, "<unknown>");
110+
return self_path;
110111
}
111112
#else
112113
// On all other platforms, use dladdr()
113114
Dl_info info;
114115
if (!dladdr(lookup_self_symbol("lbt_forward"), &info)) {
115116
fprintf(stderr, "ERROR: Unable to dladdr(\"lbt_forward\"): %s\n", dlerror());
116-
exit(1);
117+
strcpy(self_path, "<unknown>");
118+
return self_path;
117119
}
118120
strcpy(self_path, info.dli_fname);
119121
#endif

src/libblastrampoline.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ LBT_DLLEXPORT int32_t lbt_forward(const char * libname, int32_t clear, int32_t v
274274
printf(" -> CBLAS not found\n");
275275
break;
276276
default:
277-
printf(" -> ERROR: CBLAS DETECTION FAILED UNEXPECTEDLY\n");
278-
exit(1);
277+
printf(" -> ERROR: Impossible CBLAS detection result: %d\n", cblas);
278+
cblas = LBT_CBLAS_UNKNOWN;
279279
break;
280280
}
281281
}

src/libblastrampoline_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int32_t autodetect_cblas_divergence(void * handle, const char * suffix);
9292
#endif
9393

9494
// Functions in deepbindless_surrogates.c
95-
void push_fake_lsame();
96-
void pop_fake_lsame();
95+
uint8_t push_fake_lsame();
96+
uint8_t pop_fake_lsame();
9797
int fake_lsame(char * ca, char * cb);
9898
extern uint8_t use_deepbind;

0 commit comments

Comments
 (0)