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