Skip to content

Commit

Permalink
able to handle generating non general register rip instructions, but …
Browse files Browse the repository at this point in the history
…still have bug with saving float registers
  • Loading branch information
matthewfl committed Jul 24, 2016
1 parent ae0dcd1 commit 6e676b6
Show file tree
Hide file tree
Showing 14 changed files with 451 additions and 74 deletions.
12 changes: 8 additions & 4 deletions make
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ GIT_VERSION = Shell('git describe --always --long --dirty --abbrev=12', silent=T

C_FLAGS = (
'-fPIC '
'-I ./deps/ '
#'-mgeneral-regs-only '
'-ggdb '
'-O0 '
'-I ./deps/ '
'-I ./deps/udis86 '
'-I ./deps/asmjit/src '
)
Expand Down Expand Up @@ -164,15 +165,18 @@ def deps():
if not os.path.isdir('build'):
Shell('mkdir -p build')
if not os.path.isfile('deps/udis86/libudis86/.libs/libudis86.so') or not os.path.isfile('deps/udis86/libudis86/itab.h'):
Shell('cd deps/udis86 && ./autogen.sh && PYTHON=`which python2` ./configure && make', shell=True)
Shell('cd deps/udis86 && ./autogen.sh && PYTHON=`which python2` ./configure && '
#"sed -i '/^CFLAGS\ =/ s/$/\ \-mgeneral\-regs\-only/' Makefile &&"
'make V=1', shell=True)
if not os.path.isfile('build/asmjit/libasmjit.so'):
Shell('mkdir -p build/asmjit')
asm_flags = '' # -DASMJIT_ALLOC=test123
cm_args = '-DASMJIT_DISABLE_COMPILER=1 -DASMJIT_CFLAGS=\'==REPLACE_ME==\' -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc'
if RELEASE:
Shell('cd build/asmjit && cmake ../../deps/asmjit -DASMJIT_DISABLE_COMPILER=1 -DASMJIT_CFLAGS=\'==REPLACE_ME==\' -DASMJIT_RELEASE=1', shell=True)
Shell('cd build/asmjit && cmake ../../deps/asmjit {} -DASMJIT_RELEASE=1'.format(cm_args), shell=True)
asm_flags += '\-O2'
else:
Shell('cd build/asmjit && cmake ../../deps/asmjit -DASMJIT_DISABLE_COMPILER=1 -DASMJIT_CFLAGS=\'==REPLACE_ME==\' -DASMJIT_DEBUG=1', shell=True)
Shell('cd build/asmjit && cmake ../../deps/asmjit {} -DASMJIT_DEBUG=1'.format(cm_args), shell=True)
asm_flags += '\-ggdb'
Shell('sed -i s/==REPLACE_ME==/{}/ build/asmjit/CMakeFiles/asmjit.dir/flags.make'.format(asm_flags), shell=True)
Shell('cd build/asmjit && make VERBOSE=1', shell=True)
Expand Down
62 changes: 60 additions & 2 deletions src/align_udis_asmjit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ namespace {
} _do_alignment_inst;
}

// namespace redmagic {
// // template<typename T>
// // int gg(T i) {
// // return 0;
// // }

// asmjit::Operand get_asm_op_from_ud(ud_operand_t *opr) {
// using namespace asmjit;
// using namespace x86;
// // it is like I am programming node
// switch(opr->type) {
// case UD_OP_REG: {
// return get_asm_register_from_ud(opr->base, [&](auto base_reg) -> asmjit::Operand {
// return base_reg;
// });
// }
// case UD_OP_MEM: {
// uint64_t offset;
// switch(opr->offset) {
// case 8: offset = opr->lval.sbyte; break;
// case 16: offset = opr->lval.sword; break;
// case 32: offset = opr->lval.sdword; break;
// case 64: offset = opr->lval.sqword; break;
// default: assert(0);
// }
// if(opr->base != UD_NONE) {
// return get_asm_register_from_ud(opr->base, [&](auto base_reg) -> asmjit::Operand {
// if(opr->index != UD_NONE) {
// return get_asm_register_from_ud(opr->index, [&](auto index_reg) -> asmjit::Operand {

// });
// } else {
// return word_ptr(base_reg, offset);
// }
// });
// }
// }
// }
// }
// }

