1// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-allocas %s -o %t
2// RUN: %run %t 2>&1
3//
4// REQUIRES: stable-runtime
5
6// This testcase checks correct interaction between VLAs and allocas.
7
8#include <assert.h>
9#include <stdint.h>
10#include <stdlib.h>
11#include "sanitizer/asan_interface.h"
12
13// MSVC provides _alloca instead of alloca.
14#if defined(_MSC_VER) && !defined(alloca)
15# define alloca _alloca
16#endif
17
18#define RZ 32
19
20__attribute__((noinline)) void foo(int len) {
21  char *top, *bot;
22  // This alloca call should live until the end of foo.
23  char *alloca1 = (char *)alloca(len);
24  assert(!(reinterpret_cast<uintptr_t>(alloca1) & 31L));
25  // This should be first poisoned address after loop.
26  top = alloca1 - RZ;
27  for (int i = 0; i < 32; ++i) {
28    // Check that previous alloca was unpoisoned at the end of iteration.
29    if (i) assert(!__asan_region_is_poisoned(bot, 96));
30    // VLA is unpoisoned at the end of iteration.
31    volatile char array[i];
32    assert(!(reinterpret_cast<uintptr_t>(array) & 31L));
33    // Alloca is unpoisoned at the end of iteration,
34    // because dominated by VLA.
35    bot = (char *)alloca(i) - RZ;
36  }
37  // Check that all allocas from loop were unpoisoned correctly.
38  void *q = __asan_region_is_poisoned(bot, (char *)top - (char *)bot + 1);
39  assert(q == top);
40}
41
42int main(int argc, char **argv) {
43  foo(32);
44  return 0;
45}
46