1 // RUN: %clang_cl_asan -Od %s -Fe%t /MD
2 // RUN: %env_asan_opts=windows_hook_rtl_allocators=true not %run %t 2>&1 | FileCheck %s
3 // UNSUPPORTED: asan-64-bits
4 // REQUIRES: asan-rtl-heap-interception
5 
6 #include <stdio.h>
7 #include <windows.h>
8 
9 using AllocateFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, SIZE_T);
10 using FreeFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, PVOID);
11 
12 int main() {
13   HMODULE NtDllHandle = GetModuleHandle("ntdll.dll");
14   if (!NtDllHandle) {
15     puts("Couldn't load ntdll??");
16     return -1;
17   }
18 
19   auto RtlAllocateHeap_ptr = (AllocateFunctionPtr)GetProcAddress(NtDllHandle, "RtlAllocateHeap");
20   if (RtlAllocateHeap_ptr == 0) {
21     puts("Couldn't RtlAllocateHeap");
22     return -1;
23   }
24 
25   char *buffer;
26   buffer = (char *)RtlAllocateHeap_ptr(GetProcessHeap(), 0, 32),
27   buffer[33] = 'a';
28   // CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
29   // CHECK: WRITE of size 1 at [[ADDR]] thread T0
30 }
31