From 4e6f868afc6fda502730a84ae4eb03c7b5fdeebf Mon Sep 17 00:00:00 2001 From: Lukas Rothenberger Date: Tue, 27 Aug 2024 10:42:13 +0200 Subject: [PATCH] fix: potential endless recursion in hybrid instrumentation --- DiscoPoP/hybrid_analysis/InstructionCFG.cpp | 24 +++++++++++++++------ DiscoPoP/hybrid_analysis/InstructionCFG.hpp | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/DiscoPoP/hybrid_analysis/InstructionCFG.cpp b/DiscoPoP/hybrid_analysis/InstructionCFG.cpp index 035c2545a..09cabbe18 100644 --- a/DiscoPoP/hybrid_analysis/InstructionCFG.cpp +++ b/DiscoPoP/hybrid_analysis/InstructionCFG.cpp @@ -30,10 +30,11 @@ InstructionCFG::InstructionCFG(dputil::VariableNameFinder *_VNF, Function &F) : } // Add edges from last instruction in current block to first instruction all // the successor blocks - if (previousInstruction != nullptr) - findAndAddFirstRelevantInstructionInSuccessorBlocks(&BB, previousInstruction); + if (previousInstruction != nullptr){ + std::set> visited; + findAndAddFirstRelevantInstructionInSuccessorBlocks(&BB, previousInstruction, &visited); + } } - // Conect entry/exit nodes for (auto instNode : Graph::getInstructionNodes()) { if (instNode != entry && instNode != exit) { @@ -47,7 +48,17 @@ InstructionCFG::InstructionCFG(dputil::VariableNameFinder *_VNF, Function &F) : } void InstructionCFG::findAndAddFirstRelevantInstructionInSuccessorBlocks(BasicBlock *BB, - Instruction *previousInstruction) { + Instruction *previousInstruction, + std::set> *visited) { + // Check for and break out of cycles + std::pair tmp_pair = std::make_pair(BB, previousInstruction); + const bool already_visited = visited->find(tmp_pair) != visited->end(); + if(already_visited){ + return; + } + // register visited node + visited->insert(tmp_pair); + bool hasSuccessors = false; for (BasicBlock *S : successors(BB)) { hasSuccessors = true; @@ -62,8 +73,9 @@ void InstructionCFG::findAndAddFirstRelevantInstructionInSuccessorBlocks(BasicBl Graph::addEdge(Graph::getInstructionNode(previousInstruction), exit); } } - if (S != BB) - findAndAddFirstRelevantInstructionInSuccessorBlocks(S, previousInstruction); + if (S != BB){ + findAndAddFirstRelevantInstructionInSuccessorBlocks(S, previousInstruction, visited); + } next:; } } diff --git a/DiscoPoP/hybrid_analysis/InstructionCFG.hpp b/DiscoPoP/hybrid_analysis/InstructionCFG.hpp index daee2db4e..634644214 100644 --- a/DiscoPoP/hybrid_analysis/InstructionCFG.hpp +++ b/DiscoPoP/hybrid_analysis/InstructionCFG.hpp @@ -33,7 +33,7 @@ class InstructionCFG : public Graph { dputil::VariableNameFinder *VNF; set highlightedInstructionNodes; - void findAndAddFirstRelevantInstructionInSuccessorBlocks(BasicBlock *BB, Instruction *previousInstruction); + void findAndAddFirstRelevantInstructionInSuccessorBlocks(BasicBlock *BB, Instruction *previousInstruction, std::set> *visited); public: InstructionCFG(dputil::VariableNameFinder *_VNF, Function &F);