| 1 | // RUN: %clang_cl_asan %Od %s %Fe%t %MD |
| 2 | // RUN: %env_asan_opts=windows_hook_rtl_allocators=true %run %t 2>&1 | FileCheck %s |
| 3 | // UNSUPPORTED: asan-64-bits |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include <stdio.h> |
| 7 | #include <windows.h> |
| 8 | |
| 9 | extern "C" int __sanitizer_get_ownership(const volatile void *p); |
| 10 | using AllocateFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, SIZE_T); |
| 11 | using FreeFunctionPtr = PVOID(__stdcall *)(PVOID, ULONG, PVOID); |
| 12 | |
| 13 | int main() { |
| 14 | HMODULE NtDllHandle = GetModuleHandle("ntdll.dll" ); |
| 15 | if (!NtDllHandle) { |
| 16 | puts(s: "Couldn't load ntdll??" ); |
| 17 | return -1; |
| 18 | } |
| 19 | |
| 20 | auto RtlAllocateHeap_ptr = (AllocateFunctionPtr)GetProcAddress(NtDllHandle, "RtlAllocateHeap" ); |
| 21 | if (RtlAllocateHeap_ptr == 0) { |
| 22 | puts(s: "Couldn't RtlAllocateHeap" ); |
| 23 | return -1; |
| 24 | } |
| 25 | |
| 26 | auto RtlFreeHeap_ptr = (FreeFunctionPtr)GetProcAddress(NtDllHandle, "RtlFreeHeap" ); |
| 27 | if (RtlFreeHeap_ptr == 0) { |
| 28 | puts(s: "Couldn't RtlFreeHeap" ); |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | char *winbuf; |
| 33 | char *asanbuf; |
| 34 | winbuf = (char *)RtlAllocateHeap_ptr(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, 32), |
| 35 | asanbuf = (char *)RtlAllocateHeap_ptr(GetProcessHeap(), 0, 32), |
| 36 | winbuf[0] = 'a'; |
| 37 | assert(!__sanitizer_get_ownership(winbuf)); |
| 38 | assert(__sanitizer_get_ownership(asanbuf)); |
| 39 | |
| 40 | RtlFreeHeap_ptr(GetProcessHeap(), 0, winbuf); |
| 41 | RtlFreeHeap_ptr(GetProcessHeap(), 0, asanbuf); |
| 42 | puts(s: "Okay" ); |
| 43 | // CHECK: Okay |
| 44 | } |
| 45 | |