More instruction, with fixes. printf is working now, and so is test04

This commit is contained in:
ptitSeb 2018-12-22 16:15:26 +01:00
parent 5dae29cf76
commit f6fcfac678
13 changed files with 72 additions and 23 deletions

View File

@ -66,7 +66,7 @@ add_test(test03 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/box86
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref03.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )
#add_test(test01 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/box86
# -D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test04 yeah -D TEST_OUTPUT=tmpfile.txt
# -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref04.txt
# -P ${CMAKE_SOURCE_DIR}/runTest.cmake )
add_test(test04 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/box86
-D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test04 -D TEST_ARGS2=yeah -D TEST_OUTPUT=tmpfile.txt
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref04.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )

View File

@ -15,7 +15,7 @@ endif( NOT TEST_REFERENCE )
set(ENV{BOX86_LOG} 0)
# run the test program, capture the stdout/stderr and the result var
execute_process(
COMMAND ${TEST_PROGRAM} ${TEST_ARGS}
COMMAND ${TEST_PROGRAM} ${TEST_ARGS} ${TEST_ARGS2}
OUTPUT_FILE ${TEST_OUTPUT}
ERROR_VARIABLE TEST_ERROR
RESULT_VARIABLE TEST_RESULT

View File

@ -46,13 +46,17 @@ uintptr_t CreateSymbol(lib_t *maplib, const char* name)
// look for symbols that can be created
uintptr_t addr = 0;
if(strcmp(name, "__stack_chk_fail")==0) {
addr = AddBridge(maplib->bridge, vFE, &my__stack_chk_fail);
addr = AddBridge(maplib->bridge, vFE, my__stack_chk_fail);
} else if(strcmp(name, "__libc_start_main")==0) {
addr = AddBridge(maplib->bridge, vFv, &my__libc_start_main);
addr = AddBridge(maplib->bridge, vFv, my__libc_start_main);
} else if(strcmp(name, "syscall")==0) {
addr = AddBridge(maplib->bridge, uFE, &LibSyscall);
addr = AddBridge(maplib->bridge, uFE, LibSyscall);
} else if(strcmp(name, "puts")==0) {
addr = AddBridge(maplib->bridge, iFp, &puts);
addr = AddBridge(maplib->bridge, iFp, puts);
} else if(strcmp(name, "printf")==0) {
addr = AddBridge(maplib->bridge, iFopv, vfprintf);
} else if(strcmp(name, "__printf_chk")==0) {
addr = AddBridge(maplib->bridge, iFvopv, vfprintf);
}
if(addr)
AddSymbol(maplib, name, addr);

View File

@ -238,6 +238,11 @@ int main(int argc, const char **argv, const char **env) {
context->ep = GetEntryPoint(context->maplib, elf_header);
// init x86 emu
context->emu = NewX86Emu(context, context->ep, (uintptr_t)context->stack, context->stacksz);
// stack setup is much more complicated then just that!
// setup the stack...
Push(context->emu, (uint32_t)context->argv);
Push(context->emu, context->argc);
SetupX86Emu(context->emu);
SetEAX(context->emu, context->argc);
SetEBX(context->emu, (uint32_t)context->argv);
// emulate!

View File

@ -27,6 +27,8 @@ typedef void (*vFi_t)(int32_t);
typedef int32_t (*iFi_t)(int32_t);
typedef int32_t (*iFp_t)(void*);
typedef int32_t (*iFpp_t)(void*, void*);
typedef int32_t (*iFipp_t)(int, void*, void*);
typedef int32_t (*iFppp_t)(void*, void*, void*);
#define DEF(A) A f = (A)fnc
@ -75,3 +77,18 @@ void iFpv(x86emu_t *emu, uintptr_t fnc)
DEF(iFpp_t);
*(int32_t*)&R_EAX = f(p(0), (void*)stack(4));
}
void iF1pv(x86emu_t *emu, uintptr_t fnc)
{
DEF(iFipp_t);
*(int32_t*)&R_EAX = f(1, p(0), (void*)stack(4));
}
void iFopv(x86emu_t *emu, uintptr_t fnc)
{
DEF(iFppp_t);
*(int32_t*)&R_EAX = f((void*)stdout, p(0), (void*)stack(4));
}
void iFvopv(x86emu_t *emu, uintptr_t fnc)
{
DEF(iFppp_t);
*(int32_t*)&R_EAX = f((void*)stdout, p(4), (void*)stack(8));
}

View File

@ -12,6 +12,8 @@ typedef void (*wrapper_t)(x86emu_t* emu, uintptr_t fnc);
// p = pointer
// f = float, d = double, D = long double
// v = vaargs, E = current x86emu struct
// 0 = constant 0, 1 = constant 1
// o = stdout
void vFv(x86emu_t *emu, uintptr_t fnc);
void vFE(x86emu_t *emu, uintptr_t fnc);
@ -22,6 +24,9 @@ void iFi(x86emu_t *emu, uintptr_t fnc);
void iFp(x86emu_t *emu, uintptr_t fnc);
void iFpp(x86emu_t *emu, uintptr_t fnc);
void iFpv(x86emu_t *emu, uintptr_t fnc);
void iF1pv(x86emu_t *emu, uintptr_t fnc);
void iFopv(x86emu_t *emu, uintptr_t fnc);
void iFvopv(x86emu_t *emu, uintptr_t fnc);
#endif //__WRAPPER_H_

View File

@ -33,11 +33,24 @@ x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, i
for (int i=0; i<8; ++i)
emu->sbiidx[i] = &emu->regs[i];
emu->sbiidx[4] = &emu->zero;
// set default value
R_EIP = start;
R_ESP = stack + stacksize;
// stack setup is much more complicated then just that!
// if trace is activated
if(context->x86trace) {
emu->dec = InitX86TraceDecoder(context);
if(!emu->dec)
printf_log(LOG_INFO, "Failed to initialize Zydis decoder and formater, no trace activated\n");
}
return emu;
}
void SetupX86Emu(x86emu_t *emu)
{
printf_log(LOG_DEBUG, "Setup X86 Emu\n");
// push "end emu" marker address
Push(emu, (uint32_t)&EndEmuMarker);
// Setup the GS segment:
@ -48,14 +61,6 @@ x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, i
canary[getrand(4)] = 0;
memcpy(emu->globals+0x14, canary, sizeof(canary)); // put canary in place
printf_log(LOG_DEBUG, "Setting up canary (for Stack protector) at GS:0x14, value:%08X\n", *(uint32_t*)canary);
// if trace is activated
if(context->x86trace) {
emu->dec = InitX86TraceDecoder(context);
if(!emu->dec)
printf_log(LOG_INFO, "Failed to initialize Zydis decoder and formater, no trace activated\n");
}
return emu;
}
void FreeX86Emu(x86emu_t **x86emu)

