rust-lang / miri

An interpreter for Rust's mid-level intermediate representation
Apache License 2.0
4.4k stars 341 forks source link

dogfooding miri #282

Open oli-obk opened 7 years ago

oli-obk commented 7 years ago

This issue is just to document my process. It's ridiculously slow, but a lot of fun.

  1. Build yourself a rustc with -Zalways-encode-mir (stage2 build necessary)
  2. Add it to rustup (e.g. under the name stage2)
  3. Build miri in release mode with --verbose and RUSTFLAGS=-Zalways-encode-mir -Zmir-opt-level=3
  4. Copy all the --extern flags and the -L dependency=... flag
  5. RUSTFLAGS=-Zalways-encode-mir -Zmir-opt-level=3 rustup run stage2 cargo run --bin miri/bin/miri.rs --verbose -- -Zmir-opt-level=3 and append all the flags from 4. after that command line
  6. have patience
  7. rejoice
WARN:miri::fn_call: ignoring C ABI call: pthread_attr_init
WARN:miri::fn_call: ignoring C ABI call: pthread_self
WARN:miri::fn_call: ignoring C ABI call: pthread_getattr_np
WARN:miri::fn_call: ignoring C ABI call: pthread_attr_getstack
WARN:miri::fn_call: ignoring C ABI call: pthread_attr_destroy
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_lock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_unlock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutexattr_init
WARN:miri::fn_call: ignoring C ABI call: pthread_mutexattr_settype
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_init
WARN:miri::fn_call: ignoring C ABI call: pthread_mutexattr_destroy
WARN:miri::fn_call: ignoring C ABI call: pthread_condattr_init
WARN:miri::fn_call: ignoring C ABI call: pthread_condattr_setclock
WARN:miri::fn_call: ignoring C ABI call: pthread_cond_init
WARN:miri::fn_call: ignoring C ABI call: pthread_condattr_destroy
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_lock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_unlock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_lock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_unlock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_lock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_unlock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_lock
WARN:miri::fn_call: ignoring C ABI call: pthread_mutex_unlock
error: get_field_offset: can't handle type: getopts::HasArg, with layout: CEnum { discr: I8, signed: false, non_zero: false, min: 0, max: 2 }
    --> rust/rust/src/liballoc/vec.rs:1825:17
     |
1825 | /                 for element in iterator {
1826 | |                     ptr::write(ptr, element);
1827 | |                     ptr = ptr.offset(1);
1828 | |                     // NB can't overflow since we would have had to alloc the address space
1829 | |                     local_len.increment_len(1);
1830 | |                 }
     | |_________________^
     |
note: inside call to <std::vec::Vec<T> as std::vec::SpecExtend<T, I>><getopts::Opt, std::iter::Map<std::slice::Iter<getopts::OptGroup>, [closure@DefId { krate: CrateNum(12), node: DefIndex(2147483707) => getopts/12fdf82::{{impl}}[0]::parse[0]::{{closure}}[0] }]>>::spec_extend
    --> rust/rust/src/liballoc/vec.rs:1808:9
     |
1808 |         vector.spec_extend(iterator);
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to <std::vec::Vec<T> as std::vec::SpecExtend<T, I>><getopts::Opt, std::iter::Map<std::slice::Iter<getopts::OptGroup>, [closure@DefId { krate: CrateNum(12), node: DefIndex(2147483707) => getopts/12fdf82::{{impl}}[0]::parse[0]::{{closure}}[0] }]>>::from_iter
    --> rust/rust/src/liballoc/vec.rs:1695:9
     |
1695 |         <Self as SpecExtend<T, I::IntoIter>>::from_iter(iter.into_iter())
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to <std::vec::Vec<T> as std::iter::FromIterator<T>><getopts::Opt>::from_iter::<std::iter::Map<std::slice::Iter<getopts::OptGroup>, [closure@DefId { krate: CrateNum(12), node: DefIndex(2147483707) => getopts/12fdf82::{{impl}}[0]::parse[0]::{{closure}}[0] }]>>
    --> rust/rust/src/libcore/iter/iterator.rs:1302:9
     |
