58 lines · cpp
1// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t2// RUN: %run %t 2>&13//4// REQUIRES: stable-runtime5 6// See https://github.com/llvm/llvm-project/issues/1109567// XFAIL: target=sparc{{.*}}8 9// This testcase checks correct interaction between VLAs and allocas.10 11// MSVC doesn't support VLA's12// UNSUPPORTED: msvc13 14#include <assert.h>15#include <stdint.h>16#include <stdlib.h>17#include "sanitizer/asan_interface.h"18 19// MSVC provides _alloca instead of alloca.20#if defined(_MSC_VER) && !defined(alloca)21# define alloca _alloca22#endif23 24#if defined(__sun__) && defined(__svr4__)25#include <alloca.h>26#endif27 28#define RZ 3229 30__attribute__((noinline)) void foo(int len) {31 char *top, *bot;32 // This alloca call should live until the end of foo.33 char *alloca1 = (char *)alloca(len);34 assert(!(reinterpret_cast<uintptr_t>(alloca1) & 31L));35 // This should be first poisoned address after loop.36 top = alloca1 - RZ;37 for (int i = 0; i < 32; ++i) {38 // Check that previous alloca was unpoisoned at the end of iteration.39 if (i) assert(!__asan_region_is_poisoned(bot, 96));40 // VLA is unpoisoned at the end of iteration.41 volatile char array[i];42 // Ensure that asan-use-stack-safety does not optimize out the poisoning.43 if (i) array[0] = 0;44 assert(!(reinterpret_cast<uintptr_t>(array) & 31L));45 // Alloca is unpoisoned at the end of iteration,46 // because dominated by VLA.47 bot = (char *)alloca(i) - RZ;48 }49 // Check that all allocas from loop were unpoisoned correctly.50 void *q = __asan_region_is_poisoned(bot, (char *)top - (char *)bot + 1);51 assert(q == top);52}53 54int main(int argc, char **argv) {55 foo(32);56 return 0;57}58