Skip to main content
Be Bitwise

Safe Buffer Write

A function writes exactly into its 3-slot buffer on the stack. The return address is untouched and RET works normally.

A stack frame typically contains a buffer for local data and a saved return address above it (at a higher address). When code writes into the buffer without exceeding its bounds, the return address stays intact and the function returns safely. This simulation shows the 'normal' case — understanding it is essential before seeing what goes wrong in a buffer overflow.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Set up the buffer and return address

    Do this: Step through the setup instructions (MOV/STORE for return address)

    Watch for: Buffer at addresses 200-202 (empty). Return address 15 stored at address 203

    The memory layout mimics a real stack frame: a 3-byte buffer at addresses 200-202, with the return address stored immediately above at address 203. The frame sits high in memory, well away from the program's own bytes. This adjacency is what makes buffer overflows possible — but here we write safely.

  2. 2

    Fill the buffer (exactly 3 slots)

    Do this: Step through the write loop

    Watch for: Values 1, 2, 3 appear at addresses 200, 201, 202. The counter reaches 0 and the loop exits

    The loop writes exactly 3 values into exactly 3 slots. R2 counts down from 3, and when it reaches 0, the loop exits. Address 203 is never touched by the writes.

  3. 3

    Verify the return address survived

    Do this: Step through the LOAD and HALT

    Watch for: R5 = 15 — the return address is intact

    Loading from address 203 confirms the value is still 15. A function using this buffer could safely execute RET because the return address was never overwritten. Compare this with the buffer-overflow-demo to see what happens with one extra write.

Objectives

  • •Watch values 1, 2, 3 appear at addresses 200, 201, 202
  • •Verify that address 203 (return address) still holds 15
  • •See that writing exactly 3 values into a 3-slot buffer is safe
  • •Notice R5 = 15 at the end - the return address survived

Hints

  • Compare this with the buffer-overflow-demo scenario to see what one extra write does