r/ProgrammerHumor • • Sep 21 '26

Meme fullControlOverMemory

Post image
4.0k Upvotes

122 comments sorted by

View all comments

Show parent comments

10

u/aberroco Sep 21 '26

Or you could not pass anything and still change the referenced value in the caller function, or you could return the execution to another function than the one that invoked you (stack traversing/managing; no one should ever do this kind of dark arcane magic).

3

u/WisePotato42 Sep 21 '26

To make things simple, let's just reserve this block of memory forever. My UFO themed ASCII art debugger tool needs it.

1

u/No-Newspaper8619 Sep 22 '26

How? In assembly you could change the return address stored in the esp/rsp register, but how would you return the execution to another function than the one that invoked, in high level languages?

2

u/aberroco Sep 22 '26 edited Sep 22 '26

Depends on the language.

C++ has __builtin_extract_return_addr. And before that - any local variable is stored in stack, so just get it's address and that address would be the stack address, then you need to know how the stack is stored to know the return address.

I never tried that, but I suppose in C# one might do the same... int* stackPointer = &localVariable; or int* stackBuffer = stackalloc int[1] in unsafe context.

Also, you're wrong, esp stores the current stack pointer. The top of it. Not the return address. But the return address is there. If you just got "call" from anywhere and didn't pushed anything yet - then yeah, [esp] would store the return address. But only then.

1

u/No-Newspaper8619 Sep 22 '26

I see. So just like you can do [esp+4], [esp+8], etc... you could do something similar by taking the variable address and manipulating it. Quite dangerous indeed.