blob: 3873c3fceea8527157f20b5eed79855bb2233728 [file] [log] [blame]
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07001// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-allocas %s -o %t
2// RUN: %run %t 2>&1
3//
4// REQUIRES: stable-runtime
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07005
6// This testcase checks correct interaction between VLAs and allocas.
7
8#include <assert.h>
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07009#include <stdint.h>
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080010#include <stdlib.h>
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070011#include "sanitizer/asan_interface.h"
12
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080013// MSVC provides _alloca instead of alloca.
14#if defined(_MSC_VER) && !defined(alloca)
15# define alloca _alloca
16#endif
17
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070018#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}