4 ms·
Depends on the language! In C, yes, you can write right past the guard page: https://godbolt.org/z/HnnNPU https://godbolt.org/z/HnnNPU. Rust will insert a stack
by saagarjha 7y ago
Depends on the language! In C, yes, you can write right past the guard page: https://godbolt.org/z/HnnNPU https://godbolt.org/z/HnnNPU. Rust will insert a stack probe when necessary: https://godbolt.org/z/N8mGrz https://godbolt.org/z/N8mGrz
- monocasa 7y agoIn C that compiler is allowed to stack probe. The compilers just make you opt into it with -fstack-check and the like to make sure they didn't break some of your terribly written code.
- saagarjha 7y agoThat code is standards-compliant, because the standard has no knowledge of the stack. I actually had those flags enabled when I was messing around with it and they did nothing useful so I chose to leave them out. (Yes, the compiler will probe the stack–but only if you alloca, it seems. Nobody seems to have extended this protection to stack frames bloated by automatic variables.)
- monocasa 7y agoI couldn't get clang to, and gcc seems to be omitting the checks in some cases with your recursive call to foo (maybe it realizes that 'a' isn't actually accessed, but still emits the increase and decrease?). But gcc given a program that passes the address of 'a' into another function outside the compilation unit does emit stack probes with -fstack-check and -fstack-protector-all. https://godbolt.org/z/jHwZiG https://godbolt.org/z/jHwZiG And I don't care if they didn't come out and say it in the spec. If your program depends on overflowing the stack into other regions of memory you need to be taken out into the street and publicly dealt with as an example to others. That being said, I'm sure there's something about implementation defined behavior around exceeding the platform's max for automatic storage duration objects. If not I may try to sneak that into C2X, lol.
- OnlyOneCannolo 7y agoCan you explain what's going on here?
- saagarjha 7y agoThere's two pieces of code that's fairly similar in semantics: both allocate a bunch of memory on the stack by declaring a large array. In the C version I make an array that easily spans multiple pages and as such writing to the end of it would skip over any guard page. In the Rust version you can see that a __rust_probestack function appears right as I approach the page size, which internally does stack probing to make sure that the guard page isn't skipped.