Commit Graph

2826 Commits

Author SHA1 Message Date
Oli Scherer
f1756c3ddd Add a custom ui test runner and move all tests to it 2022-05-25 13:31:26 +00:00
Jakob Degen
168c83a0b7 Adjust Miri to also require return places everywhere 2022-05-24 17:16:36 +02:00
Ralf Jung
697dca2e0e clean up int2ptr code a bit 2022-05-23 10:03:33 +02:00
bors
ab03d32622 Auto merge of #2059 - carbotaniuman:master, r=RalfJung
Initial work on Miri permissive-exposed-provenance

Miri portions of the changes for portions of a permissive ptr-to-int model for Miri. This is more restrictive than what we currently have so it will probably need a flag once I figure out how to hook that up.

> This implements a form of permissive exposed-address provenance, wherein the only way to expose the address is with a cast to usize (ideally expose_addr). This is more restrictive than C in that stuff like reading the representation bytes (via unions, type-punning, transmute) does not expose the address, only expose_addr. This is less restrictive than C in that a pointer casted from an integer has union provenance of all exposed pointers, not any udi stuff.

There's a few TODOs here, namely related to `fn memory_read` and friends. We pass it the maybe/unreified provenance before `ptr_get_alloc` reifies it into a concrete one, so it doesn't have the `AllocId` (or the SB tag, but that's getting ahead of ourselves). One way this could be fixed is changing `ptr_get_alloc` and (`ptr_try_get_alloc_id` on the rustc side) to return a pointer with the tag fixed up. We could also take in different arguments, but I'm not sure what works best.

The other TODOs here are how permissive this model could be. This currently does not enforce that a ptr-to-int cast happens before the corresponding int-to-ptr (colloquial meaning of happens before, not atomic meaning). Example:

```
let ptr = 0x2000 as *const i32;
let a: i32 = 5;
let a_ptr = &a as *const i32;

// value is 0x2000;
a_ptr as usize;

println!("{}", unsafe { *ptr }); // this is valid
```

We also allow the resulting pointer to dereference different non-contiguous allocations (the "not any udi stuff" mentioned above), which I'm not sure if is allowed by LLVM.

This is the Miri side of https://github.com/rust-lang/rust/pull/95826.
2022-05-23 07:24:33 +00:00
carbotaniuman
f7bc441fd3 Initial work on permissive provenance 2022-05-23 09:07:31 +02:00
bors
72e11d370a Auto merge of #2105 - thomcc:flip-failure-rate, r=RalfJung
Fix backwards `cmpxchg_weak_failure_rate` check

Uh, I guess I got this backwards a long time ago 😓.
2022-05-23 07:03:49 +00:00
Thom Chiovoloni
fdfbd7a658
Fix backwards cmpxchg_weak_failure_rate check 2022-05-22 20:24:08 -07:00
Ben Kimock
b20c6cfd81 Factor current-span logic into a lazy caching handle 2022-05-22 18:23:01 -04:00
Matthias Krüger
89da571b5d shims: fs: silence stderr instead of stdout.
Fixes #2143
2022-05-22 21:54:00 +02:00
Matthias Krüger
d7d7a9a162 [NFC] shims: fs: fmt 2022-05-22 21:47:44 +02:00
bors
e3227cfc7e Auto merge of #2141 - saethlin:early-diagnostics-ice, r=RalfJung
Adjust diagnostics assertion so we don't ICE in setup

Fixes https://github.com/rust-lang/miri/issues/2076 just by handling diagnostics produced during setup. The tracking notes don't have any spans but it's better than an ICE.

It looks like we leak allocations 1..20, and allocations 13..19 don't have any creation notes, and 14 only has a `FreedAlloc` alloc tracking diagnostic.
2022-05-22 16:17:17 +00:00
Ralf Jung
73534a678d
tweak comment 2022-05-22 18:16:59 +02:00
Ben Kimock
486a769935 Handle diagnotics emitted in runtime initialization 2022-05-22 11:28:40 -04:00
bors
d58abab2e7 Auto merge of #2142 - saethlin:cleanup-data-race-ice, r=oli-obk
Make allow_data_races_* public and use it during EnvVars::cleanup

Fixes https://github.com/rust-lang/miri/issues/2020

I've tried for hours now to come up with a test case for this ICE with no luck. I suspect there's something about the way the data race detection works under these conditions that I just don't understand 😩.

But I tried this change out on a handful of crates and I don't see any more ICEs of this form. For whatever reason it seems like `bastion==0.4.5` is a good way to run into this, with the flags
```
MIRIFLAGS="-Zmiri-tag-raw-pointers -Zmiri-panic-on-unsupported -Zmiri-disable-isolation" cargo +miri miri test --no-fail-fast --doc
```
I think all the cases I've run into with this involve both `-Zmiri-panic-on-unsupported` and `-Zmiri-tag-raw-pointers`, so it could be that the combination of an unexpected panic and a machine halt is required.
2022-05-22 06:18:29 +00:00
Mateusz Gienieczko
bd731508d4
Use precomputed layouts. 2022-05-22 01:01:12 +02:00
Mateusz Gienieczko
a40ff562a0
Add i16 and u16 primitive layout. 2022-05-22 01:00:59 +02:00
Mateusz Gienieczko
2fa53c0385
Dynamic offset calculation in GetSystemInfo. 2022-05-21 21:42:25 +02:00
Ben Kimock
3cfce6ffb2 Make allow_data_races_* public to silence data races during cleanup 2022-05-21 15:16:15 -04:00
Mateusz Gienieczko
63e98aee0c
Change GetSystemInfo to explicit offset. 2022-05-21 19:44:12 +02:00
Mateusz Gienieczko
c4ee368acb
Set page size in GetSystemInfo. 2022-05-21 18:17:25 +02:00
Ralf Jung
8b4d613cc8 rustup 2022-05-20 18:37:57 +02:00
bors
9230b92cf2 Auto merge of #2128 - saethlin:fix-creation-range, r=RalfJung
Pass the correct size to the AllocRange for log_creation

Fixes https://github.com/rust-lang/miri/issues/2127

I guess all I needed was a bit of sleep and reassurance that this diagnostic is the wrong part of that situation.
2022-05-19 15:08:22 +00:00
Ben Kimock
ada864f387 Pass the correct size to the AllocRange for log_creation 2022-05-19 09:29:08 -04:00
Ralf Jung
ea63a695c8 rustup 2022-05-15 12:58:28 +02:00
Ralf Jung
90a190e03b don't ICE when libcore is missing 2022-05-15 10:26:47 +02:00
bors
90d28eada1 Auto merge of #2116 - carbotaniuman:minimal-miri-changes, r=RalfJung
Minimal miri changes for rustc permissive provenance

Simple no-op changes to adapt to new API surface.
2022-05-15 07:11:51 +00:00
bors
98c8c8f9b5 Auto merge of #2030 - saethlin:track-alloc-history, r=oli-obk
Print spans where tags are created and invalidated

5225225 called this "automatic tag tracking" and I think that may be a reasonable description, but I would like to kill tag tracking as a primary use of Miri if possible. Tag tracking isn't always possible; for example if the UB is only detected with isolation off and the failing tag is made unstable by removing isolation. (also it's bad UX to run the tool twice)

