|
| 1 | +//===--------- BPFCheckUnreachableIR.cpp - Issue Unreachable Error --------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Check 'unreachable' IRs and issue proper errors. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "BPF.h" |
| 14 | +#include "llvm/IR/Constants.h" |
| 15 | +#include "llvm/IR/DiagnosticInfo.h" |
| 16 | +#include "llvm/IR/Instruction.h" |
| 17 | +#include "llvm/IR/Module.h" |
| 18 | +#include "llvm/IR/Type.h" |
| 19 | +#include "llvm/IR/Value.h" |
| 20 | +#include "llvm/Pass.h" |
| 21 | + |
| 22 | +#define DEBUG_TYPE "bpf-check-unreachable-ir" |
| 23 | + |
| 24 | +using namespace llvm; |
| 25 | + |
| 26 | +static cl::opt<bool> |
| 27 | + DisableCheckUnreachableIR("bpf-disable-check-unreachable-ir", cl::Hidden, |
| 28 | + cl::desc("BPF: Disable Checking Unreachable IR"), |
| 29 | + cl::init(false)); |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +class BPFCheckUnreachableIR final : public ModulePass { |
| 34 | + bool runOnModule(Module &F) override; |
| 35 | + |
| 36 | +public: |
| 37 | + static char ID; |
| 38 | + BPFCheckUnreachableIR() : ModulePass(ID) {} |
| 39 | + |
| 40 | +private: |
| 41 | + void BPFCheckUnreachableIRImpl(Function &F); |
| 42 | + void BPFCheckInst(Function &F, BasicBlock &BB, Instruction &I); |
| 43 | + void HandleUnreachableInsn(Function &F, BasicBlock &BB, Instruction &I); |
| 44 | +}; |
| 45 | +} // End anonymous namespace |
| 46 | + |
| 47 | +char BPFCheckUnreachableIR::ID = 0; |
| 48 | +INITIALIZE_PASS(BPFCheckUnreachableIR, DEBUG_TYPE, "BPF Check Unreachable IRs", |
| 49 | + false, false) |
| 50 | + |
| 51 | +ModulePass *llvm::createBPFCheckUnreachableIR() { |
| 52 | + return new BPFCheckUnreachableIR(); |
| 53 | +} |
| 54 | + |
| 55 | +void BPFCheckUnreachableIR::HandleUnreachableInsn(Function &F, BasicBlock &BB, |
| 56 | + Instruction &I) { |
| 57 | + // LLVM may create a switch statement with default to a 'unreachable' basic |
| 58 | + // block. Do not warn for such cases. |
| 59 | + unsigned NumNoSwitches = 0, NumSwitches = 0; |
| 60 | + for (BasicBlock *Pred : predecessors(&BB)) { |
| 61 | + const Instruction *Term = Pred->getTerminator(); |
| 62 | + if (Term && Term->getOpcode() == Instruction::Switch) { |
| 63 | + NumSwitches++; |
| 64 | + continue; |
| 65 | + } |
| 66 | + NumNoSwitches++; |
| 67 | + } |
| 68 | + if (NumSwitches > 0 && NumNoSwitches == 0) |
| 69 | + return; |
| 70 | + |
| 71 | + // If the previous insn is no return, do not warn for such cases. |
| 72 | + // One example is __bpf_unreachable from libbpf bpf_headers.h. |
| 73 | + Instruction *PrevI = I.getPrevNonDebugInstruction(); |
| 74 | + if (PrevI) { |
| 75 | + auto *CI = dyn_cast<CallInst>(PrevI); |
| 76 | + if (CI && CI->doesNotReturn()) |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Typically the 'unreachable' insn is the last insn in the function. |
| 81 | + // Find the closest line number to this insn and report such info to users. |
| 82 | + uint32_t LineNum = 0; |
| 83 | + for (Instruction &Insn : llvm::reverse(BB)) { |
| 84 | + const DebugLoc &DL = Insn.getDebugLoc(); |
| 85 | + if (!DL || DL.getLine() == 0) |
| 86 | + continue; |
| 87 | + LineNum = DL.getLine(); |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + std::string LineInfo; |
| 92 | + if (LineNum) |
| 93 | + LineInfo = |
| 94 | + " from line " + std::to_string(LineNum) + " to the end of function"; |
| 95 | + |
| 96 | + F.getContext().diagnose(DiagnosticInfoGeneric( |
| 97 | + Twine("in function ") |
| 98 | + .concat("\"" + F.getName() + "\"") |
| 99 | + .concat(LineInfo) |
| 100 | + .concat(" that code was deleted as unreachable.\n") |
| 101 | + .concat(" due to uninitialized variable? try -Wuninitialized?"), |
| 102 | + DS_Error)); |
| 103 | +} |
| 104 | + |
| 105 | +void BPFCheckUnreachableIR::BPFCheckInst(Function &F, BasicBlock &BB, |
| 106 | + Instruction &I) { |
| 107 | + if (I.getOpcode() == Instruction::Unreachable) |
| 108 | + HandleUnreachableInsn(F, BB, I); |
| 109 | +} |
| 110 | + |
| 111 | +void BPFCheckUnreachableIR::BPFCheckUnreachableIRImpl(Function &F) { |
| 112 | + // A 'unreachable' will be added to the end of naked function. |
| 113 | + // Let ignore these naked functions. |
| 114 | + if (F.hasFnAttribute(Attribute::Naked)) |
| 115 | + return; |
| 116 | + |
| 117 | + for (auto &BB : F) { |
| 118 | + for (auto &I : BB) |
| 119 | + BPFCheckInst(F, BB, I); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +bool BPFCheckUnreachableIR::runOnModule(Module &M) { |
| 124 | + if (DisableCheckUnreachableIR) |
| 125 | + return false; |
| 126 | + for (Function &F : M) |
| 127 | + BPFCheckUnreachableIRImpl(F); |
| 128 | + return false; |
| 129 | +} |
0 commit comments