reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
// Checks that the ASan debugging API for getting report information
// returns correct values.
// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s

#include <sanitizer/asan_interface.h>
#include <stdio.h>
#include <stdlib.h>

// FIXME: Doesn't work with DLLs
// XFAIL: win32-dynamic-asan

int main() {
  // Disable stderr buffering. Needed on Windows.
  setvbuf(stderr, NULL, _IONBF, 0);

  char *heap_ptr = (char *)malloc(10);
  free(heap_ptr);
  int present = __asan_report_present();
  fprintf(stderr, "%s\n", (present == 0) ? "no report" : "");
  // CHECK: no report
  heap_ptr[0] = 'A'; // BOOM
  return 0;
}

// If we use %p with MSVC, it comes out all upper case. Use %08x to get
// lowercase hex.
#ifdef _MSC_VER
# ifdef _WIN64
#  define PTR_FMT "0x%08llx"
# else
#  define PTR_FMT "0x%08x"
# endif
// Solaris libc omits the leading 0x.
#elif defined(__sun__) && defined(__svr4__)
# define PTR_FMT "0x%p"
#else
# define PTR_FMT "%p"
#endif

void __asan_on_error() {
  int present = __asan_report_present();
  void *pc = __asan_get_report_pc();
  void *bp = __asan_get_report_bp();
  void *sp = __asan_get_report_sp();
  void *addr = __asan_get_report_address();
  int is_write = __asan_get_report_access_type();
  size_t access_size = __asan_get_report_access_size();
  const char *description = __asan_get_report_description();

  fprintf(stderr, "%s\n", (present == 1) ? "report" : "");
  // CHECK: report
  fprintf(stderr, "pc: " PTR_FMT "\n", pc);
  // CHECK: pc: 0x[[PC:[0-9a-f]+]]
  fprintf(stderr, "bp: " PTR_FMT "\n", bp);
  // CHECK: bp: 0x[[BP:[0-9a-f]+]]
  fprintf(stderr, "sp: " PTR_FMT "\n", sp);
  // CHECK: sp: 0x[[SP:[0-9a-f]+]]
  fprintf(stderr, "addr: " PTR_FMT "\n", addr);
  // CHECK: addr: 0x[[ADDR:[0-9a-f]+]]
  fprintf(stderr, "type: %s\n", (is_write ? "write" : "read"));
  // CHECK: type: write
  fprintf(stderr, "access_size: %ld\n", access_size);
  // CHECK: access_size: 1
  fprintf(stderr, "description: %s\n", description);
  // CHECK: description: heap-use-after-free
}

// CHECK: AddressSanitizer: heap-use-after-free on address {{0x0*}}[[ADDR]] at pc {{0x0*}}[[PC]] bp {{0x0*}}[[BP]] sp {{0x0*}}[[SP]]
// CHECK: WRITE of size 1 at {{0x0*}}[[ADDR]] thread T0