5 ms·
You unmap the memory when you free it so that it causes a segfault if you access it, and then if a segfault occurs you know something went wrong.
by eslaught 9y ago
You unmap the memory when you free it so that it causes a segfault if you access it, and then if a segfault occurs you know something went wrong.
- pebers 9y agoHow do you know that you haven't re-allocated the same memory to something else? You won't get a segfault in that case.
- stochastic_monk 9y agoThis sounds to me like re-discovering RAII and its failure cases in new garb.
- littlestymaar 9y ago> so that it causes a segfault if you access it No, this is UB. It can cause a segfault, but it can also allow bad things™ to happen.
- barrkel 9y agoLet's be clear on the difference between C and C++ undefined behaviour, and machine behaviour that GCs and runtimes can use for implementation. It is not unusual to rely on triggering a hardware exception in runtimes and GCs, up to and including segfaults. For example, a check for safepoint might be implemented by attempting to read a particular address. When the GC needs to stop the world, it could change the protection bits on the page that contains that address. This technique minimizes the amount of code in the safepoint test and doesn't require any branching logic. See e.g. https://stackoverflow.com/questions/46394575/safepoints-in-jvm https://stackoverflow.com/questions/46394575/safepoints-in-j... Another technique: a runtime can support dynamic growth of the stack by keeping an unmapped or protected page at the limit, and extending it when hit. This is how stacks work on Windows, and it relies on help from codegen: compilers targeting Windows need to generate code that performs a loop touching every page in its stack frame allocation one at a time in allocation order, if the stack frame is larger than the page size. See e.g. https://geidav.wordpress.com/tag/stack-probing/ https://geidav.wordpress.com/tag/stack-probing/
- lmm 9y agoIn C that would be true. LLVM might be able to provide stronger guarantees, particularly on specific architectures.