85cabdbd41a48a9f69a3e86ef444faddb89df766
[oota-llvm.git] / unittests / ExecutionEngine / MCJIT / MCJITCAPITest.cpp
1 //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
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 // This test suite verifies basic MCJIT functionality when invoked form the C
11 // API.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Analysis.h"
16 #include "MCJITTestAPICommon.h"
17 #include "llvm-c/Core.h"
18 #include "llvm-c/ExecutionEngine.h"
19 #include "llvm-c/Target.h"
20 #include "llvm-c/Transforms/PassManagerBuilder.h"
21 #include "llvm-c/Transforms/Scalar.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Host.h"
25 #include "gtest/gtest.h"
26
27 using namespace llvm;
28
29 static bool didCallAllocateCodeSection;
30 static bool didAllocateCompactUnwindSection;
31 static bool didCallYield;
32
33 static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
34                                              unsigned alignment,
35                                              unsigned sectionID,
36                                              const char *sectionName) {
37   didCallAllocateCodeSection = true;
38   return static_cast<SectionMemoryManager*>(object)->allocateCodeSection(
39     size, alignment, sectionID, sectionName);
40 }
41
42 static uint8_t *roundTripAllocateDataSection(void *object, uintptr_t size,
43                                              unsigned alignment,
44                                              unsigned sectionID,
45                                              const char *sectionName,
46                                              LLVMBool isReadOnly) {
47   if (!strcmp(sectionName, "__compact_unwind"))
48     didAllocateCompactUnwindSection = true;
49   return static_cast<SectionMemoryManager*>(object)->allocateDataSection(
50     size, alignment, sectionID, sectionName, isReadOnly);
51 }
52
53 static LLVMBool roundTripFinalizeMemory(void *object, char **errMsg) {
54   std::string errMsgString;
55   bool result =
56     static_cast<SectionMemoryManager*>(object)->finalizeMemory(&errMsgString);
57   if (result) {
58     *errMsg = LLVMCreateMessage(errMsgString.c_str());
59     return 1;
60   }
61   return 0;
62 }
63
64 static void roundTripDestroy(void *object) {
65   delete static_cast<SectionMemoryManager*>(object);
66 }
67
68 static void yield(LLVMContextRef, void *) {
69   didCallYield = true;
70 }
71
72 namespace {
73
74 // memory manager to test reserve allocation space callback
75 class TestReserveAllocationSpaceMemoryManager: public SectionMemoryManager {
76 public:
77   uintptr_t ReservedCodeSize;
78   uintptr_t UsedCodeSize;
79   uintptr_t ReservedDataSizeRO;
80   uintptr_t UsedDataSizeRO;
81   uintptr_t ReservedDataSizeRW;
82   uintptr_t UsedDataSizeRW;
83   
84   TestReserveAllocationSpaceMemoryManager() : 
85     ReservedCodeSize(0), UsedCodeSize(0), ReservedDataSizeRO(0), 
86     UsedDataSizeRO(0), ReservedDataSizeRW(0), UsedDataSizeRW(0) {    
87   }
88
89   bool needsToReserveAllocationSpace() override { return true; }
90
91   void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
92                               uintptr_t DataSizeRW) override {
93     ReservedCodeSize = CodeSize;
94     ReservedDataSizeRO = DataSizeRO;
95     ReservedDataSizeRW = DataSizeRW;
96   }
97
98   void useSpace(uintptr_t* UsedSize, uintptr_t Size, unsigned Alignment) {
99     uintptr_t AlignedSize = (Size + Alignment - 1) / Alignment * Alignment;
100     uintptr_t AlignedBegin = (*UsedSize + Alignment - 1) / Alignment * Alignment;
101     *UsedSize = AlignedBegin + AlignedSize;
102   }
103
104   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
105                                unsigned SectionID, StringRef SectionName,
106                                bool IsReadOnly) override {
107     useSpace(IsReadOnly ? &UsedDataSizeRO : &UsedDataSizeRW, Size, Alignment);
108     return SectionMemoryManager::allocateDataSection(Size, Alignment, 
109       SectionID, SectionName, IsReadOnly);
110   }
111
112   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
113                                unsigned SectionID,
114                                StringRef SectionName) override {
115     useSpace(&UsedCodeSize, Size, Alignment);
116     return SectionMemoryManager::allocateCodeSection(Size, Alignment, 
117       SectionID, SectionName);
118   }
119 };
120
121 class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
122 protected:
123   MCJITCAPITest() {
124     // The architectures below are known to be compatible with MCJIT as they
125     // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
126     // kept in sync.
127     SupportedArchs.push_back(Triple::aarch64);
128     SupportedArchs.push_back(Triple::arm);
129     SupportedArchs.push_back(Triple::mips);
130     SupportedArchs.push_back(Triple::mips64);
131     SupportedArchs.push_back(Triple::mips64el);
132     SupportedArchs.push_back(Triple::x86);
133     SupportedArchs.push_back(Triple::x86_64);
134
135     // Some architectures have sub-architectures in which tests will fail, like
136     // ARM. These two vectors will define if they do have sub-archs (to avoid
137     // extra work for those who don't), and if so, if they are listed to work
138     HasSubArchs.push_back(Triple::arm);
139     SupportedSubArchs.push_back("armv6");
140     SupportedSubArchs.push_back("armv7");
141
142     // The operating systems below are known to be sufficiently incompatible
143     // that they will fail the MCJIT C API tests.
144     UnsupportedEnvironments.push_back(Triple::Cygnus);
145   }
146
147   void SetUp() override {
148     didCallAllocateCodeSection = false;
149     didAllocateCompactUnwindSection = false;
150     didCallYield = false;
151     Module = nullptr;
152     Function = nullptr;
153     Engine = nullptr;
154     Error = nullptr;
155   }
156
157   void TearDown() override {
158     if (Engine)
159       LLVMDisposeExecutionEngine(Engine);
160     else if (Module)
161       LLVMDisposeModule(Module);
162   }
163   
164   void buildSimpleFunction() {
165     Module = LLVMModuleCreateWithName("simple_module");
166     
167     LLVMSetTarget(Module, HostTriple.c_str());
168     
169     Function = LLVMAddFunction(Module, "simple_function",
170                                LLVMFunctionType(LLVMInt32Type(), nullptr,0, 0));
171     LLVMSetFunctionCallConv(Function, LLVMCCallConv);
172     
173     LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
174     LLVMBuilderRef builder = LLVMCreateBuilder();
175     LLVMPositionBuilderAtEnd(builder, entry);
176     LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
177     
178     LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
179     LLVMDisposeMessage(Error);
180     
181     LLVMDisposeBuilder(builder);
182   }
183   
184   void buildFunctionThatUsesStackmap() {
185     Module = LLVMModuleCreateWithName("simple_module");
186     
187     LLVMSetTarget(Module, HostTriple.c_str());
188     
189     LLVMTypeRef stackmapParamTypes[] = { LLVMInt64Type(), LLVMInt32Type() };
190     LLVMValueRef stackmap = LLVMAddFunction(
191       Module, "llvm.experimental.stackmap",
192       LLVMFunctionType(LLVMVoidType(), stackmapParamTypes, 2, 1));
193     LLVMSetLinkage(stackmap, LLVMExternalLinkage);
194     
195     Function = LLVMAddFunction(Module, "simple_function",
196                               LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0));
197     
198     LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
199     LLVMBuilderRef builder = LLVMCreateBuilder();
200     LLVMPositionBuilderAtEnd(builder, entry);
201     LLVMValueRef stackmapArgs[] = {
202       LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 5, 0),
203       LLVMConstInt(LLVMInt32Type(), 42, 0)
204     };
205     LLVMBuildCall(builder, stackmap, stackmapArgs, 3, "");
206     LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
207     
208     LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
209     LLVMDisposeMessage(Error);
210     
211     LLVMDisposeBuilder(builder);
212   }
213   
214   void buildModuleWithCodeAndData() {
215     Module = LLVMModuleCreateWithName("simple_module");
216     
217     LLVMSetTarget(Module, HostTriple.c_str());
218     
219     // build a global int32 variable initialized to 42.
220     LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "intVal");    
221     LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0));
222     
223     {
224         Function = LLVMAddFunction(Module, "getGlobal",
225                               LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0));
226         LLVMSetFunctionCallConv(Function, LLVMCCallConv);
227         
228         LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "entry");
229         LLVMBuilderRef Builder = LLVMCreateBuilder();
230         LLVMPositionBuilderAtEnd(Builder, Entry);
231         
232         LLVMValueRef IntVal = LLVMBuildLoad(Builder, GlobalVar, "intVal");
233         LLVMBuildRet(Builder, IntVal);
234         
235         LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
236         LLVMDisposeMessage(Error);
237         
238         LLVMDisposeBuilder(Builder);
239     }
240     
241     {
242         LLVMTypeRef ParamTypes[] = { LLVMInt32Type() };
243         Function2 = LLVMAddFunction(
244           Module, "setGlobal", LLVMFunctionType(LLVMVoidType(), ParamTypes, 1, 0));
245         LLVMSetFunctionCallConv(Function2, LLVMCCallConv);
246         
247         LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function2, "entry");
248         LLVMBuilderRef Builder = LLVMCreateBuilder();
249         LLVMPositionBuilderAtEnd(Builder, Entry);
250         
251         LLVMValueRef Arg = LLVMGetParam(Function2, 0);
252         LLVMBuildStore(Builder, Arg, GlobalVar);
253         LLVMBuildRetVoid(Builder);
254         
255         LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
256         LLVMDisposeMessage(Error);
257         
258         LLVMDisposeBuilder(Builder);
259     }
260   }
261   
262   void buildMCJITOptions() {
263     LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
264     Options.OptLevel = 2;
265     
266     // Just ensure that this field still exists.
267     Options.NoFramePointerElim = false;
268   }
269   
270   void useRoundTripSectionMemoryManager() {
271     Options.MCJMM = LLVMCreateSimpleMCJITMemoryManager(
272       new SectionMemoryManager(),
273       roundTripAllocateCodeSection,
274       roundTripAllocateDataSection,
275       roundTripFinalizeMemory,
276       roundTripDestroy);
277   }
278
279   void buildMCJITEngine() {
280     ASSERT_EQ(
281       0, LLVMCreateMCJITCompilerForModule(&Engine, Module, &Options,
282                                           sizeof(Options), &Error));
283   }
284   
285   void buildAndRunPasses() {
286     LLVMPassManagerRef pass = LLVMCreatePassManager();
287     LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), pass);
288     LLVMAddConstantPropagationPass(pass);
289     LLVMAddInstructionCombiningPass(pass);
290     LLVMRunPassManager(pass, Module);
291     LLVMDisposePassManager(pass);
292   }
293   
294   void buildAndRunOptPasses() {
295     LLVMPassManagerBuilderRef passBuilder;
296     
297     passBuilder = LLVMPassManagerBuilderCreate();
298     LLVMPassManagerBuilderSetOptLevel(passBuilder, 2);
299     LLVMPassManagerBuilderSetSizeLevel(passBuilder, 0);
300     
301     LLVMPassManagerRef functionPasses =
302       LLVMCreateFunctionPassManagerForModule(Module);
303     LLVMPassManagerRef modulePasses =
304       LLVMCreatePassManager();
305     
306     LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), modulePasses);
307     
308     LLVMPassManagerBuilderPopulateFunctionPassManager(passBuilder,
309                                                       functionPasses);
310     LLVMPassManagerBuilderPopulateModulePassManager(passBuilder, modulePasses);
311     
312     LLVMPassManagerBuilderDispose(passBuilder);
313     
314     LLVMInitializeFunctionPassManager(functionPasses);
315     for (LLVMValueRef value = LLVMGetFirstFunction(Module);
316          value; value = LLVMGetNextFunction(value))
317       LLVMRunFunctionPassManager(functionPasses, value);
318     LLVMFinalizeFunctionPassManager(functionPasses);
319     
320     LLVMRunPassManager(modulePasses, Module);
321     
322     LLVMDisposePassManager(functionPasses);
323     LLVMDisposePassManager(modulePasses);
324   }
325   
326   LLVMModuleRef Module;
327   LLVMValueRef Function;
328   LLVMValueRef Function2;
329   LLVMMCJITCompilerOptions Options;
330   LLVMExecutionEngineRef Engine;
331   char *Error;
332 };
333 } // end anonymous namespace
334
335 TEST_F(MCJITCAPITest, simple_function) {
336   SKIP_UNSUPPORTED_PLATFORM;
337   
338   buildSimpleFunction();
339   buildMCJITOptions();
340   buildMCJITEngine();
341   buildAndRunPasses();
342
343   auto *functionPointer = reinterpret_cast<int (*)()>(
344       reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
345
346   EXPECT_EQ(42, functionPointer());
347 }
348
349 TEST_F(MCJITCAPITest, gva) {
350   SKIP_UNSUPPORTED_PLATFORM;
351
352   Module = LLVMModuleCreateWithName("simple_module");
353   LLVMSetTarget(Module, HostTriple.c_str());
354   LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "simple_value");
355   LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0));
356
357   buildMCJITOptions();
358   buildMCJITEngine();
359   buildAndRunPasses();
360
361   uint64_t raw = LLVMGetGlobalValueAddress(Engine, "simple_value");
362   int32_t *usable  = (int32_t *) raw;
363
364   EXPECT_EQ(42, *usable);
365 }
366
367 TEST_F(MCJITCAPITest, gfa) {
368   SKIP_UNSUPPORTED_PLATFORM;
369
370   buildSimpleFunction();
371   buildMCJITOptions();
372   buildMCJITEngine();
373   buildAndRunPasses();
374
375   uint64_t raw = LLVMGetFunctionAddress(Engine, "simple_function");
376   int (*usable)() = (int (*)()) raw;
377
378   EXPECT_EQ(42, usable());
379 }
380
381 TEST_F(MCJITCAPITest, custom_memory_manager) {
382   SKIP_UNSUPPORTED_PLATFORM;
383   
384   buildSimpleFunction();
385   buildMCJITOptions();
386   useRoundTripSectionMemoryManager();
387   buildMCJITEngine();
388   buildAndRunPasses();
389
390   auto *functionPointer = reinterpret_cast<int (*)()>(
391       reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
392
393   EXPECT_EQ(42, functionPointer());
394   EXPECT_TRUE(didCallAllocateCodeSection);
395 }
396
397 TEST_F(MCJITCAPITest, stackmap_creates_compact_unwind_on_darwin) {
398   SKIP_UNSUPPORTED_PLATFORM;
399   
400   // This test is also not supported on non-x86 platforms.
401   if (Triple(HostTriple).getArch() != Triple::x86_64)
402     return;
403   
404   buildFunctionThatUsesStackmap();
405   buildMCJITOptions();
406   useRoundTripSectionMemoryManager();
407   buildMCJITEngine();
408   buildAndRunOptPasses();
409
410   auto *functionPointer = reinterpret_cast<int (*)()>(
411       reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
412
413   EXPECT_EQ(42, functionPointer());
414   EXPECT_TRUE(didCallAllocateCodeSection);
415   
416   // Up to this point, the test is specific only to X86-64. But this next
417   // expectation is only valid on Darwin because it assumes that unwind
418   // data is made available only through compact_unwind. It would be
419   // worthwhile to extend this to handle non-Darwin platforms, in which
420   // case you'd want to look for an eh_frame or something.
421   //
422   // FIXME: Currently, MCJIT relies on a configure-time check to determine which
423   // sections to emit. The JIT client should have runtime control over this.
424   EXPECT_TRUE(
425     Triple(HostTriple).getOS() != Triple::Darwin ||
426     Triple(HostTriple).isMacOSXVersionLT(10, 7) ||
427     didAllocateCompactUnwindSection);
428 }
429
430 TEST_F(MCJITCAPITest, reserve_allocation_space) {
431   SKIP_UNSUPPORTED_PLATFORM;
432   
433   TestReserveAllocationSpaceMemoryManager* MM = new TestReserveAllocationSpaceMemoryManager();
434   
435   buildModuleWithCodeAndData();
436   buildMCJITOptions();
437   Options.MCJMM = wrap(MM);
438   buildMCJITEngine();
439   buildAndRunPasses();
440
441   auto GetGlobalFct = reinterpret_cast<int (*)()>(
442       reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
443
444   auto SetGlobalFct = reinterpret_cast<void (*)(int)>(
445       reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function2)));
446
447   SetGlobalFct(789);
448   EXPECT_EQ(789, GetGlobalFct());
449   EXPECT_LE(MM->UsedCodeSize, MM->ReservedCodeSize);
450   EXPECT_LE(MM->UsedDataSizeRO, MM->ReservedDataSizeRO);
451   EXPECT_LE(MM->UsedDataSizeRW, MM->ReservedDataSizeRW);
452   EXPECT_TRUE(MM->UsedCodeSize > 0); 
453   EXPECT_TRUE(MM->UsedDataSizeRW > 0);
454 }
455
456 TEST_F(MCJITCAPITest, yield) {
457   SKIP_UNSUPPORTED_PLATFORM;
458
459   buildSimpleFunction();
460   buildMCJITOptions();
461   buildMCJITEngine();
462   LLVMContextRef C = LLVMGetGlobalContext();
463   LLVMContextSetYieldCallback(C, yield, nullptr);
464   buildAndRunPasses();
465
466   auto *functionPointer = reinterpret_cast<int (*)()>(
467       reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
468
469   EXPECT_EQ(42, functionPointer());
470   EXPECT_TRUE(didCallYield);
471 }
472
473 static int localTestFunc() {
474   return 42;
475 }
476
477 TEST_F(MCJITCAPITest, addGlobalMapping) {
478   SKIP_UNSUPPORTED_PLATFORM;
479
480   Module = LLVMModuleCreateWithName("testModule");
481   LLVMSetTarget(Module, HostTriple.c_str());
482   LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), NULL, 0, 0);
483   LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType);
484
485   Function = LLVMAddFunction(Module, "test_fn", FunctionType);
486   LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "");
487   LLVMBuilderRef Builder = LLVMCreateBuilder();
488   LLVMPositionBuilderAtEnd(Builder, Entry);
489   LLVMValueRef RetVal = LLVMBuildCall(Builder, MappedFn, NULL, 0, "");
490   LLVMBuildRet(Builder, RetVal);
491   LLVMDisposeBuilder(Builder);
492
493   LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
494   LLVMDisposeMessage(Error);
495
496   buildMCJITOptions();
497   buildMCJITEngine();
498
499   LLVMAddGlobalMapping(
500       Engine, MappedFn,
501       reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(&localTestFunc)));
502
503   buildAndRunPasses();
504
505   uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn");
506   int (*usable)() = (int (*)()) raw;
507
508   EXPECT_EQ(42, usable());
509 }