1 // This is a host program for DLL tests.
2 //
3 // Just make sure we can compile this.
4 // The actual compile&run sequence is to be done by the DLL tests.
5 // RUN: %clang_cl_asan -Od %s -Fe%t
6 //
7 // Get the list of ASan wrappers exported by the main module RTL:
8 // note: The mangling decoration (i.e. @4 )is removed because calling convention
9 //       differ from 32-bit and 64-bit.
10 // RUN: dumpbin /EXPORTS %t | grep -o "__asan_wrap[^ ]*" | sed -e s/@.*// > %t.exports1
11 //
12 // The exception handlers differ in 32-bit and 64-bit, so we ignore them:
13 // RUN: grep '[E]XPORT:' %s | sed -e 's/.*[E]XPORT: //' > %t.exports2
14 // EXPORT: __asan_wrap__except_handler3
15 // EXPORT: __asan_wrap__except_handler4
16 // EXPORT: __asan_wrap___C_specific_handler
17 //
18 // Get the list of ASan wrappers imported by the DLL RTL:
19 // [BEWARE: be really careful with the sed commands, as this test can be run
20 //  from different environments with different shells and seds]
21 // RUN: grep INTERCEPT_LIBRARY_FUNCTION %p/../../../../lib/asan/asan_win_dll_thunk.cpp \
22 // RUN:  | grep -v define | sed -e s/.*(/__asan_wrap_/ -e s/).*//              \
23 // RUN:  > %t.imports1
24 //
25 // Add functions interecepted in asan_malloc.win.cpp and asan_win.cpp.
26 // RUN: grep '[I]MPORT:' %s | sed -e 's/.*[I]MPORT: //' > %t.imports2
27 // IMPORT: __asan_wrap_HeapAlloc
28 // IMPORT: __asan_wrap_HeapFree
29 // IMPORT: __asan_wrap_HeapReAlloc
30 // IMPORT: __asan_wrap_HeapSize
31 // IMPORT: __asan_wrap_CreateThread
32 // IMPORT: __asan_wrap_RaiseException
33 // IMPORT: __asan_wrap_RtlRaiseException
34 // IMPORT: __asan_wrap_SetUnhandledExceptionFilter
35 // IMPORT: __asan_wrap_RtlSizeHeap
36 // IMPORT: __asan_wrap_RtlAllocateHeap
37 // IMPORT: __asan_wrap_RtlReAllocateHeap
38 // IMPORT: __asan_wrap_RtlFreeHeap
39 //
40 // RUN: cat %t.imports1 %t.imports2 | sort | uniq > %t.imports-sorted
41 // RUN: cat %t.exports1 %t.exports2 | sort | uniq > %t.exports-sorted
42 //
43 // Now make sure the DLL thunk imports everything:
44 // RUN: echo
45 // RUN: echo "=== NOTE === If you see a mismatch below, please update asan_win_dll_thunk.cpp"
46 // RUN: diff %t.imports-sorted %t.exports-sorted
47 // REQUIRES: asan-static-runtime
48 
49 #include <stdio.h>
50 #include <windows.h>
51 
main(int argc,char ** argv)52 int main(int argc, char **argv) {
53   if (argc != 2) {
54     printf("Usage: %s [client].dll\n", argv[0]);
55     return 101;
56   }
57 
58   const char *dll_name = argv[1];
59 
60   HMODULE h = LoadLibrary(dll_name);
61   if (!h) {
62     DWORD err = GetLastError();
63     printf("Could not load DLL: %s (code: %lu)!\n", dll_name, err);
64 
65     LPSTR buf;
66 
67     FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
68                        FORMAT_MESSAGE_IGNORE_INSERTS,
69                    NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 0,
70                    NULL);
71 
72     printf("Error: %s\n", buf);
73 
74     LocalFree(buf);
75 
76     return 102;
77   }
78 
79   typedef int (*test_function)();
80   test_function gf = (test_function)GetProcAddress(h, "test_function");
81   if (!gf) {
82     printf("Could not locate test_function in the DLL!\n");
83     FreeLibrary(h);
84     return 103;
85   }
86 
87   int ret = gf();
88 
89   FreeLibrary(h);
90   return ret;
91 }
92