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/ADT/OwningPtr.h"
12 #include "llvm/ExecutionEngine/JIT.h"
13 #include "gtest/gtest.h"
19 TEST(MCJITMemoryManagerTest, BasicAllocations) {
20 OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
22 uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, "");
23 uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, "", true);
24 uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3, "");
25 uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, "", false);
27 EXPECT_NE((uint8_t*)0, code1);
28 EXPECT_NE((uint8_t*)0, code2);
29 EXPECT_NE((uint8_t*)0, data1);
30 EXPECT_NE((uint8_t*)0, data2);
32 // Initialize the data
33 for (unsigned i = 0; i < 256; ++i) {
40 // Verify the data (this is checking for overlaps in the addresses)
41 for (unsigned i = 0; i < 256; ++i) {
42 EXPECT_EQ(1, code1[i]);
43 EXPECT_EQ(2, code2[i]);
44 EXPECT_EQ(3, data1[i]);
45 EXPECT_EQ(4, data2[i]);
49 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
52 TEST(MCJITMemoryManagerTest, LargeAllocations) {
53 OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
55 uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1, "");
56 uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, "", true);
57 uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3, "");
58 uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, "", false);
60 EXPECT_NE((uint8_t*)0, code1);
61 EXPECT_NE((uint8_t*)0, code2);
62 EXPECT_NE((uint8_t*)0, data1);
63 EXPECT_NE((uint8_t*)0, data2);
65 // Initialize the data
66 for (unsigned i = 0; i < 0x100000; ++i) {
73 // Verify the data (this is checking for overlaps in the addresses)
74 for (unsigned i = 0; i < 0x100000; ++i) {
75 EXPECT_EQ(1, code1[i]);
76 EXPECT_EQ(2, code2[i]);
77 EXPECT_EQ(3, data1[i]);
78 EXPECT_EQ(4, data2[i]);
82 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
85 TEST(MCJITMemoryManagerTest, ManyAllocations) {
86 OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
91 for (unsigned i = 0; i < 10000; ++i) {
92 const bool isReadOnly = i % 2 == 0;
94 code[i] = MemMgr->allocateCodeSection(32, 0, 1, "");
95 data[i] = MemMgr->allocateDataSection(32, 0, 2, "", isReadOnly);
97 for (unsigned j = 0; j < 32; j++) {
98 code[i][j] = 1 + (i % 254);
99 data[i][j] = 2 + (i % 254);
102 EXPECT_NE((uint8_t *)0, code[i]);
103 EXPECT_NE((uint8_t *)0, data[i]);
106 // Verify the data (this is checking for overlaps in the addresses)
107 for (unsigned i = 0; i < 10000; ++i) {
108 for (unsigned j = 0; j < 32;j++ ) {
109 uint8_t ExpectedCode = 1 + (i % 254);
110 uint8_t ExpectedData = 2 + (i % 254);
111 EXPECT_EQ(ExpectedCode, code[i][j]);
112 EXPECT_EQ(ExpectedData, data[i][j]);
117 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
120 TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {
121 OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
123 uint8_t* code[10000];
124 uint8_t* data[10000];
126 for (unsigned i = 0; i < 10000; ++i) {
127 uintptr_t CodeSize = i % 16 + 1;
128 uintptr_t DataSize = i % 8 + 1;
130 bool isReadOnly = i % 3 == 0;
131 unsigned Align = 8 << (i % 4);
133 code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i, "");
134 data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000, "",
137 for (unsigned j = 0; j < CodeSize; j++) {
138 code[i][j] = 1 + (i % 254);
141 for (unsigned j = 0; j < DataSize; j++) {
142 data[i][j] = 2 + (i % 254);
145 EXPECT_NE((uint8_t *)0, code[i]);
146 EXPECT_NE((uint8_t *)0, data[i]);
148 uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;
149 uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;
151 EXPECT_EQ((uintptr_t)0, CodeAlign);
152 EXPECT_EQ((uintptr_t)0, DataAlign);
155 for (unsigned i = 0; i < 10000; ++i) {
156 uintptr_t CodeSize = i % 16 + 1;
157 uintptr_t DataSize = i % 8 + 1;
159 for (unsigned j = 0; j < CodeSize; j++) {
160 uint8_t ExpectedCode = 1 + (i % 254);
161 EXPECT_EQ(ExpectedCode, code[i][j]);
164 for (unsigned j = 0; j < DataSize; j++) {
165 uint8_t ExpectedData = 2 + (i % 254);
166 EXPECT_EQ(ExpectedData, data[i][j]);