llvm-project
38 строк · 1.0 Кб
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
16__attribute__((noinline)) void f(unsigned *y) {
17char x;
18char *volatile p = &x;
19char *volatile q = (char *)y;
20assert(p < q);
21assert(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.
24memset(&x, 0xab, q - p + sizeof(*y));
25}
26
27int main(int argc, char **argv)
28{
29unsigned y;
30// NOSSP: main 1
31// SSP: main 1
32fprintf(stderr, "main 1\n");
33f(&y);
34// NOSSP: main 2
35// SSP-NOT: main 2
36fprintf(stderr, "main 2\n");
37return 0;
38}
39