summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Analysis/MemoryBuiltinsTest.cpp
diff options
context:
space:
mode:
authorGeorge Burgess IV <george.burgess.iv@gmail.com>2016-12-23 01:18:09 +0000
committerGeorge Burgess IV <george.burgess.iv@gmail.com>2016-12-23 01:18:09 +0000
commitccae43a247b0791f78ea89b9cb7e59fa70f5000d (patch)
tree5759948276c77ef8d18533abeee0bd0aab144de9 /llvm/unittests/Analysis/MemoryBuiltinsTest.cpp
parent2e97554245a4a5a055c2846cc8d93dffb48101f6 (diff)
downloadbcm5719-llvm-ccae43a247b0791f78ea89b9cb7e59fa70f5000d.tar.gz
bcm5719-llvm-ccae43a247b0791f78ea89b9cb7e59fa70f5000d.zip
Don't consider allocsize functions to be allocation functions.
This patch fixes some ASAN unittest failures on FreeBSD. See the cfe-commits email thread for r290169 for more on those. According to the LangRef, the allocsize attribute only tells us about the number of bytes that exist at the memory location pointed to by the return value of a function. It does not necessarily mean that the function will only ever allocate. So, we need to be very careful about treating functions with allocsize as general allocation functions. This patch makes us fully conservative in this regard, though I suspect that we have room to be a bit more aggressive if we want. This has a FIXME that can be fixed by a relatively straightforward refactor; I just wanted to keep this patch minimal. If this sticks, I'll come back and fix it in a few days. llvm-svn: 290397
Diffstat (limited to 'llvm/unittests/Analysis/MemoryBuiltinsTest.cpp')
-rw-r--r--llvm/unittests/Analysis/MemoryBuiltinsTest.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/unittests/Analysis/MemoryBuiltinsTest.cpp b/llvm/unittests/Analysis/MemoryBuiltinsTest.cpp
new file mode 100644
index 00000000000..898ebeece4f
--- /dev/null
+++ b/llvm/unittests/Analysis/MemoryBuiltinsTest.cpp
@@ -0,0 +1,50 @@
+//===- MemoryBuiltinsTest.cpp - Tests for utilities in MemoryBuiltins.h ---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/MemoryBuiltins.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+// allocsize should not imply that a function is a traditional allocation
+// function (e.g. that can be optimized out/...); it just tells us how many
+// bytes exist at the pointer handed back by the function.
+TEST(AllocSize, AllocationBuiltinsTest) {
+ LLVMContext Context;
+ Module M("", Context);
+ IntegerType *ArgTy = Type::getInt32Ty(Context);
+
+ Function *AllocSizeFn = Function::Create(
+ FunctionType::get(Type::getInt8PtrTy(Context), {ArgTy}, false),
+ GlobalValue::ExternalLinkage, "F", &M);
+
+ AllocSizeFn->addFnAttr(Attribute::getWithAllocSizeArgs(Context, 1, None));
+
+ // 100 is arbitrary.
+ std::unique_ptr<CallInst> Caller(
+ CallInst::Create(AllocSizeFn, {ConstantInt::get(ArgTy, 100)}));
+
+ const TargetLibraryInfo *TLI = nullptr;
+ EXPECT_FALSE(isNoAliasFn(Caller.get(), TLI));
+ EXPECT_FALSE(isMallocLikeFn(Caller.get(), TLI));
+ EXPECT_FALSE(isCallocLikeFn(Caller.get(), TLI));
+ EXPECT_FALSE(isAllocLikeFn(Caller.get(), TLI));
+
+ // FIXME: We might be able to treat allocsize functions as general allocation
+ // functions. For the moment, being conservative seems better (and we'd have
+ // to plumb stuff around `isNoAliasFn`).
+ EXPECT_FALSE(isAllocationFn(Caller.get(), TLI));
+}
+}
OpenPOWER on IntegriCloud