MallocTracer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <AK/TemporaryChange.h>
  31. #include <string.h>
  32. //#define REACHABLE_DEBUG
  33. namespace UserspaceEmulator {
  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. // Mark the containing mmap region as a malloc block!
  44. mmap_region.set_malloc(true);
  45. auto* shadow_bits = mmap_region.shadow_data() + address - mmap_region.base();
  46. memset(shadow_bits, 0, size);
  47. if (auto* existing_mallocation = find_mallocation(address)) {
  48. ASSERT(existing_mallocation->freed);
  49. existing_mallocation->size = size;
  50. existing_mallocation->freed = false;
  51. existing_mallocation->malloc_backtrace = Emulator::the().raw_backtrace();
  52. existing_mallocation->free_backtrace.clear();
  53. return;
  54. }
  55. m_mallocations.append({ address, size, false, Emulator::the().raw_backtrace(), Vector<FlatPtr>() });
  56. }
  57. void MallocTracer::target_did_free(Badge<SoftCPU>, FlatPtr address)
  58. {
  59. if (!address)
  60. return;
  61. for (auto& mallocation : m_mallocations) {
  62. if (mallocation.address == address) {
  63. if (mallocation.freed) {
  64. reportln("\n=={}== \033[31;1mDouble free()\033[0m, {:p}", getpid(), address);
  65. reportln("=={}== Address {} has already been passed to free()", 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. reportln("\n=={}== \033[31;1mInvalid free()\033[0m, {:p}", getpid(), address);
  75. reportln("=={}== Address {} has never been returned by malloc()", getpid(), address);
  76. Emulator::the().dump_backtrace();
  77. }
  78. void MallocTracer::target_did_realloc(Badge<SoftCPU>, FlatPtr address, size_t size)
  79. {
  80. auto* region = Emulator::the().mmu().find_region({ 0x20, address });
  81. ASSERT(region);
  82. ASSERT(region->is_mmap());
  83. auto& mmap_region = static_cast<MmapRegion&>(*region);
  84. ASSERT(mmap_region.is_malloc_block());
  85. auto* existing_mallocation = find_mallocation(address);
  86. ASSERT(existing_mallocation);
  87. ASSERT(!existing_mallocation->freed);
  88. size_t old_size = existing_mallocation->size;
  89. auto* shadow_bits = mmap_region.shadow_data() + address - mmap_region.base();
  90. if (size > old_size) {
  91. memset(shadow_bits + old_size, 1, size - old_size);
  92. } else {
  93. memset(shadow_bits + size, 1, old_size - size);
  94. }
  95. existing_mallocation->size = size;
  96. // FIXME: Should we track malloc/realloc backtrace separately perhaps?
  97. existing_mallocation->malloc_backtrace = Emulator::the().raw_backtrace();
  98. }
  99. MallocTracer::Mallocation* MallocTracer::find_mallocation(FlatPtr address)
  100. {
  101. for (auto& mallocation : m_mallocations) {
  102. if (mallocation.contains(address))
  103. return &mallocation;
  104. }
  105. return nullptr;
  106. }
  107. MallocTracer::Mallocation* MallocTracer::find_mallocation_before(FlatPtr address)
  108. {
  109. Mallocation* found_mallocation = nullptr;
  110. for (auto& mallocation : m_mallocations) {
  111. if (mallocation.address >= address)
  112. continue;
  113. if (!found_mallocation || (mallocation.address > found_mallocation->address))
  114. found_mallocation = &mallocation;
  115. }
  116. return found_mallocation;
  117. }
  118. MallocTracer::Mallocation* MallocTracer::find_mallocation_after(FlatPtr address)
  119. {
  120. Mallocation* found_mallocation = nullptr;
  121. for (auto& mallocation : m_mallocations) {
  122. if (mallocation.address <= address)
  123. continue;
  124. if (!found_mallocation || (mallocation.address < found_mallocation->address))
  125. found_mallocation = &mallocation;
  126. }
  127. return found_mallocation;
  128. }
  129. void MallocTracer::audit_read(FlatPtr address, size_t size)
  130. {
  131. if (!m_auditing_enabled)
  132. return;
  133. if (Emulator::the().is_in_malloc_or_free())
  134. return;
  135. auto* mallocation = find_mallocation(address);
  136. if (!mallocation) {
  137. reportln("\n=={}== \033[31;1mHeap buffer overflow\033[0m, invalid {}-byte read at address {:p}", getpid(), size, address);
  138. Emulator::the().dump_backtrace();
  139. auto* mallocation_before = find_mallocation_before(address);
  140. auto* mallocation_after = find_mallocation_after(address);
  141. size_t distance_to_mallocation_before = mallocation_before ? (address - mallocation_before->address - mallocation_before->size) : 0;
  142. size_t distance_to_mallocation_after = mallocation_after ? (mallocation_after->address - address) : 0;
  143. if (mallocation_before && (!mallocation_after || distance_to_mallocation_before < distance_to_mallocation_after)) {
  144. reportln("=={}== Address is {} byte(s) after block of size {}, identity {:p}, allocated at:", getpid(), distance_to_mallocation_before, mallocation_before->size, mallocation_before->address);
  145. Emulator::the().dump_backtrace(mallocation_before->malloc_backtrace);
  146. return;
  147. }
  148. if (mallocation_after && (!mallocation_before || distance_to_mallocation_after < distance_to_mallocation_before)) {
  149. reportln("=={}== Address is {} byte(s) before block of size {}, identity {:p}, allocated at:", getpid(), distance_to_mallocation_after, mallocation_after->size, mallocation_after->address);
  150. Emulator::the().dump_backtrace(mallocation_after->malloc_backtrace);
  151. }
  152. return;
  153. }
  154. size_t offset_into_mallocation = address - mallocation->address;
  155. if (mallocation->freed) {
  156. reportln("\n=={}== \033[31;1mUse-after-free\033[0m, invalid {}-byte read at address {:p}", getpid(), size, address);
  157. Emulator::the().dump_backtrace();
  158. reportln("=={}== Address is {} byte(s) into block of size {}, allocated at:", getpid(), offset_into_mallocation, mallocation->size);
  159. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  160. reportln("=={}== Later freed at:", getpid());
  161. Emulator::the().dump_backtrace(mallocation->free_backtrace);
  162. return;
  163. }
  164. }
  165. void MallocTracer::audit_write(FlatPtr address, size_t size)
  166. {
  167. if (!m_auditing_enabled)
  168. return;
  169. if (Emulator::the().is_in_malloc_or_free())
  170. return;
  171. auto* mallocation = find_mallocation(address);
  172. if (!mallocation) {
  173. reportln("\n=={}== \033[31;1mHeap buffer overflow\033[0m, invalid {}-byte write at address {:p}", getpid(), size, address);
  174. Emulator::the().dump_backtrace();
  175. auto* mallocation_before = find_mallocation_before(address);
  176. auto* mallocation_after = find_mallocation_after(address);
  177. size_t distance_to_mallocation_before = mallocation_before ? (address - mallocation_before->address - mallocation_before->size) : 0;
  178. size_t distance_to_mallocation_after = mallocation_after ? (mallocation_after->address - address) : 0;
  179. if (mallocation_before && (!mallocation_after || distance_to_mallocation_before < distance_to_mallocation_after)) {
  180. reportln("=={}== Address is {} byte(s) after block of size {}, identity {:p}, allocated at:", getpid(), distance_to_mallocation_before, mallocation_before->size, mallocation_before->address);
  181. Emulator::the().dump_backtrace(mallocation_before->malloc_backtrace);
  182. return;
  183. }
  184. if (mallocation_after && (!mallocation_before || distance_to_mallocation_after < distance_to_mallocation_before)) {
  185. reportln("=={}== Address is {} byte(s) before block of size {}, identity {:p}, allocated at:", getpid(), distance_to_mallocation_after, mallocation_after->size, mallocation_after->address);
  186. Emulator::the().dump_backtrace(mallocation_after->malloc_backtrace);
  187. }
  188. return;
  189. }
  190. size_t offset_into_mallocation = address - mallocation->address;
  191. if (mallocation->freed) {
  192. reportln("\n=={}== \033[31;1mUse-after-free\033[0m, invalid {}-byte write at address {:p}", getpid(), size, address);
  193. Emulator::the().dump_backtrace();
  194. reportln("=={}== Address is {} byte(s) into block of size {}, allocated at:", getpid(), offset_into_mallocation, mallocation->size);
  195. Emulator::the().dump_backtrace(mallocation->malloc_backtrace);
  196. reportln("=={}== Later freed at:", getpid());
  197. Emulator::the().dump_backtrace(mallocation->free_backtrace);
  198. return;
  199. }
  200. }
  201. bool MallocTracer::is_reachable(const Mallocation& mallocation) const
  202. {
  203. ASSERT(!mallocation.freed);
  204. // 1. Search in active (non-freed) mallocations for pointers to this mallocation
  205. for (auto& other_mallocation : m_mallocations) {
  206. if (&mallocation == &other_mallocation)
  207. continue;
  208. if (other_mallocation.freed)
  209. continue;
  210. size_t pointers_in_mallocation = other_mallocation.size / sizeof(u32);
  211. for (size_t i = 0; i < pointers_in_mallocation; ++i) {
  212. auto value = Emulator::the().mmu().read32({ 0x20, other_mallocation.address + i * sizeof(u32) });
  213. if (value.value() == mallocation.address && !value.is_uninitialized()) {
  214. #ifdef REACHABLE_DEBUG
  215. reportln("mallocation {:p} is reachable from other mallocation {:p}", mallocation.address, other_mallocation.address);
  216. #endif
  217. return true;
  218. }
  219. }
  220. }
  221. bool reachable = false;
  222. // 2. Search in other memory regions for pointers to this mallocation
  223. Emulator::the().mmu().for_each_region([&](auto& region) {
  224. // Skip the stack
  225. if (region.is_stack())
  226. return IterationDecision::Continue;
  227. if (region.is_text())
  228. return IterationDecision::Continue;
  229. // Skip malloc blocks
  230. if (region.is_mmap() && static_cast<const MmapRegion&>(region).is_malloc_block())
  231. return IterationDecision::Continue;
  232. size_t pointers_in_region = region.size() / sizeof(u32);
  233. for (size_t i = 0; i < pointers_in_region; ++i) {
  234. auto value = region.read32(i * sizeof(u32));
  235. if (value.value() == mallocation.address && !value.is_uninitialized()) {
  236. #ifdef REACHABLE_DEBUG
  237. reportln("mallocation {:p} is reachable from region {:p}-{:p}", mallocation.address, region.base(), region.end() - 1);
  238. #endif
  239. reachable = true;
  240. return IterationDecision::Break;
  241. }
  242. }
  243. return IterationDecision::Continue;
  244. });
  245. return reachable;
  246. }
  247. void MallocTracer::dump_leak_report()
  248. {
  249. TemporaryChange change(m_auditing_enabled, false);
  250. size_t bytes_leaked = 0;
  251. size_t leaks_found = 0;
  252. for (auto& mallocation : m_mallocations) {
  253. if (mallocation.freed)
  254. continue;
  255. if (is_reachable(mallocation))
  256. continue;
  257. ++leaks_found;
  258. bytes_leaked += mallocation.size;
  259. reportln("\n=={}== \033[31;1mLeak\033[0m, {}-byte allocation at address {:p}", getpid(), mallocation.size, mallocation.address);
  260. Emulator::the().dump_backtrace(mallocation.malloc_backtrace);
  261. }
  262. if (!leaks_found)
  263. reportln("\n=={}== \033[32;1mNo leaks found!\033[0m", getpid());
  264. else
  265. reportln("\n=={}== \033[31;1m{} leak(s) found: {} byte(s) leaked\033[0m", getpid(), leaks_found, bytes_leaked);
  266. }
  267. }