Commit Graph

2497 Commits

Author SHA1 Message Date
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
David Cook
417ac2939a Implement libc::nanosleep shim 2020-09-06 18:07:17 -05:00
David Cook
6d323e1032 Refactor timespec parsing, improve error handling 2020-09-06 18:07:14 -05:00
LeSeulArtichaut
4f2f87b2df Change ty.kind -> ty.kind() 2020-09-04 22:03:45 +02:00
David Cook
a6746ad893 Add comment 2020-09-02 20:58:41 -05:00
David Cook
7a2c6812b9 Per-thread errno storage 2020-09-02 20:57:20 -05:00
David Cook
da2f268443 Review comments 2020-08-31 19:32:14 -05:00
David Cook
3ba7f46058 Move panic payload state from Machine to Thread 2020-08-29 21:38:37 -05:00
Ralf Jung
d4e5943259 use real align_offset unless we symbolic alignment check is enabled 2020-08-16 18:26:04 +02:00
Ralf Jung
664706662f adjust diagnostics to alignment check mode 2020-08-16 17:16:53 +02:00
Ralf Jung
cb985670c1 make alignment check integer-based by default, and add an option to make it symbolic 2020-08-16 17:08:38 +02:00
Ralf Jung
df311293e2 fix Stderr::as_file_handle error message 2020-08-13 14:27:03 +02:00
Samrat Man Singh
0c25064117
Remove unnecessary whitespace
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-08-13 16:18:08 +05:30
Samrat Man Singh
46852d736e Remove lifetime from FileDescriptor trait
Also:
- Remove type annotate `handles` declaration instead of every insert.
- Add note about flush being unnecessary when writing to stderr
2020-08-13 15:01:52 +05:30
Ralf Jung
80929e17ae add test for unused ill-formed constant
also use better span in TopFrameInfo
2020-08-13 08:33:36 +02:00
Oliver Scherer
94f13efefd Bump for rustc changes 2020-08-11 11:37:29 +02:00
Ralf Jung
5ea5e9fc2c accept ReferencedConstant errors in Miri (can happen post-monomorphization) 2020-08-10 09:02:45 +02:00
Ralf Jung
5657f08bea fs: move isolation handling to inside trait 2020-08-08 15:24:30 +02:00
bors
eaf5d32ac7 Auto merge of #1501 - samrat:fix-fs-error-handling, r=RalfJung
Bubble up errors from FileDescriptor::as_file_handle

Instead of indicating incorrectly that a handle was not found, return the error from `as_file_handle` indicating the operation is not supported on the FD.

Addresses some comments in #1495
2020-08-08 12:28:08 +00:00
Ralf Jung
07a4383ac8 rustup 2020-08-08 14:05:11 +02:00
Samrat Man Singh
1069f6b174 Fix handling of as_file_handle error for fullfsync 2020-08-08 17:28:41 +05:30
Samrat Man Singh
045bcab1eb Add FIXME's for dup and other syscalls to support stdin/out/err 2020-08-08 15:08:29 +05:30
Samrat Man Singh
773dfb31f0 Bubble up error from FileDescriptor::as_file_handle
...instead of handle_not_found
2020-08-08 14:42:50 +05:30
Samrat Man Singh
bdef57ea45 Flush to stdout from FileDescriptor::write for Stdout
Also, remove unnecessary `-Zmiri-disable-isolation` in test
2020-08-04 20:40:48 +05:30
Samrat Man Singh
bea7113eb8 Add impl FileDescriptor for stdin, stdout, stderr
- Use `FileDescriptor::read` for stdin reads
- Use `FileDescriptor::write` for stdout/err writes
- Handle stdout/err reads in `FileDescriptor::read`
2020-08-04 01:14:52 +05:30
bors
69d050fb6b Auto merge of #1495 - samrat:fd-trait, r=oli-obk
Add FileDescriptor trait to abstract fn's on File's and Stdin,Stdout,Stderr

Related issue: #1486

Instead of mapping FDs to `FileHandle`, map them to a `FileDescriptor` trait object. The goal is to eventually have both `FileHandle` as well as `Stdin`, `Stdout` and `Stderr` implement this trait so that syscalls involving FDs can handle both `File`s as well as the standard IO streams.

