asan_dll_thunk.cc revision 43e62df906327f6ffa492edb933af1195143d149
1//===-- asan_dll_thunk.cc -------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// This file defines a family of thunks that should be statically linked into
13// the DLLs that have ASan instrumentation in order to delegate the calls to the
14// shared runtime that lives in the main binary.
15// See https://code.google.com/p/address-sanitizer/issues/detail?id=209 for the
16// details.
17//===----------------------------------------------------------------------===//
18
19// Only compile this code when buidling asan_dll_thunk.lib
20// Using #ifdef rather than relying on Makefiles etc.
21// simplifies the build procedure.
22#ifdef ASAN_DLL_THUNK
23
24// ----------------- Helper functions and macros --------------------- {{{1
25extern "C" {
26void *__stdcall GetModuleHandleA(const char *module_name);
27void *__stdcall GetProcAddress(void *module, const char *proc_name);
28void abort();
29}
30
31static void *getRealProcAddressOrDie(const char *name) {
32  void *ret = GetProcAddress(GetModuleHandleA(0), name);
33  if (!ret)
34    abort();
35  return ret;
36}
37
38#define WRAP_V_V(name)                                                         \
39  extern "C" void name() {                                                     \
40    typedef void (*fntype)();                                                  \
41    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
42    fn();                                                                      \
43  }
44
45#define WRAP_V_W(name)                                                         \
46  extern "C" void name(void *arg) {                                            \
47    typedef void (*fntype)(void *arg);                                         \
48    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
49    fn(arg);                                                                   \
50  }
51
52#define WRAP_V_WW(name)                                                        \
53  extern "C" void name(void *arg1, void *arg2) {                               \
54    typedef void (*fntype)(void *, void *);                                    \
55    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
56    fn(arg1, arg2);                                                            \
57  }
58
59#define WRAP_W_W(name)                                                         \
60  extern "C" void *name(void *arg) {                                           \
61    typedef void *(*fntype)(void *arg);                                        \
62    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
63    return fn(arg);                                                            \
64  }
65
66#define WRAP_W_WW(name)                                                        \
67  extern "C" void *name(void *arg1, void *arg2) {                              \
68    typedef void *(*fntype)(void *, void *);                                   \
69    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
70    return fn(arg1, arg2);                                                     \
71  }
72
73#define WRAP_W_WWW(name)                                                       \
74  extern "C" void *name(void *arg1, void *arg2, void *arg3) {                  \
75    typedef void *(*fntype)(void *, void *, void *);                           \
76    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
77    return fn(arg1, arg2, arg3);                                               \
78  }
79
80#define WRAP_W_WWWW(name)                                                      \
81  extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) {      \
82    typedef void *(*fntype)(void *, void *, void *, void *);                   \
83    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
84    return fn(arg1, arg2, arg3, arg4);                                         \
85  }
86
87#define WRAP_W_WWWWW(name)                                                     \
88  extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4,        \
89                        void *arg5) {                                          \
90    typedef void *(*fntype)(void *, void *, void *, void *, void *);           \
91    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
92    return fn(arg1, arg2, arg3, arg4, arg5);                                   \
93  }
94
95#define WRAP_W_WWWWWW(name)                                                    \
96  extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4,        \
97                        void *arg5, void *arg6) {                              \
98    typedef void *(*fntype)(void *, void *, void *, void *, void *, void *);   \
99    fntype fn = (fntype)getRealProcAddressOrDie(#name);                        \
100    return fn(arg1, arg2, arg3, arg4, arg5, arg6);                             \
101  }
102// }}}
103
104// ----------------- ASan own interface functions --------------------
105WRAP_V_V(__asan_init_v3)
106
107WRAP_V_W(__asan_report_store1)
108WRAP_V_W(__asan_report_store2)
109WRAP_V_W(__asan_report_store4)
110WRAP_V_W(__asan_report_store8)
111WRAP_V_W(__asan_report_store16)
112WRAP_V_WW(__asan_report_store_n)
113
114WRAP_V_W(__asan_report_load1)
115WRAP_V_W(__asan_report_load2)
116WRAP_V_W(__asan_report_load4)
117WRAP_V_W(__asan_report_load8)
118WRAP_V_W(__asan_report_load16)
119WRAP_V_WW(__asan_report_load_n)
120
121WRAP_V_WW(__asan_register_globals)
122WRAP_V_WW(__asan_unregister_globals)
123
124// TODO(timurrrr): Add more interface functions on the as-needed basis.
125
126// ----------------- Memory allocation functions ---------------------
127WRAP_V_W(free)
128WRAP_V_WW(_free_dbg)
129
130WRAP_W_W(malloc)
131WRAP_W_WWWW(_malloc_dbg)
132
133WRAP_W_WW(calloc)
134WRAP_W_WWWWW(_calloc_dbg)
135WRAP_W_WWW(_calloc_impl)
136
137WRAP_W_WW(realloc)
138WRAP_W_WWW(_realloc_dbg)
139WRAP_W_WWW(_recalloc)
140
141WRAP_W_W(_msize)
142
143// TODO(timurrrr): Do we need to add _Crt* stuff here? (see asan_malloc_win.cc).
144
145#endif // ASAN_DLL_THUNK
146