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