1 //===- JITTest.cpp - Unit tests for the JIT -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/OwningPtr.h"
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/Assembly/Parser.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Constant.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/ExecutionEngine/JIT.h"
19 #include "llvm/ExecutionEngine/JITMemoryManager.h"
20 #include "llvm/Function.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/GlobalVariable.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Module.h"
25 #include "llvm/ModuleProvider.h"
26 #include "llvm/Support/IRBuilder.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/TypeBuilder.h"
29 #include "llvm/Target/TargetSelect.h"
30 #include "llvm/Type.h"
38 Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
39 std::vector<const Type*> params;
40 const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
42 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
43 BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F);
44 IRBuilder<> builder(Entry);
45 Value *Load = builder.CreateLoad(G);
46 const Type *GTy = G->getType()->getElementType();
47 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
48 builder.CreateStore(Add, G);
49 builder.CreateRet(Add);
53 std::string DumpFunction(const Function *F) {
55 raw_string_ostream(Result) << "" << *F;
59 class RecordingJITMemoryManager : public JITMemoryManager {
60 const OwningPtr<JITMemoryManager> Base;
62 RecordingJITMemoryManager()
63 : Base(JITMemoryManager::CreateDefaultMemManager()) {
67 virtual void setMemoryWritable() { Base->setMemoryWritable(); }
68 virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
69 virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); }
70 virtual void AllocateGOT() { Base->AllocateGOT(); }
71 virtual uint8_t *getGOTBase() const { return Base->getGOTBase(); }
72 struct StartFunctionBodyCall {
73 StartFunctionBodyCall(uint8_t *Result, const Function *F,
74 uintptr_t ActualSize, uintptr_t ActualSizeResult)
75 : Result(Result), F(F), F_dump(DumpFunction(F)),
76 ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
81 uintptr_t ActualSizeResult;
83 std::vector<StartFunctionBodyCall> startFunctionBodyCalls;
84 virtual uint8_t *startFunctionBody(const Function *F,
85 uintptr_t &ActualSize) {
86 uintptr_t InitialActualSize = ActualSize;
87 uint8_t *Result = Base->startFunctionBody(F, ActualSize);
88 startFunctionBodyCalls.push_back(
89 StartFunctionBodyCall(Result, F, InitialActualSize, ActualSize));
93 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
96 return Base->allocateStub(F, StubSize, Alignment);
98 struct EndFunctionBodyCall {
99 EndFunctionBodyCall(const Function *F, uint8_t *FunctionStart,
100 uint8_t *FunctionEnd)
101 : F(F), F_dump(DumpFunction(F)),
102 FunctionStart(FunctionStart), FunctionEnd(FunctionEnd) {}
105 uint8_t *FunctionStart;
106 uint8_t *FunctionEnd;
108 std::vector<EndFunctionBodyCall> endFunctionBodyCalls;
109 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
110 uint8_t *FunctionEnd) {
111 endFunctionBodyCalls.push_back(
112 EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
113 Base->endFunctionBody(F, FunctionStart, FunctionEnd);
115 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
116 return Base->allocateSpace(Size, Alignment);
118 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
119 return Base->allocateGlobal(Size, Alignment);
121 struct DeallocateFunctionBodyCall {
122 DeallocateFunctionBodyCall(const void *Body) : Body(Body) {}
125 std::vector<DeallocateFunctionBodyCall> deallocateFunctionBodyCalls;
126 virtual void deallocateFunctionBody(void *Body) {
127 deallocateFunctionBodyCalls.push_back(DeallocateFunctionBodyCall(Body));
128 Base->deallocateFunctionBody(Body);
130 struct DeallocateExceptionTableCall {
131 DeallocateExceptionTableCall(const void *ET) : ET(ET) {}
134 std::vector<DeallocateExceptionTableCall> deallocateExceptionTableCalls;
135 virtual void deallocateExceptionTable(void *ET) {
136 deallocateExceptionTableCalls.push_back(DeallocateExceptionTableCall(ET));
137 Base->deallocateExceptionTable(ET);
139 struct StartExceptionTableCall {
140 StartExceptionTableCall(uint8_t *Result, const Function *F,
141 uintptr_t ActualSize, uintptr_t ActualSizeResult)
142 : Result(Result), F(F), F_dump(DumpFunction(F)),
143 ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
147 uintptr_t ActualSize;
148 uintptr_t ActualSizeResult;
150 std::vector<StartExceptionTableCall> startExceptionTableCalls;
151 virtual uint8_t* startExceptionTable(const Function* F,
152 uintptr_t &ActualSize) {
153 uintptr_t InitialActualSize = ActualSize;
154 uint8_t *Result = Base->startExceptionTable(F, ActualSize);
155 startExceptionTableCalls.push_back(
156 StartExceptionTableCall(Result, F, InitialActualSize, ActualSize));
159 struct EndExceptionTableCall {
160 EndExceptionTableCall(const Function *F, uint8_t *TableStart,
161 uint8_t *TableEnd, uint8_t* FrameRegister)
162 : F(F), F_dump(DumpFunction(F)),
163 TableStart(TableStart), TableEnd(TableEnd),
164 FrameRegister(FrameRegister) {}
169 uint8_t *FrameRegister;
171 std::vector<EndExceptionTableCall> endExceptionTableCalls;
172 virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
173 uint8_t *TableEnd, uint8_t* FrameRegister) {
174 endExceptionTableCalls.push_back(
175 EndExceptionTableCall(F, TableStart, TableEnd, FrameRegister));
176 return Base->endExceptionTable(F, TableStart, TableEnd, FrameRegister);
180 class JITTest : public testing::Test {
182 virtual void SetUp() {
183 M = new Module("<main>", Context);
184 MP = new ExistingModuleProvider(M);
185 RJMM = new RecordingJITMemoryManager;
187 TheJIT.reset(EngineBuilder(MP).setEngineKind(EngineKind::JIT)
188 .setJITMemoryManager(RJMM)
189 .setErrorStr(&Error).create());
190 ASSERT_TRUE(TheJIT.get() != NULL) << Error;
193 void LoadAssembly(const char *assembly) {
195 bool success = NULL != ParseAssemblyString(assembly, M, Error, Context);
197 raw_string_ostream os(errMsg);
199 ASSERT_TRUE(success) << os.str();
203 Module *M; // Owned by MP.
204 ModuleProvider *MP; // Owned by ExecutionEngine.
205 RecordingJITMemoryManager *RJMM;
206 OwningPtr<ExecutionEngine> TheJIT;
209 // Regression test for a bug. The JIT used to allocate globals inside the same
210 // memory block used for the function, and when the function code was freed,
211 // the global was left in the same place. This test allocates a function
212 // that uses and global, deallocates it, and then makes sure that the global
213 // stays alive after that.
214 TEST(JIT, GlobalInFunction) {
216 Module *M = new Module("<main>", context);
217 ExistingModuleProvider *MP = new ExistingModuleProvider(M);
219 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
220 // Tell the memory manager to poison freed memory so that accessing freed
221 // memory is more easily tested.
222 MemMgr->setPoisonMemory(true);
224 OwningPtr<ExecutionEngine> JIT(EngineBuilder(MP)
225 .setEngineKind(EngineKind::JIT)
227 .setJITMemoryManager(MemMgr)
228 // The next line enables the fix:
229 .setAllocateGVsWithCode(false)
231 ASSERT_EQ(Error, "");
233 // Create a global variable.
234 const Type *GTy = Type::getInt32Ty(context);
235 GlobalVariable *G = new GlobalVariable(
238 false, // Not constant.
239 GlobalValue::InternalLinkage,
240 Constant::getNullValue(GTy),
243 // Make a function that points to a global.
244 Function *F1 = makeReturnGlobal("F1", G, M);
246 // Get the pointer to the native code to force it to JIT the function and
247 // allocate space for the global.
249 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1));
251 // Since F1 was codegen'd, a pointer to G should be available.
252 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
253 ASSERT_NE((int32_t*)NULL, GPtr);
256 // F1() should increment G.
260 // Make a second function identical to the first, referring to the same
262 Function *F2 = makeReturnGlobal("F2", G, M);
264 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2));
266 // F2() should increment G.
271 JIT->freeMachineCodeForFunction(F1);
273 // F2() should *still* increment G.
278 int PlusOne(int arg) {
282 TEST_F(JITTest, FarCallToKnownFunction) {
283 // x86-64 can only make direct calls to functions within 32 bits of
284 // the current PC. To call anything farther away, we have to load
285 // the address into a register and call through the register. The
286 // current JIT does this by allocating a stub for any far call.
287 // There was a bug in which the JIT tried to emit a direct call when
288 // the target was already in the JIT's global mappings and lazy
289 // compilation was disabled.
291 Function *KnownFunction = Function::Create(
292 TypeBuilder<int(int), false>::get(Context),
293 GlobalValue::ExternalLinkage, "known", M);
294 TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);
296 // int test() { return known(7); }
297 Function *TestFunction = Function::Create(
298 TypeBuilder<int(), false>::get(Context),
299 GlobalValue::ExternalLinkage, "test", M);
300 BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction);
301 IRBuilder<> Builder(Entry);
302 Value *result = Builder.CreateCall(
304 ConstantInt::get(TypeBuilder<int, false>::get(Context), 7));
305 Builder.CreateRet(result);
307 TheJIT->DisableLazyCompilation(true);
308 int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>(
309 (intptr_t)TheJIT->getPointerToFunction(TestFunction));
310 // This used to crash in trying to call PlusOne().
311 EXPECT_EQ(8, TestFunctionPtr());
314 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__ppc__)
315 // Test a function C which calls A and B which call each other.
316 TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
317 TheJIT->DisableLazyCompilation(true);
319 const FunctionType *Func1Ty =
320 cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
321 std::vector<const Type*> arg_types;
322 arg_types.push_back(Type::getInt1Ty(Context));
323 const FunctionType *FuncTy = FunctionType::get(
324 Type::getVoidTy(Context), arg_types, false);
325 Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
327 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
329 Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage,
331 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
332 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
333 BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2);
334 BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2);
335 BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3);
336 BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3);
337 BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3);
339 // Make Func1 call Func2(0) and Func3(0).
340 IRBuilder<> Builder(Block1);
341 Builder.CreateCall(Func2, ConstantInt::getTrue(Context));
342 Builder.CreateCall(Func3, ConstantInt::getTrue(Context));
343 Builder.CreateRetVoid();
345 // void Func2(bool b) { if (b) { Func3(false); return; } return; }
346 Builder.SetInsertPoint(Block2);
347 Builder.CreateCondBr(Func2->arg_begin(), True2, False2);
348 Builder.SetInsertPoint(True2);
349 Builder.CreateCall(Func3, ConstantInt::getFalse(Context));
350 Builder.CreateRetVoid();
351 Builder.SetInsertPoint(False2);
352 Builder.CreateRetVoid();
354 // void Func3(bool b) { if (b) { Func2(false); return; } return; }
355 Builder.SetInsertPoint(Block3);
356 Builder.CreateCondBr(Func3->arg_begin(), True3, False3);
357 Builder.SetInsertPoint(True3);
358 Builder.CreateCall(Func2, ConstantInt::getFalse(Context));
359 Builder.CreateRetVoid();
360 Builder.SetInsertPoint(False3);
361 Builder.CreateRetVoid();
363 // Compile the function to native code
365 reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1));
370 // Regression test for PR5162. This used to trigger an AssertingVH inside the
371 // JIT's Function to stub mapping.
372 TEST_F(JITTest, NonLazyLeaksNoStubs) {
373 TheJIT->DisableLazyCompilation(true);
375 // Create two functions with a single basic block each.
376 const FunctionType *FuncTy =
377 cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
378 Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
380 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
382 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
383 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
385 // The first function calls the second and returns the result
386 IRBuilder<> Builder(Block1);
387 Value *Result = Builder.CreateCall(Func2);
388 Builder.CreateRet(Result);
390 // The second function just returns a constant
391 Builder.SetInsertPoint(Block2);
392 Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42));
394 // Compile the function to native code
395 (void)TheJIT->getPointerToFunction(Func1);
397 // Free the JIT state for the functions
398 TheJIT->freeMachineCodeForFunction(Func1);
399 TheJIT->freeMachineCodeForFunction(Func2);
401 // Delete the first function (and show that is has no users)
402 EXPECT_EQ(Func1->getNumUses(), 0u);
403 Func1->eraseFromParent();
405 // Delete the second function (and show that it has no users - it had one,
406 // func1 but that's gone now)
407 EXPECT_EQ(Func2->getNumUses(), 0u);
408 Func2->eraseFromParent();
412 TEST_F(JITTest, ModuleDeletion) {
413 TheJIT->DisableLazyCompilation(false);
414 LoadAssembly("define void @main() { "
415 " call i32 @computeVal() "
419 "define internal i32 @computeVal() { "
422 Function *func = M->getFunction("main");
423 TheJIT->getPointerToFunction(func);
424 TheJIT->deleteModuleProvider(MP);
426 SmallPtrSet<const void*, 2> FunctionsDeallocated;
427 for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size();
429 FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body);
431 for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) {
432 EXPECT_TRUE(FunctionsDeallocated.count(
433 RJMM->startFunctionBodyCalls[i].Result))
434 << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump;
436 EXPECT_EQ(RJMM->startFunctionBodyCalls.size(),
437 RJMM->deallocateFunctionBodyCalls.size());
439 SmallPtrSet<const void*, 2> ExceptionTablesDeallocated;
440 unsigned NumTablesDeallocated = 0;
441 for (unsigned i = 0, e = RJMM->deallocateExceptionTableCalls.size();
443 ExceptionTablesDeallocated.insert(
444 RJMM->deallocateExceptionTableCalls[i].ET);
445 if (RJMM->deallocateExceptionTableCalls[i].ET != NULL) {
446 // If JITEmitDebugInfo is off, we'll "deallocate" NULL, which doesn't
447 // appear in startExceptionTableCalls.
448 NumTablesDeallocated++;
451 for (unsigned i = 0, e = RJMM->startExceptionTableCalls.size(); i != e; ++i) {
452 EXPECT_TRUE(ExceptionTablesDeallocated.count(
453 RJMM->startExceptionTableCalls[i].Result))
454 << "Function's exception table leaked: \n"
455 << RJMM->startExceptionTableCalls[i].F_dump;
457 EXPECT_EQ(RJMM->startExceptionTableCalls.size(),
458 NumTablesDeallocated);
461 typedef int (*FooPtr) ();
463 TEST_F(JITTest, NoStubs) {
464 LoadAssembly("define void @bar() {"
469 "define i32 @foo() {"
475 "define i32 @main() {"
477 "%0 = call i32 @foo()"
481 Function *foo = M->getFunction("foo");
482 uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo));
483 FooPtr ptr = (FooPtr)(tmp);
487 // We should now allocate no more stubs, we have the code to foo
488 // and the existing stub for bar.
489 int stubsBefore = RJMM->stubsAllocated;
490 Function *func = M->getFunction("main");
491 TheJIT->getPointerToFunction(func);
493 Function *bar = M->getFunction("bar");
494 TheJIT->getPointerToFunction(bar);
496 ASSERT_EQ(stubsBefore, RJMM->stubsAllocated);
499 // This code is copied from JITEventListenerTest, but it only runs once for all
500 // the tests in this directory. Everything seems fine, but that's strange
502 class JITEnvironment : public testing::Environment {
503 virtual void SetUp() {
504 // Required to create a JIT.
505 InitializeNativeTarget();
508 testing::Environment* const jit_env =
509 testing::AddGlobalTestEnvironment(new JITEnvironment);