mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:38:12 +00:00
UserspaceEmulator+LibC: Have UE notice realloc() and update accounting
When a mallocation is shrunk/grown without moving, UE needs to update its precise metadata about the mallocation, since it tracks *exactly* how many bytes were allocated, not just the malloc chunk size.
This commit is contained in:
parent
a0e25b2d31
commit
c4dd77a170
4 changed files with 41 additions and 1 deletions
|
@ -87,6 +87,34 @@ void MallocTracer::target_did_free(Badge<SoftCPU>, FlatPtr address)
|
|||
Emulator::the().dump_backtrace();
|
||||
}
|
||||
|
||||
void MallocTracer::target_did_realloc(Badge<SoftCPU>, FlatPtr address, size_t size)
|
||||
{
|
||||
auto* region = Emulator::the().mmu().find_region({ 0x20, address });
|
||||
ASSERT(region);
|
||||
ASSERT(region->is_mmap());
|
||||
auto& mmap_region = static_cast<MmapRegion&>(*region);
|
||||
|
||||
ASSERT(mmap_region.is_malloc_block());
|
||||
|
||||
auto* existing_mallocation = find_mallocation(address);
|
||||
ASSERT(existing_mallocation);
|
||||
ASSERT(!existing_mallocation->freed);
|
||||
|
||||
size_t old_size = existing_mallocation->size;
|
||||
|
||||
auto* shadow_bits = mmap_region.shadow_data() + address - mmap_region.base();
|
||||
|
||||
if (size > old_size) {
|
||||
memset(shadow_bits + old_size, 1, size - old_size);
|
||||
} else {
|
||||
memset(shadow_bits + size, 1, old_size - size);
|
||||
}
|
||||
|
||||
existing_mallocation->size = size;
|
||||
// FIXME: Should we track malloc/realloc backtrace separately perhaps?
|
||||
existing_mallocation->malloc_backtrace = Emulator::the().raw_backtrace();
|
||||
}
|
||||
|
||||
MallocTracer::Mallocation* MallocTracer::find_mallocation(FlatPtr address)
|
||||
{
|
||||
for (auto& mallocation : m_mallocations) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue