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

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