Extend test thread args lifetime
The argument passed to each thread in test-pthread.c indicates a thread number to report when finished. This value is read out by the thread into a local variable early in the thread's execution. Currently, the address passed as this argument is the address of a loop local. However, it is possible that the created thread will not be scheduled to run or will not read the argument before the thread creation loop finishes and the local is destroyed. This can lead to odd behavior, usually observed as multiple threads reporting the same thread_num. Fix this issue by storing the thread arguments in a parallel array to the array of threads. This ensures that the thread arguments are in scope as long as the threads themselves. Discovered with tests/test-pthread with AddressSanitizer enabled.
This commit is contained in:
parent
efc71a3c13
commit
6e414d61c7
|
@ -71,17 +71,17 @@ static void *run_test_in_thread(void *arg)
|
|||
int main(int argc,char **argv)
|
||||
{
|
||||
pthread_t threads[NTHR];
|
||||
struct thr_arg_s thr_args[NTHR];
|
||||
int i, j;
|
||||
|
||||
printf("Creating %d threads\n",NTHR);
|
||||
|
||||
for(i = 0;i<NTHR;i++)
|
||||
{
|
||||
struct thr_arg_s thr_arg;
|
||||
int result;
|
||||
thr_arg.thr_num=i;
|
||||
thr_args[i].thr_num=i;
|
||||
result = pthread_create(&threads[i],NULL,run_test_in_thread,
|
||||
(void *)&thr_arg);
|
||||
(void *)&thr_args[i]);
|
||||
if(result!=0)
|
||||
{
|
||||
fprintf(stderr,"Cannot create thread %d\n",i);
|
||||
|
|
Loading…
Reference in New Issue