This is just one of the things we can do with https://github.com/rust-lang/miri/pull/2024

The memory usage of this is _shockingly_ low, I think because the memory usage of Miri is driven by allocations where each byte ends up with its own very large stack. The memory usage in this change is linear with the number of tags, not tags * bytes. If memory usage gets out of control we can cap the number of events we save per allocation, from experience we tend to only use the most recent few in diagnostics but of course there's no guarantee of that so if we can manage to keep everything that would be best.

In many cases now I can tell exactly what these codebases are doing wrong just from the new outputs here, which I think is extremely cool.

New helps generated with plain old `cargo miri test` on `rust-argon2` v1.0.0:
```
test argon2::tests::single_thread_verification_multi_lane_hash ... error: Undefined Behavior: trying to reborrow <1485898> for Unique permission at alloc110523[0x0], but that tag does not exist in the borrow stack for this location
   --> /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/mem/manually_drop.rs:89:9
    |
89  |         slot.value
    |         ^^^^^^^^^^
    |         |
    |         trying to reborrow <1485898> for Unique permission at alloc110523[0x0], but that tag does not exist in the borrow stack for this location
    |         this error occurs as part of a reborrow at alloc110523[0x0..0x20]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <1485898> was created by a retag at offsets [0x0..0x20]
   --> src/memory.rs:42:13
    |
42  |             vec.push(unsafe { &mut (*ptr) });
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: <1485898> was later invalidated at offsets [0x0..0x20]
   --> src/memory.rs:42:31
    |
42  |             vec.push(unsafe { &mut (*ptr) });
    |                               ^^^^^^^^^^^
```

