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
21void f1_alloca() {
22 char *dynamic_buffer = (char *)alloca(200);
23 fprintf(stderr, format: "dynamic_buffer = %p\n", dynamic_buffer);
24 memset(s: dynamic_buffer, c: 'y', n: 200);
25 return;
26}
27
28static const int kDynamicArraySize = 200;
29
30void f1_vla() {
31 char dynamic_buffer[kDynamicArraySize];
32 fprintf(stderr, format: "dynamic_buffer = %p\n", dynamic_buffer);
33 memset(s: dynamic_buffer, c: 'y', n: kDynamicArraySize);
34 return;
35}
36
37void f2() {
38 char buf[1024];
39 memset(s: buf, c: 'x', n: 1024);
40}
41
42int main(int argc, const char *argv[]) {
43 if (!strcmp(s1: argv[1], s2: "1")) {
44 f1_alloca();
45 } else if (!strcmp(s1: argv[1], s2: "2")) {
46 f1_vla();
47 }
48 f2();
49 fprintf(stderr, format: "Done.\n");
50 return 0;
51}
52
53// CHECK-NOT: ERROR: AddressSanitizer
54// CHECK: Done.
55

source code of compiler-rt/test/asan/TestCases/alloca_constant_size.cpp