func findMemoryBlock(_ address: UnsafeRawPointer) -> MemoryBlock

Given an arbitrary memory address how do I find (in runtime) the nature of memory block it belongs to?

For stack addresses I guess there's some "stack start" and "stack end" of the current thread. For other threads' stacks - I guess I'd have to enumerate all threads to get those ranges. I also found that I can use malloc_size and sometimes it gives me correct result (the size if non zero at least), although it doesn't give me the beginning of the block memory address belongs to. For anything else I have no clue at the moment.

Ideal method I am looking for:

struct MemoryBlock { let type: MemoryBlockType // stack, heap, unmapped, etc let start: UnsafeRawPointer let size: Int let attributes // e.g. red / write }

func findMemoryBlock(_ address: UnsafeRawPointer) -> MemoryBlock

PS. the language doesn't matter (e.g. can be C) so long as this method works in a swift/obj-c app.

Replies

Given an arbitrary memory address how do I find (in runtime) the nature of memory block it belongs to?

This is not easy. Why do you need to do this?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • A debugging tool. Part of the puzzle is knowing upfront if I can safely read N bytes of memory from a given address, or somehow try/catch reading from memory that can fail with, say, address error when I am reading past the heap, etc. I can drop down to C if that's not easy in Swift.

Add a Comment