From 95f740600c530658a40687f169e2337171cbe612 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Mon, 15 Oct 2018 18:45:55 +0000 Subject: [PATCH 1/8] improve README instructions for using `rustup` and for compiling separate Cargo projects --- README.md | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e59accaea1b9..9c6128643c95 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,29 @@ You can also set `-Zmiri-start-fn` to make Miri start evaluation with the ## Running Miri on your own project('s test suite) -Install Miri as a cargo subcommand with `cargo install --all-features`, and install -a full libstd as described above. +Install Miri as a cargo subcommand with `cargo install --all-features --path .`. -Then, inside your own project, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly -miri` to run your project, if it is a bin project, or run -`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri test` to run all tests in your -project through Miri. +Compile your project and its dependencies against a MIR-enabled libstd as described +above: + +1. Run `cargo clean` to eliminate any cached dependencies that were built against +the non-MIR `libstd`. +2. For a binary project, run `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri` to +build and run your project; for a binary or library, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri test` +to run all tests in your project through Miri. + +If you forget to set `MIRI_SYSROOT`, be sure to run `cargo clean` again before +correcting it. Otherwise you are likely to get "dependency was built against possibly +newer std" errors. + +## Using Rustup To Specify a Specific Nightly + +To target a specific nightly, modify the above instructions as follows. + +1. Install Miri using `cargo +nightly-2018-10-15 install --all-features --path .`, +with the date replaced as appropriate. +2. Run `xargo/build.sh` as `rustup run nightly-2018-10-15 build.sh`. +3. When running tests, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly-2018-10-15 miri test`. ## Miri `-Z` flags From f77b29294829564f6d95148ef1e3749587518dfa Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 19 Oct 2018 15:07:19 +0000 Subject: [PATCH 2/8] added line indicating that `build.sh` and `cargo miri` need the same toolchain --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 9c6128643c95..087a2a44cff7 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,10 @@ with the date replaced as appropriate. 2. Run `xargo/build.sh` as `rustup run nightly-2018-10-15 build.sh`. 3. When running tests, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly-2018-10-15 miri test`. +You may prefer to do this rather than depending on the rustup default toolchain, +if you routinely update the default, since **it is essential that `xargo/build.sh` +is run with the same toolchain as `cargo miri`.** + ## Miri `-Z` flags Miri adds some extra `-Z` flags to control its behavior: From 3dcf655eead8aa4f041d881a91200fc9998d4405 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Sat, 20 Oct 2018 16:31:15 +0000 Subject: [PATCH 3/8] readme: pull "common problems" into their own section --- README.md | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 087a2a44cff7..e5c2850ef3eb 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,33 @@ above: 1. Run `cargo clean` to eliminate any cached dependencies that were built against the non-MIR `libstd`. -2. For a binary project, run `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri` to -build and run your project; for a binary or library, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri test` -to run all tests in your project through Miri. +2. To run all tests in your project through, Miri, use +`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri test`. +3. If you have a binary project, you can run it through Miri using +`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri`. -If you forget to set `MIRI_SYSROOT`, be sure to run `cargo clean` again before -correcting it. Otherwise you are likely to get "dependency was built against possibly -newer std" errors. +### Common Problems + +When modifying the above instructions, you may encounter a number of confusing compiler +errors. + +#### "constant evaluation error: no mir for ``" + +You may have forgotten to set `MIRI_SYSROOT` when calling `cargo miri test`, and +your program called into `std` or `core`. Be sure to set `MIRI_SYSROOT=~/.xargo/HOST`. + +#### "found possibly newer version of crate `std` which `` depends on" + +Your build directory may contain artifacts from an earlier build that did/did not +have `MIRI_SYSROOT` set. Run `cargo clean` before switching from non-Miri to Miri +builds and vice-versa. + +#### "found crate `std` compiled by an incompatible version of rustc" + +You may be running `cargo miri test` with a different compiler version than the one +used to build the MIR-enabled `std`. Be sure to consistently use the same toolchain, +perhaps by following the below instructions to specify a specific nightly for use +with Miri. ## Using Rustup To Specify a Specific Nightly @@ -68,13 +88,9 @@ To target a specific nightly, modify the above instructions as follows. 1. Install Miri using `cargo +nightly-2018-10-15 install --all-features --path .`, with the date replaced as appropriate. -2. Run `xargo/build.sh` as `rustup run nightly-2018-10-15 build.sh`. +2. Run `xargo/build.sh` as `rustup run nightly-2018-10-15 xargo/build.sh`. 3. When running tests, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly-2018-10-15 miri test`. -You may prefer to do this rather than depending on the rustup default toolchain, -if you routinely update the default, since **it is essential that `xargo/build.sh` -is run with the same toolchain as `cargo miri`.** - ## Miri `-Z` flags Miri adds some extra `-Z` flags to control its behavior: From abda1a8ebb4f38929a6f62e73b81ce6deb95ef61 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Sun, 21 Oct 2018 16:46:28 +0000 Subject: [PATCH 4/8] rename `rust-toolchain` to `rust-version`; add note to README about usage --- .travis.yml | 3 +-- README.md | 4 +++- appveyor.yml | 2 +- rust-toolchain => rust-version | 0 4 files changed, 5 insertions(+), 4 deletions(-) rename rust-toolchain => rust-version (100%) diff --git a/.travis.yml b/.travis.yml index d2315c7e9538..095af11627fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,9 +18,8 @@ before_script: if [ "$TRAVIS_EVENT_TYPE" = cron ]; then RUST_TOOLCHAIN=nightly else - RUST_TOOLCHAIN=$(cat rust-toolchain) + RUST_TOOLCHAIN=$(cat rust-version) fi -- rm rust-toolchain # install Rust - curl https://build.travis-ci.org/files/rustup-init.sh -sSf | sh -s -- -y --default-toolchain "$RUST_TOOLCHAIN" - export PATH=$HOME/.cargo/bin:$PATH diff --git a/README.md b/README.md index e5c2850ef3eb..76127d8b62a9 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,9 @@ with Miri. ## Using Rustup To Specify a Specific Nightly -To target a specific nightly, modify the above instructions as follows. +To target a specific nightly, modify the above instructions as follows. It is recommended +to use the nightly specified in the `rust-version` file in this repo, since that is the +most recent nightly supported by Miri. 1. Install Miri using `cargo +nightly-2018-10-15 install --all-features --path .`, with the date replaced as appropriate. diff --git a/appveyor.yml b/appveyor.yml index 4614891a3129..cf578120c9dd 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -16,7 +16,7 @@ branches: install: # install Rust - set PATH=C:\Program Files\Git\mingw64\bin;C:\msys64\mingw%MSYS2_BITS%\bin;%PATH% - - set /p RUST_TOOLCHAIN= Date: Tue, 23 Oct 2018 15:21:19 +0000 Subject: [PATCH 5/8] README: remove "specific nightly" instructions and use +nightly throughout Also replace `cargo miri test` with `cargo miri` in general examples. --- README.md | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 76127d8b62a9..7597c9a0e11b 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,19 @@ undergraduate research course at the [University of Saskatchewan][usask]. ## Building Miri I recommend that you install [rustup][rustup] to obtain Rust. Miri comes with a -`rust-toolchain` file so rustup will automatically pick a suitable nightly -version. Then all you have to do is: +`rust-version` file describing the latest supported nightly version of the Rust +compiler toolchain. Then all you have to do is: ```sh -cargo build +cargo +nightly build ``` +with `+nightly` replaced with the appropriate nightly version of Rust. + ## Running Miri ```sh -cargo run tests/run-pass/vecs.rs # Or whatever test you like. +cargo +nightly run tests/run-pass/vecs.rs # Or whatever test you like. ``` ## Running Miri with full libstd @@ -28,15 +30,15 @@ Miri hits a call to such a function, execution terminates. To fix this, it is possible to compile libstd with full MIR: ```sh -rustup component add rust-src -cargo install xargo -xargo/build.sh +rustup component add --toolchain nightly rust-src +cargo +nightly install xargo +rustup run nightly xargo/build.sh ``` Now you can run Miri against the libstd compiled by xargo: ```sh -MIRI_SYSROOT=~/.xargo/HOST cargo run tests/run-pass-fullmir/hashmap.rs +MIRI_SYSROOT=~/.xargo/HOST cargo +nightly run tests/run-pass-fullmir/hashmap.rs ``` Notice that you will have to re-run the last step of the preparations above when @@ -47,7 +49,7 @@ You can also set `-Zmiri-start-fn` to make Miri start evaluation with the ## Running Miri on your own project('s test suite) -Install Miri as a cargo subcommand with `cargo install --all-features --path .`. +Install Miri as a cargo subcommand with `cargo install +nightly --all-features --path .`. Compile your project and its dependencies against a MIR-enabled libstd as described above: @@ -61,12 +63,12 @@ the non-MIR `libstd`. ### Common Problems -When modifying the above instructions, you may encounter a number of confusing compiler +When using the above instructions, you may encounter a number of confusing compiler errors. #### "constant evaluation error: no mir for ``" -You may have forgotten to set `MIRI_SYSROOT` when calling `cargo miri test`, and +You may have forgotten to set `MIRI_SYSROOT` when calling `cargo miri`, and your program called into `std` or `core`. Be sure to set `MIRI_SYSROOT=~/.xargo/HOST`. #### "found possibly newer version of crate `std` which `` depends on" @@ -77,21 +79,9 @@ builds and vice-versa. #### "found crate `std` compiled by an incompatible version of rustc" -You may be running `cargo miri test` with a different compiler version than the one +You may be running `cargo miri` with a different compiler version than the one used to build the MIR-enabled `std`. Be sure to consistently use the same toolchain, -perhaps by following the below instructions to specify a specific nightly for use -with Miri. - -## Using Rustup To Specify a Specific Nightly - -To target a specific nightly, modify the above instructions as follows. It is recommended -to use the nightly specified in the `rust-version` file in this repo, since that is the -most recent nightly supported by Miri. - -1. Install Miri using `cargo +nightly-2018-10-15 install --all-features --path .`, -with the date replaced as appropriate. -2. Run `xargo/build.sh` as `rustup run nightly-2018-10-15 xargo/build.sh`. -3. When running tests, use `MIRI_SYSROOT=~/.xargo/HOST cargo +nightly-2018-10-15 miri test`. +which should be the toolchain specified in the `rust-version` file. ## Miri `-Z` flags From 86aa8352c695a59db5822f267b6c64ccf601220d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 24 Oct 2018 15:29:48 +0200 Subject: [PATCH 6/8] Work on miri installation and usage instructions --- README.md | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7597c9a0e11b..c5ac81729994 100644 --- a/README.md +++ b/README.md @@ -7,15 +7,23 @@ undergraduate research course at the [University of Saskatchewan][usask]. ## Building Miri -I recommend that you install [rustup][rustup] to obtain Rust. Miri comes with a -`rust-version` file describing the latest supported nightly version of the Rust -compiler toolchain. Then all you have to do is: +I recommend that you install [rustup][rustup] to obtain Rust. Then all you have +to do is: ```sh cargo +nightly build ``` -with `+nightly` replaced with the appropriate nightly version of Rust. +This uses the very latest Rust version. If you experience any problem, refer to +the `rust-version` file which contains a particular Rust nightly version that +has been tested against the version of miri you are using. Make sure to use +that particular `nightly-YYYY-MM-DD` whenever the instructions just say +`nightly`. + +To avoid repeating the nightly version all the time, you can use +`rustup override set nightly` (or `rustup override set nightly-YYYY-MM-DD`), +which means `nightly` Rust will automatically be used whenever you are working +in this directory. ## Running Miri @@ -41,18 +49,23 @@ Now you can run Miri against the libstd compiled by xargo: MIRI_SYSROOT=~/.xargo/HOST cargo +nightly run tests/run-pass-fullmir/hashmap.rs ``` -Notice that you will have to re-run the last step of the preparations above when -your toolchain changes (e.g., when you update the nightly). - -You can also set `-Zmiri-start-fn` to make Miri start evaluation with the -`start_fn` lang item, instead of starting at the `main` function. +Notice that you will have to re-run the last step of the preparations above +(`xargo/build.sh`) when your toolchain changes (e.g., when you update the +nightly). ## Running Miri on your own project('s test suite) -Install Miri as a cargo subcommand with `cargo install +nightly --all-features --path .`. +Install Miri as a cargo subcommand with `cargo install +nightly --all-features +--path .`. Be aware that if you used `rustup override set` to fix a particular +Rust version for the miri directory, that will *not* apply to your own project +directory! You have to use a consistent Rust version for building miri and your +project for this to work, so remember to either always specify the nightly +version manually, overriding it in your project directory as well, or use +`rustup default nightly` (or `rustup default nightly-YYYY-MM-DD`) to globally +make `nightly` the default toolchain. -Compile your project and its dependencies against a MIR-enabled libstd as described -above: +We assume that you have prepared a MIR-enabled libstd as described above. Now +compile your project and its dependencies against that libstd: 1. Run `cargo clean` to eliminate any cached dependencies that were built against the non-MIR `libstd`. From 8f9ca242fad4b17a505d26b41e8168cc051d4fe9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 24 Oct 2018 15:32:51 +0200 Subject: [PATCH 7/8] expand -Z flag docs --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c5ac81729994..0e88c8138a51 100644 --- a/README.md +++ b/README.md @@ -98,11 +98,17 @@ which should be the toolchain specified in the `rust-version` file. ## Miri `-Z` flags -Miri adds some extra `-Z` flags to control its behavior: +Several `-Z` flags are relevant for miri: -* `-Zmiri-start-fn`: This makes interpretation start with `lang_start` (defined - in libstd) instead of starting with `main`. Requires full MIR! -* `-Zmiri-disable-validation` disables enforcing the validity invariant. +* `-Zmir-opt-level` controls how many MIR optimizations are performed. miri + overrides the default to be `0`; be advised that using any higher level can + make miri miss bugs in your program because they got optimized away. +* `-Zalways-encode-mir` 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. +* `-Zmiri-disable-validation` is a custom `-Z` flag added by miri. It disables + enforcing the validity invariant, which is enforced by default. This is + mostly useful for debugging; it means miri will miss bugs in your program. ## Development and Debugging From d890a70c39affeb9db3cfa9545905284a981114d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 25 Oct 2018 11:37:42 +0200 Subject: [PATCH 8/8] update for is_null removal --- src/fn_call.rs | 6 +++--- src/intrinsic.rs | 2 +- src/tls.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index 0cbd891a34e4..2d142ab20dc6 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -132,7 +132,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo "free" => { let ptr = self.read_scalar(args[0])?.not_undef()?.erase_tag(); // raw ptr operation, no tag - if !ptr.is_null() { + if !ptr.is_null_ptr(&self) { self.memory.deallocate( ptr.to_ptr()?.with_default_tag(), None, @@ -353,7 +353,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo let mut success = None; { let name_ptr = self.read_scalar(args[0])?.not_undef()?.erase_tag(); // raw ptr operation - if !name_ptr.is_null() { + if !name_ptr.is_null_ptr(&self) { let name = self.memory.read_c_str(name_ptr.to_ptr()?.with_default_tag())?; if !name.is_empty() && !name.contains(&b'=') { success = Some(self.machine.env_vars.remove(name)); @@ -376,7 +376,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for super::MiriEvalCo let name_ptr = self.read_scalar(args[0])?.not_undef()?.erase_tag(); // raw ptr operation let value_ptr = self.read_scalar(args[1])?.to_ptr()?.erase_tag(); // raw ptr operation let value = self.memory.read_c_str(value_ptr.with_default_tag())?; - if !name_ptr.is_null() { + if !name_ptr.is_null_ptr(&self) { let name = self.memory.read_c_str(name_ptr.to_ptr()?.with_default_tag())?; if !name.is_empty() && !name.contains(&b'=') { new = Some((name.to_owned(), value.to_owned())); diff --git a/src/intrinsic.rs b/src/intrinsic.rs index 5c86075883b0..dceeaf89aa5d 100644 --- a/src/intrinsic.rs +++ b/src/intrinsic.rs @@ -231,7 +231,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, ' let a = self.read_value(args[0])?; let b = self.read_value(args[1])?; // check x % y != 0 - if !self.binary_op_val(mir::BinOp::Rem, a, b)?.0.is_null() { + if self.binary_op_val(mir::BinOp::Rem, a, b)?.0.to_bytes()? != 0 { return err!(ValidationFailure(format!("exact_div: {:?} cannot be divided by {:?}", a, b))); } self.binop_ignore_overflow(mir::BinOp::Div, a, b, dest)?; diff --git a/src/tls.rs b/src/tls.rs index 2bddc43df8c4..b315e27c45d0 100644 --- a/src/tls.rs +++ b/src/tls.rs @@ -121,7 +121,7 @@ impl<'tcx> TlsData<'tcx> { for (&key, &mut TlsEntry { ref mut data, dtor }) in thread_local.range_mut((start, Unbounded)) { - if !data.is_null() { + if !data.is_null_ptr(cx) { if let Some(dtor) = dtor { let ret = Some((dtor, *data, key)); *data = Scalar::ptr_null(cx);