added another lifter (POPVSP)...

merge-requests/7/head
_xeroxz 3 years ago
parent 53f22b26bb
commit d94ff6126d

@ -50,6 +50,7 @@ list(APPEND vmdevirt_SOURCES
"src/lifters/lreg.cpp" "src/lifters/lreg.cpp"
"src/lifters/mul.cpp" "src/lifters/mul.cpp"
"src/lifters/nand.cpp" "src/lifters/nand.cpp"
"src/lifters/popvsp.cpp"
"src/lifters/pushvsp.cpp" "src/lifters/pushvsp.cpp"
"src/lifters/read.cpp" "src/lifters/read.cpp"
"src/lifters/sflags.cpp" "src/lifters/sflags.cpp"

@ -1 +1 @@
Subproject commit 05c98b1ef57c3375ffd455221a3be1be6110d4eb Subproject commit 9b5f89f4881862598181163069c69f584257d3f0

@ -65,10 +65,10 @@ namespace vm
llvm::Value *load_value( std::uint8_t byte_size, llvm::GlobalValue *global ); llvm::Value *load_value( std::uint8_t byte_size, llvm::GlobalValue *global );
llvm::Value *load_value( std::uint8_t byte_size, llvm::AllocaInst *var ); llvm::Value *load_value( std::uint8_t byte_size, llvm::AllocaInst *var );
llvm::Value *compute_sf( std::uint8_t byte_size, llvm::Value *val ); llvm::Value *sf( std::uint8_t byte_size, llvm::Value *val );
llvm::Value *compute_zf( std::uint8_t byte_size, llvm::Value *val ); llvm::Value *zf( std::uint8_t byte_size, llvm::Value *val );
llvm::Value *compute_pf( std::uint8_t byte_size, llvm::Value *val ); llvm::Value *pf( std::uint8_t byte_size, llvm::Value *val );
llvm::Value *combine_flags( llvm::Value *cf, llvm::Value *pf, llvm::Value *af, llvm::Value *zf, llvm::Value *sf, llvm::Value *flags( llvm::Value *cf, llvm::Value *pf, llvm::Value *af, llvm::Value *zf, llvm::Value *sf,
llvm::Value *of ); llvm::Value *of );
}; };
} // namespace vm } // namespace vm

@ -36,6 +36,7 @@ namespace vm
static lifter_callback_t lregq, lregdw; static lifter_callback_t lregq, lregdw;
static lifter_callback_t pushvsp; static lifter_callback_t pushvsp;
static lifter_callback_t popvsp;
static lifter_callback_t readq, readdw; static lifter_callback_t readq, readdw;
static lifter_callback_t nandq, nanddw; static lifter_callback_t nandq, nanddw;
static lifter_callback_t shrq; static lifter_callback_t shrq;
@ -56,6 +57,7 @@ namespace vm
{ vm::handler::ADDW, &addw }, { vm::handler::ADDW, &addw },
{ vm::handler::SHRQ, &shrq }, { vm::handler::SHRQ, &shrq },
{ vm::handler::PUSHVSP, &pushvsp }, { vm::handler::PUSHVSP, &pushvsp },
{ vm::handler::POPVSP, &popvsp },
{ vm::handler::SREGQ, &sregq }, { vm::handler::SREGQ, &sregq },
{ vm::handler::SREGDW, &sregdw }, { vm::handler::SREGDW, &sregdw },
{ vm::handler::LREGQ, &lregq }, { vm::handler::LREGQ, &lregq },

