[llvm] [BOLT] Gadget scanner: analyze functions without CFG information (PR #133461)

Kristof Beyls via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 7 05:32:35 PDT 2025


================
@@ -522,37 +639,34 @@ Analysis::findGadgets(BinaryFunction &BF,
                       MCPlusBuilder::AllocatorIdTy AllocatorId) {
   FunctionAnalysisResult Result;
 
-  PacRetAnalysis PRA(BF, AllocatorId, {});
-  PRA.run();
+  auto PRA = PacRetAnalysis::create(BF, AllocatorId, {});
+  PRA->run();
   LLVM_DEBUG({
     dbgs() << " After PacRetAnalysis:\n";
     BF.dump();
   });
 
   BinaryContext &BC = BF.getBinaryContext();
-  for (BinaryBasicBlock &BB : BF) {
-    for (int64_t I = 0, E = BB.size(); I < E; ++I) {
-      MCInstReference Inst(&BB, I);
-      const State &S = *PRA.getStateBefore(Inst);
-
-      // If non-empty state was never propagated from the entry basic block
-      // to Inst, assume it to be unreachable and report a warning.
-      if (S.empty()) {
-        Result.Diagnostics.push_back(std::make_shared<GenericReport>(
-            Inst, "Warning: unreachable instruction found"));
-        continue;
-      }
-
-      if (auto Report = shouldReportReturnGadget(BC, Inst, S))
-        Result.Diagnostics.push_back(Report);
-
-      if (PacRetGadgetsOnly)
-        continue;
-
-      if (auto Report = shouldReportCallGadget(BC, Inst, S))
-        Result.Diagnostics.push_back(Report);
+  iterateOverInstrs(BF, [&](MCInstReference Inst) {
----------------
kbeyls wrote:

I'm guessing that you might've tried to write this line so that it looks like a "traditional" loop, a bit like
```
   for (MCInstReference &Inst: getInstRefIterator(BF)) {
      ...
   }
```
or something a bit similar? I'm assuming using a templated function was much simpler than whatever might be needed to make this look more like a regular for loop?

https://github.com/llvm/llvm-project/pull/133461


More information about the llvm-commits mailing list