MallocTracer.cpp 9.6 KB

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