-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathbootstrap.c
More file actions
461 lines (387 loc) · 15.8 KB
/
bootstrap.c
File metadata and controls
461 lines (387 loc) · 15.8 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "bootstrap.h"
#include "config/config.h"
#include "crt.h"
#include "runtimes/coreclr.h"
#include "runtimes/il2cpp.h"
#include "runtimes/mono.h"
#include "util/logging.h"
#include "util/paths.h"
#include "util/util.h"
bool_t mono_debug_init_called = FALSE;
bool_t mono_is_net35 = FALSE;
void mono_doorstop_bootstrap(void *mono_domain) {
if (getenv(TEXT("DOORSTOP_INITIALIZED"))) {
LOG("DOORSTOP_INITIALIZED is set! Skipping!");
return;
}
setenv(TEXT("DOORSTOP_INITIALIZED"), TEXT("TRUE"), TRUE);
mono.thread_set_main(mono.thread_current());
char_t *app_path = program_path();
if (mono.domain_set_config) {
#define CONFIG_EXT TEXT(".config")
char_t *config_path =
calloc(strlen(app_path) + 1 + STR_LEN(CONFIG_EXT), sizeof(char_t));
strcpy(config_path, app_path);
strcat(config_path, CONFIG_EXT);
char_t *folder_path = get_folder_name(app_path);
char *config_path_n = narrow(config_path);
char *folder_path_n = narrow(folder_path);
LOG("Setting config paths: base dir: %s; config path: %s\n",
folder_path, config_path);
mono.domain_set_config(mono_domain, folder_path_n, config_path_n);
free(folder_path);
free(config_path);
free(config_path_n);
free(folder_path_n);
#undef CONFIG_EXT
}
setenv(TEXT("DOORSTOP_INVOKE_NATIVE_DLL_PATH"), config.target_native_library, TRUE);
if (config.target_native_library && config.target_native_library[0] != '\0') {
LOG("Native plug‑in configured: %s", config.target_native_library);
void *native_handle = dlopen(config.target_native_library, 0);
if (!native_handle) {
LOG("Failed to load native library: %s",
config.target_native_library);
} else {
typedef void (*plugin_main_t)(void);
plugin_main_t pluginMain =
(plugin_main_t)dlsym(native_handle, "PluginMain");
if (!pluginMain) {
pluginMain = (plugin_main_t)dlsym(native_handle, "Start");
}
#ifdef _WIN32
if (!pluginMain) {
// As a last resort on Windows, manually invoke DllMain with
// DLL_PROCESS_ATTACH.
typedef BOOL(WINAPI * DllMain_t)(HMODULE, DWORD, LPVOID);
DllMain_t dllMain = (DllMain_t)dlsym(native_handle, "DllMain");
if (dllMain) {
HMODULE mod = (HMODULE)native_handle;
LOG("Calling DllMain for %s", config.target_native_library);
dllMain(mod, DLL_PROCESS_ATTACH, NULL);
}
}
#endif
if (pluginMain) {
LOG("Calling native entrypoint for %s",
config.target_native_library);
pluginMain();
} else {
LOG("No PluginMain/Start/DllMain found in %s",
config.target_native_library);
}
}
}
setenv(TEXT("DOORSTOP_INVOKE_DLL_PATH"), config.target_assembly, TRUE);
setenv(TEXT("DOORSTOP_PROCESS_PATH"), app_path, TRUE);
char *assembly_dir = mono.assembly_getrootdir();
char_t *norm_assembly_dir = widen(assembly_dir);
mono.config_parse(NULL);
LOG("Assembly dir: %s", norm_assembly_dir);
setenv(TEXT("DOORSTOP_MANAGED_FOLDER_DIR"), norm_assembly_dir, TRUE);
free(norm_assembly_dir);
LOG("Opening assembly: %s", config.target_assembly);
void *file = fopen(config.target_assembly, "r");
if (!file) {
LOG("Failed to open assembly: %s", config.target_assembly);
return;
}
size_t size = get_file_size(file);
void *data = malloc(size);
fread(data, size, 1, file);
fclose(file);
LOG("Opened Assembly DLL (%d bytes); opening its main image", size);
char *dll_path = narrow(config.target_assembly);
MonoImageOpenStatus s = MONO_IMAGE_OK;
void *image = mono.image_open_from_data_with_name(data, size, TRUE, &s,
FALSE, dll_path);
free(data);
if (s != MONO_IMAGE_OK) {
LOG("Failed to load assembly image: %s. Got result: %d\n",
config.target_assembly, s);
return;
}
LOG("Image opened; loading included assembly");
s = MONO_IMAGE_OK;
void *assembly = mono.assembly_load_from_full(image, dll_path, &s, FALSE);
free(dll_path);
if (s != MONO_IMAGE_OK) {
LOG("Failed to load assembly: %s. Got result: %d\n",
config.target_assembly, s);
return;
}
LOG("Assembly loaded; looking for Doorstop.Entrypoint:Start");
void *desc = mono.method_desc_new("Doorstop.Entrypoint:Start", TRUE);
void *method = mono.method_desc_search_in_image(desc, image);
mono.method_desc_free(desc);
if (!method) {
LOG("Failed to find method Doorstop.Entrypoint:Start");
return;
}
void *signature = mono.method_signature(method);
unsigned int params = mono.signature_get_param_count(signature);
if (params != 0) {
LOG("Method has %d parameters; expected 0", params);
return;
}
LOG("Invoking method %p", method);
void *exc = NULL;
mono.runtime_invoke(method, NULL, NULL, &exc);
if (exc != NULL) {
LOG("Error invoking code!");
if (mono.object_to_string) {
void *str = mono.object_to_string(exc, NULL);
char *exc_str_n = mono.string_to_utf8(str);
char_t *exc_str = widen(exc_str_n);
LOG("Error message: %s", exc_str);
LOG("\n");
free(exc_str);
mono.free(exc_str_n);
}
}
LOG("Done");
free(app_path);
}
void *init_mono(const char *root_domain_name, const char *runtime_version) {
char_t *root_domain_name_w = widen(root_domain_name);
char_t *runtime_version_w = widen(runtime_version);
LOG("Starting mono domain \"%s\"", root_domain_name_w);
LOG("Runtime version: %s", runtime_version_w);
if (strlen(runtime_version_w) > 2 &&
(runtime_version_w[1] == L'2' || runtime_version_w[1] == L'1')) {
mono_is_net35 = TRUE;
}
free(root_domain_name_w);
free(runtime_version_w);
char *root_dir_n = mono.assembly_getrootdir();
char_t *root_dir = widen(root_dir_n);
LOG("Current root: %s", root_dir);
LOG("Overriding mono DLL search path");
size_t mono_search_path_len = strlen(root_dir) + 1;
char_t *override_dir_full = NULL;
char_t *config_path_value = config.mono_dll_search_path_override;
bool_t has_override = config_path_value && strlen(config_path_value);
if (has_override) {
size_t path_start = 0;
override_dir_full = calloc(MAX_PATH, sizeof(char_t));
memset(override_dir_full, 0, MAX_PATH * sizeof(char_t));
bool_t found_path = FALSE;
for (size_t i = 0; i <= strlen(config_path_value); i++) {
char_t current_char = config_path_value[i];
if (current_char == *PATH_SEP || current_char == 0) {
if (i <= path_start) {
path_start++;
continue;
}
size_t path_len = i - path_start;
char_t *path = calloc(path_len + 1, sizeof(char_t));
strncpy(path, config_path_value + path_start, path_len);
path[path_len] = 0;
char_t *full_path = get_full_path(path);
if (strlen(override_dir_full) + strlen(full_path) + 2 >
MAX_PATH) {
LOG("Ignoring this root path because its absolute version "
"is too long: %s",
full_path);
free(path);
free(full_path);
path_start = i + 1;
continue;
}
if (found_path) {
strcat(override_dir_full, PATH_SEP);
}
strcat(override_dir_full, full_path);
LOG("Adding root path: %s", full_path);
free(path);
free(full_path);
found_path = TRUE;
path_start = i + 1;
}
}
mono_search_path_len += strlen(override_dir_full) + 1;
}
char_t *mono_search_path = calloc(mono_search_path_len + 1, sizeof(char_t));
if (override_dir_full && strlen(override_dir_full)) {
strcat(mono_search_path, override_dir_full);
strcat(mono_search_path, PATH_SEP);
}
strcat(mono_search_path, root_dir);
LOG("Mono search path: %s", mono_search_path);
char *mono_search_path_n = narrow(mono_search_path);
mono.set_assemblies_path(mono_search_path_n);
setenv(TEXT("DOORSTOP_DLL_SEARCH_DIRS"), mono_search_path, TRUE);
free(mono_search_path);
free(mono_search_path_n);
if (override_dir_full) {
free(override_dir_full);
}
hook_mono_jit_parse_options(0, NULL);
bool_t debugger_already_enabled = mono_debug_init_called;
if (mono.debug_enabled) {
debugger_already_enabled |= mono.debug_enabled();
}
void *domain = NULL;
if (config.mono_debug_enabled && !debugger_already_enabled) {
LOG("Detected mono debugger is not initialized; initialized it");
mono.debug_init(MONO_DEBUG_FORMAT_MONO);
}
domain = mono.jit_init_version(root_domain_name, runtime_version);
mono_doorstop_bootstrap(domain);
return domain;
}
void il2cpp_doorstop_bootstrap() {
if (!config.clr_corlib_dir || !config.clr_runtime_coreclr_path) {
LOG("No CoreCLR paths set, skipping loading");
return;
}
LOG("CoreCLR runtime path: %s", config.clr_runtime_coreclr_path);
LOG("CoreCLR corlib dir: %s", config.clr_corlib_dir);
if (!file_exists(config.clr_runtime_coreclr_path) ||
!folder_exists(config.clr_corlib_dir)) {
LOG("CoreCLR startup dirs are not set up skipping invoking Doorstop");
return;
}
void *coreclr_module = dlopen(config.clr_runtime_coreclr_path, RTLD_LAZY);
LOG("Loaded coreclr.dll: %p", coreclr_module);
if (!coreclr_module) {
LOG("Failed to load CoreCLR runtime!");
return;
}
load_coreclr_funcs(coreclr_module);
char_t *app_path = program_path();
char *app_path_n = narrow(app_path);
char_t *target_dir = get_folder_name(config.target_assembly);
char *target_dir_n = narrow(target_dir);
char_t *target_name = get_file_name(config.target_assembly, FALSE);
char *target_name_n = narrow(target_name);
char_t *app_paths_env =
calloc(strlen(config.clr_corlib_dir) + 1 + strlen(target_dir) + 1,
sizeof(char_t));
strcat(app_paths_env, config.clr_corlib_dir);
strcat(app_paths_env, PATH_SEP);
strcat(app_paths_env, target_dir);
const char *app_paths_env_n = narrow(app_paths_env);
LOG("App path: %s", app_path);
LOG("Target dir: %s", target_dir);
LOG("Target name: %s", target_name);
LOG("APP_PATHS: %s", app_paths_env);
const char *props = "APP_PATHS";
setenv(TEXT("DOORSTOP_INITIALIZED"), TEXT("TRUE"), TRUE);
setenv(TEXT("DOORSTOP_INVOKE_DLL_PATH"), config.target_assembly, TRUE);
setenv(TEXT("DOORSTOP_MANAGED_FOLDER_DIR"), config.clr_corlib_dir, TRUE);
setenv(TEXT("DOORSTOP_PROCESS_PATH"), app_path, TRUE);
setenv(TEXT("DOORSTOP_DLL_SEARCH_DIRS"), app_paths_env, TRUE);
void *host = NULL;
unsigned int domain_id = 0;
int result = coreclr.initialize(app_path_n, "Doorstop Domain", 1, &props,
&app_paths_env_n, &host, &domain_id);
if (result != 0) {
LOG("Failed to initialize CoreCLR: 0x%08x", result);
return;
}
void (*startup)() = NULL;
result = coreclr.create_delegate(host, domain_id, target_name_n,
"Doorstop.Entrypoint", "Start",
(void **)&startup);
if (result != 0) {
LOG("Failed to get entrypoint delegate: 0x%08x", result);
return;
}
LOG("Invoking Doorstop.Entrypoint.Start()");
startup();
}
int init_il2cpp(const char *domain_name) {
char_t *domain_name_w = widen(domain_name);
LOG("Starting IL2CPP domain \"%s\"", domain_name_w);
free(domain_name_w);
const int orig_result = il2cpp.init(domain_name);
il2cpp_doorstop_bootstrap();
return orig_result;
}
#define MONO_DEBUG_ARG_START \
TEXT("--debugger-agent=transport=dt_socket,server=y,address=")
#define MONO_DEBUG_NO_SUSPEND TEXT(",suspend=n")
#define MONO_DEBUG_NO_SUSPEND_NET35 TEXT(",suspend=n,defer=y")
void hook_mono_jit_parse_options(int argc, char **argv) {
char_t *debug_options = getenv(TEXT("DNSPY_UNITY_DBG2"));
if (debug_options) {
config.mono_debug_enabled = TRUE;
}
if (config.mono_debug_enabled) {
LOG("Configuring mono debug server");
int size = argc + 1;
char **new_argv = calloc(size, sizeof(char *));
memcpy(new_argv, argv, argc * sizeof(char *));
size_t debug_args_len =
STR_LEN(MONO_DEBUG_ARG_START) + strlen(config.mono_debug_address);
if (!config.mono_debug_suspend) {
if (mono_is_net35) {
debug_args_len += STR_LEN(MONO_DEBUG_NO_SUSPEND_NET35);
} else {
debug_args_len += STR_LEN(MONO_DEBUG_NO_SUSPEND);
}
}
if (!debug_options) {
debug_options = calloc(debug_args_len + 1, sizeof(char_t));
strcat(debug_options, MONO_DEBUG_ARG_START);
strcat(debug_options, config.mono_debug_address);
if (!config.mono_debug_suspend) {
if (mono_is_net35) {
strcat(debug_options, MONO_DEBUG_NO_SUSPEND_NET35);
} else {
strcat(debug_options, MONO_DEBUG_NO_SUSPEND);
}
}
}
LOG("Debug options: %s", debug_options);
char *debug_options_n = narrow(debug_options);
new_argv[argc] = debug_options_n;
mono.jit_parse_options(size, new_argv);
free(debug_options);
free(debug_options_n);
free(new_argv);
} else {
mono.jit_parse_options(argc, argv);
}
}
void *hook_mono_image_open_from_data_with_name(void *data,
unsigned long data_len,
int need_copy,
MonoImageOpenStatus *status,
int refonly, const char *name) {
void *result = NULL;
if (config.mono_dll_search_path_override) {
char_t *name_wide = widen(name);
char_t *name_file = get_file_name(name_wide, TRUE);
free(name_wide);
size_t name_file_len = strlen(name_file);
size_t bcl_root_len = strlen(config.mono_dll_search_path_override);
char_t *new_full_path =
calloc(name_file_len + bcl_root_len + 2, sizeof(char_t));
strcat(new_full_path, config.mono_dll_search_path_override);
strcat(new_full_path, TEXT("/"));
strcat(new_full_path, name_file);
if (file_exists(new_full_path)) {
void *file = fopen(new_full_path, "r");
size_t size = get_file_size(file);
void *buf = malloc(size);
fread(buf, 1, size, file);
fclose(file);
result = mono.image_open_from_data_with_name(buf, size, need_copy,
status, refonly, name);
if (need_copy)
free(buf);
}
free(new_full_path);
}
if (!result) {
result = mono.image_open_from_data_with_name(data, data_len, need_copy,
status, refonly, name);
}
return result;
}
void hook_mono_debug_init(MonoDebugFormat format) {
mono_debug_init_called = TRUE;
mono.debug_init(format);
}