brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · 6a5f8a1 Raw
64 lines · cpp
1// RUN: %clang_cl_asan %Od %MT -o %t %s2// RUN: %env_asan_opts=windows_hook_rtl_allocators=true %run %t 2>&1 | FileCheck %s3#include <cassert>4#include <iostream>5#include <sanitizer/allocator_interface.h>6#include <windows.h>7 8int main() {9  void *ptr = malloc(0);10  if (ptr)11    std::cerr << "allocated!\n";12 13  // Check the 'allocate 1 instead of 0' hack hasn't changed14  // Note that as of b3452d90b043a398639e62b0ab01aa339cc649de, dereferencing15  // the pointer will be detected as a heap-buffer-overflow.16  if (__sanitizer_get_allocated_size(ptr) != 1)17    return 1;18 19  free(ptr);20 21  /*22        HeapAlloc hack for our asan interceptor is to change 023        sized allocations to size 1 to avoid weird inconsistencies24        between how realloc and heaprealloc handle 0 size allocations.25 26        Note this test relies on these instructions being intercepted.27        Without ASAN HeapRealloc on line 27 would return a ptr whose28        HeapSize would be 0. This test makes sure that the underlying behavior29        of our hack hasn't changed underneath us.30 31        We can get rid of the test (or change it to test for the correct32        behavior) once we fix the interceptor or write a different allocator33        to handle 0 sized allocations properly by default.34 35    */36  ptr = HeapAlloc(GetProcessHeap(), 0, 0);37  if (!ptr)38    return 1;39  void *ptr2 = HeapReAlloc(GetProcessHeap(), 0, ptr, 0);40  if (!ptr2)41    return 1;42  size_t heapsize = HeapSize(GetProcessHeap(), 0, ptr2);43  if (heapsize != 1) { // will be 0 without ASAN turned on44    std::cerr << "HeapAlloc size failure! " << heapsize << " != 1\n";45    return 1;46  }47  void *ptr3 = HeapReAlloc(GetProcessHeap(), 0, ptr2, 3);48  if (!ptr3)49    return 1;50  heapsize = HeapSize(GetProcessHeap(), 0, ptr3);51 52  if (heapsize != 3) {53    std::cerr << "HeapAlloc size failure! " << heapsize << " != 3\n";54    return 1;55  }56  HeapFree(GetProcessHeap(), 0, ptr3);57  return 0;58}59 60// CHECK: allocated!61// CHECK-NOT: heap-buffer-overflow62// CHECK-NOT: AddressSanitizer63// CHECK-NOT: HeapAlloc size failure!64