brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · da1f5f2 Raw
37 lines · cpp
1// RUN: %clangxx_asan -O0 %s -o %t && %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s2// RUN: %clangxx_asan -O0 %s -o %t -fsanitize-address-use-after-return=always && not %run %t 2>&1 | FileCheck %s3 4#include "defines.h"5#include <stdint.h>6#include <string.h>7 8#define kFrameSize (2048)9#define KFrameSizeMask (0x07ff)10 11ATTRIBUTE_NOINLINE12char *pretend_to_do_something(char *x) {13  __asm__ __volatile__("" : : "r"(x) : "memory");14  return x;15}16 17ATTRIBUTE_NOINLINE18char *OverwriteFakeFrameLastWord() {19  char x[1024];20  memset(x, 0, sizeof(x));21  uint64_t ptr_int = (reinterpret_cast<uint64_t>(x) & ~KFrameSizeMask) +22                     kFrameSize - sizeof(char **);23  char **ptr = reinterpret_cast<char **>(ptr_int);24  *ptr = nullptr;25  return pretend_to_do_something(x);26}27 28int main(int argc, char **argv) {29  char *x = OverwriteFakeFrameLastWord();30  // CHECK: ERROR: AddressSanitizer: stack-buffer-overflow on address31  // CHECK: is located in stack of thread T0 at offset {{2040|2044}} in frame32  // CHECK:  in OverwriteFakeFrameLastWord{{.*}}fakeframe-right-redzone.cpp:33  // CHECK: [{{16|32}}, {{1040|1056}}) 'x'34  pretend_to_do_something(x);35  return 0;36}37