Skip to content

Commit d8956f0

Browse files
authored
Merge pull request #580 from RalfJung/cargo-miri
Cargo miri tweaks and test that we can exclude tests
2 parents 1f68737 + adba97e commit d8956f0

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ branches:
3838
env:
3939
global:
4040
- RUST_TEST_NOCAPTURE=1
41+
- RUST_BACKTRACE=1

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,28 @@ example above), overriding it in your project directory as well, or use `rustup
3939
default nightly` (or `rustup default nightly-YYYY-MM-DD`) to globally make
4040
`nightly` the default toolchain.
4141

42-
Now you can run your project in miri:
42+
Now you can run your project in Miri:
4343

4444
1. Run `cargo clean` to eliminate any cached dependencies. Miri needs your
4545
dependencies to be compiled the right way, that would not happen if they have
4646
previously already been compiled.
4747
2. To run all tests in your project through Miri, use `cargo +nightly miri test`.
48-
**NOTE**: This is currently broken, see the discussion in
49-
[#479](https://github.com/solson/miri/issues/479).
5048
3. If you have a binary project, you can run it through Miri using `cargo
5149
+nightly miri run`.
5250

51+
When running code via `cargo miri`, the `cargo-miri` feature is set. You can
52+
use this to exclude test cases that will fail under Miri because they do things
53+
Miri does not support:
54+
55+
```rust
56+
#[cfg(not(feature = "cargo-miri"))]
57+
#[test]
58+
fn does_not_work_on_miri() {
59+
let x = 0u8;
60+
assert!(&x as *const _ as usize % 4 < 4);
61+
}
62+
```
63+
5364
### Common Problems
5465

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

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ install:
2525
build: false
2626

2727
test_script:
28-
- set RUSTFLAGS=-g
28+
- set RUST_TEST_NOCAPTURE=1
2929
- set RUST_BACKTRACE=1
3030
# Build miri
3131
- cargo build --release --all-features --all-targets

src/bin/cargo-miri.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,14 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
123123
let line = out.stderr.lines().nth(0)
124124
.expect("malformed `xargo --version` output: not at least one line")
125125
.expect("malformed `xargo --version` output: error reading first line");
126-
let version = line.split(' ').nth(1)
127-
.expect("malformed `xargo --version` output: not at least two words");
126+
let (name, version) = {
127+
let mut split = line.split(' ');
128+
(split.next().expect("malformed `xargo --version` output: empty"),
129+
split.next().expect("malformed `xargo --version` output: not at least two words"))
130+
};
131+
if name != "xargo" {
132+
panic!("malformed `xargo --version` output: application name is not `xargo`");
133+
}
128134
let mut version_pieces = version.split('.');
129135
let major = version_pieces.next()
130136
.expect("malformed `xargo --version` output: not a major version piece")
@@ -414,8 +420,6 @@ where
414420
args.push("--".to_owned());
415421
}
416422
args.push("--emit=dep-info,metadata".to_owned());
417-
args.push("--cfg".to_owned());
418-
args.push(r#"feature="cargo-miri""#.to_owned());
419423

420424
let path = std::env::current_exe().expect("current executable path invalid");
421425
let exit_status = Command::new("cargo")

src/fn_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
398398
Err(_) => -1,
399399
}
400400
} else {
401-
warn!("Ignored output to FD {}", fd);
401+
eprintln!("Miri: Ignored output to FD {}", fd);
402402
n as i64 // pretend it all went well
403403
}; // now result is the value we return back to the program
404404
this.write_scalar(

test-cargo-miri/run-test.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
import sys, subprocess
99

10+
def fail(msg):
11+
print("TEST FAIL: {}".format(msg))
12+
sys.exit(1)
13+
1014
def test(name, cmd, stdout_ref, stderr_ref):
1115
print("==> Testing `{}` <==".format(name))
1216
## Call `cargo miri`, capture all output
@@ -25,13 +29,11 @@ def test(name, cmd, stdout_ref, stderr_ref):
2529
print(stderr, end="")
2630
# Test for failures
2731
if p.returncode != 0:
28-
sys.exit(1)
32+
fail("Non-zero exit status")
2933
if stdout != open(stdout_ref).read():
30-
print("stdout does not match reference")
31-
sys.exit(1)
34+
fail("stdout does not match reference")
3235
if stderr != open(stderr_ref).read():
33-
print("stderr does not match reference")
34-
sys.exit(1)
36+
fail("stderr does not match reference")
3537

3638
def test_cargo_miri_run():
3739
test("cargo miri run", ["cargo", "miri", "run", "-q"], "stdout.ref", "stderr.ref")

test-cargo-miri/tests/foo.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ fn bar() {
99
fn baz() {
1010
assert_eq!(5, 5);
1111
}
12+
13+
// A test that won't work on miri
14+
#[cfg(not(feature = "cargo-miri"))]
15+
#[test]
16+
fn does_not_work_on_miri() {
17+
let x = 0u8;
18+
assert!(&x as *const _ as usize % 4 < 4);
19+
}

0 commit comments

Comments
 (0)