merge changes from main, i will now delete main

master
xtremegamer1 2 years ago
commit a47406d73d

@ -2,7 +2,6 @@
name = "vmemu" name = "vmemu"
[subdir.deps] [subdir.deps]
[subdir.tools]
[target.vmemu] [target.vmemu]
type = "static" type = "static"

2
deps/vmprofiler vendored

@ -1 +1 @@
Subproject commit fcdc3866c658145d352e7b9f4bdf5fde0f30aaf3 Subproject commit e3abc955e14349a1a01d2ea1f998feafa7486de0

@ -176,6 +176,7 @@ void emu_t::extract_branch_data() {
br1 += m_vm->m_module_base; br1 += m_vm->m_module_base;
br2 += m_vm->m_module_base; br2 += m_vm->m_module_base;
// We need to handle the case that the jump points to another jump
auto br1_legit = legit_branch(*cc_blk, br1); auto br1_legit = legit_branch(*cc_blk, br1);
auto br2_legit = legit_branch(*cc_blk, br2); auto br2_legit = legit_branch(*cc_blk, br2);
std::printf("> br1 legit: %d, br2 legit: %d\n", br1_legit, br2_legit); std::printf("> br1 legit: %d, br2 legit: %d\n", br1_legit, br2_legit);
@ -273,19 +274,10 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
} }
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false; if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false;
uc_context* ctx; uc_context* ctx;
uct_context_alloc(uc, &ctx); uct_context_alloc(uc, &ctx);
uc_context_save(uc, ctx); uc_context_save(uc, ctx);
// if this is the first instruction of this handler then save the stack...
if (!obj->cc_trace.m_instrs.size()) {
obj->cc_trace.m_vip = obj->cc_blk->m_vm.vip;
obj->cc_trace.m_vsp = obj->cc_blk->m_vm.vsp;
obj->cc_trace.m_stack = new std::uint8_t[STACK_SIZE]; g_new_delete_tracker++;
uc_mem_read(uc, STACK_BASE, obj->cc_trace.m_stack, STACK_SIZE);
}
obj->cc_trace.m_instrs.push_back({instr, ctx}); obj->cc_trace.m_instrs.push_back({instr, ctx});
// RET or JMP REG means the end of a vm handler... // RET or JMP REG means the end of a vm handler...
@ -296,7 +288,7 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
// makes it easier for profiles to be correct... // makes it easier for profiles to be correct...
vm::instrs::deobfuscate(obj->cc_trace); vm::instrs::deobfuscate(obj->cc_trace);
// find the last MOV REG, DWORD PTR [VIP] in the instruction stream, then /* // find the last MOV REG, DWORD PTR [VIP] in the instruction stream, then
// remove any instructions from this instruction to the JMP/RET... // remove any instructions from this instruction to the JMP/RET...
const auto rva_fetch = std::find_if( const auto rva_fetch = std::find_if(
obj->cc_trace.m_instrs.rbegin(), obj->cc_trace.m_instrs.rend(), obj->cc_trace.m_instrs.rbegin(), obj->cc_trace.m_instrs.rend(),
@ -307,7 +299,7 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
i.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && i.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
i.operands[1].mem.base == vip && i.operands[1].size == 32; i.operands[1].mem.base == vip && i.operands[1].size == 32;
}); });
if (rva_fetch != obj->cc_trace.m_instrs.rend()) if (rva_fetch != obj->cc_trace.m_instrs.rend())
{ {
@ -317,17 +309,16 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
} }
obj->cc_trace.m_instrs.erase((rva_fetch + 1).base(), obj->cc_trace.m_instrs.erase((rva_fetch + 1).base(),
obj->cc_trace.m_instrs.end()); obj->cc_trace.m_instrs.end());
} } */
const auto vinstr = vm::instrs::determine(obj->cc_trace); const auto vinstr = vm::instrs::determine(obj->cc_trace);
// -- free the trace since we will start a new one... // -- free the trace since we will start a new one...
std::for_each(obj->cc_trace.m_instrs.begin(), obj->cc_trace.m_instrs.end(), std::for_each(obj->cc_trace.m_instrs.begin(), obj->cc_trace.m_instrs.end(),
[&](const vm::instrs::emu_instr_t& instr) { [&](const vm::instrs::emu_instr_t& instr) {
uct_context_free(instr.m_cpu); uct_context_free(instr.m_cpu);
}); });
delete[] obj->cc_trace.m_stack; if (obj->cc_trace.m_stack) g_new_delete_tracker--;
obj->cc_trace.m_instrs.clear(); obj->cc_trace.m_instrs.clear();
if (vinstr.mnemonic != vm::instrs::mnemonic_t::jmp) { if (vinstr.mnemonic != vm::instrs::mnemonic_t::jmp) {
@ -569,6 +560,10 @@ bool emu_t::legit_branch(vm::instrs::vblk_t& vblk, std::uintptr_t branch_addr) {
uc_reg_read(uc, UC_X86_REG_RIP, &rip); uc_reg_read(uc, UC_X86_REG_RIP, &rip);
uc_reg_read(uc, vm::instrs::reg_map[vblk.m_vm.vsp], &vsp); uc_reg_read(uc, vm::instrs::reg_map[vblk.m_vm.vsp], &vsp);
uc_mem_write(uc, vsp, &branch_addr, sizeof branch_addr); uc_mem_write(uc, vsp, &branch_addr, sizeof branch_addr);
// reset vsp and vip to their pre-jmp status in the current code trace
// this will be reset when the jmp instruction is emulated again
cc_trace.m_vsp = cc_blk->m_vm.vsp, cc_trace.m_vip = cc_blk->m_vm.vip;
m_sreg_cnt = 0u; m_sreg_cnt = 0u;
uc_emu_start(uc, rip, 0ull, 0ull, 0ull); uc_emu_start(uc, rip, 0ull, 0ull, 0ull);
@ -610,31 +605,37 @@ std::optional<std::pair<std::uintptr_t, std::uintptr_t>> emu_t::could_have_jcc(
return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64; return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64;
}; };
const auto lconst1 = static const auto valid_mem = [=](uintptr_t loc)
std::find_if(vinstrs.rbegin(), vinstrs.rend(), lconst64_chk); {
// check to see if the imm val is inside of the image...
if (lconst1 == vinstrs.rend()) return {}; if (loc > m_vm->m_image_base + m_vm->m_image_size ||
loc < m_vm->m_image_base ||
const auto lconst2 = std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk); loc > m_vm->m_image_base + m_vm->m_image_size ||
loc < m_vm->m_image_base)
return false;
// check to see if the imm's points to something inside of an executable
// section...
if (!vm::utils::scn::executable(
m_vm->m_module_base,
(loc - m_vm->m_image_base) + m_vm->m_module_base))
return false;
return true;
};
auto lconst1 = vinstrs.rbegin();
do {
lconst1 =
std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk);
if (lconst1 == vinstrs.rend()) return {};
} while (!valid_mem(lconst1->imm.val));
auto lconst2 = lconst1;
do {
lconst2 =
std::find_if(lconst2 + 1, vinstrs.rend(), lconst64_chk);
if (lconst2 == vinstrs.rend()) return {}; if (lconst2 == vinstrs.rend()) return {};
} while (!valid_mem(lconst1->imm.val));
// check to see if the imm val is inside of the image...
if (lconst1->imm.val > m_vm->m_image_base + m_vm->m_image_size ||
lconst1->imm.val < m_vm->m_image_base ||
lconst2->imm.val > m_vm->m_image_base + m_vm->m_image_size ||
lconst2->imm.val < m_vm->m_image_base)
return {};
// check to see if the imm's points to something inside of an executable
// section...
if (!vm::utils::scn::executable(
m_vm->m_module_base,
(lconst1->imm.val - m_vm->m_image_base) + m_vm->m_module_base) ||
!vm::utils::scn::executable(
m_vm->m_module_base,
(lconst2->imm.val - m_vm->m_image_base) + m_vm->m_module_base))
return {};
return {{lconst1->imm.val, lconst2->imm.val}}; return {{lconst1->imm.val, lconst2->imm.val}};
} }

Loading…
Cancel
Save