What is Speculative execution?

A CPU technique that executes instructions ahead based on predictions, such as branch outcomes, before they are known. Wrong results are discarded, but traces such as cache state may remain.

Field: Hardware Security

Japanese name: 投機的実行

Why CPUs execute ahead of time

A high-performance CPU has a deep pipeline and executes instructions out of order, so waiting for every branch to resolve would leave it idle for many cycles. Instead, a branch predictor guesses the outcome and the core keeps fetching and executing along the predicted path. These speculative results are held in internal buffers and committed, or retired, only once the guess is confirmed. If the prediction was wrong, the core discards the speculative work and restarts on the correct path, so the program's visible results are always correct.

Architectural vs. microarchitectural state

Rolling back a misprediction restores the architectural state, meaning the registers and memory a program can observe directly. It does not undo changes to microarchitectural state such as which lines are in the cache, TLB contents or predictor history. Those changes are normally harmless because programs cannot read them directly, but timing can reveal them: an access to a cached line is measurably faster than one to main memory. This gap between what is rolled back and what is not is the root of speculative-execution vulnerabilities.

How Spectre and Meltdown exploit it

In Spectre, disclosed along with Meltdown, an attacker trains the branch predictor so that a path which should never run, such as code past a bounds check, executes speculatively, reads a secret, and uses its value to touch a particular cache line. The results are discarded, but timing which line is cached reveals the secret through a cache side channel. Meltdown abused CPUs that let a speculative load return data from kernel memory before the permission check took effect. Nearly all high-performance CPUs were affected by at least one variant.

Countermeasures and their cost

Mitigations span every layer: operating systems separated kernel and user page tables, compilers insert speculation barriers or use indirect-branch sequences that avoid poisoned predictions, microcode updates add controls over predictor behavior, and CPUs designed after the disclosure build in hardware fixes. Each restriction on speculation, prediction or shared caches reduces leakage but can cost performance, so designers choose how far to go based on what data is at stake. The episode showed that performance features can become security weaknesses even when software is bug-free.

Try it: questions about Speculative execution

Which is an example of vulnerabilities revealing attacks that exploit traces of speculative execution left in the cache to infer data that should not be accessible?

  1. ARowhammer
  2. BSpectre and Meltdown
  3. CLatch-up
Show answer and explanation

Answer: B. Spectre and Meltdown

Spectre and Meltdown exploit the fact that although speculatively executed results are discarded, traces of the data loaded at that time remain in the cache; timing differences (a side channel) then reveal secrets that should be inaccessible. Spectre in particular affects many high-performance CPUs that perform speculative execution, prompting OS patches and processor design changes. They showed how performance features can become security weaknesses.

What is the principle behind the speculative execution vulnerability known as Spectre?

  1. AA CPU arithmetic error changes the results of additions
  2. BInstructions executed speculatively along a predicted branch are rolled back, but their traces in the cache remain and can be read by timing measurements
  3. CSpeculative execution overheats the CPU
Show answer and explanation

Answer: B. Instructions executed speculatively along a predicted branch are rolled back, but their traces in the cache remain and can be read by timing measurements

CPUs execute instructions on the predicted side of a branch before its outcome is known and discard the results if the prediction was wrong. In Spectre, an attacker trains the predictor to mispredict, so a path that should never execute reads secret data and loads a cache location that depends on its value. The results are discarded but the cache state is not restored, so the value can be inferred through a cache side channel.

FAQ

Is speculative execution the same as out-of-order execution?

They are related but different. Out-of-order execution reorders instructions whose inputs are ready, while speculative execution runs instructions before it is known that they should execute at all, such as those after an unresolved branch; most high-performance cores do both.

What is the difference between Spectre and Meltdown?

Spectre tricks the branch predictor into speculatively running legitimate code paths that leak data the victim program can access. Meltdown exploited CPUs whose speculative loads could read kernel memory before the permission check took effect, and it was largely fixed by kernel page-table isolation and later hardware changes.

Why can discarded speculative results still leak data?

The CPU discards register results but does not restore the cache, so data loaded during speculation leaves a trace that can be detected by measuring access times.

Questions that mention Speculative execution (4)

Speculative execution appears in 6 practice questions across 3 topics, including Hardware Security, Hardware Security: Advanced Topics and Trends, CPUs and architecture.

  1. A task took 10 seconds before mitigations for speculative execution were applied and made 5 million system calls during that time. Assume that the mitigations increase the processing time per system call from 0.4 µs to 0.6 µs and leave the time of everything else unchanged. By what proportion does the total time of the task increase?
  2. Which best describes the problems addressed by hardware security?
  3. Which statement about countermeasures to speculative-execution vulnerabilities is generally correct?
  4. Which correctly pairs the descriptions of retpoline and IBRS, countermeasures against the Spectre variant that abuses indirect branch prediction (branch target injection)?

Books on Hardware Security Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

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