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// MSVC doesn't support VLAs in the first place.
9// UNSUPPORTED: msvc
10
11#include "defines.h"
12#include "sanitizer/asan_interface.h"
13#include <assert.h>
14#include <stdint.h>
15#include <stdlib.h>
16
17// MSVC provides _alloca instead of alloca.
18#if defined(_MSC_VER) && !defined(alloca)
19# define alloca _alloca
20#endif
21
22#if defined(__sun__) && defined(__svr4__)
23#include <alloca.h>
24#endif
25
26void *top, *bot;
27
28ATTRIBUTE_NOINLINE void foo(int len) {
29 char x;
30 top = &x;
31 volatile char array[len];
32 if (len)
33 array[0] = 0;
34 assert(!(reinterpret_cast<uintptr_t>(array) & 31L));
35 alloca(len);
36 for (int i = 0; i < 32; ++i) {
37 volatile char array[i];
38 if (i)
39 array[0] = 0;
40 bot = alloca(i);
41 assert(!(reinterpret_cast<uintptr_t>(bot) & 31L));
42 }
43}
44
45int main(int argc, char **argv) {
46 foo(len: 32);
47 void *q = __asan_region_is_poisoned(beg: bot, size: (char *)top - (char *)bot);
48 assert(!q);
49 return 0;
50}
51

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

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