Parcourir la source

UserspaceEmulator: Forget ChunkedBlocks after they are munmap()'ed

This is not ideal since we lose free() backtraces, but it will require
some thinking to get this right.
Andreas Kling il y a 4 ans
Parent
commit
8fd97bee7f

+ 8 - 0
DevTools/UserspaceEmulator/MallocTracer.cpp

@@ -40,6 +40,14 @@ MallocTracer::MallocTracer()
 {
 }
 
+void MallocTracer::notify_malloc_block_was_released(Badge<MmapRegion>, MmapRegion& region)
+{
+    // FIXME: It's sad that we may lose a bunch of free() backtraces here,
+    //        but if the address is reused for a new ChunkedBlock, things will
+    //        get extremely confused.
+    m_chunked_blocks.remove(region.base());
+}
+
 void MallocTracer::target_did_malloc(Badge<SoftCPU>, FlatPtr address, size_t size)
 {
     auto* region = Emulator::the().mmu().find_region({ 0x20, address });

+ 3 - 0
DevTools/UserspaceEmulator/MallocTracer.h

@@ -34,6 +34,7 @@
 
 namespace UserspaceEmulator {
 
+class MmapRegion;
 class SoftCPU;
 
 class MallocTracer {
@@ -44,6 +45,8 @@ public:
     void target_did_free(Badge<SoftCPU>, FlatPtr address);
     void target_did_realloc(Badge<SoftCPU>, FlatPtr address, size_t);
 
+    void notify_malloc_block_was_released(Badge<MmapRegion>, MmapRegion&);
+
     void audit_read(FlatPtr address, size_t);
     void audit_write(FlatPtr address, size_t);
 

+ 5 - 0
DevTools/UserspaceEmulator/MmapRegion.cpp

@@ -58,6 +58,11 @@ MmapRegion::MmapRegion(u32 base, u32 size, int prot)
 
 MmapRegion::~MmapRegion()
 {
+    if (is_malloc_block()) {
+        if (auto* tracer = Emulator::the().malloc_tracer())
+            tracer->notify_malloc_block_was_released({}, *this);
+    }
+
     free(m_shadow_data);
     if (m_file_backed)
         munmap(m_data, size());