And with `-Zmiri-tag-raw-pointers` on `slab` v0.4.5
```
error: Undefined Behavior: trying to reborrow <2915> for Unique permission at alloc1418[0x0], but that tag does not exist in the borrow stack for this location
   --> /tmp/slab-0.4.5/src/lib.rs:835:16
    |
835 |         match (&mut *ptr1, &mut *ptr2) {
    |                ^^^^^^^^^^
    |                |
    |                trying to reborrow <2915> for Unique permission at alloc1418[0x0], but that tag does not exist in the borrow stack for this location
    |                this error occurs as part of a reborrow at alloc1418[0x0..0x10]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <2915> was created by a retag at offsets [0x0..0x10]
   --> /tmp/slab-0.4.5/src/lib.rs:833:20
    |
833 |         let ptr1 = self.entries.get_unchecked_mut(key1) as *mut Entry<T>;
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: <2915> was later invalidated at offsets [0x0..0x20]
   --> /tmp/slab-0.4.5/src/lib.rs:834:20
    |
834 |         let ptr2 = self.entries.get_unchecked_mut(key2) as *mut Entry<T>;
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

And without raw pointer tagging, `cargo miri test` on `half` v1.8.2
```
error: Undefined Behavior: trying to reborrow <untagged> for Unique permission at alloc1340[0x0], but that tag only grants SharedReadOnly permission for this location
   --> /home/ben/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/slice/raw.rs:141:9
    |
141 |         &mut *ptr::slice_from_raw_parts_mut(data, len)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |         |
    |         trying to reborrow <untagged> for Unique permission at alloc1340[0x0], but that tag only grants SharedReadOnly permission for this location
    |         this error occurs as part of a reborrow at alloc1340[0x0..0x6]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: tag was most recently created at offsets [0x0..0x6]
   --> /tmp/half-1.8.2/src/slice.rs:309:22
    |
309 |         let length = self.len();
    |                      ^^^^^^^^^^
help: this tag was also created here at offsets [0x0..0x6]
   --> /tmp/half-1.8.2/src/slice.rs:308:23
    |
308 |         let pointer = self.as_ptr() as *mut u16;
    |                       ^^^^^^^^^^^^^
