<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>src/tools/miri/README.md - fork/rust @ master</title>
<meta name="description" content="src/tools/miri/README.md in fork/rust at master.">
<meta property="og:title" content="src/tools/miri/README.md - fork/rust @ master">
<meta property="og:description" content="src/tools/miri/README.md in fork/rust at master.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="master" data-tip="3bcad65fbf67b6edf87c949531fbc13435882e6b">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/rust</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/rust</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">bors</span><span class="commit-message" data-commit="3bcad65fbf67b6edf87c949531fbc13435882e6b">Auto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee</span><span class="snapshot-sha sha" data-commit="3bcad65fbf67b6edf87c949531fbc13435882e6b">3bcad65fbf6</span><time datetime="2023-10-05T15:24:54+00:00" title="2023-10-05 15:24:54 +0000">2023-10-05</time></p>
<header class="topbar"><details class="switcher"><summary>master</summary><div class="menu"><strong>branches</strong><a class="current" href="/fork/rust/ref/master/">master</a><strong>tags</strong><a href="/fork/rust/ref/1.39.0/">1.39.0</a><a href="/fork/rust/ref/1.40.0/">1.40.0</a><a href="/fork/rust/ref/1.41.0/">1.41.0</a><a href="/fork/rust/ref/1.41.1/">1.41.1</a><a href="/fork/rust/ref/1.42.0/">1.42.0</a><a href="/fork/rust/ref/1.43.0/">1.43.0</a><a href="/fork/rust/ref/1.43.1/">1.43.1</a><a href="/fork/rust/ref/1.44.0/">1.44.0</a><a class="all" href="/fork/rust/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/rust/">rust</a> / <a href="/fork/rust/ref/master/tree/src/">src</a> / <a href="/fork/rust/ref/master/tree/src/tools/">tools</a> / <a href="/fork/rust/ref/master/tree/src/tools/miri/">miri</a> / <span class="cur">README.md</span></nav>
<span class="view-stats"><span>36.8 KiB</span><span>631 lines</span></span><span class="actions"><a class="raw" href="/fork/rust/raw/6127eada2221e5feb4aa7f429f5310fd25d6842e/src/tools/miri/README.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h1>Miri</h1>
<p>An experimental interpreter for <a href="https://www.rust-lang.org/">Rust</a>'s
<a href="https://github.com/rust-lang/rfcs/blob/master/text/1211-mir.md">mid-level intermediate representation</a> (MIR). It can run binaries and
test suites of cargo projects and detect certain classes of
<a href="https://doc.rust-lang.org/reference/behavior-considered-undefined.html">undefined behavior</a>,
for example:</p>
<ul>
<li>Out-of-bounds memory accesses and use-after-free</li>
<li>Invalid use of uninitialized data</li>
<li>Violation of intrinsic preconditions (an <a href="https://doc.rust-lang.org/stable/std/hint/fn.unreachable_unchecked.html"><code>unreachable_unchecked</code></a> being
reached, calling <a href="https://doc.rust-lang.org/stable/std/ptr/fn.copy_nonoverlapping.html"><code>copy_nonoverlapping</code></a> with overlapping ranges, ...)</li>
<li>Not sufficiently aligned memory accesses and references</li>
<li>Violation of <em>some</em> basic type invariants (a <code>bool</code> that is not 0 or 1, for example,
or an invalid enum discriminant)</li>
<li><strong>Experimental</strong>: Violations of the <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md">Stacked Borrows</a> rules governing aliasing
for reference types</li>
<li><strong>Experimental</strong>: Violations of the <a href="https://perso.crans.org/vanille/treebor/">Tree Borrows</a> aliasing rules, as an optional
alternative to <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md">Stacked Borrows</a></li>
<li><strong>Experimental</strong>: Data races</li>
</ul>
<p>On top of that, Miri will also tell you about memory leaks: when there is memory
still allocated at the end of the execution, and that memory is not reachable
from a global <code>static</code>, Miri will raise an error.</p>
<p>Miri supports almost all Rust language features; in particular, unwinding and
concurrency are properly supported (including some experimental emulation of
weak memory effects, i.e., reads can return outdated values).</p>
<p>You can use Miri to emulate programs on other targets, e.g. to ensure that
byte-level data manipulation works correctly both on little-endian and
big-endian systems. See
<a href="#cross-interpretation-running-for-different-targets">cross-interpretation</a>
below.</p>
<p>Miri has already discovered some <a href="#bugs-found-by-miri">real-world bugs</a>. If you
found a bug with Miri, we'd appreciate if you tell us and we'll add it to the
list!</p>
<p>By default, Miri ensures a fully deterministic execution and isolates the
program from the host system. Some APIs that would usually access the host, such
as gathering entropy for random number generators, environment variables, and
clocks, are replaced by deterministic "fake" implementations. Set
<code>MIRIFLAGS="-Zmiri-disable-isolation"</code> to access the real system APIs instead.
(In particular, the "fake" system RNG APIs make Miri <strong>not suited for
cryptographic use</strong>! Do not generate keys using Miri.)</p>
<p>All that said, be aware that Miri will <strong>not catch all cases of undefined
behavior</strong> in your program, and cannot run all programs:</p>
<ul>
<li>
<p>There are still plenty of open questions around the basic invariants for some
types and when these invariants even have to hold. Miri tries to avoid false
positives here, so if your program runs fine in Miri right now that is by no
means a guarantee that it is UB-free when these questions get answered.</p>
<p>In particular, Miri does not check that references point to valid data.</p>
</li>
<li>
<p>If the program relies on unspecified details of how data is laid out, it will
still run fine in Miri -- but might break (including causing UB) on different
compiler versions or different platforms.</p>
</li>
<li>
<p>Program execution is non-deterministic when it depends, for example, on where
exactly in memory allocations end up, or on the exact interleaving of
concurrent threads. Miri tests one of many possible executions of your
program. You can alleviate this to some extent by running Miri with different
values for <code>-Zmiri-seed</code>, but that will still by far not explore all possible
executions.</p>
</li>
<li>
<p>Miri runs the program as a platform-independent interpreter, so the program
has no access to most platform-specific APIs or FFI. A few APIs have been
implemented (such as printing to stdout, accessing environment variables, and
basic file system access) but most have not: for example, Miri currently does
not support networking. System API support varies between targets; if you run
on Windows it is a good idea to use <code>--target x86_64-unknown-linux-gnu</code> to get
better support.</p>
</li>
<li>
<p>Weak memory emulation may <a href="https://github.com/rust-lang/miri/issues/2301">produce weak behaviours</a>
unobservable by compiled programs running on real hardware when <code>SeqCst</code> fences are used, and it
cannot produce all behaviors possibly observable on real hardware.</p>
</li>
</ul>
<p>Moreover, Miri fundamentally cannot tell you whether your code is <em>sound</em>. <a href="https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#soundness-of-code--of-a-library">Soundness</a> is the property
of never causing undefined behavior when invoked from arbitrary safe code, even in combination with
other sound code. In contrast, Miri can just tell you if <em>a particular way of interacting with your
code</em> (e.g., a test suite) causes any undefined behavior. It is up to you to ensure sufficient
coverage.</p>
<h2>Using Miri</h2>
<p>Install Miri on Rust nightly via <code>rustup</code>:</p>
<pre class="language-bash"><a-f>rustup</a-f> +nightly component add miri</pre>
<p>If <code>rustup</code> says the <code>miri</code> component is unavailable, that's because not all
nightly releases come with all tools. Check out
<a href="https://rust-lang.github.io/rustup-components-history">this website</a> to
determine a nightly version that comes with Miri and install that using <code>rustup toolchain install nightly-YYYY-MM-DD</code>. Either way, all of the following commands
assume the right toolchain is pinned via <code>rustup override set nightly</code> or
<code>rustup override set nightly-YYYY-MM-DD</code>. (Alternatively, use <code>cargo +nightly</code>/<code>cargo +nightly-YYYY-MM-DD</code> for each of the following commands.)</p>
<p>Now you can run your project in Miri:</p>
<ol>
<li>Run <code>cargo clean</code> to eliminate any cached dependencies. Miri needs your
dependencies to be compiled the right way, that would not happen if they have
previously already been compiled.</li>
<li>To run all tests in your project through Miri, use <code>cargo miri test</code>.</li>
<li>If you have a binary project, you can run it through Miri using <code>cargo miri run</code>.</li>
</ol>
<p>The first time you run Miri, it will perform some extra setup and install some
dependencies. It will ask you for confirmation before installing anything.</p>
<p><code>cargo miri run/test</code> supports the exact same flags as <code>cargo run/test</code>. For
example, <code>cargo miri test filter</code> only runs the tests containing <code>filter</code> in
their name.</p>
<p>You can pass arguments to Miri via <code>MIRIFLAGS</code>. For example,
<code>MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run</code> runs the program
without checking the aliasing of references.</p>
<p>When compiling code via <code>cargo miri</code>, the <code>cfg(miri)</code> config flag is set for code
that will be interpret under Miri. You can use this to ignore test cases that fail
under Miri because they do things Miri does not support:</p>
<pre class="language-rust"><a-at>#</a-at><a-p>[</a-p><a-at>test</a-at><a-p>]</a-p>
<a-at>#</a-at><a-p>[</a-p><a-at>cfg_attr</a-at><a-p>(</a-p><a-at>miri</a-at><a-p>,</a-p><a-at> ignore</a-at><a-p>)]</a-p>
<a-k>fn</a-k> <a-f>does_not_work_on_miri</a-f><a-p>()</a-p> <a-p>{</a-p>
    tokio<a-p>::</a-p><a-f>run</a-f><a-p>(</a-p>futures<a-p>::</a-p>future<a-p>::</a-p><a-f>ok</a-f><a-p>::&lt;</a-p><a-t>_</a-t><a-p>,</a-p> <a-p>()&gt;(()));</a-p>
<a-p>}</a-p></pre>
<p>There is no way to list all the infinite things Miri cannot do, but the
interpreter will explicitly tell you when it finds something unsupported:</p>
<pre>error: unsupported operation: can't call foreign function: bind
    ...
    = help: this is likely not a bug in the program; it indicates that the program \
            performed an operation that the interpreter does not support
</pre>
<h3>Cross-interpretation: running for different targets</h3>
<p>Miri can not only run a binary or test suite for your host target, it can also
perform cross-interpretation for arbitrary foreign targets: <code>cargo miri run --target x86_64-unknown-linux-gnu</code> will run your program as if it was a Linux
program, no matter your host OS. This is particularly useful if you are using
Windows, as the Linux target is much better supported than Windows targets.</p>
<p>You can also use this to test platforms with different properties than your host
platform. For example <code>cargo miri test --target s390x-unknown-linux-gnu</code>
will run your test suite on a big-endian target, which is useful for testing
endian-sensitive code.</p>
<h3>Running Miri on CI</h3>
<p>To run Miri on CI, make sure that you handle the case where the latest nightly
does not ship the Miri component because it currently does not build. <code>rustup toolchain install --component</code> knows how to handle this situation, so the
following snippet should always work:</p>
<pre class="language-bash"><a-f>rustup</a-f> toolchain install nightly <a-co>--component</a-co> miri
<a-f>rustup</a-f> override set nightly

<a-f>cargo</a-f> miri test</pre>
<p>Here is an example job for GitHub Actions:</p>
<pre class="language-yaml">  <a-pr>miri</a-pr><a-p>:</a-p>
    <a-pr>name</a-pr><a-p>:</a-p> <a-s>&quot;Miri&quot;</a-s>
    <a-pr>runs-on</a-pr><a-p>:</a-p> <a-s>ubuntu-latest</a-s>
    <a-pr>steps</a-pr><a-p>:</a-p>
      <a-p>-</a-p> <a-pr>uses</a-pr><a-p>:</a-p> <a-s>actions/checkout@v3</a-s>
      <a-p>-</a-p> <a-pr>name</a-pr><a-p>:</a-p> <a-s>Install Miri</a-s>
        <a-pr>run</a-pr><a-p>:</a-p> <a-p>|</a-p><a-s>
          rustup toolchain install nightly --component miri
          rustup override set nightly
          cargo miri setup</a-s>
      <a-p>-</a-p> <a-pr>name</a-pr><a-p>:</a-p> <a-s>Test with Miri</a-s>
        <a-pr>run</a-pr><a-p>:</a-p> <a-s>cargo miri test</a-s></pre>
<p>The explicit <code>cargo miri setup</code> helps to keep the output of the actual test step
clean.</p>
<h3>Testing for alignment issues</h3>
<p>Miri can sometimes miss misaligned accesses since allocations can "happen to be"
aligned just right. You can use <code>-Zmiri-symbolic-alignment-check</code> to definitely
catch all such issues, but that flag will also cause false positives when code
does manual pointer arithmetic to account for alignment. Another alternative is
to call Miri with various values for <code>-Zmiri-seed</code>; that will alter the
randomness that is used to determine allocation base addresses. The following
snippet calls Miri in a loop with different values for the seed:</p>
<pre>for SEED in $(seq 0 255); do
  echo &quot;Trying seed: $SEED&quot;
  MIRIFLAGS=-Zmiri-seed=$SEED cargo miri test || { echo &quot;Failing seed: $SEED&quot;; break; };
