1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include "stdafx.h"
#include <vector>
#include <string>
#include <sstream>
#include <array>
#pragma comment(lib, "vcruntime.lib")
struct ResolvedDllEntry {
const char * const baseDllName;
const char * const functionName;
HMODULE moduleBase;
FARPROC resolvedProc;
};
#define DLL_ENTRY(dll_name, function_name) \
{ dll_name, function_name, NULL, NULL }
#define MSVCRT_ENTRY(function_name) DLL_ENTRY("msvcrt.dll", function_name)
static struct ResolvedDllEntry resolved_smybols[] = {
MSVCRT_ENTRY("_errno"),
MSVCRT_ENTRY("malloc"), MSVCRT_ENTRY("free"), MSVCRT_ENTRY("_callnewh"),
MSVCRT_ENTRY("_invalid_parameter_noinfo_noreturn"),
MSVCRT_ENTRY("abort"), MSVCRT_ENTRY("calloc"), MSVCRT_ENTRY("frexp"),
MSVCRT_ENTRY("islower"), MSVCRT_ENTRY("isspace"), MSVCRT_ENTRY("isupper"),
MSVCRT_ENTRY("ldexp"), MSVCRT_ENTRY("localeconv"), MSVCRT_ENTRY("__pctype_func"),
MSVCRT_ENTRY("___lc_locale_name_func"), MSVCRT_ENTRY("___lc_codepage_func")
};
static const SIZE_T resolved_symbols_size =
sizeof(resolved_smybols) / sizeof(resolved_smybols[0]);
enum SymbolIndex {
SYM_ERRNO,
SYM_MALLOC, SYM_FREE, SYM_CALLNEWH,
SYM_INVALID_PARAMETER_NOINFO_NORETURN,
SYM_ABORT, SYM_CALLOC, SYM_FREXP,
SYM_ISLOWER, SYM_ISSPACE, SYM_ISUPPER,
SYM_LDEXP, SYM_LOCALECONV, SYM_PCTYPE,
SYM_LC_LOCALE_NAME, SYM_LC_CODEPAGE,
NUMBER_OF_SYMBOLS
};
static_assert(NUMBER_OF_SYMBOLS == resolved_symbols_size, "Invalid number of Symbols in the table/enum");
#define WRAPPER_FUNCTION(symbol_index, linker_function_name, return_type, ...) \
typedef return_type (* symbol_index ## _FN)(__VA_ARGS__); \
extern "C" \
return_type linker_function_name(__VA_ARGS__)
#define RUN_REAL_FN(symbol_index, ...) \
(((symbol_index ## _FN)resolved_smybols[symbol_index].resolvedProc)(__VA_ARGS__))
int* __cdecl _errno(void) {
typedef int*(*SYM_ERRNO_FN)();
return (((SYM_ERRNO_FN)resolved_smybols[SYM_ERRNO].resolvedProc)());
}
WRAPPER_FUNCTION(SYM_MALLOC, malloc, void *, size_t n) {
return RUN_REAL_FN(SYM_MALLOC, n);
}
WRAPPER_FUNCTION(SYM_FREE, free, void, void *p) {
RUN_REAL_FN(SYM_FREE, p);
}
WRAPPER_FUNCTION(SYM_CALLNEWH, _callnewh, int, size_t n) {
return RUN_REAL_FN(SYM_CALLNEWH, n);
}
WRAPPER_FUNCTION(SYM_INVALID_PARAMETER_NOINFO_NORETURN,
_invalid_parameter_noinfo_noreturn, void, void) {
ExitProcess(1);
}
WRAPPER_FUNCTION(SYM_ABORT, abort, void, void) {
RUN_REAL_FN(SYM_ABORT);
}
WRAPPER_FUNCTION(SYM_CALLOC, calloc, void *, size_t n, size_t s) {
return RUN_REAL_FN(SYM_CALLOC, n, s);
}
WRAPPER_FUNCTION(SYM_FREXP, frexp, double, double x, int *expptr) {
return RUN_REAL_FN(SYM_FREXP, x, expptr);
}
WRAPPER_FUNCTION(SYM_ISLOWER, islower, int, int c) {
return RUN_REAL_FN(SYM_ISLOWER, c);
}
WRAPPER_FUNCTION(SYM_ISSPACE, isspace, int, int c) {
return RUN_REAL_FN(SYM_ISSPACE, c);
}
WRAPPER_FUNCTION(SYM_ISUPPER, isupper, int, int c) {
return RUN_REAL_FN(SYM_ISUPPER, c);
}
WRAPPER_FUNCTION(SYM_LDEXP, ldexp, double, double x, int exp) {
return RUN_REAL_FN(SYM_LDEXP, x, exp);
}
WRAPPER_FUNCTION(SYM_LOCALECONV, localeconv, struct lconv *, void) {
return RUN_REAL_FN(SYM_LOCALECONV);
}
WRAPPER_FUNCTION(SYM_PCTYPE, __pctype_func, const unsigned short *, void) {
return RUN_REAL_FN(SYM_PCTYPE);
}
WRAPPER_FUNCTION(SYM_LC_LOCALE_NAME, ___lc_locale_name_func, wchar_t **, void) {
return RUN_REAL_FN(SYM_LC_LOCALE_NAME);
}
WRAPPER_FUNCTION(SYM_LC_CODEPAGE, ___lc_codepage_func, UINT, void) {
return RUN_REAL_FN(SYM_LC_CODEPAGE);
}
extern "C"
static bool resolve_all_symbols(void) {
bool result = true;
for (SIZE_T i = 0; i < 3; ++i) {
resolved_smybols[i].moduleBase = LoadLibraryA(resolved_smybols[i].baseDllName);
if (!resolved_smybols[i].moduleBase) {
result = false;
continue;
}
resolved_smybols[i].resolvedProc = GetProcAddress(resolved_smybols[i].moduleBase,
resolved_smybols[i].functionName);
if (!resolved_smybols[i].resolvedProc) {
result = false;
}
}
return result;
}
void APIENTRY LibEntry(PVOID user_ptr)
{
static bool firstEntry = true;
if (firstEntry) {
firstEntry = false;
if (!resolve_all_symbols()) {
MessageBoxA(NULL,
"COULD NOT RESOLVE ALL DYNAMIC DLL SYMBOLS !!!",
"TestDLL Notification",
MB_OK | MB_ICONINFORMATION);
return;
}
void *bla = malloc(10);
free(bla);
#if 1
std::string text;
std::vector<DWORD> blubb;
text = "DllMain from TestDLL: ";
blubb.push_back(1);
blubb.push_back(2);
//std::stringstream muh;
//muh << "bla" << "," << "blubb";
MessageBoxA(NULL,
text.c_str(),
"TestDLL Notification",
MB_OK | MB_ICONINFORMATION);
#else
MessageBoxA(NULL,
"TEST !!!",
"TestDLL Notification",
MB_OK | MB_ICONINFORMATION);
#endif
}
}
|