View File

@ -5,6 +5,7 @@ typedef struct x86emu_s x86emu_t;
typedef struct box86context_s box86context_t;
x86emu_t *NewX86Emu(box86context_t *context, uintptr_t start, uintptr_t stack, int stacksize);
void SetupX86Emu(x86emu_t *emu);
void FreeX86Emu(x86emu_t **x86emu);
uint32_t GetEAX(x86emu_t *emu);

View File

@ -49,13 +49,25 @@ int Run(x86emu_t *emu)
GetGb(emu, &op2, nextop);
op2->byte[0] = add8(emu, op1->byte[0], op2->byte[0]);
break;
case 0x01: /* ADD Ed,Gd */
nextop = Fetch8(emu);
GetEd(emu, &op1, &ea1, nextop);
GetG(emu, &op2, nextop);
op1->dword[0] = add32(emu, op1->dword[0], op2->dword[0]);
break;
case 0x02: /* ADD Gd,Ed */
nextop = Fetch8(emu);
GetEd(emu, &op2, &ea2, nextop);
GetG(emu, &op1, nextop);
op1->dword[0] = add32(emu, op1->dword[0], op2->dword[0]);
break;
case 0x04: /* ADD AL, Ib */
tmp8u = Fetch8(emu);
R_AL = add8(emu, R_AL, tmp8u);
break;
case 0x31: /* XOR Ed,Gd */
nextop = Fetch8(emu);
GetEd(emu, &op1, &ea2, nextop);
GetEd(emu, &op1, &ea1, nextop);
GetG(emu, &op2, nextop);
op1->dword[0] = xor32(emu, op1->dword[0], op2->dword[0]);
break;

View File

@ -135,7 +135,7 @@ void GetEd(x86emu_t *emu, reg32_t **op, reg32_t *ea, uint32_t v)
*op = (reg32_t*)Fetch32(emu);
return;
}
*op = &emu->regs[_AX+m];
*op = (reg32_t*)(emu->regs[_AX+m].dword[0]);
return;
} else if(m>=0x40 && m<=0x47) {
uintptr_t base;

View File

@ -1 +1 @@
Hello, argc=2, argv[0]=./test04, argv[1]=yeah
Hello, argc=2 argv[1]=yeah

Binary file not shown.

View File

@ -2,6 +2,6 @@
int main(int argc, char **argv)
{
printf("Hello, argc=%d, argv[0]=%s, argv[%d]=%s\n", argc, argv[0], argc-1, argv[argc-1]);
printf("Hello, argc=%d argv[%d]=%s\n", argc, argc-1, argv[argc-1]);
return 0;
}