forked from RbxStu/RbxStu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
207 lines (173 loc) · 8.7 KB
/
main.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
// C
#include <MinHook.h>
#include <StudioOffsets.h>
#include <Windows.h>
// C++
#include <DbgHelp.h>
#include <Log.hpp>
#include <iostream>
#include <thread>
#include "Environment/Environment.hpp"
#include "Execution.hpp"
#include "Hook.hpp"
#include "Scheduler.hpp"
#include "Utilities.hpp"
#include "oxorany.hpp"
long exception_filter(PEXCEPTION_POINTERS pExceptionPointers) {
const auto function_name = "ExceptionFilter::UnhandledExceptionFilter";
const auto *pContext = pExceptionPointers->ContextRecord;
LOG_TO_FILE_AND_CONSOLE(function_name, "WARNING: Exception caught! Exception Code: %lx",
pExceptionPointers->ExceptionRecord->ExceptionCode);
LOG_TO_FILE_AND_CONSOLE(function_name, "Exception Information:");
LOG_TO_FILE_AND_CONSOLE(function_name, "Exception Caught @ %llx", pExceptionPointers->ContextRecord->Rip);
LOG_TO_FILE_AND_CONSOLE(function_name, "Module.dll @ %llx",
reinterpret_cast<std::uintptr_t>(GetModuleHandleA("Module.dll")));
LOG_TO_FILE_AND_CONSOLE(function_name, "Rebased Module @ 0x%llx",
pExceptionPointers->ContextRecord->Rip -
reinterpret_cast<std::uintptr_t>(GetModuleHandleA("Module.dll")));
LOG_TO_FILE_AND_CONSOLE(function_name, "RobloxStudioBeta.exe @ %llx",
reinterpret_cast<std::uintptr_t>(GetModuleHandleA("RobloxStudioBeta.exe")));
LOG_TO_FILE_AND_CONSOLE(function_name, "Rebased Studio @ 0x%llx",
pContext->Rip - reinterpret_cast<std::uintptr_t>(GetModuleHandleA("RobloxStudioBeta.exe")));
LOG_TO_FILE_AND_CONSOLE(function_name, "-- START REGISTERS STATE --\r\n");
LOG_TO_FILE_AND_CONSOLE(function_name, "-- START GP REGISTERS --");
LOG_TO_FILE_AND_CONSOLE(function_name, "RAX: 0x%llx", pContext->Rax);
LOG_TO_FILE_AND_CONSOLE(function_name, "RBX: 0x%llx", pContext->Rbx);
LOG_TO_FILE_AND_CONSOLE(function_name, "RCX: 0x%llx", pContext->Rcx);
LOG_TO_FILE_AND_CONSOLE(function_name, "RDX: 0x%llx", pContext->Rdx);
LOG_TO_FILE_AND_CONSOLE(function_name, "RDI: 0x%llx", pContext->Rdi);
LOG_TO_FILE_AND_CONSOLE(function_name, "RSI: 0x%llx", pContext->Rsi);
LOG_TO_FILE_AND_CONSOLE(function_name, "-- R8 - R15 --");
LOG_TO_FILE_AND_CONSOLE(function_name, "R08: 0x%llx", pContext->R8);
LOG_TO_FILE_AND_CONSOLE(function_name, "R09: 0x%llx", pContext->R9);
LOG_TO_FILE_AND_CONSOLE(function_name, "R10: 0x%llx", pContext->R10);
LOG_TO_FILE_AND_CONSOLE(function_name, "R11: 0x%llx", pContext->R11);
LOG_TO_FILE_AND_CONSOLE(function_name, "R12: 0x%llx", pContext->R12);
LOG_TO_FILE_AND_CONSOLE(function_name, "R13: 0x%llx", pContext->R13);
LOG_TO_FILE_AND_CONSOLE(function_name, "R14: 0x%llx", pContext->R14);
LOG_TO_FILE_AND_CONSOLE(function_name, "R15: 0x%llx", pContext->R15);
LOG_TO_FILE_AND_CONSOLE(function_name, "-- END GP REGISTERS --\r\n");
LOG_TO_FILE_AND_CONSOLE(function_name, "-- START STACK POINTERS --");
LOG_TO_FILE_AND_CONSOLE(function_name, "RBP: 0x%llx", pContext->Rbp);
LOG_TO_FILE_AND_CONSOLE(function_name, "RSP: 0x%llx", pContext->Rsp);
LOG_TO_FILE_AND_CONSOLE(function_name, "-- END STACK POINTERS --\r\n");
LOG_TO_FILE_AND_CONSOLE(function_name, "-- END REGISTERS STATE --\r\n");
LOG_TO_FILE_AND_CONSOLE(function_name, "-- Stack Trace:");
SymInitialize(GetCurrentProcess(), nullptr, TRUE);
void *stack[256];
const unsigned short frameCount = RtlCaptureStackBackTrace(0, 100, stack, nullptr);
for (unsigned short i = 0; i < frameCount; ++i) {
auto address = reinterpret_cast<DWORD64>(stack[i]);
char symbolBuffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
auto *symbol = reinterpret_cast<SYMBOL_INFO *>(symbolBuffer);
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
DWORD value{};
DWORD *pValue = &value;
if (SymFromAddr(GetCurrentProcess(), address, nullptr, symbol) && ((*pValue = symbol->Address - address)) &&
SymFromAddr(GetCurrentProcess(), address, reinterpret_cast<PDWORD64>(pValue), symbol)) {
LOG_TO_FILE_AND_CONSOLE(
function_name, "[Stack Frame %d] Inside %s @ 0x%llx; Studio Rebase: 0x%llx", i, symbol->Name,
address,
address - reinterpret_cast<std::uintptr_t>(GetModuleHandleA("RobloxStudioBeta.exe")) + 0x140000000);
} else {
LOG_TO_FILE_AND_CONSOLE(
function_name, "[Stack Frame %d] Unknown Subroutine @ 0x%llx; Studio Rebase: 0x%llx", i,
symbol->Name, address,
address - reinterpret_cast<std::uintptr_t>(GetModuleHandleA("RobloxStudioBeta.exe")) + 0x140000000);
}
}
std::cout << std::endl;
std::stringstream sstream{};
for (unsigned short i = 0; i < frameCount; ++i) {
sstream << ("0x") << std::hex << reinterpret_cast<std::uintptr_t>(stack[i]);
if (i < frameCount) {
sstream << (" -> ");
} else {
sstream << ("\r\n");
}
}
LOG_TO_FILE_AND_CONSOLE(function_name, "%s", sstream.str().c_str());
// Clean up
SymCleanup(GetCurrentProcess());
LOG_TO_FILE_AND_CONSOLE(
function_name,
"Stack frames captured. Log flushed to disk as file '%s'! Send to developer for more information! Roblox "
"Studio will now close, this is NOT a Studio Bug! It is probably caused by RbxStu!",
Log::get_singleton()->get_log_path().c_str());
Log::get_singleton()->flush_to_disk();
Sleep(30000);
exit(-1);
return EXCEPTION_EXECUTE_HANDLER;
}
int main(int argc, char **argv, char **envp) {
// TODO: Fix Garbage Collection causing crashes.
// FIXME: Avoid hooking to fix it, but seems not possible due to the fact it may be caused by an Engine-level bug.
SetUnhandledExceptionFilter(exception_filter);
AllocConsole();
freopen_s(reinterpret_cast<FILE **>(stdin), ("CONOUT$"), ("w"), stdout);
freopen_s(reinterpret_cast<FILE **>(stdin), ("CONIN$"), ("r"), stdin);
LOG_TO_FILE_AND_CONSOLE("main", "Initializing log writer on exit");
atexit([] {
LOG_TO_FILE_AND_CONSOLE("main::reinit", "Flushing log to disk...");
Log::get_singleton()->flush_to_disk();
});
LOG_TO_FILE_AND_CONSOLE("main", "Initializing hook...");
const auto pHook{Hook::get_singleton()};
pHook->initialize();
pHook->install_additional_hooks();
Sleep(500);
pHook->install_hook();
pHook->wait_until_initialised();
LOG_TO_FILE_AND_CONSOLE("main", "Hook initialization completed. lua_State* obtained.");
LOG_TO_FILE_AND_CONSOLE("main", "Initializing executor environment...");
auto scheduler{Scheduler::get_singleton()};
auto environment = Environment::get_singleton();
auto schedulerKey = 0;
environment->register_env(scheduler->get_global_executor_state(), true, &schedulerKey);
pHook->remove_hook();
std::string buffer{};
while (oxorany(true)) {
buffer.clear();
printf("\r\n[main] Input lua code: ");
getline(std::cin, buffer);
if (strcmp(buffer.c_str(), "reinit()") == 0) {
LOG_TO_FILE_AND_CONSOLE("main::reinit", "Re-Initializing execution environment.");
scheduler->re_initialize();
pHook->install_hook();
pHook->wait_until_initialised();
environment->register_env(scheduler->get_global_executor_state(), true, &schedulerKey);
pHook->remove_hook();
LOG_TO_FILE_AND_CONSOLE("main::reinit", "Re-Initialization successful!");
continue;
}
printf("\r\nPushing to StudioExecutor::Scheduler...\r\n");
scheduler->schedule_job(buffer);
Sleep(2000);
}
return 0;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
const DWORD fdwReason, // reason for calling function
const LPVOID lpvReserved) // reserved
{
// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
CreateThread(nullptr, 0x1000, reinterpret_cast<LPTHREAD_START_ROUTINE>(main), 0, 0, nullptr);
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
if (lpvReserved != nullptr) {
break; // do not do cleanup if process termination scenario
}
// Perform any necessary cleanup.
break;
}
return oxorany(TRUE); // Successful DLL_PROCESS_ATTACH.
}