SafePointer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2023 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. using namespace dsr;
  26. void dsr::assertNonNegativeSize(intptr_t size) {
  27. if (size < 0) {
  28. throwError(U"Negative size of SafePointer!\n");
  29. }
  30. }
  31. 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, intptr_t claimedSize, intptr_t elementSize) {
  32. if (regionStart == nullptr || pointer < regionStart || pointer + claimedSize > regionEnd) {
  33. String message;
  34. if (regionStart == nullptr) {
  35. string_append(message, U"\n _____________________ SafePointer null exception! _____________________\n");
  36. } else {
  37. string_append(message, U"\n _________________ SafePointer out of bound exception! _________________\n");
  38. }
  39. string_append(message, U"/\n");
  40. string_append(message, U"| Name: ", name, U"\n");
  41. string_append(message, U"| Method: ", method, U"\n");
  42. string_append(message, U"| Region: ", (uintptr_t)regionStart, U" to ", (uintptr_t)regionEnd, U"\n");
  43. string_append(message, U"| Region size: ", (intptr_t)(regionEnd - regionStart), U" bytes\n");
  44. string_append(message, U"| Base pointer: ", (uintptr_t)data, U"\n");
  45. string_append(message, U"| Requested pointer: ", (uintptr_t)pointer, U"\n");
  46. string_append(message, U"| Requested size: ", claimedSize, U" bytes\n");
  47. intptr_t startOffset = (intptr_t)pointer - (intptr_t)regionStart;
  48. intptr_t baseOffset = (intptr_t)pointer - (intptr_t)data;
  49. // Index relative to allocation start
  50. // regionStart is the start of the accessible memory region
  51. if (startOffset != baseOffset) {
  52. string_append(message, U"| Start offset: ", startOffset, U" bytes\n");
  53. if (startOffset % elementSize == 0) {
  54. intptr_t index = startOffset / elementSize;
  55. intptr_t elementCount = ((intptr_t)regionEnd - (intptr_t)regionStart) / elementSize;
  56. string_append(message, U"| Start index: ", index, U" [0..", (elementCount - 1), U"]\n");
  57. }
  58. }
  59. // Base index relative to the stored pointer within the region
  60. // data is the base of the allocation at index zero
  61. string_append(message, U"| Base offset: ", baseOffset, U" bytes\n");
  62. if (baseOffset % elementSize == 0) {
  63. intptr_t index = baseOffset / elementSize;
  64. intptr_t elementCount = ((intptr_t)regionEnd - (intptr_t)data) / elementSize;
  65. string_append(message, U"| Base index: ", index, U" [0..", (elementCount - 1), U"]\n");
  66. }
  67. string_append(message, U"\\_______________________________________________________________________\n\n");
  68. throwError(message);
  69. }
  70. }