Join the discussion
Write your take first — we'll ask for email only when you're ready to publish.
- Hacker News
- What do you need the executable stack for? You call using a function pointer, there's no executable read/write memory involved in using a function pointer.by Dwedit
- need to represent the "captured-variables"/closureby mwkaufma
- Nested functions may require a context pointer of some kind, which the caller can't supply. One way of doing this: create a thunk on the stack that provides the context pointer, and use that address as the pointer to the nested function.
But now the stack needs to be executable.
by tom_ - A nested function requires an extra parameter for the nested stack pointer, which is passed in a dedicated register on most ABIs. You can't spell this kind of function type in C. To make a C-callable function pointer, the compiler needs to generate a little bit of code (a trampoline) that stuffs the appropriate stack pointer in the appropriate register.
That trampoline needs to live somewhere. Since the function is inherently noncallable after the stack returns, and C programmers hate it when their compiler sneaks in extra malloc calls under the hood, the compiler decides to stick the trampoline on the stack instead of heap-allocating it.
by jcranmer - Self-modifying code is cool. It's a shame we had to disable it for security.by inigyou
- It was maybe cool 50 years ago or so. Nowadays it's no longer needed. Possible performance gains of such code are marginal and modern programming languages allow generating many specialized and optimized code pieces using the same template, so that self-modification is no longer needed.
There is also JIT (like in regexp engines), but it's different story.
by Panzerschrek - I was re-reading Ian Lance Taylor's series of articles on Linkers [0], and one thing I didn't realise is that using dynamically linked libraries almost requires self-modifying code unless you want to resolve all function calls are start-time (which would make startup slower).
I'm still working my way through it so it's possible that I've misunderstood this section, though, and one question I haven't answered is how they get around the typical restriction on w+x pages.
[0] Particularly this one https://www.airs.com/blog/archives/41
by anitil - Not just security. Instruction caches must also be aware of self-modifying code.by mcculley
- Related: previous article in the series, https://news.ycombinator.com/item?id=49308685 (103 points, 47 comments)
- At first, I was annoyed by having to read AT&T syntax. Then I was disoriented by realizing the next snippet was in AT&T syntax without the '%' sigil for registers. But the technique is cool.by gue5t
- Thanks. Sigils fixed (may take a couple of minutes).by uecker
- Have always wondered, why can’t nested functions just be normal functions inside a namespace?
It seems silly to expose a tiny helper to the entire compilation unit when it is only meant to be used inside one function…
by BobbyTables2 - > why can’t nested functions just be normal functions inside a namespace
Because GCC's nested functions are closures - they can access local variables within the function.
by wavemode