This PR adds the `FileDescriptor` trait and an `impl` for `FileHandle`. I'll open a separate PR for implementing the trait for the standard IO streams.
2020-08-03 15:50:30 +00:00
Samrat Man Singh
79e066fc95 Remove unnecessary clone() on writable 2020-08-03 20:39:09 +05:30
Samrat Man Singh
3386f12eca Wrap io::Result from FileDescriptor::{read,write,seek} in InterpResult
The outer InterpResult will be used to indicate that a fn is not
implemented for a struct(eg. `write` for Stdin).

The inner io::Result is just the result from the read/write/seek.
2020-08-03 11:01:42 +05:30
Samrat Man Singh
e3956f4200 Add FileDescriptor trait to abstract fn's on File's and Std{in,out,err} 2020-08-01 20:53:50 +05:30
Ralf Jung
2dfde5b696 remove upstreamed intrinsic impls 2020-07-30 18:31:40 +02:00
Ralf Jung
d340933112 rustup 2020-07-30 18:31:19 +02:00
Ralf Jung
bec7aab7fd Typos
Co-authored-by: Oliver Scherer <github35764891676564198441@oli-obk.de>
2020-07-27 23:32:15 +02:00
Ralf Jung
6fbaa72642 fix diagnostics printing when triggered during TLS dtor scheduling 2020-07-27 23:32:15 +02:00
Ralf Jung
c77540ce13 deallocate thread-local statics when the thread dies 2020-07-27 23:32:15 +02:00
Ralf Jung
2a42f8e93c fix and test order of TLS dtors and thread joining 2020-07-27 23:32:15 +02:00
Ralf Jung
7b07fc385c get_or_create_thread_local_alloc_id: share code with Memory::get_global_alloc 2020-07-27 23:32:15 +02:00
Ralf Jung
0a4e8caa8c adjust to canonical_alloc_id removal 2020-07-27 23:32:15 +02:00
Philippe Nadon
6dd700fd17 Changed not_undef to check_init in foreign_items.rs
Due to changes from upstream, a commit using not_undef was inserted, which had to be updated to use check_init.

related issue #71193
2020-07-27 13:07:25 -06:00
Philippe Nadon
21268157ff renamed Immediate::to_scalar_or_undef
to Immediate::to_scalar_or_uninit

in src/shims/intrinsics.rs

related issue #71193
2020-07-26 16:05:20 -06:00
Philippe Nadon
5161ba346c renamed ScalarMaybeUninit::not_undef to check_init
Related to PR https://github.com/rust-lang/rust/pull/74664
2020-07-26 16:05:20 -06:00
Ralf Jung
bc0569253f enable leak check tests on Windows 2020-07-23 15:56:38 +02:00
Ralf Jung
fef5fa2ae1 add a Miri extern fn to mark an allocation as being a static root for leak checking 2020-07-23 15:47:33 +02:00
Ralf Jung
4033358956 make miri_start_panic intrinsic an FFI function 2020-07-23 15:20:36 +02:00
bors
592b140880 Auto merge of #1482 - canova:remote-unreachable, r=RalfJung
Remove unreachable intrinsic

This is now supported by the interpreter with https://github.com/rust-lang/rust/pull/74459. We can remove this intrinsic implementation here.
This is covered by this test: https://github.com/rust-lang/miri/blob/master/tests/compile-fail/unreachable.rs

I guess we need to wait until the rust-lang/rust PR merges into nightly, and then we can update `rust-version` hash in the PR, right?

r? @oli-obk
2020-07-23 09:14:08 +00:00
Ralf Jung
7d6aec6887 rustup 2020-07-23 10:40:13 +02:00
Ralf Jung
cded9b7142 set errno on stdout write failure 2020-07-18 17:58:21 +02:00
bors
b021209b0e Auto merge of #1436 - samrat:support-stdin-read, r=RalfJung
Handle `read`s on STDIN

Closes #1434
2020-07-18 14:56:38 +00:00
Nazım Can Altınova
d1aee6965b Remove unreachable intrinsic 2020-07-18 12:48:26 +02:00
Samrat Man Singh
f4d1841811 Remove unnecessary cast 2020-07-18 10:45:06 +05:30
Samrat Man Singh
4c1beb2e45 Ensure buffer for reading from Stdin is smaller than machine usize
Also, set appropriate error code on failure
2020-07-17 20:18:07 +05:30
Ralf Jung
545aa60195 fix typo
Co-authored-by: Oliver Scherer <github35764891676564198441@oli-obk.de>
2020-07-17 13:40:13 +02:00
Ralf Jung
d617d615e4 fix non-fatal diagnostics stacktraces 2020-07-17 12:55:45 +02:00