[PATCH v1 bpf-next 09/18] bpf: Find fmod_ret candidates in bpf_scan_ir
From: Anton Protopopov
Date: Fri Nov 06 2026 - 15:52:28 EST
Find the functions which can become fmod_ret targets: functions which
return a 32 or 64 bit integer and have a path from the entry without
observable side effects (a clean path) which returns an error. A
fmod_ret program which returns this error instead of calling the
function then behaves like the path. List the errors in the record of
the function, sorted and unique.
Take only the blocks which no dirty path reaches: after a dirty path
joins a clean one, a branch may depend on what the dirty path did. Only
the block of the return may also be reached by dirty paths, as it
returns the same constant on all paths.
For now an error must be a constant returned by a clean path. The
following changes find more of the errors.
Signed-off-by: Anton Protopopov <a.s.protopopov@xxxxxxxxx>
---
scripts/llvm-plugins/bpf_scan_ir.cpp | 117 ++++++++++++++++++++++++++-
1 file changed, 113 insertions(+), 4 deletions(-)
diff --git a/scripts/llvm-plugins/bpf_scan_ir.cpp b/scripts/llvm-plugins/bpf_scan_ir.cpp
index 6700896749d4..6828fa73717d 100644
--- a/scripts/llvm-plugins/bpf_scan_ir.cpp
+++ b/scripts/llvm-plugins/bpf_scan_ir.cpp
@@ -3,11 +3,17 @@
#include "../bpf/fmodret_metadata.h"
+#include <algorithm>
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Analysis.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
@@ -22,9 +28,114 @@ using namespace llvm;
namespace {
+static constexpr int MaxErrno = 4095;
+
/* The errors which clean paths of a function return */
using ReturnValues = SmallVector<int64_t, 8>;
+/* Return true and set N if V is an errno constant */
+static bool errnoConstant(const Value *V, int64_t &N)
+{
+ auto *C = dyn_cast<ConstantInt>(V);
+
+ if (!C || (C->getBitWidth() != 32 && C->getBitWidth() != 64))
+ return false;
+
+ N = C->getSExtValue();
+ return N >= -MaxErrno && N < 0;
+}
+
+static bool cleanInstruction(const Instruction &I)
+{
+ if (auto *Intrinsic = dyn_cast<IntrinsicInst>(&I)) {
+ switch (Intrinsic->getIntrinsicID()) {
+ case Intrinsic::assume:
+ case Intrinsic::lifetime_start:
+ case Intrinsic::lifetime_end:
+ case Intrinsic::invariant_start:
+ case Intrinsic::invariant_end:
+ return true;
+ default:
+ break;
+ }
+ }
+ return !I.mayHaveSideEffects();
+}
+
+static bool cleanBB(BasicBlock *BB)
+{
+ for (Instruction &I : *BB) {
+ if (!cleanInstruction(I))
+ return false;
+ }
+ return true;
+}
+
+/* Add the errors which clean paths of F are known to return to VALUES */
+static void cleanReturnValues(Function &F, ReturnValues &Values)
+{
+ SmallPtrSet<BasicBlock *, 32> CleanBBs;
+ SmallVector<BasicBlock *, 32> Worklist;
+ bool Changed;
+
+ /* The clean blocks which the entry reaches through clean blocks */
+ if (cleanBB(&F.getEntryBlock())) {
+ CleanBBs.insert(&F.getEntryBlock());
+ Worklist.push_back(&F.getEntryBlock());
+ }
+ while (!Worklist.empty()) {
+ BasicBlock *BB = Worklist.pop_back_val();
+
+ for (BasicBlock *Successor : successors(BB)) {
+ if (!CleanBBs.contains(Successor) && cleanBB(Successor)) {
+ CleanBBs.insert(Successor);
+ Worklist.push_back(Successor);
+ }
+ }
+ }
+
+ /*
+ * Keep the blocks which only clean paths reach: after a dirty path joins
+ * a clean one, a branch may depend on what the dirty path did
+ */
+ do {
+ Changed = false;
+ for (BasicBlock &BB : F) {
+ if (!CleanBBs.contains(&BB))
+ continue;
+ for (BasicBlock *Pred : predecessors(&BB)) {
+ if (!CleanBBs.contains(Pred)) {
+ CleanBBs.erase(&BB);
+ Changed = true;
+ break;
+ }
+ }
+ }
+ } while (Changed);
+
+ /*
+ * The returns of the clean blocks which only clean paths reach or which
+ * one of them branches to. Dirty paths may also reach the latter, but
+ * the returned constant is the same on all paths.
+ */
+ for (BasicBlock &BB : F) {
+ auto *Return = dyn_cast<ReturnInst>(BB.getTerminator());
+ bool Entered = CleanBBs.contains(&BB);
+ int64_t N;
+
+ if (!Return || !Return->getReturnValue() || !cleanBB(&BB))
+ continue;
+ for (BasicBlock *Pred : predecessors(&BB))
+ Entered |= CleanBBs.contains(Pred);
+ if (!Entered)
+ continue;
+ if (errnoConstant(Return->getReturnValue(), N))
+ Values.push_back(N);
+ }
+ llvm::sort(Values);
+ Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
+}
+
struct FunctionDescription {
Function *Fn;
unsigned Flags;
@@ -86,10 +197,8 @@ public:
Flags |= FMODRET_FN_WEAK;
if (Ret->isIntegerTy(64))
Flags |= FMODRET_FN_RET64;
- /*
- * Nothing finds the values yet. A weak definition always
- * has a record, see fmodret_metadata.h.
- */
+ cleanReturnValues(F, Values);
+ /* A weak definition always has a record, see fmodret_metadata.h */
if (!Values.empty() || (Flags & FMODRET_FN_WEAK))
Descriptions.push_back({&F, Flags, std::move(Values)});
}
--
2.43.0