aboutsummaryrefslogtreecommitdiff
path: root/TestDLL
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2019-09-29 00:33:30 +0200
committerToni Uhlig <matzeton@googlemail.com>2019-09-29 00:33:30 +0200
commitf7669d45f4a0acfdb83a7cbe5f9fd496acd9f907 (patch)
tree27da0d5d2ba287e404c486e9a175c2613bf84845 /TestDLL
parent5259dc69ebc108c4f5eec024a22ae80396fe39a7 (diff)
introduced new dynamic symbol resolver for CRT functions
Diffstat (limited to 'TestDLL')
-rw-r--r--TestDLL/dllmain.cpp132
1 files changed, 95 insertions, 37 deletions
diff --git a/TestDLL/dllmain.cpp b/TestDLL/dllmain.cpp
index 1097050..860a9a9 100644
--- a/TestDLL/dllmain.cpp
+++ b/TestDLL/dllmain.cpp
@@ -3,43 +3,103 @@
#include <vector>
#include <string>
#include <sstream>
-
-extern "C"
-BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
+#include <array>
#pragma comment(lib, "vcruntime.lib")
-typedef void(*myfree_t)(void *ptr);
-typedef void*(*mymalloc_t)(size_t n);
-typedef int(*_mycallnewh_t)(size_t n);
-static myfree_t myfree;
-static mymalloc_t mymalloc;
-static _mycallnewh_t _mycallnewh;
+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("malloc"), MSVCRT_ENTRY("free"), MSVCRT_ENTRY("_callnewh"),
+ 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("memchr"), MSVCRT_ENTRY("memcmp"), MSVCRT_ENTRY("memcpy")
+};
+static const SIZE_T resolved_symbols_size =
+sizeof(resolved_smybols) / sizeof(resolved_smybols[0]);
-void _invalid_parameter_noinfo_noreturn(void) {
+enum SymbolIndex {
+ 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_MEMCHR, SYM_MEMCMP, SYM_MEMCPY
+};
+
+#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__))
+
+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);
}
-
-extern "C"
-void * malloc(size_t n) {
- return mymalloc(n);
+WRAPPER_FUNCTION(SYM_ABORT, abort, void, void) {
+ RUN_REAL_FN(SYM_ABORT);
}
-
-extern "C"
-void free(void *ptr) {
- myfree(ptr);
+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);
}
+
extern "C"
-int _callnewh(size_t n) {
- return _mycallnewh(n);
-}
+static bool resolve_all_symbols(void) {
+ bool result = true;
-void MyFnResolve(void) {
+ 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;
@@ -47,34 +107,32 @@ void APIENTRY LibEntry(PVOID user_ptr)
if (firstEntry) {
firstEntry = false;
- HMODULE msvcrtModule = LoadLibraryA("msvcrt.dll");
- mymalloc = (mymalloc_t) GetProcAddress(msvcrtModule, "malloc");
- myfree = (myfree_t) GetProcAddress(msvcrtModule, "free");
- _mycallnewh = (_mycallnewh_t)GetProcAddress(msvcrtModule, "_callnewh");
- if (!mymalloc || !myfree || !_mycallnewh) {
+ 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::wstringstream muh;
+ //std::stringstream muh;
//muh << "bla" << "," << "blubb";
-#endif
MessageBoxA(NULL,
text.c_str(),
"TestDLL Notification",
MB_OK | MB_ICONINFORMATION);
-#if 0
- if (firstEntry &&
- !_CRT_INIT(NULL, DLL_PROCESS_ATTACH, NULL)) {
- MessageBoxA(NULL,
- "DllMain _CRT_INIT failed",
- "TestDLL Notification",
- MB_OK | MB_ICONINFORMATION);
- }
+#else
+ MessageBoxA(NULL,
+ "TEST !!!",
+ "TestDLL Notification",
+ MB_OK | MB_ICONINFORMATION);
#endif
}
} \ No newline at end of file