by david-given · 11y ago · 0 comments
I'm not sure if this will work in real life; it looks like it's relying heavily on C compiler magic to ensure that the stack's laid out the way it wants. (Plus, allocating stacks with malloc() doesn't work on some architectures, and I have encountered pthreads implementations that get very unhappy if you change stacks on them).
But it's still a deeply impressive piece of lateral thinking.
In the real world you'd create a stack with mmap with the MMAP_STACK flag. You would ensure the stack is property aligned and make sure there's a guard page at the end of it.
char stack[STACK_SIZE];
int anchor_[unoptimisable_];
char filler_[(char*)&anchor_ - (char*)(stack + STACK_SIZE)];
...
(Haven't tested it, but it's basically the same principle. The same caveats about stack layout and VLAs apply.)It doesn't seem to work correctly on my machine. Writing beyond heap bounds and uninitialized reads.
I like articles that combine hacking and C, but this one really needs more explanation.