]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/test/asan/TestCases/alloca_constant_size.cc
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / test / asan / TestCases / alloca_constant_size.cc
1 // Regression test for https://github.com/google/sanitizers/issues/691
2
3 // RUN: %clangxx_asan -O0 %s -o %t -fstack-protector
4 // RUN: %run %t 1 2>&1 | FileCheck %s
5 // RUN: %run %t 2 2>&1 | FileCheck %s
6
7 #include <stdio.h>
8 #include <string.h>
9
10 // MSVC provides _alloca instead of alloca.
11 #if defined(_MSC_VER) && !defined(alloca)
12 # define alloca _alloca
13 #elif defined(__FreeBSD__)
14 #include <stdlib.h>
15 #else
16 #include <alloca.h>
17 #endif
18
19
20 void f1_alloca() {
21 char *dynamic_buffer = (char *)alloca(200);
22 fprintf(stderr, "dynamic_buffer = %p\n", dynamic_buffer);
23 memset(dynamic_buffer, 'y', 200);
24 return;
25 }
26
27 static const int kDynamicArraySize = 200;
28
29 void f1_vla() {
30 char dynamic_buffer[kDynamicArraySize];
31 fprintf(stderr, "dynamic_buffer = %p\n", dynamic_buffer);
32 memset(dynamic_buffer, 'y', kDynamicArraySize);
33 return;
34 }
35
36 void f2() {
37 char buf[1024];
38 memset(buf, 'x', 1024);
39 }
40
41 int main(int argc, const char *argv[]) {
42 if (!strcmp(argv[1], "1")) {
43 f1_alloca();
44 } else if (!strcmp(argv[1], "2")) {
45 f1_vla();
46 }
47 f2();
48 fprintf(stderr, "Done.\n");
49 return 0;
50 }
51
52 // CHECK-NOT: ERROR: AddressSanitizer
53 // CHECK: Done.