1 //===- MCJITMemoryManagerTest.cpp - Unit tests for the JIT memory manager -===//
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 "llvm/ExecutionEngine/SectionMemoryManager.h"
11 #include "llvm/ExecutionEngine/JIT.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
TEST(MCJITMemoryManagerTest,BasicAllocations)18 TEST(MCJITMemoryManagerTest, BasicAllocations) {
19 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
20
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);
25
26 EXPECT_NE((uint8_t*)nullptr, code1);
27 EXPECT_NE((uint8_t*)nullptr, code2);
28 EXPECT_NE((uint8_t*)nullptr, data1);
29 EXPECT_NE((uint8_t*)nullptr, data2);
30
31 // Initialize the data
32 for (unsigned i = 0; i < 256; ++i) {
33 code1[i] = 1;
34 code2[i] = 2;
35 data1[i] = 3;
36 data2[i] = 4;
37 }
38
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]);
45 }
46
47 std::string Error;
48 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
49 }
50
TEST(MCJITMemoryManagerTest,LargeAllocations)51 TEST(MCJITMemoryManagerTest, LargeAllocations) {
52 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
53
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);
58
59 EXPECT_NE((uint8_t*)nullptr, code1);
60 EXPECT_NE((uint8_t*)nullptr, code2);
61 EXPECT_NE((uint8_t*)nullptr, data1);
62 EXPECT_NE((uint8_t*)nullptr, data2);
63
64 // Initialize the data
65 for (unsigned i = 0; i < 0x100000; ++i) {
66 code1[i] = 1;
67 code2[i] = 2;
68 data1[i] = 3;
69 data2[i] = 4;
70 }
71
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]);
78 }
79
80 std::string Error;
81 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
82 }
83
TEST(MCJITMemoryManagerTest,ManyAllocations)84 TEST(MCJITMemoryManagerTest, ManyAllocations) {
85 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
86
87 uint8_t* code[10000];
88 uint8_t* data[10000];
89
90 for (unsigned i = 0; i < 10000; ++i) {
91 const bool isReadOnly = i % 2 == 0;
92
93 code[i] = MemMgr->allocateCodeSection(32, 0, 1, "");
94 data[i] = MemMgr->allocateDataSection(32, 0, 2, "", isReadOnly);
95
96 for (unsigned j = 0; j < 32; j++) {
97 code[i][j] = 1 + (i % 254);
98 data[i][j] = 2 + (i % 254);
99 }
100
101 EXPECT_NE((uint8_t *)nullptr, code[i]);
102 EXPECT_NE((uint8_t *)nullptr, data[i]);
103 }
104
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]);
112 }
113 }
114
115 std::string Error;
116 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
117 }
118
TEST(MCJITMemoryManagerTest,ManyVariedAllocations)119 TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {
120 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
121
122 uint8_t* code[10000];
123 uint8_t* data[10000];
124
125 for (unsigned i = 0; i < 10000; ++i) {
126 uintptr_t CodeSize = i % 16 + 1;
127 uintptr_t DataSize = i % 8 + 1;
128
129 bool isReadOnly = i % 3 == 0;
130 unsigned Align = 8 << (i % 4);
131
132 code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i, "");
133 data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000, "",
134 isReadOnly);
135
136 for (unsigned j = 0; j < CodeSize; j++) {
137 code[i][j] = 1 + (i % 254);
138 }
139
140 for (unsigned j = 0; j < DataSize; j++) {
141 data[i][j] = 2 + (i % 254);
142 }
143
144 EXPECT_NE((uint8_t *)nullptr, code[i]);
145 EXPECT_NE((uint8_t *)nullptr, data[i]);
146
147 uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;
148 uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;
149
150 EXPECT_EQ((uintptr_t)0, CodeAlign);
151 EXPECT_EQ((uintptr_t)0, DataAlign);
152 }
153
154 for (unsigned i = 0; i < 10000; ++i) {
155 uintptr_t CodeSize = i % 16 + 1;
156 uintptr_t DataSize = i % 8 + 1;
157
158 for (unsigned j = 0; j < CodeSize; j++) {
159 uint8_t ExpectedCode = 1 + (i % 254);
160 EXPECT_EQ(ExpectedCode, code[i][j]);
161 }
162
163 for (unsigned j = 0; j < DataSize; j++) {
164 uint8_t ExpectedData = 2 + (i % 254);
165 EXPECT_EQ(ExpectedData, data[i][j]);
166 }
167 }
168 }
169
170 } // Namespace
171
172