blob: 129f21f84fbe7eeed4a242817cacd7909bbcb4a2 [file] [log] [blame]
Reid Kleckner4b1511b2009-07-18 00:42:18 +00001//===- JITTest.cpp - Unit tests for the JIT -------------------------------===//
Jeffrey Yasskin489393d2009-07-08 21:59:57 +00002//
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"
Reid Kleckner4b1511b2009-07-18 00:42:18 +000021#include "llvm/LLVMContext.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000022#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
28using namespace llvm;
29
30namespace {
31
32Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
33 std::vector<const Type*> params;
Owen Anderson9adc0ab2009-07-14 23:09:55 +000034 const FunctionType *FTy =
35 getGlobalContext().getFunctionType(G->getType()->getElementType(),
Dan Gohmanc6f40b62009-07-11 13:56:14 +000036 params, false);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000037 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();
Owen Andersoneed707b2009-07-24 23:12:02 +000042 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000043 builder.CreateStore(Add, G);
44 builder.CreateRet(Add);
45 return F;
46}
47
48// Regression test for a bug. The JIT used to allocate globals inside the same
49// memory block used for the function, and when the function code was freed,
50// the global was left in the same place. This test allocates a function
51// that uses and global, deallocates it, and then makes sure that the global
52// stays alive after that.
53TEST(JIT, GlobalInFunction) {
54 LLVMContext context;
55 Module *M = new Module("<main>", context);
56 ExistingModuleProvider *MP = new ExistingModuleProvider(M);
57
58 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
59 // Tell the memory manager to poison freed memory so that accessing freed
60 // memory is more easily tested.
61 MemMgr->setPoisonMemory(true);
62 std::string Error;
Reid Kleckner4b1511b2009-07-18 00:42:18 +000063 OwningPtr<ExecutionEngine> JIT(EngineBuilder(MP)
Daniel Dunbard370d772009-07-18 06:08:49 +000064 .setEngineKind(EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +000065 .setErrorStr(&Error)
66 .setJITMemoryManager(MemMgr)
67 // The next line enables the fix:
68 .setAllocateGVsWithCode(false)
69 .create());
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000070 ASSERT_EQ(Error, "");
71
72 // Create a global variable.
73 const Type *GTy = Type::Int32Ty;
74 GlobalVariable *G = new GlobalVariable(
75 *M,
76 GTy,
77 false, // Not constant.
78 GlobalValue::InternalLinkage,
Owen Anderson0a5372e2009-07-13 04:09:18 +000079 context.getNullValue(GTy),
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000080 "myglobal");
81
82 // Make a function that points to a global.
83 Function *F1 = makeReturnGlobal("F1", G, M);
84
85 // Get the pointer to the native code to force it to JIT the function and
86 // allocate space for the global.
87 void (*F1Ptr)();
88 // Hack to avoid ISO C++ warning about casting function pointers.
89 *(void**)(void*)&F1Ptr = JIT->getPointerToFunction(F1);
90
91 // Since F1 was codegen'd, a pointer to G should be available.
92 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
93 ASSERT_NE((int32_t*)NULL, GPtr);
94 EXPECT_EQ(0, *GPtr);
95
96 // F1() should increment G.
97 F1Ptr();
98 EXPECT_EQ(1, *GPtr);
99
100 // Make a second function identical to the first, referring to the same
101 // global.
102 Function *F2 = makeReturnGlobal("F2", G, M);
103 // Hack to avoid ISO C++ warning about casting function pointers.
104 void (*F2Ptr)();
105 *(void**)(void*)&F2Ptr = JIT->getPointerToFunction(F2);
106
107 // F2() should increment G.
108 F2Ptr();
109 EXPECT_EQ(2, *GPtr);
110
111 // Deallocate F1.
112 JIT->freeMachineCodeForFunction(F1);
113
114 // F2() should *still* increment G.
115 F2Ptr();
116 EXPECT_EQ(3, *GPtr);
117}
118
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000119// This code is copied from JITEventListenerTest, but it only runs once for all
120// the tests in this directory. Everything seems fine, but that's strange
121// behavior.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000122class JITEnvironment : public testing::Environment {
123 virtual void SetUp() {
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000124 // Required to create a JIT.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000125 InitializeNativeTarget();
126 }
127};
128testing::Environment* const jit_env =
129 testing::AddGlobalTestEnvironment(new JITEnvironment);
130
131}