@ -129,21 +129,21 @@ namespace vm
return vmp_rtn->llvm_fptr; return vmp_rtn->llvm_fptr;
} }
llvm::Value *devirt_t::compute_sf( std::uint8_t byte_size, llvm::Value *val ) llvm::Value *devirt_t::sf( std::uint8_t byte_size, llvm::Value *val )
{ {
auto op_size = llvm::IntegerType::get( *llvm_ctx, byte_size * 8 ); auto op_size = llvm::IntegerType::get( *llvm_ctx, byte_size * 8 );
auto msb = ir_builder->CreateLShr( val, ( byte_size * 8 ) - 1 ); auto msb = ir_builder->CreateLShr( val, ( byte_size * 8 ) - 1 );
return ir_builder->CreateZExt( msb, llvm::IntegerType::get( *llvm_ctx, 64 ) ); return ir_builder->CreateZExt( msb, llvm::IntegerType::get( *llvm_ctx, 64 ) );
} }
llvm::Value *devirt_t::compute_zf( std::uint8_t byte_size, llvm::Value *val ) llvm::Value *devirt_t::zf( std::uint8_t byte_size, llvm::Value *val )
{ {
auto op_size = llvm::IntegerType::get( *llvm_ctx, byte_size * 8 ); auto op_size = llvm::IntegerType::get( *llvm_ctx, byte_size * 8 );
auto is_zero = ir_builder->CreateICmpEQ( val, llvm::ConstantInt::get( op_size, 0 ) ); auto is_zero = ir_builder->CreateICmpEQ( val, llvm::ConstantInt::get( op_size, 0 ) );
return ir_builder->CreateZExt( is_zero, llvm::IntegerType::get( *llvm_ctx, 64 ) ); return ir_builder->CreateZExt( is_zero, llvm::IntegerType::get( *llvm_ctx, 64 ) );
} }
llvm::Value *devirt_t::compute_pf( std::uint8_t byte_size, llvm::Value *val ) llvm::Value *devirt_t::pf( std::uint8_t byte_size, llvm::Value *val )
{ {
auto operand_size = llvm::IntegerType::get( *llvm_ctx, byte_size * 8 ); auto operand_size = llvm::IntegerType::get( *llvm_ctx, byte_size * 8 );
auto popcount_intrinsic = llvm::Intrinsic::getDeclaration( llvm_module, llvm::Intrinsic::ctpop, auto popcount_intrinsic = llvm::Intrinsic::getDeclaration( llvm_module, llvm::Intrinsic::ctpop,
@ -154,7 +154,7 @@ namespace vm
return ir_builder->CreateCall( popcount_intrinsic, { extended_bits } ); return ir_builder->CreateCall( popcount_intrinsic, { extended_bits } );
} }
llvm::Value *devirt_t::combine_flags( llvm::Value *cf, llvm::Value *pf, llvm::Value *af, llvm::Value *zf, llvm::Value *devirt_t::flags( llvm::Value *cf, llvm::Value *pf, llvm::Value *af, llvm::Value *zf,
llvm::Value *sf, llvm::Value *of ) llvm::Value *sf, llvm::Value *of )
{ {
auto shifted_pf = ir_builder->CreateShl( pf, 2, "shifted_pf", true, true ); auto shifted_pf = ir_builder->CreateShl( pf, 2, "shifted_pf", true, true );

@ -24,12 +24,12 @@ namespace vm
auto s_of_bit = rtn->ir_builder->CreateExtractValue( s_add, { 1 } ); auto s_of_bit = rtn->ir_builder->CreateExtractValue( s_add, { 1 } );
auto of = rtn->ir_builder->CreateZExt( s_of_bit, llvm::IntegerType::get( *rtn->llvm_ctx, 64 ) ); auto of = rtn->ir_builder->CreateZExt( s_of_bit, llvm::IntegerType::get( *rtn->llvm_ctx, 64 ) );
auto sf = rtn->compute_sf( byte_size, u_sum ); auto sf = rtn->sf( byte_size, u_sum );
auto zf = rtn->compute_zf( byte_size, u_sum ); auto zf = rtn->zf( byte_size, u_sum );
auto pf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), auto pf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ),
0 ); // TODO make clean PF bit computation... 0 ); // TODO make clean PF bit computation...
auto flags_calc = rtn->combine_flags( auto flags_calc = rtn->flags(
cf, pf, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ), zf, sf, of ); cf, pf, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ), zf, sf, of );
return flags_calc; return flags_calc;

@ -7,11 +7,11 @@ namespace vm
auto cf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ); auto cf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 );
auto of = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ); auto of = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 );
auto sf = rtn->compute_sf( byte_size, result ); auto sf = rtn->sf( byte_size, result );
auto zf = rtn->compute_zf( byte_size, result ); auto zf = rtn->zf( byte_size, result );
auto pf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ); auto pf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 );
return rtn->combine_flags( cf, pf, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ), return rtn->flags( cf, pf, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ),
zf, sf, of ); zf, sf, of );
} }

