Skip to content

Commit

Permalink
Merge pull request #543 from RalfJung/auto-libstd
Browse files Browse the repository at this point in the history
Automatically build libstd
  • Loading branch information
RalfJung authored Nov 27, 2018
2 parents 5a8ed25 + fbd7d11 commit 80a6e73
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 148 deletions.
16 changes: 9 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ os:
- osx

before_script:
# macOS weirdness (https://github.com/travis-ci/travis-ci/issues/6307)
# macOS weirdness (https://github.com/travis-ci/travis-ci/issues/6307, https://github.com/travis-ci/travis-ci/issues/10165)
- if [[ "$TRAVIS_OS_NAME" == osx ]]; then rvm get stable; fi
# Compute the rust version we use. We do not use "language: rust" to have more control here.
- |
Expand All @@ -28,8 +28,6 @@ before_script:
- rustup target add i686-unknown-linux-gnu
- rustup target add i686-pc-windows-gnu
- rustup target add i686-pc-windows-msvc
- rustup component add rust-src
- cargo install xargo || echo "Skipping xargo install"

script:
- set -e
Expand All @@ -39,11 +37,15 @@ script:
cargo test --release --all-features &&
cargo install --all-features --force --path .
- |
# get ourselves a MIR-full libstd
xargo/build.sh &&
export MIRI_SYSROOT=~/.xargo/HOST
# Get ourselves a MIR-full libstd, and use it henceforth
cargo miri setup &&
if [ "$TRAVIS_OS_NAME" == osx ]; then
export MIRI_SYSROOT=~/Library/Caches/miri.miri.miri/HOST
else
export MIRI_SYSROOT=~/.cache/miri/HOST
fi
- |
# run all tests with full mir
# Test miri with full MIR
cargo test --release --all-features
- |
# Test cargo integration
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ required-features = ["rustc_tests"]
[dependencies]
byteorder = { version = "1.1", features = ["i128"]}
cargo_metadata = { version = "0.6", optional = true }
directories = { version = "1.0", optional = true }
env_logger = "0.5"
log = "0.4"

[build-dependencies]
vergen = "3"

[features]
cargo_miri = ["cargo_metadata"]
default = ["cargo_miri"]
cargo_miri = ["cargo_metadata", "directories"]
rustc_tests = []

[dev-dependencies]
Expand Down
77 changes: 25 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ in this directory.

[rustup]: https://www.rustup.rs

## Running Miri
## Running Miri on tiny examples

```sh
cargo +nightly run -- -Zmiri-disable-validation tests/run-pass/vecs.rs # Or whatever test you like.
Expand All @@ -51,74 +51,47 @@ cargo +nightly run -- -Zmiri-disable-validation tests/run-pass/vecs.rs # Or what
We have to disable validation because that can lead to errors when libstd is not
compiled the right way.

## Running Miri with full libstd

Per default libstd does not contain the MIR of non-polymorphic functions, and
also does not contain some extra MIR statements that miri needs for validation.
When Miri hits a call to such a function, execution terminates, and even when
the MIR is present, validation can fail. To fix this, it is possible to compile
libstd with full MIR:

```sh
rustup component add --toolchain nightly rust-src
cargo +nightly install xargo
rustup run nightly xargo/build.sh
```
## Running Miri on your own project('s test suite)

Now you can run Miri against the libstd compiled by xargo:
Install Miri as a cargo subcommand:

```sh
MIRI_SYSROOT=~/.xargo/HOST cargo +nightly run tests/run-pass-fullmir/hashmap.rs
cargo +nightly install --git https://github.com/solson/miri/
```

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 +nightly install --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.

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`.
2. To run all tests in your project through, Miri, use
`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri test`. **NOTE**: This is
currently broken, see the discussion in
[#479](https://github.com/solson/miri/issues/479).
3. If you have a binary project, you can run it through Miri using
`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri`.
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.

1. Run `cargo clean` 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.
2. To run all tests in your project through Miri, use `cargo +nightly miri test`.
**NOTE**: This is currently broken, see the discussion in
[#479](https://github.com/solson/miri/issues/479).
3. If you have a binary project, you can run it through Miri using `cargo
+nightly miri run`.

### Common Problems

When using the above instructions, you may encounter a number of confusing compiler
errors.

#### "constant evaluation error: no mir for `<function>`"

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 `<dependency>` 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.
Your build directory may contain artifacts from an earlier build that have/have
not been built for Miri. 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` with a different compiler version than the one
used to build the MIR-enabled `std`. Be sure to consistently use the same toolchain,
which should be the toolchain specified in the `rust-version` file.
used to build the custom libstd that Miri uses, and Miri failed to detect that.
Try deleting `~/.cache/miri`.

## Miri `-Z` flags

Expand Down
16 changes: 5 additions & 11 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,19 @@ install:
- rustup-init.exe -y --default-host %TARGET% --default-toolchain %RUST_TOOLCHAIN%
- set PATH=%USERPROFILE%\.cargo\bin;%PATH%
- rustc --version
# Customize installation.
- rustup component add rust-src
- cargo install xargo
# Prepare a libstd with MIR (cannot use bash script, obviously).
# The flags here should be kept in sync with `add_miri_default_args` in `src/lib.rs`.
- cd xargo
- set RUSTFLAGS=-Zalways-encode-mir -Zmir-emit-retag -Zmir-opt-level=0
- xargo build
- set RUSTFLAGS=
- cd ..

build: false

test_script:
- set RUSTFLAGS=-g
- set RUST_BACKTRACE=1
# Test plain miri
- cargo build --release --all-features --all-targets
- cargo test --release --all-features
- set MIRI_SYSROOT=%USERPROFILE%\.xargo\HOST
# Get ourselves a MIR-full libstd, and use it henceforth
- cargo run --release --all-features --bin cargo-miri -- miri setup
- set MIRI_SYSROOT=%USERPROFILE%\AppData\Local\miri\miri\cache\HOST
# Test miri with full MIR
- cargo test --release --all-features

notifications:
Expand Down
4 changes: 2 additions & 2 deletions cargo-miri-test/run-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import sys, subprocess

def test_cargo_miri():
print("==> Testing `cargo miri` <==")
print("==> Testing `cargo miri run` <==")
## Call `cargo miri`, capture all output
p = subprocess.Popen(
["cargo", "miri", "-q"],
["cargo", "miri", "run", "-q"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
Expand Down
Loading

0 comments on commit 80a6e73

Please sign in to comment.