logoalt Hacker News

Dwedityesterday at 3:56 PM4 repliesview on HN

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.


Replies

jcranmeryesterday at 7:07 PM

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.

tom_yesterday at 4:03 PM

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.

show 1 reply
mwkaufmayesterday at 6:22 PM

need to represent the "captured-variables"/closure

show 1 reply