Moved emit functions to seperate files from signals.h (#2516)

This commit is contained in:
Yang Liu 2025-04-09 17:57:16 +08:00 committed by GitHub
parent 7391befb7e
commit d0a5352c1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 177 additions and 165 deletions

View File

@ -348,6 +348,7 @@ set_source_files_properties(
set(OS_LINUX_SRC
"${BOX64_ROOT}/src/os/backtrace.c"
"${BOX64_ROOT}/src/os/emit_signals_linux.c"
"${BOX64_ROOT}/src/os/os_linux.c"
)

View File

@ -21,6 +21,7 @@
#include "emu/x87emu_private.h"
#include "x64trace.h"
#include "signals.h"
#include "emit_signals.h"
#include "dynarec_native.h"
#include "custommem.h"
#include "bridge.h"

View File

@ -11,9 +11,9 @@
#define F64S *(int64_t*)(addr+=8, addr-8)
#define PK(a) *(uint8_t*)(addr+a)
#ifdef DYNAREC
#define STEP check_exec(emu, addr); if(step && !ACCESS_FLAG(F_TF)) return 0;
#define STEP2 check_exec(emu, addr); if(step && !ACCESS_FLAG(F_TF)) {R_RIP = addr; return 0;}
#define STEP3 check_exec(emu, addr); if(*step) (*step)++;
#define STEP CheckExec(emu, addr); if(step && !ACCESS_FLAG(F_TF)) return 0;
#define STEP2 CheckExec(emu, addr); if(step && !ACCESS_FLAG(F_TF)) {R_RIP = addr; return 0;}
#define STEP3 CheckExec(emu, addr); if(*step) (*step)++;
#else
#define STEP
#define STEP2

View File

@ -27,7 +27,7 @@
#include "wrapper.h"
#include "box64context.h"
#include "librarian.h"
#include "signals.h"
#include "emit_signals.h"
#include "tools/bridge_private.h"
#include <elf.h>

View File

@ -20,7 +20,7 @@
#include "x87emu_private.h"
#include "box64context.h"
#include "alternate.h"
#include "signals.h"
#include "emit_signals.h"
#ifdef DYNAREC
#include "../dynarec/native_lock.h"
#endif
@ -67,7 +67,7 @@ int Run(x64emu_t *emu, int step)
#ifdef TEST_INTERPRETER
test->memsize = 0;
#else
check_exec(emu, R_RIP);
CheckExec(emu, R_RIP);
x64emurun:
while(1)
#endif

View File

@ -22,7 +22,7 @@
#include "box64context.h"
#include "my_cpuid.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#include "x64shaext.h"
#ifdef DYNAREC
#include "custommem.h"

View File

@ -19,7 +19,7 @@
#include "x87emu_private.h"
#include "box64context.h"
#include "alternate.h"
#include "signals.h"
#include "emit_signals.h"
#include "modrm.h"

View File

@ -19,7 +19,7 @@
#include "x64trace.h"
#include "x87emu_private.h"
#include "box64context.h"
#include "signals.h"
#include "emit_signals.h"
#include "bridge.h"
#ifdef DYNAREC
#include "custommem.h"

View File

@ -19,7 +19,7 @@
#include "x87emu_private.h"
#include "box64context.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#ifdef DYNAREC
#include "../dynarec/native_lock.h"
#endif

View File

@ -21,7 +21,7 @@
#include "box64context.h"
#include "my_cpuid.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#include "x64shaext.h"
#ifdef DYNAREC
#include "custommem.h"

View File

@ -21,7 +21,7 @@
#include "box64context.h"
#include "my_cpuid.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#ifdef DYNAREC
#include "custommem.h"
#include "../dynarec/native_lock.h"

View File

@ -21,7 +21,7 @@
#include "box64context.h"
#include "my_cpuid.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#include "x64shaext.h"
#ifdef DYNAREC
#include "custommem.h"

View File

@ -21,7 +21,7 @@
#include "box64context.h"
#include "my_cpuid.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#include "x64shaext.h"
#ifdef DYNAREC
#include "custommem.h"

View File

@ -21,7 +21,7 @@
#include "box64context.h"
#include "my_cpuid.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#include "x64shaext.h"
#include "x64compstrings.h"
#ifdef DYNAREC

View File

@ -20,7 +20,7 @@
#include "x87emu_private.h"
#include "box64context.h"
#include "bridge.h"
#include "signals.h"
#include "emit_signals.h"
#include "modrm.h"

View File

@ -35,6 +35,7 @@
#include "box64context.h"
#include "callback.h"
#include "signals.h"
#include "emit_signals.h"
#include "x64tls.h"
#include "elfloader.h"

View File

@ -23,7 +23,7 @@
#include "wrapper32.h"
#include "box32context.h"
#include "librarian.h"
#include "signals.h"
#include "emit_signals.h"
#include "tools/bridge_private.h"
#include <elf.h>

View File

@ -0,0 +1,13 @@
#ifndef __EMIT_SIGNALS_H_
#define __EMIT_SIGNALS_H_
#include <stdint.h>
typedef struct x64emu_s x64emu_t;
void EmitSignal(x64emu_t* emu, int sig, void* addr, int code);
void EmitInterruption(x64emu_t* emu, int num, void* addr);
void EmitDiv0(x64emu_t* emu, void* addr, int code);
void CheckExec(x64emu_t* emu, uintptr_t addr);
#endif // __EMIT_SIGNALS_H_

View File

@ -36,9 +36,6 @@ int InternalMunmap(void* addr, unsigned long length);
int GetTID(void);
int SchedYield(void);
void EmitSignal(void* emu, int sig, void* addr, int code);
void EmitDiv0(void* emu, void* addr, int code);
void EmitInterruption(void* emu, int num, void* addr);
void EmuX64Syscall(void* emu);
void EmuX86Syscall(void* emu);

View File

@ -74,9 +74,4 @@ int my_syscall_rt_sigaction(x64emu_t* emu, int signum, const x64_sigaction_resto
void init_signal_helper(box64context_t* context);
void fini_signal_helper(void);
void emit_signal(x64emu_t* emu, int sig, void* addr, int code);
void emit_interruption(x64emu_t* emu, int num, void* addr);
void emit_div0(x64emu_t* emu, void* addr, int code);
void check_exec(x64emu_t* emu, uintptr_t addr);
#endif //__SIGNALS_H__

View File

@ -2078,131 +2078,6 @@ void my_sigactionhandler(int32_t sig, siginfo_t* info, void * ucntx)
my_sigactionhandler_oldcode(emu, sig, 0, info, ucntx, NULL, db, x64pc);
}
void emit_signal(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 check_exec(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
emit_signal(emu, SIGSEGV, (void*)addr, 0xecec);
}
}
void emit_interruption(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 emit_div0(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);
}
EXPORT sighandler_t my_signal(x64emu_t* emu, int signum, sighandler_t handler)
{
if(signum<0 || signum>MAX_SIGNAL)

144
src/os/emit_signals_linux.c Normal file
View File

@ -0,0 +1,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);
}

View File

@ -19,21 +19,6 @@ int SchedYield(void)
return sched_yield();
}
void EmitSignal(void* emu, int sig, void* addr, int code)
{
return emit_signal((x64emu_t*)emu, sig, addr, code);
}
void EmitDiv0(void* emu, void* addr, int code)
{
return emit_div0((x64emu_t*)emu, addr, code);
}
void EmitInterruption(void* emu, int num, void* addr)
{
return emit_interruption((x64emu_t*)emu, num, addr);
}
int IsBridgeSignature(char s, char c)
{
return s == 'S' && c == 'C';