1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:48:12 +00:00

UserspaceEmulator: Track malloc ChunkedBlocks for faster auditing

Instead of doing an O(n) scan over all the mallocations whenever we're
doing a read/write audit, UE now keeps track of ChunkedBlocks and their
chunks. Both the block lookup and the chunk lookup is O(1).

We know what ChunkedBlocks look like via mallocdefs.h from LibC.

Note that the old linear scan is still in use for big mallocations,
but the vast majority of mallocations are chunked, so this helps a lot.

This makes malloc auditing significantly faster! :^)
This commit is contained in:
Andreas Kling 2020-11-14 18:27:08 +01:00
parent b494cfea38
commit 2fceffff6f
2 changed files with 103 additions and 32 deletions

View file

@ -27,6 +27,8 @@
#pragma once
#include <AK/Badge.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Types.h>
#include <AK/Vector.h>
@ -56,18 +58,42 @@ private:
FlatPtr address { 0 };
size_t size { 0 };
bool used { false };
bool freed { false };
Vector<FlatPtr> malloc_backtrace;
Vector<FlatPtr> free_backtrace;
};
struct TrackedChunkedBlock {
FlatPtr address { 0 };
size_t chunk_size { 0 };
Vector<Mallocation> mallocations;
};
template<typename Callback>
void for_each_mallocation(Callback callback) const
{
for (auto& it : m_chunked_blocks) {
for (auto& mallocation : it.value->mallocations) {
if (mallocation.used && callback(mallocation) == IterationDecision::Break)
return;
}
}
for (auto& big_mallocation : m_big_mallocations) {
if (callback(big_mallocation) == IterationDecision::Break)
return;
}
}
Mallocation* find_mallocation(FlatPtr);
Mallocation* find_mallocation_before(FlatPtr);
Mallocation* find_mallocation_after(FlatPtr);
bool is_reachable(const Mallocation&) const;
Vector<Mallocation> m_mallocations;
HashMap<FlatPtr, NonnullOwnPtr<TrackedChunkedBlock>> m_chunked_blocks;
Vector<Mallocation> m_big_mallocations;
bool m_auditing_enabled { true };
};