ab09acad0d3b65c6366a44c79eb1302071078bb8
[oota-llvm.git] / unittests / ExecutionEngine / MCJIT / MCJITMemoryManagerTest.cpp
1 //===- MCJITMemoryManagerTest.cpp - Unit tests for the JIT memory manager -===//\r
2 //\r
3 //                     The LLVM Compiler Infrastructure\r
4 //\r
5 // This file is distributed under the University of Illinois Open Source\r
6 // License. See LICENSE.TXT for details.\r
7 //\r
8 //===----------------------------------------------------------------------===//\r
9 \r
10 #include "llvm/ExecutionEngine/SectionMemoryManager.h"\r
11 #include "llvm/ADT/OwningPtr.h"\r
12 #include "llvm/ExecutionEngine/JIT.h"\r
13 #include "gtest/gtest.h"\r
14 \r
15 using namespace llvm;\r
16 \r
17 namespace {\r
18 \r
19 TEST(MCJITMemoryManagerTest, BasicAllocations) {\r
20   OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
21 \r
22   uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);\r
23   uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, true);\r
24   uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3);\r
25   uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, false);\r
26 \r
27   EXPECT_NE((uint8_t*)0, code1);\r
28   EXPECT_NE((uint8_t*)0, code2);\r
29   EXPECT_NE((uint8_t*)0, data1);\r
30   EXPECT_NE((uint8_t*)0, data2);\r
31 \r
32   // Initialize the data\r
33   for (unsigned i = 0; i < 256; ++i) {\r
34     code1[i] = 1;\r
35     code2[i] = 2;\r
36     data1[i] = 3;\r
37     data2[i] = 4;\r
38   }\r
39 \r
40   // Verify the data (this is checking for overlaps in the addresses)\r
41   for (unsigned i = 0; i < 256; ++i) {\r
42     EXPECT_EQ(1, code1[i]);\r
43     EXPECT_EQ(2, code2[i]);\r
44     EXPECT_EQ(3, data1[i]);\r
45     EXPECT_EQ(4, data2[i]);\r
46   }\r
47 \r
48   std::string Error;\r
49   EXPECT_FALSE(MemMgr->applyPermissions(&Error));\r
50 }\r
51 \r
52 TEST(MCJITMemoryManagerTest, LargeAllocations) {\r
53   OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
54 \r
55   uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1);\r
56   uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, true);\r
57   uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3);\r
58   uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, false);\r
59 \r
60   EXPECT_NE((uint8_t*)0, code1);\r
61   EXPECT_NE((uint8_t*)0, code2);\r
62   EXPECT_NE((uint8_t*)0, data1);\r
63   EXPECT_NE((uint8_t*)0, data2);\r
64 \r
65   // Initialize the data\r
66   for (unsigned i = 0; i < 0x100000; ++i) {\r
67     code1[i] = 1;\r
68     code2[i] = 2;\r
69     data1[i] = 3;\r
70     data2[i] = 4;\r
71   }\r
72 \r
73   // Verify the data (this is checking for overlaps in the addresses)\r
74   for (unsigned i = 0; i < 0x100000; ++i) {\r
75     EXPECT_EQ(1, code1[i]);\r
76     EXPECT_EQ(2, code2[i]);\r
77     EXPECT_EQ(3, data1[i]);\r
78     EXPECT_EQ(4, data2[i]);\r
79   }\r
80 \r
81   std::string Error;\r
82   EXPECT_FALSE(MemMgr->applyPermissions(&Error));\r
83 }\r
84 \r
85 TEST(MCJITMemoryManagerTest, ManyAllocations) {\r
86   OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
87 \r
88   uint8_t* code[10000];\r
89   uint8_t* data[10000];\r
90 \r
91   for (unsigned i = 0; i < 10000; ++i) {\r
92     const bool isReadOnly = i % 2 == 0;\r
93 \r
94     code[i] = MemMgr->allocateCodeSection(32, 0, 1);\r
95     data[i] = MemMgr->allocateDataSection(32, 0, 2, isReadOnly);\r
96 \r
97     for (unsigned j = 0; j < 32; j++) {\r
98       code[i][j] = 1 + (i % 254);\r
99       data[i][j] = 2 + (i % 254);\r
100     }\r
101 \r
102     EXPECT_NE((uint8_t *)0, code[i]);\r
103     EXPECT_NE((uint8_t *)0, data[i]);\r
104   }\r
105 \r
106   // Verify the data (this is checking for overlaps in the addresses)\r
107   for (unsigned i = 0; i < 10000; ++i) {\r
108     for (unsigned j = 0; j < 32;j++ ) {\r
109       uint8_t ExpectedCode = 1 + (i % 254);\r
110       uint8_t ExpectedData = 2 + (i % 254);\r
111       EXPECT_EQ(ExpectedCode, code[i][j]);\r
112       EXPECT_EQ(ExpectedData, data[i][j]);\r
113     }\r
114   }\r
115 \r
116   std::string Error;\r
117   EXPECT_FALSE(MemMgr->applyPermissions(&Error));\r
118 }\r
119 \r
120 TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {\r
121   OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());\r
122 \r
123   uint8_t* code[10000];\r
124   uint8_t* data[10000];\r
125 \r
126   for (unsigned i = 0; i < 10000; ++i) {\r
127     uintptr_t CodeSize = i % 16 + 1;\r
128     uintptr_t DataSize = i % 8 + 1;\r
129 \r
130     bool isReadOnly = i % 3 == 0;\r
131     unsigned Align = 8 << (i % 4);\r
132 \r
133     code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i);\r
134     data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000,\r
135                                           isReadOnly);\r
136 \r
137     for (unsigned j = 0; j < CodeSize; j++) {\r
138       code[i][j] = 1 + (i % 254);\r
139     }\r
140 \r
141     for (unsigned j = 0; j < DataSize; j++) {\r
142       data[i][j] = 2 + (i % 254);\r
143     }\r
144 \r
145     EXPECT_NE((uint8_t *)0, code[i]);\r
146     EXPECT_NE((uint8_t *)0, data[i]);\r
147 \r
148     uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;\r
149     uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;\r
150 \r
151     EXPECT_EQ((uintptr_t)0, CodeAlign);\r
152     EXPECT_EQ((uintptr_t)0, DataAlign);\r
153   }\r
154 \r
155   for (unsigned i = 0; i < 10000; ++i) {\r
156     uintptr_t CodeSize = i % 16 + 1;\r
157     uintptr_t DataSize = i % 8 + 1;\r
158 \r
159     for (unsigned j = 0; j < CodeSize; j++) {\r
160       uint8_t ExpectedCode = 1 + (i % 254);\r
161       EXPECT_EQ(ExpectedCode, code[i][j]);\r
162     }\r
163 \r
164     for (unsigned j = 0; j < DataSize; j++) {\r
165       uint8_t ExpectedData = 2 + (i % 254);\r
166       EXPECT_EQ(ExpectedData, data[i][j]); \r
167     }\r
168   }\r
169 }\r
170 \r
171 } // Namespace\r
172 \r