SafePointer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2024 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "SafePointer.h"
  24. #include "../api/stringAPI.h"
  25. #ifdef SAFE_POINTER_CHECKS
  26. #include <thread>
  27. #include <mutex>
  28. #endif
  29. using namespace dsr;
  30. // Thread hash of memory without any specific owner.
  31. static uint64_t ANY_THREAD_HASH = 0xF986BA1496E872A5;
  32. #ifdef SAFE_POINTER_CHECKS
  33. // A primitive hash function that assumes that all compared objects have the same length, so that trailing zeroes can be ignored.
  34. static uint64_t hash(const uint8_t *bytes, size_t size) {
  35. uint64_t result = 527950984572370412;
  36. uint64_t a = 701348790128743674;
  37. uint64_t b = 418235620918472195;
  38. uint64_t c = 405871623857064987;
  39. uint64_t d = 685601283756306982;
  40. uint64_t e = 560123876058723749;
  41. uint64_t f = 123875604857293847;
  42. uint64_t g = 906123857648761038;
  43. uint64_t h = 720862395187683741;
  44. for (size_t byteIndex = 0; byteIndex < size; byteIndex++) {
  45. uint8_t byte = bytes[byteIndex];
  46. a = (a * 5819 + byteIndex * 75364 + 1746983) ^ 8761236358;
  47. b = (b * 4870 + byteIndex * 64294 + 6891364) ^ 2346987034;
  48. c = (c * 7059 + byteIndex * 91724 + 9234068) ^ 8016458371;
  49. d = (d * 2987 + byteIndex * 35729 + 5298712) ^ 1589721358;
  50. e = (e * 6198 + byteIndex * 11635 + 6349823) ^ 2938479216;
  51. f = (f * 5613 + byteIndex * 31873 + 7468895) ^ 5368713452;
  52. g = (g * 7462 + byteIndex * 98271 + 1287650) ^ 9120572938;
  53. h = (h * 1670 + byteIndex * 37488 + 6361083) ^ 4867350662;
  54. if (byte & 1) result = result ^ a;
  55. if (byte & 2) result = result ^ b;
  56. if (byte & 4) result = result ^ c;
  57. if (byte & 8) result = result ^ d;
  58. if (byte & 16) result = result ^ e;
  59. if (byte & 32) result = result ^ f;
  60. if (byte & 64) result = result ^ g;
  61. if (byte & 128) result = result ^ h;
  62. }
  63. return result;
  64. }
  65. // Hashed thread identity.
  66. static uint64_t createThreadHash() {
  67. std::thread::id id = std::this_thread::get_id();
  68. const uint8_t *bytes = (const uint8_t*)&id;
  69. return hash(bytes, sizeof(std::thread::id));
  70. }
  71. thread_local const uint64_t currentThreadHash = createThreadHash();
  72. // Globally unique identifiers for memory allocations.
  73. // Different allocations can have the same address at different times when allocations are recycled,
  74. // so a globally unique identifier is needed to make sure that we access the same allocation.
  75. // We start at a constant of high entropy to minimize the risk of accidental matches and then increase by one in modulo 2⁶⁴ to prevent repetition of the exact same value.
  76. static std::mutex idLock;
  77. static uint64_t idCounter = 0xD13A98271E08BF57;
  78. static uint64_t createIdentity() {
  79. uint64_t result;
  80. idLock.lock();
  81. result = idCounter;
  82. idCounter++;
  83. idLock.unlock();
  84. return result;
  85. }
  86. AllocationHeader::AllocationHeader()
  87. : totalSize(0), threadHash(0), allocationIdentity(0) {}
  88. AllocationHeader::AllocationHeader(uintptr_t totalSize, bool threadLocal)
  89. : totalSize(totalSize), threadHash(threadLocal ? currentThreadHash : ANY_THREAD_HASH), allocationIdentity(createIdentity()) {}
  90. #else
  91. AllocationHeader::AllocationHeader()
  92. : totalSize(0) {}
  93. AllocationHeader::AllocationHeader(uintptr_t totalSize, bool threadLocal)
  94. : totalSize(totalSize) {}
  95. #endif
  96. #ifdef SAFE_POINTER_CHECKS
  97. void dsr::assertNonNegativeSize(intptr_t size) {
  98. if (size < 0) {
  99. throwError(U"Negative size of SafePointer!\n");
  100. }
  101. }
  102. void dsr::assertInsideSafePointer(const char* method, const char* name, const uint8_t* pointer, const uint8_t* data, const uint8_t* regionStart, const uint8_t* regionEnd, const AllocationHeader *header, uint64_t allocationIdentity, intptr_t claimedSize, intptr_t elementSize) {
  103. if (regionStart == nullptr) {
  104. throwError(U"SafePointer exception! Tried to use a null pointer!\n");
  105. return;
  106. }
  107. // If the pointer has an allocation header, check that the identity matches the one stored in the pointer.
  108. if (header != nullptr) {
  109. uint64_t headerIdentity, headerHash;
  110. try {
  111. // Both allocation identity and thread hash may match by mistake, but in most of the cases this will give more information about why it happened.
  112. headerIdentity = header->allocationIdentity;
  113. headerHash = header->threadHash;
  114. } catch(...) {
  115. throwError(U"SafePointer exception! Tried to access memory not available to the application!\n");
  116. return;
  117. }
  118. if (headerIdentity != allocationIdentity) {
  119. throwError(U"SafePointer exception! Accessing freed memory or corrupted allocation header!\n headerIdentity = ", headerIdentity, U"\n allocationIdentity = ", allocationIdentity, U"");
  120. return;
  121. } else if (headerHash != ANY_THREAD_HASH && headerHash != currentThreadHash) {
  122. throwError(U"SafePointer exception! Accessing another thread's private memory!\n headerHash = ", headerHash, U"\n currentThreadHash = ", currentThreadHash, U"\n");
  123. return;
  124. }
  125. }
  126. if (pointer < regionStart || pointer + claimedSize > regionEnd) {
  127. String message;
  128. string_append(message, U"\n _________________ SafePointer out of bound exception! _________________\n");
  129. string_append(message, U"/\n");
  130. string_append(message, U"| Name: ", name, U"\n");
  131. string_append(message, U"| Method: ", method, U"\n");
  132. string_append(message, U"| Region: ", (uintptr_t)regionStart, U" to ", (uintptr_t)regionEnd, U"\n");
  133. string_append(message, U"| Region size: ", (intptr_t)(regionEnd - regionStart), U" bytes\n");
  134. string_append(message, U"| Base pointer: ", (uintptr_t)data, U"\n");
  135. string_append(message, U"| Requested pointer: ", (uintptr_t)pointer, U"\n");
  136. string_append(message, U"| Requested size: ", claimedSize, U" bytes\n");
  137. intptr_t startOffset = (intptr_t)pointer - (intptr_t)regionStart;
  138. intptr_t baseOffset = (intptr_t)pointer - (intptr_t)data;
  139. // Index relative to allocation start
  140. // regionStart is the start of the accessible memory region
  141. if (startOffset != baseOffset) {
  142. string_append(message, U"| Start offset: ", startOffset, U" bytes\n");
  143. if (startOffset % elementSize == 0) {
  144. intptr_t index = startOffset / elementSize;
  145. intptr_t elementCount = ((intptr_t)regionEnd - (intptr_t)regionStart) / elementSize;
  146. string_append(message, U"| Start index: ", index, U" [0..", (elementCount - 1), U"]\n");
  147. }
  148. }
  149. // Base index relative to the stored pointer within the region
  150. // data is the base of the allocation at index zero
  151. string_append(message, U"| Base offset: ", baseOffset, U" bytes\n");
  152. if (baseOffset % elementSize == 0) {
  153. intptr_t index = baseOffset / elementSize;
  154. intptr_t elementCount = ((intptr_t)regionEnd - (intptr_t)data) / elementSize;
  155. string_append(message, U"| Base index: ", index, U" [0..", (elementCount - 1), U"]\n");
  156. }
  157. string_append(message, U"\\_______________________________________________________________________\n\n");
  158. throwError(message);
  159. return;
  160. }
  161. }
  162. #endif