[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / unittests / ExecutionEngine / MCJIT / MCJITMemoryManagerTest.cpp
blob7a756a707160adbd2a97ed5d0da08d6d72395018
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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
16 TEST(MCJITMemoryManagerTest, BasicAllocations) {
17 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
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);
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);
29 // Initialize the data
30 for (unsigned i = 0; i < 256; ++i) {
31 code1[i] = 1;
32 code2[i] = 2;
33 data1[i] = 3;
34 data2[i] = 4;
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]);
45 std::string Error;
46 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
49 TEST(MCJITMemoryManagerTest, LargeAllocations) {
50 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
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);
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);
62 // Initialize the data
63 for (unsigned i = 0; i < 0x100000; ++i) {
64 code1[i] = 1;
65 code2[i] = 2;
66 data1[i] = 3;
67 data2[i] = 4;
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]);
78 std::string Error;
79 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
82 TEST(MCJITMemoryManagerTest, ManyAllocations) {
83 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
85 uint8_t* code[10000];
86 uint8_t* data[10000];
88 for (unsigned i = 0; i < 10000; ++i) {
89 const bool isReadOnly = i % 2 == 0;
91 code[i] = MemMgr->allocateCodeSection(32, 0, 1, "");
92 data[i] = MemMgr->allocateDataSection(32, 0, 2, "", isReadOnly);
94 for (unsigned j = 0; j < 32; j++) {
95 code[i][j] = 1 + (i % 254);
96 data[i][j] = 2 + (i % 254);
99 EXPECT_NE((uint8_t *)nullptr, code[i]);
100 EXPECT_NE((uint8_t *)nullptr, data[i]);
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]);
113 std::string Error;
114 EXPECT_FALSE(MemMgr->finalizeMemory(&Error));
117 TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {
118 std::unique_ptr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
120 uint8_t* code[10000];
121 uint8_t* data[10000];
123 for (unsigned i = 0; i < 10000; ++i) {
124 uintptr_t CodeSize = i % 16 + 1;
125 uintptr_t DataSize = i % 8 + 1;
127 bool isReadOnly = i % 3 == 0;
128 unsigned Align = 8 << (i % 4);
130 code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i, "");
131 data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000, "",
132 isReadOnly);
134 for (unsigned j = 0; j < CodeSize; j++) {
135 code[i][j] = 1 + (i % 254);
138 for (unsigned j = 0; j < DataSize; j++) {
139 data[i][j] = 2 + (i % 254);
142 EXPECT_NE((uint8_t *)nullptr, code[i]);
143 EXPECT_NE((uint8_t *)nullptr, data[i]);
145 uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;
146 uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;
148 EXPECT_EQ((uintptr_t)0, CodeAlign);
149 EXPECT_EQ((uintptr_t)0, DataAlign);
152 for (unsigned i = 0; i < 10000; ++i) {
153 uintptr_t CodeSize = i % 16 + 1;
154 uintptr_t DataSize = i % 8 + 1;
156 for (unsigned j = 0; j < CodeSize; j++) {
157 uint8_t ExpectedCode = 1 + (i % 254);
158 EXPECT_EQ(ExpectedCode, code[i][j]);
161 for (unsigned j = 0; j < DataSize; j++) {
162 uint8_t ExpectedData = 2 + (i % 254);
163 EXPECT_EQ(ExpectedData, data[i][j]);
168 } // Namespace