1 //===- MCJITMemoryManagerTest.cpp - Unit tests for the JIT memory manager -===//
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 "llvm/ExecutionEngine/SectionMemoryManager.h"
11 #include "llvm/ExecutionEngine/JIT.h"
12 #include "gtest/gtest.h"
18 TEST(MCJITMemoryManagerTest, BasicAllocations) {
19 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
21 uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, "");
22 uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, "", true);
23 uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3, "");
24 uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, "", false);
26 EXPECT_NE((uint8_t*)0, code1);
27 EXPECT_NE((uint8_t*)0, code2);
28 EXPECT_NE((uint8_t*)0, data1);
29 EXPECT_NE((uint8_t*)0, data2);
31 // Initialize the data
32 for (unsigned i = 0; i < 256; ++i) {
39 // Verify the data (this is checking for overlaps in the addresses)
40 for (unsigned i = 0; i < 256; ++i) {
41 EXPECT_EQ(1, code1[i]);
42 EXPECT_EQ(2, code2[i]);
43 EXPECT_EQ(3, data1[i]);
44 EXPECT_EQ(4, data2[i]);
48 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
51 TEST(MCJITMemoryManagerTest, LargeAllocations) {
52 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
54 uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1, "");
55 uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, "", true);
56 uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3, "");
57 uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, "", false);
59 EXPECT_NE((uint8_t*)0, code1);
60 EXPECT_NE((uint8_t*)0, code2);
61 EXPECT_NE((uint8_t*)0, data1);
62 EXPECT_NE((uint8_t*)0, data2);
64 // Initialize the data
65 for (unsigned i = 0; i < 0x100000; ++i) {
72 // Verify the data (this is checking for overlaps in the addresses)
73 for (unsigned i = 0; i < 0x100000; ++i) {
74 EXPECT_EQ(1, code1[i]);
75 EXPECT_EQ(2, code2[i]);
76 EXPECT_EQ(3, data1[i]);
77 EXPECT_EQ(4, data2[i]);
81 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
84 TEST(MCJITMemoryManagerTest, ManyAllocations) {
85 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
90 for (unsigned i = 0; i < 10000; ++i) {
91 const bool isReadOnly = i % 2 == 0;
93 code[i] = MemMgr->allocateCodeSection(32, 0, 1, "");
94 data[i] = MemMgr->allocateDataSection(32, 0, 2, "", isReadOnly);
96 for (unsigned j = 0; j < 32; j++) {
97 code[i][j] = 1 + (i % 254);
98 data[i][j] = 2 + (i % 254);
101 EXPECT_NE((uint8_t *)0, code[i]);
102 EXPECT_NE((uint8_t *)0, data[i]);
105 // Verify the data (this is checking for overlaps in the addresses)
106 for (unsigned i = 0; i < 10000; ++i) {
107 for (unsigned j = 0; j < 32;j++ ) {
108 uint8_t ExpectedCode = 1 + (i % 254);
109 uint8_t ExpectedData = 2 + (i % 254);
110 EXPECT_EQ(ExpectedCode, code[i][j]);
111 EXPECT_EQ(ExpectedData, data[i][j]);
116 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
119 TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {
120 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
122 uint8_t* code[10000];
123 uint8_t* data[10000];
125 for (unsigned i = 0; i < 10000; ++i) {
126 uintptr_t CodeSize = i % 16 + 1;
127 uintptr_t DataSize = i % 8 + 1;
129 bool isReadOnly = i % 3 == 0;
130 unsigned Align = 8 << (i % 4);
132 code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i, "");
133 data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000, "",
136 for (unsigned j = 0; j < CodeSize; j++) {
137 code[i][j] = 1 + (i % 254);
140 for (unsigned j = 0; j < DataSize; j++) {
141 data[i][j] = 2 + (i % 254);
144 EXPECT_NE((uint8_t *)0, code[i]);
145 EXPECT_NE((uint8_t *)0, data[i]);
147 uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;
148 uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;
150 EXPECT_EQ((uintptr_t)0, CodeAlign);
151 EXPECT_EQ((uintptr_t)0, DataAlign);
154 for (unsigned i = 0; i < 10000; ++i) {
155 uintptr_t CodeSize = i % 16 + 1;
156 uintptr_t DataSize = i % 8 + 1;
158 for (unsigned j = 0; j < CodeSize; j++) {
159 uint8_t ExpectedCode = 1 + (i % 254);
160 EXPECT_EQ(ExpectedCode, code[i][j]);
163 for (unsigned j = 0; j < DataSize; j++) {
164 uint8_t ExpectedData = 2 + (i % 254);
165 EXPECT_EQ(ExpectedData, data[i][j]);