1 // RUN: %clang_safestack -fno-stack-protector -D_FORTIFY_SOURCE=0 -g %s -o %t.nossp
2 // RUN: %run %t.nossp 2>&1 | FileCheck --check-prefix=NOSSP %s
3 
4 // RUN: %clang_safestack -fstack-protector-all -D_FORTIFY_SOURCE=0 -g %s -o %t.ssp
5 // RUN: env LIBC_FATAL_STDERR_=1 not --crash %run %t.ssp 2>&1 | \
6 // RUN:     FileCheck -check-prefix=SSP %s
7 
8 // Test stack canaries on the unsafe stack.
9 
10 // REQUIRES: stable-runtime
11 
12 #include <assert.h>
13 #include <stdio.h>
14 #include <string.h>
15 
f(unsigned * y)16 __attribute__((noinline)) void f(unsigned *y) {
17   char x;
18   char *volatile p = &x;
19   char *volatile q = (char *)y;
20   assert(p < q);
21   assert(q - p < 1024); // sanity
22   // This has technically undefined behavior, but we know the actual layout of
23   // the unsafe stack and this should not touch anything important.
24   memset(&x, 0xab, q - p + sizeof(*y));
25 }
26 
main(int argc,char ** argv)27 int main(int argc, char **argv)
28 {
29   unsigned y;
30   // NOSSP: main 1
31   // SSP: main 1
32   fprintf(stderr, "main 1\n");
33   f(&y);
34   // NOSSP: main 2
35   // SSP-NOT: main 2
36   fprintf(stderr, "main 2\n");
37   return 0;
38 }
39