2010-04-08 20:47:09 +02:00
|
|
|
//===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-04-08 20:47:09 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass statically checks for common and easily-identified constructs
|
|
|
|
// which produce undefined or likely unintended behavior in LLVM IR.
|
|
|
|
//
|
|
|
|
// It is not a guarantee of correctness, in two ways. First, it isn't
|
|
|
|
// comprehensive. There are checks which could be done statically which are
|
|
|
|
// not yet implemented. Some of these are indicated by TODO comments, but
|
|
|
|
// those aren't comprehensive either. Second, many conditions cannot be
|
|
|
|
// checked statically. This pass does no dynamic instrumentation, so it
|
|
|
|
// can't check for all possible problems.
|
2014-03-06 18:33:55 +01:00
|
|
|
//
|
2010-04-08 20:47:09 +02:00
|
|
|
// Another limitation is that it assumes all code will be executed. A store
|
|
|
|
// through a null pointer in a basic block which is never reached is harmless,
|
2010-07-06 17:21:57 +02:00
|
|
|
// but this pass will warn about it anyway. This is the main reason why most
|
|
|
|
// of these checks live here instead of in the Verifier pass.
|
2010-04-22 03:30:05 +02:00
|
|
|
//
|
2010-04-08 20:47:09 +02:00
|
|
|
// Optimization passes may make conditions that this pass checks for more or
|
|
|
|
// less obvious. If an optimization pass appears to be introducing a warning,
|
|
|
|
// it may be that the optimization pass is merely exposing an existing
|
|
|
|
// condition in the code.
|
2014-03-06 18:33:55 +01:00
|
|
|
//
|
2010-04-08 20:47:09 +02:00
|
|
|
// This code may be run before instcombine. In many cases, instcombine checks
|
|
|
|
// for the same kinds of things and turns instructions with undefined behavior
|
|
|
|
// into unreachable (or equivalent). Because of this, this pass makes some
|
|
|
|
// effort to look through bitcasts and so on.
|
2014-03-06 18:33:55 +01:00
|
|
|
//
|
2010-04-08 20:47:09 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-03 06:34:20 +02:00
|
|
|
#include "llvm/Analysis/Lint.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2010-04-08 20:47:09 +02:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2016-12-19 09:22:17 +01:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2010-05-28 18:21:24 +02:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2010-05-28 18:21:24 +02:00
|
|
|
#include "llvm/Analysis/Loads.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/Analysis/MemoryLocation.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2015-01-15 03:16:27 +01:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2010-04-08 20:47:09 +02:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-13 10:26:24 +01:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2014-03-06 04:23:41 +01:00
|
|
|
#include "llvm/IR/InstVisitor.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2015-02-13 11:01:29 +01:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/IR/Module.h"
|
2020-09-03 06:54:27 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Pass.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
2010-04-08 20:47:09 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-04-26 18:39:58 +02:00
|
|
|
#include "llvm/Support/KnownBits.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2010-04-08 20:47:09 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-08-13 02:50:41 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
|
2010-04-08 20:47:09 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2020-09-03 06:54:27 +02:00
|
|
|
namespace MemRef {
|
|
|
|
static const unsigned Read = 1;
|
|
|
|
static const unsigned Write = 2;
|
|
|
|
static const unsigned Callee = 4;
|
|
|
|
static const unsigned Branchee = 8;
|
|
|
|
} // end namespace MemRef
|
|
|
|
|
|
|
|
class Lint : public InstVisitor<Lint> {
|
|
|
|
friend class InstVisitor<Lint>;
|
|
|
|
|
|
|
|
void visitFunction(Function &F);
|
|
|
|
|
|
|
|
void visitCallBase(CallBase &CB);
|
2020-11-19 20:53:27 +01:00
|
|
|
void visitMemoryReference(Instruction &I, const MemoryLocation &Loc,
|
2020-09-03 06:54:27 +02:00
|
|
|
MaybeAlign Alignment, Type *Ty, unsigned Flags);
|
|
|
|
void visitEHBeginCatch(IntrinsicInst *II);
|
|
|
|
void visitEHEndCatch(IntrinsicInst *II);
|
|
|
|
|
|
|
|
void visitReturnInst(ReturnInst &I);
|
|
|
|
void visitLoadInst(LoadInst &I);
|
|
|
|
void visitStoreInst(StoreInst &I);
|
|
|
|
void visitXor(BinaryOperator &I);
|
|
|
|
void visitSub(BinaryOperator &I);
|
|
|
|
void visitLShr(BinaryOperator &I);
|
|
|
|
void visitAShr(BinaryOperator &I);
|
|
|
|
void visitShl(BinaryOperator &I);
|
|
|
|
void visitSDiv(BinaryOperator &I);
|
|
|
|
void visitUDiv(BinaryOperator &I);
|
|
|
|
void visitSRem(BinaryOperator &I);
|
|
|
|
void visitURem(BinaryOperator &I);
|
|
|
|
void visitAllocaInst(AllocaInst &I);
|
|
|
|
void visitVAArgInst(VAArgInst &I);
|
|
|
|
void visitIndirectBrInst(IndirectBrInst &I);
|
|
|
|
void visitExtractElementInst(ExtractElementInst &I);
|
|
|
|
void visitInsertElementInst(InsertElementInst &I);
|
|
|
|
void visitUnreachableInst(UnreachableInst &I);
|
|
|
|
|
|
|
|
Value *findValue(Value *V, bool OffsetOk) const;
|
|
|
|
Value *findValueImpl(Value *V, bool OffsetOk,
|
|
|
|
SmallPtrSetImpl<Value *> &Visited) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Module *Mod;
|
|
|
|
const DataLayout *DL;
|
|
|
|
AliasAnalysis *AA;
|
|
|
|
AssumptionCache *AC;
|
|
|
|
DominatorTree *DT;
|
|
|
|
TargetLibraryInfo *TLI;
|
|
|
|
|
|
|
|
std::string Messages;
|
|
|
|
raw_string_ostream MessagesStr;
|
|
|
|
|
|
|
|
Lint(Module *Mod, const DataLayout *DL, AliasAnalysis *AA,
|
|
|
|
AssumptionCache *AC, DominatorTree *DT, TargetLibraryInfo *TLI)
|
|
|
|
: Mod(Mod), DL(DL), AA(AA), AC(AC), DT(DT), TLI(TLI),
|
|
|
|
MessagesStr(Messages) {}
|
|
|
|
|
|
|
|
void WriteValues(ArrayRef<const Value *> Vs) {
|
|
|
|
for (const Value *V : Vs) {
|
|
|
|
if (!V)
|
|
|
|
continue;
|
|
|
|
if (isa<Instruction>(V)) {
|
|
|
|
MessagesStr << *V << '\n';
|
|
|
|
} else {
|
|
|
|
V->printAsOperand(MessagesStr, true, Mod);
|
|
|
|
MessagesStr << '\n';
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-03 06:54:27 +02:00
|
|
|
}
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2020-09-03 06:54:27 +02:00
|
|
|
/// A check failed, so printout out the condition and the message.
|
|
|
|
///
|
|
|
|
/// This provides a nice place to put a breakpoint if you want to see why
|
|
|
|
/// something is not correct.
|
|
|
|
void CheckFailed(const Twine &Message) { MessagesStr << Message << '\n'; }
|
|
|
|
|
|
|
|
/// A check failed (with values to print).
|
|
|
|
///
|
|
|
|
/// This calls the Message-only version so that the above is easier to set
|
|
|
|
/// a breakpoint on.
|
|
|
|
template <typename T1, typename... Ts>
|
|
|
|
void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
|
|
|
|
CheckFailed(Message);
|
|
|
|
WriteValues({V1, Vs...});
|
|
|
|
}
|
|
|
|
};
|
2016-08-13 02:50:41 +02:00
|
|
|
} // end anonymous namespace
|
2010-04-08 20:47:09 +02:00
|
|
|
|
|
|
|
// Assert - We know that cond should be true, if not print an error message.
|
2020-09-03 06:54:27 +02:00
|
|
|
#define Assert(C, ...) \
|
|
|
|
do { \
|
|
|
|
if (!(C)) { \
|
|
|
|
CheckFailed(__VA_ARGS__); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2010-04-09 03:39:53 +02:00
|
|
|
void Lint::visitFunction(Function &F) {
|
|
|
|
// This isn't undefined behavior, it's just a little unusual, and it's a
|
|
|
|
// fairly common mistake to neglect to name a function.
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(F.hasName() || F.hasLocalLinkage(),
|
|
|
|
"Unusual: Unnamed function with non-local linkage", &F);
|
2010-07-06 17:23:00 +02:00
|
|
|
|
|
|
|
// TODO: Check for irreducible control flow.
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 03:31:42 +02:00
|
|
|
void Lint::visitCallBase(CallBase &I) {
|
2020-04-28 05:15:59 +02:00
|
|
|
Value *Callee = I.getCalledOperand();
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2020-11-17 20:11:09 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getAfter(Callee), None, nullptr,
|
|
|
|
MemRef::Callee);
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2015-08-06 04:05:46 +02:00
|
|
|
if (Function *F = dyn_cast<Function>(findValue(Callee,
|
2015-03-10 03:37:25 +01:00
|
|
|
/*OffsetOk=*/false))) {
|
2020-04-20 03:31:42 +02:00
|
|
|
Assert(I.getCallingConv() == F->getCallingConv(),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: Caller and callee calling convention differ",
|
|
|
|
&I);
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2011-07-18 06:54:35 +02:00
|
|
|
FunctionType *FT = F->getFunctionType();
|
2020-04-20 03:31:42 +02:00
|
|
|
unsigned NumActualArgs = I.arg_size();
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(FT->isVarArg() ? FT->getNumParams() <= NumActualArgs
|
|
|
|
: FT->getNumParams() == NumActualArgs,
|
|
|
|
"Undefined behavior: Call argument count mismatches callee "
|
|
|
|
"argument count",
|
|
|
|
&I);
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(FT->getReturnType() == I.getType(),
|
|
|
|
"Undefined behavior: Call return type mismatches "
|
|
|
|
"callee return type",
|
|
|
|
&I);
|
2010-07-12 20:02:04 +02:00
|
|
|
|
2010-05-28 23:43:57 +02:00
|
|
|
// Check argument types (in case the callee was casted) and attributes.
|
2010-07-06 17:23:00 +02:00
|
|
|
// TODO: Verify that caller and callee attributes are compatible.
|
2010-05-28 23:43:57 +02:00
|
|
|
Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end();
|
2020-04-20 19:28:11 +02:00
|
|
|
auto AI = I.arg_begin(), AE = I.arg_end();
|
2010-05-28 23:43:57 +02:00
|
|
|
for (; AI != AE; ++AI) {
|
|
|
|
Value *Actual = *AI;
|
|
|
|
if (PI != PE) {
|
Analysis: Remove implicit ilist iterator conversions
Remove implicit ilist iterator conversions from LLVMAnalysis.
I came across something really scary in `llvm::isKnownNotFullPoison()`
which relied on `Instruction::getNextNode()` being completely broken
(not surprising, but scary nevertheless). This function is documented
(and coded to) return `nullptr` when it gets to the sentinel, but with
an `ilist_half_node` as a sentinel, the sentinel check looks into some
other memory and we don't recognize we've hit the end.
Rooting out these scary cases is the reason I'm removing the implicit
conversions before doing anything else with `ilist`; I'm not at all
surprised that clients rely on badness.
I found another scary case -- this time, not relying on badness, just
bad (but I guess getting lucky so far) -- in
`ObjectSizeOffsetEvaluator::compute_()`. Here, we save out the
insertion point, do some things, and then restore it. Previously, we
let the iterator auto-convert to `Instruction*`, and then set it back
using the `Instruction*` version:
Instruction *PrevInsertPoint = Builder.GetInsertPoint();
/* Logic that may change insert point */
if (PrevInsertPoint)
Builder.SetInsertPoint(PrevInsertPoint);
The check for `PrevInsertPoint` doesn't protect correctly against bad
accesses. If the insertion point has been set to the end of a basic
block (i.e., `SetInsertPoint(SomeBB)`), then `GetInsertPoint()` returns
an iterator pointing at the list sentinel. The version of
`SetInsertPoint()` that's getting called will then call
`PrevInsertPoint->getParent()`, which explodes horribly. The only
reason this hasn't blown up is that it's fairly unlikely the builder is
adding to the end of the block; usually, we're adding instructions
somewhere before the terminator.
llvm-svn: 249925
2015-10-10 02:53:03 +02:00
|
|
|
Argument *Formal = &*PI++;
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(Formal->getType() == Actual->getType(),
|
|
|
|
"Undefined behavior: Call argument type mismatches "
|
|
|
|
"callee parameter type",
|
|
|
|
&I);
|
2010-06-01 22:51:40 +02:00
|
|
|
|
2010-12-13 23:53:18 +01:00
|
|
|
// Check that noalias arguments don't alias other arguments. This is
|
|
|
|
// not fully precise because we don't know the sizes of the dereferenced
|
|
|
|
// memory regions.
|
2017-12-27 09:48:33 +01:00
|
|
|
if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) {
|
2020-04-20 03:31:42 +02:00
|
|
|
AttributeList PAL = I.getAttributes();
|
2017-12-27 09:48:33 +01:00
|
|
|
unsigned ArgNo = 0;
|
2020-04-20 19:28:11 +02:00
|
|
|
for (auto BI = I.arg_begin(); BI != AE; ++BI, ++ArgNo) {
|
2017-12-27 09:48:33 +01:00
|
|
|
// Skip ByVal arguments since they will be memcpy'd to the callee's
|
|
|
|
// stack so we're not really passing the pointer anyway.
|
2019-04-24 01:43:47 +02:00
|
|
|
if (PAL.hasParamAttribute(ArgNo, Attribute::ByVal))
|
|
|
|
continue;
|
|
|
|
// If both arguments are readonly, they have no dependence.
|
2020-04-20 03:31:42 +02:00
|
|
|
if (Formal->onlyReadsMemory() && I.onlyReadsMemory(ArgNo))
|
2017-12-27 09:48:33 +01:00
|
|
|
continue;
|
2010-12-10 21:04:06 +01:00
|
|
|
if (AI != BI && (*BI)->getType()->isPointerTy()) {
|
2015-06-22 04:16:51 +02:00
|
|
|
AliasResult Result = AA->alias(*AI, *BI);
|
2021-03-05 11:58:13 +01:00
|
|
|
Assert(Result != AliasResult::MustAlias &&
|
|
|
|
Result != AliasResult::PartialAlias,
|
2015-03-07 22:15:40 +01:00
|
|
|
"Unusual: noalias argument aliases another argument", &I);
|
2010-12-10 21:04:06 +01:00
|
|
|
}
|
2017-12-27 09:48:33 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-01 22:51:40 +02:00
|
|
|
|
|
|
|
// Check that an sret argument points to valid memory.
|
2010-05-28 23:43:57 +02:00
|
|
|
if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) {
|
2020-09-23 17:03:38 +02:00
|
|
|
Type *Ty = Formal->getParamStructRetType();
|
2020-11-19 20:53:27 +01:00
|
|
|
MemoryLocation Loc(
|
|
|
|
Actual, LocationSize::precise(DL->getTypeStoreSize(Ty)));
|
|
|
|
visitMemoryReference(I, Loc, DL->getABITypeAlign(Ty), Ty,
|
2015-03-10 03:37:25 +01:00
|
|
|
MemRef::Read | MemRef::Write);
|
2010-05-28 23:43:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 03:31:42 +02:00
|
|
|
if (const auto *CI = dyn_cast<CallInst>(&I)) {
|
2017-11-15 08:46:48 +01:00
|
|
|
if (CI->isTailCall()) {
|
|
|
|
const AttributeList &PAL = CI->getAttributes();
|
|
|
|
unsigned ArgNo = 0;
|
2020-04-20 03:31:42 +02:00
|
|
|
for (Value *Arg : I.args()) {
|
2017-11-15 08:46:48 +01:00
|
|
|
// Skip ByVal arguments since they will be memcpy'd to the callee's
|
|
|
|
// stack anyway.
|
|
|
|
if (PAL.hasParamAttribute(ArgNo++, Attribute::ByVal))
|
|
|
|
continue;
|
|
|
|
Value *Obj = findValue(Arg, /*OffsetOk=*/true);
|
|
|
|
Assert(!isa<AllocaInst>(Obj),
|
|
|
|
"Undefined behavior: Call with \"tail\" keyword references "
|
|
|
|
"alloca",
|
|
|
|
&I);
|
|
|
|
}
|
2010-05-26 23:46:36 +02:00
|
|
|
}
|
2017-11-15 08:46:48 +01:00
|
|
|
}
|
2010-05-26 23:46:36 +02:00
|
|
|
|
2010-04-08 20:47:09 +02:00
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I))
|
|
|
|
switch (II->getIntrinsicID()) {
|
2020-09-03 06:54:27 +02:00
|
|
|
default:
|
|
|
|
break;
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2020-09-03 06:54:27 +02:00
|
|
|
// TODO: Check more intrinsics
|
2010-04-08 20:47:09 +02:00
|
|
|
|
|
|
|
case Intrinsic::memcpy: {
|
|
|
|
MemCpyInst *MCI = cast<MemCpyInst>(&I);
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForDest(MCI),
|
2020-07-01 16:32:30 +02:00
|
|
|
MCI->getDestAlign(), nullptr, MemRef::Write);
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForSource(MCI),
|
2020-07-01 16:32:30 +02:00
|
|
|
MCI->getSourceAlign(), nullptr, MemRef::Read);
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2010-04-09 03:39:53 +02:00
|
|
|
// Check that the memcpy arguments don't overlap. The AliasAnalysis API
|
|
|
|
// isn't expressive enough for what we really want to do. Known partial
|
|
|
|
// overlap is not distinguished from the case where nothing is known.
|
2020-11-17 20:11:09 +01:00
|
|
|
auto Size = LocationSize::afterPointer();
|
2010-04-08 20:47:09 +02:00
|
|
|
if (const ConstantInt *Len =
|
2015-08-06 04:05:46 +02:00
|
|
|
dyn_cast<ConstantInt>(findValue(MCI->getLength(),
|
2015-03-10 03:37:25 +01:00
|
|
|
/*OffsetOk=*/false)))
|
2010-04-08 20:47:09 +02:00
|
|
|
if (Len->getValue().isIntN(32))
|
2018-12-23 03:50:08 +01:00
|
|
|
Size = LocationSize::precise(Len->getValue().getZExtValue());
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) !=
|
2021-03-05 11:58:13 +01:00
|
|
|
AliasResult::MustAlias,
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: memcpy source and destination overlap", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-12-19 17:33:36 +01:00
|
|
|
case Intrinsic::memcpy_inline: {
|
|
|
|
MemCpyInlineInst *MCII = cast<MemCpyInlineInst>(&I);
|
|
|
|
const uint64_t Size = MCII->getLength()->getValue().getLimitedValue();
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForDest(MCII),
|
|
|
|
MCII->getDestAlign(), nullptr, MemRef::Write);
|
|
|
|
visitMemoryReference(I, MemoryLocation::getForSource(MCII),
|
|
|
|
MCII->getSourceAlign(), nullptr, MemRef::Read);
|
2019-12-19 17:33:36 +01:00
|
|
|
|
|
|
|
// Check that the memcpy arguments don't overlap. The AliasAnalysis API
|
|
|
|
// isn't expressive enough for what we really want to do. Known partial
|
|
|
|
// overlap is not distinguished from the case where nothing is known.
|
|
|
|
const LocationSize LS = LocationSize::precise(Size);
|
2021-03-05 11:58:13 +01:00
|
|
|
Assert(AA->alias(MCII->getSource(), LS, MCII->getDest(), LS) !=
|
|
|
|
AliasResult::MustAlias,
|
2019-12-19 17:33:36 +01:00
|
|
|
"Undefined behavior: memcpy source and destination overlap", &I);
|
|
|
|
break;
|
|
|
|
}
|
2010-04-08 20:47:09 +02:00
|
|
|
case Intrinsic::memmove: {
|
|
|
|
MemMoveInst *MMI = cast<MemMoveInst>(&I);
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForDest(MMI),
|
2020-07-01 16:32:30 +02:00
|
|
|
MMI->getDestAlign(), nullptr, MemRef::Write);
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForSource(MMI),
|
2020-07-01 16:32:30 +02:00
|
|
|
MMI->getSourceAlign(), nullptr, MemRef::Read);
|
2010-04-08 20:47:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::memset: {
|
|
|
|
MemSetInst *MSI = cast<MemSetInst>(&I);
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForDest(MSI),
|
2020-07-01 16:32:30 +02:00
|
|
|
MSI->getDestAlign(), nullptr, MemRef::Write);
|
2010-04-08 20:47:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Intrinsic::vastart:
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(I.getParent()->getParent()->isVarArg(),
|
|
|
|
"Undefined behavior: va_start called in a non-varargs function",
|
|
|
|
&I);
|
2010-04-09 03:39:53 +02:00
|
|
|
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
|
|
|
|
nullptr, MemRef::Read | MemRef::Write);
|
2010-04-08 20:47:09 +02:00
|
|
|
break;
|
|
|
|
case Intrinsic::vacopy:
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
|
|
|
|
nullptr, MemRef::Write);
|
|
|
|
visitMemoryReference(I, MemoryLocation::getForArgument(&I, 1, TLI), None,
|
|
|
|
nullptr, MemRef::Read);
|
2010-04-08 20:47:09 +02:00
|
|
|
break;
|
|
|
|
case Intrinsic::vaend:
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
|
|
|
|
nullptr, MemRef::Read | MemRef::Write);
|
2010-04-08 20:47:09 +02:00
|
|
|
break;
|
2010-05-27 00:21:25 +02:00
|
|
|
|
|
|
|
case Intrinsic::stackrestore:
|
|
|
|
// Stackrestore doesn't read or write memory, but it sets the
|
|
|
|
// stack pointer, which the compiler may read from or write to
|
|
|
|
// at any time, so check it for both readability and writeability.
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
|
|
|
|
nullptr, MemRef::Read | MemRef::Write);
|
2010-05-27 00:21:25 +02:00
|
|
|
break;
|
2020-09-17 09:47:39 +02:00
|
|
|
case Intrinsic::get_active_lane_mask:
|
|
|
|
if (auto *TripCount = dyn_cast<ConstantInt>(I.getArgOperand(1)))
|
|
|
|
Assert(!TripCount->isZero(), "get_active_lane_mask: operand #2 "
|
|
|
|
"must be greater than 0", &I);
|
|
|
|
break;
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitReturnInst(ReturnInst &I) {
|
|
|
|
Function *F = I.getParent()->getParent();
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!F->doesNotReturn(),
|
|
|
|
"Unusual: Return statement in function with noreturn attribute", &I);
|
2010-05-28 06:33:42 +02:00
|
|
|
|
|
|
|
if (Value *V = I.getReturnValue()) {
|
2015-08-06 04:05:46 +02:00
|
|
|
Value *Obj = findValue(V, /*OffsetOk=*/true);
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<AllocaInst>(Obj), "Unusual: Returning alloca value", &I);
|
2010-05-28 06:33:42 +02:00
|
|
|
}
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2010-05-28 23:43:57 +02:00
|
|
|
// TODO: Check that the reference is in bounds.
|
2010-07-06 17:23:00 +02:00
|
|
|
// TODO: Check readnone/readonly function attributes.
|
2020-11-19 20:53:27 +01:00
|
|
|
void Lint::visitMemoryReference(Instruction &I, const MemoryLocation &Loc,
|
2020-07-01 16:32:30 +02:00
|
|
|
MaybeAlign Align, Type *Ty, unsigned Flags) {
|
2010-05-28 23:43:57 +02:00
|
|
|
// If no memory is being referenced, it doesn't matter if the pointer
|
|
|
|
// is valid.
|
2020-11-19 20:53:27 +01:00
|
|
|
if (Loc.Size.isZero())
|
2010-05-28 23:43:57 +02:00
|
|
|
return;
|
|
|
|
|
2020-11-19 20:53:27 +01:00
|
|
|
Value *Ptr = const_cast<Value *>(Loc.Ptr);
|
2015-08-06 04:05:46 +02:00
|
|
|
Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true);
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<ConstantPointerNull>(UnderlyingObject),
|
|
|
|
"Undefined behavior: Null pointer dereference", &I);
|
|
|
|
Assert(!isa<UndefValue>(UnderlyingObject),
|
|
|
|
"Undefined behavior: Undef pointer dereference", &I);
|
|
|
|
Assert(!isa<ConstantInt>(UnderlyingObject) ||
|
2017-07-06 20:39:47 +02:00
|
|
|
!cast<ConstantInt>(UnderlyingObject)->isMinusOne(),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Unusual: All-ones pointer dereference", &I);
|
|
|
|
Assert(!isa<ConstantInt>(UnderlyingObject) ||
|
|
|
|
!cast<ConstantInt>(UnderlyingObject)->isOne(),
|
|
|
|
"Unusual: Address one pointer dereference", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
|
2010-04-30 21:05:00 +02:00
|
|
|
if (Flags & MemRef::Write) {
|
|
|
|
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!GV->isConstant(), "Undefined behavior: Write to read-only memory",
|
|
|
|
&I);
|
|
|
|
Assert(!isa<Function>(UnderlyingObject) &&
|
|
|
|
!isa<BlockAddress>(UnderlyingObject),
|
|
|
|
"Undefined behavior: Write to text section", &I);
|
2010-04-30 21:05:00 +02:00
|
|
|
}
|
|
|
|
if (Flags & MemRef::Read) {
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<Function>(UnderlyingObject), "Unusual: Load from function body",
|
|
|
|
&I);
|
|
|
|
Assert(!isa<BlockAddress>(UnderlyingObject),
|
|
|
|
"Undefined behavior: Load from block address", &I);
|
2010-04-30 21:05:00 +02:00
|
|
|
}
|
|
|
|
if (Flags & MemRef::Callee) {
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<BlockAddress>(UnderlyingObject),
|
|
|
|
"Undefined behavior: Call to block address", &I);
|
2010-04-30 21:05:00 +02:00
|
|
|
}
|
|
|
|
if (Flags & MemRef::Branchee) {
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<Constant>(UnderlyingObject) ||
|
|
|
|
isa<BlockAddress>(UnderlyingObject),
|
|
|
|
"Undefined behavior: Branch to non-blockaddress", &I);
|
2010-04-30 21:05:00 +02:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:45:36 +02:00
|
|
|
// Check for buffer overflows and misalignment.
|
2013-01-31 03:00:45 +01:00
|
|
|
// Only handles memory references that read/write something simple like an
|
|
|
|
// alloca instruction or a global variable.
|
|
|
|
int64_t Offset = 0;
|
2015-08-06 04:05:46 +02:00
|
|
|
if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, *DL)) {
|
2013-01-31 03:00:45 +01:00
|
|
|
// OK, so the access is to a constant offset from Ptr. Check that Ptr is
|
|
|
|
// something we can handle and if so extract the size of this base object
|
|
|
|
// along with its alignment.
|
2015-06-17 09:21:38 +02:00
|
|
|
uint64_t BaseSize = MemoryLocation::UnknownSize;
|
2020-07-01 16:32:30 +02:00
|
|
|
MaybeAlign BaseAlign;
|
2013-01-31 03:00:45 +01:00
|
|
|
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
|
|
|
|
Type *ATy = AI->getAllocatedType();
|
2015-03-10 03:37:25 +01:00
|
|
|
if (!AI->isArrayAllocation() && ATy->isSized())
|
2015-08-06 04:05:46 +02:00
|
|
|
BaseSize = DL->getTypeAllocSize(ATy);
|
2020-07-01 16:32:30 +02:00
|
|
|
BaseAlign = AI->getAlign();
|
2013-01-31 03:00:45 +01:00
|
|
|
} else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
|
|
|
|
// If the global may be defined differently in another compilation unit
|
|
|
|
// then don't warn about funky memory accesses.
|
|
|
|
if (GV->hasDefinitiveInitializer()) {
|
2016-01-16 21:30:46 +01:00
|
|
|
Type *GTy = GV->getValueType();
|
2015-03-10 03:37:25 +01:00
|
|
|
if (GTy->isSized())
|
2015-08-06 04:05:46 +02:00
|
|
|
BaseSize = DL->getTypeAllocSize(GTy);
|
2020-07-01 16:32:30 +02:00
|
|
|
BaseAlign = GV->getAlign();
|
|
|
|
if (!BaseAlign && GTy->isSized())
|
|
|
|
BaseAlign = DL->getABITypeAlign(GTy);
|
2012-09-25 12:00:49 +02:00
|
|
|
}
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
2013-01-31 03:00:45 +01:00
|
|
|
|
|
|
|
// Accesses from before the start or after the end of the object are not
|
|
|
|
// defined.
|
2020-11-19 20:53:27 +01:00
|
|
|
Assert(!Loc.Size.hasValue() || BaseSize == MemoryLocation::UnknownSize ||
|
|
|
|
(Offset >= 0 && Offset + Loc.Size.getValue() <= BaseSize),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: Buffer overflow", &I);
|
2013-01-31 03:00:45 +01:00
|
|
|
|
|
|
|
// Accesses that say that the memory is more aligned than it is are not
|
|
|
|
// defined.
|
2020-07-01 16:32:30 +02:00
|
|
|
if (!Align && Ty && Ty->isSized())
|
|
|
|
Align = DL->getABITypeAlign(Ty);
|
|
|
|
if (BaseAlign && Align)
|
|
|
|
Assert(*Align <= commonAlignment(*BaseAlign, Offset),
|
|
|
|
"Undefined behavior: Memory reference address is misaligned", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitLoadInst(LoadInst &I) {
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::get(&I), I.getAlign(), I.getType(),
|
|
|
|
MemRef::Read);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitStoreInst(StoreInst &I) {
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::get(&I), I.getAlign(),
|
|
|
|
I.getOperand(0)->getType(), MemRef::Write);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2010-04-09 03:39:53 +02:00
|
|
|
void Lint::visitXor(BinaryOperator &I) {
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
|
|
|
|
"Undefined result: xor(undef, undef)", &I);
|
2010-04-09 03:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitSub(BinaryOperator &I) {
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
|
|
|
|
"Undefined result: sub(undef, undef)", &I);
|
2010-04-09 03:39:53 +02:00
|
|
|
}
|
|
|
|
|
2010-04-09 01:05:57 +02:00
|
|
|
void Lint::visitLShr(BinaryOperator &I) {
|
2015-08-06 04:05:46 +02:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(1),
|
|
|
|
/*OffsetOk=*/false)))
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
|
|
|
|
"Undefined result: Shift count out of range", &I);
|
2010-04-09 01:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitAShr(BinaryOperator &I) {
|
2015-08-06 04:05:46 +02:00
|
|
|
if (ConstantInt *CI =
|
|
|
|
dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
|
|
|
|
"Undefined result: Shift count out of range", &I);
|
2010-04-09 01:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitShl(BinaryOperator &I) {
|
2015-08-06 04:05:46 +02:00
|
|
|
if (ConstantInt *CI =
|
|
|
|
dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
|
|
|
|
"Undefined result: Shift count out of range", &I);
|
2010-04-09 01:05:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 09:22:17 +01:00
|
|
|
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT,
|
|
|
|
AssumptionCache *AC) {
|
2010-04-09 03:39:53 +02:00
|
|
|
// Assume undef could be zero.
|
2013-08-27 01:29:33 +02:00
|
|
|
if (isa<UndefValue>(V))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
VectorType *VecTy = dyn_cast<VectorType>(V->getType());
|
|
|
|
if (!VecTy) {
|
2020-09-03 06:54:27 +02:00
|
|
|
KnownBits Known =
|
|
|
|
computeKnownBits(V, DL, 0, AC, dyn_cast<Instruction>(V), DT);
|
2017-05-05 19:36:09 +02:00
|
|
|
return Known.isZero();
|
2013-08-27 01:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Per-component check doesn't work with zeroinitializer
|
|
|
|
Constant *C = dyn_cast<Constant>(V);
|
|
|
|
if (!C)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (C->isZeroValue())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// For a vector, KnownZero will only be true if all values are zero, so check
|
|
|
|
// this per component
|
2020-07-22 23:36:48 +02:00
|
|
|
for (unsigned I = 0, N = cast<FixedVectorType>(VecTy)->getNumElements();
|
|
|
|
I != N; ++I) {
|
2013-08-27 01:29:33 +02:00
|
|
|
Constant *Elem = C->getAggregateElement(I);
|
|
|
|
if (isa<UndefValue>(Elem))
|
|
|
|
return true;
|
|
|
|
|
2017-05-24 18:53:07 +02:00
|
|
|
KnownBits Known = computeKnownBits(Elem, DL);
|
2017-05-05 19:36:09 +02:00
|
|
|
if (Known.isZero())
|
2013-08-27 01:29:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
2010-04-09 03:39:53 +02:00
|
|
|
|
2013-08-27 01:29:33 +02:00
|
|
|
return false;
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitSDiv(BinaryOperator &I) {
|
2016-12-19 09:22:17 +01:00
|
|
|
Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: Division by zero", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitUDiv(BinaryOperator &I) {
|
2016-12-19 09:22:17 +01:00
|
|
|
Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: Division by zero", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitSRem(BinaryOperator &I) {
|
2016-12-19 09:22:17 +01:00
|
|
|
Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: Division by zero", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitURem(BinaryOperator &I) {
|
2016-12-19 09:22:17 +01:00
|
|
|
Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined behavior: Division by zero", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitAllocaInst(AllocaInst &I) {
|
|
|
|
if (isa<ConstantInt>(I.getArraySize()))
|
|
|
|
// This isn't undefined behavior, it's just an obvious pessimization.
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(&I.getParent()->getParent()->getEntryBlock() == I.getParent(),
|
|
|
|
"Pessimization: Static alloca outside of entry block", &I);
|
2010-07-06 17:23:00 +02:00
|
|
|
|
|
|
|
// TODO: Check for an unusual size (MSB set?)
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitVAArgInst(VAArgInst &I) {
|
2020-11-19 20:53:27 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::get(&I), None, nullptr,
|
|
|
|
MemRef::Read | MemRef::Write);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitIndirectBrInst(IndirectBrInst &I) {
|
2020-11-17 20:11:09 +01:00
|
|
|
visitMemoryReference(I, MemoryLocation::getAfter(I.getAddress()), None,
|
|
|
|
nullptr, MemRef::Branchee);
|
2010-08-03 01:06:43 +02:00
|
|
|
|
2015-03-07 22:15:40 +01:00
|
|
|
Assert(I.getNumDestinations() != 0,
|
|
|
|
"Undefined behavior: indirectbr with no destinations", &I);
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2010-04-09 01:05:57 +02:00
|
|
|
void Lint::visitExtractElementInst(ExtractElementInst &I) {
|
2015-08-06 04:05:46 +02:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getIndexOperand(),
|
|
|
|
/*OffsetOk=*/false)))
|
2020-07-22 23:36:48 +02:00
|
|
|
Assert(
|
|
|
|
CI->getValue().ult(
|
|
|
|
cast<FixedVectorType>(I.getVectorOperandType())->getNumElements()),
|
|
|
|
"Undefined result: extractelement index out of range", &I);
|
2010-04-09 01:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitInsertElementInst(InsertElementInst &I) {
|
2015-08-06 04:05:46 +02:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(2),
|
|
|
|
/*OffsetOk=*/false)))
|
2020-07-22 23:36:48 +02:00
|
|
|
Assert(CI->getValue().ult(
|
|
|
|
cast<FixedVectorType>(I.getType())->getNumElements()),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Undefined result: insertelement index out of range", &I);
|
2010-04-09 03:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lint::visitUnreachableInst(UnreachableInst &I) {
|
|
|
|
// This isn't undefined behavior, it's merely suspicious.
|
Analysis: Remove implicit ilist iterator conversions
Remove implicit ilist iterator conversions from LLVMAnalysis.
I came across something really scary in `llvm::isKnownNotFullPoison()`
which relied on `Instruction::getNextNode()` being completely broken
(not surprising, but scary nevertheless). This function is documented
(and coded to) return `nullptr` when it gets to the sentinel, but with
an `ilist_half_node` as a sentinel, the sentinel check looks into some
other memory and we don't recognize we've hit the end.
Rooting out these scary cases is the reason I'm removing the implicit
conversions before doing anything else with `ilist`; I'm not at all
surprised that clients rely on badness.
I found another scary case -- this time, not relying on badness, just
bad (but I guess getting lucky so far) -- in
`ObjectSizeOffsetEvaluator::compute_()`. Here, we save out the
insertion point, do some things, and then restore it. Previously, we
let the iterator auto-convert to `Instruction*`, and then set it back
using the `Instruction*` version:
Instruction *PrevInsertPoint = Builder.GetInsertPoint();
/* Logic that may change insert point */
if (PrevInsertPoint)
Builder.SetInsertPoint(PrevInsertPoint);
The check for `PrevInsertPoint` doesn't protect correctly against bad
accesses. If the insertion point has been set to the end of a basic
block (i.e., `SetInsertPoint(SomeBB)`), then `GetInsertPoint()` returns
an iterator pointing at the list sentinel. The version of
`SetInsertPoint()` that's getting called will then call
`PrevInsertPoint->getParent()`, which explodes horribly. The only
reason this hasn't blown up is that it's fairly unlikely the builder is
adding to the end of the block; usually, we're adding instructions
somewhere before the terminator.
llvm-svn: 249925
2015-10-10 02:53:03 +02:00
|
|
|
Assert(&I == &I.getParent()->front() ||
|
|
|
|
std::prev(I.getIterator())->mayHaveSideEffects(),
|
2015-03-07 22:15:40 +01:00
|
|
|
"Unusual: unreachable immediately preceded by instruction without "
|
|
|
|
"side effects",
|
|
|
|
&I);
|
2010-04-09 01:05:57 +02:00
|
|
|
}
|
|
|
|
|
2010-05-28 18:21:24 +02:00
|
|
|
/// findValue - Look through bitcasts and simple memory reference patterns
|
|
|
|
/// to identify an equivalent, but more informative, value. If OffsetOk
|
|
|
|
/// is true, look through getelementptrs with non-zero offsets too.
|
|
|
|
///
|
|
|
|
/// Most analysis passes don't require this logic, because instcombine
|
|
|
|
/// will simplify most of these kinds of things away. But it's a goal of
|
|
|
|
/// this Lint pass to be useful even on non-optimized IR.
|
2015-08-06 04:05:46 +02:00
|
|
|
Value *Lint::findValue(Value *V, bool OffsetOk) const {
|
2010-05-28 18:45:33 +02:00
|
|
|
SmallPtrSet<Value *, 4> Visited;
|
2015-08-06 04:05:46 +02:00
|
|
|
return findValueImpl(V, OffsetOk, Visited);
|
2010-05-28 18:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// findValueImpl - Implementation helper for findValue.
|
2015-08-06 04:05:46 +02:00
|
|
|
Value *Lint::findValueImpl(Value *V, bool OffsetOk,
|
2014-08-21 07:55:13 +02:00
|
|
|
SmallPtrSetImpl<Value *> &Visited) const {
|
2010-05-28 18:45:33 +02:00
|
|
|
// Detect self-referential values.
|
2014-11-19 08:49:26 +01:00
|
|
|
if (!Visited.insert(V).second)
|
2010-05-28 18:45:33 +02:00
|
|
|
return UndefValue::get(V->getType());
|
|
|
|
|
2010-05-28 18:21:24 +02:00
|
|
|
// TODO: Look through sext or zext cast, when the result is known to
|
|
|
|
// be interpreted as signed or unsigned, respectively.
|
2010-05-28 23:43:57 +02:00
|
|
|
// TODO: Look through eliminable cast pairs.
|
2010-05-28 18:21:24 +02:00
|
|
|
// TODO: Look through calls with unique return values.
|
|
|
|
// TODO: Look through vector insert/extract/shuffle.
|
2020-07-31 11:09:54 +02:00
|
|
|
V = OffsetOk ? getUnderlyingObject(V) : V->stripPointerCasts();
|
2010-05-28 18:21:24 +02:00
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(V)) {
|
Analysis: Remove implicit ilist iterator conversions
Remove implicit ilist iterator conversions from LLVMAnalysis.
I came across something really scary in `llvm::isKnownNotFullPoison()`
which relied on `Instruction::getNextNode()` being completely broken
(not surprising, but scary nevertheless). This function is documented
(and coded to) return `nullptr` when it gets to the sentinel, but with
an `ilist_half_node` as a sentinel, the sentinel check looks into some
other memory and we don't recognize we've hit the end.
Rooting out these scary cases is the reason I'm removing the implicit
conversions before doing anything else with `ilist`; I'm not at all
surprised that clients rely on badness.
I found another scary case -- this time, not relying on badness, just
bad (but I guess getting lucky so far) -- in
`ObjectSizeOffsetEvaluator::compute_()`. Here, we save out the
insertion point, do some things, and then restore it. Previously, we
let the iterator auto-convert to `Instruction*`, and then set it back
using the `Instruction*` version:
Instruction *PrevInsertPoint = Builder.GetInsertPoint();
/* Logic that may change insert point */
if (PrevInsertPoint)
Builder.SetInsertPoint(PrevInsertPoint);
The check for `PrevInsertPoint` doesn't protect correctly against bad
accesses. If the insertion point has been set to the end of a basic
block (i.e., `SetInsertPoint(SomeBB)`), then `GetInsertPoint()` returns
an iterator pointing at the list sentinel. The version of
`SetInsertPoint()` that's getting called will then call
`PrevInsertPoint->getParent()`, which explodes horribly. The only
reason this hasn't blown up is that it's fairly unlikely the builder is
adding to the end of the block; usually, we're adding instructions
somewhere before the terminator.
llvm-svn: 249925
2015-10-10 02:53:03 +02:00
|
|
|
BasicBlock::iterator BBI = L->getIterator();
|
2010-05-28 18:21:24 +02:00
|
|
|
BasicBlock *BB = L->getParent();
|
2010-05-28 19:44:00 +02:00
|
|
|
SmallPtrSet<BasicBlock *, 4> VisitedBlocks;
|
2010-05-28 18:21:24 +02:00
|
|
|
for (;;) {
|
2014-11-19 08:49:26 +01:00
|
|
|
if (!VisitedBlocks.insert(BB).second)
|
|
|
|
break;
|
2015-09-18 21:14:35 +02:00
|
|
|
if (Value *U =
|
2020-09-03 06:54:27 +02:00
|
|
|
FindAvailableLoadedValue(L, BB, BBI, DefMaxInstsToScan, AA))
|
2015-08-06 04:05:46 +02:00
|
|
|
return findValueImpl(U, OffsetOk, Visited);
|
2020-09-03 06:54:27 +02:00
|
|
|
if (BBI != BB->begin())
|
|
|
|
break;
|
2010-05-28 19:44:00 +02:00
|
|
|
BB = BB->getUniquePredecessor();
|
2020-09-03 06:54:27 +02:00
|
|
|
if (!BB)
|
|
|
|
break;
|
2010-05-28 18:21:24 +02:00
|
|
|
BBI = BB->end();
|
|
|
|
}
|
2010-05-28 23:43:57 +02:00
|
|
|
} else if (PHINode *PN = dyn_cast<PHINode>(V)) {
|
2010-11-17 05:30:22 +01:00
|
|
|
if (Value *W = PN->hasConstantValue())
|
2020-05-19 13:30:26 +02:00
|
|
|
return findValueImpl(W, OffsetOk, Visited);
|
2010-05-28 18:21:24 +02:00
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(V)) {
|
2015-08-06 04:05:46 +02:00
|
|
|
if (CI->isNoopCast(*DL))
|
|
|
|
return findValueImpl(CI->getOperand(0), OffsetOk, Visited);
|
2010-05-28 18:21:24 +02:00
|
|
|
} else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) {
|
2020-09-03 06:54:27 +02:00
|
|
|
if (Value *W =
|
|
|
|
FindInsertedValue(Ex->getAggregateOperand(), Ex->getIndices()))
|
2010-05-28 18:21:24 +02:00
|
|
|
if (W != V)
|
2015-08-06 04:05:46 +02:00
|
|
|
return findValueImpl(W, OffsetOk, Visited);
|
2010-05-28 23:43:57 +02:00
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
|
|
|
|
// Same as above, but for ConstantExpr instead of Instruction.
|
|
|
|
if (Instruction::isCast(CE->getOpcode())) {
|
|
|
|
if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
|
2015-03-10 03:37:25 +01:00
|
|
|
CE->getOperand(0)->getType(), CE->getType(),
|
2017-10-03 08:03:49 +02:00
|
|
|
*DL))
|
2015-08-06 04:05:46 +02:00
|
|
|
return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
|
2010-05-28 23:43:57 +02:00
|
|
|
} else if (CE->getOpcode() == Instruction::ExtractValue) {
|
2011-04-13 17:22:40 +02:00
|
|
|
ArrayRef<unsigned> Indices = CE->getIndices();
|
2011-07-13 12:26:04 +02:00
|
|
|
if (Value *W = FindInsertedValue(CE->getOperand(0), Indices))
|
2010-05-28 23:43:57 +02:00
|
|
|
if (W != V)
|
2015-08-06 04:05:46 +02:00
|
|
|
return findValueImpl(W, OffsetOk, Visited);
|
2010-05-28 23:43:57 +02:00
|
|
|
}
|
2010-05-28 18:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// As a last resort, try SimplifyInstruction or constant folding.
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(V)) {
|
2017-04-28 21:55:38 +02:00
|
|
|
if (Value *W = SimplifyInstruction(Inst, {*DL, TLI, DT, AC}))
|
2015-08-06 04:05:46 +02:00
|
|
|
return findValueImpl(W, OffsetOk, Visited);
|
2016-07-29 05:27:26 +02:00
|
|
|
} else if (auto *C = dyn_cast<Constant>(V)) {
|
2020-03-03 19:16:11 +01:00
|
|
|
Value *W = ConstantFoldConstant(C, *DL, TLI);
|
|
|
|
if (W != V)
|
|
|
|
return findValueImpl(W, OffsetOk, Visited);
|
2010-05-28 18:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2020-09-03 06:54:27 +02:00
|
|
|
PreservedAnalyses LintPass::run(Function &F, FunctionAnalysisManager &AM) {
|
|
|
|
auto *Mod = F.getParent();
|
|
|
|
auto *DL = &F.getParent()->getDataLayout();
|
|
|
|
auto *AA = &AM.getResult<AAManager>(F);
|
|
|
|
auto *AC = &AM.getResult<AssumptionAnalysis>(F);
|
|
|
|
auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
|
|
|
|
Lint L(Mod, DL, AA, AC, DT, TLI);
|
|
|
|
L.visit(F);
|
|
|
|
dbgs() << L.MessagesStr.str();
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
class LintLegacyPass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
LintLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeLintLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
}
|
|
|
|
void print(raw_ostream &O, const Module *M) const override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
char LintLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LintLegacyPass, "lint", "Statically lint-checks LLVM IR",
|
|
|
|
false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(LintLegacyPass, "lint", "Statically lint-checks LLVM IR",
|
|
|
|
false, true)
|
|
|
|
|
|
|
|
bool LintLegacyPass::runOnFunction(Function &F) {
|
|
|
|
auto *Mod = F.getParent();
|
|
|
|
auto *DL = &F.getParent()->getDataLayout();
|
|
|
|
auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
|
|
|
auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
|
|
|
auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
|
|
|
Lint L(Mod, DL, AA, AC, DT, TLI);
|
|
|
|
L.visit(F);
|
|
|
|
dbgs() << L.MessagesStr.str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-08 20:47:09 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implement the public interfaces to this file...
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-03 06:54:27 +02:00
|
|
|
FunctionPass *llvm::createLintLegacyPassPass() { return new LintLegacyPass(); }
|
2010-04-08 20:47:09 +02:00
|
|
|
|
|
|
|
/// lintFunction - Check a function for errors, printing messages on stderr.
|
|
|
|
///
|
|
|
|
void llvm::lintFunction(const Function &f) {
|
2020-09-03 06:54:27 +02:00
|
|
|
Function &F = const_cast<Function &>(f);
|
2010-04-08 20:47:09 +02:00
|
|
|
assert(!F.isDeclaration() && "Cannot lint external functions");
|
|
|
|
|
2015-02-13 11:01:29 +01:00
|
|
|
legacy::FunctionPassManager FPM(F.getParent());
|
2020-09-03 06:54:27 +02:00
|
|
|
auto *V = new LintLegacyPass();
|
2010-04-08 20:47:09 +02:00
|
|
|
FPM.add(V);
|
|
|
|
FPM.run(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// lintModule - Check a module for errors, printing messages on stderr.
|
|
|
|
///
|
2010-05-27 00:28:53 +02:00
|
|
|
void llvm::lintModule(const Module &M) {
|
2015-02-13 11:01:29 +01:00
|
|
|
legacy::PassManager PM;
|
2020-09-03 06:54:27 +02:00
|
|
|
auto *V = new LintLegacyPass();
|
2010-04-08 20:47:09 +02:00
|
|
|
PM.add(V);
|
2020-09-03 06:54:27 +02:00
|
|
|
PM.run(const_cast<Module &>(M));
|
2010-04-08 20:47:09 +02:00
|
|
|
}
|