typo
[web.git] / ralf / _posts / 2022-07-02-miri.md
1 ---
2 title: "The last two years in Miri"
3 categories: rust
4 reddit: /rust/comments/vq3mmu/the_last_two_years_in_miri/
5 ---
6
7 It has been [almost two years]({% post_url 2020-09-28-miri %}) since my last Miri status update.
8 A lot has happened in the mean time that I would like to tell you all about!
9 If you are using Miri, you might also be seeing new errors in code that previously worked fine; read on for more details on that.
10
11 For the uninitiated, [Miri](https://github.com/rust-lang/miri/) is an interpreter that runs your Rust code and checks if it triggers any [Undefined Behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) (UB for short).
12 You can think of it a as very thorough (and very slow) version of valgrind/ASan/TSan/UBSan:
13 Miri will detect when your program uses uninitialized memory incorrectly, performs out-of-bounds memory accesses or pointer arithmetic, causes a data race, violates key language invariants, does not ensure proper pointer alignment, or causes incorrect aliasing.
14 As such, it is most helpful when writing unsafe code, as it aids in ensuring that you follow all the rules required for unsafe code to be correct and safe.
15 Miri also detects memory leaks, i.e., it informs you at the end of program execution if there is any memory that was not deallocated properly.
16
17 <!-- MORE -->
18
19 Moreover, Miri is able to run code for other targets: for example, you might be developing code on x86_64, a 64-bit little-endian architecture.
20 When you do low-level bit manipulation, it is easy to introduce bugs that only show up on 32-bit systems or big-endian architectures.
21 You can run Miri with `--target i686-unknown-linux-gnu` and `--target mips64-unknown-linux-gnuabi64` to test your code in those situations -- and this will work even if your host OS is macOS or Windows!
22
23 That said, it's not all roses and rainbows.
24 Since Miri just knows how to interpret Rust code, it will get stuck when you call into C code.
25 Miri knows how to execute a certain small set of well-known C functions (e.g. to access environment variables or open files), but it is still easy to run into an "unsupported operation" error due to missing C library implementations.
26 In many cases you should be able to still write tests that cover the remaining code that does not need to, for example, directly access the network;
27 but I also hope that Miri will keep growing its support for key platform APIs.
28
29 ## Miri progress
30
31 So, what progress has Miri made in the last two years?
32
33 ### Concurrency
34
35 The story of concurrency in Miri continues to surprise me: I had not even planned for Miri to support concurrency, but people just keep showing up and implement one part of it after the other, so now we have pretty good support for finding concurrency bugs!
36
37 In that spirit, @JCTyblaidd implemented a data race detector.
38 So if your code does not use appropriate atomic operations to make sure all accesses are suitably synchronized, Miri will now detect that problem and report Undefined Behavior.
39 [Here's a demo](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2dc29d339658dd2e1e74b84fcffc3926).
40 (Click that link and then select "Tools - Miri" to see this in action.)
41 Our data race error reports could be improved a lot (in particular they only show one of the two conflicting accesses involved in a data race), but they are still useful and have already found several data races in the wild.
42
43 @thomcc changed our `compare_exchange_weak` implementation so that it randomly just fails with 80% probability.
44 (The exact rate is adjustable via `-Zmiri-compared-exchange-weak-failure-rate=<x>`.)
45 [Here's a demo](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0af8443b760985ce01640135ffb83749).
46 This is super useful to find issues where code uses `compare_exchange_weak` but cannot handle spurious failures, since those are very unlikely to occur in the wild.
47
48 @henryboisdequin added support for the atomic `fetch_min` and `fetch_max` operations, completing our support of the `Atomic*` types.
49
50 And finally, @cbeuw showed up and added "weak memory emulation".
51 This means that when you do an atomic load, you might not observe the latest value written to that location; instead, a previous value can be returned.
52 [Here's a demo](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7809a750bda54f0cc458b81823c79db7).
53 This happens on real hardware, so having this supported in Miri helps to find more potential bugs.
54 The caveat is that Miri still cannot produce *all* the behaviors that the actual program might exhibit.
55 Also, the C++20 revision of the C++ memory model disallowed some possible behaviors that were previously allowed, but Miri might produce those behaviors -- there is currently no known algorithm that would prevent that.
56 This should be very rare though.
57
58 I then just put the icing on the cake by fixing some long-standing issues in our scheduler, so that it no longer gets stuck in spin loops.
59 Miri now has a chance to preempt the running thread at the end of each basic block; the preemption probability is 1% but you can adjust it (using `-Zmiri-preemption-rate=<x>`).
60
61 All of this made our concurrency support sufficiently solid that it no longer shows any warning about being "experimental".
62 For example, it has already found a [data race in the standard library](https://github.com/rust-lang/rust/issues/98498).
63 I can barely express how happy and proud I am that I had to do basically none of this work. :)
64
65 One warning though: several of the improvements mentioned above rely on doing random choices.
66 So, it is now more likely than before that Miri will work fine one day, and then show an error after some seemingly inconsequential change to the program the next day.
67 I will get back to these problems later.
68
69 ### Pointer provenance and Stacked Borrows
70
71 One of the most subtle aspects of Miri is [Stacked Borrows](https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md).
72 The aliasing model is already quite complicated, and actually debugging what happens when Miri finds an aliasing violation in your code can be pretty tricky.
73 However, @saethlin made this a lot easier!
74 The error messages now show a lot more detail and point to several relevant locations in the code: not only where the bad access happened, but also where the pointer tag used for that access was created, and where that tag was invalidated.
75 I am very impressed by how good some of these errors are, just [check this out](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e831ed0f262e039dda4d24c159a9f5b0).
76
77 Another big thing that happened recently is the entire ["Strict Provenance" story](https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance).
78 I am super excited by these developments, because they offer the chance to fix some long-standing open problems in Miri:
79 the issues with "untagged" raw pointers in Stacked Borrows, and Miri not properly supporting integer-to-pointer casts.
80
81 After a lot of work by @carbotaniuman and myself, the situation now is as follows:
82 - Miri always properly tags raw pointers.
83   So there are no longer any counter-intuitive behaviors caused by Miri "mixing up" two raw pointers that point to the same address, but were computed in a different way.
84     (We had a `-Zmiri-tag-raw-pointers` flag for a while that also achieves this; that flag is now on-by-default.)
85 - If you do not use any integer-to-pointer casts, then you can stop reading here!
86   You can pass `-Zmiri-strict-provenance` to Miri to ensure that this is indeed the case.
87 - If you *are* using integer-to-pointer casts, then Miri will warn about that. You now have two options.
88   - The ideal solution is to avoid using integer-to-pointer casts, and to follow Strict Provenance instead.
89     The [pointer library docs](https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance) explain in more detail what exactly that means.
90     Note that the APIs described there are still unstable, but a [polyfill](https://crates.io/crates/sptr) is available for stable Rust.
91     Also see [Gankra's blog post](https://gankra.github.io/blah/tower-of-weakenings/) and [my own blog post]({% post_url 2022-04-11-provenance-exposed %}) for some more background on this subject.
92   - If the casts are in code you do not control, or if you cannot currently avoid integer-to-pointer casts, you can pass `-Zmiri-permissive-provenance` to Miri to silence the warning.
93     Know that this means that Miri might miss some bugs in your code:
94     integer-to-pointer casts make it impossible to precisely track which pointer came from where, so Miri will conservatively accept some code that actually should be rejected.
95
96 This is overall much better than previously -- there is nothing funky going on with raw pointers any more, and we should never incorrectly report UB any more even when integer-to-pointer casts are used.
97 :-)
98
99 ### Other areas
100
101 Concurrency and pointer aliasing are the two big changes, but there is also a long tail of smaller changes that together make Miri a hack of a lot more useful than it used to be:
102
103 - @teryror made Miri support doctests, so now `cargo miri test` will also check your doctests for UB!
104 - @Smittyvb fixed our fast-math intrinsics to properly report UB when they are used on non-finite values.
105 - @hyd-dev added "symbol resolution" support to Miri, so if one part of your Rust code defines a function with a given `link_name`, and another piece of Rust code imports that function via an `extern` block, Miri now knows how to find the right function implementation.
106 - @atsmtat added a `-Zmiri-isolation-error=<action>` flag so when a function call is rejected due to isolation, evaluation can continue by reporting an error code to the interpreted program.
107 - @landaire added `-Zmiri-panic-on-unsupported`, which makes Miri raise a panic rather than stopping evaluation when an unsupported system function is encountered.
108 This can be useful to keep going with the next test in a test suite.
109 However, it also raises panics where usually that would be impossible, which can lead to surprising behavior.
110 - @DrMeepster added support for running programs that use the `#[start]` attribute, and @oli-obk made that work even for targets without `libstd`.
111 (You need to set `MIRI_NO_STD=1` to make the latter work.)
112 - @DrMeepster also implemented support for the `#[global_allocator]` attribute.
113 - @camelid made Miri optionally detect UB due to uninitialized integers, which has since become the default.
114 - @saethlin made our errors more readable by pruning irrelevant details from the backtraces.
115 - I have implemented support for calling methods on types like `Pin<Box<dyn Trait>>`.
116 - @oli-obk fixed our handling of types like `MaybeUninit<u64>`, where previously we did not properly support only *some* of the bytes being initialized.
117
118 We also improved out platform API and intrinsic support:
119
120 - Thanks to @m-ou-se Miri now supports the Linux futex APIs used by the Rust standard library.
121   This was crucial for std's `park()` and `unpark()`, but meanwhile is also used for many other synchronization primitives.
122 - On the file system side, @Aaron1011 implemented `readlink`, which makes `std::fs::read_link` work on Linux and macOS.
123 - @asquared31415 made the three-argument form of `open` work.
124 - @tavianator implemented `readdir64` so we can still list directories on Linux (the Rust standard library was changed to use that function rather than `readdir64_r`).
125 - @Aaron1011 has also improved the rendering of panic backtraces inside the interpreter.
126 - @frewsxcv implemented the missing bits to make the aarch64-apple-darwin target work in Miri.
127 - I implemented the intrinsics required by `std::simd`, so portable-simd code should work with Miri.
128   It will not be very fast, though...
129 - @V0ldek made our Windows `GetSystemInfo` shim work in more situations.
130 - @saethlin added support for `*_COARSE` clocks on Linux.
131 - @InfRandomness has started on getting Miri to work on FreeBSD targets (but this support is still incomplete).
132
133 ### Bugfixes and cleanup
134
135 And of course there were tons of bugfixes.
136 I want to particularly call out @hyd-dev who fixed a *lot* of issues in our `cargo miri` frontend.
137 @dtolnay did a lot of code cleanup, making Miri pass by clippy's critical eyes and ensuring all our tests are properly formatted.
138 And last not least, @oli-obk completely re-wrote our test suite so that we can finally actually test the full output of Miri.
139
140 I have probably forgotten to mention something interesting as well.
141 [See here](https://github.com/rust-lang/miri/graphs/contributors?from=2020-09-29&to=2022-07-02&type=c) for the full list of amazing people who contributed to Miri since my last update.
142 I cannot thank all of you enough! <3
143
144 ## Help, Miri suddenly says my code is broken
145
146 Several of the changes mentioned above, in particular with regards to concurrency and Stacked Borrows, mean that Miri is now able to detect more problems than before.
147 On the one hand, that's of course great, but on the other hand, it can mean that when you re-test Miri on some code that seemed fine, it might suddenly complain!
148 And because of all the non-determinism, it might also be the case that Miri *sometimes* complains, and sometimes doesn't (or that it works fine locally but complains on CI).
149 What can you do when that happens?
150
151 If Miri shows a new Stacked Borrows error, then that is probably caused by raw pointers now being properly tagged.
152 The new Stacked Borrows messages should make it easier than before to diagnose these problems, but in the end this still remains a case-by-case issue.
153 For example, [this program](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=14ddec169895d111578ec96757df95d1) will print:
154 ```
155 error: Undefined Behavior: attempting a read access using <3255> at alloc1770[0x4], but that tag does not exist in the borrow stack for this location
156  --> src/main.rs:4:25
157   |
158 4 |     let _val = unsafe { *ptr.add(1) }; // ...and use it to access the *second* element.
159   |                         ^^^^^^^^^^^
160   |                         |
161   |                         attempting a read access using <3255> at alloc1770[0x4], but that tag does not exist in the borrow stack for this location
162   |                         this error occurs as part of an access at alloc1770[0x4..0x8]
163   |
164   = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
165   = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
166 help: <3255> was created by a retag at offsets [0x0..0x4]
167  --> src/main.rs:3:15
168   |
169 3 |     let ptr = &x[0] as *const i32; // We create a pointer to the *first* element...
170   |               ^^^^^
171   = note: backtrace:
172   = note: inside `main` at src/main.rs:4:25
173 ```
174 In this case, the clue is in the offsets: note that the tag was created for offsets `[0x0..0x4]` (as usual in Rust, this *excludes* `0x4`), and the access was at `alloc1770[0x4]`.
175 The pointer was thus used outside the offset range for which its tag (`<3255>`) is valid.
176 The fix is to use `x.as_ptr()` rather than `&x[0] as *const i32` to get a pointer that is valid for the entire array.
177
178 If the error only shows up sometimes, then it probably has something to do with concurrency.
179 Miri is not *truly* random, but uses a pseudo-random number generator to make all concurrency-related choices (such as when to schedule another thread).
180 This means you can explore various different possible choices by passing different *seeds* for Miri to use for its pseudo-random number generator.
181 The following little shell snippet will run Miri with many different seeds, which is great to be able to locally reproduce a failure that you saw on CI, but that you are having trouble reproducing:
182 ```
183 for SEED in $({ echo obase=16; seq 0 255; } | bc); do
184   echo "Trying seed: $SEED"
185   MIRIFLAGS=-Zmiri-seed=$SEED cargo miri test || { echo "Failing seed: $SEED"; break; };
186 done
187 ```
188 It is important that you use exactly the same `MIRIFLAGS` as CI to ensure the failure can even happen!
189 It is also a good idea to use a filter with `cargo miri test FILTER` to ensure only the test you care about is being run.
190
191 Once you confirmed that this is indeed a non-deterministic test failure, you can narrow it down further by reducing Miri's non-determinism:
192 - You can pass `-Zmiri-preemption-rate=0` to make the scheduler non-preemptive (only schedule to other threads when a thread explicitly yields).
193   This *can* lead to infinite loops if there are spin-loops that do not yield, but if it makes the problem go away, then the problem needs some very particular scheduling decisions to surface, which might help you track down its source.
194 - You can also pass `-Zmiri-disable-weak-memory-emulation` which has the effect of making atomic loads always return the latest value stored in that location.
195   If that makes the problem go away, then the issue is likely caused by insufficient synchronization somewhere. It might be a missing fence, or a `Relaxed` access that should be `Release`/`Acquire`.
196 - Finally, `-Zmiri-compare-exchange-weak-failure-rate=0` makes `compared_exchange_weak` behave exactly like `compare_exchange`.
197   If that makes the problem go away, then some code using `compared_exchange_weak` is not properly handling spurious failures.
198
199 Passing all of these flags will make Miri's concurrency entirely deterministic.
200 That can be useful to avoid non-deterministic test failures, but note that this will also mask many real-world bugs.
201 Those test failures are often real, even if they can be hard to track down!
202
203 If you are still having trouble, feel free to come visit us in our [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/269128-miri), which is the official communication channel for Miri.
204
205 By the way, if you are still disabling some tests on Miri because Miri used to not support panics/concurrency, it's time to give those tests another try. :)
206 So this is a good opportunity to go over your `cfg(miri)` and similar attributes and re-evaluate if they are still needed.
207
208 ## Using Miri
209
210 If this post made you curious and you want to give Miri a try, here's how to do that.
211 Assuming you have a crate with some unsafe code, and you already have a test suite (you are testing your unsafe code, right?), you can just install Miri (`rustup +nightly component add miri`) and then run `cargo +nightly miri test` to execute all tests in Miri.
212 Note that this requires the nightly toolchain as Miri is still an experimental tool.
213
214 Miri is very slow, so it is likely that some tests will take way too long to be feasible.
215 You can adjust iteration counts in Miri without affecting non-Miri testing as follows:
216 {% highlight rust %}
217 let limit = if cfg!(miri) { 10 } else { 10_000 };
218 {% endhighlight %}
219 If your test suite needs to access OS facilities such as timers or the file system, set `MIRIFLAGS=-Zmiri-disable-isolation` to enable those.
220 (Miri will tell you when that is necessary.)
221 If your test suite runs into an unsupported operation, please [report an issue](https://github.com/rust-lang/miri/issues).
222
223 If you want to add Miri to your CI to ensure your test suite keeps working in Miri, please consult our [README](https://github.com/rust-lang/miri/#running-miri-on-ci).
224 That document is also a great starting point for any other questions you might have.
225
226 Miri is also integrated into the [Rust Playground](https://play.rust-lang.org/): you can select Miri in the "Tools" menu to check the code for Undefined Behavior.
227
228 If Miri complains about your code and you do not understand why, we are happy to help!
229 The best place to ask for support is our [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/269128-miri).
230 Questions are much easier to answer if you manage to reproduce the problem in a small self-contained bit of example code (ideally on the playground), but feel free to ask even if you do not know how to reduce the problem.
231
232 ## Helping Miri
233
234 If you want to help improve Miri, that's awesome!
235 The [issue tracker](https://github.com/rust-lang/miri/issues) is a good place to start; the list of issues is short enough that you can just browse through it rather quickly to see if anything pikes your interest.
236 The ones that are particularly suited for getting started are marked with a green label, but notice that even "E-easy" issues can require some amount of Rust experience -- Miri is not a good codebase for your first steps in Rust.
237 Another good starting point is to try to implement the missing bit of functionality that keeps your test suite from working.
238 If you need any mentoring, just [get in touch](https://rust-lang.zulipchat.com/#narrow/stream/269128-miri). :)
239
240 That's it for now.
241 I am totally blown away by how many people (and companies!) are already using and even contributing to Miri.
242 This endeavor of re-shaping the way we approach correctness of unsafe code has been way more successful than my wildest dreams.
243 I hope Miri can also help you to ensure correctness of your unsafe code, and I am excited for what the next year(s) of Miri development will bring. :D