]> git.lizzy.rs Git - rust.git/blob - CONTRIBUTING.md
d6436873938ed604fd25176f9674ea77a3120020
[rust.git] / CONTRIBUTING.md
1 # Contribution Guide
2
3 If you want to hack on miri yourself, great!  Here are some resources you might
4 find useful.
5
6 ## Getting started
7
8 Check out the issues on this GitHub repository for some ideas. There's lots that
9 needs to be done that we haven't documented in the issues yet, however. For more
10 ideas or help with hacking on Miri, you can contact us (`oli-obk` and `RalfJ`)
11 on the [Rust Zulip].
12
13 [Rust Zulip]: https://rust-lang.zulipchat.com
14
15 ### Fixing Miri when rustc changes
16
17 Miri is heavily tied to rustc internals, so it is very common that rustc changes
18 break Miri.  Fixing those is a good way to get starting working on Miri.
19 Usually, Miri will require changes similar to the other consumers of the changed
20 rustc API, so reading the rustc PR diff is a good way to get an idea for what is
21 needed.
22
23 When submitting a PR against Miri after fixing it for rustc changes, make sure
24 you update the `rust-version` file.  That file always contains the exact rustc
25 git commit with which Miri works, and it is the version that our CI tests Miri
26 against.
27
28 ## Building Miri with a nightly rustc
29
30 Miri heavily relies on internal rustc interfaces to execute MIR.  Still, some
31 things (like adding support for a new intrinsic or a shim for an external
32 function being called) can be done by working just on the Miri side.
33
34 To prepare, make sure you are using a nightly Rust compiler.  You also need to
35 have the `rust-src` and `rustc-dev` components installed, which you can add via
36 `rustup component add rust-src rustc-dev`.  Then you should be able to just
37 `cargo build` Miri.
38
39 In case this fails, your nightly might be incompatible with Miri master.  The
40 `rust-version` file contains the commit hash of rustc that Miri is currently
41 tested against; you can use that to find a nightly that works or you might have
42 to wait for the next nightly to get released. You can also use
43 [`rustup-toolchain-install-master`](https://github.com/kennytm/rustup-toolchain-install-master)
44 to install that exact version of rustc as a toolchain:
45 ```
46 rustup-toolchain-install-master $(cat rust-version) -c rust-src -c rustc-dev
47 ```
48
49 Another common problem is outdated dependencies: Miri does not come with a
50 lockfile (it cannot, due to how it gets embedded into the rustc build). So you
51 have to run `cargo update` every now and then yourself to make sure you are
52 using the latest versions of everything (which is what gets tested on CI).
53
54 ## Testing the Miri driver
55 [testing-miri]: #testing-the-miri-driver
56
57 The Miri driver in the `miri` binary is the "heart" of Miri: it is basically a
58 version of `rustc` that, instead of compiling your code, runs it.  It accepts
59 all the same flags as `rustc` (though the ones only affecting code generation
60 and linking obviously will have no effect) [and more][miri-flags].
61
62 Running the Miri driver requires some fiddling with environment variables, so
63 the `miri` script helps you do that.  For example, you can run the driver on a
64 particular file by doing
65
66 ```sh
67 ./miri run tests/run-pass/format.rs
68 ./miri run tests/run-pass/hello.rs --target i686-unknown-linux-gnu
69 ```
70
71 and you can run the test suite using:
72
73 ```
74 ./miri test
75 ```
76
77 `./miri test FILTER` only runs those tests that contain `FILTER` in their
78 filename (including the base directory, e.g. `./miri test fail` will run all
79 compile-fail tests).
80
81 You can get a trace of which MIR statements are being executed by setting the
82 `MIRI_LOG` environment variable.  For example:
83
84 ```sh
85 MIRI_LOG=info ./miri run tests/run-pass/vecs.rs
86 ```
87
88 Setting `MIRI_LOG` like this will configure logging for Miri itself as well as
89 the `rustc::mir::interpret` and `rustc_mir::interpret` modules in rustc.  You
90 can also do more targeted configuration, e.g. the following helps debug the
91 stacked borrows implementation:
92
93 ```sh
94 MIRI_LOG=rustc_mir::interpret=info,miri::stacked_borrows ./miri run tests/run-pass/vecs.rs
95 ```
96
97 In addition, you can set `MIRI_BACKTRACE=1` to get a backtrace of where an
98 evaluation error was originally raised.
99
100 ## Testing `cargo miri`
101
102 Working with the driver directly gives you full control, but you also lose all
103 the convenience provided by cargo.  Once your test case depends on a crate, it
104 is probably easier to test it with the cargo wrapper.  You can install your
105 development version of Miri using
106
107 ```
108 ./miri install
109 ```
110
111 and then you can use it as if it was installed by `rustup`.  Make sure you use
112 the same toolchain when calling `cargo miri` that you used when installing Miri!
113
114 There's a test for the cargo wrapper in the `test-cargo-miri` directory; run
115 `./run-test.py` in there to execute it.
116
117 ## Building Miri with a locally built rustc
118
119 A big part of the Miri driver lives in rustc, so working on Miri will sometimes
120 require using a locally built rustc.  The bug you want to fix may actually be on
121 the rustc side, or you just need to get more detailed trace of the execution
122 than what is possible with release builds -- in both cases, you should develop
123 miri against a rustc you compiled yourself, with debug assertions (and hence
124 tracing) enabled.
125
126 The setup for a local rustc works as follows:
127 ```sh
128 git clone https://github.com/rust-lang/rust/ rustc
129 cd rustc
130 cp config.toml.example config.toml
131 # Now edit `config.toml` and set `debug-assertions = true`.
132 # This step can take 30 minutes and more.
133 ./x.py build src/rustc
134 # If you change something, you can get a faster rebuild by doing
135 ./x.py --keep-stage 0 build src/rustc
136 # You may have to change the architecture in the next command
137 rustup toolchain link custom build/x86_64-unknown-linux-gnu/stage2
138 # Now cd to your Miri directory, then configure rustup
139 rustup override set custom
140 ```
141
142 With this, you should now have a working development setup!  See
143 [above][testing-miri] for how to proceed working with the Miri driver.