brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · 24b93c5 Raw
77 lines · cpp
1// Regression test for2// https://bugs.llvm.org/show_bug.cgi?id=324343 4// REQUIRES: shared_cxxabi5 6// RUN: %clangxx_asan -fexceptions -O0 %s -o %t7// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t8 9// The current implementation of this functionality requires special10// combination of libraries that are not used by default on NetBSD11// XFAIL: target={{.*netbsd.*}}12// FIXME: Bug 4270313// XFAIL: target={{.*solaris.*}}14 15// https://reviews.llvm.org/D111703 made compiler incompatible with released NDK.16// UNSUPPORTED: android && arm-target-arch17 18#include "defines.h"19#include <assert.h>20#include <exception>21#include <sanitizer/asan_interface.h>22 23namespace {24 25// Not instrumented because std::rethrow_exception is a [[noreturn]] function,26// for which the compiler would emit a call to __asan_handle_no_return which27// unpoisons the stack.28// We emulate here some code not compiled with asan. This function is not29// [[noreturn]] because the scenario we're emulating doesn't always throw. If it30// were [[noreturn]], the calling code would emit a call to31// __asan_handle_no_return.32void ATTRIBUTE_NO_SANITIZE_ADDRESS33uninstrumented_rethrow_exception(std::exception_ptr const &exc_ptr) {34  std::rethrow_exception(exc_ptr);35}36 37char *poisoned1;38char *poisoned2;39 40// Create redzones for stack variables in shadow memory and call41// std::rethrow_exception which should unpoison the entire stack.42void create_redzones_and_throw(std::exception_ptr const &exc_ptr) {43  char a[100];44  poisoned1 = a - 1;45  poisoned2 = a + sizeof(a);46  assert(__asan_address_is_poisoned(poisoned1));47  assert(__asan_address_is_poisoned(poisoned2));48  uninstrumented_rethrow_exception(exc_ptr);49}50 51} // namespace52 53// Check that std::rethrow_exception is intercepted by asan and the interception54// unpoisons the stack.55// If std::rethrow_exception is NOT intercepted, then calls to this function56// from instrumented code will still unpoison the stack because57// std::rethrow_exception is a [[noreturn]] function and any [[noreturn]]58// function call will be instrumented with __asan_handle_no_return.59// However, calls to std::rethrow_exception from UNinstrumented code will not60// unpoison the stack, so we need to intercept std::rethrow_exception to61// unpoison the stack.62int main() {63  // In some implementations of std::make_exception_ptr, e.g. libstdc++ prior to64  // gcc 7, this function calls __cxa_throw. The __cxa_throw is intercepted by65  // asan to unpoison the entire stack; since this test essentially tests that66  // the stack is unpoisoned by a call to std::rethrow_exception, we need to67  // generate the exception_ptr BEFORE we have the local variables poison the68  // stack.69  std::exception_ptr my_exception_ptr = std::make_exception_ptr("up");70 71  try {72    create_redzones_and_throw(my_exception_ptr);73  } catch(char const *) {74    assert(!__asan_region_is_poisoned(poisoned1, poisoned2 - poisoned1 + 1));75  }76}77