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
|
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#ifndef ANDROID
#include <execinfo.h>
#endif
#include "box64context.h"
#include "custommem.h"
#include "debug.h"
#include "elfloader.h"
#include "emit_signals.h"
#include "emu/x64emu_private.h"
#include "regs.h"
#include "signals.h"
#include "x64emu.h"
void my_sigactionhandler_oldcode(x64emu_t* emu, int32_t sig, int simple, siginfo_t* info, void * ucntx, int* old_code, void* cur_db, uintptr_t x64pc);
void EmitSignal(x64emu_t* emu, int sig, void* addr, int code)
{
siginfo_t info = { 0 };
info.si_signo = sig;
info.si_errno = (sig == SIGSEGV) ? 0x1234 : 0; // Mark as a sign this is a #GP(0) (like privileged instruction)
info.si_code = code;
if (sig == SIGSEGV && code == 0xbad0) {
info.si_errno = 0xbad0;
info.si_code = 0;
} else if (sig == SIGSEGV && code == 0xecec) {
info.si_errno = 0xecec;
info.si_code = SEGV_ACCERR;
} else if (sig == SIGSEGV && code == 0xb09d) {
info.si_errno = 0xb09d;
info.si_code = 0;
}
info.si_addr = addr;
const char* x64name = NULL;
const char* elfname = NULL;
if (BOX64ENV(log) > LOG_INFO || BOX64ENV(dynarec_dump) || BOX64ENV(showsegv)) {
x64name = getAddrFunctionName(R_RIP);
elfheader_t* elf = FindElfAddress(my_context, R_RIP);
if (elf)
elfname = ElfName(elf);
printf_log(LOG_NONE, "Emit Signal %d at IP=%p(%s / %s) / addr=%p, code=0x%x\n", sig, (void*)R_RIP, x64name ? x64name : "???", elfname ? elfname : "?", addr, code);
print_rolling_log(LOG_INFO);
if ((BOX64ENV(showbt) || sig == SIGABRT) && BOX64ENV(log) >= LOG_INFO) {
// show native bt
#define BT_BUF_SIZE 100
int nptrs;
void* buffer[BT_BUF_SIZE];
char** strings;
#ifndef ANDROID
nptrs = backtrace(buffer, BT_BUF_SIZE);
strings = backtrace_symbols(buffer, nptrs);
if (strings) {
for (int j = 0; j < nptrs; j++)
printf_log(LOG_INFO, "NativeBT: %s\n", strings[j]);
free(strings);
} else
printf_log(LOG_INFO, "NativeBT: none (%d/%s)\n", errno, strerror(errno));
#endif
extern int my_backtrace_ip(x64emu_t * emu, void** buffer, int size); // in wrappedlibc
extern char** my_backtrace_symbols(x64emu_t * emu, uintptr_t * buffer, int size);
// save and set real RIP/RSP
nptrs = my_backtrace_ip(emu, buffer, BT_BUF_SIZE);
strings = my_backtrace_symbols(emu, (uintptr_t*)buffer, nptrs);
if (strings) {
for (int j = 0; j < nptrs; j++)
printf_log(LOG_INFO, "EmulatedBT: %s\n", strings[j]);
free(strings);
} else
printf_log(LOG_INFO, "EmulatedBT: none\n");
}
printf_log(LOG_NONE, DumpCPURegs(emu, R_RIP, emu->segs[_CS] == 0x23));
printf_log(LOG_NONE, "Emu Stack: %p 0x%lx%s\n", emu->init_stack, emu->size_stack, emu->stack2free ? " owned" : "");
// if(!elf) {
// FILE* f = fopen("/proc/self/maps", "r");
// if(f) {
// char line[1024];
// while(!feof(f)) {
// char* ret = fgets(line, sizeof(line), f);
// printf_log(LOG_NONE, "\t%s", ret);
// }
// fclose(f);
// }
// }
if (sig == SIGILL) {
uint8_t* mem = (uint8_t*)R_RIP;
printf_log(LOG_NONE, "SIGILL: Opcode at ip is %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n", mem[0], mem[1], mem[2], mem[3], mem[4], mem[5]);
}
}
my_sigactionhandler_oldcode(emu, sig, 0, &info, NULL, NULL, NULL, R_RIP);
}
void CheckExec(x64emu_t* emu, uintptr_t addr)
{
if (box64_pagesize != 4096)
return; // disabling the test, 4K pagesize simlation isn't good enough for this
while ((getProtection_fast(addr) & (PROT_EXEC | PROT_READ)) != (PROT_EXEC | PROT_READ)) {
R_RIP = addr; // incase there is a slight difference
EmitSignal(emu, SIGSEGV, (void*)addr, 0xecec);
}
}
void EmitInterruption(x64emu_t* emu, int num, void* addr)
{
siginfo_t info = { 0 };
info.si_signo = SIGSEGV;
info.si_errno = 0xdead;
info.si_code = num;
info.si_addr = NULL; // addr;
const char* x64name = NULL;
const char* elfname = NULL;
if (BOX64ENV(log) > LOG_INFO || BOX64ENV(dynarec_dump) || BOX64ENV(showsegv)) {
x64name = getAddrFunctionName(R_RIP);
elfheader_t* elf = FindElfAddress(my_context, R_RIP);
if (elf)
elfname = ElfName(elf);
printf_log(LOG_NONE, "Emit Interruption 0x%x at IP=%p(%s / %s) / addr=%p\n", num, (void*)R_RIP, x64name ? x64name : "???", elfname ? elfname : "?", addr);
}
my_sigactionhandler_oldcode(emu, SIGSEGV, 0, &info, NULL, NULL, NULL, R_RIP);
}
void EmitDiv0(x64emu_t* emu, void* addr, int code)
{
siginfo_t info = { 0 };
info.si_signo = SIGSEGV;
info.si_errno = 0xcafe;
info.si_code = code;
info.si_addr = addr;
const char* x64name = NULL;
const char* elfname = NULL;
if (BOX64ENV(log) > LOG_INFO || BOX64ENV(dynarec_dump) || BOX64ENV(showsegv)) {
x64name = getAddrFunctionName(R_RIP);
elfheader_t* elf = FindElfAddress(my_context, R_RIP);
if (elf)
elfname = ElfName(elf);
printf_log(LOG_NONE, "Emit Divide by 0 at IP=%p(%s / %s) / addr=%p\n", (void*)R_RIP, x64name ? x64name : "???", elfname ? elfname : "?", addr);
}
my_sigactionhandler_oldcode(emu, SIGSEGV, 0, &info, NULL, NULL, NULL, R_RIP);
}
|