@ -0,0 +1,16 @@
#include <vm_lifters.hpp>
namespace vm
{
lifters_t::lifter_callback_t lifters_t::popvsp =
[ & ]( vm::devirt_t *rtn, const vm::instrs::code_block_t &vm_code_block, const vm::instrs::virt_instr_t &vinstr,
llvm::IRBuilder<> *ir_builder ) {
auto &vmp_rtn = rtn->vmp_rtns.back();
auto stack = ir_builder->CreateLoad( vmp_rtn->stack );
auto stack_ptr_ptr = ir_builder->CreatePointerCast(
stack, llvm::PointerType::get( llvm::PointerType::get( ir_builder->getInt8Ty(), 0ull ), 0ull ) );
auto stack_ptr = ir_builder->CreateLoad( stack_ptr_ptr );
ir_builder->CreateStore( stack_ptr, vmp_rtn->stack );
};
}

@ -11,12 +11,12 @@ namespace vm
auto cf = rtn->ir_builder->CreateZExt( msb, llvm::IntegerType::get( *rtn->llvm_ctx, 64 ) ); auto cf = rtn->ir_builder->CreateZExt( msb, llvm::IntegerType::get( *rtn->llvm_ctx, 64 ) );
auto of = auto of =
rtn->compute_sf( byte_size, lhs ); // we reuse the compute_sf helper since the flag expression is the same rtn->sf( byte_size, lhs ); // we reuse the compute_sf helper since the flag expression is the same
auto sf = rtn->compute_sf( byte_size, result ); auto sf = rtn->sf( byte_size, result );
auto zf = rtn->compute_zf( byte_size, result ); auto zf = rtn->zf( byte_size, result );
auto pf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ); auto pf = llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 );
return rtn->combine_flags( cf, pf, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ), return rtn->flags( cf, pf, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 0 ),
zf, sf, of ); zf, sf, of );
} }

@ -8,6 +8,8 @@ int main( int argc, const char *argv[] )
{ {
argparse::argument_parser_t parser( "vmdevirt", "virtual instruction pseudo code generator" ); argparse::argument_parser_t parser( "vmdevirt", "virtual instruction pseudo code generator" );
parser.add_argument().name( "--vmp2file" ).required( true ).description( "path to .vmp2 file..." ); parser.add_argument().name( "--vmp2file" ).required( true ).description( "path to .vmp2 file..." );
parser.add_argument().name( "--bin" ).required( true ).description(
"path to the image in which to apply devirtualized code too...\n" );
parser.enable_help(); parser.enable_help();
auto err = parser.parse( argc, argv ); auto err = parser.parse( argc, argv );

@ -27,13 +27,10 @@ namespace vm
void vmp_rtn_t::create_routine( void ) void vmp_rtn_t::create_routine( void )
{ {
// function has no arguments and returns void... maybe change this in the future as i learn
// more and more LLVM...
auto func_ty = auto func_ty =
llvm::FunctionType::get( llvm::PointerType::getInt8PtrTy( ir_builder->getContext() ), llvm::FunctionType::get( llvm::PointerType::getInt8PtrTy( ir_builder->getContext() ),
{ llvm::PointerType::getInt8PtrTy( ir_builder->getContext() ) }, false ); { llvm::PointerType::getInt8PtrTy( ir_builder->getContext() ) }, false );
// convert the rtn_begin address to a hex string and prepend "rtn_" to it...
std::stringstream rtn_name; std::stringstream rtn_name;
rtn_name << "rtn_" << std::hex << rtn_begin; rtn_name << "rtn_" << std::hex << rtn_begin;
llvm_fptr = llvm::Function::Create( func_ty, llvm::GlobalValue::LinkageTypes::ExternalLinkage, llvm_fptr = llvm::Function::Create( func_ty, llvm::GlobalValue::LinkageTypes::ExternalLinkage,

Loading…
Cancel
Save