|
| 1 | +// RUN: %clang_cl_asan %Od %s %Fe%t %MD |
| 2 | +// RUN: %env_asan_opts=windows_hook_rtl_allocators=true:halt_on_error=false not %run %t 2>&1 | FileCheck %s |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <windows.h> |
| 6 | + |
| 7 | +using AllocateFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, SIZE_T); |
| 8 | +using ReAllocateFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, PVOID, SIZE_T); |
| 9 | +using FreeFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, PVOID); |
| 10 | + |
| 11 | +int main() { |
| 12 | + HMODULE NtDllHandle = GetModuleHandle("ntdll.dll"); |
| 13 | + if (!NtDllHandle) { |
| 14 | + puts("Couldn't load ntdll??"); |
| 15 | + return -1; |
| 16 | + } |
| 17 | + |
| 18 | + auto RtlAllocateHeap_ptr = |
| 19 | + (AllocateFunctionPtr)GetProcAddress(NtDllHandle, "RtlAllocateHeap"); |
| 20 | + if (RtlAllocateHeap_ptr == 0) { |
| 21 | + puts("Couldn't RtlAllocateHeap"); |
| 22 | + return -1; |
| 23 | + } |
| 24 | + |
| 25 | + auto RtlReAllocateHeap_ptr = |
| 26 | + (ReAllocateFunctionPtr)GetProcAddress(NtDllHandle, "RtlReAllocateHeap"); |
| 27 | + if (RtlReAllocateHeap_ptr == 0) { |
| 28 | + puts("Couldn't find RtlReAllocateHeap"); |
| 29 | + return -1; |
| 30 | + } |
| 31 | + |
| 32 | + auto RtlFreeHeap_ptr = |
| 33 | + (FreeFunctionPtr)GetProcAddress(NtDllHandle, "RtlFreeHeap"); |
| 34 | + if (RtlFreeHeap_ptr == 0) { |
| 35 | + puts("Couldn't RtlFreeHeap"); |
| 36 | + return -1; |
| 37 | + } |
| 38 | + |
| 39 | + char *buffer; |
| 40 | + buffer = (char *)RtlAllocateHeap_ptr(GetProcessHeap(), 0, 48), |
| 41 | + |
| 42 | + RtlReAllocateHeap_ptr(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, buffer, |
| 43 | + 16); |
| 44 | + buffer[15] = 'a'; |
| 45 | + puts("Okay 15"); |
| 46 | + fflush(stdout); |
| 47 | + // CHECK: Okay 15 |
| 48 | + |
| 49 | + RtlReAllocateHeap_ptr(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, buffer, |
| 50 | + 32); |
| 51 | + buffer[31] = 'a'; |
| 52 | + puts("Okay 31"); |
| 53 | + fflush(stdout); |
| 54 | + // CHECK: Okay 31 |
| 55 | + |
| 56 | + buffer[32] = 'a'; |
| 57 | + // CHECK: AddressSanitizer: use-after-poison on address [[ADDR:0x[0-9a-f]+]] |
| 58 | + // CHECK: WRITE of size 1 at [[ADDR]] thread T0 |
| 59 | + |
| 60 | + RtlFreeHeap_ptr(GetProcessHeap(), 0, buffer); |
| 61 | +} |
0 commit comments