enum X86InstId AlignedInstructions::get_asm_mnem(enum ud_mnemonic_code mnem) {
return aligned_instructions[mnem].asm_code;
}
Expand All @@ -48,6 +89,15 @@ AlignedInstructions::AlignedInstructions(ud_t *disassm) {
pc = ud_insn_off(disassm);
len = ud_insn_len(disassm);

// assert(disassm->pfx_rex == UD_NONE);
assert(disassm->pfx_seg == UD_NONE);
assert(disassm->pfx_opr == UD_NONE);
assert(disassm->pfx_adr == UD_NONE);
assert(disassm->pfx_lock == UD_NONE);
assert(disassm->pfx_rep == UD_NONE);
assert(disassm->pfx_repe == UD_NONE);
assert(disassm->pfx_repne == UD_NONE);

for(int i = 0;; i++) {
const ud_operand_t *opr = ud_insn_opr(disassm, i);
if(opr == NULL)
Expand Down Expand Up @@ -142,8 +192,16 @@ const asmjit::Operand AlignedInstructions::get_asm_op(unsigned int i) {
return imm(info->imm_value);
case UD_OP_JIMM:
return imm_u(info->address);
case UD_OP_REG:
return get_asm_register_from_rinfo(info->register_i);
case UD_OP_REG: {
if(info->register_i.index == -1) {
// then this is a special ud register type which won't be referencing memory so we don't care
return get_asm_register_from_ud(info->register_i.ud_reg_type, [&](auto reg) -> asmjit::Operand {
return reg;
});
} else {
return get_asm_register_from_rinfo(info->register_i);
}
}
case UD_OP_MEM: {
if(info->base_register.index != -1) {
if(info->index_register.index == -1) {
Expand Down
27 changes: 25 additions & 2 deletions src/align_udis_asmjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace redmagic {
struct register_info {
int8_t index;
int8_t size;
ud_type ud_reg_type;
};

class AlignedInstructions {
Expand Down Expand Up @@ -266,11 +267,10 @@ namespace redmagic {
register_info r;
r.index = ud_register_to_sys(t);
r.size = ud_register_to_size(t);
r.ud_reg_type = t;
return r;
}



static auto& get_asm_register_from_rinfo(register_info r) {
// take the sys struct register id and convert it to asmjit
using namespace asmjit::x86;
Expand Down Expand Up @@ -375,6 +375,29 @@ namespace redmagic {
assert(0);
}

// this function takes a function that is provided the register type since asmjit uses multiple types
// and they can't be corressed together
template<typename func_t>
auto get_asm_register_from_ud(ud_type t, func_t func) {
using namespace asmjit;
using namespace x86;
switch(t) {
case UD_R_AL ... UD_R_GS:
return func(get_asm_register_from_rinfo(ud_register_to_rinfo(t)));
case UD_R_XMM0 ... UD_R_XMM15:
return func(x86RegData.xmm[t - UD_R_XMM0]);
case UD_R_MM0 ... UD_R_MM7:
return func(x86RegData.mm[t - UD_R_MM0]);
case UD_R_ST0 ... UD_R_ST7:
return func(x86RegData.fp[t - UD_R_ST0]);
case UD_R_YMM0 ... UD_R_YMM15:
return func(x86RegData.ymm[t - UD_R_YMM0]);
case UD_NONE:
default: assert(0);
}
}

// asmjit::Operand get_asm_op_from_ud(ud_operand_t *opr);

}

Expand Down
15 changes: 15 additions & 0 deletions src/asm_macros.S
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,26 @@
mov %fs, 200(%rsp)
mov %gs, 208(%rsp)

// 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
// 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)

.endm

//////////////////////////////////////////////////
.macro m_pop_all_regs

// extra registers
mov %rsp, %rax
and $~15, %rax
fxrstorq 0xf0(%rax)

// eflags
movq 144(%rsp), %r14
popq %r15
Expand Down
2 changes: 1 addition & 1 deletion src/code_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CodeBuffer* CodeBuffer::CreateBuffer(size_t size) {
if(buffer == MAP_FAILED) {
perror("failed to mmap buffer");
}
memset(buffer, 0, size);
memset(buffer, 0xCC, size); // write int3 so that if we end up here we should alert the debugger

CodeBuffer* ret = new CodeBuffer();
owning_code_buffers.push_back(ret);
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

#define CONF_VERBOSE

#define CONF_GLOBAL_ABORT

#endif // REDMAGIC_CONFIG_H_
2 changes: 1 addition & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#define TRACE_STACK_OFFSET 0x08ff /* hardcode offset from this base stack */
#define TRACE_STACK_OFFSET 0x09ff /* hardcode offset from this base stack */

#define TRACE_RESUME_ADDRESS_OFFSET 0x0800 /* hardcode offset to find jump to loc */

Expand Down
11 changes: 6 additions & 5 deletions src/jit_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ namespace redmagic {
Manager();

void* begin_trace(void *id, void *ret_addr);
void* end_trace(void *id);
void* end_trace(void *id, void *ret_addr);
void* jump_to_trace(void *id);

void* backwards_branch(void *id, void *ret_addr);
void* fellthrough_branch(void*);
void* fellthrough_branch(void *id, void *ret_addr);

void ensure_not_traced();

Expand Down Expand Up @@ -110,6 +110,7 @@ namespace redmagic {
bool is_temp_disabled = false;
bool is_traced = false;
bool is_compiled = false;
bool did_abort = false;
};

//extern thread_local std::vector<tracer_stack_state> trace_return_addr;
Expand Down Expand Up @@ -225,10 +226,10 @@ namespace redmagic {
}

public:
std::mutex generation_mutex;
// std::unique_lock<std::mutex> generation_lock = std::unique_lock<std::mutex>(_generation_mutex);
// std::mutex generation_mutex;
// // std::unique_lock<std::mutex> generation_lock = std::unique_lock<std::mutex>(_generation_mutex);

CodeBuffer *_next;
// CodeBuffer *_next;

private:
uint8_t *buffer;
Expand Down
5 changes: 4 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ int main(int argc, char* argv[]) {

int cond_var = 0;

float a = 1.2;

while(1) {
switch(program[pc]) {
case PRINT_NUM:
redmagic_temp_disable();
cout << program[pc + 1] << endl;
redmagic_temp_enable();
//printf("%i\n", program[pc + 1]);
a *= 1.001;
pc += 2;
break;
case JUMP_BACK:
Expand All @@ -80,7 +83,7 @@ int main(int argc, char* argv[]) {
pc += 2;
break;
case EXIT:
cout << "hitting normal exit case\n";
cout << "hitting normal exit case\n" << a;
exit(0);
default:
exit(-1);
Expand Down
Loading

0 comments on commit 6e676b6

Please sign in to comment.