From 25281e79bd3cd347f0ae06a43c8b735e9d149a00 Mon Sep 17 00:00:00 2001 From: Matthew Francis-Landau Date: Tue, 26 Jul 2016 13:38:36 -0700 Subject: [PATCH] fix encoding (,%r12,8) operand --- .gitmodules | 2 +- deps/asmjit | 2 +- src/align_udis_asmjit.cc | 5 +++-- src/asm_interface.S | 5 +++++ src/asm_macros.S | 8 ++++---- src/manager.cc | 15 ++++++++++++++- src/tracer.cc | 13 ++++++++++--- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/.gitmodules b/.gitmodules index 16f81e4..122b699 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,4 +3,4 @@ url = https://github.com/vmt/udis86.git [submodule "deps/asmjit"] path = deps/asmjit - url = https://github.com/kobalicek/asmjit.git + url = git@10.7.0.1:u/mfl/asmjit diff --git a/deps/asmjit b/deps/asmjit index 6467c73..c4dc897 160000 --- a/deps/asmjit +++ b/deps/asmjit @@ -1 +1 @@ -Subproject commit 6467c73ddfd6be1bd90104fab35676f26c319d54 +Subproject commit c4dc897a26063937f206d646da715d3b0c6e7d36 diff --git a/src/align_udis_asmjit.cc b/src/align_udis_asmjit.cc index 9c69816..4251c25 100644 --- a/src/align_udis_asmjit.cc +++ b/src/align_udis_asmjit.cc @@ -225,7 +225,8 @@ const asmjit::Operand AlignedInstructions::get_asm_op(unsigned int i) { assert(info->index_register.index != -1); int scale = 0; switch(info->index_scale) { - case 0: scale = 0; break; + //case 0: scale = 0; break; + case 1: scale = 0; break; case 2: scale = 1; break; case 4: scale = 2; break; case 8: scale = 3; break; @@ -234,7 +235,7 @@ const asmjit::Operand AlignedInstructions::get_asm_op(unsigned int i) { case 64: scale = 6; break; default: assert(0); } - return x86::ptr_abs(0, get_asm_register_from_rinfo(info->index_register), scale, info->offset); + return x86::ptr_abs((Ptr)info->offset, get_asm_register_from_rinfo(info->index_register), scale, 0); } } default: diff --git a/src/asm_interface.S b/src/asm_interface.S index c2a423c..facf7db 100644 --- a/src/asm_interface.S +++ b/src/asm_interface.S @@ -92,5 +92,10 @@ red_asm_start_nested_trace: jmp *%rax +testing: + jmp 0x123fa(,%r12,8) + mov 0xfafafa(,%r12,8), %rax + + // we don't need executable stack .section .note.GNU-stack,"",%progbits diff --git a/src/asm_macros.S b/src/asm_macros.S index 491c967..cbb8c2b 100644 --- a/src/asm_macros.S +++ b/src/asm_macros.S @@ -71,12 +71,12 @@ // 208 == 0xd0 // extra registers.... // make sure that the region we are going to save these too is aligned to 16 byte boundary - mov %rsp, %rax + movq %rsp, %rax // yes this is add instead of a subtract even though this is on the stack (we are "lower" on the stack then the regs struct) //add $16, %rax // just include this add in the offset of fxsaveq and $~15, %rax - fxsaveq 0xf0(%rax) + fxsaveq 0x100(%rax) .endm @@ -84,9 +84,9 @@ .macro m_pop_all_regs // extra registers - mov %rsp, %rax + movq %rsp, %rax and $~15, %rax - fxrstorq 0xf0(%rax) + fxrstorq 0x100(%rax) // eflags movq 144(%rsp), %r14 diff --git a/src/manager.cc b/src/manager.cc index 155992c..1cad619 100644 --- a/src/manager.cc +++ b/src/manager.cc @@ -2,6 +2,7 @@ #include "tracer.h" #include +#include using namespace redmagic; using namespace std; @@ -27,6 +28,10 @@ namespace redmagic { thread_local vector threadl_tracer_stack; thread_local uint32_t this_thread_id = 0; + +#ifdef CONF_GLOBAL_ABORT + extern long global_icount_abort; +#endif } class UnprotectMalloc { @@ -70,7 +75,8 @@ extern "C" void* red_user_fellthrough_branch(void *id, void *ret_addr) { extern "C" void* red_user_ensure_not_traced(void *_, void *ret_addr) { // TODO: - assert(!manager->get_tracer_head()->is_traced); + auto head = manager->get_tracer_head(); + assert(!head->is_traced || head->did_abort); // TODO: better manage abort return NULL; } @@ -107,6 +113,12 @@ extern "C" void redmagic_start() { redmagic::manager = new Manager(); // int r = mprotect(p, 4*1024, PROT_NONE); // assert(!r); + +#ifdef CONF_GLOBAL_ABORT + char *abort_v = getenv("REDMAGIC_GLOBAL_ABORT"); + if(abort_v) + redmagic::global_icount_abort = atol(abort_v); +#endif } extern "C" void redmagic_do_not_trace_function(void *function_pointer) { @@ -306,6 +318,7 @@ void* Manager::backwards_branch(void *id, void *ret_addr) { assert(!head->is_compiled); assert(head->tracer == info->tracer); + assert(!info->disabled); void *ret = head->tracer->EndTraceLoop(); head->is_compiled = true; Tracer *l = head->tracer; diff --git a/src/tracer.cc b/src/tracer.cc index c3f2436..6b7a06f 100644 --- a/src/tracer.cc +++ b/src/tracer.cc @@ -244,6 +244,7 @@ extern "C" void* red_branch_to_sub_trace(void *resume_addr, void *sub_trace_id, // maybe treat this as a temp disabled inner loop } assert(info->starting_point != nullptr); + assert(!info->disabled); auto new_head = manager->push_tracer_stack(); new_head->is_traced = true; new_head->trace_id = sub_trace_id; @@ -304,6 +305,8 @@ void* Tracer::Start(void *start_addr) { did_abort = true; manager->get_tracer_head()->did_abort = true; + CodeBuffer::Relase(buffer); + buffer = nullptr; return start_addr; } #endif @@ -405,7 +408,7 @@ void Tracer::Run(struct user_regs_struct *other_stack) { dladdr((void*)ud_insn_off(&disassm), &dlinfo); auto ins_loc = ud_insn_off(&disassm); - red_printf("[%10lu %8i %#016lx] \t%-35s %-20s %s\n", global_icount, icount, ins_loc, ud_insn_asm(&disassm), ud_insn_hex(&disassm), dlinfo.dli_sname); + red_printf("[%10lu %8i %#016lx] \t%-38s %-20s %s\n", global_icount, icount, ins_loc, ud_insn_asm(&disassm), ud_insn_hex(&disassm), dlinfo.dli_sname); #endif //fprintf(stderr, ); @@ -624,13 +627,16 @@ void Tracer::finish_patch() { extern "C" void* red_asm_resume_eval_block(void*, void*); +float float_a = 9.4; + void Tracer::continue_program(mem_loc_t resume_loc) { + red_printf("==> %#016lx\n", resume_loc); assert(regs_struct->rsp - TRACE_STACK_OFFSET == (register_t)regs_struct); regs_struct->rsp += move_stack_by; move_stack_by = 0; *((register_t*)(regs_struct->rsp - TRACE_RESUME_ADDRESS_OFFSET)) = resume_loc; regs_struct = (struct user_regs_struct*)red_asm_resume_eval_block(&resume_struct, regs_struct); - + float_a *= 1.000001; } @@ -1424,7 +1430,8 @@ void Tracer::replace_rip_instruction() { /*... UD_Imovzx:*/ case UD_Imov: case UD_Imovsxd: - case UD_Imovss: { + case UD_Imovss: + case UD_Imovsd: { const ud_operand_t *opr1 = ud_insn_opr(&disassm, 0); // dest address const ud_operand_t *opr2 = ud_insn_opr(&disassm, 1); // source address assert(opr1 != NULL && opr2 != NULL);