MallocTracer.cpp 8.2 KB

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