MCJITMemoryManagerTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "llvm/ExecutionEngine/SectionMemoryManager.h"
  10. #include "llvm/ADT/OwningPtr.h"
  11. #include "llvm/ExecutionEngine/JIT.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. namespace {
  15. TEST(MCJITMemoryManagerTest, BasicAllocations) {
  16. OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
  17. uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1);
  18. uint8_t *data1 = MemMgr->allocateDataSection(256, 0, 2, true);
  19. uint8_t *code2 = MemMgr->allocateCodeSection(256, 0, 3);
  20. uint8_t *data2 = MemMgr->allocateDataSection(256, 0, 4, false);
  21. EXPECT_NE((uint8_t*)0, code1);
  22. EXPECT_NE((uint8_t*)0, code2);
  23. EXPECT_NE((uint8_t*)0, data1);
  24. EXPECT_NE((uint8_t*)0, data2);
  25. // Initialize the data
  26. for (unsigned i = 0; i < 256; ++i) {
  27. code1[i] = 1;
  28. code2[i] = 2;
  29. data1[i] = 3;
  30. data2[i] = 4;
  31. }
  32. // Verify the data (this is checking for overlaps in the addresses)
  33. for (unsigned i = 0; i < 256; ++i) {
  34. EXPECT_EQ(1, code1[i]);
  35. EXPECT_EQ(2, code2[i]);
  36. EXPECT_EQ(3, data1[i]);
  37. EXPECT_EQ(4, data2[i]);
  38. }
  39. std::string Error;
  40. EXPECT_FALSE(MemMgr->applyPermissions(&Error));
  41. }
  42. TEST(MCJITMemoryManagerTest, LargeAllocations) {
  43. OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
  44. uint8_t *code1 = MemMgr->allocateCodeSection(0x100000, 0, 1);
  45. uint8_t *data1 = MemMgr->allocateDataSection(0x100000, 0, 2, true);
  46. uint8_t *code2 = MemMgr->allocateCodeSection(0x100000, 0, 3);
  47. uint8_t *data2 = MemMgr->allocateDataSection(0x100000, 0, 4, false);
  48. EXPECT_NE((uint8_t*)0, code1);
  49. EXPECT_NE((uint8_t*)0, code2);
  50. EXPECT_NE((uint8_t*)0, data1);
  51. EXPECT_NE((uint8_t*)0, data2);
  52. // Initialize the data
  53. for (unsigned i = 0; i < 0x100000; ++i) {
  54. code1[i] = 1;
  55. code2[i] = 2;
  56. data1[i] = 3;
  57. data2[i] = 4;
  58. }
  59. // Verify the data (this is checking for overlaps in the addresses)
  60. for (unsigned i = 0; i < 0x100000; ++i) {
  61. EXPECT_EQ(1, code1[i]);
  62. EXPECT_EQ(2, code2[i]);
  63. EXPECT_EQ(3, data1[i]);
  64. EXPECT_EQ(4, data2[i]);
  65. }
  66. std::string Error;
  67. EXPECT_FALSE(MemMgr->applyPermissions(&Error));
  68. }
  69. TEST(MCJITMemoryManagerTest, ManyAllocations) {
  70. OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
  71. uint8_t* code[10000];
  72. uint8_t* data[10000];
  73. for (unsigned i = 0; i < 10000; ++i) {
  74. const bool isReadOnly = i % 2 == 0;
  75. code[i] = MemMgr->allocateCodeSection(32, 0, 1);
  76. data[i] = MemMgr->allocateDataSection(32, 0, 2, isReadOnly);
  77. for (unsigned j = 0; j < 32; j++) {
  78. code[i][j] = 1 + (i % 254);
  79. data[i][j] = 2 + (i % 254);
  80. }
  81. EXPECT_NE((uint8_t *)0, code[i]);
  82. EXPECT_NE((uint8_t *)0, data[i]);
  83. }
  84. // Verify the data (this is checking for overlaps in the addresses)
  85. for (unsigned i = 0; i < 10000; ++i) {
  86. for (unsigned j = 0; j < 32;j++ ) {
  87. uint8_t ExpectedCode = 1 + (i % 254);
  88. uint8_t ExpectedData = 2 + (i % 254);
  89. EXPECT_EQ(ExpectedCode, code[i][j]);
  90. EXPECT_EQ(ExpectedData, data[i][j]);
  91. }
  92. }
  93. std::string Error;
  94. EXPECT_FALSE(MemMgr->applyPermissions(&Error));
  95. }
  96. TEST(MCJITMemoryManagerTest, ManyVariedAllocations) {
  97. OwningPtr<SectionMemoryManager> MemMgr(new SectionMemoryManager());
  98. uint8_t* code[10000];
  99. uint8_t* data[10000];
  100. for (unsigned i = 0; i < 10000; ++i) {
  101. uintptr_t CodeSize = i % 16 + 1;
  102. uintptr_t DataSize = i % 8 + 1;
  103. bool isReadOnly = i % 3 == 0;
  104. unsigned Align = 8 << (i % 4);
  105. code[i] = MemMgr->allocateCodeSection(CodeSize, Align, i);
  106. data[i] = MemMgr->allocateDataSection(DataSize, Align, i + 10000,
  107. isReadOnly);
  108. for (unsigned j = 0; j < CodeSize; j++) {
  109. code[i][j] = 1 + (i % 254);
  110. }
  111. for (unsigned j = 0; j < DataSize; j++) {
  112. data[i][j] = 2 + (i % 254);
  113. }
  114. EXPECT_NE((uint8_t *)0, code[i]);
  115. EXPECT_NE((uint8_t *)0, data[i]);
  116. uintptr_t CodeAlign = Align ? (uintptr_t)code[i] % Align : 0;
  117. uintptr_t DataAlign = Align ? (uintptr_t)data[i] % Align : 0;
  118. EXPECT_EQ((uintptr_t)0, CodeAlign);
  119. EXPECT_EQ((uintptr_t)0, DataAlign);
  120. }
  121. for (unsigned i = 0; i < 10000; ++i) {
  122. uintptr_t CodeSize = i % 16 + 1;
  123. uintptr_t DataSize = i % 8 + 1;
  124. for (unsigned j = 0; j < CodeSize; j++) {
  125. uint8_t ExpectedCode = 1 + (i % 254);
  126. EXPECT_EQ(ExpectedCode, code[i][j]);
  127. }
  128. for (unsigned j = 0; j < DataSize; j++) {
  129. uint8_t ExpectedData = 2 + (i % 254);
  130. EXPECT_EQ(ExpectedData, data[i][j]);
  131. }
  132. }
  133. }
  134. } // Namespace