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

Fix parallel access crashes and misbehavior #136

Merged
merged 9 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Next version
- GPR#127: Recognise hyphens in option names in the COFF .drectve section. Fixes #126 (Reza Barazesh)
- GPR#136: Fix parallel access crashes and misbehavior (David Allsopp, Jan Midtgaard, Antonin Décimo)


Version 0.43
- GPR#108: Add -lgcc_s to Cygwin's link libraries, upstreaming a patch from the
Expand Down
43 changes: 37 additions & 6 deletions flexdll.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ typedef struct dlunit {
} dlunit;
typedef void *resolver(void*, const char*);

static HANDLE units_mutex = INVALID_HANDLE_VALUE;

/* Error reporting */
/* The latest error must be kept in some variable so that flexdll_dlerror can
* report it but this causes data races (and possible segmentation faults) in
Expand All @@ -65,7 +67,7 @@ typedef void *resolver(void*, const char*);
* - TLS_ERROR_RESET will reset what is stored in the structure, so this is
* intended for initialisation entry points (flexdll_dlopen, flexdll_relocate)
* - TLS_ERROR_NOP will keep the current content of the structure, for all other
* entry points (flexdll_dlerror, ll_dlerror)
* entry points (flexdll_dlerror, ll_dlerror, flexdll_dlsym)
*
* The other exported entrypoints do not need to access the error storage.
*/
Expand Down Expand Up @@ -496,8 +498,24 @@ void *flexdll_wdlopen(const wchar_t *file, int mode) {
#endif /* __STDC_SECURE_LIB__ >= 200411L*/
#endif /* CYGWIN */

again:
if (units_mutex == INVALID_HANDLE_VALUE) {
HANDLE hMutex = CreateMutex(NULL, TRUE, NULL);
if (hMutex == NULL) {
if (!err->code) err->code = 1;
return NULL;
}
if (InterlockedCompareExchangePointer(&units_mutex, hMutex, INVALID_HANDLE_VALUE) != INVALID_HANDLE_VALUE) {
CloseHandle(hMutex);
goto again;
}
} else if (WaitForSingleObject(units_mutex, INFINITE) == WAIT_FAILED) {
if (!err->code) err->code = 1;
return NULL;
}

handle = ll_dlopen(file, exec);
if (!handle) { if (!err->code) err->code = 1; return NULL; }
if (!handle) { if (!err->code) err->code = 1; ReleaseMutex(units_mutex); return NULL; }

unit = units;
while ((NULL != unit) && (unit->handle != handle)) unit = unit->next;
Expand All @@ -516,9 +534,11 @@ void *flexdll_wdlopen(const wchar_t *file, int mode) {
/* Relocation has already been done if the flexdll's DLL entry point
is used */
flexdll_relocate(ll_dlsym(handle, "reloctbl"));
if (err->code) { flexdll_dlclose(unit); return NULL; }
if (err->code) { flexdll_dlclose(unit); ReleaseMutex(units_mutex); return NULL; }
}

ReleaseMutex(units_mutex);

return unit;
}

Expand Down Expand Up @@ -561,9 +581,20 @@ void flexdll_dlclose(void *u) {


void *flexdll_dlsym(void *u, const char *name) {
if (u == &main_unit) return find_symbol_global(NULL,name);
else if (NULL == u) return find_symbol(&static_symtable,name);
else return find_symbol(((dlunit*)u)->symtbl,name);
void *res;
err_t * err;
err = get_tls_error(TLS_ERROR_NOP);
if (err == NULL) return NULL;

if (WaitForSingleObject(units_mutex, INFINITE) == WAIT_FAILED) {
Copy link
Contributor

@MisterDA MisterDA Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (WaitForSingleObject(units_mutex, INFINITE) == WAIT_FAILED) {
if (WaitForSingleObject(units_mutex, INFINITE) != WAIT_OBJECT_0) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same for this one)

if (!err->code) err->code = 1;
return NULL;
}
if (u == &main_unit) res = find_symbol_global(NULL,name);
else if (NULL == u) res = find_symbol(&static_symtable,name);
else res = find_symbol(((dlunit*)u)->symtbl,name);
ReleaseMutex(units_mutex);
return res;
}

char *flexdll_dlerror() {
Expand Down