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