-1

I have to create a LLVM analysis pass for an exam project which consist of printing the independent path of a function using the baseline method.

Currently, I am struggling on how can I build the baseline path traversing the various basic block. Furthermore, I know that basic block are already organized in a CFG but checking the documentation I can't find any useful method to build a linked list of basic block representing a path from the entry point to the end point of a function. I am not an expert with the LLVM environment and I want to ask if someone with more knowledge knows how to build this kind of path.

Thank you everyone.

Update: i followed the advice of the answer to this post and i made this code for building a path:

#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/CFG.h"
#include <set>
#include <list>
using namespace llvm;
using namespace std;


void Build_Baseline_path(BasicBlock *Start, set<BasicBlock *> Explored, list<BasicBlock *> Decision_points, list<BasicBlock *>Path) {

for (BasicBlock *Successor : successors(Start)) {

    Instruction *Teriminator = Successor->getTerminator();
    const char *Instruction_string = Teriminator->getOpcodeName();

    if (Instruction_string == "br" || Instruction_string == "switch") {
        errs() << "Decision point found" << "\n";
        Decision_points.push_back(Successor);
    }

    if (Instruction_string == "ret") {
        if (Explored.find(Successor) == Explored.end()) {
            errs() << "Added node to the baseline path" << "\n";
            Path.push_back(Successor);
            return;
        }
        return; 
    }

    if (Explored.find(Successor) == Explored.end()) {
        Path.push_back(Successor);
        Build_Baseline_path(Successor,Explored,Decision_points,Path);
    }
}
}

This is a code that wrote in another file .cpp and i include it in my Function Pass, but when i run the pass with this function, everything is blocked and seems like that my pc is crashing when i run this pass. I tried to comment the call of this function in the pass to see if the problem is somewhere else, but everything works fine so the problem is in this code, what is wrong in this code? I am sorry but i am a novice with c++, i can't figure out how to solve this.

1 Answers1

0

First off, there isn't a single end point. At least four kinds of instructions may be end points: return, unreachable and in some cases call/invoke (when the called function throws and the exception isn't caught in this function).

Accordingly, there are many possible paths. The number of possible paths is not even sure to be countable, depending on how you treat loops.

If you regard loops in a simplistic way and ignore exceptions, then it's simple to construct a list of paths. There exists an iterator called successors() which you can use as in this answer. You can use successors() in a recursive function to process successors, and when you reach a return or something like that, you act on the path you've built.

arnt
  • 8,949
  • 5
  • 24
  • 32