MallocTracer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. MallocTracer::MallocTracer()
  34. {
  35. }
  36. void MallocTracer::target_did_malloc(Badge<SoftCPU>, FlatPtr address, size_t size)
  37. {
  38. auto* region = Emulator::the().mmu().find_region({ 0x20, address });
  39. ASSERT(region);
  40. ASSERT(region->is_mmap());
  41. auto& mmap_region = static_cast<MmapRegion&>(*region);
  42. // Mark the containing mmap region as a malloc block!
  43. mmap_region.set_malloc(true);
  44. auto* shadow_bits = mmap_region.shadow_data() + address - mmap_region.base();
  45. memset(shadow_bits, 0, size);
  46. if (auto* existing_mallocation = find_mallocation(address)) {
  47. ASSERT(existing_mallocation->freed);
  48. existing_mallocation->size = size;
  49. existing_mallocation->freed = false;
  50. existing_mallocation->malloc_backtrace = Emulator::the().raw_backtrace();
  51. existing_mallocation->free_backtrace.clear();
  52. return;
  53. }
  54. m_mallocations.append({ address, size, false, Emulator::the().raw_backtrace(), Vector<FlatPtr>() });
  55. }
  56. void MallocTracer::target_did_free(Badge<SoftCPU>, FlatPtr address)
  57. {
  58. if (!address)
  59. return;
  60. for (auto& mallocation : m_mallocations) {
  61. if (mallocation.address == address) {
  62. if (mallocation.freed) {
  63. report("\n");
  64. report("==%d== \033[31;1mDouble free()\033[0m, %p\n", getpid(), address);
  65. report("==%d== Address %p has already been passed to free()\n", getpid(), address);
  66. Emulator::the().dump_backtrace();
  67. } else {
  68. mallocation.freed = true;
  69. mallocation.free_backtrace = Emulator::the().raw_backtrace();
  70. }
  71. return;
  72. }
  73. }
  74. report("\n");
  75. report("==%d== \033[31;1mInvalid free()\033[0m, %p\n", getpid(), address);
  76. report("==%d== Address %p has never been returned by malloc()\n", getpid(), address);
  77. Emulator::the().dump_backtrace();
  78. }
  79. MallocTracer::Mallocation* MallocTracer::find_mallocation(FlatPtr address)
  80. {
  81. for (auto& mallocation : m_mallocations) {
  82. if (mallocation.contains(address))
  83. return &mallocation;
  84. }
  85. return nullptr;
  86. }
  87. MallocTracer::Mallocation* MallocTracer::find_mallocation_before(FlatPtr address)
  88. {
  89. Mallocation* found_mallocation = nullptr;
  90. for (auto& mallocation : m_mallocations) {
  91. if (mallocation.address >= address)
  92. continue;
  93. if (!found_mallocation || (mallocation.address > found_mallocation->address))
  94. found_mallocation = &mallocation;
  95. }
  96. return found_mallocation;
  97. }
  98. void MallocTracer::audit_read(FlatPtr address, size_t size)
  99. {
  100. if (!m_auditing_enabled)
  101. return;
  102. if (Emulator::the().is_in_malloc_or_free())
  103. return;
  104. auto* mallocation = find_mallocation(address);
  105. if (!mallocation) {
  106. report("\n");
  107. report("==%d== \033[31;1mHeap buffer overflow\033[0m, invalid %zu-byte read at address %p\n", getpid(), size, address);
  108. Emulator::the().dump_backtrace();
  109. if ((mallocation = find_mallocation_before(address))) {
  110. size_t offset_into_mallocation = address - mallocation->address;
  111. report("==%d== Address is %zu byte(s) after block of size %zu, allocated at:\n", getpid(), offset_into_mallocation - mallocation->size, mallocation->size);
  112. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  113. }
  114. return;
  115. }
  116. size_t offset_into_mallocation = address - mallocation->address;
  117. if (mallocation->freed) {
  118. report("\n");
  119. report("==%d== \033[31;1mUse-after-free\033[0m, invalid %zu-byte read at address %p\n", getpid(), size, address);
  120. Emulator::the().dump_backtrace();
  121. report("==%d== Address is %zu byte(s) into block of size %zu, allocated at:\n", getpid(), offset_into_mallocation, mallocation->size);
  122. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  123. report("==%d== Later freed at:\n", getpid());
  124. Emulator::the().dump_backtrace(mallocation->free_backtrace);
  125. return;
  126. }
  127. }
  128. void MallocTracer::audit_write(FlatPtr address, size_t size)
  129. {
  130. if (!m_auditing_enabled)
  131. return;
  132. if (Emulator::the().is_in_malloc_or_free())
  133. return;
  134. auto* mallocation = find_mallocation(address);
  135. if (!mallocation) {
  136. report("\n");
  137. report("==%d== \033[31;1mHeap buffer overflow\033[0m, invalid %zu-byte write at address %p\n", getpid(), size, address);
  138. Emulator::the().dump_backtrace();
  139. if ((mallocation = find_mallocation_before(address))) {
  140. size_t offset_into_mallocation = address - mallocation->address;
  141. report("==%d== Address is %zu byte(s) after block of size %zu, allocated at:\n", getpid(), offset_into_mallocation - mallocation->size, mallocation->size);
  142. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  143. }
  144. return;
  145. }
  146. size_t offset_into_mallocation = address - mallocation->address;
  147. if (mallocation->freed) {
  148. report("\n");
  149. report("==%d== \033[31;1mUse-after-free\033[0m, invalid %zu-byte write at address %p\n", getpid(), size, address);
  150. Emulator::the().dump_backtrace();
  151. report("==%d== Address is %zu byte(s) into block of size %zu, allocated at:\n", getpid(), offset_into_mallocation, mallocation->size);
  152. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  153. report("==%d== Later freed at:\n", getpid());
  154. Emulator::the().dump_backtrace(mallocation->free_backtrace);
  155. return;
  156. }
  157. }
  158. bool MallocTracer::is_reachable(const Mallocation& mallocation) const
  159. {
  160. ASSERT(!mallocation.freed);
  161. // 1. Search in active (non-freed) mallocations for pointers to this mallocation
  162. for (auto& other_mallocation : m_mallocations) {
  163. if (&mallocation == &other_mallocation)
  164. continue;
  165. if (other_mallocation.freed)
  166. continue;
  167. size_t pointers_in_mallocation = other_mallocation.size / sizeof(u32);
  168. for (size_t i = 0; i < pointers_in_mallocation; ++i) {
  169. auto value = Emulator::the().mmu().read32({ 0x20, other_mallocation.address + i * sizeof(u32) });
  170. if (value.value() == mallocation.address && !value.is_uninitialized()) {
  171. #ifdef REACHABLE_DEBUG
  172. report("mallocation %p is reachable from other mallocation %p\n", mallocation.address, other_mallocation.address);
  173. #endif
  174. return true;
  175. }
  176. }
  177. }
  178. bool reachable = false;
  179. // 2. Search in other memory regions for pointers to this mallocation
  180. Emulator::the().mmu().for_each_region([&](auto& region) {
  181. // Skip the stack
  182. if (region.is_stack())
  183. return IterationDecision::Continue;
  184. if (region.is_text())
  185. return IterationDecision::Continue;
  186. // Skip malloc blocks
  187. if (region.is_mmap() && static_cast<const MmapRegion&>(region).is_malloc_block())
  188. return IterationDecision::Continue;
  189. size_t pointers_in_region = region.size() / sizeof(u32);
  190. for (size_t i = 0; i < pointers_in_region; ++i) {
  191. auto value = region.read32(i * sizeof(u32));
  192. if (value.value() == mallocation.address && !value.is_uninitialized()) {
  193. #ifdef REACHABLE_DEBUG
  194. report("mallocation %p is reachable from region %p-%p\n", mallocation.address, region.base(), region.end() - 1);
  195. #endif
  196. reachable = true;
  197. return IterationDecision::Break;
  198. }
  199. }
  200. return IterationDecision::Continue;
  201. });
  202. return reachable;
  203. }
  204. void MallocTracer::dump_leak_report()
  205. {
  206. TemporaryChange change(m_auditing_enabled, false);
  207. size_t bytes_leaked = 0;
  208. size_t leaks_found = 0;
  209. for (auto& mallocation : m_mallocations) {
  210. if (mallocation.freed)
  211. continue;
  212. if (is_reachable(mallocation))
  213. continue;
  214. ++leaks_found;
  215. bytes_leaked += mallocation.size;
  216. report("\n");
  217. report("==%d== \033[31;1mLeak\033[0m, %zu-byte allocation at address %#08x\n", getpid(), mallocation.size, mallocation.address);
  218. Emulator::the().dump_backtrace(mallocation.malloc_backtrace);
  219. }
  220. report("\n");
  221. if (!leaks_found)
  222. report("==%d== \033[32;1mNo leaks found!\033[0m\n", getpid());
  223. else
  224. report("==%d== \033[31;1m%zu leak(s) found: %zu byte(s) leaked\033[0m\n", getpid(), leaks_found, bytes_leaked);
  225. }
  226. }