-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_runtime.cpp
298 lines (282 loc) · 9.78 KB
/
bin_runtime.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
#include "runtime_env.h"
#include "utils.h"
#include "constants.h"
#include "libraryloader.h"
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <climits>
#include <stdexcept>
#include <csignal>
#include <csetjmp>
#include <string>
#include <unordered_map>
#include <utility>
#ifdef _WIN32
#include <dbghelp.h>
#include <Psapi.h>
#else
#include <execinfo.h>
#endif
using namespace std;
using uchar=unsigned char;
using ushort=unsigned short;
const size_t MAX_STACKTRACE_SIZE=256;
const size_t MAX_STACKTRACE_NAMELEN_WIN=256;
#ifdef _WIN32
const uchar pathsep='\\';
const int current_platform=WIN32_;
#else
const uchar pathsep='/';
const int current_platform=POSIX;
#endif
unordered_map<string, LibraryLoader*> loaded_libs;
LibraryLoader *loadLibrary(const char *libname) {
auto it = loaded_libs.find(libname);
if (it != loaded_libs.end()) {
return it->second; // 已找到,直接返回
} else {
// 未找到,尝试创建新的 LibraryLoader
try {
LibraryLoader *newLib = new LibraryLoader(libname);
loaded_libs[libname] = newLib;
return newLib;
} catch (runtime_error) {
return nullptr; // nullptr 表示加载失败
}
}
}
void *getLibraryFunc(const char *libname, const char *funcname) {
LibraryLoader *lib = loadLibrary(libname);
if(lib == nullptr) return nullptr;
return lib->getSymbol(funcname);
}
void freeLibrary(const char *libname) {
auto it = loaded_libs.find(libname);
if (it != loaded_libs.end()) {
LibraryLoader *lib = it->second;
delete lib;
loaded_libs.erase(it);
} //else {
//throw runtime_error("Attempt to free an unloaded library");
//}
}
void *loadExecutable(const char *filename,size_t *memsize=nullptr){
FILE *file = fopen(filename, "rb");
if (file == nullptr)
throw filenotfound(strerror(errno));
fseek(file, 0, SEEK_END);
size_t size = ftell(file);rewind(file);
uchar *buffer = new uchar[size];
size_t bytesRead = fread(buffer, 1, size, file);
if (bytesRead != size) {
delete[] buffer;fclose(file);
throw runtime_error("Error reading file");
}
void *func=allocExecMemory(size);
memcpy(func,buffer,size);
if(memsize!=nullptr)*memsize=size;
delete[] buffer;
return func;
}
void *getFunc(const char *funcname){
string name(funcname);
if(imported_funcs.find(name)==imported_funcs.end())
return nullptr;
return imported_funcs[name].first;
}
int import(const char *modname,bool reload=false,void **return_ptr=nullptr){
string path(modname);
size_t name_start,sep_pos=path.find_last_of(pathsep);
size_t ext_pos=path.find_last_of('.');
if(ext_pos==string::npos){
ext_pos=path.size();
path+=FILEEXT;
}
if(sep_pos==string::npos)name_start=0;
else name_start=sep_pos+1;
string func_name=path.substr(sep_pos+1,ext_pos-(sep_pos+1));
if(imported_funcs.find(func_name)!=imported_funcs.end() && !reload){
return IMPORT_SUCCESS; // 模块已存在,并且不重新加载
}
try{
size_t size;
void *funcptr=loadExecutable(path.c_str(),&size);
if(return_ptr!=nullptr)*return_ptr=funcptr;
imported_funcs[func_name]=pair<void *,size_t>(funcptr,size);
}catch(filenotfound){
return MODULE_NOT_FOUND;
}catch(runtime_error){
return UNKNOWN_ERROR;
}
return IMPORT_SUCCESS;
}
int forceReload(const char *modname){
return import(modname,true);
}
int loadModule(const char *modname){
return import(modname,false);
}
void debugModuleInfo(){
size_t total_size=0;char *converted;
printf("Loaded modules:\n");
for(auto &[func_name,value]:imported_funcs){
size_t size=value.second;
converted=convert_size(size);
printf("%s (%s)\n",func_name.c_str(),converted);
delete converted;
total_size+=size;
}
converted=convert_size(total_size);
printf("Total module memory: %s\n\n",converted);
delete converted;
printf("Loaded libraries:\n");
if(loaded_libs.empty()){
printf("(No libraries loaded)\n\n");
} else {
for(auto &[libname,lib]:loaded_libs){
printf("%s (0x%llx)\n",libname.c_str(),lib->handle);
}
printf("\n");
}
}
pair<string,void *> findModuleByAddress(void *stack_address){
size_t address=(size_t)stack_address;
for(const auto &[name,info]:imported_funcs){
size_t addr=(size_t)info.first;
size_t size=info.second;
if(address>=(size_t)addr && address<(size_t)addr+size){
return pair<string,void *>(name,info.first);
}
}
return make_pair<string,void *>("",nullptr);
}
#ifdef _WIN32
void stackTrace() {
void *stack[MAX_STACKTRACE_SIZE];
ushort frames;
SYMBOL_INFO *symbol;
HANDLE process = GetCurrentProcess();
// 初始化符号处理
SymInitialize(process, NULL, TRUE);
// 获取调用栈
frames = CaptureStackBackTrace(0, MAX_STACKTRACE_SIZE, stack, NULL);
symbol = (SYMBOL_INFO *)malloc(sizeof(SYMBOL_INFO) + \
MAX_STACKTRACE_NAMELEN_WIN * sizeof(char));
symbol->MaxNameLen = MAX_STACKTRACE_NAMELEN_WIN-1;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
fprintf(stderr, "Stacktrace:\n");
for (ushort i = 0; i < frames; i++) {
void *address=stack[i]; // 栈的当前地址
// 从系统获取符号信息
DWORD64 baseAddr = SymGetModuleBase(process, (DWORD64)address);
DWORD64 funcOffset = (DWORD64)address - baseAddr; // 函数相对模块基地址的偏移量
char filename[MAX_STACKTRACE_NAMELEN_WIN];
IMAGEHLP_MODULE64 moduleInfo;
moduleInfo.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
const char *moduleName;
if (SymGetModuleInfo64(process, (DWORD64)address, &moduleInfo)){
char *token,*context;
moduleName=moduleInfo.ImageName;
token=strtok_s(moduleInfo.ImageName,"/\\",&context);
while(token!=nullptr){
moduleName=token;
token=strtok_s(nullptr,"/\\",&context);
}
} else moduleName=nullptr;
const char *funcName;size_t funcAddress=0; // 函数的绝对地址
if(SymFromAddr(process, (DWORD64)address, 0, symbol)){
funcName=symbol->Name;
funcAddress=symbol->Address;
} else funcName=nullptr;
//从加载的bin文件自身获取符号
if(!moduleName && !funcName){
auto info=findModuleByAddress(address);
if(!info.first.empty()){
funcName=info.first.c_str();
funcAddress=(size_t)info.second;
}
}
const char *base_msg=(moduleName)?"ModuleBase + ":"";
if(funcName){
fprintf(stderr, "%s ! %s (%s0x%llx + 0x%llx)\n",
defaultVal((const char*)moduleName,"<Unknown module>"), funcName,
base_msg, funcAddress-baseAddr, address-funcAddress);
} else {
fprintf(stderr, "%s ! <Unknown function> (%s0x%llx)\n",
defaultVal((const char*)moduleName,"<Unknown module>"),
base_msg, (DWORD64)address - baseAddr);
}
}
free(symbol);
SymCleanup(process);
}
#else
void stackTrace() {
void *array[MAX_STACKTRACE_SIZE];
size_t size;
// 获取堆栈中的地址
size = backtrace(array, MAX_STACKTRACE_SIZE);
// 打印堆栈信息
fprintf(stderr, "Stacktrace:\n");
backtrace_symbols_fd(array, size, STDERR_FILENO);
}
#endif
FILE *getstdin(){return stdin;} // stdin为调用__acrt_iob_func的宏
FILE *getstdout(){return stdout;}
FILE *getstderr(){return stderr;}
void abort_(){raise(SIGABRT);} // 不使用标准库的abort
static RuntimeEnv *runtime_env=new RuntimeEnv();
using ExecutableMain=int (*)(int,const char**,RuntimeEnv*);
void initRuntimeEnv(RuntimeEnv *runtime_env){
runtime_env->version=RuntimeVersion{RUNTIME_VERSION_MAJOR,
RUNTIME_VERSION_MINOR,RUNTIME_VERSION_REVISION};
runtime_env->platform=current_platform;
runtime_env->import=loadModule;
runtime_env->getFunc=getFunc;
runtime_env->getLibraryFunc=getLibraryFunc;
runtime_env->freeLibrary=freeLibrary;
runtime_env->debugModuleInfo=debugModuleInfo;
runtime_env->getstdin=getstdin;
runtime_env->getstdout=getstdout;
runtime_env->getstderr=getstderr;
runtime_env->stackTrace=stackTrace;
runtime_env->abort=abort_;
}
int execExecutable(const char *filename,int argc,const char *argv[]){
// argv的第0项是程序目录,从第1项开始是命令行参数
size_t size;ExecutableMain mainfunc;
int import_result=import(filename,&size,(void**)(&mainfunc));
if(import_result!=0)
throw runtime_error(
"Import main module failed with code "+to_string(import_result));
signal(SIGABRT, signal_handler);
signal(SIGSEGV, signal_handler);int signum;
if((signum=setjmp(jmp_env))==0){
int result=mainfunc(argc,argv,runtime_env);
signal(SIGABRT, SIG_DFL);signal(SIGSEGV, SIG_DFL);
freeExecMemory((void *)mainfunc,size);
return result;
}else{
switch(signum){
case SIGABRT:
printf("%s called abort(), exiting\n",filename);break;
case SIGSEGV:
printf("Segmentation fault caused from %s\n",filename);break;
default:
printf("Caught signal %d from %s\n",signum,filename);
}
stackTrace();
signal(SIGABRT, SIG_DFL);signal(SIGSEGV, SIG_DFL);
freeExecMemory((void *)mainfunc,size);
return INT_MAX;
}
}
int main(int argc,const char *argv[]) {
initRuntimeEnv(runtime_env);
if(argc>1){
return execExecutable(argv[1],argc-1,argv+1);
}
printf("Usage: %s <%s file> args ...\n",argv[0],FILEEXT);
return 0;
}