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