Commit Graph

2448 Commits

Author SHA1 Message Date
Aaron Hill
6a6767fa2a
Apply suggestions from code review
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-10-11 13:40:00 -04:00
Aaron Hill
be51e6bd07
Add an fn_ptr field to MiriFrame
The `backtrace-rs` crate can use this to implement
`Frame::symbol_address`, which is used to skip frames
above the call to `Backtrace::capture` on the stack.

The function pointer will not be useable for comparison purposes if the
function is generic, as CTFE creates a new function pointer for each
cast of a (monomorphized) generic function. However, this already
affects code running under Miri, and isn't a problem for `backtrace-rs`
(which only casts a non-generic function).

I've added logic to allow `MiriFrame` to have either 4 or 5 fields - if
a 5th field is present, we write the function pointer to it.
2020-10-10 13:07:57 -04:00
Aaron Hill
8e8828259a
Use macro callsite spans in backtrace
This mirrors what we do in the debuginfo used for runtime backtraces.
2020-10-05 17:29:55 -04:00
Ralf Jung
3aaab3dd98
more punctuation 2020-10-04 23:07:19 +02:00
Aaron Hill
c1c82c2503
Properly capitalize PathConversion 2020-10-04 16:28:09 -04:00
Aaron Hill
9e6320f101
Move convert_path_separator to trait and use it in readlink 2020-10-04 12:34:13 -04:00
Aaron Hill
462f58298a
Make helper functions freestanding 2020-10-04 12:06:51 -04:00
Aaron Hill
8b89c7e1da
Use panicking coversions instead of as 2020-10-04 12:06:51 -04:00
Aaron Hill
186d59a3ec
Move some helper functions around 2020-10-04 12:06:51 -04:00
Aaron Hill
78bc89b4fc
Implement readlink
Due to the truncating behavior of `readlink`, I was not able to
directly use any of the existing C-cstring helper functions.
2020-10-04 12:06:51 -04:00
Ralf Jung
2b2a3a0cc1 check that all syscall arguments are scalars 2020-10-03 16:01:53 +02:00
Ralf Jung
b350c80a31 add backtics back in isolation error message 2020-10-03 15:25:30 +02:00
Mara Bos
68776d2921
Add FIXME about type of addr in futex implementation.
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-10-03 14:32:30 +02:00
Mara Bos
c268ee2bcb
Add note about use of force_ptr in futex implementation.
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-10-03 14:21:37 +02:00
Mara Bos
dfcb46a4e0 Update syscall FIXME to include note about 'wrong' types. 2020-10-03 13:39:16 +02:00
Mara Bos
9d764c5750 Add FIXME note about variadic syscall(). 2020-10-03 12:18:38 +02:00
Mara Bos
6df54c47a7 Use read_scalar_at_offset in futex_wait instead of memory.get_raw. 2020-10-03 12:11:24 +02:00
Mara Bos
66282754ff Remove backtics from isolation error.
Otherwise `FUTEX_WAIT with timeout` will look weird.
2020-10-03 11:38:16 +02:00
Mara Bos
924fd56944 Only allow FUTEX_WAIT with timeout when isoloation is disabled. 2020-10-03 11:35:13 +02:00
Mara Bos
c9627b25fb Use correct return type for syscall(SYS_futex). 2020-10-03 00:40:53 +02:00
Mara Bos
e64ead2f46 Implement timeouts for FUTEX_WAIT. 2020-10-02 23:34:14 +02:00
Mara Bos
d5b3f54b46 Use force_ptr in futex implementation. 2020-10-02 21:59:11 +02:00
Mara Bos
422b5053a9
Add note about arguments in futex implementation.
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-10-02 20:56:01 +02:00
Mara Bos
dabd980567 Update note about number of arguments to SYS_futex. 2020-10-02 20:53:31 +02:00
Mara Bos
ee3eb4b223 Add comments that document SYS_futex better. 2020-10-02 10:47:53 +02:00
Mara Bos
712e8006b3 Improve handling of the addr argument in SYS_futex. 2020-10-02 10:47:36 +02:00
Mara Bos
c2fa27c3b8 Check maximum amount of arguments to SYS_futex. 2020-10-02 10:46:57 +02:00
Mara Bos
1c582e7c96 Return correct value from futex_wait. 2020-10-02 01:49:20 +02:00
Mara Bos
69cea1dc92 Only check futex pointer in futex_wait and not in futex_wake.
futex_wake doesn't access the futex itself, so should accept pointers to
memory that's no longer there.
2020-10-01 22:57:27 +02:00
Mara Bos
6c2f36eb6b Erase tag from futex pointers. 2020-10-01 21:06:16 +02:00
Mara Bos
281a538226 Move futex syscall to its own file. 2020-10-01 21:03:36 +02:00
Mara Bos
1ffc5bb563 Implement futex_wait and futex_wake.
This does not support futex_wait with a timeout yet.
2020-10-01 20:55:54 +02:00
Ralf Jung
eaf56c57e5 rustup; adjust for rustc_driver changes 2020-09-29 11:05:10 +02:00
Aaron Hill
5571bcfc4f
Require #[repr(C)] on MiriFrame 2020-09-28 13:44:46 -04:00
Aaron Hill
e1bce19ca9
Make some error messages lowercase 2020-09-28 13:44:45 -04:00
Aaron Hill
f756e3a93f
Explain encoding scheme 2020-09-28 13:44:45 -04:00
Aaron Hill
b89f6561e5
Move things around 2020-09-28 13:44:45 -04:00
Aaron Hill
ef43c5a614
Use a 'flags' parameter instead of 'version' 2020-09-28 13:44:45 -04:00
Aaron Hill
22f1eb01ee
Add API for capturing backtrace
This PR adds two new Miri-defined extern functions:
`miri_get_backtrace` and `miri_resolve_frame`, which are documented in
the README. Together, they allow obtaining a backtrace for the currently
executing program.

