[llvm-dev] libasan bug: pthread_create never returns

Michalis Niarchos via llvm-dev llvm-dev at lists.llvm.org
Wed Jan 24 01:39:44 PST 2018


Hi all,

We observed that under certain circumstances pthread_create() (libasan
implemention) does not return. The conditions are the following:

- address sanitizer enabled
- main and child thread run on the same CPU (affinity)
- real-time scheduling policy for both threads
- different scheduling priority values (this includes the case
   of setting a real-time policy for one of the threads and
   a normal policy for the other)

I am attaching an example code that can be used to reproduce this
behavior.

Thanks,
Michalis

-- 
Michalis Niarchos
Developer
M +30 6934468006
Niometrics | The Network Analytics Company | www.niometrics.com

This email is intended only for the named addressee(s) and may contain
confidential and/or privileged information.  If you are not the named
addressee (or have received this e-mail in error), please notify the
sender immediately.  The unauthorised use, disclosure, distribution or
copying of the contents in this e-mail is prohibited.
-------------- next part --------------
/*
	Build:
		(clang-5.0): clang -fsanitize=address rt_thread.c -o rt_thread
		(gcc-7.2.0): gcc -fsanitize=address rt_thread.c -o rt_thread

	Bug:
	pthread_create() never returns

	Note:
	To reproduce the main and the child thread have to 
	run on the same CPU with different real-time priorities
	(including zero for non-real-time policies).
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <errno.h>
#include <unistd.h>

void *
dummy_worker(void *ptr)
{
	printf("Hello from worker\n");

	return NULL;
}

int
main(void)
{
	struct sched_param schedule;
	
	schedule.sched_priority = 50;

	if (sched_setscheduler(getpid(), SCHED_RR, &schedule) == 1) {
		perror("sched_setscheduler"); 
		exit(EXIT_FAILURE);
	}
	
	cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(1, &cpuset);

	if (sched_setaffinity(getpid(), sizeof(cpuset), &cpuset) == -1) {
		perror("sched_setaffinity"); 
		exit(EXIT_FAILURE);
	}
	
	printf("Hey from main\n");
	
	schedule.sched_priority = 20;

	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setschedpolicy(&attr, SCHED_RR);
	pthread_attr_setschedparam(&attr, &schedule);
	pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);

	pthread_t th;
	int rc;
	if ((rc = pthread_create(&th, &attr, dummy_worker, NULL)) != 0) {
		errno = rc;
		perror("pthread_create");
		exit(EXIT_FAILURE);
	}

	pthread_join(th, NULL);

	pthread_attr_destroy(&attr);

	return 0;
}


More information about the llvm-dev mailing list