bug fix - not all code paths were being traced... whoops...

dev
_xeroxz 3 years ago
parent e2173bb4c4
commit e19c711346

@ -93,10 +93,8 @@ namespace vm
return false; return false;
} }
if ( !cc_block ) if ( cc_block )
return false; code_blocks.push_back( code_block );
code_blocks.push_back( code_block );
// code_blocks.size() will continue to grow as all branches are traced... // code_blocks.size() will continue to grow as all branches are traced...
// when idx is > code_blocks.size() then we have traced all branches... // when idx is > code_blocks.size() then we have traced all branches...
@ -110,118 +108,114 @@ namespace vm
{ {
case vm::instrs::jcc_type::branching: case vm::instrs::jcc_type::branching:
{ {
if ( std::find( vip_begins.begin(), vip_begins.end(), _code_block.code_block.jcc.block_addr[ 1 ] ) != if ( std::find( vip_begins.begin(), vip_begins.end(), _code_block.code_block.jcc.block_addr[ 1 ] ) ==
vip_begins.end() ) vip_begins.end() )
continue;
std::uintptr_t rbp = 0ull;
std::uint32_t branch_rva =
( _code_block.code_block.jcc.block_addr[ 1 ] - g_vm_ctx->module_base ) + g_vm_ctx->image_base;
// setup object globals so that the tracing will work...
code_block_data_t branch_block{ {}, nullptr, nullptr };
cc_block = &branch_block;
g_vm_ctx = _code_block.g_vm_ctx.get();
// restore register values...
if ( ( err = uc_context_restore( uc_ctx, _code_block.cpu_ctx->context ) ) )
{ {
std::printf( "> failed to restore emu context... reason = %d\n", err ); std::uintptr_t rbp = 0ull;
return false; std::uint32_t branch_rva =
} ( _code_block.code_block.jcc.block_addr[ 1 ] - g_vm_ctx->module_base ) + g_vm_ctx->image_base;
// restore stack values... // setup object globals so that the tracing will work...
if ( ( err = uc_mem_write( uc_ctx, STACK_BASE, _code_block.cpu_ctx->stack, STACK_SIZE ) ) ) code_block_data_t branch_block{ {}, nullptr, nullptr };
{ cc_block = &branch_block;
std::printf( "> failed to restore stack... reason = %d\n", err ); g_vm_ctx = _code_block.g_vm_ctx.get();
return false;
}
// get the address in rbp (top of vsp)... then patch the branch rva... // restore register values...
if ( ( err = uc_reg_read( uc_ctx, UC_X86_REG_RBP, &rbp ) ) ) if ( ( err = uc_context_restore( uc_ctx, _code_block.cpu_ctx->context ) ) )
{ {
std::printf( "> failed to read rbp... reason = %d\n", err ); std::printf( "> failed to restore emu context... reason = %d\n", err );
return false; return false;
} }
// patch the branch rva... // restore stack values...
if ( ( err = uc_mem_write( uc_ctx, rbp, &branch_rva, sizeof branch_rva ) ) ) if ( ( err = uc_mem_write( uc_ctx, STACK_BASE, _code_block.cpu_ctx->stack, STACK_SIZE ) ) )
{ {
std::printf( "> failed to patch branch rva... reason = %d\n", err ); std::printf( "> failed to restore stack... reason = %d\n", err );
return false; return false;
} }
std::printf( "> beginning execution at = 0x%p\n", _code_block.cpu_ctx->rip ); // get the address in rbp (top of vsp)... then patch the branch rva...
if ( ( err = uc_emu_start( uc_ctx, _code_block.cpu_ctx->rip, 0ull, 0ull, 0ull ) ) ) if ( ( err = uc_reg_read( uc_ctx, UC_X86_REG_RBP, &rbp ) ) )
{ {
std::printf( "> error starting emu... reason = %d\n", err ); std::printf( "> failed to read rbp... reason = %d\n", err );
return false; return false;
} }
if ( !cc_block ) // patch the branch rva...
return false; if ( ( err = uc_mem_write( uc_ctx, rbp, &branch_rva, sizeof branch_rva ) ) )
{
std::printf( "> failed to patch branch rva... reason = %d\n", err );
return false;
}
// push back new block that has been traced... std::printf( "> beginning execution at = 0x%p\n", _code_block.cpu_ctx->rip );
code_blocks.push_back( branch_block ); if ( ( err = uc_emu_start( uc_ctx, _code_block.cpu_ctx->rip, 0ull, 0ull, 0ull ) ) )
{
std::printf( "> error starting emu... reason = %d\n", err );
return false;
}
if ( cc_block )
// push back new block that has been traced...
code_blocks.push_back( branch_block );
}
// drop down and execute the absolute case as well since that // drop down and execute the absolute case as well since that
// will trace the first branch... // will trace the first branch...
} }
case vm::instrs::jcc_type::absolute: case vm::instrs::jcc_type::absolute:
{ {
if ( std::find( vip_begins.begin(), vip_begins.end(), _code_block.code_block.jcc.block_addr[ 0 ] ) != if ( std::find( vip_begins.begin(), vip_begins.end(), _code_block.code_block.jcc.block_addr[ 0 ] ) ==
vip_begins.end() ) vip_begins.end() )
continue; {
std::uintptr_t rbp = 0ull;
std::uint32_t branch_rva =
( _code_block.code_block.jcc.block_addr[ 0 ] - g_vm_ctx->module_base ) + g_vm_ctx->image_base;
// setup object globals so that the tracing will work... std::uintptr_t rbp = 0ull;
code_block_data_t branch_block{ {}, nullptr, nullptr }; std::uint32_t branch_rva =
cc_block = &branch_block; ( _code_block.code_block.jcc.block_addr[ 0 ] - g_vm_ctx->module_base ) + g_vm_ctx->image_base;
g_vm_ctx = _code_block.g_vm_ctx.get();
// restore register values... // setup object globals so that the tracing will work...
if ( ( err = uc_context_restore( uc_ctx, _code_block.cpu_ctx->context ) ) ) code_block_data_t branch_block{ {}, nullptr, nullptr };
{ cc_block = &branch_block;
std::printf( "> failed to restore emu context... reason = %d\n", err ); g_vm_ctx = _code_block.g_vm_ctx.get();
return false;
}
// restore stack values... // restore register values...
if ( ( err = uc_mem_write( uc_ctx, STACK_BASE, _code_block.cpu_ctx->stack, STACK_SIZE ) ) ) if ( ( err = uc_context_restore( uc_ctx, _code_block.cpu_ctx->context ) ) )
{ {
std::printf( "> failed to restore stack... reason = %d\n", err ); std::printf( "> failed to restore emu context... reason = %d\n", err );
return false; return false;
} }
// get the address in rbp (top of vsp)... then patch the branch rva... // restore stack values...
if ( ( err = uc_reg_read( uc_ctx, UC_X86_REG_RBP, &rbp ) ) ) if ( ( err = uc_mem_write( uc_ctx, STACK_BASE, _code_block.cpu_ctx->stack, STACK_SIZE ) ) )
{ {
std::printf( "> failed to read rbp... reason = %d\n", err ); std::printf( "> failed to restore stack... reason = %d\n", err );
return false; return false;
} }
// patch the branch rva... // get the address in rbp (top of vsp)... then patch the branch rva...
if ( ( err = uc_mem_write( uc_ctx, rbp, &branch_rva, sizeof branch_rva ) ) ) if ( ( err = uc_reg_read( uc_ctx, UC_X86_REG_RBP, &rbp ) ) )
{ {
std::printf( "> failed to patch branch rva... reason = %d\n", err ); std::printf( "> failed to read rbp... reason = %d\n", err );
return false; return false;
} }
std::printf( "> beginning execution at = 0x%p\n", _code_block.cpu_ctx->rip ); // patch the branch rva...
if ( ( err = uc_emu_start( uc_ctx, _code_block.cpu_ctx->rip, 0ull, 0ull, 0ull ) ) ) if ( ( err = uc_mem_write( uc_ctx, rbp, &branch_rva, sizeof branch_rva ) ) )
{ {
std::printf( "> error starting emu... reason = %d\n", err ); std::printf( "> failed to patch branch rva... reason = %d\n", err );
return false; return false;
} }
if ( !cc_block ) std::printf( "> beginning execution at = 0x%p\n", _code_block.cpu_ctx->rip );
return false; if ( ( err = uc_emu_start( uc_ctx, _code_block.cpu_ctx->rip, 0ull, 0ull, 0ull ) ) )
{
std::printf( "> error starting emu... reason = %d\n", err );
return false;
}
// push back new block that has been traced... if ( cc_block )
code_blocks.push_back( branch_block ); // push back new block that has been traced...
code_blocks.push_back( branch_block );
}
break; break;
} }
case vm::instrs::jcc_type::switch_case: case vm::instrs::jcc_type::switch_case:
@ -277,11 +271,9 @@ namespace vm
return false; return false;
} }
if ( !cc_block ) if ( cc_block )
return false; // push back new block that has been traced...
code_blocks.push_back( branch_block );
// push back new block that has been traced...
code_blocks.push_back( branch_block );
} }
break; break;
} }

Loading…
Cancel
Save