Up front I want to say that this is a question of curiosity and learning, not something I'm trying to actually do currently.
Rust (famously) doesn't allow accessing uninitialised memory or accessing memory outside of allocations without causing UB. That does to cause issues in certain situations though, and I'm wondering what the resolution to these issues are:
- Scenario 1: I'm implementing something like AdressSanitizer in Rust: some sort of loadable in-process debugger. It clearly needs to be able to access and print (to the user) various memory in dubious state. How do we go about this without causing UB? Miri gets around this by being on the outside of the interpreter, as does valgrind. Ptrace again will at most cause UB in some other process (not myself). So it is the in-process case I'm interested in here.
- Scenario 2: I'm implementing the "root allocator" in an operating system or no-std or similar. I can't just fall back to asking the OS for mmap or brk and then portion that out to the my callers. It isn't allocators all the way down after all! How does this root allocator avoid UB due to accessing memory outside allocations? How do we resolve this?