xref: /aosp_15_r20/external/compiler-rt/test/lsan/TestCases/guard-page.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Check that if LSan finds that SP doesn't point into thread stack (e.g.
2*7c3d14c8STreehugger Robot // if swapcontext is used), LSan will not hit the guard page.
3*7c3d14c8STreehugger Robot // RUN: %clang_lsan %s -o %t && %run %t
4*7c3d14c8STreehugger Robot #include <errno.h>
5*7c3d14c8STreehugger Robot #include <stdio.h>
6*7c3d14c8STreehugger Robot #include <stdlib.h>
7*7c3d14c8STreehugger Robot #include <string.h>
8*7c3d14c8STreehugger Robot #include <pthread.h>
9*7c3d14c8STreehugger Robot #include <ucontext.h>
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
12*7c3d14c8STreehugger Robot pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
13*7c3d14c8STreehugger Robot int ctxfunc_started = 0;
14*7c3d14c8STreehugger Robot 
die(const char * msg,int err)15*7c3d14c8STreehugger Robot static void die(const char* msg, int err) {
16*7c3d14c8STreehugger Robot   if (err == 0)
17*7c3d14c8STreehugger Robot     err = errno;
18*7c3d14c8STreehugger Robot   fprintf(stderr, "%s: %s\n", msg, strerror(err));
19*7c3d14c8STreehugger Robot   exit(EXIT_FAILURE);
20*7c3d14c8STreehugger Robot }
21*7c3d14c8STreehugger Robot 
ctxfunc()22*7c3d14c8STreehugger Robot static void ctxfunc() {
23*7c3d14c8STreehugger Robot   pthread_mutex_lock(&mutex);
24*7c3d14c8STreehugger Robot   ctxfunc_started = 1;
25*7c3d14c8STreehugger Robot   pthread_cond_signal(&cond);
26*7c3d14c8STreehugger Robot   pthread_mutex_unlock(&mutex);
27*7c3d14c8STreehugger Robot   // Leave this context alive when the program exits.
28*7c3d14c8STreehugger Robot   for (;;);
29*7c3d14c8STreehugger Robot }
30*7c3d14c8STreehugger Robot 
thread(void * arg)31*7c3d14c8STreehugger Robot static void* thread(void* arg) {
32*7c3d14c8STreehugger Robot   (void)arg;
33*7c3d14c8STreehugger Robot   ucontext_t ctx;
34*7c3d14c8STreehugger Robot   void* stack;
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot   if (getcontext(&ctx) < 0)
37*7c3d14c8STreehugger Robot     die("getcontext", 0);
38*7c3d14c8STreehugger Robot   stack = malloc(1 << 10);
39*7c3d14c8STreehugger Robot   if (stack == NULL)
40*7c3d14c8STreehugger Robot     die("malloc", 0);
41*7c3d14c8STreehugger Robot   ctx.uc_stack.ss_sp = stack;
42*7c3d14c8STreehugger Robot   ctx.uc_stack.ss_size = 1 << 10;
43*7c3d14c8STreehugger Robot   makecontext(&ctx, ctxfunc, 0);
44*7c3d14c8STreehugger Robot   setcontext(&ctx);
45*7c3d14c8STreehugger Robot   die("setcontext", 0);
46*7c3d14c8STreehugger Robot   return NULL;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot 
main()49*7c3d14c8STreehugger Robot int main() {
50*7c3d14c8STreehugger Robot   pthread_t tid;
51*7c3d14c8STreehugger Robot   int i;
52*7c3d14c8STreehugger Robot 
53*7c3d14c8STreehugger Robot   pthread_mutex_lock(&mutex);
54*7c3d14c8STreehugger Robot   i = pthread_create(&tid, NULL, thread, NULL);
55*7c3d14c8STreehugger Robot   if (i != 0)
56*7c3d14c8STreehugger Robot     die("pthread_create", i);
57*7c3d14c8STreehugger Robot   while (!ctxfunc_started) pthread_cond_wait(&cond, &mutex);
58*7c3d14c8STreehugger Robot   pthread_mutex_unlock(&mutex);
59*7c3d14c8STreehugger Robot   return 0;
60*7c3d14c8STreehugger Robot }
61