libthread: add pthreadperthread mode and use under ASAN

ASAN can't deal with the coroutine stacks.
In theory we can call into ASAN runtime to let it know about them,
but ASAN still has problems with fork or exit happening from a
non-system stack. Bypass all possible problems by just having
a full OS thread for each libthread thread. The threads are still
cooperatively scheduled within a proc (in thos mode, a group of OS threads).

Setting the environment variable LIBTHREAD=pthreadperthread
will enable the pthreadperthread mode, as will building with
CC9FLAGS='-fsanitize=address' in $PLAN9/config.

This solution is much more general than ASAN - for example if
you are trying to find all the thread stacks in a reproducible crash
you can use pthreadperthread mode with any debugger that
knows only about OS threads.
This commit is contained in:
Russ Cox
2020-05-17 08:24:54 -04:00
parent 06687f70ba
commit baef953da2
4 changed files with 120 additions and 18 deletions

View File

@@ -99,6 +99,23 @@ startprocfn(void *v)
pthread_exit(0);
}
static void
startpthreadfn(void *v)
{
void **a;
Proc *p;
_Thread *t;
a = (void**)v;
p = a[0];
t = a[1];
free(a);
t->osprocid = pthread_self();
pthread_detach(t->osprocid);
_threadpthreadmain(p, t);
pthread_exit(0);
}
void
_procstart(Proc *p, void (*fn)(Proc*))
{
@@ -116,6 +133,22 @@ _procstart(Proc *p, void (*fn)(Proc*))
}
}
void
_threadpthreadstart(Proc *p, _Thread *t)
{
void **a;
a = malloc(3*sizeof a[0]);
if(a == nil)
sysfatal("_pthreadstart malloc: %r");
a[0] = p;
a[1] = t;
if(pthread_create(&t->osprocid, nil, (void*(*)(void*))startpthreadfn, (void*)a) < 0){
fprint(2, "pthread_create: %r\n");
abort();
}
}
static pthread_key_t prockey;
Proc*