I've added a test showing how these APIs are used. I've also prepared a
companion PR `backtrace-rs`, which will allow
`backtrace::Backtrace::new()` to work automatically under Miri.

Once these two PRs are merged, we will be able to print backtraces from
the normal Rust panic hook (since libstd is now using backtrace-rs).

A few notes:
* Resolving the backtrace frames is *very* slow - you can actually see
  each line being printed out one at a time. Some local testing showed
  that this is not (primrary) caused by resolving a `Span` - it seems
  to be just Miri being slow.
* For the first time, we now interact directly with a user-defined
  struct (instead of just executing the user-provided MIR that
  manipulates the struct). To allow for future changes, I've added
  a 'version' parameter (currently required to be 0). This should allow
  us to change the `MiriFrame` struct should the need ever arise.
* I used the approach suggested by @oli-obk - a returned backtrace
  pointer consists of a base function allocation, with the 'offset'
  used to encode the `Span.lo`. This allows losslessly reconstructing
  the location information in `miri_resolve_frame`.
* There are a few quirks on the `backtrace-rs` side:
  * `backtrace-rs` calls `getcwd()` by default to try to simplify
    the filename. This results in an isolation error by default,
    which could be annoying when printing a backtrace from libstd.
  * `backtrace-rs` tries to remove 'internal' frames (everything between
     the call to `Backtrace::new()` and the internal API call made by
     backtrace-rs) by comparing the returned frame pointer value to
     a Rust function pointer. This doesn't work due to the way we
     construct the frame pointers passed to the caller. We could
     attempt to support this kind of comparison, or just add a
    `#[cfg(miri)]` and ignore the frames ourselves.
2020-09-28 13:44:45 -04:00
Lzu Tao
56ea94dfa3 Remove assume intrinsic from EvalContextExt
It has been moved to rustc_mir.
2020-09-27 10:00:06 +07:00
Ralf Jung
b0baa151b2 rustup 2020-09-20 13:13:57 +02:00
Ralf Jung
97a71c0c77
fmt
Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
2020-09-18 13:34:25 +02:00
Ralf Jung
32cdb7131b support panic=abort 2020-09-18 13:10:18 +02:00
bors
ce29fbf406 Auto merge of #1540 - RalfJung:cargo-miri-redone, r=oli-obk
Redo cargo-miri logic

This rewrite the cargo-miri logic for running the requested crate(s) following what we outlined in https://github.com/rust-lang/miri/issues/739: `cargo miri run/test $FLAGS` (almost) literally invokes `cargo run/test $FLAGS` but with some environment variables set so that we can control what happens:
* `RUSTC_WRAPPER` is set so that we get invoked instead of `rustc`. We use that power to mess with the flags being used for the build (things to be interpreted by Miri use a different sysroot), and when we are detecting a binary crate meant to be run by Miri, we grab the info we care about and put it into a JSON file for later use.
* `CARGO_TARGET_$TARGET_RUNNER` is set so what we get invoked when cargo wants to run a binary. At that point we take that JSON info from before and use it to invoke Miri.