1302 |         FromIterator::from_iter(self)
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to <std::iter::Map<std::slice::Iter<getopts::OptGroup>, [closure@DefId { krate: CrateNum(12), node: DefIndex(2147483707) => getopts/12fdf82::{{impl}}[0]::parse[0]::{{closure}}[0] }]> as std::iter::Iterator>::collect::<std::vec::Vec<getopts::Opt>>
    --> /home/ws/ca8159/.cargo/registry/src/github.com-1ecc6299db9ec823/getopts-0.2.14/src/lib.rs:301:30
     |
301  |         let opts: Vec<Opt> = self.grps.iter().map(|x| x.long_to_short()).collect();
     |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to getopts::Options::parse::<&[std::string::String]>
    --> rust/rust/src/librustc_driver/lib.rs:1031:25
     |
1031 |     let matches = match options.parse(args) {
     |                         ^^^^^^^^^^^^^^^^^^^
note: inside call to rustc_driver::handle_options
    --> rust/rust/src/librustc_driver/lib.rs:170:25
     |
170  |     let matches = match handle_options(args) {
     |                         ^^^^^^^^^^^^^^^^^^^^
note: inside call to rustc_driver::run_compiler
    --> miri/bin/miri.rs:206:5
     |
206  |     rustc_driver::run_compiler(&args, &mut MiriCompilerCalls(RustcDefaultCalls), None, None);
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to main
    --> rust/rust/src/libcore/ops/function.rs:143:5
     |
143  |     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to <fn() as std::ops::FnOnce<()>>::call_once - shim(fn())
    --> rust/rust/src/libstd/sys_common/backtrace.rs:136:5
     |
136  |     f()
     |     ^^^
note: inside call to std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>
    --> rust/rust/src/libstd/rt.rs:62:13
     |
62   |             ::sys_common::backtrace::__rust_begin_short_backtrace(main)
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside call to closure
    --> rust/rust/src/libstd/panicking.rs:479:40
     |
479  |             ptr::write(&mut (*data).r, f());
     |                                        ^^^
note: inside call to std::panicking::try::do_call::<[closure@DefId { krate: CrateNum(1), node: DefIndex(2147491399) => std/78549ff::rt[0]::lang_start[0]::{{closure}}[0] } 0:&fn()], ()>
    --> rust/rust/src/libstd/panicking.rs:475:5
     |
475  | /     fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
476  | |         unsafe {
477  | |             let data = data as *mut Data<F, R>;
478  | |             let f = ptr::read(&mut (*data).f);
479  | |             ptr::write(&mut (*data).r, f());
480  | |         }
481  | |     }
     | |_____^
note: inside call to std::panicking::try::<(), [closure@DefId { krate: CrateNum(1), node: DefIndex(2147491399) => std/78549ff::rt[0]::lang_start[0]::{{closure}}[0] } 0:&fn()]>
    --> rust/rust/src/libstd/panic.rs:361:9
     |
361  |         panicking::try(f)
     |         ^^^^^^^^^^^^^^^^^
note: inside call to std::panic::catch_unwind::<[closure@DefId { krate: CrateNum(1), node: DefIndex(2147491399) => std/78549ff::rt[0]::lang_start[0]::{{closure}}[0] } 0:&fn()], ()>
    --> rust/rust/src/libstd/rt.rs:61:19
     |
61   |           let res = panic::catch_unwind(|| {
     |  ___________________^
62   | |             ::sys_common::backtrace::__rust_begin_short_backtrace(main)
63   | |         });
     | |__________^
note: inside call to std::rt::lang_start
    --> rust/rust/src/libstd/rt.rs:34:1
     |
34   | / fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize {
35   | |     use panic;
36   | |     use sys;
37   | |     use sys_common;
...    |
74   | |     }
75   | | }
     | |_^
RalfJung commented 7 years ago

Build yourself a rustc with -Zalways-encode-mir (stage2 build necessary)

Where does that flag have to be added?

oli-obk commented 7 years ago

Good question. I turned it on unconditionally in the source... I should probably add a config.toml flag

saethlin commented 2 years ago

