memtrace
PlaygroundBetaLearn memoryFollow on

Getting started

  • Introduction

Unsafe Rust

  • Introduction
  • Raw pointers
  • Undefined behavior

Real world

  • Cloudflare: 100 TB saved in 1.1.1.1's DNS cache

Playground

  • How the Playground works
  • What the Playground can't do yet

Unsafe Rust

Undefined behavior

What the compiler assumes never happens, and how to catch it when it does

What it means

Undefined behavior means the compiler is allowed to assume it never happens. A program that triggers it can print the expected answer, garbage, or crash, and change between builds

Common causes

  • Reading or writing memory that has been freed (use-after-free)
  • Reading or writing outside an allocation (buffer overflow)
  • Two threads touching the same memory at once with at least one write (data race)
  • Breaking the aliasing rules, e.g. writing through a raw pointer while a &mut to the same data is still live
  • Producing an invalid value, such as a bool that isn't 0 or 1

Check it with Miri

cargo +nightly miri run runs a program in an interpreter that reports most undefined behavior, even when the program seems to work

Resources

  • The Rust Reference: Behavior considered undefinedThe full list of what counts as undefined behavior
  • MiriAn interpreter that detects undefined behavior
PreviousRaw pointersNextCloudflare: 100 TB saved in 1.1.1.1's DNS cache

On this page

  1. What it means
  2. Common causes
  3. Resources