MallocTracer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "MallocTracer.h"
  27. #include "Emulator.h"
  28. #include "MmapRegion.h"
  29. #include <AK/LogStream.h>
  30. #include <string.h>
  31. //#define REACHABLE_DEBUG
  32. namespace UserspaceEmulator {
  33. static pid_t s_pid = getpid();
  34. MallocTracer::MallocTracer()
  35. {
  36. }
  37. void MallocTracer::target_did_malloc(Badge<SoftCPU>, FlatPtr address, size_t size)
  38. {
  39. auto* region = Emulator::the().mmu().find_region({ 0x20, address });
  40. ASSERT(region);
  41. ASSERT(region->is_mmap());
  42. auto& mmap_region = static_cast<MmapRegion&>(*region);
  43. auto* shadow_bits = mmap_region.shadow_data() + address - mmap_region.base();
  44. memset(shadow_bits, 0, size);
  45. if (auto* existing_mallocation = find_mallocation(address)) {
  46. ASSERT(existing_mallocation->freed);
  47. existing_mallocation->size = size;
  48. existing_mallocation->freed = false;
  49. existing_mallocation->malloc_backtrace = Emulator::the().raw_backtrace();
  50. existing_mallocation->free_backtrace.clear();
  51. return;
  52. }
  53. m_mallocations.append({ address, size, false, Emulator::the().raw_backtrace(), Vector<FlatPtr>() });
  54. }
  55. void MallocTracer::target_did_free(Badge<SoftCPU>, FlatPtr address)
  56. {
  57. if (!address)
  58. return;
  59. for (auto& mallocation : m_mallocations) {
  60. if (mallocation.address == address) {
  61. if (mallocation.freed) {
  62. dbgprintf("\n");
  63. dbgprintf("==%d== \033[31;1mDouble free()\033[0m, %p\n", s_pid, address);
  64. dbgprintf("==%d== Address %p has already been passed to free()\n", s_pid, address);
  65. Emulator::the().dump_backtrace();
  66. } else {
  67. mallocation.freed = true;
  68. mallocation.free_backtrace = Emulator::the().raw_backtrace();
  69. }
  70. return;
  71. }
  72. }
  73. dbgprintf("\n");
  74. dbgprintf("==%d== \033[31;1mInvalid free()\033[0m, %p\n", s_pid, address);
  75. dbgprintf("==%d== Address %p has never been returned by malloc()\n", s_pid, address);
  76. Emulator::the().dump_backtrace();
  77. }
  78. MallocTracer::Mallocation* MallocTracer::find_mallocation(FlatPtr address)
  79. {
  80. for (auto& mallocation : m_mallocations) {
  81. if (mallocation.contains(address))
  82. return &mallocation;
  83. }
  84. return nullptr;
  85. }
  86. void MallocTracer::audit_read(FlatPtr address, size_t size)
  87. {
  88. if (!m_auditing_enabled)
  89. return;
  90. if (Emulator::the().is_in_malloc_or_free())
  91. return;
  92. auto* mallocation = find_mallocation(address);
  93. if (!mallocation)
  94. return;
  95. size_t offset_into_mallocation = address - mallocation->address;
  96. if (mallocation->freed) {
  97. dbgprintf("\n");
  98. dbgprintf("==%d== \033[31;1mUse-after-free\033[0m, invalid %zu-byte read at address %p\n", s_pid, size, address);
  99. Emulator::the().dump_backtrace();
  100. dbgprintf("==%d== Address is %zu bytes into block of size %zu, allocated at:\n", s_pid, offset_into_mallocation, mallocation->size);
  101. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  102. dbgprintf("==%d== Later freed at:\n", s_pid, offset_into_mallocation, mallocation->size);
  103. Emulator::the().dump_backtrace(mallocation->free_backtrace);
  104. return;
  105. }
  106. }
  107. void MallocTracer::audit_write(FlatPtr address, size_t size)
  108. {
  109. if (!m_auditing_enabled)
  110. return;
  111. if (Emulator::the().is_in_malloc_or_free())
  112. return;
  113. auto* mallocation = find_mallocation(address);
  114. if (!mallocation)
  115. return;
  116. size_t offset_into_mallocation = address - mallocation->address;
  117. if (mallocation->freed) {
  118. dbgprintf("\n");
  119. dbgprintf("==%d== \033[31;1mUse-after-free\033[0m, invalid %zu-byte write at address %p\n", s_pid, size, address);
  120. Emulator::the().dump_backtrace();
  121. dbgprintf("==%d== Address is %zu bytes into block of size %zu, allocated at:\n", s_pid, offset_into_mallocation, mallocation->size);
  122. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  123. dbgprintf("==%d== Later freed at:\n", s_pid, offset_into_mallocation, mallocation->size);
  124. Emulator::the().dump_backtrace(mallocation->free_backtrace);
  125. return;
  126. }
  127. }
  128. bool MallocTracer::is_reachable(const Mallocation& mallocation) const
  129. {
  130. ASSERT(!mallocation.freed);
  131. // 1. Search in active (non-freed) mallocations for pointers to this mallocation
  132. for (auto& other_mallocation : m_mallocations) {
  133. if (&mallocation == &other_mallocation)
  134. continue;
  135. if (other_mallocation.freed)
  136. continue;
  137. size_t pointers_in_mallocation = other_mallocation.size / sizeof(u32);
  138. for (size_t i = 0; i < pointers_in_mallocation; ++i) {
  139. auto value = Emulator::the().mmu().read32({ 0x20, other_mallocation.address + i * sizeof(u32) });
  140. if (value.value() == mallocation.address && !value.is_uninitialized()) {
  141. #ifdef REACHABLE_DEBUG
  142. dbgprintf("mallocation %p is reachable from other mallocation %p\n", mallocation.address, other_mallocation.address);
  143. #endif
  144. return true;
  145. }
  146. }
  147. }
  148. bool reachable = false;
  149. // 2. Search in other memory regions for pointers to this mallocation
  150. Emulator::the().mmu().for_each_region([&](auto& region) {
  151. // Skip the stack
  152. if (region.is_stack())
  153. return IterationDecision::Continue;
  154. if (region.is_text())
  155. return IterationDecision::Continue;
  156. // Skip malloc blocks
  157. if (region.is_mmap() && static_cast<const MmapRegion&>(region).is_malloc_block())
  158. return IterationDecision::Continue;
  159. size_t pointers_in_region = region.size() / sizeof(u32);
  160. for (size_t i = 0; i < pointers_in_region; ++i) {
  161. auto value = region.read32(i * sizeof(u32));
  162. if (value.value() == mallocation.address && !value.is_uninitialized()) {
  163. #ifdef REACHABLE_DEBUG
  164. dbgprintf("mallocation %p is reachable from region %p-%p\n", mallocation.address, region.base(), region.end() - 1);
  165. #endif
  166. reachable = true;
  167. return IterationDecision::Break;
  168. }
  169. }
  170. return IterationDecision::Continue;
  171. });
  172. return reachable;
  173. }
  174. void MallocTracer::dump_leak_report()
  175. {
  176. TemporaryChange change(m_auditing_enabled, false);
  177. size_t bytes_leaked = 0;
  178. size_t leaks_found = 0;
  179. for (auto& mallocation : m_mallocations) {
  180. if (mallocation.freed)
  181. continue;
  182. if (is_reachable(mallocation))
  183. continue;
  184. ++leaks_found;
  185. bytes_leaked += mallocation.size;
  186. dbgprintf("\n");
  187. dbgprintf("==%d== \033[31;1mLeak\033[0m, %zu-byte allocation at address %p\n", s_pid, mallocation.size, mallocation.address);
  188. Emulator::the().dump_backtrace(mallocation.malloc_backtrace);
  189. }
  190. dbgprintf("\n");
  191. if (!leaks_found)
  192. dbgprintf("==%d== \033[32;1mNo leaks found!\033[0m\n", s_pid);
  193. else
  194. dbgprintf("==%d== \033[31;1m%zu leak(s) found: %zu byte(s) leaked\033[0m\n", s_pid, leaks_found, bytes_leaked);
  195. }
  196. }