SafePointer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "../settings.h"
  26. #ifdef SAFE_POINTER_CHECKS
  27. #include <thread>
  28. #include <mutex>
  29. #endif
  30. using namespace dsr;
  31. #ifdef SAFE_POINTER_CHECKS
  32. // Thread hash of memory without any specific owner.
  33. static uint64_t ANY_THREAD_HASH = 0xF986BA1496E872A5;
  34. // A primitive hash function that assumes that all compared objects have the same length, so that trailing zeroes can be ignored.
  35. static uint64_t hash(const uint8_t *bytes, size_t size) {
  36. uint64_t result = 527950984572370412;
  37. uint64_t a = 701348790128743674;
  38. uint64_t b = 418235620918472195;
  39. uint64_t c = 405871623857064987;
  40. uint64_t d = 685601283756306982;
  41. uint64_t e = 560123876058723749;
  42. uint64_t f = 123875604857293847;
  43. uint64_t g = 906123857648761038;
  44. uint64_t h = 720862395187683741;
  45. for (size_t byteIndex = 0; byteIndex < size; byteIndex++) {
  46. uint8_t byte = bytes[byteIndex];
  47. a = (a * 5819 + byteIndex * 75364 + 1746983) ^ 8761236358;
  48. b = (b * 4870 + byteIndex * 64294 + 6891364) ^ 2346987034;
  49. c = (c * 7059 + byteIndex * 91724 + 9234068) ^ 8016458371;
  50. d = (d * 2987 + byteIndex * 35729 + 5298712) ^ 1589721358;
  51. e = (e * 6198 + byteIndex * 11635 + 6349823) ^ 2938479216;
  52. f = (f * 5613 + byteIndex * 31873 + 7468895) ^ 5368713452;
  53. g = (g * 7462 + byteIndex * 98271 + 1287650) ^ 9120572938;
  54. h = (h * 1670 + byteIndex * 37488 + 6361083) ^ 4867350662;
  55. if (byte & 1) result = result ^ a;
  56. if (byte & 2) result = result ^ b;
  57. if (byte & 4) result = result ^ c;
  58. if (byte & 8) result = result ^ d;
  59. if (byte & 16) result = result ^ e;
  60. if (byte & 32) result = result ^ f;
  61. if (byte & 64) result = result ^ g;
  62. if (byte & 128) result = result ^ h;
  63. }
  64. return result;
  65. }
  66. // Hashed thread identity.
  67. static uint64_t createThreadHash() {
  68. std::thread::id id = std::this_thread::get_id();
  69. const uint8_t *bytes = (const uint8_t*)&id;
  70. return hash(bytes, sizeof(std::thread::id));
  71. }
  72. thread_local const uint64_t currentThreadHash = createThreadHash();
  73. // Globally unique identifiers for memory allocations.
  74. // Different allocations can have the same address at different times when allocations are recycled,
  75. // so a globally unique identifier is needed to make sure that we access the same allocation.
  76. // 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.
  77. static std::mutex idLock;
  78. static uint64_t idCounter = 0xD13A98271E08BF57;
  79. static uint64_t createIdentity() {
  80. uint64_t result;
  81. idLock.lock();
  82. result = idCounter;
  83. idCounter++;
  84. idLock.unlock();
  85. return result;
  86. }
  87. AllocationHeader::AllocationHeader()
  88. : totalSize(0), threadHash(0), allocationIdentity(0) {}
  89. AllocationHeader::AllocationHeader(uintptr_t totalSize, bool threadLocal, const char *name)
  90. : totalSize(totalSize), name(name), threadHash(threadLocal ? currentThreadHash : ANY_THREAD_HASH), allocationIdentity(createIdentity()) {}
  91. void AllocationHeader::reuse(bool threadLocal, const char *name) {
  92. this->threadHash = threadLocal ? currentThreadHash : ANY_THREAD_HASH;
  93. this->allocationIdentity = createIdentity();
  94. this->name = name;
  95. }
  96. #else
  97. AllocationHeader::AllocationHeader()
  98. : totalSize(0) {}
  99. // TODO: Avoid passing the debug name in release mode by placing these functions in memory.h.
  100. // Create separate methods for getting the thread hash and the next allocation nonce.
  101. AllocationHeader::AllocationHeader(uintptr_t totalSize, bool threadLocal, const char *name)
  102. : totalSize(totalSize) {}
  103. void AllocationHeader::reuse(bool threadLocal, const char *name) {}
  104. #endif
  105. #ifdef SAFE_POINTER_CHECKS
  106. void dsr::impl_assertNonNegativeSize(intptr_t size) {
  107. if (size < 0) {
  108. throwError(U"Negative size of SafePointer!\n");
  109. }
  110. }
  111. static bool isOutOfBound(const uint8_t* claimedStart, const uint8_t* claimedEnd, const uint8_t* permittedStart, const uint8_t* permittedEnd) {
  112. return claimedStart < permittedStart || claimedEnd > permittedEnd;
  113. }
  114. static void throwPointerError(const ReadableString &title, const char* methodName, const char* pointerName, const FixedAscii<256> &allocationName, const uint8_t* claimedStart, const uint8_t* claimedEnd, intptr_t elementSize, const uint8_t* permittedStart, const uint8_t* permittedEnd, const AllocationHeader *pointerHeader, uint64_t allocationIdentity, uint64_t headerIdentity, uint64_t headerHash) {
  115. bool outOfBound = isOutOfBound(claimedStart, claimedEnd, permittedStart, permittedEnd);
  116. String *target = &(string_getPrintBuffer());
  117. string_clear(*target);
  118. string_append(*target, title, U"\n");
  119. string_append(*target, U" _______________________________________________________________________\n");
  120. string_append(*target, U"/\n");
  121. string_append(*target, U"| SafePointer operation: ", methodName, U"\n");
  122. string_append(*target, U"| Pointer name: ", pointerName, U"\n");
  123. #ifdef EXTRA_SAFE_POINTER_CHECKS
  124. if (pointerHeader != nullptr) {
  125. string_append(*target, U"| Allocation name : ", allocationName, U"\n");
  126. string_append(*target, U"| Thread hash:\n");
  127. if (headerHash == ANY_THREAD_HASH) {
  128. string_append(*target, U"| Shared with all threads\n");
  129. } else {
  130. string_append(*target, U"| Owner thread : ", headerHash, U"\n");
  131. string_append(*target, U"| Calling thread : ", currentThreadHash, U"\n");
  132. }
  133. string_append(*target, U"| Identity:\n");
  134. string_append(*target, U"| Found : ", headerIdentity, U"\n");
  135. string_append(*target, U"| Expected : ", allocationIdentity, U"\n");
  136. // TODO: Check if the requested data is outside of the memory allocation's used size or just the permitted region within the allocation.
  137. // TODO: Iterate over allocations using until the same header address as in the pointer is found:
  138. heap_forAllHeapAllocations([target, pointerHeader](AllocationHeader * header, void * allocation) {
  139. // We found the allocation in the heap, so we know that it is an active heap allocation.
  140. if (pointerHeader == header) {
  141. // The allocation size is the space that can be expanded into without having to reallocate.
  142. string_append(*target, U"| Allocation size : ", heap_getAllocationSize(allocation), U" bytes\n");
  143. // The used size is what the application asked for from the allocator.
  144. // The permissed region often include the whole used size and some padding for aligned memory reads.
  145. string_append(*target, U"| Used size : ", heap_getUsedSize(allocation), U" bytes\n");
  146. }
  147. });
  148. }
  149. #endif
  150. if (outOfBound) {
  151. string_append(*target, U"| Claimed memory is outside of the pointer's permitted memory region!\n");
  152. } else {
  153. string_append(*target, U"| Claimed memory is safely within the permitted memory region.\n");
  154. }
  155. string_append(*target, U"| Permitted region : ", (uintptr_t)permittedStart, U" to ", (uintptr_t)permittedEnd, U" of ", (intptr_t)(permittedEnd - permittedStart), U" bytes\n");
  156. string_append(*target, U"| Requested region : ", (uintptr_t)claimedStart, U" to ", (uintptr_t)claimedEnd, U" of ", (uintptr_t)(claimedEnd - claimedStart), U" bytes\n");
  157. string_append(*target, U"| Element size : ", elementSize, U" bytes\n");
  158. string_append(*target, U"\\_______________________________________________________________________\n\n");
  159. string_sendMessage(*target, MessageType::Error);
  160. }
  161. static thread_local bool inside = false;
  162. void dsr::impl_assertInsideSafePointer(const char* methodName, const char* pointerName, const uint8_t* claimedStart, const uint8_t* claimedEnd, intptr_t elementSize, const uint8_t* permittedStart, const uint8_t* permittedEnd, const AllocationHeader *header, uint64_t allocationIdentity) {
  163. // Abort to avoid infinite recursion from printing text if we are already inside of another check.
  164. if (inside) return;
  165. inside = true;
  166. if (permittedStart == nullptr) {
  167. throwPointerError(U"SafePointer identity exception! Tried to use a null pointer.", methodName, pointerName, "(null)", claimedStart, claimedEnd, elementSize, permittedStart, permittedEnd, header, allocationIdentity, 0, 0);
  168. return;
  169. }
  170. // If the pointer has an allocation header, check that the identity matches the one stored in the pointer.
  171. uint64_t headerIdentity = 0;
  172. uint64_t headerHash = 0;
  173. FixedAscii<256> allocationName("(null)");
  174. #ifdef EXTRA_SAFE_POINTER_CHECKS
  175. if (header != nullptr) {
  176. #ifndef DSR_HARD_EXIT_ON_ERROR
  177. // This only works if the application has registered a signal handler throwing an error on SIGSEGV, like in the regression tests.
  178. try {
  179. #endif
  180. // 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.
  181. headerIdentity = header->allocationIdentity;
  182. headerHash = header->threadHash;
  183. if (header->name != nullptr) {
  184. // Clone into fixed size memory when we do not know if the memory is corrupted.
  185. allocationName = FixedAscii<256>(header->name);
  186. }
  187. #ifndef DSR_HARD_EXIT_ON_ERROR
  188. } catch(...) {
  189. headerIdentity = 0;
  190. headerHash = 0;
  191. throwPointerError(U"SafePointer exception! Tried to access memory not available to the application.", methodName, pointerName, "(invalid)", claimedStart, claimedEnd, elementSize, permittedStart, permittedEnd, header, allocationIdentity, headerIdentity, headerHash);
  192. return;
  193. }
  194. #endif
  195. if (headerIdentity != allocationIdentity) {
  196. throwPointerError(U"SafePointer identity exception!", methodName, pointerName, allocationName, claimedStart, claimedEnd, elementSize, permittedStart, permittedEnd, header, allocationIdentity, headerIdentity, headerHash);
  197. return;
  198. } else if (headerHash != ANY_THREAD_HASH && headerHash != currentThreadHash) {
  199. throwPointerError(U"SafePointer thread hash exception!", methodName, pointerName, allocationName, claimedStart, claimedEnd, elementSize, permittedStart, permittedEnd, header, allocationIdentity, headerIdentity, headerHash);
  200. return;
  201. }
  202. }
  203. #endif
  204. if (isOutOfBound(claimedStart, claimedEnd, permittedStart, permittedEnd)) {
  205. throwPointerError(U"SafePointer out of bound exception!", methodName, pointerName, allocationName, claimedStart, claimedEnd, elementSize, permittedStart, permittedEnd, header, allocationIdentity, headerIdentity, headerHash);
  206. return;
  207. }
  208. inside = false;
  209. }
  210. #endif