From e6b2cceaf6af500284fafadf7a9929973ec598f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Wei=C3=9F?= <leon.weiss@rub.de> Date: Thu, 15 Jun 2023 12:02:20 +0200 Subject: [PATCH] Add feedback why the backward search has been stopped --- irdb-libs/ir_builders/back_search.hpp | 45 ++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/irdb-libs/ir_builders/back_search.hpp b/irdb-libs/ir_builders/back_search.hpp index 3ffd09d9c..a3dd6bba7 100644 --- a/irdb-libs/ir_builders/back_search.hpp +++ b/irdb-libs/ir_builders/back_search.hpp @@ -59,17 +59,19 @@ void calc_preds(FileIR_t* firp) } -// search for an expression in prior instructions. -bool backup_until(const string &insn_type_regex_str, // what to search for +// search for an expression in prior instructions. Return whether expression was found. +// If an assignment is found, stopped_because_set is set to this instruction and false is returned +bool backup_until_or_move(const string &insn_type_regex_str, // what to search for Instruction_t *& prev, // output param -- the instruction we found. Instruction_t* orig, // where to start the search. + Instruction_t *& stopped_because_set, // output param -- the instruction that stopped the search because of stop_if_set. nullptr if the search was not stopped because of this reason const string & stop_if_set="", // stop if an operand that's written matches this expression. const string & stop_if_opcode="", // stop if an opcode matches this expression bool recursive=false, // search recursively? uint32_t max_insns=10000u, // max number of instructions to search through. uint32_t max_recursions=5u) // make number of blocks to recusive into { - + stopped_because_set = static_cast<Instruction_t *>(nullptr); const auto find_or_build_regex=[&] (const string& s) -> regex_t& { // declare a freer for regexs so they go away when the program ends. @@ -130,8 +132,10 @@ bool backup_until(const string &insn_type_regex_str, // what to search for { for(const auto &operand : disasm->getOperands()) { - if(operand->isWritten() && regexec(&stop_operand_expression, operand->getString().c_str(), 0, nullptr, 0) == 0) - return false; + if(operand->isWritten() && regexec(&stop_operand_expression, operand->getString().c_str(), 0, nullptr, 0) == 0) { + stopped_because_set = prev; + return false; + } } } // if we have a stop_if_opcode expresison, check the opcode to see if it matches. @@ -156,14 +160,16 @@ bool backup_until(const string &insn_type_regex_str, // what to search for { for(const auto &operand : disasm->getOperands()) { - if(operand->isWritten() && regexec(&stop_operand_expression, operand->getString().c_str(), 0, nullptr, 0) == 0) - return false; + if(operand->isWritten() && regexec(&stop_operand_expression, operand->getString().c_str(), 0, nullptr, 0) == 0) { + stopped_because_set = pred; + return false; + } } } // if we have a stop_if_opcode expresison, check the opcode to see if it matches. if(stop_if_opcode!="" && regexec(&stop_opcode_expression, disasm->getMnemonic().c_str(), 0, nullptr, 0) == 0) return false; - if(backup_until(insn_type_regex_str, prev, pred, stop_if_set, stop_if_opcode, recursive, max_insns, max_recursions/mypreds.size())) + if(backup_until_or_move(insn_type_regex_str, prev, pred, stopped_because_set, stop_if_set, stop_if_opcode, recursive, max_insns, max_recursions/mypreds.size())) return true; // reset for next call @@ -173,5 +179,28 @@ bool backup_until(const string &insn_type_regex_str, // what to search for return false; } +// search for an expression in prior instructions. +bool backup_until(const string &insn_type_regex_str, // what to search for + Instruction_t *& prev, // output param -- the instruction we found. + Instruction_t* orig, // where to start the search. + const string & stop_if_set="", // stop if an operand that's written matches this expression. + const string & stop_if_opcode="", // stop if an opcode matches this expression + bool recursive=false, // search recursively? + uint32_t max_insns=10000u, // max number of instructions to search through. + uint32_t max_recursions=5u) // make number of blocks to recusive into +{ + // This function is just a proxy to keep the old API intact + auto discard = static_cast<Instruction_t *>(nullptr); + + return backup_until_or_move(insn_type_regex_str, + prev, + orig, + discard, + stop_if_set, + stop_if_opcode, + recursive, + max_insns, + max_recursions); +} #endif -- GitLab