1// RUN: %clang_cl_asan %LD %Od -DDLL %s %Fe%t.dll
2// RUN: %clang_cl_asan %Od -DEXE %s %Fe%te.exe
3// RUN: %env_asan_opts=report_globals=2 %run %te.exe %t.dll 2>&1 | FileCheck %s
4
5#include <windows.h>
6#include <stdio.h>
7#include <string.h>
8
9extern "C" {
10#if defined(EXE)
11int main(int argc, char **argv) {
12 if (argc != 2) {
13 printf("Usage: %s [client].dll\n", argv[0]);
14 return 101;
15 }
16 const char *dll_name = argv[1];
17
18// CHECK: time to load DLL
19 printf("time to load DLL\n");
20 fflush(0);
21
22// On DLL load, the "in DLL\n" string is registered:
23// CHECK: Added Global{{.*}} size=19
24// CHECK: in DLL(reason=1)
25 HMODULE dll = LoadLibrary(dll_name);
26 if (dll == NULL)
27 return 3;
28
29// CHECK: in DLL(reason=0)
30// CHECK-NEXT: Removed Global{{.*}} size=19
31 if (!FreeLibrary(dll))
32 return 4;
33
34// CHECK: bye!
35 printf("bye!\n");
36 fflush(0);
37}
38#elif defined(DLL)
39BOOL WINAPI DllMain(HMODULE, DWORD reason, LPVOID) {
40 printf("in DLL(reason=%d)\n", (int)reason);
41 fflush(0);
42 return TRUE;
43}
44#else
45# error oops!
46#endif
47}
48

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