Heap description
Jon Abbott (1421) 2651 posts |
Is there a legal way to get a description of a heap? I’m trying to find the last allocated address, so need to either walk it or get the Heap base offset. OS_Heap 1 doesn’t provide any information that actually describes the heap, so do I simply make assumptions about the heap structure? |
Charles Ferguson (8243) 427 posts |
You don’t need to make assumptions. The heap structure is documented in the chapter on memory management. |
Jon Abbott (1421) 2651 posts |
Quoting the PRM:
Based on that, any code that reads the heap structure is making an assumption about the heap structure. The Wiki documentation for OS_Heap 0 doesn’t document the structure, which leads me to believe its “internal use only” and still liable to change. I’m fairly certain something changed in the Heap structure, as I recall many years ago I raised the fact Modules no longer align as per the PRM. |
Stuart Swales (8827) 1357 posts |
But to walk a heap you’d need even more assumptions! I think the ‘should not be relied on’ phrase in the PRM was reasonable in 1987, but can be updated to be ‘this will not change (for 32 bit systems)’. Even if additional information were to be added to the header, it could simply be stored in the first allocated block. If I remember, a few years ago, OS_Heap changed such that heap blocks could be allocated at 4 byte granularity, rather than the since-forever 8 byte granularity, but the structure of the heap header and of heap blocks (the size word describing bytes allocated, including the size word) is unchanged. |
Simon Willcocks (1499) 519 posts |
Last allocated address? Couldn’t you just modify the kernel to store the most recent value returned from OS_Heap somewhere, maybe just set a variable? Or is there something in Pyromaniac to log these things? |
Jon Abbott (1421) 2651 posts |
Quite, the only “legal” means at the moment would be to intercept OS_Heap and track the highest allocated block for the heap in question. That’s a bit extreme, so instead I’ve just read Heap+8 (Heap base offset) and hope the structure doesn’t change in the future. At some point, I’ll modify the Wiki documentation to include the structure with some kind of caveat to cover the fact it’s been in the PRM for 20+ years. |