xref: /llvm-project/compiler-rt/test/asan/TestCases/Windows/dll_host.cpp (revision 0360f3218a13666123849f6699216bdbebe64833)
1 // UNSUPPORTED: target={{.*-windows-gnu}}
2 
3 // This is a host program for DLL tests.
4 //
5 // Just make sure we can compile this.
6 // The actual compile&run sequence is to be done by the DLL tests.
7 // RUN: %clang_cl_asan -Od %s -Fe%t
8 
9 #include <stdio.h>
10 #include <windows.h>
11 
main(int argc,char ** argv)12 int main(int argc, char **argv) {
13   if (argc != 2) {
14     printf("Usage: %s [client].dll\n", argv[0]);
15     return 101;
16   }
17 
18   const char *dll_name = argv[1];
19 
20   HMODULE h = LoadLibrary(dll_name);
21   if (!h) {
22     DWORD err = GetLastError();
23     printf("Could not load DLL: %s (code: %lu)!\n", dll_name, err);
24 
25     LPSTR buf;
26 
27     FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
28                        FORMAT_MESSAGE_IGNORE_INSERTS,
29                    NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 0,
30                    NULL);
31 
32     printf("Error: %s\n", buf);
33 
34     LocalFree(buf);
35 
36     return 102;
37   }
38 
39   typedef int (*test_function)();
40   test_function gf = (test_function)GetProcAddress(h, "test_function");
41   if (!gf) {
42     printf("Could not locate test_function in the DLL!\n");
43     FreeLibrary(h);
44     return 103;
45   }
46 
47   int ret = gf();
48 
49   FreeLibrary(h);
50   return ret;
51 }
52