41caeb21bf83083d42ae264269cc00a3291fc964
[oota-llvm.git] / unittests / ExecutionEngine / JIT / JITTest.cpp
1 //===- JITTest.cpp - Unit tests for the JIT -------------------------------===//
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 "gtest/gtest.h"
11 #include "llvm/ADT/OwningPtr.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/Constant.h"
14 #include "llvm/Constants.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ExecutionEngine/JIT.h"
17 #include "llvm/ExecutionEngine/JITMemoryManager.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalValue.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/Support/IRBuilder.h"
25 #include "llvm/Target/TargetSelect.h"
26 #include "llvm/Type.h"
27
28 using namespace llvm;
29
30 namespace {
31
32 Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
33   std::vector<const Type*> params;
34   const FunctionType *FTy =
35     getGlobalContext().getFunctionType(G->getType()->getElementType(),
36                                               params, false);
37   Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
38   BasicBlock *Entry = BasicBlock::Create("entry", F);
39   IRBuilder<> builder(Entry);
40   Value *Load = builder.CreateLoad(G);
41   const Type *GTy = G->getType()->getElementType();
42   Value *Add = builder.CreateAdd(Load,
43     getGlobalContext().getConstantInt(GTy, 1LL));
44   builder.CreateStore(Add, G);
45   builder.CreateRet(Add);
46   return F;
47 }
48
49 // Regression test for a bug.  The JIT used to allocate globals inside the same
50 // memory block used for the function, and when the function code was freed,
51 // the global was left in the same place.  This test allocates a function
52 // that uses and global, deallocates it, and then makes sure that the global
53 // stays alive after that.
54 TEST(JIT, GlobalInFunction) {
55   LLVMContext context;
56   Module *M = new Module("<main>", context);
57   ExistingModuleProvider *MP = new ExistingModuleProvider(M);
58
59   JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
60   // Tell the memory manager to poison freed memory so that accessing freed
61   // memory is more easily tested.
62   MemMgr->setPoisonMemory(true);
63   std::string Error;
64   OwningPtr<ExecutionEngine> JIT(EngineBuilder(MP)
65                                  .setEngineKind(EngineKind::JIT)
66                                  .setErrorStr(&Error)
67                                  .setJITMemoryManager(MemMgr)
68                                  // The next line enables the fix:
69                                  .setAllocateGVsWithCode(false)
70                                  .create());
71   ASSERT_EQ(Error, "");
72
73   // Create a global variable.
74   const Type *GTy = Type::Int32Ty;
75   GlobalVariable *G = new GlobalVariable(
76       *M,
77       GTy,
78       false,  // Not constant.
79       GlobalValue::InternalLinkage,
80       context.getNullValue(GTy),
81       "myglobal");
82
83   // Make a function that points to a global.
84   Function *F1 = makeReturnGlobal("F1", G, M);
85
86   // Get the pointer to the native code to force it to JIT the function and
87   // allocate space for the global.
88   void (*F1Ptr)();
89   // Hack to avoid ISO C++ warning about casting function pointers.
90   *(void**)(void*)&F1Ptr = JIT->getPointerToFunction(F1);
91
92   // Since F1 was codegen'd, a pointer to G should be available.
93   int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
94   ASSERT_NE((int32_t*)NULL, GPtr);
95   EXPECT_EQ(0, *GPtr);
96
97   // F1() should increment G.
98   F1Ptr();
99   EXPECT_EQ(1, *GPtr);
100
101   // Make a second function identical to the first, referring to the same
102   // global.
103   Function *F2 = makeReturnGlobal("F2", G, M);
104   // Hack to avoid ISO C++ warning about casting function pointers.
105   void (*F2Ptr)();
106   *(void**)(void*)&F2Ptr = JIT->getPointerToFunction(F2);
107
108   // F2() should increment G.
109   F2Ptr();
110   EXPECT_EQ(2, *GPtr);
111
112   // Deallocate F1.
113   JIT->freeMachineCodeForFunction(F1);
114
115   // F2() should *still* increment G.
116   F2Ptr();
117   EXPECT_EQ(3, *GPtr);
118 }
119
120 // This code is copied from JITEventListenerTest, but it only runs once for all
121 // the tests in this directory.  Everything seems fine, but that's strange
122 // behavior.
123 class JITEnvironment : public testing::Environment {
124   virtual void SetUp() {
125     // Required to create a JIT.
126     InitializeNativeTarget();
127   }
128 };
129 testing::Environment* const jit_env =
130   testing::AddGlobalTestEnvironment(new JITEnvironment);
131
132 }