Skip to content

Commit

Permalink
fix encoding (,%r12,8) operand
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfl committed Jul 26, 2016
1 parent 6e676b6 commit 25281e7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [email protected]:u/mfl/asmjit
2 changes: 1 addition & 1 deletion deps/asmjit
Submodule asmjit updated from 6467c7 to c4dc89
5 changes: 3 additions & 2 deletions src/align_udis_asmjit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/asm_interface.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions src/asm_macros.S
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@
// 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

//////////////////////////////////////////////////
.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
Expand Down
15 changes: 14 additions & 1 deletion src/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "tracer.h"

#include <dlfcn.h>
#include <stdlib.h>

using namespace redmagic;
using namespace std;
Expand All @@ -27,6 +28,10 @@ namespace redmagic {
thread_local vector<tracer_stack_state> threadl_tracer_stack;

thread_local uint32_t this_thread_id = 0;

#ifdef CONF_GLOBAL_ABORT
extern long global_icount_abort;
#endif
}

class UnprotectMalloc {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 10 additions & 3 deletions src/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, );
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 25281e7

Please sign in to comment.