-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deadloack in dlopen. NFC (#18487)
When within dlopen itself we hold an exclusive lock so we need to disable the automatic code synchronization during that time. Split out from #18376
- Loading branch information
Showing
4 changed files
with
188 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <assert.h> | ||
#include <dlfcn.h> | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
|
||
#ifndef NUM_THREADS | ||
#define NUM_THREADS 2 | ||
#endif | ||
|
||
typedef int* (*sidey_data_type)(); | ||
typedef int (*func_t)(); | ||
typedef func_t (*sidey_func_type)(); | ||
|
||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
_Atomic int thread_count = 0; | ||
|
||
void* thread_main(void* arg) { | ||
int num = (intptr_t)arg; | ||
printf("thread_main %d %p\n", num, pthread_self()); | ||
thread_count++; | ||
|
||
// busy wait until all threads are running | ||
while (thread_count != NUM_THREADS) {} | ||
|
||
char filename[255]; | ||
sprintf(filename, "liblib%d.so", num); | ||
printf("loading %s\n", filename); | ||
void* handle = dlopen(filename, RTLD_NOW|RTLD_GLOBAL); | ||
printf("done loading %s\n", filename); | ||
if (!handle) { | ||
printf("dlerror: %s\n", dlerror()); | ||
} | ||
assert(handle); | ||
/* | ||
* TODO(sbc): We have a bug that new functions added to the table via dlsym | ||
* are not yet correctly synchronized between threads. | ||
* Uncommenting the code below will cause a "table out of sync" error. | ||
*/ | ||
/* | ||
sidey_data_type p_side_data_address; | ||
sidey_func_type p_side_func_address; | ||
p_side_data_address = dlsym(handle, "side_data_address"); | ||
printf("p_side_data_address=%p\n", p_side_data_address); | ||
p_side_func_address = dlsym(handle, "side_func_address"); | ||
printf("p_side_func_address=%p\n", p_side_func_address); | ||
*/ | ||
|
||
printf("done thread_main %d\n", num); | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
printf("in main: %p\n", pthread_self()); | ||
pthread_mutex_lock(&mutex); | ||
|
||
// start a bunch of threads while holding the lock | ||
pthread_t threads[NUM_THREADS]; | ||
for (int i = 0; i < NUM_THREADS; i++) { | ||
pthread_create(&threads[i], NULL, thread_main, (void*)i); | ||
} | ||
|
||
// busy wait until all threads are running | ||
while (thread_count != NUM_THREADS) {} | ||
|
||
for (int i = 0; i < NUM_THREADS; i++) { | ||
pthread_join(threads[i], NULL); | ||
} | ||
|
||
printf("main done\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters