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