1#include <stdio.h>
2#include <windows.h>
3
4// RUN: %clang_cl_asan %LD %Od -DDLL %s %Fe%t.dll
5// RUN: %clang_cl %Od -DEXE %s %Fe%te.exe
6// RUN: %env_asan_opts=windows_hook_rtl_allocators=true not %run %te.exe %t.dll 2>&1 | FileCheck %s
7// REQUIRES: asan-dynamic-runtime
8// REQUIRES: asan-32-bits
9
10#include <cassert>
11#include <stdio.h>
12#include <windows.h>
13extern "C" {
14#if defined(EXE)
15
16int main(int argc, char **argv) {
17 void *region_without_hooks = HeapAlloc(GetProcessHeap(), 0, 10);
18 HMODULE lib = LoadLibraryA(argv[1]);
19 assert(lib != INVALID_HANDLE_VALUE);
20
21 void *region_w_hooks = HeapAlloc(GetProcessHeap(), 0, 10);
22 assert(region_w_hooks != nullptr);
23 assert(0 != FreeLibrary(lib));
24
25 fprintf(stderr, "WITHOUT:0x%08x\n", (unsigned int)region_without_hooks);
26 fprintf(stderr, "WITH:0x%08x\n", (unsigned int)region_w_hooks);
27
28 assert(0 != HeapFree(GetProcessHeap(), 0, region_without_hooks));
29 assert(0 != HeapFree(GetProcessHeap(), 0, region_w_hooks));
30
31 HeapFree(GetProcessHeap(), 0, region_w_hooks); //will dump
32}
33#elif defined(DLL)
34// This global is registered at startup.
35
36BOOL WINAPI DllMain(HMODULE, DWORD reason, LPVOID) {
37 fprintf(stderr, "in DLL(reason=%d)\n", (int)reason);
38 fflush(0);
39 return TRUE;
40}
41
42// CHECK: in DLL(reason=1)
43// CHECK: in DLL(reason=0)
44// CHECK: WITHOUT:[[WITHOUT:0x[0-9a-fA-F]+]]
45// CHECK: WITH:[[WITH:0x[0-9a-fA-F]+]]
46// CHECK: AddressSanitizer: attempting double-free on [[WITH]] in thread T0:
47
48#else
49#error oops!
50#endif
51}
52

source code of compiler-rt/test/asan/TestCases/Windows/dll_unload.cpp