SDL_test_memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <SDL3/SDL_test.h>
  19. #ifdef HAVE_LIBUNWIND_H
  20. #define UNW_LOCAL_ONLY
  21. #include <libunwind.h>
  22. #endif
  23. #ifdef SDL_PLATFORM_WIN32
  24. #include <windows.h>
  25. #include <dbghelp.h>
  26. static void *s_dbghelp;
  27. typedef BOOL (__stdcall *dbghelp_SymInitialize_fn)(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess);
  28. typedef BOOL (__stdcall *dbghelp_SymFromAddr_fn)(HANDLE hProcess, DWORD64 Address, PDWORD64 Displacement, PSYMBOL_INFO Symbol);
  29. static dbghelp_SymFromAddr_fn dbghelp_SymFromAddr;
  30. #ifdef _WIN64
  31. typedef BOOL (__stdcall *dbghelp_SymGetLineFromAddr_fn)(HANDLE hProcess, DWORD64 qwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line);
  32. #else
  33. typedef BOOL (__stdcall *dbghelp_SymGetLineFromAddr_fn)(HANDLE hProcess, DWORD qwAddr, PDWORD pdwDisplacement, PIMAGEHLP_LINE Line);
  34. #endif
  35. static dbghelp_SymGetLineFromAddr_fn dbghelp_SymGetLineFromAddr;
  36. /* older SDKs might not have this: */
  37. __declspec(dllimport) USHORT WINAPI RtlCaptureStackBackTrace(ULONG FramesToSkip, ULONG FramesToCapture, PVOID* BackTrace, PULONG BackTraceHash);
  38. #define CaptureStackBackTrace RtlCaptureStackBackTrace
  39. #endif
  40. /* This is a simple tracking allocator to demonstrate the use of SDL's
  41. memory allocation replacement functionality.
  42. It gets slow with large numbers of allocations and shouldn't be used
  43. for production code.
  44. */
  45. #define MAXIMUM_TRACKED_STACK_DEPTH 32
  46. typedef struct SDL_tracked_allocation
  47. {
  48. void *mem;
  49. size_t size;
  50. Uint64 stack[MAXIMUM_TRACKED_STACK_DEPTH];
  51. char stack_names[MAXIMUM_TRACKED_STACK_DEPTH][256];
  52. struct SDL_tracked_allocation *next;
  53. } SDL_tracked_allocation;
  54. static SDLTest_Crc32Context s_crc32_context;
  55. static SDL_malloc_func SDL_malloc_orig = NULL;
  56. static SDL_calloc_func SDL_calloc_orig = NULL;
  57. static SDL_realloc_func SDL_realloc_orig = NULL;
  58. static SDL_free_func SDL_free_orig = NULL;
  59. static int s_previous_allocations = 0;
  60. static SDL_tracked_allocation *s_tracked_allocations[256];
  61. static SDL_bool s_randfill_allocations = SDL_FALSE;
  62. static unsigned int get_allocation_bucket(void *mem)
  63. {
  64. CrcUint32 crc_value;
  65. unsigned int index;
  66. SDLTest_Crc32Calc(&s_crc32_context, (CrcUint8 *)&mem, sizeof(mem), &crc_value);
  67. index = (crc_value & (SDL_arraysize(s_tracked_allocations) - 1));
  68. return index;
  69. }
  70. static SDL_tracked_allocation* SDL_GetTrackedAllocation(void *mem)
  71. {
  72. SDL_tracked_allocation *entry;
  73. int index = get_allocation_bucket(mem);
  74. for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
  75. if (mem == entry->mem) {
  76. return entry;
  77. }
  78. }
  79. return NULL;
  80. }
  81. static size_t SDL_GetTrackedAllocationSize(void *mem)
  82. {
  83. SDL_tracked_allocation *entry = SDL_GetTrackedAllocation(mem);
  84. return entry ? entry->size : SIZE_MAX;
  85. }
  86. static SDL_bool SDL_IsAllocationTracked(void *mem)
  87. {
  88. return SDL_GetTrackedAllocation(mem) != NULL;
  89. }
  90. static void SDL_TrackAllocation(void *mem, size_t size)
  91. {
  92. SDL_tracked_allocation *entry;
  93. int index = get_allocation_bucket(mem);
  94. if (SDL_IsAllocationTracked(mem)) {
  95. return;
  96. }
  97. entry = (SDL_tracked_allocation *)SDL_malloc_orig(sizeof(*entry));
  98. if (!entry) {
  99. return;
  100. }
  101. entry->mem = mem;
  102. entry->size = size;
  103. /* Generate the stack trace for the allocation */
  104. SDL_zeroa(entry->stack);
  105. #ifdef HAVE_LIBUNWIND_H
  106. {
  107. int stack_index;
  108. unw_cursor_t cursor;
  109. unw_context_t context;
  110. unw_getcontext(&context);
  111. unw_init_local(&cursor, &context);
  112. stack_index = 0;
  113. while (unw_step(&cursor) > 0) {
  114. unw_word_t offset, pc;
  115. char sym[236];
  116. unw_get_reg(&cursor, UNW_REG_IP, &pc);
  117. entry->stack[stack_index] = pc;
  118. if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
  119. SDL_snprintf(entry->stack_names[stack_index], sizeof(entry->stack_names[stack_index]), "%s+0x%llx", sym, (unsigned long long)offset);
  120. }
  121. ++stack_index;
  122. if (stack_index == SDL_arraysize(entry->stack)) {
  123. break;
  124. }
  125. }
  126. }
  127. #elif defined(SDL_PLATFORM_WIN32)
  128. {
  129. Uint32 count;
  130. PVOID frames[63];
  131. Uint32 i;
  132. count = CaptureStackBackTrace(1, SDL_arraysize(frames), frames, NULL);
  133. entry->size = SDL_min(count, MAXIMUM_TRACKED_STACK_DEPTH);
  134. for (i = 0; i < entry->size; i++) {
  135. char symbol_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
  136. PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)symbol_buffer;
  137. DWORD64 dwDisplacement = 0;
  138. DWORD lineColumn = 0;
  139. pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
  140. pSymbol->MaxNameLen = MAX_SYM_NAME;
  141. IMAGEHLP_LINE line;
  142. line.SizeOfStruct = sizeof(line);
  143. entry->stack[i] = (Uint64)(uintptr_t)frames[i];
  144. if (s_dbghelp) {
  145. if (!dbghelp_SymFromAddr(GetCurrentProcess(), (DWORD64)(uintptr_t)frames[i], &dwDisplacement, pSymbol)) {
  146. SDL_strlcpy(pSymbol->Name, "???", MAX_SYM_NAME);
  147. dwDisplacement = 0;
  148. }
  149. if (!dbghelp_SymGetLineFromAddr(GetCurrentProcess(), (DWORD64)(uintptr_t)frames[i], &lineColumn, &line)) {
  150. line.FileName = "";
  151. line.LineNumber = 0;
  152. }
  153. SDL_snprintf(entry->stack_names[i], sizeof(entry->stack_names[i]), "%s+0x%I64x %s:%u", pSymbol->Name, dwDisplacement, line.FileName, (Uint32)line.LineNumber);
  154. }
  155. }
  156. }
  157. #endif /* HAVE_LIBUNWIND_H */
  158. entry->next = s_tracked_allocations[index];
  159. s_tracked_allocations[index] = entry;
  160. }
  161. static void SDL_UntrackAllocation(void *mem)
  162. {
  163. SDL_tracked_allocation *entry, *prev;
  164. int index = get_allocation_bucket(mem);
  165. prev = NULL;
  166. for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
  167. if (mem == entry->mem) {
  168. if (prev) {
  169. prev->next = entry->next;
  170. } else {
  171. s_tracked_allocations[index] = entry->next;
  172. }
  173. SDL_free_orig(entry);
  174. return;
  175. }
  176. prev = entry;
  177. }
  178. }
  179. static void rand_fill_memory(void* ptr, size_t start, size_t end)
  180. {
  181. Uint8* mem = (Uint8*) ptr;
  182. size_t i;
  183. if (!s_randfill_allocations)
  184. return;
  185. for (i = start; i < end; ++i) {
  186. mem[i] = SDLTest_RandomUint8();
  187. }
  188. }
  189. static void *SDLCALL SDLTest_TrackedMalloc(size_t size)
  190. {
  191. void *mem;
  192. mem = SDL_malloc_orig(size);
  193. if (mem) {
  194. SDL_TrackAllocation(mem, size);
  195. rand_fill_memory(mem, 0, size);
  196. }
  197. return mem;
  198. }
  199. static void *SDLCALL SDLTest_TrackedCalloc(size_t nmemb, size_t size)
  200. {
  201. void *mem;
  202. mem = SDL_calloc_orig(nmemb, size);
  203. if (mem) {
  204. SDL_TrackAllocation(mem, nmemb * size);
  205. }
  206. return mem;
  207. }
  208. static void *SDLCALL SDLTest_TrackedRealloc(void *ptr, size_t size)
  209. {
  210. void *mem;
  211. size_t old_size = 0;
  212. if (ptr) {
  213. old_size = SDL_GetTrackedAllocationSize(ptr);
  214. SDL_assert(old_size != SIZE_MAX);
  215. }
  216. mem = SDL_realloc_orig(ptr, size);
  217. if (ptr) {
  218. SDL_UntrackAllocation(ptr);
  219. }
  220. if (mem) {
  221. SDL_TrackAllocation(mem, size);
  222. if (size > old_size) {
  223. rand_fill_memory(mem, old_size, size);
  224. }
  225. }
  226. return mem;
  227. }
  228. static void SDLCALL SDLTest_TrackedFree(void *ptr)
  229. {
  230. if (!ptr) {
  231. return;
  232. }
  233. if (!s_previous_allocations) {
  234. SDL_assert(SDL_IsAllocationTracked(ptr));
  235. }
  236. SDL_UntrackAllocation(ptr);
  237. SDL_free_orig(ptr);
  238. }
  239. void SDLTest_TrackAllocations(void)
  240. {
  241. if (SDL_malloc_orig) {
  242. return;
  243. }
  244. SDLTest_Crc32Init(&s_crc32_context);
  245. s_previous_allocations = SDL_GetNumAllocations();
  246. if (s_previous_allocations != 0) {
  247. SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
  248. }
  249. #ifdef SDL_PLATFORM_WIN32
  250. {
  251. s_dbghelp = SDL_LoadObject("dbghelp.dll");
  252. if (s_dbghelp) {
  253. dbghelp_SymInitialize_fn dbghelp_SymInitialize;
  254. dbghelp_SymInitialize = (dbghelp_SymInitialize_fn)SDL_LoadFunction(s_dbghelp, "SymInitialize");
  255. dbghelp_SymFromAddr = (dbghelp_SymFromAddr_fn)SDL_LoadFunction(s_dbghelp, "SymFromAddr");
  256. #ifdef _WIN64
  257. dbghelp_SymGetLineFromAddr = (dbghelp_SymGetLineFromAddr_fn)SDL_LoadFunction(s_dbghelp, "SymGetLineFromAddr64");
  258. #else
  259. dbghelp_SymGetLineFromAddr = (dbghelp_SymGetLineFromAddr_fn)SDL_LoadFunction(s_dbghelp, "SymGetLineFromAddr");
  260. #endif
  261. if (!dbghelp_SymInitialize || !dbghelp_SymFromAddr || !dbghelp_SymGetLineFromAddr) {
  262. SDL_UnloadObject(s_dbghelp);
  263. s_dbghelp = NULL;
  264. } else {
  265. if (!dbghelp_SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
  266. SDL_UnloadObject(s_dbghelp);
  267. s_dbghelp = NULL;
  268. }
  269. }
  270. }
  271. }
  272. #endif
  273. SDL_GetMemoryFunctions(&SDL_malloc_orig,
  274. &SDL_calloc_orig,
  275. &SDL_realloc_orig,
  276. &SDL_free_orig);
  277. SDL_SetMemoryFunctions(SDLTest_TrackedMalloc,
  278. SDLTest_TrackedCalloc,
  279. SDLTest_TrackedRealloc,
  280. SDLTest_TrackedFree);
  281. }
  282. void SDLTest_RandFillAllocations()
  283. {
  284. SDLTest_TrackAllocations();
  285. s_randfill_allocations = SDL_TRUE;
  286. }
  287. void SDLTest_LogAllocations(void)
  288. {
  289. char *message = NULL;
  290. size_t message_size = 0;
  291. char line[128], *tmp;
  292. SDL_tracked_allocation *entry;
  293. int index, count, stack_index;
  294. Uint64 total_allocated;
  295. if (!SDL_malloc_orig) {
  296. return;
  297. }
  298. message = SDL_realloc_orig(NULL, 1);
  299. if (!message) {
  300. return;
  301. }
  302. *message = 0;
  303. #define ADD_LINE() \
  304. message_size += (SDL_strlen(line) + 1); \
  305. tmp = (char *)SDL_realloc_orig(message, message_size); \
  306. if (!tmp) { \
  307. return; \
  308. } \
  309. message = tmp; \
  310. SDL_strlcat(message, line, message_size)
  311. SDL_strlcpy(line, "Memory allocations:\n", sizeof(line));
  312. ADD_LINE();
  313. count = 0;
  314. total_allocated = 0;
  315. for (index = 0; index < SDL_arraysize(s_tracked_allocations); ++index) {
  316. for (entry = s_tracked_allocations[index]; entry; entry = entry->next) {
  317. (void)SDL_snprintf(line, sizeof(line), "Allocation %d: %d bytes\n", count, (int)entry->size);
  318. ADD_LINE();
  319. /* Start at stack index 1 to skip our tracking functions */
  320. for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
  321. if (!entry->stack[stack_index]) {
  322. break;
  323. }
  324. (void)SDL_snprintf(line, sizeof(line), "\t0x%" SDL_PRIx64 ": %s\n", entry->stack[stack_index], entry->stack_names[stack_index]);
  325. ADD_LINE();
  326. }
  327. total_allocated += entry->size;
  328. ++count;
  329. }
  330. }
  331. (void)SDL_snprintf(line, sizeof(line), "Total: %.2f Kb in %d allocations\n", total_allocated / 1024.0, count);
  332. ADD_LINE();
  333. #undef ADD_LINE
  334. SDL_Log("%s", message);
  335. }