This issue is pretty old, but every few months someone on the community Discord will point out some dodgy code in rustc and everyone will clamor that we should run Miri on rustc or Miri on Miri. So I think it's fair to say that there is ambient interest in this, if it were made more accessible.

I looked over the instructions above and I think there is a bit missing.

  1. How does one provide -Zalways-encode-mir? The instructions mention a stage2 build, so does one set RUSTFLAGS_BOOTSTRAP or RUSTFLAGS_NOT_BOOTSTRAP? Both?
  2. What's up with the -Zmir-opt-level=3? I thought the whole point of Miri was to run on fully unoptimized MIR.
  3. Instructions mention cargo run --bin miri/bin/miri.rs which seems wrong? I've only ever build/run inside the repo with ./miri. Is this use of cargo run directly correct?
oli-obk commented 2 years ago
  1. How does one provide -Zalways-encode-mir? The instructions mention a stage2 build, so does one set RUSTFLAGS_BOOTSTRAP or RUSTFLAGS_NOT_BOOTSTRAP? Both?

only RUSTFLAGS_NOT_BOOTSTRAP should be necessary

2. What's up with the -Zmir-opt-level=3? I thought the whole point of Miri was to run on fully unoptimized MIR.

that was just me hoping it would run faster that way ^^

3. Instructions mention cargo run --bin miri/bin/miri.rs which seems wrong? I've only ever build/run inside the repo with ./miri. Is this use of cargo run directly correct?

yea that's really old. I think if you install miri in step 3 you can use cargo miri run inside the miri repository itself

saethlin commented 2 years ago

With this in my config.toml:

extended = true
tools = ["cargo", "clippy", "rustfmt", "src", "miri"]

I ran this to build rustc:

RUSTFLAGS_NOT_BOOTSTRAP x build --stage 2

Then based on the contents of the miri script, I ran

export RUSTFLAGS="-C link-args=-Wl,-rpath,/home/ben/rust/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib"

Then tried to

cargo +stage2 miri run --bin miri --verbose

And all I see is:

     Running `/home/ben/.cargo/bin/cargo-miri /home/ben/.cargo/bin/miri --crate-name miri --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="stack-cache"' -C metadata=24c414736c1dfa74 -C extra-filename=-24c414736c1dfa74 --out-dir /home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -C linker=clang -C incremental=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/incremental -L dependency=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps -L dependency=/home/ben/miri/target/miri/debug/deps --extern env_logger=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/libenv_logger-6f6d8da2b7ee6306.rmeta --extern getrandom=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-d277bf60883b4b86.rmeta --extern libc=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/liblibc-ed6a90483caf3f1a.rmeta --extern libffi=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/liblibffi-f56d2af76a51dbca.rmeta --extern libloading=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/liblibloading-7816860288464c34.rmeta --extern log=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/liblog-fea090e2364200e8.rmeta --extern measureme=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/libmeasureme-25a6023984e08d37.rmeta --extern rand=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/librand-f84f5437562e1f0a.rmeta --extern rustc_workspace_hack=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/librustc_workspace_hack-2ee5e29a45fa7105.rmeta --extern shell_escape=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/libshell_escape-f71c5889ef27c6e3.rmeta --extern smallvec=/home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/deps/libsmallvec-e028de92ed4a41db.rmeta -C link-args=-Wl,-rpath,/home/ben/rust/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib -L /home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/build/libffi-sys-b8d8cd4be8d71230/out/libffi-root/lib -L /home/ben/miri/target/miri/x86_64-unknown-linux-gnu/debug/build/libffi-sys-b8d8cd4be8d71230/out/libffi-root/lib64`
error[E0463]: can't find crate for `rustc_apfloat`
  --> src/lib.rs:42:1
   |
42 | extern crate rustc_apfloat;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
   |
   = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview`

and more missing rustc_ crates then a cascade of errors from missing them.

There is for example a rustc_apfloat rlib in the directory mentioned in my RUSTFLAGS. Do you have any idea what's going on here?

bjorn3 commented 2 years ago

The rustc-dev compoment needs to be installed.