Bug Summary

File:unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
Location:line 104, column 22
Description:Called function pointer is null (null dereference)

Annotated Source Code

1//===- MCJITObjectCacheTest.cpp - Unit tests for MCJIT object caching -----===//
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#include "MCJITTestBase.h"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringSet.h"
14#include "llvm/ExecutionEngine/MCJIT.h"
15#include "llvm/ExecutionEngine/ObjectCache.h"
16#include "llvm/ExecutionEngine/SectionMemoryManager.h"
17#include "gtest/gtest.h"
18
19using namespace llvm;
20
21namespace {
22
23class TestObjectCache : public ObjectCache {
24public:
25 TestObjectCache() : DuplicateInserted(false) { }
26
27 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
28 // If we've seen this module before, note that.
29 const std::string ModuleID = M->getModuleIdentifier();
30 if (ObjMap.find(ModuleID) != ObjMap.end())
31 DuplicateInserted = true;
32 // Store a copy of the buffer in our map.
33 ObjMap[ModuleID] = copyBuffer(Obj);
34 }
35
36 std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {
37 const MemoryBuffer* BufferFound = getObjectInternal(M);
38 ModulesLookedUp.insert(M->getModuleIdentifier());
39 if (!BufferFound)
40 return nullptr;
41 // Our test cache wants to maintain ownership of its object buffers
42 // so we make a copy here for the execution engine.
43 return MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer());
44 }
45
46 // Test-harness-specific functions
47 bool wereDuplicatesInserted() { return DuplicateInserted; }
48
49 bool wasModuleLookedUp(const Module *M) {
50 return ModulesLookedUp.find(M->getModuleIdentifier())
51 != ModulesLookedUp.end();
52 }
53
54 const MemoryBuffer* getObjectInternal(const Module* M) {
55 // Look for the module in our map.
56 const std::string ModuleID = M->getModuleIdentifier();
57 StringMap<const MemoryBuffer *>::iterator it = ObjMap.find(ModuleID);
58 if (it == ObjMap.end())
59 return nullptr;
60 return it->second;
61 }
62
63private:
64 MemoryBuffer *copyBuffer(MemoryBufferRef Buf) {
65 // Create a local copy of the buffer.
66 std::unique_ptr<MemoryBuffer> NewBuffer =
67 MemoryBuffer::getMemBufferCopy(Buf.getBuffer());
68 MemoryBuffer *Ret = NewBuffer.get();
69 AllocatedBuffers.push_back(std::move(NewBuffer));
70 return Ret;
71 }
72
73 StringMap<const MemoryBuffer *> ObjMap;
74 StringSet<> ModulesLookedUp;
75 SmallVector<std::unique_ptr<MemoryBuffer>, 2> AllocatedBuffers;
76 bool DuplicateInserted;
77};
78
79class MCJITObjectCacheTest : public testing::Test, public MCJITTestBase {
80protected:
81 enum {
82 OriginalRC = 6,
83 ReplacementRC = 7
84 };
85
86 void SetUp() override {
87 M.reset(createEmptyModule("<main>"));
88 Main = insertMainFunction(M.get(), OriginalRC);
89 }
90
91 void compileAndRun(int ExpectedRC = OriginalRC) {
92 // This function shouldn't be called until after SetUp.
93 ASSERT_TRUE(bool(TheJIT))switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(bool(TheJIT))) ; else
return ::testing::internal::AssertHelper(::testing::TestPartResult
::kFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 93, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "bool(TheJIT)", "false", "true").c_str()) = ::testing::Message
()
;
94 ASSERT_TRUE(nullptr != Main)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(nullptr != Main)) ; else
return ::testing::internal::AssertHelper(::testing::TestPartResult
::kFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 94, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "nullptr != Main", "false", "true").c_str()) = ::testing::Message
()
;
95
96 // We may be using a null cache, so ensure compilation is valid.
97 TheJIT->finalizeObject();
98 void *vPtr = TheJIT->getPointerToFunction(Main);
2
'vPtr' initialized here
99
100 EXPECT_TRUE(nullptr != vPtr)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(nullptr != vPtr)) ; else
::testing::internal::AssertHelper(::testing::TestPartResult::
kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 100, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "nullptr != vPtr", "false", "true").c_str()) = ::testing::Message
()
3
Within the expansion of the macro 'EXPECT_TRUE':
a
Assuming pointer value is null
101 << "Unable to get pointer to main() from JIT";
102
103 int (*FuncPtr)() = (int(*)())(intptr_t)vPtr;
4
'FuncPtr' initialized to a null pointer value
104 int returnCode = FuncPtr();
5
Called function pointer is null (null dereference)
105 EXPECT_EQ(returnCode, ExpectedRC)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing
::internal::IsNullLiteralHelper(returnCode)) == 1)>::Compare
("returnCode", "ExpectedRC", returnCode, ExpectedRC))) ; else
::testing::internal::AssertHelper(::testing::TestPartResult::
kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 105, gtest_ar.failure_message()) = ::testing::Message()
;
106 }
107
108 Function *Main;
109};
110
111TEST_F(MCJITObjectCacheTest, SetNullObjectCache)class MCJITObjectCacheTest_SetNullObjectCache_Test : public MCJITObjectCacheTest
{ public: MCJITObjectCacheTest_SetNullObjectCache_Test() {} private
: virtual void TestBody(); static ::testing::TestInfo* const test_info_
__attribute__ ((unused)); MCJITObjectCacheTest_SetNullObjectCache_Test
(MCJITObjectCacheTest_SetNullObjectCache_Test const &); void
operator=(MCJITObjectCacheTest_SetNullObjectCache_Test const
&);};::testing::TestInfo* const MCJITObjectCacheTest_SetNullObjectCache_Test
::test_info_ = ::testing::internal::MakeAndRegisterTestInfo(
"MCJITObjectCacheTest", "SetNullObjectCache", __null, __null
, (::testing::internal::GetTypeId<MCJITObjectCacheTest>
()), MCJITObjectCacheTest::SetUpTestCase, MCJITObjectCacheTest
::TearDownTestCase, new ::testing::internal::TestFactoryImpl<
MCJITObjectCacheTest_SetNullObjectCache_Test>);void MCJITObjectCacheTest_SetNullObjectCache_Test
::TestBody()
{
112 SKIP_UNSUPPORTED_PLATFORMdo if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) return; while
(0)
;
113
114 createJIT(std::move(M));
115
116 TheJIT->setObjectCache(nullptr);
117
118 compileAndRun();
119}
120
121TEST_F(MCJITObjectCacheTest, VerifyBasicObjectCaching)class MCJITObjectCacheTest_VerifyBasicObjectCaching_Test : public
MCJITObjectCacheTest { public: MCJITObjectCacheTest_VerifyBasicObjectCaching_Test
() {} private: virtual void TestBody(); static ::testing::TestInfo
* const test_info_ __attribute__ ((unused)); MCJITObjectCacheTest_VerifyBasicObjectCaching_Test
(MCJITObjectCacheTest_VerifyBasicObjectCaching_Test const &
); void operator=(MCJITObjectCacheTest_VerifyBasicObjectCaching_Test
const &);};::testing::TestInfo* const MCJITObjectCacheTest_VerifyBasicObjectCaching_Test
::test_info_ = ::testing::internal::MakeAndRegisterTestInfo(
"MCJITObjectCacheTest", "VerifyBasicObjectCaching", __null, __null
, (::testing::internal::GetTypeId<MCJITObjectCacheTest>
()), MCJITObjectCacheTest::SetUpTestCase, MCJITObjectCacheTest
::TearDownTestCase, new ::testing::internal::TestFactoryImpl<
MCJITObjectCacheTest_VerifyBasicObjectCaching_Test>);void
MCJITObjectCacheTest_VerifyBasicObjectCaching_Test::TestBody
()
{
122 SKIP_UNSUPPORTED_PLATFORMdo if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) return; while
(0)
;
123
124 std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
125
126 // Save a copy of the module pointer before handing it off to MCJIT.
127 const Module * SavedModulePointer = M.get();
128
129 createJIT(std::move(M));
130
131 TheJIT->setObjectCache(Cache.get());
132
133 // Verify that our object cache does not contain the module yet.
134 const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SavedModulePointer);
135 EXPECT_EQ(nullptr, ObjBuffer)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing
::internal::IsNullLiteralHelper(nullptr)) == 1)>::Compare(
"nullptr", "ObjBuffer", nullptr, ObjBuffer))) ; else ::testing
::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 135, gtest_ar.failure_message()) = ::testing::Message()
;
136
137 compileAndRun();
138
139 // Verify that MCJIT tried to look-up this module in the cache.
140 EXPECT_TRUE(Cache->wasModuleLookedUp(SavedModulePointer))switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(Cache->wasModuleLookedUp
(SavedModulePointer))) ; else ::testing::internal::AssertHelper
(::testing::TestPartResult::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 140, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "Cache->wasModuleLookedUp(SavedModulePointer)", "false",
"true").c_str()) = ::testing::Message()
;
141
142 // Verify that our object cache now contains the module.
143 ObjBuffer = Cache->getObjectInternal(SavedModulePointer);
144 EXPECT_TRUE(nullptr != ObjBuffer)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(nullptr != ObjBuffer)
) ; else ::testing::internal::AssertHelper(::testing::TestPartResult
::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 144, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "nullptr != ObjBuffer", "false", "true").c_str()) = ::testing
::Message()
;
145
146 // Verify that the cache was only notified once.
147 EXPECT_FALSE(Cache->wereDuplicatesInserted())switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(!(Cache->wereDuplicatesInserted
()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult
::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 147, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "Cache->wereDuplicatesInserted()", "true", "false").c_str
()) = ::testing::Message()
;
148}
149
150TEST_F(MCJITObjectCacheTest, VerifyLoadFromCache)class MCJITObjectCacheTest_VerifyLoadFromCache_Test : public MCJITObjectCacheTest
{ public: MCJITObjectCacheTest_VerifyLoadFromCache_Test() {}
private: virtual void TestBody(); static ::testing::TestInfo
* const test_info_ __attribute__ ((unused)); MCJITObjectCacheTest_VerifyLoadFromCache_Test
(MCJITObjectCacheTest_VerifyLoadFromCache_Test const &); void
operator=(MCJITObjectCacheTest_VerifyLoadFromCache_Test const
&);};::testing::TestInfo* const MCJITObjectCacheTest_VerifyLoadFromCache_Test
::test_info_ = ::testing::internal::MakeAndRegisterTestInfo(
"MCJITObjectCacheTest", "VerifyLoadFromCache", __null, __null
, (::testing::internal::GetTypeId<MCJITObjectCacheTest>
()), MCJITObjectCacheTest::SetUpTestCase, MCJITObjectCacheTest
::TearDownTestCase, new ::testing::internal::TestFactoryImpl<
MCJITObjectCacheTest_VerifyLoadFromCache_Test>);void MCJITObjectCacheTest_VerifyLoadFromCache_Test
::TestBody()
{
151 SKIP_UNSUPPORTED_PLATFORMdo if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) return; while
(0)
;
152
153 std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
154
155 // Compile this module with an MCJIT engine
156 createJIT(std::move(M));
157 TheJIT->setObjectCache(Cache.get());
158 TheJIT->finalizeObject();
159
160 // Destroy the MCJIT engine we just used
161 TheJIT.reset();
162
163 // Create a new memory manager.
164 MM.reset(new SectionMemoryManager());
165
166 // Create a new module and save it. Use a different return code so we can
167 // tell if MCJIT compiled this module or used the cache.
168 M.reset(createEmptyModule("<main>"));
169 Main = insertMainFunction(M.get(), ReplacementRC);
170 const Module * SecondModulePointer = M.get();
171
172 // Create a new MCJIT instance to load this module then execute it.
173 createJIT(std::move(M));
174 TheJIT->setObjectCache(Cache.get());
175 compileAndRun();
176
177 // Verify that MCJIT tried to look-up this module in the cache.
178 EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer))switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(Cache->wasModuleLookedUp
(SecondModulePointer))) ; else ::testing::internal::AssertHelper
(::testing::TestPartResult::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 178, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "Cache->wasModuleLookedUp(SecondModulePointer)", "false"
, "true").c_str()) = ::testing::Message()
;
179
180 // Verify that MCJIT didn't try to cache this again.
181 EXPECT_FALSE(Cache->wereDuplicatesInserted())switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(!(Cache->wereDuplicatesInserted
()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult
::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 181, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "Cache->wereDuplicatesInserted()", "true", "false").c_str
()) = ::testing::Message()
;
182}
183
184TEST_F(MCJITObjectCacheTest, VerifyNonLoadFromCache)class MCJITObjectCacheTest_VerifyNonLoadFromCache_Test : public
MCJITObjectCacheTest { public: MCJITObjectCacheTest_VerifyNonLoadFromCache_Test
() {} private: virtual void TestBody(); static ::testing::TestInfo
* const test_info_ __attribute__ ((unused)); MCJITObjectCacheTest_VerifyNonLoadFromCache_Test
(MCJITObjectCacheTest_VerifyNonLoadFromCache_Test const &
); void operator=(MCJITObjectCacheTest_VerifyNonLoadFromCache_Test
const &);};::testing::TestInfo* const MCJITObjectCacheTest_VerifyNonLoadFromCache_Test
::test_info_ = ::testing::internal::MakeAndRegisterTestInfo(
"MCJITObjectCacheTest", "VerifyNonLoadFromCache", __null, __null
, (::testing::internal::GetTypeId<MCJITObjectCacheTest>
()), MCJITObjectCacheTest::SetUpTestCase, MCJITObjectCacheTest
::TearDownTestCase, new ::testing::internal::TestFactoryImpl<
MCJITObjectCacheTest_VerifyNonLoadFromCache_Test>);void MCJITObjectCacheTest_VerifyNonLoadFromCache_Test
::TestBody()
{
185 SKIP_UNSUPPORTED_PLATFORMdo if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) return; while
(0)
;
186
187 std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
188
189 // Compile this module with an MCJIT engine
190 createJIT(std::move(M));
191 TheJIT->setObjectCache(Cache.get());
192 TheJIT->finalizeObject();
193
194 // Destroy the MCJIT engine we just used
195 TheJIT.reset();
196
197 // Create a new memory manager.
198 MM.reset(new SectionMemoryManager());
199
200 // Create a new module and save it. Use a different return code so we can
201 // tell if MCJIT compiled this module or used the cache. Note that we use
202 // a new module name here so the module shouldn't be found in the cache.
203 M.reset(createEmptyModule("<not-main>"));
204 Main = insertMainFunction(M.get(), ReplacementRC);
205 const Module * SecondModulePointer = M.get();
206
207 // Create a new MCJIT instance to load this module then execute it.
208 createJIT(std::move(M));
209 TheJIT->setObjectCache(Cache.get());
210
211 // Verify that our object cache does not contain the module yet.
212 const MemoryBuffer *ObjBuffer = Cache->getObjectInternal(SecondModulePointer);
213 EXPECT_EQ(nullptr, ObjBuffer)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing
::internal::IsNullLiteralHelper(nullptr)) == 1)>::Compare(
"nullptr", "ObjBuffer", nullptr, ObjBuffer))) ; else ::testing
::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 213, gtest_ar.failure_message()) = ::testing::Message()
;
214
215 // Run the function and look for the replacement return code.
216 compileAndRun(ReplacementRC);
1
Calling 'MCJITObjectCacheTest::compileAndRun'
217
218 // Verify that MCJIT tried to look-up this module in the cache.
219 EXPECT_TRUE(Cache->wasModuleLookedUp(SecondModulePointer))switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(Cache->wasModuleLookedUp
(SecondModulePointer))) ; else ::testing::internal::AssertHelper
(::testing::TestPartResult::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 219, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "Cache->wasModuleLookedUp(SecondModulePointer)", "false"
, "true").c_str()) = ::testing::Message()
;
220
221 // Verify that our object cache now contains the module.
222 ObjBuffer = Cache->getObjectInternal(SecondModulePointer);
223 EXPECT_TRUE(nullptr != ObjBuffer)switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(nullptr != ObjBuffer)
) ; else ::testing::internal::AssertHelper(::testing::TestPartResult
::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 223, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "nullptr != ObjBuffer", "false", "true").c_str()) = ::testing
::Message()
;
224
225 // Verify that MCJIT didn't try to cache this again.
226 EXPECT_FALSE(Cache->wereDuplicatesInserted())switch (0) case 0: default: if (const ::testing::AssertionResult
gtest_ar_ = ::testing::AssertionResult(!(Cache->wereDuplicatesInserted
()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult
::kNonFatalFailure, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn257205/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp"
, 226, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_
, "Cache->wereDuplicatesInserted()", "true", "false").c_str
()) = ::testing::Message()
;
227}
228
229} // end anonymous namespace