From 5e561bbd5dcedf5338e939ef8ead1eadd797240c Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Sun, 30 Sep 2012 07:30:10 +0000 Subject: [PATCH] Ignore apparent buffer overruns on external or weak globals. This is a major source of false positives due to globals being declared in a header with some kind of incomplete (small) type, but the actual definition being bigger. llvm-svn: 164912 --- llvm/lib/Analysis/Lint.cpp | 18 +++++++++++------- llvm/test/Other/lint.ll | 5 +++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Analysis/Lint.cpp b/llvm/lib/Analysis/Lint.cpp index 9258aee..7bd9457 100644 --- a/llvm/lib/Analysis/Lint.cpp +++ b/llvm/lib/Analysis/Lint.cpp @@ -430,13 +430,17 @@ void Lint::visitMemoryReference(Instruction &I, BaseAlign = AI->getAlignment(); if (BaseAlign == 0 && ATy->isSized()) BaseAlign = TD->getABITypeAlignment(ATy); - } else if (GlobalValue *GV = dyn_cast(Base)) { - Type *GTy = GV->getType()->getElementType(); - if (GTy->isSized()) - BaseSize = TD->getTypeAllocSize(GTy); - BaseAlign = GV->getAlignment(); - if (BaseAlign == 0 && GTy->isSized()) - BaseAlign = TD->getABITypeAlignment(GTy); + } else if (GlobalVariable *GV = dyn_cast(Base)) { + // If the global may be defined differently in another compilation unit + // then don't warn about funky memory accesses. + if (GV->hasDefinitiveInitializer()) { + Type *GTy = GV->getType()->getElementType(); + if (GTy->isSized()) + BaseSize = TD->getTypeAllocSize(GTy); + BaseAlign = GV->getAlignment(); + if (BaseAlign == 0 && GTy->isSized()) + BaseAlign = TD->getABITypeAlignment(GTy); + } } // Accesses from before the start or after the end of the object are not diff --git a/llvm/test/Other/lint.ll b/llvm/test/Other/lint.ll index d3ab988..78bbbe9 100644 --- a/llvm/test/Other/lint.ll +++ b/llvm/test/Other/lint.ll @@ -9,6 +9,7 @@ declare void @has_noaliases(i32* noalias %p, i32* %q) declare void @one_arg(i32) @CG = constant i32 7 +@E = external global i8 define i32 @foo() noreturn { %buf = alloca i8 @@ -100,6 +101,10 @@ next: ret i32 0 foo: +; CHECK-NOT: Undefined behavior: Buffer overflow +; CHECK-NOT: Memory reference address is misaligned + %e = bitcast i8* @E to i64* + store i64 0, i64* %e %z = add i32 0, 0 ; CHECK: unreachable immediately preceded by instruction without side effects unreachable -- 2.7.4