xref: /llvm-project/compiler-rt/test/asan/TestCases/alloca_loop_unpoisoning.cpp (revision ebd4dc42630e201c15894b48c3ea890eaa8c3b18)
1 // RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t
2 // RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t 2>&1
3 //
4 // REQUIRES: stable-runtime
5 
6 // This testcase checks that allocas and VLAs inside loop are correctly unpoisoned.
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 #if defined(__sun__) && defined(__svr4__)
19 #include <alloca.h>
20 #endif
21 
22 void *top, *bot;
23 
foo(int len)24 __attribute__((noinline)) void foo(int len) {
25   char x;
26   top = &x;
27   volatile char array[len];
28   if (len)
29     array[0] = 0;
30   assert(!(reinterpret_cast<uintptr_t>(array) & 31L));
31   alloca(len);
32   for (int i = 0; i < 32; ++i) {
33     volatile char array[i];
34     if (i)
35       array[0] = 0;
36     bot = alloca(i);
37     assert(!(reinterpret_cast<uintptr_t>(bot) & 31L));
38   }
39 }
40 
main(int argc,char ** argv)41 int main(int argc, char **argv) {
42   foo(32);
43   void *q = __asan_region_is_poisoned(bot, (char *)top - (char *)bot);
44   assert(!q);
45   return 0;
46 }
47