done
</pre>
<h3>Supported targets</h3>
<p>Miri does not support all targets supported by Rust. The good news, however, is
that no matter your host OS/platform, it is easy to run code for <em>any</em> target
using <code>--target</code>!</p>
<p>The following targets are tested on CI and thus should always work (to the
degree documented below):</p>
<ul>
<li>All Rust <a href="https://doc.rust-lang.org/rustc/platform-support.html">Tier 1 targets</a> are supported by
Miri. They are all checked on Miri's CI, and some (at least one per OS) are even checked on every
Rust PR, so the shipped Miri should always work on these targets.</li>
<li>We also support <code>s390x-unknown-linux-gnu</code> as our "big-endian target of choice".</li>
<li>For every other target with OS <code>linux</code>, <code>macos</code>, or <code>windows</code>, Miri should generally work, but we
make no promises.</li>
<li>For targets on other operating systems, even basic operations such as printing to the standard
output might not work, and Miri might fail before even reaching the <code>main</code> function.</li>
</ul>
<p>However, even for targets that we do support, the degree of support for accessing platform APIs
(such as the file system) differs between targets: generally, Linux targets have the best support,
and macOS targets are usually on par. Windows is supported less well.</p>
<h3>Running tests in parallel</h3>
<p>Though it implements Rust threading, Miri itself is a single-threaded interpreter.
This means that when running <code>cargo miri test</code>, you will probably see a dramatic
increase in the amount of time it takes to run your whole test suite due to the
inherent interpreter slowdown and a loss of parallelism.</p>
<p>You can get your test suite's parallelism back by running <code>cargo miri nextest run -jN</code>
(note that you will need <a href="https://nexte.st"><code>cargo-nextest</code></a> installed).
This works because <code>cargo-nextest</code> collects a list of all tests then launches a
separate <code>cargo miri run</code> for each test. You will need to specify a <code>-j</code> or <code>--test-threads</code>;
by default <code>cargo miri nextest run</code> runs one test at a time. For more details, see the
<a href="https://nexte.st/book/miri.html"><code>cargo-nextest</code> Miri documentation</a>.</p>
<p>Note: This one-test-per-process model means that <code>cargo miri test</code> is able to detect data
races where two tests race on a shared resource, but <code>cargo miri nextest run</code> will not detect
such races.</p>
<p>Note: <code>cargo-nextest</code> does not support doctests, see https://github.com/nextest-rs/nextest/issues/16</p>
<h3>Common Problems</h3>
<p>When using the above instructions, you may encounter a number of confusing compiler
errors.</p>
<h4>"note: run with <code>RUST_BACKTRACE=1</code> environment variable to display a backtrace"</h4>
<p>You may see this when trying to get Miri to display a backtrace. By default, Miri
doesn't expose any environment to the program, so running
<code>RUST_BACKTRACE=1 cargo miri test</code> will not do what you expect.</p>
<p>To get a backtrace, you need to disable isolation
<a href="#miri--z-flags-and-environment-variables">using <code>-Zmiri-disable-isolation</code></a>:</p>
<pre class="language-bash"><a-pr>RUST_BACKTRACE</a-pr>=1 <a-pr>MIRIFLAGS</a-pr>=<a-s>&quot;-Zmiri-disable-isolation&quot;</a-s> <a-f>cargo</a-f> miri test</pre>
<h4>"found possibly newer version of crate <code>std</code> which <code>&lt;dependency&gt;</code> depends on"</h4>
<p>Your build directory may contain artifacts from an earlier build that have/have
not been built for Miri. Run <code>cargo clean</code> before switching from non-Miri to
Miri builds and vice-versa.</p>
<h4>"found crate <code>std</code> compiled by an incompatible version of rustc"</h4>
<p>You may be running <code>cargo miri</code> with a different compiler version than the one
used to build the custom libstd that Miri uses, and Miri failed to detect that.
Try deleting <code>~/.cache/miri</code>.</p>
<h4>"no mir for <code>std::rt::lang_start_internal</code>"</h4>
<p>This means the sysroot you are using was not compiled with Miri in mind.  This
should never happen when you use <code>cargo miri</code> because that takes care of setting
up the sysroot.  If you are using <code>miri</code> (the Miri driver) directly, see the
<a href="/fork/rust/ref/master/blob/src/tools/miri/CONTRIBUTING.md">contributors' guide</a> for how to use <code>./miri</code> to best do that.</p>
<h2>Miri <code>-Z</code> flags and environment variables</h2>
<p>Miri adds its own set of <code>-Z</code> flags, which are usually set via the <code>MIRIFLAGS</code>
environment variable. We first document the most relevant and most commonly used flags:</p>
<ul>
<li><code>-Zmiri-compare-exchange-weak-failure-rate=&lt;rate&gt;</code> changes the failure rate of
<code>compare_exchange_weak</code> operations. The default is <code>0.8</code> (so 4 out of 5 weak ops will fail).
You can change it to any value between <code>0.0</code> and <code>1.0</code>, where <code>1.0</code> means it
will always fail and <code>0.0</code> means it will never fail. Note than setting it to
<code>1.0</code> will likely cause hangs, since it means programs using
<code>compare_exchange_weak</code> cannot make progress.</li>
<li><code>-Zmiri-disable-isolation</code> disables host isolation.  As a consequence,
the program has access to host resources such as environment variables, file
systems, and randomness.</li>
<li><code>-Zmiri-disable-leak-backtraces</code> disables backtraces reports for memory leaks. By default, a
backtrace is captured for every allocation when it is created, just in case it leaks. This incurs
some memory overhead to store data that is almost never used. This flag is implied by
<code>-Zmiri-ignore-leaks</code>.</li>
<li><code>-Zmiri-env-forward=&lt;var&gt;</code> forwards the <code>var</code> environment variable to the interpreted program. Can
be used multiple times to forward several variables. Execution will still be deterministic if the
value of forwarded variables stays the same. Has no effect if <code>-Zmiri-disable-isolation</code> is set.</li>
<li><code>-Zmiri-ignore-leaks</code> disables the memory leak checker, and also allows some
remaining threads to exist when the main thread exits.</li>
<li><code>-Zmiri-isolation-error=&lt;action&gt;</code> configures Miri's response to operations
requiring host access while isolation is enabled. <code>abort</code>, <code>hide</code>, <code>warn</code>,
and <code>warn-nobacktrace</code> are the supported actions. The default is to <code>abort</code>,
which halts the machine. Some (but not all) operations also support continuing
execution with a "permission denied" error being returned to the program.
<code>warn</code> prints a full backtrace when that happens; <code>warn-nobacktrace</code> is less
verbose. <code>hide</code> hides the warning entirely.</li>
<li><code>-Zmiri-num-cpus</code> states the number of available CPUs to be reported by miri. By default, the
number of available CPUs is <code>1</code>. Note that this flag does not affect how miri handles threads in
any way.</li>
<li><code>-Zmiri-permissive-provenance</code> disables the warning for integer-to-pointer casts and
<a href="https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html"><code>ptr::from_exposed_addr</code></a>.
This will necessarily miss some bugs as those operations are not efficiently and accurately
implementable in a sanitizer, but it will only miss bugs that concern memory/pointers which is
subject to these operations.</li>
<li><code>-Zmiri-preemption-rate</code> configures the probability that at the end of a basic block, the active
thread will be preempted. The default is <code>0.01</code> (i.e., 1%). Setting this to <code>0</code> disables
preemption.</li>
<li><code>-Zmiri-report-progress</code> makes Miri print the current stacktrace every now and then, so you can
tell what it is doing when a program just keeps running. You can customize how frequently the
report is printed via <code>-Zmiri-report-progress=&lt;blocks&gt;</code>, which prints the report every N basic
blocks.</li>
<li><code>-Zmiri-seed=&lt;num&gt;</code> configures the seed of the RNG that Miri uses to resolve non-determinism. This
RNG is used to pick base addresses for allocations, to determine preemption and failure of
<code>compare_exchange_weak</code>, and to control store buffering for weak memory emulation. When isolation
is enabled (the default), this is also used to emulate system entropy. The default seed is 0. You
can increase test coverage by running Miri multiple times with different seeds.</li>
<li><code>-Zmiri-strict-provenance</code> enables <a href="https://github.com/rust-lang/rust/issues/95228">strict
provenance</a> checking in Miri. This means that
casting an integer to a pointer yields a result with 'invalid' provenance, i.e., with provenance
that cannot be used for any memory access.</li>
<li><code>-Zmiri-symbolic-alignment-check</code> makes the alignment check more strict.  By default, alignment is
checked by casting the pointer to an integer, and making sure that is a multiple of the alignment.
This can lead to cases where a program passes the alignment check by pure chance, because things
"happened to be" sufficiently aligned -- there is no UB in this execution but there would be UB in
others.  To avoid such cases, the symbolic alignment check only takes into account the requested
alignment of the relevant allocation, and the offset into that allocation.  This avoids missing
such bugs, but it also incurs some false positives when the code does manual integer arithmetic to
ensure alignment.  (The standard library <code>align_to</code> method works fine in both modes; under
symbolic alignment it only fills the middle slice when the allocation guarantees sufficient
alignment.)</li>
</ul>
<p>The remaining flags are for advanced use only, and more likely to change or be removed.
Some of these are <strong>unsound</strong>, which means they can lead
to Miri failing to detect cases of undefined behavior in a program.</p>
<ul>
<li><code>-Zmiri-disable-abi-check</code> disables checking <a href="https://doc.rust-lang.org/reference/items/functions.html#extern-function-qualifier">function ABI</a>. Using this flag
is <strong>unsound</strong>. This flag is <strong>deprecated</strong>.</li>
<li><code>-Zmiri-disable-alignment-check</code> disables checking pointer alignment, so you
can focus on other failures, but it means Miri can miss bugs in your program.
Using this flag is <strong>unsound</strong>.</li>
<li><code>-Zmiri-disable-data-race-detector</code> disables checking for data races.  Using
this flag is <strong>unsound</strong>. This implies <code>-Zmiri-disable-weak-memory-emulation</code>.</li>
<li><code>-Zmiri-disable-stacked-borrows</code> disables checking the experimental
aliasing rules to track borrows (<a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md">Stacked Borrows</a> and <a href="https://perso.crans.org/vanille/treebor/">Tree Borrows</a>).
This can make Miri run faster, but it also means no aliasing violations will
be detected. Using this flag is <strong>unsound</strong> (but the affected soundness rules
are experimental). Later flags take precedence: borrow tracking can be reactivated
by <code>-Zmiri-tree-borrows</code>.</li>
<li><code>-Zmiri-disable-validation</code> disables enforcing validity invariants, which are
enforced by default.  This is mostly useful to focus on other failures (such
as out-of-bounds accesses) first.  Setting this flag means Miri can miss bugs
in your program.  However, this can also help to make Miri run faster.  Using
this flag is <strong>unsound</strong>.</li>
<li><code>-Zmiri-disable-weak-memory-emulation</code> disables the emulation of some C++11 weak
memory effects.</li>
<li><code>-Zmiri-extern-so-file=&lt;path to a shared object file&gt;</code> is an experimental flag for providing support
for FFI calls. Functions not provided by that file are still executed via the usual Miri shims.
<strong>WARNING</strong>: If an invalid/incorrect <code>.so</code> file is specified, this can cause undefined behaviour in Miri itself!
And of course, Miri cannot do any checks on the actions taken by the external code.
Note that Miri has its own handling of file descriptors, so if you want to replace <em>some</em> functions
working on file descriptors, you will have to replace <em>all</em> of them, or the two kinds of
file descriptors will be mixed up.
This is <strong>work in progress</strong>; currently, only integer arguments and return values are
supported (and no, pointer/integer casts to work around this limitation will not work;
they will fail horribly). It also only works on unix hosts for now.
Follow <a href="https://github.com/rust-lang/miri/issues/2365">the discussion on supporting other types</a>.</li>
<li><code>-Zmiri-measureme=&lt;name&gt;</code> enables <code>measureme</code> profiling for the interpreted program.
This can be used to find which parts of your program are executing slowly under Miri.
The profile is written out to a file inside a directory called <code>&lt;name&gt;</code>, and can be processed
using the tools in the repository https://github.com/rust-lang/measureme.</li>
<li><code>-Zmiri-mute-stdout-stderr</code> silently ignores all writes to stdout and stderr,
but reports to the program that it did actually write. This is useful when you
are not interested in the actual program's output, but only want to see Miri's
errors and warnings.</li>
<li><code>-Zmiri-panic-on-unsupported</code> will makes some forms of unsupported functionality,
such as FFI and unsupported syscalls, panic within the context of the emulated
application instead of raising an error within the context of Miri (and halting
execution). Note that code might not expect these operations to ever panic, so
this flag can lead to strange (mis)behavior.</li>
<li><code>-Zmiri-retag-fields[=&lt;all|none|scalar&gt;]</code> controls when Stacked Borrows retagging recurses into
fields. <code>all</code> means it always recurses (the default, and equivalent to <code>-Zmiri-retag-fields</code>
without an explicit value), <code>none</code> means it never recurses, <code>scalar</code> means it only recurses for
types where we would also emit <code>noalias</code> annotations in the generated LLVM IR (types passed as
individual scalars or pairs of scalars). Setting this to <code>none</code> is <strong>unsound</strong>.</li>
<li><code>-Zmiri-tag-gc=&lt;blocks&gt;</code> configures how often the pointer tag garbage collector runs. The default
is to search for and remove unreachable tags once every <code>10000</code> basic blocks. Setting this to
<code>0</code> disables the garbage collector, which causes some programs to have explosive memory usage
and/or super-linear runtime.</li>
<li><code>-Zmiri-track-alloc-id=&lt;id1&gt;,&lt;id2&gt;,...</code> shows a backtrace when the given allocations are
being allocated or freed.  This helps in debugging memory leaks and
use after free bugs. Specifying this argument multiple times does not overwrite the previous
values, instead it appends its values to the list. Listing an id multiple times has no effect.</li>
<li><code>-Zmiri-track-call-id=&lt;id1&gt;,&lt;id2&gt;,...</code> shows a backtrace when the given call ids are
assigned to a stack frame.  This helps in debugging UB related to Stacked
Borrows "protectors". Specifying this argument multiple times does not overwrite the previous
values, instead it appends its values to the list. Listing an id multiple times has no effect.</li>
<li><code>-Zmiri-track-pointer-tag=&lt;tag1&gt;,&lt;tag2&gt;,...</code> shows a backtrace when a given pointer tag
is created and when (if ever) it is popped from a borrow stack (which is where the tag becomes invalid
and any future use of it will error).  This helps you in finding out why UB is
happening and where in your code would be a good place to look for it.
Specifying this argument multiple times does not overwrite the previous
values, instead it appends its values to the list. Listing a tag multiple times has no effect.</li>
<li><code>-Zmiri-track-weak-memory-loads</code> shows a backtrace when weak memory emulation returns an outdated
value from a load. This can help diagnose problems that disappear under
<code>-Zmiri-disable-weak-memory-emulation</code>.</li>
<li><code>-Zmiri-tree-borrows</code> replaces <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md">Stacked Borrows</a> with the <a href="https://perso.crans.org/vanille/treebor/">Tree Borrows</a> rules.
The soundness rules are already experimental without this flag, but even more
so with this flag.</li>
<li><code>-Zmiri-force-page-size=&lt;num&gt;</code> overrides the default page size for an architecture, in multiples of 1k.
<code>4</code> is default for most targets. This value should always be a power of 2 and nonzero.</li>
<li><code>-Zmiri-unique-is-unique</code> performs additional aliasing checks for <code>core::ptr::Unique</code> to ensure
that it could theoretically be considered <code>noalias</code>. This flag is experimental and has
an effect only when used with <code>-Zmiri-tree-borrows</code>.</li>
</ul>
<p>Some native rustc <code>-Z</code> flags are also very relevant for Miri:</p>
<ul>
<li><code>-Zmir-opt-level</code> controls how many MIR optimizations are performed.  Miri
overrides the default to be <code>0</code>; be advised that using any higher level can
make Miri miss bugs in your program because they got optimized away.</li>
<li><code>-Zalways-encode-mir</code> makes rustc dump MIR even for completely monomorphic
functions.  This is needed so that Miri can execute such functions, so Miri
sets this flag per default.</li>
<li><code>-Zmir-emit-retag</code> controls whether <code>Retag</code> statements are emitted. Miri
enables this per default because it is needed for <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md">Stacked Borrows</a> and <a href="https://perso.crans.org/vanille/treebor/">Tree Borrows</a>.</li>
</ul>
<p>Moreover, Miri recognizes some environment variables:</p>
<ul>
<li><code>MIRI_AUTO_OPS</code> indicates whether the automatic execution of rustfmt, clippy and toolchain setup
should be skipped. If it is set to <code>no</code>, they are skipped. This is used to allow automated IDE
actions to avoid the auto ops.</li>
<li><code>MIRI_LOG</code>, <code>MIRI_BACKTRACE</code> control logging and backtrace printing during
Miri executions, also <a href="/fork/rust/ref/master/blob/src/tools/miri/CONTRIBUTING.md#testing-the-miri-driver">see "Testing the Miri driver" in <code>CONTRIBUTING.md</code></a>.</li>
<li><code>MIRIFLAGS</code> (recognized by <code>cargo miri</code> and the test suite) defines extra
flags to be passed to Miri.</li>
<li><code>MIRI_LIB_SRC</code> defines the directory where Miri expects the sources of the
standard library that it will build and use for interpretation. This directory
must point to the <code>library</code> subdirectory of a <code>rust-lang/rust</code> repository
checkout. Note that changing files in that directory does not automatically
trigger a re-build of the standard library; you have to clear the Miri build
cache manually (on Linux, <code>rm -rf ~/.cache/miri</code>;
on Windows, <code>rmdir /S "%LOCALAPPDATA%\rust-lang\miri\cache"</code>;
and on macOS, <code>rm -rf ~/Library/Caches/org.rust-lang.miri</code>).</li>
<li><code>MIRI_SYSROOT</code> (recognized by <code>cargo miri</code> and the Miri driver) indicates the sysroot to use. When
using <code>cargo miri</code>, this skips the automatic setup -- only set this if you do not want to use the
automatically created sysroot. For directly invoking the Miri driver, this variable (or a
<code>--sysroot</code> flag) is mandatory. When invoking <code>cargo miri setup</code>, this indicates where the sysroot
will be put.</li>
<li><code>MIRI_TEST_TARGET</code> (recognized by the test suite and the <code>./miri</code> script) indicates which target
architecture to test against.  <code>miri</code> and <code>cargo miri</code> accept the <code>--target</code> flag for the same
purpose.</li>
<li><code>MIRI_TEST_THREADS</code> (recognized by the test suite): set the number of threads to use for running tests.
By default the number of cores is used.</li>
<li><code>MIRI_NO_STD</code> (recognized by <code>cargo miri</code> and the test suite) makes sure that the target's
sysroot is built without libstd. This allows testing and running no_std programs.</li>
<li><code>RUSTC_BLESS</code> (recognized by the test suite and <code>cargo-miri-test/run-test.py</code>): overwrite all
<code>stderr</code> and <code>stdout</code> files instead of checking whether the output matches.</li>
<li><code>MIRI_SKIP_UI_CHECKS</code> (recognized by the test suite): don't check whether the
<code>stderr</code> or <code>stdout</code> files match the actual output.</li>
</ul>
<p>The following environment variables are <em>internal</em> and must not be used by
anyone but Miri itself. They are used to communicate between different Miri
binaries, and as such worth documenting:</p>
<ul>
<li><code>MIRI_BE_RUSTC</code> can be set to <code>host</code> or <code>target</code>. It tells the Miri driver to
actually not interpret the code but compile it like rustc would. With <code>target</code>, Miri sets
some compiler flags to prepare the code for interpretation; with <code>host</code>, this is not done.
This environment variable is useful to be sure that the compiled <code>rlib</code>s are compatible
with Miri.</li>
<li><code>MIRI_CALLED_FROM_SETUP</code> is set during the Miri sysroot build,
which will re-invoke <code>cargo-miri</code> as the <code>rustc</code> to use for this build.</li>
<li><code>MIRI_CALLED_FROM_RUSTDOC</code> when set to any value tells <code>cargo-miri</code> that it is
running as a child process of <code>rustdoc</code>, which invokes it twice for each doc-test
and requires special treatment, most notably a check-only build before interpretation.
This is set by <code>cargo-miri</code> itself when running as a <code>rustdoc</code>-wrapper.</li>
<li><code>MIRI_CWD</code> when set to any value tells the Miri driver to change to the given
directory after loading all the source files, but before commencing
interpretation. This is useful if the interpreted program wants a different
working directory at run-time than at build-time.</li>
<li><code>MIRI_LOCAL_CRATES</code> is set by <code>cargo-miri</code> to tell the Miri driver which
crates should be given special treatment in diagnostics, in addition to the
crate currently being compiled.</li>
<li><code>MIRI_VERBOSE</code> when set to any value tells the various <code>cargo-miri</code> phases to
perform verbose logging.</li>
<li><code>MIRI_HOST_SYSROOT</code> is set by bootstrap to tell <code>cargo-miri</code> which sysroot to use for <em>host</em>
operations.</li>
</ul>
<h2>Miri <code>extern</code> functions</h2>
<p>Miri provides some <code>extern</code> functions that programs can import to access
Miri-specific functionality. They are declared in
<a href="/tests/utils/miri_extern.rs">/tests/utils/miri_extern.rs</a>.</p>
<h2>Contributing and getting help</h2>
<p>If you want to contribute to Miri, great!  Please check out our
<a href="/fork/rust/ref/master/blob/src/tools/miri/CONTRIBUTING.md">contribution guide</a>.</p>
<p>For help with running Miri, you can open an issue here on
GitHub or use the <a href="https://rust-lang.zulipchat.com/#narrow/stream/269128-miri">Miri stream on the Rust Zulip</a>.</p>
<h2>History</h2>
<p>This project began as part of an undergraduate research course in 2015 by
@solson at the <a href="https://www.usask.ca/">University of Saskatchewan</a>.  There are <a href="https://solson.me/miri-slides.pdf">slides</a> and a
<a href="https://solson.me/miri-report.pdf">report</a> available from that project.  In 2016, @oli-obk joined to prepare Miri
for eventually being used as const evaluator in the Rust compiler itself
(basically, for <code>const</code> and <code>static</code> stuff), replacing the old evaluator that
worked directly on the AST.  In 2017, @RalfJung did an internship with Mozilla
and began developing Miri towards a tool for detecting undefined behavior, and
also using Miri as a way to explore the consequences of various possible
definitions for undefined behavior in Rust.  @oli-obk's move of the Miri engine
into the compiler finally came to completion in early 2018.  Meanwhile, later
that year, @RalfJung did a second internship, developing Miri further with
support for checking basic type invariants and verifying that references are
used according to their aliasing restrictions.</p>
<h2>Bugs found by Miri</h2>
<p>Miri has already found a number of bugs in the Rust standard library and beyond, which we collect here.</p>
<p>Definite bugs found:</p>
<ul>
<li><a href="https://github.com/rust-lang/rust/issues/53566"><code>Debug for vec_deque::Iter</code> accessing uninitialized memory</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/53804"><code>Vec::into_iter</code> doing an unaligned ZST read</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/54908"><code>From&lt;&amp;[T]&gt; for Rc</code> creating a not sufficiently aligned reference</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/54957"><code>BTreeMap</code> creating a shared reference pointing to a too small allocation</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/61082"><code>Vec::append</code> creating a dangling reference</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/56319">Futures turning a shared reference into a mutable one</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/58200"><code>str</code> turning a shared reference into a mutable one</a></li>
<li><a href="https://github.com/rust-random/rand/issues/779"><code>rand</code> performing unaligned reads</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/62251">The Unix allocator calling <code>posix_memalign</code> in an invalid way</a></li>
<li><a href="https://github.com/rust-random/getrandom/pull/73"><code>getrandom</code> calling the <code>getrandom</code> syscall in an invalid way</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/69770"><code>Vec</code></a> and <a href="https://github.com/rust-lang/rust/issues/69769"><code>BTreeMap</code></a> leaking memory under some (panicky) conditions</li>
<li><a href="https://github.com/maciejhirsz/beef/issues/12"><code>beef</code> leaking memory</a></li>
<li><a href="https://github.com/Firstyear/concread/commit/b15be53b6ec076acb295a5c0483cdb4bf9be838f#diff-6282b2fc8e98bd089a1f0c86f648157cR229"><code>EbrCell</code> using uninitialized memory incorrectly</a></li>
<li><a href="https://github.com/tikv/tikv/issues/7613">TiKV performing an unaligned pointer access</a></li>
<li><a href="https://github.com/servo/servo/issues/26357"><code>servo_arc</code> creating a dangling shared reference</a></li>
<li><a href="https://github.com/tikv/tikv/pull/7751">TiKV constructing out-of-bounds pointers (and overlapping mutable references)</a></li>
<li><a href="https://github.com/hsivonen/encoding_rs/pull/53"><code>encoding_rs</code> doing out-of-bounds pointer arithmetic</a></li>
<li><a href="https://github.com/tikv/agatedb/pull/24">TiKV using <code>Vec::from_raw_parts</code> incorrectly</a></li>
<li>Incorrect doctests for <a href="https://github.com/rust-lang/rust/pull/84052"><code>AtomicPtr</code></a> and <a href="https://github.com/rust-lang/rust/pull/84053"><code>Box::from_raw_in</code></a></li>
<li><a href="https://github.com/Gankra/thin-vec/pull/27">Insufficient alignment in <code>ThinVec</code></a></li>
<li><a href="https://github.com/crossbeam-rs/crossbeam/pull/779"><code>crossbeam-epoch</code> calling <code>assume_init</code> on a partly-initialized <code>MaybeUninit</code></a></li>
<li><a href="https://github.com/dermesser/integer-encoding-rs/pull/23"><code>integer-encoding</code> dereferencing a misaligned pointer</a></li>
<li><a href="https://github.com/rkyv/rkyv/commit/a9417193a34757e12e24263178be8b2eebb72456"><code>rkyv</code> constructing a <code>Box&lt;[u8]&gt;</code> from an overaligned allocation</a></li>
<li><a href="https://github.com/vorner/arc-swap/issues/76">Data race in <code>arc-swap</code></a></li>
<li><a href="https://github.com/rust-lang/rust/issues/98498">Data race in <code>thread::scope</code></a></li>
<li><a href="https://www.reddit.com/r/rust/comments/vq3mmu/comment/ienc7t0?context=3"><code>regex</code> incorrectly handling unaligned <code>Vec&lt;u8&gt;</code> buffers</a></li>
<li><a href="https://github.com/matklad/once_cell/issues/186">Incorrect use of <code>compare_exchange_weak</code> in <code>once_cell</code></a></li>
<li><a href="https://github.com/rust-lang/rust/pull/106084">Dropping with unaligned pointers in <code>vec::IntoIter</code></a></li>
</ul>
<p>Violations of <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md">Stacked Borrows</a> found that are likely bugs (but Stacked Borrows is currently just an experiment):</p>
<ul>
<li><a href="https://github.com/rust-lang/rust/pull/56161"><code>VecDeque::drain</code> creating overlapping mutable references</a></li>
<li>Various <code>BTreeMap</code> problems
<ul>
<li><a href="https://github.com/rust-lang/rust/pull/58431"><code>BTreeMap</code> iterators creating mutable references that overlap with shared references</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/73915"><code>BTreeMap::iter_mut</code> creating overlapping mutable references</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/78477"><code>BTreeMap</code> node insertion using raw pointers outside their valid memory area</a></li>
</ul>
</li>
<li><a href="https://github.com/rust-lang/rust/pull/60072"><code>LinkedList</code> cursor insertion creating overlapping mutable references</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/60847"><code>Vec::push</code> invalidating existing references into the vector</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/68549"><code>align_to_mut</code> violating uniqueness of mutable references</a></li>
<li><a href="https://github.com/bodil/sized-chunks/issues/8"><code>sized-chunks</code> creating aliasing mutable references</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/70301"><code>String::push_str</code> invalidating existing references into the string</a></li>
<li><a href="https://github.com/dtolnay/ryu/issues/24"><code>ryu</code> using raw pointers outside their valid memory area</a></li>
<li><a href="https://github.com/rust-lang/miri/issues/1364">ink! creating overlapping mutable references</a></li>
<li><a href="https://github.com/tikv/tikv/pull/7709">TiKV creating overlapping mutable reference and raw pointer</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/70479">Windows <code>Env</code> iterator using a raw pointer outside its valid memory area</a></li>
<li><a href="https://github.com/rust-lang/rust/issues/74029"><code>VecDeque::iter_mut</code> creating overlapping mutable references</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/78602">Various standard library aliasing issues involving raw pointers</a></li>
<li><a href="https://github.com/rust-lang/rust/pull/85610"><code>&lt;[T]&gt;::copy_within</code> using a loan after invalidating it</a></li>
</ul>
<h2>Scientific papers employing Miri</h2>
<ul>
<li><a href="https://plv.mpi-sws.org/rustbelt/stacked-borrows/">Stacked Borrows: An Aliasing Model for Rust</a></li>
<li><a href="https://www.amazon.science/publications/using-lightweight-formal-methods-to-validate-a-key-value-storage-node-in-amazon-s3">Using Lightweight Formal Methods to Validate a Key-Value Storage Node in Amazon S3</a></li>
<li><a href="https://dl.acm.org/doi/10.1145/3453483.3454084">SyRust: Automatic Testing of Rust Libraries with Semantic-Aware Program Synthesis</a></li>
</ul>
<h2>License</h2>
<p>Licensed under either of</p>
<ul>
<li>Apache License, Version 2.0 (<a href="/fork/rust/ref/master/blob/src/tools/miri/LICENSE-APACHE">LICENSE-APACHE</a> or
http://www.apache.org/licenses/LICENSE-2.0)</li>
<li>MIT license (<a href="/fork/rust/ref/master/blob/src/tools/miri/LICENSE-MIT">LICENSE-MIT</a> or
http://opensource.org/licenses/MIT)</li>
</ul>
<p>at your option.</p>
<h3>Contribution</h3>
<p>Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be dual licensed as above, without any
additional terms or conditions.</p>
</section><div class="code-panel"><pre class="code src" data-hl="src/tools/miri/README.md"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"># Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">An experimental interpreter for [Rust][rust]'s</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text">[mid-level intermediate representation][mir] (MIR). It can run binaries and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text">test suites of cargo projects and detect certain classes of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text">[undefined behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">for example:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text">* Out-of-bounds memory accesses and use-after-free</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text">* Invalid use of uninitialized data</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">* Violation of intrinsic preconditions (an [`unreachable_unchecked`] being</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text">  reached, calling [`copy_nonoverlapping`] with overlapping ranges, ...)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text">* Not sufficiently aligned memory accesses and references</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text">* Violation of *some* basic type invariants (a `bool` that is not 0 or 1, for example,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text">  or an invalid enum discriminant)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text">* **Experimental**: Violations of the [Stacked Borrows] rules governing aliasing</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">  for reference types</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text">* **Experimental**: Violations of the [Tree Borrows] aliasing rules, as an optional</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text">  alternative to [Stacked Borrows]</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text">* **Experimental**: Data races</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text">On top of that, Miri will also tell you about memory leaks: when there is memory</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text">still allocated at the end of the execution, and that memory is not reachable</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text">from a global `static`, Miri will raise an error.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text">Miri supports almost all Rust language features; in particular, unwinding and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text">concurrency are properly supported (including some experimental emulation of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text">weak memory effects, i.e., reads can return outdated values).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text">You can use Miri to emulate programs on other targets, e.g. to ensure that</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text">byte-level data manipulation works correctly both on little-endian and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text">big-endian systems. See</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text">[cross-interpretation](#cross-interpretation-running-for-different-targets)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text">below.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text">Miri has already discovered some [real-world bugs](#bugs-found-by-miri). If you</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text">found a bug with Miri, we'd appreciate if you tell us and we'll add it to the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text">list!</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text">By default, Miri ensures a fully deterministic execution and isolates the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text">program from the host system. Some APIs that would usually access the host, such</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text">as gathering entropy for random number generators, environment variables, and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text">clocks, are replaced by deterministic &quot;fake&quot; implementations. Set</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text">`MIRIFLAGS=&quot;-Zmiri-disable-isolation&quot;` to access the real system APIs instead.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text">(In particular, the &quot;fake&quot; system RNG APIs make Miri **not suited for</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text">cryptographic use**! Do not generate keys using Miri.)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text">All that said, be aware that Miri will **not catch all cases of undefined</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text">behavior** in your program, and cannot run all programs:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text">* There are still plenty of open questions around the basic invariants for some</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text">  types and when these invariants even have to hold. Miri tries to avoid false</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text">  positives here, so if your program runs fine in Miri right now that is by no</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text">  means a guarantee that it is UB-free when these questions get answered.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text">    In particular, Miri does not check that references point to valid data.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text">* If the program relies on unspecified details of how data is laid out, it will</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text">  still run fine in Miri -- but might break (including causing UB) on different</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text">  compiler versions or different platforms.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text">* Program execution is non-deterministic when it depends, for example, on where</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text">  exactly in memory allocations end up, or on the exact interleaving of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text">  concurrent threads. Miri tests one of many possible executions of your</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text">  program. You can alleviate this to some extent by running Miri with different</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text">  values for `-Zmiri-seed`, but that will still by far not explore all possible</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text">  executions.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text">* Miri runs the program as a platform-independent interpreter, so the program</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text">  has no access to most platform-specific APIs or FFI. A few APIs have been</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text">  implemented (such as printing to stdout, accessing environment variables, and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text">  basic file system access) but most have not: for example, Miri currently does</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text">  not support networking. System API support varies between targets; if you run</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text">  on Windows it is a good idea to use `--target x86_64-unknown-linux-gnu` to get</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text">  better support.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text">* Weak memory emulation may [produce weak behaviours](https://github.com/rust-lang/miri/issues/2301)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text">  unobservable by compiled programs running on real hardware when `SeqCst` fences are used, and it</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text">  cannot produce all behaviors possibly observable on real hardware.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L76" href="#L76">76</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L77" href="#L77">77</a><span class="code-text">Moreover, Miri fundamentally cannot tell you whether your code is *sound*. [Soundness] is the property</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L78" href="#L78">78</a><span class="code-text">of never causing undefined behavior when invoked from arbitrary safe code, even in combination with</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L79" href="#L79">79</a><span class="code-text">other sound code. In contrast, Miri can just tell you if *a particular way of interacting with your</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L80" href="#L80">80</a><span class="code-text">code* (e.g., a test suite) causes any undefined behavior. It is up to you to ensure sufficient</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L81" href="#L81">81</a><span class="code-text">coverage.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L82" href="#L82">82</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L83" href="#L83">83</a><span class="code-text">[rust]: https://www.rust-lang.org/</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L84" href="#L84">84</a><span class="code-text">[mir]: https://github.com/rust-lang/rfcs/blob/master/text/1211-mir.md</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L85" href="#L85">85</a><span class="code-text">[`unreachable_unchecked`]: https://doc.rust-lang.org/stable/std/hint/fn.unreachable_unchecked.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L86" href="#L86">86</a><span class="code-text">[`copy_nonoverlapping`]: https://doc.rust-lang.org/stable/std/ptr/fn.copy_nonoverlapping.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L87" href="#L87">87</a><span class="code-text">[Stacked Borrows]: https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L88" href="#L88">88</a><span class="code-text">[Tree Borrows]: https://perso.crans.org/vanille/treebor/</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L89" href="#L89">89</a><span class="code-text">[Soundness]: https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#soundness-of-code--of-a-library</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L90" href="#L90">90</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L91" href="#L91">91</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L92" href="#L92">92</a><span class="code-text">## Using Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L93" href="#L93">93</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L94" href="#L94">94</a><span class="code-text">Install Miri on Rust nightly via `rustup`:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L95" href="#L95">95</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L96" href="#L96">96</a><span class="code-text">```sh</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L97" href="#L97">97</a><span class="code-text">rustup +nightly component add miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L98" href="#L98">98</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L99" href="#L99">99</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L100" href="#L100">100</a><span class="code-text">If `rustup` says the `miri` component is unavailable, that's because not all</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L101" href="#L101">101</a><span class="code-text">nightly releases come with all tools. Check out</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L102" href="#L102">102</a><span class="code-text">[this website](https://rust-lang.github.io/rustup-components-history) to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L103" href="#L103">103</a><span class="code-text">determine a nightly version that comes with Miri and install that using `rustup</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L104" href="#L104">104</a><span class="code-text">toolchain install nightly-YYYY-MM-DD`. Either way, all of the following commands</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L105" href="#L105">105</a><span class="code-text">assume the right toolchain is pinned via `rustup override set nightly` or</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L106" href="#L106">106</a><span class="code-text">`rustup override set nightly-YYYY-MM-DD`. (Alternatively, use `cargo</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L107" href="#L107">107</a><span class="code-text">+nightly`/`cargo +nightly-YYYY-MM-DD` for each of the following commands.)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L108" href="#L108">108</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L109" href="#L109">109</a><span class="code-text">Now you can run your project in Miri:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L110" href="#L110">110</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L111" href="#L111">111</a><span class="code-text">1. Run `cargo clean` to eliminate any cached dependencies. Miri needs your</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L112" href="#L112">112</a><span class="code-text">   dependencies to be compiled the right way, that would not happen if they have</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L113" href="#L113">113</a><span class="code-text">   previously already been compiled.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L114" href="#L114">114</a><span class="code-text">2. To run all tests in your project through Miri, use `cargo miri test`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L115" href="#L115">115</a><span class="code-text">3. If you have a binary project, you can run it through Miri using `cargo miri run`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L116" href="#L116">116</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L117" href="#L117">117</a><span class="code-text">The first time you run Miri, it will perform some extra setup and install some</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L118" href="#L118">118</a><span class="code-text">dependencies. It will ask you for confirmation before installing anything.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L119" href="#L119">119</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L120" href="#L120">120</a><span class="code-text">`cargo miri run/test` supports the exact same flags as `cargo run/test`. For</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L121" href="#L121">121</a><span class="code-text">example, `cargo miri test filter` only runs the tests containing `filter` in</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L122" href="#L122">122</a><span class="code-text">their name.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L123" href="#L123">123</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L124" href="#L124">124</a><span class="code-text">You can pass arguments to Miri via `MIRIFLAGS`. For example,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L125" href="#L125">125</a><span class="code-text">`MIRIFLAGS=&quot;-Zmiri-disable-stacked-borrows&quot; cargo miri run` runs the program</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L126" href="#L126">126</a><span class="code-text">without checking the aliasing of references.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L127" href="#L127">127</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L128" href="#L128">128</a><span class="code-text">When compiling code via `cargo miri`, the `cfg(miri)` config flag is set for code</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L129" href="#L129">129</a><span class="code-text">that will be interpret under Miri. You can use this to ignore test cases that fail</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L130" href="#L130">130</a><span class="code-text">under Miri because they do things Miri does not support:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L131" href="#L131">131</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L132" href="#L132">132</a><span class="code-text">```rust</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L133" href="#L133">133</a><span class="code-text">#[test]</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L134" href="#L134">134</a><span class="code-text">#[cfg_attr(miri, ignore)]</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L135" href="#L135">135</a><span class="code-text">fn does_not_work_on_miri() {</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L136" href="#L136">136</a><span class="code-text">    tokio::run(futures::future::ok::&lt;_, ()&gt;(()));</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L137" href="#L137">137</a><span class="code-text">}</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L138" href="#L138">138</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L139" href="#L139">139</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L140" href="#L140">140</a><span class="code-text">There is no way to list all the infinite things Miri cannot do, but the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L141" href="#L141">141</a><span class="code-text">interpreter will explicitly tell you when it finds something unsupported:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L142" href="#L142">142</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L143" href="#L143">143</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L144" href="#L144">144</a><span class="code-text">error: unsupported operation: can't call foreign function: bind</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L145" href="#L145">145</a><span class="code-text">    ...</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L146" href="#L146">146</a><span class="code-text">    = help: this is likely not a bug in the program; it indicates that the program \</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L147" href="#L147">147</a><span class="code-text">            performed an operation that the interpreter does not support</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L148" href="#L148">148</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L149" href="#L149">149</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L150" href="#L150">150</a><span class="code-text">### Cross-interpretation: running for different targets</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L151" href="#L151">151</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L152" href="#L152">152</a><span class="code-text">Miri can not only run a binary or test suite for your host target, it can also</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L153" href="#L153">153</a><span class="code-text">perform cross-interpretation for arbitrary foreign targets: `cargo miri run</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L154" href="#L154">154</a><span class="code-text">--target x86_64-unknown-linux-gnu` will run your program as if it was a Linux</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L155" href="#L155">155</a><span class="code-text">program, no matter your host OS. This is particularly useful if you are using</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L156" href="#L156">156</a><span class="code-text">Windows, as the Linux target is much better supported than Windows targets.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L157" href="#L157">157</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L158" href="#L158">158</a><span class="code-text">You can also use this to test platforms with different properties than your host</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L159" href="#L159">159</a><span class="code-text">platform. For example `cargo miri test --target s390x-unknown-linux-gnu`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L160" href="#L160">160</a><span class="code-text">will run your test suite on a big-endian target, which is useful for testing</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L161" href="#L161">161</a><span class="code-text">endian-sensitive code.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L162" href="#L162">162</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L163" href="#L163">163</a><span class="code-text">### Running Miri on CI</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L164" href="#L164">164</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L165" href="#L165">165</a><span class="code-text">To run Miri on CI, make sure that you handle the case where the latest nightly</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L166" href="#L166">166</a><span class="code-text">does not ship the Miri component because it currently does not build. `rustup</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L167" href="#L167">167</a><span class="code-text">toolchain install --component` knows how to handle this situation, so the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L168" href="#L168">168</a><span class="code-text">following snippet should always work:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L169" href="#L169">169</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L170" href="#L170">170</a><span class="code-text">```sh</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L171" href="#L171">171</a><span class="code-text">rustup toolchain install nightly --component miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L172" href="#L172">172</a><span class="code-text">rustup override set nightly</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L173" href="#L173">173</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L174" href="#L174">174</a><span class="code-text">cargo miri test</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L175" href="#L175">175</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L176" href="#L176">176</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L177" href="#L177">177</a><span class="code-text">Here is an example job for GitHub Actions:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L178" href="#L178">178</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L179" href="#L179">179</a><span class="code-text">```yaml</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L180" href="#L180">180</a><span class="code-text">  miri:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L181" href="#L181">181</a><span class="code-text">    name: &quot;Miri&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L182" href="#L182">182</a><span class="code-text">    runs-on: ubuntu-latest</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L183" href="#L183">183</a><span class="code-text">    steps:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L184" href="#L184">184</a><span class="code-text">      - uses: actions/checkout@v3</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L185" href="#L185">185</a><span class="code-text">      - name: Install Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L186" href="#L186">186</a><span class="code-text">        run: |</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L187" href="#L187">187</a><span class="code-text">          rustup toolchain install nightly --component miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L188" href="#L188">188</a><span class="code-text">          rustup override set nightly</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L189" href="#L189">189</a><span class="code-text">          cargo miri setup</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L190" href="#L190">190</a><span class="code-text">      - name: Test with Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L191" href="#L191">191</a><span class="code-text">        run: cargo miri test</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L192" href="#L192">192</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L193" href="#L193">193</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L194" href="#L194">194</a><span class="code-text">The explicit `cargo miri setup` helps to keep the output of the actual test step</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L195" href="#L195">195</a><span class="code-text">clean.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L196" href="#L196">196</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L197" href="#L197">197</a><span class="code-text">### Testing for alignment issues</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L198" href="#L198">198</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L199" href="#L199">199</a><span class="code-text">Miri can sometimes miss misaligned accesses since allocations can &quot;happen to be&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L200" href="#L200">200</a><span class="code-text">aligned just right. You can use `-Zmiri-symbolic-alignment-check` to definitely</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L201" href="#L201">201</a><span class="code-text">catch all such issues, but that flag will also cause false positives when code</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L202" href="#L202">202</a><span class="code-text">does manual pointer arithmetic to account for alignment. Another alternative is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L203" href="#L203">203</a><span class="code-text">to call Miri with various values for `-Zmiri-seed`; that will alter the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L204" href="#L204">204</a><span class="code-text">randomness that is used to determine allocation base addresses. The following</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L205" href="#L205">205</a><span class="code-text">snippet calls Miri in a loop with different values for the seed:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L206" href="#L206">206</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L207" href="#L207">207</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L208" href="#L208">208</a><span class="code-text">for SEED in $(seq 0 255); do</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L209" href="#L209">209</a><span class="code-text">  echo &quot;Trying seed: $SEED&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L210" href="#L210">210</a><span class="code-text">  MIRIFLAGS=-Zmiri-seed=$SEED cargo miri test || { echo &quot;Failing seed: $SEED&quot;; break; };</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L211" href="#L211">211</a><span class="code-text">done</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L212" href="#L212">212</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L213" href="#L213">213</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L214" href="#L214">214</a><span class="code-text">### Supported targets</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L215" href="#L215">215</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L216" href="#L216">216</a><span class="code-text">Miri does not support all targets supported by Rust. The good news, however, is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L217" href="#L217">217</a><span class="code-text">that no matter your host OS/platform, it is easy to run code for *any* target</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L218" href="#L218">218</a><span class="code-text">using `--target`!</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L219" href="#L219">219</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L220" href="#L220">220</a><span class="code-text">The following targets are tested on CI and thus should always work (to the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L221" href="#L221">221</a><span class="code-text">degree documented below):</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L222" href="#L222">222</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L223" href="#L223">223</a><span class="code-text">- All Rust [Tier 1 targets](https://doc.rust-lang.org/rustc/platform-support.html) are supported by</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L224" href="#L224">224</a><span class="code-text">  Miri. They are all checked on Miri's CI, and some (at least one per OS) are even checked on every</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L225" href="#L225">225</a><span class="code-text">  Rust PR, so the shipped Miri should always work on these targets.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L226" href="#L226">226</a><span class="code-text">- We also support `s390x-unknown-linux-gnu` as our &quot;big-endian target of choice&quot;.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L227" href="#L227">227</a><span class="code-text">- For every other target with OS `linux`, `macos`, or `windows`, Miri should generally work, but we</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L228" href="#L228">228</a><span class="code-text">  make no promises.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L229" href="#L229">229</a><span class="code-text">- For targets on other operating systems, even basic operations such as printing to the standard</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L230" href="#L230">230</a><span class="code-text">  output might not work, and Miri might fail before even reaching the `main` function.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L231" href="#L231">231</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L232" href="#L232">232</a><span class="code-text">However, even for targets that we do support, the degree of support for accessing platform APIs</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L233" href="#L233">233</a><span class="code-text">(such as the file system) differs between targets: generally, Linux targets have the best support,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L234" href="#L234">234</a><span class="code-text">and macOS targets are usually on par. Windows is supported less well.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L235" href="#L235">235</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L236" href="#L236">236</a><span class="code-text">### Running tests in parallel</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L237" href="#L237">237</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L238" href="#L238">238</a><span class="code-text">Though it implements Rust threading, Miri itself is a single-threaded interpreter.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L239" href="#L239">239</a><span class="code-text">This means that when running `cargo miri test`, you will probably see a dramatic</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L240" href="#L240">240</a><span class="code-text">increase in the amount of time it takes to run your whole test suite due to the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L241" href="#L241">241</a><span class="code-text">inherent interpreter slowdown and a loss of parallelism.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L242" href="#L242">242</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L243" href="#L243">243</a><span class="code-text">You can get your test suite's parallelism back by running `cargo miri nextest run -jN`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L244" href="#L244">244</a><span class="code-text">(note that you will need [`cargo-nextest`](https://nexte.st) installed).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L245" href="#L245">245</a><span class="code-text">This works because `cargo-nextest` collects a list of all tests then launches a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L246" href="#L246">246</a><span class="code-text">separate `cargo miri run` for each test. You will need to specify a `-j` or `--test-threads`;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L247" href="#L247">247</a><span class="code-text">by default `cargo miri nextest run` runs one test at a time. For more details, see the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L248" href="#L248">248</a><span class="code-text">[`cargo-nextest` Miri documentation](https://nexte.st/book/miri.html).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L249" href="#L249">249</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L250" href="#L250">250</a><span class="code-text">Note: This one-test-per-process model means that `cargo miri test` is able to detect data</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L251" href="#L251">251</a><span class="code-text">races where two tests race on a shared resource, but `cargo miri nextest run` will not detect</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L252" href="#L252">252</a><span class="code-text">such races.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L253" href="#L253">253</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L254" href="#L254">254</a><span class="code-text">Note: `cargo-nextest` does not support doctests, see https://github.com/nextest-rs/nextest/issues/16</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L255" href="#L255">255</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L256" href="#L256">256</a><span class="code-text">### Common Problems</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L257" href="#L257">257</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L258" href="#L258">258</a><span class="code-text">When using the above instructions, you may encounter a number of confusing compiler</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L259" href="#L259">259</a><span class="code-text">errors.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L260" href="#L260">260</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L261" href="#L261">261</a><span class="code-text">#### &quot;note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L262" href="#L262">262</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L263" href="#L263">263</a><span class="code-text">You may see this when trying to get Miri to display a backtrace. By default, Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L264" href="#L264">264</a><span class="code-text">doesn't expose any environment to the program, so running</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L265" href="#L265">265</a><span class="code-text">`RUST_BACKTRACE=1 cargo miri test` will not do what you expect.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L266" href="#L266">266</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L267" href="#L267">267</a><span class="code-text">To get a backtrace, you need to disable isolation</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L268" href="#L268">268</a><span class="code-text">[using `-Zmiri-disable-isolation`][miri-flags]:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L269" href="#L269">269</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L270" href="#L270">270</a><span class="code-text">```sh</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L271" href="#L271">271</a><span class="code-text">RUST_BACKTRACE=1 MIRIFLAGS=&quot;-Zmiri-disable-isolation&quot; cargo miri test</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L272" href="#L272">272</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L273" href="#L273">273</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L274" href="#L274">274</a><span class="code-text">#### &quot;found possibly newer version of crate `std` which `&lt;dependency&gt;` depends on&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L275" href="#L275">275</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L276" href="#L276">276</a><span class="code-text">Your build directory may contain artifacts from an earlier build that have/have</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L277" href="#L277">277</a><span class="code-text">not been built for Miri. Run `cargo clean` before switching from non-Miri to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L278" href="#L278">278</a><span class="code-text">Miri builds and vice-versa.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L279" href="#L279">279</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L280" href="#L280">280</a><span class="code-text">#### &quot;found crate `std` compiled by an incompatible version of rustc&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L281" href="#L281">281</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L282" href="#L282">282</a><span class="code-text">You may be running `cargo miri` with a different compiler version than the one</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L283" href="#L283">283</a><span class="code-text">used to build the custom libstd that Miri uses, and Miri failed to detect that.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L284" href="#L284">284</a><span class="code-text">Try deleting `~/.cache/miri`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L285" href="#L285">285</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L286" href="#L286">286</a><span class="code-text">#### &quot;no mir for `std::rt::lang_start_internal`&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L287" href="#L287">287</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L288" href="#L288">288</a><span class="code-text">This means the sysroot you are using was not compiled with Miri in mind.  This</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L289" href="#L289">289</a><span class="code-text">should never happen when you use `cargo miri` because that takes care of setting</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L290" href="#L290">290</a><span class="code-text">up the sysroot.  If you are using `miri` (the Miri driver) directly, see the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L291" href="#L291">291</a><span class="code-text">[contributors' guide](CONTRIBUTING.md) for how to use `./miri` to best do that.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L292" href="#L292">292</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L293" href="#L293">293</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L294" href="#L294">294</a><span class="code-text">## Miri `-Z` flags and environment variables</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L295" href="#L295">295</a><span class="code-text">[miri-flags]: #miri--z-flags-and-environment-variables</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L296" href="#L296">296</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L297" href="#L297">297</a><span class="code-text">Miri adds its own set of `-Z` flags, which are usually set via the `MIRIFLAGS`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L298" href="#L298">298</a><span class="code-text">environment variable. We first document the most relevant and most commonly used flags:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L299" href="#L299">299</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L300" href="#L300">300</a><span class="code-text">* `-Zmiri-compare-exchange-weak-failure-rate=&lt;rate&gt;` changes the failure rate of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L301" href="#L301">301</a><span class="code-text">  `compare_exchange_weak` operations. The default is `0.8` (so 4 out of 5 weak ops will fail).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L302" href="#L302">302</a><span class="code-text">  You can change it to any value between `0.0` and `1.0`, where `1.0` means it</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L303" href="#L303">303</a><span class="code-text">  will always fail and `0.0` means it will never fail. Note than setting it to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L304" href="#L304">304</a><span class="code-text">  `1.0` will likely cause hangs, since it means programs using</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L305" href="#L305">305</a><span class="code-text">  `compare_exchange_weak` cannot make progress.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L306" href="#L306">306</a><span class="code-text">* `-Zmiri-disable-isolation` disables host isolation.  As a consequence,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L307" href="#L307">307</a><span class="code-text">  the program has access to host resources such as environment variables, file</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L308" href="#L308">308</a><span class="code-text">  systems, and randomness.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L309" href="#L309">309</a><span class="code-text">* `-Zmiri-disable-leak-backtraces` disables backtraces reports for memory leaks. By default, a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L310" href="#L310">310</a><span class="code-text">  backtrace is captured for every allocation when it is created, just in case it leaks. This incurs</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L311" href="#L311">311</a><span class="code-text">  some memory overhead to store data that is almost never used. This flag is implied by</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L312" href="#L312">312</a><span class="code-text">  `-Zmiri-ignore-leaks`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L313" href="#L313">313</a><span class="code-text">* `-Zmiri-env-forward=&lt;var&gt;` forwards the `var` environment variable to the interpreted program. Can</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L314" href="#L314">314</a><span class="code-text">  be used multiple times to forward several variables. Execution will still be deterministic if the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L315" href="#L315">315</a><span class="code-text">  value of forwarded variables stays the same. Has no effect if `-Zmiri-disable-isolation` is set.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L316" href="#L316">316</a><span class="code-text">* `-Zmiri-ignore-leaks` disables the memory leak checker, and also allows some</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L317" href="#L317">317</a><span class="code-text">  remaining threads to exist when the main thread exits.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L318" href="#L318">318</a><span class="code-text">* `-Zmiri-isolation-error=&lt;action&gt;` configures Miri's response to operations</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L319" href="#L319">319</a><span class="code-text">  requiring host access while isolation is enabled. `abort`, `hide`, `warn`,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L320" href="#L320">320</a><span class="code-text">  and `warn-nobacktrace` are the supported actions. The default is to `abort`,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L321" href="#L321">321</a><span class="code-text">  which halts the machine. Some (but not all) operations also support continuing</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L322" href="#L322">322</a><span class="code-text">  execution with a &quot;permission denied&quot; error being returned to the program.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L323" href="#L323">323</a><span class="code-text">  `warn` prints a full backtrace when that happens; `warn-nobacktrace` is less</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L324" href="#L324">324</a><span class="code-text">  verbose. `hide` hides the warning entirely.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L325" href="#L325">325</a><span class="code-text">* `-Zmiri-num-cpus` states the number of available CPUs to be reported by miri. By default, the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L326" href="#L326">326</a><span class="code-text">  number of available CPUs is `1`. Note that this flag does not affect how miri handles threads in</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L327" href="#L327">327</a><span class="code-text">  any way.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L328" href="#L328">328</a><span class="code-text">* `-Zmiri-permissive-provenance` disables the warning for integer-to-pointer casts and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L329" href="#L329">329</a><span class="code-text">  [`ptr::from_exposed_addr`](https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L330" href="#L330">330</a><span class="code-text">  This will necessarily miss some bugs as those operations are not efficiently and accurately</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L331" href="#L331">331</a><span class="code-text">  implementable in a sanitizer, but it will only miss bugs that concern memory/pointers which is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L332" href="#L332">332</a><span class="code-text">  subject to these operations.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L333" href="#L333">333</a><span class="code-text">* `-Zmiri-preemption-rate` configures the probability that at the end of a basic block, the active</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L334" href="#L334">334</a><span class="code-text">  thread will be preempted. The default is `0.01` (i.e., 1%). Setting this to `0` disables</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L335" href="#L335">335</a><span class="code-text">  preemption.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L336" href="#L336">336</a><span class="code-text">* `-Zmiri-report-progress` makes Miri print the current stacktrace every now and then, so you can</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L337" href="#L337">337</a><span class="code-text">  tell what it is doing when a program just keeps running. You can customize how frequently the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L338" href="#L338">338</a><span class="code-text">  report is printed via `-Zmiri-report-progress=&lt;blocks&gt;`, which prints the report every N basic</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L339" href="#L339">339</a><span class="code-text">  blocks.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L340" href="#L340">340</a><span class="code-text">* `-Zmiri-seed=&lt;num&gt;` configures the seed of the RNG that Miri uses to resolve non-determinism. This</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L341" href="#L341">341</a><span class="code-text">  RNG is used to pick base addresses for allocations, to determine preemption and failure of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L342" href="#L342">342</a><span class="code-text">  `compare_exchange_weak`, and to control store buffering for weak memory emulation. When isolation</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L343" href="#L343">343</a><span class="code-text">  is enabled (the default), this is also used to emulate system entropy. The default seed is 0. You</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L344" href="#L344">344</a><span class="code-text">  can increase test coverage by running Miri multiple times with different seeds.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L345" href="#L345">345</a><span class="code-text">* `-Zmiri-strict-provenance` enables [strict</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L346" href="#L346">346</a><span class="code-text">  provenance](https://github.com/rust-lang/rust/issues/95228) checking in Miri. This means that</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L347" href="#L347">347</a><span class="code-text">  casting an integer to a pointer yields a result with 'invalid' provenance, i.e., with provenance</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L348" href="#L348">348</a><span class="code-text">  that cannot be used for any memory access.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L349" href="#L349">349</a><span class="code-text">* `-Zmiri-symbolic-alignment-check` makes the alignment check more strict.  By default, alignment is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L350" href="#L350">350</a><span class="code-text">  checked by casting the pointer to an integer, and making sure that is a multiple of the alignment.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L351" href="#L351">351</a><span class="code-text">  This can lead to cases where a program passes the alignment check by pure chance, because things</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L352" href="#L352">352</a><span class="code-text">  &quot;happened to be&quot; sufficiently aligned -- there is no UB in this execution but there would be UB in</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L353" href="#L353">353</a><span class="code-text">  others.  To avoid such cases, the symbolic alignment check only takes into account the requested</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L354" href="#L354">354</a><span class="code-text">  alignment of the relevant allocation, and the offset into that allocation.  This avoids missing</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L355" href="#L355">355</a><span class="code-text">  such bugs, but it also incurs some false positives when the code does manual integer arithmetic to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L356" href="#L356">356</a><span class="code-text">  ensure alignment.  (The standard library `align_to` method works fine in both modes; under</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L357" href="#L357">357</a><span class="code-text">  symbolic alignment it only fills the middle slice when the allocation guarantees sufficient</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L358" href="#L358">358</a><span class="code-text">  alignment.)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L359" href="#L359">359</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L360" href="#L360">360</a><span class="code-text">The remaining flags are for advanced use only, and more likely to change or be removed.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L361" href="#L361">361</a><span class="code-text">Some of these are **unsound**, which means they can lead</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L362" href="#L362">362</a><span class="code-text">to Miri failing to detect cases of undefined behavior in a program.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L363" href="#L363">363</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L364" href="#L364">364</a><span class="code-text">* `-Zmiri-disable-abi-check` disables checking [function ABI]. Using this flag</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L365" href="#L365">365</a><span class="code-text">  is **unsound**. This flag is **deprecated**.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L366" href="#L366">366</a><span class="code-text">* `-Zmiri-disable-alignment-check` disables checking pointer alignment, so you</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L367" href="#L367">367</a><span class="code-text">  can focus on other failures, but it means Miri can miss bugs in your program.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L368" href="#L368">368</a><span class="code-text">  Using this flag is **unsound**.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L369" href="#L369">369</a><span class="code-text">* `-Zmiri-disable-data-race-detector` disables checking for data races.  Using</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L370" href="#L370">370</a><span class="code-text">  this flag is **unsound**. This implies `-Zmiri-disable-weak-memory-emulation`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L371" href="#L371">371</a><span class="code-text">* `-Zmiri-disable-stacked-borrows` disables checking the experimental</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L372" href="#L372">372</a><span class="code-text">  aliasing rules to track borrows ([Stacked Borrows] and [Tree Borrows]).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L373" href="#L373">373</a><span class="code-text">  This can make Miri run faster, but it also means no aliasing violations will</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L374" href="#L374">374</a><span class="code-text">  be detected. Using this flag is **unsound** (but the affected soundness rules</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L375" href="#L375">375</a><span class="code-text">  are experimental). Later flags take precedence: borrow tracking can be reactivated</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L376" href="#L376">376</a><span class="code-text">  by `-Zmiri-tree-borrows`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L377" href="#L377">377</a><span class="code-text">* `-Zmiri-disable-validation` disables enforcing validity invariants, which are</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L378" href="#L378">378</a><span class="code-text">  enforced by default.  This is mostly useful to focus on other failures (such</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L379" href="#L379">379</a><span class="code-text">  as out-of-bounds accesses) first.  Setting this flag means Miri can miss bugs</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L380" href="#L380">380</a><span class="code-text">  in your program.  However, this can also help to make Miri run faster.  Using</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L381" href="#L381">381</a><span class="code-text">  this flag is **unsound**.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L382" href="#L382">382</a><span class="code-text">* `-Zmiri-disable-weak-memory-emulation` disables the emulation of some C++11 weak</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L383" href="#L383">383</a><span class="code-text">  memory effects.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L384" href="#L384">384</a><span class="code-text">* `-Zmiri-extern-so-file=&lt;path to a shared object file&gt;` is an experimental flag for providing support</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L385" href="#L385">385</a><span class="code-text">  for FFI calls. Functions not provided by that file are still executed via the usual Miri shims.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L386" href="#L386">386</a><span class="code-text">  **WARNING**: If an invalid/incorrect `.so` file is specified, this can cause undefined behaviour in Miri itself!</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L387" href="#L387">387</a><span class="code-text">  And of course, Miri cannot do any checks on the actions taken by the external code.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L388" href="#L388">388</a><span class="code-text">  Note that Miri has its own handling of file descriptors, so if you want to replace *some* functions</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L389" href="#L389">389</a><span class="code-text">  working on file descriptors, you will have to replace *all* of them, or the two kinds of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L390" href="#L390">390</a><span class="code-text">  file descriptors will be mixed up.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L391" href="#L391">391</a><span class="code-text">  This is **work in progress**; currently, only integer arguments and return values are</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L392" href="#L392">392</a><span class="code-text">  supported (and no, pointer/integer casts to work around this limitation will not work;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L393" href="#L393">393</a><span class="code-text">  they will fail horribly). It also only works on unix hosts for now.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L394" href="#L394">394</a><span class="code-text">  Follow [the discussion on supporting other types](https://github.com/rust-lang/miri/issues/2365).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L395" href="#L395">395</a><span class="code-text">* `-Zmiri-measureme=&lt;name&gt;` enables `measureme` profiling for the interpreted program.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L396" href="#L396">396</a><span class="code-text">   This can be used to find which parts of your program are executing slowly under Miri.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L397" href="#L397">397</a><span class="code-text">   The profile is written out to a file inside a directory called `&lt;name&gt;`, and can be processed</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L398" href="#L398">398</a><span class="code-text">   using the tools in the repository https://github.com/rust-lang/measureme.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L399" href="#L399">399</a><span class="code-text">* `-Zmiri-mute-stdout-stderr` silently ignores all writes to stdout and stderr,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L400" href="#L400">400</a><span class="code-text">  but reports to the program that it did actually write. This is useful when you</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L401" href="#L401">401</a><span class="code-text">  are not interested in the actual program's output, but only want to see Miri's</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L402" href="#L402">402</a><span class="code-text">  errors and warnings.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L403" href="#L403">403</a><span class="code-text">* `-Zmiri-panic-on-unsupported` will makes some forms of unsupported functionality,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L404" href="#L404">404</a><span class="code-text">  such as FFI and unsupported syscalls, panic within the context of the emulated</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L405" href="#L405">405</a><span class="code-text">  application instead of raising an error within the context of Miri (and halting</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L406" href="#L406">406</a><span class="code-text">  execution). Note that code might not expect these operations to ever panic, so</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L407" href="#L407">407</a><span class="code-text">  this flag can lead to strange (mis)behavior.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L408" href="#L408">408</a><span class="code-text">* `-Zmiri-retag-fields[=&lt;all|none|scalar&gt;]` controls when Stacked Borrows retagging recurses into</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L409" href="#L409">409</a><span class="code-text">  fields. `all` means it always recurses (the default, and equivalent to `-Zmiri-retag-fields`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L410" href="#L410">410</a><span class="code-text">  without an explicit value), `none` means it never recurses, `scalar` means it only recurses for</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L411" href="#L411">411</a><span class="code-text">  types where we would also emit `noalias` annotations in the generated LLVM IR (types passed as</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L412" href="#L412">412</a><span class="code-text">  individual scalars or pairs of scalars). Setting this to `none` is **unsound**.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L413" href="#L413">413</a><span class="code-text">* `-Zmiri-tag-gc=&lt;blocks&gt;` configures how often the pointer tag garbage collector runs. The default</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L414" href="#L414">414</a><span class="code-text">  is to search for and remove unreachable tags once every `10000` basic blocks. Setting this to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L415" href="#L415">415</a><span class="code-text">  `0` disables the garbage collector, which causes some programs to have explosive memory usage</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L416" href="#L416">416</a><span class="code-text">  and/or super-linear runtime.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L417" href="#L417">417</a><span class="code-text">* `-Zmiri-track-alloc-id=&lt;id1&gt;,&lt;id2&gt;,...` shows a backtrace when the given allocations are</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L418" href="#L418">418</a><span class="code-text">  being allocated or freed.  This helps in debugging memory leaks and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L419" href="#L419">419</a><span class="code-text">  use after free bugs. Specifying this argument multiple times does not overwrite the previous</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L420" href="#L420">420</a><span class="code-text">  values, instead it appends its values to the list. Listing an id multiple times has no effect.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L421" href="#L421">421</a><span class="code-text">* `-Zmiri-track-call-id=&lt;id1&gt;,&lt;id2&gt;,...` shows a backtrace when the given call ids are</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L422" href="#L422">422</a><span class="code-text">  assigned to a stack frame.  This helps in debugging UB related to Stacked</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L423" href="#L423">423</a><span class="code-text">  Borrows &quot;protectors&quot;. Specifying this argument multiple times does not overwrite the previous</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L424" href="#L424">424</a><span class="code-text">  values, instead it appends its values to the list. Listing an id multiple times has no effect.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L425" href="#L425">425</a><span class="code-text">* `-Zmiri-track-pointer-tag=&lt;tag1&gt;,&lt;tag2&gt;,...` shows a backtrace when a given pointer tag</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L426" href="#L426">426</a><span class="code-text">  is created and when (if ever) it is popped from a borrow stack (which is where the tag becomes invalid</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L427" href="#L427">427</a><span class="code-text">  and any future use of it will error).  This helps you in finding out why UB is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L428" href="#L428">428</a><span class="code-text">  happening and where in your code would be a good place to look for it.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L429" href="#L429">429</a><span class="code-text">  Specifying this argument multiple times does not overwrite the previous</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L430" href="#L430">430</a><span class="code-text">  values, instead it appends its values to the list. Listing a tag multiple times has no effect.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L431" href="#L431">431</a><span class="code-text">* `-Zmiri-track-weak-memory-loads` shows a backtrace when weak memory emulation returns an outdated</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L432" href="#L432">432</a><span class="code-text">  value from a load. This can help diagnose problems that disappear under</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L433" href="#L433">433</a><span class="code-text">  `-Zmiri-disable-weak-memory-emulation`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L434" href="#L434">434</a><span class="code-text">* `-Zmiri-tree-borrows` replaces [Stacked Borrows] with the [Tree Borrows] rules.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L435" href="#L435">435</a><span class="code-text">  The soundness rules are already experimental without this flag, but even more</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L436" href="#L436">436</a><span class="code-text">  so with this flag.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L437" href="#L437">437</a><span class="code-text">* `-Zmiri-force-page-size=&lt;num&gt;` overrides the default page size for an architecture, in multiples of 1k.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L438" href="#L438">438</a><span class="code-text">  `4` is default for most targets. This value should always be a power of 2 and nonzero.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L439" href="#L439">439</a><span class="code-text">* `-Zmiri-unique-is-unique` performs additional aliasing checks for `core::ptr::Unique` to ensure</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L440" href="#L440">440</a><span class="code-text">  that it could theoretically be considered `noalias`. This flag is experimental and has</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L441" href="#L441">441</a><span class="code-text">  an effect only when used with `-Zmiri-tree-borrows`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L442" href="#L442">442</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L443" href="#L443">443</a><span class="code-text">[function ABI]: https://doc.rust-lang.org/reference/items/functions.html#extern-function-qualifier</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L444" href="#L444">444</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L445" href="#L445">445</a><span class="code-text">Some native rustc `-Z` flags are also very relevant for Miri:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L446" href="#L446">446</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L447" href="#L447">447</a><span class="code-text">* `-Zmir-opt-level` controls how many MIR optimizations are performed.  Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L448" href="#L448">448</a><span class="code-text">  overrides the default to be `0`; be advised that using any higher level can</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L449" href="#L449">449</a><span class="code-text">  make Miri miss bugs in your program because they got optimized away.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L450" href="#L450">450</a><span class="code-text">* `-Zalways-encode-mir` makes rustc dump MIR even for completely monomorphic</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L451" href="#L451">451</a><span class="code-text">  functions.  This is needed so that Miri can execute such functions, so Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L452" href="#L452">452</a><span class="code-text">  sets this flag per default.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L453" href="#L453">453</a><span class="code-text">* `-Zmir-emit-retag` controls whether `Retag` statements are emitted. Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L454" href="#L454">454</a><span class="code-text">  enables this per default because it is needed for [Stacked Borrows] and [Tree Borrows].</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L455" href="#L455">455</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L456" href="#L456">456</a><span class="code-text">Moreover, Miri recognizes some environment variables:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L457" href="#L457">457</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L458" href="#L458">458</a><span class="code-text">* `MIRI_AUTO_OPS` indicates whether the automatic execution of rustfmt, clippy and toolchain setup</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L459" href="#L459">459</a><span class="code-text">  should be skipped. If it is set to `no`, they are skipped. This is used to allow automated IDE</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L460" href="#L460">460</a><span class="code-text">  actions to avoid the auto ops.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L461" href="#L461">461</a><span class="code-text">* `MIRI_LOG`, `MIRI_BACKTRACE` control logging and backtrace printing during</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L462" href="#L462">462</a><span class="code-text">  Miri executions, also [see &quot;Testing the Miri driver&quot; in `CONTRIBUTING.md`][testing-miri].</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L463" href="#L463">463</a><span class="code-text">* `MIRIFLAGS` (recognized by `cargo miri` and the test suite) defines extra</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L464" href="#L464">464</a><span class="code-text">  flags to be passed to Miri.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L465" href="#L465">465</a><span class="code-text">* `MIRI_LIB_SRC` defines the directory where Miri expects the sources of the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L466" href="#L466">466</a><span class="code-text">  standard library that it will build and use for interpretation. This directory</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L467" href="#L467">467</a><span class="code-text">  must point to the `library` subdirectory of a `rust-lang/rust` repository</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L468" href="#L468">468</a><span class="code-text">  checkout. Note that changing files in that directory does not automatically</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L469" href="#L469">469</a><span class="code-text">  trigger a re-build of the standard library; you have to clear the Miri build</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L470" href="#L470">470</a><span class="code-text">  cache manually (on Linux, `rm -rf ~/.cache/miri`;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L471" href="#L471">471</a><span class="code-text">  on Windows, `rmdir /S &quot;%LOCALAPPDATA%\rust-lang\miri\cache&quot;`;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L472" href="#L472">472</a><span class="code-text">  and on macOS, `rm -rf ~/Library/Caches/org.rust-lang.miri`).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L473" href="#L473">473</a><span class="code-text">* `MIRI_SYSROOT` (recognized by `cargo miri` and the Miri driver) indicates the sysroot to use. When</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L474" href="#L474">474</a><span class="code-text">  using `cargo miri`, this skips the automatic setup -- only set this if you do not want to use the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L475" href="#L475">475</a><span class="code-text">  automatically created sysroot. For directly invoking the Miri driver, this variable (or a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L476" href="#L476">476</a><span class="code-text">  `--sysroot` flag) is mandatory. When invoking `cargo miri setup`, this indicates where the sysroot</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L477" href="#L477">477</a><span class="code-text">  will be put.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L478" href="#L478">478</a><span class="code-text">* `MIRI_TEST_TARGET` (recognized by the test suite and the `./miri` script) indicates which target</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L479" href="#L479">479</a><span class="code-text">  architecture to test against.  `miri` and `cargo miri` accept the `--target` flag for the same</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L480" href="#L480">480</a><span class="code-text">  purpose.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L481" href="#L481">481</a><span class="code-text">* `MIRI_TEST_THREADS` (recognized by the test suite): set the number of threads to use for running tests.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L482" href="#L482">482</a><span class="code-text">  By default the number of cores is used.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L483" href="#L483">483</a><span class="code-text">* `MIRI_NO_STD` (recognized by `cargo miri` and the test suite) makes sure that the target's</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L484" href="#L484">484</a><span class="code-text">  sysroot is built without libstd. This allows testing and running no_std programs.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L485" href="#L485">485</a><span class="code-text">* `RUSTC_BLESS` (recognized by the test suite and `cargo-miri-test/run-test.py`): overwrite all</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L486" href="#L486">486</a><span class="code-text">  `stderr` and `stdout` files instead of checking whether the output matches.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L487" href="#L487">487</a><span class="code-text">* `MIRI_SKIP_UI_CHECKS` (recognized by the test suite): don't check whether the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L488" href="#L488">488</a><span class="code-text">  `stderr` or `stdout` files match the actual output.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L489" href="#L489">489</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L490" href="#L490">490</a><span class="code-text">The following environment variables are *internal* and must not be used by</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L491" href="#L491">491</a><span class="code-text">anyone but Miri itself. They are used to communicate between different Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L492" href="#L492">492</a><span class="code-text">binaries, and as such worth documenting:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L493" href="#L493">493</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L494" href="#L494">494</a><span class="code-text">* `MIRI_BE_RUSTC` can be set to `host` or `target`. It tells the Miri driver to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L495" href="#L495">495</a><span class="code-text">  actually not interpret the code but compile it like rustc would. With `target`, Miri sets</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L496" href="#L496">496</a><span class="code-text">  some compiler flags to prepare the code for interpretation; with `host`, this is not done.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L497" href="#L497">497</a><span class="code-text">  This environment variable is useful to be sure that the compiled `rlib`s are compatible</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L498" href="#L498">498</a><span class="code-text">  with Miri.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L499" href="#L499">499</a><span class="code-text">* `MIRI_CALLED_FROM_SETUP` is set during the Miri sysroot build,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L500" href="#L500">500</a><span class="code-text">  which will re-invoke `cargo-miri` as the `rustc` to use for this build.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L501" href="#L501">501</a><span class="code-text">* `MIRI_CALLED_FROM_RUSTDOC` when set to any value tells `cargo-miri` that it is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L502" href="#L502">502</a><span class="code-text">  running as a child process of `rustdoc`, which invokes it twice for each doc-test</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L503" href="#L503">503</a><span class="code-text">  and requires special treatment, most notably a check-only build before interpretation.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L504" href="#L504">504</a><span class="code-text">  This is set by `cargo-miri` itself when running as a `rustdoc`-wrapper.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L505" href="#L505">505</a><span class="code-text">* `MIRI_CWD` when set to any value tells the Miri driver to change to the given</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L506" href="#L506">506</a><span class="code-text">  directory after loading all the source files, but before commencing</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L507" href="#L507">507</a><span class="code-text">  interpretation. This is useful if the interpreted program wants a different</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L508" href="#L508">508</a><span class="code-text">  working directory at run-time than at build-time.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L509" href="#L509">509</a><span class="code-text">* `MIRI_LOCAL_CRATES` is set by `cargo-miri` to tell the Miri driver which</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L510" href="#L510">510</a><span class="code-text">  crates should be given special treatment in diagnostics, in addition to the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L511" href="#L511">511</a><span class="code-text">  crate currently being compiled.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L512" href="#L512">512</a><span class="code-text">* `MIRI_VERBOSE` when set to any value tells the various `cargo-miri` phases to</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L513" href="#L513">513</a><span class="code-text">  perform verbose logging.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L514" href="#L514">514</a><span class="code-text">* `MIRI_HOST_SYSROOT` is set by bootstrap to tell `cargo-miri` which sysroot to use for *host*</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L515" href="#L515">515</a><span class="code-text">  operations.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L516" href="#L516">516</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L517" href="#L517">517</a><span class="code-text">[testing-miri]: CONTRIBUTING.md#testing-the-miri-driver</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L518" href="#L518">518</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L519" href="#L519">519</a><span class="code-text">## Miri `extern` functions</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L520" href="#L520">520</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L521" href="#L521">521</a><span class="code-text">Miri provides some `extern` functions that programs can import to access</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L522" href="#L522">522</a><span class="code-text">Miri-specific functionality. They are declared in</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L523" href="#L523">523</a><span class="code-text">[/tests/utils/miri\_extern.rs](/tests/utils/miri_extern.rs).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L524" href="#L524">524</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L525" href="#L525">525</a><span class="code-text">## Contributing and getting help</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L526" href="#L526">526</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L527" href="#L527">527</a><span class="code-text">If you want to contribute to Miri, great!  Please check out our</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L528" href="#L528">528</a><span class="code-text">[contribution guide](CONTRIBUTING.md).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L529" href="#L529">529</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L530" href="#L530">530</a><span class="code-text">For help with running Miri, you can open an issue here on</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L531" href="#L531">531</a><span class="code-text">GitHub or use the [Miri stream on the Rust Zulip][zulip].</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L532" href="#L532">532</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L533" href="#L533">533</a><span class="code-text">[zulip]: https://rust-lang.zulipchat.com/#narrow/stream/269128-miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L534" href="#L534">534</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L535" href="#L535">535</a><span class="code-text">## History</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L536" href="#L536">536</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L537" href="#L537">537</a><span class="code-text">This project began as part of an undergraduate research course in 2015 by</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L538" href="#L538">538</a><span class="code-text">@solson at the [University of Saskatchewan][usask].  There are [slides] and a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L539" href="#L539">539</a><span class="code-text">[report] available from that project.  In 2016, @oli-obk joined to prepare Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L540" href="#L540">540</a><span class="code-text">for eventually being used as const evaluator in the Rust compiler itself</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L541" href="#L541">541</a><span class="code-text">(basically, for `const` and `static` stuff), replacing the old evaluator that</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L542" href="#L542">542</a><span class="code-text">worked directly on the AST.  In 2017, @RalfJung did an internship with Mozilla</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L543" href="#L543">543</a><span class="code-text">and began developing Miri towards a tool for detecting undefined behavior, and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L544" href="#L544">544</a><span class="code-text">also using Miri as a way to explore the consequences of various possible</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L545" href="#L545">545</a><span class="code-text">definitions for undefined behavior in Rust.  @oli-obk's move of the Miri engine</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L546" href="#L546">546</a><span class="code-text">into the compiler finally came to completion in early 2018.  Meanwhile, later</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L547" href="#L547">547</a><span class="code-text">that year, @RalfJung did a second internship, developing Miri further with</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L548" href="#L548">548</a><span class="code-text">support for checking basic type invariants and verifying that references are</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L549" href="#L549">549</a><span class="code-text">used according to their aliasing restrictions.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L550" href="#L550">550</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L551" href="#L551">551</a><span class="code-text">[usask]: https://www.usask.ca/</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L552" href="#L552">552</a><span class="code-text">[slides]: https://solson.me/miri-slides.pdf</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L553" href="#L553">553</a><span class="code-text">[report]: https://solson.me/miri-report.pdf</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L554" href="#L554">554</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L555" href="#L555">555</a><span class="code-text">## Bugs found by Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L556" href="#L556">556</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L557" href="#L557">557</a><span class="code-text">Miri has already found a number of bugs in the Rust standard library and beyond, which we collect here.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L558" href="#L558">558</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L559" href="#L559">559</a><span class="code-text">Definite bugs found:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L560" href="#L560">560</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L561" href="#L561">561</a><span class="code-text">* [`Debug for vec_deque::Iter` accessing uninitialized memory](https://github.com/rust-lang/rust/issues/53566)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L562" href="#L562">562</a><span class="code-text">* [`Vec::into_iter` doing an unaligned ZST read](https://github.com/rust-lang/rust/pull/53804)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L563" href="#L563">563</a><span class="code-text">* [`From&lt;&amp;[T]&gt; for Rc` creating a not sufficiently aligned reference](https://github.com/rust-lang/rust/issues/54908)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L564" href="#L564">564</a><span class="code-text">* [`BTreeMap` creating a shared reference pointing to a too small allocation](https://github.com/rust-lang/rust/issues/54957)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L565" href="#L565">565</a><span class="code-text">* [`Vec::append` creating a dangling reference](https://github.com/rust-lang/rust/pull/61082)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L566" href="#L566">566</a><span class="code-text">* [Futures turning a shared reference into a mutable one](https://github.com/rust-lang/rust/pull/56319)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L567" href="#L567">567</a><span class="code-text">* [`str` turning a shared reference into a mutable one](https://github.com/rust-lang/rust/pull/58200)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L568" href="#L568">568</a><span class="code-text">* [`rand` performing unaligned reads](https://github.com/rust-random/rand/issues/779)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L569" href="#L569">569</a><span class="code-text">* [The Unix allocator calling `posix_memalign` in an invalid way](https://github.com/rust-lang/rust/issues/62251)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L570" href="#L570">570</a><span class="code-text">* [`getrandom` calling the `getrandom` syscall in an invalid way](https://github.com/rust-random/getrandom/pull/73)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L571" href="#L571">571</a><span class="code-text">* [`Vec`](https://github.com/rust-lang/rust/issues/69770) and [`BTreeMap`](https://github.com/rust-lang/rust/issues/69769) leaking memory under some (panicky) conditions</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L572" href="#L572">572</a><span class="code-text">* [`beef` leaking memory](https://github.com/maciejhirsz/beef/issues/12)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L573" href="#L573">573</a><span class="code-text">* [`EbrCell` using uninitialized memory incorrectly](https://github.com/Firstyear/concread/commit/b15be53b6ec076acb295a5c0483cdb4bf9be838f#diff-6282b2fc8e98bd089a1f0c86f648157cR229)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L574" href="#L574">574</a><span class="code-text">* [TiKV performing an unaligned pointer access](https://github.com/tikv/tikv/issues/7613)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L575" href="#L575">575</a><span class="code-text">* [`servo_arc` creating a dangling shared reference](https://github.com/servo/servo/issues/26357)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L576" href="#L576">576</a><span class="code-text">* [TiKV constructing out-of-bounds pointers (and overlapping mutable references)](https://github.com/tikv/tikv/pull/7751)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L577" href="#L577">577</a><span class="code-text">* [`encoding_rs` doing out-of-bounds pointer arithmetic](https://github.com/hsivonen/encoding_rs/pull/53)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L578" href="#L578">578</a><span class="code-text">* [TiKV using `Vec::from_raw_parts` incorrectly](https://github.com/tikv/agatedb/pull/24)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L579" href="#L579">579</a><span class="code-text">* Incorrect doctests for [`AtomicPtr`](https://github.com/rust-lang/rust/pull/84052) and [`Box::from_raw_in`](https://github.com/rust-lang/rust/pull/84053)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L580" href="#L580">580</a><span class="code-text">* [Insufficient alignment in `ThinVec`](https://github.com/Gankra/thin-vec/pull/27)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L581" href="#L581">581</a><span class="code-text">* [`crossbeam-epoch` calling `assume_init` on a partly-initialized `MaybeUninit`](https://github.com/crossbeam-rs/crossbeam/pull/779)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L582" href="#L582">582</a><span class="code-text">* [`integer-encoding` dereferencing a misaligned pointer](https://github.com/dermesser/integer-encoding-rs/pull/23)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L583" href="#L583">583</a><span class="code-text">* [`rkyv` constructing a `Box&lt;[u8]&gt;` from an overaligned allocation](https://github.com/rkyv/rkyv/commit/a9417193a34757e12e24263178be8b2eebb72456)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L584" href="#L584">584</a><span class="code-text">* [Data race in `arc-swap`](https://github.com/vorner/arc-swap/issues/76)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L585" href="#L585">585</a><span class="code-text">* [Data race in `thread::scope`](https://github.com/rust-lang/rust/issues/98498)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L586" href="#L586">586</a><span class="code-text">* [`regex` incorrectly handling unaligned `Vec&lt;u8&gt;` buffers](https://www.reddit.com/r/rust/comments/vq3mmu/comment/ienc7t0?context=3)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L587" href="#L587">587</a><span class="code-text">* [Incorrect use of `compare_exchange_weak` in `once_cell`](https://github.com/matklad/once_cell/issues/186)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L588" href="#L588">588</a><span class="code-text">* [Dropping with unaligned pointers in `vec::IntoIter`](https://github.com/rust-lang/rust/pull/106084)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L589" href="#L589">589</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L590" href="#L590">590</a><span class="code-text">Violations of [Stacked Borrows] found that are likely bugs (but Stacked Borrows is currently just an experiment):</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L591" href="#L591">591</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L592" href="#L592">592</a><span class="code-text">* [`VecDeque::drain` creating overlapping mutable references](https://github.com/rust-lang/rust/pull/56161)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L593" href="#L593">593</a><span class="code-text">* Various `BTreeMap` problems</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L594" href="#L594">594</a><span class="code-text">    * [`BTreeMap` iterators creating mutable references that overlap with shared references](https://github.com/rust-lang/rust/pull/58431)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L595" href="#L595">595</a><span class="code-text">    * [`BTreeMap::iter_mut` creating overlapping mutable references](https://github.com/rust-lang/rust/issues/73915)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L596" href="#L596">596</a><span class="code-text">    * [`BTreeMap` node insertion using raw pointers outside their valid memory area](https://github.com/rust-lang/rust/issues/78477)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L597" href="#L597">597</a><span class="code-text">* [`LinkedList` cursor insertion creating overlapping mutable references](https://github.com/rust-lang/rust/pull/60072)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L598" href="#L598">598</a><span class="code-text">* [`Vec::push` invalidating existing references into the vector](https://github.com/rust-lang/rust/issues/60847)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L599" href="#L599">599</a><span class="code-text">* [`align_to_mut` violating uniqueness of mutable references](https://github.com/rust-lang/rust/issues/68549)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L600" href="#L600">600</a><span class="code-text">* [`sized-chunks` creating aliasing mutable references](https://github.com/bodil/sized-chunks/issues/8)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L601" href="#L601">601</a><span class="code-text">* [`String::push_str` invalidating existing references into the string](https://github.com/rust-lang/rust/issues/70301)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L602" href="#L602">602</a><span class="code-text">* [`ryu` using raw pointers outside their valid memory area](https://github.com/dtolnay/ryu/issues/24)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L603" href="#L603">603</a><span class="code-text">* [ink! creating overlapping mutable references](https://github.com/rust-lang/miri/issues/1364)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L604" href="#L604">604</a><span class="code-text">* [TiKV creating overlapping mutable reference and raw pointer](https://github.com/tikv/tikv/pull/7709)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L605" href="#L605">605</a><span class="code-text">* [Windows `Env` iterator using a raw pointer outside its valid memory area](https://github.com/rust-lang/rust/pull/70479)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L606" href="#L606">606</a><span class="code-text">* [`VecDeque::iter_mut` creating overlapping mutable references](https://github.com/rust-lang/rust/issues/74029)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L607" href="#L607">607</a><span class="code-text">* [Various standard library aliasing issues involving raw pointers](https://github.com/rust-lang/rust/pull/78602)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L608" href="#L608">608</a><span class="code-text">* [`&lt;[T]&gt;::copy_within` using a loan after invalidating it](https://github.com/rust-lang/rust/pull/85610)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L609" href="#L609">609</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L610" href="#L610">610</a><span class="code-text">## Scientific papers employing Miri</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L611" href="#L611">611</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L612" href="#L612">612</a><span class="code-text">* [Stacked Borrows: An Aliasing Model for Rust](https://plv.mpi-sws.org/rustbelt/stacked-borrows/)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L613" href="#L613">613</a><span class="code-text">* [Using Lightweight Formal Methods to Validate a Key-Value Storage Node in Amazon S3](https://www.amazon.science/publications/using-lightweight-formal-methods-to-validate-a-key-value-storage-node-in-amazon-s3)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L614" href="#L614">614</a><span class="code-text">* [SyRust: Automatic Testing of Rust Libraries with Semantic-Aware Program Synthesis](https://dl.acm.org/doi/10.1145/3453483.3454084)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L615" href="#L615">615</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L616" href="#L616">616</a><span class="code-text">## License</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L617" href="#L617">617</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L618" href="#L618">618</a><span class="code-text">Licensed under either of</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L619" href="#L619">619</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L620" href="#L620">620</a><span class="code-text">  * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L621" href="#L621">621</a><span class="code-text">    http://www.apache.org/licenses/LICENSE-2.0)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L622" href="#L622">622</a><span class="code-text">  * MIT license ([LICENSE-MIT](LICENSE-MIT) or</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L623" href="#L623">623</a><span class="code-text">    http://opensource.org/licenses/MIT)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L624" href="#L624">624</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L625" href="#L625">625</a><span class="code-text">at your option.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L626" href="#L626">626</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L627" href="#L627">627</a><span class="code-text">### Contribution</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L628" href="#L628">628</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L629" href="#L629">629</a><span class="code-text">Unless you explicitly state otherwise, any contribution intentionally submitted</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L630" href="#L630">630</a><span class="code-text">for inclusion in the work by you shall be dual licensed as above, without any</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L631" href="#L631">631</a><span class="code-text">additional terms or conditions.</span></span></pre></div></div>
</main>
</body>
</html>
