<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58023>58023</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[ASan] sem_timedwait hangs with 32-bit binary
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mpolacek
</td>
</tr>
</table>
<pre>
The following program hangs with -m32, but not without:
`$ clang -m32 -fsanitize=address -lpthread sem_timedwait.c; ./a.out`
(and therefore the same happens with gcc)
```
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <semaphore.h>
typedef struct {
unsigned int start;
unsigned int count;
unsigned int other;
unsigned int iter;
} threadlocal_t;
threadlocal_t data_one __attribute__((aligned(64))) = { 0, 0, 0, 0 };
threadlocal_t data_two __attribute__((aligned(64))) = { 0, 0, 0, 0 };
sem_t sem_one;
sem_t sem_two;
void *thread_func_one(void *arg)
{
int n;
unsigned int other;
struct timespec timeout;
struct timespec sleeptime;
timeout.tv_sec = 0;
timeout.tv_nsec = 1000;
sleeptime.tv_sec = 1;
sleeptime.tv_nsec = 0;
for ( n = 1; n < 10 ; n++ ) {
while ( 0 != sem_timedwait(&sem_one,&timeout) );
//sem_wait(&sem_one);
printf("%s loop %i\n",__FUNCTION__,n);
data_one.count = 0;
nanosleep(&sleeptime,0);
sem_post(&sem_two);
}
printf("%s done\n",__FUNCTION__);
return 0;
}
void *thread_func_two(void *arg)
{
int n;
unsigned int other;
struct timespec timeout;
struct timespec sleeptime;
timeout.tv_sec = 0;
timeout.tv_nsec = 1000;
sleeptime.tv_sec = 1;
sleeptime.tv_nsec = 0;
for ( n = 1; n < 10 ; n++ ) {
while ( 0 != sem_timedwait(&sem_two,&timeout) );
//sem_wait(&sem_two);
printf("%s loop %i\n",__FUNCTION__,n);
data_one.count = 0;
nanosleep(&sleeptime,0);
sem_post(&sem_one);
}
printf("%s done\n",__FUNCTION__);
return 0;
}
int create_thread(int policy,int priority,void *(*start_routine)(void*),pthread_t *tidp)
{
int retval = 1;
pthread_attr_t attr;
struct sched_param sched_param;
if ( retval )
{
if ( 0 != ( errno = pthread_attr_init(&attr)) )
{
perror("Error: pthread_attr_init failed");
retval = 0;
}
}
if ( retval )
{
if ( 0 != ( errno = pthread_attr_setschedpolicy(&attr,policy)) )
{
perror("Error:pthread_attr_setschedpolicy failed");
retval = 0;
}
}
if ( retval )
{
if ( 0 != ( errno = pthread_attr_getschedparam(&attr,&sched_param)) )
{
perror("Error:pthread_attr_getschedparam failed");
retval = 0;
}
}
if ( retval )
{
sched_param.sched_priority = priority;
if ( 0 != (errno = pthread_attr_setschedparam(&attr,&sched_param)) )
{
perror("Error:pthread_attr_setschedparam failed");
retval = 0;
}
}
if ( 0 != ( errno = pthread_create(tidp,&attr,start_routine,0)) )
{
perror("Error: pthread_create failed");
retval = 0;
}
if ( retval )
{
if ( 0 != ( errno = pthread_attr_destroy(&attr)) )
{
perror("Error:pthread_attr_destroy failed");
retval = 0;
}
}
return retval;
}
int main(int argc, char *argv[])
{
int retval[2] = { 1,1 };
pthread_t tid[2];
void *status;
sem_init(&sem_one,0,1);
sem_init(&sem_two,0,0);
if ( retval[0] )
{
retval[0] = create_thread(SCHED_FIFO,84,thread_func_one,&tid[0]);
}
if ( retval[1] )
{
retval[1] = create_thread(SCHED_FIFO,84,thread_func_two,&tid[1]);
}
if ( retval[0] )
{
if ( 0 != ( errno = pthread_join(tid[0],&status)) )
{
perror("Error: pthread_join failed");
retval[0] = 0;
}
}
if ( retval[1] )
{
if ( 0 != ( errno = pthread_join(tid[1],&status)) )
{
perror("Error: pthread_join failed");
retval[1] = 0;
}
}
printf ("data_one.count=%d data_one.other=%d\n",data_one.count,data_one.other);
printf ("data_two.count=%d data_two.other=%d\n",data_two.count,data_two.other);
return retval[0]&retval[1]?0:1;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http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