Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 1 | //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass statically checks for common and easily-identified constructs |
| 11 | // which produce undefined or likely unintended behavior in LLVM IR. |
| 12 | // |
| 13 | // It is not a guarantee of correctness, in two ways. First, it isn't |
| 14 | // comprehensive. There are checks which could be done statically which are |
| 15 | // not yet implemented. Some of these are indicated by TODO comments, but |
| 16 | // those aren't comprehensive either. Second, many conditions cannot be |
| 17 | // checked statically. This pass does no dynamic instrumentation, so it |
| 18 | // can't check for all possible problems. |
| 19 | // |
| 20 | // Another limitation is that it assumes all code will be executed. A store |
| 21 | // through a null pointer in a basic block which is never reached is harmless, |
| 22 | // but this pass will warn about it anyway. |
Dan Gohman | 0883355 | 2010-04-22 01:30:05 +0000 | [diff] [blame] | 23 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 24 | // Optimization passes may make conditions that this pass checks for more or |
| 25 | // less obvious. If an optimization pass appears to be introducing a warning, |
| 26 | // it may be that the optimization pass is merely exposing an existing |
| 27 | // condition in the code. |
| 28 | // |
| 29 | // This code may be run before instcombine. In many cases, instcombine checks |
| 30 | // for the same kinds of things and turns instructions with undefined behavior |
| 31 | // into unreachable (or equivalent). Because of this, this pass makes some |
| 32 | // effort to look through bitcasts and so on. |
| 33 | // |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "llvm/Analysis/Passes.h" |
| 37 | #include "llvm/Analysis/AliasAnalysis.h" |
| 38 | #include "llvm/Analysis/Lint.h" |
| 39 | #include "llvm/Analysis/ValueTracking.h" |
| 40 | #include "llvm/Assembly/Writer.h" |
| 41 | #include "llvm/Target/TargetData.h" |
| 42 | #include "llvm/Pass.h" |
| 43 | #include "llvm/PassManager.h" |
| 44 | #include "llvm/IntrinsicInst.h" |
| 45 | #include "llvm/Function.h" |
| 46 | #include "llvm/Support/CallSite.h" |
| 47 | #include "llvm/Support/Debug.h" |
| 48 | #include "llvm/Support/InstVisitor.h" |
| 49 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 51 | using namespace llvm; |
| 52 | |
| 53 | namespace { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 54 | namespace MemRef { |
| 55 | static unsigned Read = 1; |
| 56 | static unsigned Write = 2; |
| 57 | static unsigned Callee = 4; |
| 58 | static unsigned Branchee = 8; |
| 59 | } |
| 60 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 61 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 62 | friend class InstVisitor<Lint>; |
| 63 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 64 | void visitFunction(Function &F); |
| 65 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 66 | void visitCallSite(CallSite CS); |
| 67 | void visitMemoryReference(Instruction &I, Value *Ptr, unsigned Align, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 68 | const Type *Ty, unsigned Flags); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 69 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 70 | void visitCallInst(CallInst &I); |
| 71 | void visitInvokeInst(InvokeInst &I); |
| 72 | void visitReturnInst(ReturnInst &I); |
| 73 | void visitLoadInst(LoadInst &I); |
| 74 | void visitStoreInst(StoreInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 75 | void visitXor(BinaryOperator &I); |
| 76 | void visitSub(BinaryOperator &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 77 | void visitLShr(BinaryOperator &I); |
| 78 | void visitAShr(BinaryOperator &I); |
| 79 | void visitShl(BinaryOperator &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 80 | void visitSDiv(BinaryOperator &I); |
| 81 | void visitUDiv(BinaryOperator &I); |
| 82 | void visitSRem(BinaryOperator &I); |
| 83 | void visitURem(BinaryOperator &I); |
| 84 | void visitAllocaInst(AllocaInst &I); |
| 85 | void visitVAArgInst(VAArgInst &I); |
| 86 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 87 | void visitExtractElementInst(ExtractElementInst &I); |
| 88 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 89 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 90 | |
| 91 | public: |
| 92 | Module *Mod; |
| 93 | AliasAnalysis *AA; |
| 94 | TargetData *TD; |
| 95 | |
| 96 | std::string Messages; |
| 97 | raw_string_ostream MessagesStr; |
| 98 | |
| 99 | static char ID; // Pass identification, replacement for typeid |
| 100 | Lint() : FunctionPass(&ID), MessagesStr(Messages) {} |
| 101 | |
| 102 | virtual bool runOnFunction(Function &F); |
| 103 | |
| 104 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 105 | AU.setPreservesAll(); |
| 106 | AU.addRequired<AliasAnalysis>(); |
| 107 | } |
| 108 | virtual void print(raw_ostream &O, const Module *M) const {} |
| 109 | |
| 110 | void WriteValue(const Value *V) { |
| 111 | if (!V) return; |
| 112 | if (isa<Instruction>(V)) { |
| 113 | MessagesStr << *V << '\n'; |
| 114 | } else { |
| 115 | WriteAsOperand(MessagesStr, V, true, Mod); |
| 116 | MessagesStr << '\n'; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void WriteType(const Type *T) { |
| 121 | if (!T) return; |
| 122 | MessagesStr << ' '; |
| 123 | WriteTypeSymbolic(MessagesStr, T, Mod); |
| 124 | } |
| 125 | |
| 126 | // CheckFailed - A check failed, so print out the condition and the message |
| 127 | // that failed. This provides a nice place to put a breakpoint if you want |
| 128 | // to see why something is not correct. |
| 129 | void CheckFailed(const Twine &Message, |
| 130 | const Value *V1 = 0, const Value *V2 = 0, |
| 131 | const Value *V3 = 0, const Value *V4 = 0) { |
| 132 | MessagesStr << Message.str() << "\n"; |
| 133 | WriteValue(V1); |
| 134 | WriteValue(V2); |
| 135 | WriteValue(V3); |
| 136 | WriteValue(V4); |
| 137 | } |
| 138 | |
| 139 | void CheckFailed(const Twine &Message, const Value *V1, |
| 140 | const Type *T2, const Value *V3 = 0) { |
| 141 | MessagesStr << Message.str() << "\n"; |
| 142 | WriteValue(V1); |
| 143 | WriteType(T2); |
| 144 | WriteValue(V3); |
| 145 | } |
| 146 | |
| 147 | void CheckFailed(const Twine &Message, const Type *T1, |
| 148 | const Type *T2 = 0, const Type *T3 = 0) { |
| 149 | MessagesStr << Message.str() << "\n"; |
| 150 | WriteType(T1); |
| 151 | WriteType(T2); |
| 152 | WriteType(T3); |
| 153 | } |
| 154 | }; |
| 155 | } |
| 156 | |
| 157 | char Lint::ID = 0; |
| 158 | static RegisterPass<Lint> |
| 159 | X("lint", "Statically lint-checks LLVM IR", false, true); |
| 160 | |
| 161 | // Assert - We know that cond should be true, if not print an error message. |
| 162 | #define Assert(C, M) \ |
| 163 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
| 164 | #define Assert1(C, M, V1) \ |
| 165 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
| 166 | #define Assert2(C, M, V1, V2) \ |
| 167 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
| 168 | #define Assert3(C, M, V1, V2, V3) \ |
| 169 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 170 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 171 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
| 172 | |
| 173 | // Lint::run - This is the main Analysis entry point for a |
| 174 | // function. |
| 175 | // |
| 176 | bool Lint::runOnFunction(Function &F) { |
| 177 | Mod = F.getParent(); |
| 178 | AA = &getAnalysis<AliasAnalysis>(); |
| 179 | TD = getAnalysisIfAvailable<TargetData>(); |
| 180 | visit(F); |
| 181 | dbgs() << MessagesStr.str(); |
| 182 | return false; |
| 183 | } |
| 184 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 185 | void Lint::visitFunction(Function &F) { |
| 186 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 187 | // fairly common mistake to neglect to name a function. |
| 188 | Assert1(F.hasName() || F.hasLocalLinkage(), |
| 189 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void Lint::visitCallSite(CallSite CS) { |
| 193 | Instruction &I = *CS.getInstruction(); |
| 194 | Value *Callee = CS.getCalledValue(); |
| 195 | |
| 196 | // TODO: Check function alignment? |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 197 | visitMemoryReference(I, Callee, 0, 0, MemRef::Callee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 198 | |
| 199 | if (Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) { |
| 200 | Assert1(CS.getCallingConv() == F->getCallingConv(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 201 | "Undefined behavior: Caller and callee calling convention differ", |
| 202 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 203 | |
| 204 | const FunctionType *FT = F->getFunctionType(); |
| 205 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 206 | |
| 207 | Assert1(FT->isVarArg() ? |
| 208 | FT->getNumParams() <= NumActualArgs : |
| 209 | FT->getNumParams() == NumActualArgs, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 210 | "Undefined behavior: Call argument count mismatches callee " |
| 211 | "argument count", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 212 | |
| 213 | // TODO: Check argument types (in case the callee was casted) |
| 214 | |
| 215 | // TODO: Check ABI-significant attributes. |
| 216 | |
| 217 | // TODO: Check noalias attribute. |
| 218 | |
| 219 | // TODO: Check sret attribute. |
| 220 | } |
| 221 | |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame^] | 222 | if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall()) |
| 223 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 224 | AI != AE; ++AI) { |
| 225 | Value *Obj = (*AI)->getUnderlyingObject(); |
| 226 | Assert1(!isa<AllocaInst>(Obj) && !isa<VAArgInst>(Obj), |
| 227 | "Undefined behavior: Call with \"tail\" keyword references " |
| 228 | "alloca or va_arg", &I); |
| 229 | } |
| 230 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 231 | |
| 232 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 233 | switch (II->getIntrinsicID()) { |
| 234 | default: break; |
| 235 | |
| 236 | // TODO: Check more intrinsics |
| 237 | |
| 238 | case Intrinsic::memcpy: { |
| 239 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 240 | visitMemoryReference(I, MCI->getSource(), MCI->getAlignment(), 0, |
| 241 | MemRef::Write); |
| 242 | visitMemoryReference(I, MCI->getDest(), MCI->getAlignment(), 0, |
| 243 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 244 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 245 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 246 | // isn't expressive enough for what we really want to do. Known partial |
| 247 | // overlap is not distinguished from the case where nothing is known. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 248 | unsigned Size = 0; |
| 249 | if (const ConstantInt *Len = |
| 250 | dyn_cast<ConstantInt>(MCI->getLength()->stripPointerCasts())) |
| 251 | if (Len->getValue().isIntN(32)) |
| 252 | Size = Len->getValue().getZExtValue(); |
| 253 | Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
| 254 | AliasAnalysis::MustAlias, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 255 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | case Intrinsic::memmove: { |
| 259 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 260 | visitMemoryReference(I, MMI->getSource(), MMI->getAlignment(), 0, |
| 261 | MemRef::Write); |
| 262 | visitMemoryReference(I, MMI->getDest(), MMI->getAlignment(), 0, |
| 263 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | case Intrinsic::memset: { |
| 267 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 268 | visitMemoryReference(I, MSI->getDest(), MSI->getAlignment(), 0, |
| 269 | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | |
| 273 | case Intrinsic::vastart: |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 274 | Assert1(I.getParent()->getParent()->isVarArg(), |
| 275 | "Undefined behavior: va_start called in a non-varargs function", |
| 276 | &I); |
| 277 | |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 278 | visitMemoryReference(I, CS.getArgument(0), 0, 0, |
| 279 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 280 | break; |
| 281 | case Intrinsic::vacopy: |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 282 | visitMemoryReference(I, CS.getArgument(0), 0, 0, MemRef::Write); |
| 283 | visitMemoryReference(I, CS.getArgument(1), 0, 0, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 284 | break; |
| 285 | case Intrinsic::vaend: |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 286 | visitMemoryReference(I, CS.getArgument(0), 0, 0, |
| 287 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 288 | break; |
| 289 | |
| 290 | case Intrinsic::stackrestore: |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 291 | visitMemoryReference(I, CS.getArgument(0), 0, 0, |
| 292 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void Lint::visitCallInst(CallInst &I) { |
| 298 | return visitCallSite(&I); |
| 299 | } |
| 300 | |
| 301 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 302 | return visitCallSite(&I); |
| 303 | } |
| 304 | |
| 305 | void Lint::visitReturnInst(ReturnInst &I) { |
| 306 | Function *F = I.getParent()->getParent(); |
| 307 | Assert1(!F->doesNotReturn(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 308 | "Unusual: Return statement in function with noreturn attribute", |
| 309 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // TODO: Add a length argument and check that the reference is in bounds |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 313 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 314 | Value *Ptr, unsigned Align, const Type *Ty, |
| 315 | unsigned Flags) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 316 | Value *UnderlyingObject = Ptr->getUnderlyingObject(); |
| 317 | Assert1(!isa<ConstantPointerNull>(UnderlyingObject), |
| 318 | "Undefined behavior: Null pointer dereference", &I); |
| 319 | Assert1(!isa<UndefValue>(UnderlyingObject), |
| 320 | "Undefined behavior: Undef pointer dereference", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 321 | |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 322 | if (Flags & MemRef::Write) { |
| 323 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
| 324 | Assert1(!GV->isConstant(), |
| 325 | "Undefined behavior: Write to read-only memory", &I); |
| 326 | Assert1(!isa<Function>(UnderlyingObject) && |
| 327 | !isa<BlockAddress>(UnderlyingObject), |
| 328 | "Undefined behavior: Write to text section", &I); |
| 329 | } |
| 330 | if (Flags & MemRef::Read) { |
| 331 | Assert1(!isa<Function>(UnderlyingObject), |
| 332 | "Unusual: Load from function body", &I); |
| 333 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 334 | "Undefined behavior: Load from block address", &I); |
| 335 | } |
| 336 | if (Flags & MemRef::Callee) { |
| 337 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 338 | "Undefined behavior: Call to block address", &I); |
| 339 | } |
| 340 | if (Flags & MemRef::Branchee) { |
| 341 | Assert1(!isa<Constant>(UnderlyingObject) || |
| 342 | isa<BlockAddress>(UnderlyingObject), |
| 343 | "Undefined behavior: Branch to non-blockaddress", &I); |
| 344 | } |
| 345 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 346 | if (TD) { |
| 347 | if (Align == 0 && Ty) Align = TD->getABITypeAlignment(Ty); |
| 348 | |
| 349 | if (Align != 0) { |
| 350 | unsigned BitWidth = TD->getTypeSizeInBits(Ptr->getType()); |
| 351 | APInt Mask = APInt::getAllOnesValue(BitWidth), |
| 352 | KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 353 | ComputeMaskedBits(Ptr, Mask, KnownZero, KnownOne, TD); |
| 354 | Assert1(!(KnownOne & APInt::getLowBitsSet(BitWidth, Log2_32(Align))), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 355 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 361 | visitMemoryReference(I, I.getPointerOperand(), I.getAlignment(), I.getType(), |
| 362 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void Lint::visitStoreInst(StoreInst &I) { |
| 366 | visitMemoryReference(I, I.getPointerOperand(), I.getAlignment(), |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 367 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 370 | void Lint::visitXor(BinaryOperator &I) { |
| 371 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 372 | !isa<UndefValue>(I.getOperand(1)), |
| 373 | "Undefined result: xor(undef, undef)", &I); |
| 374 | } |
| 375 | |
| 376 | void Lint::visitSub(BinaryOperator &I) { |
| 377 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 378 | !isa<UndefValue>(I.getOperand(1)), |
| 379 | "Undefined result: sub(undef, undef)", &I); |
| 380 | } |
| 381 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 382 | void Lint::visitLShr(BinaryOperator &I) { |
| 383 | if (ConstantInt *CI = |
| 384 | dyn_cast<ConstantInt>(I.getOperand(1)->stripPointerCasts())) |
| 385 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 386 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void Lint::visitAShr(BinaryOperator &I) { |
| 390 | if (ConstantInt *CI = |
| 391 | dyn_cast<ConstantInt>(I.getOperand(1)->stripPointerCasts())) |
| 392 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 393 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void Lint::visitShl(BinaryOperator &I) { |
| 397 | if (ConstantInt *CI = |
| 398 | dyn_cast<ConstantInt>(I.getOperand(1)->stripPointerCasts())) |
| 399 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 400 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 403 | static bool isZero(Value *V, TargetData *TD) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 404 | // Assume undef could be zero. |
| 405 | if (isa<UndefValue>(V)) return true; |
| 406 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 407 | unsigned BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 408 | APInt Mask = APInt::getAllOnesValue(BitWidth), |
| 409 | KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 410 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD); |
| 411 | return KnownZero.isAllOnesValue(); |
| 412 | } |
| 413 | |
| 414 | void Lint::visitSDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 415 | Assert1(!isZero(I.getOperand(1), TD), |
| 416 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void Lint::visitUDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 420 | Assert1(!isZero(I.getOperand(1), TD), |
| 421 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void Lint::visitSRem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 425 | Assert1(!isZero(I.getOperand(1), TD), |
| 426 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void Lint::visitURem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 430 | Assert1(!isZero(I.getOperand(1), TD), |
| 431 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 435 | if (isa<ConstantInt>(I.getArraySize())) |
| 436 | // This isn't undefined behavior, it's just an obvious pessimization. |
| 437 | Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 438 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void Lint::visitVAArgInst(VAArgInst &I) { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 442 | visitMemoryReference(I, I.getOperand(0), 0, 0, |
| 443 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 447 | visitMemoryReference(I, I.getAddress(), 0, 0, MemRef::Branchee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 450 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
| 451 | if (ConstantInt *CI = |
| 452 | dyn_cast<ConstantInt>(I.getIndexOperand()->stripPointerCasts())) |
| 453 | Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 454 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
| 458 | if (ConstantInt *CI = |
| 459 | dyn_cast<ConstantInt>(I.getOperand(2)->stripPointerCasts())) |
| 460 | Assert1(CI->getValue().ult(I.getType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 461 | "Undefined result: insertelement index out of range", &I); |
| 462 | } |
| 463 | |
| 464 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 465 | // This isn't undefined behavior, it's merely suspicious. |
| 466 | Assert1(&I == I.getParent()->begin() || |
| 467 | prior(BasicBlock::iterator(&I))->mayHaveSideEffects(), |
| 468 | "Unusual: unreachable immediately preceded by instruction without " |
| 469 | "side effects", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 472 | //===----------------------------------------------------------------------===// |
| 473 | // Implement the public interfaces to this file... |
| 474 | //===----------------------------------------------------------------------===// |
| 475 | |
| 476 | FunctionPass *llvm::createLintPass() { |
| 477 | return new Lint(); |
| 478 | } |
| 479 | |
| 480 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 481 | /// |
| 482 | void llvm::lintFunction(const Function &f) { |
| 483 | Function &F = const_cast<Function&>(f); |
| 484 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 485 | |
| 486 | FunctionPassManager FPM(F.getParent()); |
| 487 | Lint *V = new Lint(); |
| 488 | FPM.add(V); |
| 489 | FPM.run(F); |
| 490 | } |
| 491 | |
| 492 | /// lintModule - Check a module for errors, printing messages on stderr. |
| 493 | /// Return true if the module is corrupt. |
| 494 | /// |
| 495 | void llvm::lintModule(const Module &M, std::string *ErrorInfo) { |
| 496 | PassManager PM; |
| 497 | Lint *V = new Lint(); |
| 498 | PM.add(V); |
| 499 | PM.run(const_cast<Module&>(M)); |
| 500 | |
| 501 | if (ErrorInfo) |
| 502 | *ErrorInfo = V->MessagesStr.str(); |
| 503 | } |