```
The second suggestion is close to guesswork, but from experience it tends to be correct (as in, it tends to locate the pointer the user wanted) more often that it doesn't.
2022-05-14 19:27:04 +00:00
carbotaniuman
f8478df6dc Bump rustc for permissive provenance 2022-05-14 13:03:47 -05:00
Ben Kimock
8ff0aac06c More review feedback
* Store the local crates in an Rc<[CrateNum]>
* Move all the allocation history into Stacks
* Clean up the implementation of get_logs_relevant_to a bit
2022-05-13 19:04:51 -04:00
Ralf Jung
fde022007b data_race: use glob import like most files 2022-05-13 23:19:17 +02:00
bors
3f111c166a Auto merge of #2114 - cbeuw:shim-rmw, r=RalfJung
Use atomic RMW for `{mutex, rwlock, cond, srwlock}_get_or_create_id` functions

This is required for #1963

`{mutex, rwlock, cond, srwlock}_get_or_create_id()` currently checks whether an ID field is 0 using an atomic read, allocate one and get a new ID if it is, then write it in a separate atomic write. This is fine without weak memory. For instance, in `pthread_mutex_lock` which may be called by two threads concurrently, only one thread can read 0, create and then write a new ID, the later-run thread will always see the newly created ID and never 0.
```rust
    fn pthread_mutex_lock(&mut self, mutex_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
        let this = self.eval_context_mut();

        let kind = mutex_get_kind(this, mutex_op)?.check_init()?;
        let id = mutex_get_or_create_id(this, mutex_op)?;
        let active_thread = this.get_active_thread();
```

However, with weak memory behaviour, both threads may read 0: the first thread has to see 0 because nothing else was written to it, and the second thread is not guaranteed to observe the latest value, causing a duplicate mutex to be created and both threads "successfully" acquiring the lock at the same time.

This is a pretty typical pattern requiring the use of atomic RMWs. RMW *always* reads the latest value in a location, so only one thread can create the new mutex and ID, all others scheduled later will see the new ID.
2022-05-13 19:04:59 +00:00
Andy Wang
9e38dc4d49
Move and rename offset_and_layout_to_place to deref_operand_and_offset 2022-05-13 18:42:53 +01:00
Andy Wang
10d978c180
Inline _create() calls and add assertions 2022-05-12 21:06:17 +01:00
Andy Wang
a5db2c32e5
Refactor to hide *_next_id functions 2022-05-12 20:31:40 +01:00
bors
d33e7fc31c Auto merge of #2117 - RalfJung:rustup, r=RalfJung
rustup

Cc https://github.com/rust-lang/rust/issues/96975
2022-05-12 17:02:04 +00:00
Ralf Jung
19e1c72a77 rustup 2022-05-12 19:01:04 +02:00
Oli Scherer
0ee9d3d047 Update a path to libstd source in our comments 2022-05-12 10:42:53 +00:00
Ben Kimock
972b3b340a Cleanup/Refactoring from review
* Pass a ThreadInfo down to grant/access to get the current span lazily
* Rename add_* to log_* for clarity
* Hoist borrow_mut calls out of loops by tweaking the for_each signature
* Explain the parameters of check_protector a bit more
2022-05-11 20:07:44 -04:00
Andy Wang
1a7f6d504a
Use proper atomic rmw for {mutex, rwlock, cond, srwlock}_get_or_create_id 2022-05-11 22:42:39 +01:00
Ralf Jung
c15845bf29 when MIRI_LOG is set, set RUSTC_LOG_ENTRY_EXIT 2022-05-11 10:04:30 +02:00
Ralf Jung
a2f6837574 rustfmt 2022-05-10 12:21:01 +02:00
bors
36c274aa38 Auto merge of #2084 - rust-lang:silence, r=RalfJung
Add a command line flag to avoid printing to stdout and stderr

This is practical for tests that don't actually care about the output and thus don't want it intermingled with miri's warnings, errors or ICEs

fixes #2083
2022-05-10 07:24:07 +00:00
Ralf Jung
8dbe1d02cd rustfmt 2022-05-09 11:17:52 +02:00
Ralf Jung
5a6c4a60fe rustup 2022-05-09 10:21:15 +02:00
Ralf Jung
d585b92fe3 test for "erroneous constant used" post-monomorphization error 2022-05-08 17:56:09 +02:00
Ralf Jung
9605ae807d rustup 2022-05-06 17:28:01 +02:00
bors
4bb36593e9 Auto merge of #2094 - dtolnay-contrib:truncate, r=RalfJung
Replace `as` casts in llvm.x86.addcarry.64 implementation

Recommended in https://github.com/rust-lang/miri/pull/2090#pullrequestreview-958491177.
2022-05-01 07:06:57 +00:00
Ben Kimock
68ab457aa0 Pass AccessKind to check_protector 2022-04-30 14:59:00 -04:00
David Tolnay
8d42a7cfdf
Replace as casts in llvm.x86.addcarry.64 implementation 2022-04-30 10:33:55 -07:00
David Tolnay
1d9e91ed50
Replace unneeded use of ref in favor of "match ergonomics" 2022-04-30 10:30:05 -07:00