Skip to content

Commit

Permalink
largely working
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfl committed Jul 22, 2016
1 parent 3e78813 commit a56edc5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/asm_snippets.S
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ red_asm_pop_all_regs_start:
m_pop_all_regs
red_asm_pop_all_regs_end:


.global red_asm_jump_rsi
red_asm_jump_rsi:
jmp *%rsi

// we don't need executable stack
.section .note.GNU-stack,"",%progbits
4 changes: 2 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ int main(int argc, char* argv[]) {
while(1) {
switch(program[pc]) {
case PRINT_NUM:
//redmagic_temp_disable();
redmagic_temp_disable();
cout << program[pc + 1] << endl;
//redmagic_temp_enable();
redmagic_temp_enable();
//printf("%i\n", program[pc + 1]);
pc += 2;
break;
Expand Down
4 changes: 4 additions & 0 deletions src/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ static const char *avoid_inlining_methods[] = {
"redmagic_ensure_not_traced",
"redmagic_temp_disable",
"redmagic_temp_enable",
"redmagic_is_traced",
"redmagic_disable_branch",
"redmagic_do_not_trace_function"
};

// namespace redmagic {
Expand Down Expand Up @@ -319,6 +322,7 @@ void* Manager::backwards_branch(void *id, void *ret_addr) {
head->is_compiled = true;
head->is_traced = true;
head->trace_id = id;
red_printf("entering trace %x\n", id);
return info->starting_point;
}
// don't care about atomic since we are just trying to get an estimate, so if we lose some counts it is fine
Expand Down
18 changes: 18 additions & 0 deletions src/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ extern "C" void* red_end_trace(mem_loc_t normal_end_address) {
} else {
protected_malloc = false;
}
red_printf("exiting trace %x\n", head.trace_id);
return ret;
}

Expand Down Expand Up @@ -459,6 +460,9 @@ void* Tracer::TempDisableTrace() {
compiler.mov(asmjit::x86::r15, asmjit::imm_u(0xdeadcafe));
compiler.bind(label);
auto written = compiler.finalize();
// SimpleCompiler compiler2(buffer.get());
// compiler2.mov(asmjit::x86::rax, asmjit::x86::ptr(asmjit::x86::rsp, -8));
// compiler2.TestRegister(RAX)
write_interrupt_block();

//temp_disable_resume = (void*)(written.getRawBuffer() + written.getOffset());
Expand All @@ -467,6 +471,19 @@ void* Tracer::TempDisableTrace() {
return (void*)last_call_ret_addr;
}

extern "C" void red_asm_jump_rsi();

void Tracer::TempEnableTrace(void *resume_pc) {
// check that the temp enable instruction is coming in at a expected spot, otherwise fork a new trace
set_pc((mem_loc_t)resume_pc);
SimpleCompiler compiler(buffer.get());
// the "normal" return address will be set to ris when this returns from the temp disabled region
//compiler.mov(asmjit::x86::rax, asmjit::x86::ptr(asmjit::x86::rsp, -8));
compiler.TestRegister((mem_loc_t)&red_asm_jump_rsi, RSI, (register_t)resume_pc);
auto written = compiler.finalize();
write_interrupt_block();
}

extern "C" void red_asm_start_nested_trace();

void Tracer::JumpToNestedLoop(void *nested_trace_id) {
Expand Down Expand Up @@ -1345,6 +1362,7 @@ void Tracer::replace_rip_instruction() {
//case UD_Ipush: // have to do push independently since the stack is moving
case UD_Iadd:
case UD_Isub:
case UD_Iimul:
case UD_Icmp:
case UD_Itest:
case UD_Ixor:
Expand Down
2 changes: 1 addition & 1 deletion src/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace redmagic {

// generate a temp disable command, sets the thread local where to resume to address
void* TempDisableTrace();
void TempEnableTrace(void *resume_pc) { set_pc((uint64_t)resume_pc); }
void TempEnableTrace(void *resume_pc);// { set_pc((uint64_t)resume_pc); }

void* ReplaceIsTracedCall();

Expand Down
2 changes: 1 addition & 1 deletion src/user_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef DISABLE_REDMAGIC

__asm__("jmp_rax: \n"
"add $8, %rsp \n"
"pop %rsi \n"
"jmp *%rax \n"
);

Expand Down

0 comments on commit a56edc5

Please sign in to comment.