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