-
Notifications
You must be signed in to change notification settings - Fork 0
/
x32-abi-windows.cpp
302 lines (248 loc) · 7.96 KB
/
x32-abi-windows.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <Windows.h>
#include <PSApi.h>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <random>
#include <vector>
extern "C" IMAGE_DOS_HEADER __ImageBase;
#if defined (_M_ARM64)
#define ARCHITECTURE "arm-64"
#elif defined(_M_ARM)
#define ARCHITECTURE "arm-32"
#elif defined(_M_X64)
#define ARCHITECTURE "x86-64"
#else
#define ARCHITECTURE "x86-32"
#define NATIVE_X86_32 1
#endif
BOOL (WINAPI * ptrIsWow64Process2) (HANDLE, USHORT *, USHORT *) = NULL;
VOID (WINAPI * ptrGetSystemTimeAsFileTime) (LPFILETIME) = NULL; // GetSystemTimePreciseAsFileTime
template <typename P>
bool symbol (HMODULE h, P & pointer, const char * name) {
if (P p = reinterpret_cast <P> (GetProcAddress (h, name))) {
pointer = p;
return true;
} else
return false;
}
const char * platform () {
if (ptrIsWow64Process2) {
USHORT emulated = 0;
USHORT native = 0;
if (ptrIsWow64Process2 (GetCurrentProcess (), &emulated, &native)) {
if (emulated) {
switch (native) {
case IMAGE_FILE_MACHINE_IA64: return "itanium";
case IMAGE_FILE_MACHINE_AMD64: return "x86-64";
case IMAGE_FILE_MACHINE_ARM64: return "arm-64";
default:
return "unexpected";
}
} else
return "native";
}
} else {
BOOL wow = FALSE;
if (IsWow64Process (GetCurrentProcess (), &wow)) {
if (wow)
return "x86-64";
else
return "native";
}
}
return "error";
}
std::mt19937 random_generator;
std::uniform_int_distribution <long> random_distribution;
template <typename T>
struct short_ptr {
long pointer;
public:
short_ptr () noexcept
: pointer (0) {};
short_ptr (T * native) noexcept
: pointer (long (reinterpret_cast <std::intptr_t> (native))) {}
short_ptr (short_ptr && other) noexcept
: pointer (other.pointer) {}
short_ptr (const short_ptr & other) noexcept
: pointer (other.pointer) {}
short_ptr & operator = (short_ptr && other) noexcept {
this->pointer = other.pointer;
return *this;
}
short_ptr & operator = (const short_ptr & other) noexcept {
this->pointer = other.pointer;
return *this;
}
T * operator -> () {
return reinterpret_cast <T *> (std::intptr_t (this->pointer));
}
const T * operator -> () const {
return reinterpret_cast <const T *> (std::intptr_t (this->pointer));
}
operator T * () {
return reinterpret_cast <T *> (std::intptr_t (this->pointer));
}
operator const T * () const {
return reinterpret_cast <const T *> (std::intptr_t (this->pointer));
}
};
struct short_ptr_node {
typedef short_ptr <short_ptr_node> ptr_type;
ptr_type a = nullptr;
char cache_line_breaker_a [64 - sizeof (ptr_type)];
ptr_type b = nullptr;
char cache_line_breaker_b [64 - sizeof (ptr_type)];
ptr_type c = nullptr;
long data = random_distribution (random_generator);
};
struct naked_ptr_node {
typedef naked_ptr_node * ptr_type;
ptr_type a = nullptr;
char cache_line_breaker_a [64 - sizeof (ptr_type)];
ptr_type b = nullptr;
char cache_line_breaker_b [64 - sizeof (ptr_type)];
ptr_type c = nullptr;
long data = random_distribution (random_generator);
};
template <typename T>
T * pick (T * p, long i) {
switch (i % 3) {
case 0: return p->a;
case 1: return p->b;
case 2: return p->c;
}
return nullptr;
}
template <typename T>
T * walk (T * p, long x = 0) {
while (auto next = pick (p, p->data ^ x)) {
p = next;
x /= 3;
}
return p;
}
std::size_t allocated = 0;
template <typename T>
T * make (std::vector <T *> & pregenerated) {
if (random_distribution (random_generator) % 8) { // leave some NULL
if (pregenerated.empty ()) {
pregenerated.resize (1024 * 1024);
for (auto & pg : pregenerated) {
pg = new T;
}
std::shuffle (pregenerated.begin (), pregenerated.end (), random_generator);
}
auto p = pregenerated.back ();
pregenerated.pop_back ();
++allocated;
return p;
} else
return nullptr;
}
template <typename T>
void build (T * parent, std::size_t depth, std::vector <T *> & pregenerated) {
parent->a = make (pregenerated);
parent->b = make (pregenerated);
parent->c = make (pregenerated);
if (depth--) {
if (parent->a) build (( T *) parent->a, depth, pregenerated);
if (parent->b) build (( T *) parent->b, depth, pregenerated);
if (parent->c) build (( T *) parent->c, depth, pregenerated);
}
}
struct time {
union {
FILETIME ft;
long long ll = 0;
};
time & update () {
ptrGetSystemTimeAsFileTime (&this->ft);
return *this;
}
time localize () {
time localized;
FileTimeToLocalFileTime (&this->ft, &localized.ft);
return localized;
}
SYSTEMTIME decode () {
SYSTEMTIME st;
FileTimeToSystemTime (&this->ft, &st);
return st;
}
double difference (const time & earlier) const {
return (this->ll - earlier.ll) / 10'000'000.0; // convert to seconds
}
};
time t;
void log (const char * format, ...) {
time tt;
auto st = tt.update ().localize ().decode ();
va_list args; \
va_start (args, format);
std::printf ("%02u:%02u:%02u.%03u [%.2f] ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, tt.difference (t));
std::vprintf (format, args);
std::printf ("\n");
va_end (args);
t.update ();
}
template <typename T>
void test () {
static constexpr auto DEPTH = 15u;
static constexpr auto WALKS = 256*65536u;
T root;
try {
std::vector <T *> pregenerated;
build (&root, DEPTH, pregenerated);
log ("built tree of %zu nodes in %u levels", allocated, DEPTH);
PROCESS_MEMORY_COUNTERS memory;
memory.cb = sizeof memory;
GetProcessMemoryInfo (GetCurrentProcess (), &memory, sizeof memory);
log ("using %.1f MB to store %.1f MB of data (%.2f%% overhead)",
memory.PagefileUsage / 1048576.0,
(long long) allocated * sizeof (T) / 1048576.0,
100.0 * memory.PagefileUsage / (long long) (allocated * sizeof (T)) - 100.0
);
;
std::uintmax_t sum = 0;
for (auto x = 0u; x != WALKS; ++x) {
sum += walk (&root, x)->data;
}
log ("walked tree %u times, sum of data is %zu", WALKS, sum);
} catch (const std::bad_alloc &) {
log ("bad alloc");
}
}
int main () {
// some APIs
if (auto hKernel32 = GetModuleHandle (L"KERNEL32")) {
symbol (hKernel32, ptrIsWow64Process2, "IsWow64Process2");
symbol (hKernel32, ptrGetSystemTimeAsFileTime, "GetSystemTimePreciseAsFileTime");
}
if (ptrGetSystemTimeAsFileTime == NULL) {
ptrGetSystemTimeAsFileTime = GetSystemTimeAsFileTime;
}
// make more sure we crash in case of issue
HeapSetInformation (GetProcessHeap (), HeapEnableTerminationOnCorruption, NULL, 0);
// platform info
auto header = reinterpret_cast <const IMAGE_NT_HEADERS *> (reinterpret_cast <const char *> (&__ImageBase) + __ImageBase.e_lfanew);
bool laa = header->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE;
std::printf ("X32-ABI on WINDOWS test; " ARCHITECTURE " on %s, large addresses %s\n\n", platform (), laa ? "enabled" : "DISABLED");
// start
t.update ();
#ifdef NATIVE_X86_32
log ("native...");
test <naked_ptr_node> ();
#else
if (laa) {
log ("naked_ptr_node...");
test <naked_ptr_node> ();
} else {
log ("short_ptr_node ...");
test <short_ptr_node> ();
}
#endif
log ("done.");
return 0;
}