Overall this works great! We get all sorts of cargo magic for free, and do not even need `cargo-metadata` any more. There's one annoying point though, which I have not managed to entirely work around yet: this means we are doing a full build, not just a check-build. Given that our sysroot is MIR-only, I was surprised that this even worked, but still -- this means we are doing more work than we should. So I also added some patching of arguments to make sure `--emit` does not contain `link`, and then more patching was required of the `--extern` flags for the binary because those referenced the `.rlib` files but now only `.rmeta` exists, and that is still not fully working because cargo seems to expect those `.rmeta` files and now triggers a rebuild each time as those files are still missing. My current plan is to make our wrapper create some empty dummy files with the right names, but the amount of hacks we are stacking on top of each other here is getting worrysome.^^ `@ehuss` your input would be welcome on this issue.

Due to re-using cargo more literally, this also changes flag parsing to match `cargo`. So `-Zmiri` flags now have to be passed via an environment variable (Cc https://github.com/rust-lang/miri/issues/1416).

This PR is not ready yet, but the parts that are there I think can be reviewed already. TODO:
* [x] [Fix Windows](https://github.com/rust-lang/miri/pull/1540#issuecomment-688733741).
* [x] Figure out how we can do check-only builds without the rebuild problem above. ~~I am also worried about cases where `build.rs` script might detect check-only builds and then do less work; I think some crates in rustc are doing that and if this is a thing in the wider ecosystem we need to find a way to support this as well.~~ (postponed that until we have a concrete example)
* [x] Currently cargo runs doctests as well, and they are not run in Miri. We should at least figure out a way to not run them at all (resolving https://github.com/rust-lang/miri/issues/584 is left for a future PR).
* [x] For some time, detect the old way of passing `-Zmiri` flags and warn that this will need updating. For some simple situations we can probably make it still support the old way, but I plan to remove those hacks after a bit. This is just to give people and me time to go around and send PRs to all projects that use Miri on CI, and update their use of the flags.
* [x] Add a test for stdin handling (https://github.com/rust-lang/miri/issues/1505). This should work now but we should be sure.
* [x] Add a test for cargo env vars (https://github.com/rust-lang/miri/issues/1515).
* [x] Check if https://github.com/rust-lang/miri/issues/1516 is resolved.
* [x] Check if https://github.com/rust-lang/miri/issues/1001 and https://github.com/rust-lang/miri/issues/1514 are resolved.
* [x] Check if https://github.com/rust-lang/miri/issues/1312 is resolved (not sure if it is wort adding a test).
* [x] Check if https://github.com/rust-lang/miri/issues/1512 is resolved (not sure if it is wort adding a test).

Fixes https://github.com/rust-lang/miri/issues/700.
Fixes https://github.com/rust-lang/miri/issues/739.
Fixes https://github.com/rust-lang/miri/issues/1001.
Fixes https://github.com/rust-lang/miri/issues/1312 (without a test, as we run without cargo's stdin/stdout wrapping now, as the test for stdin confirms).
Fixes https://github.com/rust-lang/miri/issues/1416.
Fixes https://github.com/rust-lang/miri/issues/1505.
Fixes https://github.com/rust-lang/miri/issues/1512 (without a test, as cargo now does all that handling anyway, which various other tests confirm).
Fixes https://github.com/rust-lang/miri/issues/1514.
Fixes https://github.com/rust-lang/miri/issues/1516.

Cc `@alecmocatta` who reported many of the bugs above; would be great if you could help with the tests e.g. by providing some small examples I could try.
r? `@oli-obk`
2020-09-17 16:52:23 +00:00
Ralf Jung
3163242ff1 rustup; no need to special-case the guaranteed_eq/ne intrinsics any more 2020-09-17 17:42:52 +02:00
Ralf Jung
b244a2ddaa make sure subcrate tests have the right cwd 2020-09-17 17:33:47 +02:00
Samrat Man Singh
563fb8e43d Implement dup and close for stdin/stdout/stderr
Support F_DUPFD on stdin/stdout/stderr

Enable `close`-ing stdin/stdout/stderr

For `dup`, check if FD is `File` first

If not, clone the appropriate standard IO stream

Merge POSIX `close` and `dup` tests into same module

Also, add assertion that `write` on a closed FD returns an error.

Add `dup` as FileDescriptor trait fn

Also:
- Fix `close` so it drops `self` instead of reference to it
- Remove FD clamping in insert_fd_with_min_fd, since FDs 0-2 can be
closed

Fix fs_libc tests

Make error message when closing stdin/out/err more specific

Return io::Result from `FileDescriptor::dup`

Change error message when closing stdin/out/err

Refactor `FileDescriptor::dup` impl for `FileHandle`

Remove empty line
2020-09-09 23:26:33 +05:30
David Cook
b06f0d16a9 Use try block instead of closure 2020-09-07 15:09:34 -05:00
David Cook
597360f499 Simplify read_timespec error handling 2020-09-07 11:31:28 -05:00
David Cook
33e928c9ca Review comments 2020-09-07 10:54:39 -05:00