Skip to content

Commit 3b2f04e

Browse files
committed
Add std::out_of_range exception test
1 parent 57fe37e commit 3b2f04e

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

a/dllmain.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <cstdio>
33
#include <exception>
44
#include <Windows.h>
5+
#include <string>
6+
#include <stdexcept>
57

68
#pragma comment(lib,"ws2_32.lib")
79
#pragma comment(lib,"wintrust.lib")
@@ -85,6 +87,11 @@ int exception(int exception_type) {
8587
throw '1';
8688
case 2:
8789
throw std::exception("2");
90+
case 3:
91+
{
92+
std::string s = "foo";
93+
s.at(10);
94+
}
8895
default:
8996
throw (DWORD64)-1;
9097
}
@@ -98,6 +105,10 @@ int exception(int exception_type) {
98105
printf("exception code = %c\n", val);
99106
return val - '0';
100107
}
108+
catch (const std::out_of_range& e) {
109+
printf("%s\n", e.what());
110+
return 3;
111+
}
101112
catch (std::exception val) {
102113
printf("exception code = %s\n", val.what());
103114
return 2;

test/test.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,89 @@ static PVOID ReadDllFile(LPCSTR FileName) {
1818
return buffer;
1919
}
2020

21-
int main() {
21+
int test_a_dll() {
22+
LPVOID buffer = ReadDllFile("a.dll");
23+
24+
HMEMORYMODULE m1 = nullptr, m2 = m1;
25+
HMODULE hModule = nullptr;
26+
FARPROC pfn = nullptr;
27+
DWORD MemoryModuleFeatures = 0;
28+
29+
typedef int(*_exception)(int code);
30+
_exception exception = nullptr;
31+
HRSRC hRsrc;
32+
DWORD SizeofRes;
33+
HGLOBAL gRes;
34+
char str[10];
35+
36+
LdrQuerySystemMemoryModuleFeatures(&MemoryModuleFeatures);
37+
if (MemoryModuleFeatures != MEMORY_FEATURE_ALL) {
38+
printf("not support all features on this version of windows.\n");
39+
}
40+
41+
if (!NT_SUCCESS(LdrLoadDllMemoryExW(&m1, nullptr, 0, buffer, 0, L"kernel64", nullptr))) goto end;
42+
LoadLibraryW(L"wininet.dll");
43+
if (!NT_SUCCESS(LdrLoadDllMemoryExW(&m2, nullptr, 0, buffer, 0, L"kernel128", nullptr))) goto end;
44+
45+
//forward export
46+
hModule = (HMODULE)m1;
47+
pfn = (decltype(pfn))(GetProcAddress(hModule, "Socket")); //ws2_32.WSASocketW
48+
pfn = (decltype(pfn))(GetProcAddress(hModule, "VerifyTruse")); //wintrust.WinVerifyTrust
49+
hModule = (HMODULE)m2;
50+
pfn = (decltype(pfn))(GetProcAddress(hModule, "Socket"));
51+
pfn = (decltype(pfn))(GetProcAddress(hModule, "VerifyTruse"));
52+
53+
//exception
54+
hModule = (HMODULE)m1;
55+
exception = (_exception)GetProcAddress(hModule, "exception");
56+
if (exception) {
57+
for (int i = 0; i < 5; ++i)exception(i);
58+
}
59+
60+
//tls
61+
pfn = GetProcAddress(hModule, "thread");
62+
if (pfn && pfn()) {
63+
printf("thread test failed.\n");
64+
}
65+
66+
//resource
67+
if (!LoadStringA(hModule, 101, str, 10)) {
68+
printf("load string failed.\n");
69+
}
70+
else {
71+
printf("%s\n", str);
72+
}
73+
if (!(hRsrc = FindResourceA(hModule, MAKEINTRESOURCEA(102), "BINARY"))) {
74+
printf("find binary resource failed.\n");
75+
}
76+
else {
77+
if ((SizeofRes = SizeofResource(hModule, hRsrc)) != 0x10) {
78+
printf("invalid res size.\n");
79+
}
80+
else {
81+
if (!(gRes = LoadResource(hModule, hRsrc))) {
82+
printf("load res failed.\n");
83+
}
84+
else {
85+
if (!LockResource(gRes))printf("lock res failed.\n");
86+
else {
87+
printf("resource test success.\n");
88+
}
89+
}
90+
}
91+
}
92+
93+
end:
94+
delete[]buffer;
95+
if (m1)LdrUnloadDllMemory(m1);
96+
FreeLibrary(LoadLibraryW(L"wininet.dll"));
97+
FreeLibrary(GetModuleHandleW(L"wininet.dll"));
98+
if (m2)LdrUnloadDllMemory(m2);
99+
100+
return 0;
101+
}
102+
103+
int test_user32() {
22104
HMODULE hModule;
23105
NTSTATUS status;
24106
PVOID buffer = ReadDllFile("C:\\Windows\\System32\\user32.dll");
@@ -49,3 +131,8 @@ int main() {
49131

50132
return 0;
51133
}
134+
135+
int main() {
136+
test_a_dll();
137+
return 0;
138+
}

0 commit comments

Comments
 (0)