What is Return address?

The address to go back to when a function finishes. In RISC-V, jal puts the next instruction's address in ra (x1); in x86, call pushes it on the stack.

Field: ISA & Assembly

Japanese name: リターンアドレス

How a call saves it

A call must record where execution should resume, which is the address of the instruction after the call. In RISC-V, jal (jump and link) writes PC + 4 into a register, by convention ra (x1), and jumps; the return is ret, a pseudo-instruction for jalr x0, 0(ra). Calls through function pointers, virtual functions and shared-library stubs use jalr with a register target. In x86, call pushes the return address onto the stack and ret pops it back into the instruction pointer, so the stack always holds it.

Leaf and non-leaf functions

Because every jal overwrites ra, a RISC-V function that calls other functions must save its own return address first. A typical prologue is addi sp, sp, -16 to allocate a frame on the downward-growing stack, then sw ra, 12(sp) and sw s0, 8(sp) to save ra and a callee-saved register. The epilogue reloads them, restores sp and executes ret. A leaf function, which calls nothing, can leave ra in its register and may need no stack frame at all, which makes small functions cheap.

Why attackers target it

A return address stored on the stack sits next to local buffers, so a buffer overflow can overwrite it and redirect execution when the function returns. Classic attacks wrote machine code onto the stack and jumped to it; W^X policies with the page-table no-execute bit block that. Attackers then shifted to return-oriented programming, chaining existing code fragments. Further defenses include stack canaries checked before return, address-space layout randomization and hardware shadow stacks that keep a protected second copy of each return address.

Return addresses in hardware

Out-of-order CPUs predict where a return will go long before ra or the stack is read. A return address stack (RAS) in the branch predictor pushes the fall-through address on each call and pops it on each return, giving very accurate predictions for well-nested code. Exceptions and interrupts are different: the CPU saves the interrupted PC in a dedicated register such as RISC-V mepc or sepc, together with the previous privilege mode, and mret or sret restores all of them at once rather than a plain ret.

Try it: questions about Return address

When a program calls a subroutine (function), which area is generally used to temporarily save the return address and similar information?

  1. AThe stack
  2. BCache tags
  3. CA fixed region of ROM
Show answer and explanation

Answer: A. The stack

Calling a subroutine pushes the return address, registers to be preserved and local variables onto the stack. On ARM and RISC-V, the return address first goes into a link register and is saved to the stack when needed. Its last-in, first-out (LIFO) structure ensures correct returns even with nested or recursive calls, and the stack pointer marks its top. Excessive recursion exhausts it (stack overflow), and buffer overruns that overwrite return addresses are a classic attack.

FAQ

Where is the return address stored in RISC-V?

In register ra (x1), written by jal or jalr at the call. A non-leaf function copies it to its stack frame before making its own calls and restores it before ret.

What is the difference between call/ret in x86 and jal/ret in RISC-V?

x86 call always pushes the return address onto the stack and ret pops it, while RISC-V jal places it in a register and only touches memory if the function chooses to save it.

Why is overwriting the return address dangerous?

When the function returns, the CPU jumps to whatever address is there, so an attacker who overwrites it through a buffer overflow can take control of the program.

Questions that mention Return address (10)

Return address appears in 11 practice questions across 3 topics, including ISA & Assembly, Hardware Security: Advanced Topics and Trends, CPUs and architecture.

  1. What does a CPU's program counter (PC) hold?
  2. What is the property of register x0 (zero) in RISC-V?
  3. What does the RISC-V instruction "jal ra, func" do?
  4. A function that calls no other functions (a leaf function) often does not need to save ra on the stack. Why?
  5. What is the purpose of executing "addi sp, sp, -16" at the start of a RISC-V function?
  6. The code above is the start (prologue) of a function. What does it do?
  7. How is a call through a C function pointer, "(*fp)(x)", generally performed in RISC-V?
  8. Which correctly describes the mechanism of Arm pointer authentication (PAC)?
  9. Which correctly describes the hardware shadow stack adopted in Intel CET (Control-flow Enforcement Technology) and elsewhere?
  10. Regarding hardware features that support control-flow integrity, which correctly pairs what protects returns from functions with what protects indirect calls and jumps such as those through function pointers?

Books on ISA & Assembly Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary