blob: 8f29918a992a5c1cf8d34d698d6be0ed02c6848c [file] [log] [blame]
Jeffrey Yasskin489393d2009-07-08 21:59:57 +00001//===- JITEmitter.cpp - Unit tests for the JIT code emitter ---------------===//
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/Module.h"
22#include "llvm/ModuleProvider.h"
23#include "llvm/Support/IRBuilder.h"
24#include "llvm/Target/TargetSelect.h"
25#include "llvm/Type.h"
26
27using namespace llvm;
28
29namespace {
30
31Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
32 std::vector<const Type*> params;
33 const FunctionType *FTy = FunctionType::get(Type::VoidTy, params, false);
34 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
35 BasicBlock *Entry = BasicBlock::Create("entry", F);
36 IRBuilder<> builder(Entry);
37 Value *Load = builder.CreateLoad(G);
38 const Type *GTy = G->getType()->getElementType();
39 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
40 builder.CreateStore(Add, G);
41 builder.CreateRet(Add);
42 return F;
43}
44
45// Regression test for a bug. The JIT used to allocate globals inside the same
46// memory block used for the function, and when the function code was freed,
47// the global was left in the same place. This test allocates a function
48// that uses and global, deallocates it, and then makes sure that the global
49// stays alive after that.
50TEST(JIT, GlobalInFunction) {
51 LLVMContext context;
52 Module *M = new Module("<main>", context);
53 ExistingModuleProvider *MP = new ExistingModuleProvider(M);
54
55 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
56 // Tell the memory manager to poison freed memory so that accessing freed
57 // memory is more easily tested.
58 MemMgr->setPoisonMemory(true);
59 std::string Error;
60 OwningPtr<ExecutionEngine> JIT(ExecutionEngine::createJIT(
61 MP,
62 &Error,
63 MemMgr,
64 CodeGenOpt::Default,
65 false)); // This last argument enables the fix.
66 ASSERT_EQ(Error, "");
67
68 // Create a global variable.
69 const Type *GTy = Type::Int32Ty;
70 GlobalVariable *G = new GlobalVariable(
71 *M,
72 GTy,
73 false, // Not constant.
74 GlobalValue::InternalLinkage,
75 Constant::getNullValue(GTy),
76 "myglobal");
77
78 // Make a function that points to a global.
79 Function *F1 = makeReturnGlobal("F1", G, M);
80
81 // Get the pointer to the native code to force it to JIT the function and
82 // allocate space for the global.
83 void (*F1Ptr)();
84 // Hack to avoid ISO C++ warning about casting function pointers.
85 *(void**)(void*)&F1Ptr = JIT->getPointerToFunction(F1);
86
87 // Since F1 was codegen'd, a pointer to G should be available.
88 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
89 ASSERT_NE((int32_t*)NULL, GPtr);
90 EXPECT_EQ(0, *GPtr);
91
92 // F1() should increment G.
93 F1Ptr();
94 EXPECT_EQ(1, *GPtr);
95
96 // Make a second function identical to the first, referring to the same
97 // global.
98 Function *F2 = makeReturnGlobal("F2", G, M);
99 // Hack to avoid ISO C++ warning about casting function pointers.
100 void (*F2Ptr)();
101 *(void**)(void*)&F2Ptr = JIT->getPointerToFunction(F2);
102
103 // F2() should increment G.
104 F2Ptr();
105 EXPECT_EQ(2, *GPtr);
106
107 // Deallocate F1.
108 JIT->freeMachineCodeForFunction(F1);
109
110 // F2() should *still* increment G.
111 F2Ptr();
112 EXPECT_EQ(3, *GPtr);
113}
114
115// TODO(rnk): This seems to only run once for both tests, which is unexpected.
116// That works just fine, but we shouldn't duplicate the code.
117class JITEnvironment : public testing::Environment {
118 virtual void SetUp() {
119 // Required for ExecutionEngine::createJIT to create a JIT.
120 InitializeNativeTarget();
121 }
122};
123testing::Environment* const jit_env =
124 testing::AddGlobalTestEnvironment(new JITEnvironment);
125
126}