Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The actual behavior revolves around the details of how memory is allocated on the stack (which is implementation dependent, leading to undefined behavior). Let's say when your code is entered (called) the stack offset is 0 at that point and what RSP is pointing tois the return address.</p> <p>If you take a quick look at the assembler this might jump out at you:</p> <pre><code>sub $0x10,%rsp </code></pre> <p>This reserves space for your local variables, and you were expecting this. It's easy to think these 16 bytes are the only stack space we're reserving. And if we go past that, we'll be overwriting the return value and crashing the process (or at least the thread).</p> <p>Because it's easy to miss the first instruction:</p> <pre><code>push %rbp </code></pre> <p>Saves the base pointer as part of the calling convention (so the call stack can be traced), and takes up an additional 8 bytes (for 64-bit architecture, ebp is only 4 bytes on 32-bit). So you have 24 bytes before you start to overwrite the return address. And remember if you enter 24 characters, a terminating null ('\0') will be stored as the 25th character, and that's the one that will corrupt the return address.</p> <p>And while the base pointer stored on the stack is also overwritten, it's not used in <code>main</code> after that. But note the caller will be messed up because:</p> <pre><code>leaveq </code></pre> <p>Will set RSP to RBP, then POP RBP. So if the caller references local variables after the call returns, there will likely be a problem. If the caller were different (if you were using a different run-time), writing to that 17th character might have been a problem (possibly causing a SEGFAULT in the caller).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload