xref: /aosp_15_r20/external/compiler-rt/test/safestack/overflow.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clang_safestack %s -o %t
2*7c3d14c8STreehugger Robot // RUN: %run %t
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // RUN: %clang_nosafestack -fno-stack-protector %s -o %t
5*7c3d14c8STreehugger Robot // RUN: not %run %t
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot // Test that buffer overflows on the unsafe stack do not affect variables on the
8*7c3d14c8STreehugger Robot // safe stack.
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot // REQUIRES: stable-runtime
11*7c3d14c8STreehugger Robot 
12*7c3d14c8STreehugger Robot __attribute__((noinline))
fct(volatile int * buffer)13*7c3d14c8STreehugger Robot void fct(volatile int *buffer)
14*7c3d14c8STreehugger Robot {
15*7c3d14c8STreehugger Robot   memset(buffer - 1, 0, 7 * sizeof(int));
16*7c3d14c8STreehugger Robot }
17*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)18*7c3d14c8STreehugger Robot int main(int argc, char **argv)
19*7c3d14c8STreehugger Robot {
20*7c3d14c8STreehugger Robot   int prebuf[7];
21*7c3d14c8STreehugger Robot   int value1 = 42;
22*7c3d14c8STreehugger Robot   int buffer[5];
23*7c3d14c8STreehugger Robot   int value2 = 42;
24*7c3d14c8STreehugger Robot   int postbuf[7];
25*7c3d14c8STreehugger Robot   fct(prebuf + 1);
26*7c3d14c8STreehugger Robot   fct(postbuf + 1);
27*7c3d14c8STreehugger Robot   fct(buffer);
28*7c3d14c8STreehugger Robot   return value1 != 42 || value2 != 42;
29*7c3d14c8STreehugger Robot }
30