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 #include <stdlib.h>
10
11 // MSVC provides _alloca instead of alloca.
12 #if defined(_MSC_VER) && !defined(alloca)
13 # define alloca _alloca
14 #endif
15
16 #if defined(__sun__) && defined(__svr4__)
17 #include <alloca.h>
18 #endif
19
20
f1_alloca()21 void f1_alloca() {
22 char *dynamic_buffer = (char *)alloca(200);
23 fprintf(stderr, "dynamic_buffer = %p\n", dynamic_buffer);
24 memset(dynamic_buffer, 'y', 200);
25 return;
26 }
27
28 static const int kDynamicArraySize = 200;
29
f1_vla()30 void f1_vla() {
31 char dynamic_buffer[kDynamicArraySize];
32 fprintf(stderr, "dynamic_buffer = %p\n", dynamic_buffer);
33 memset(dynamic_buffer, 'y', kDynamicArraySize);
34 return;
35 }
36
f2()37 void f2() {
38 char buf[1024];
39 memset(buf, 'x', 1024);
40 }
41
main(int argc,const char * argv[])42 int main(int argc, const char *argv[]) {
43 if (!strcmp(argv[1], "1")) {
44 f1_alloca();
45 } else if (!strcmp(argv[1], "2")) {
46 f1_vla();
47 }
48 f2();
49 fprintf(stderr, "Done.\n");
50 return 0;
51 }
52
53 // CHECK-NOT: ERROR: AddressSanitizer
54 // CHECK: Done.
55