Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsound Debug impl for collections::linked_list::IterMut #85813

Closed
steffahn opened this issue May 29, 2021 · 5 comments · Fixed by #85814
Closed

Unsound Debug impl for collections::linked_list::IterMut #85813

steffahn opened this issue May 29, 2021 · 5 comments · Fixed by #85814
Labels
A-collections Area: std::collections. C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-critical Critical priority T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

use std::{collections::LinkedList, fmt, sync::Barrier, thread};

struct BadDebugBehavior(Vec<&'static i32>, Barrier, Barrier);
impl fmt::Debug for BadDebugBehavior {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let elem = &self.0[0];
        self.1.wait();
        self.2.wait();
        write!(f, "{}", elem)
    }
}
fn main() {
    let l = Box::leak(Box::new(LinkedList::new()));
    l.push_back(BadDebugBehavior(vec![&0], Barrier::new(2), Barrier::new(2)));
    let mut i = l.iter_mut();
    let r = i.next().unwrap();
    let t = thread::spawn(move || {
        r.1.wait();
        r.0.push(&0);
        r.2.wait();
    });
    dbg!(&i);

    t.join().unwrap();
}
   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.67s
     Running `target/debug/playground`
[src/main.rs:22] &i = IterMut(
    [
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     9 Segmentation fault      timeout --signal=KILL ${timeout} "$@"

(playground)

@rustbot label T-libs-impl, A-collections


Caused by violation of safety invariant as documented in IterMut

/// A mutable iterator over the elements of a `LinkedList`.
///
/// This `struct` is created by [`LinkedList::iter_mut()`]. See its
/// documentation for more.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, T: 'a> {
    // We do *not* exclusively own the entire list here, references to node's `element`
    // have been handed out by the iterator! So be careful when using this; the methods
    // called must be aware that there can be aliasing pointers to `element`.
    list: &'a mut LinkedList<T>,
    head: Option<NonNull<Node<T>>>,
    tail: Option<NonNull<Node<T>>>,
    len: usize,
}

yet Debug implementation does

#[stable(feature = "collection_debug", since = "1.17.0")]
impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("IterMut").field(&self.list).field(&self.len).finish()
    }
}

https://doc.rust-lang.org/nightly/src/alloc/collections/linked_list.rs.html#79

I’m not sure where the list field is really used at all, so maybe it can be replaced by a marker marker: PhantomData<&'a mut Node<T>>, similar to linked_list::Iter’s implementation.

@steffahn steffahn added the C-bug Category: This is a bug. label May 29, 2021
@rustbot rustbot added A-collections Area: std::collections. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 29, 2021
@jyn514 jyn514 added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label May 29, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 29, 2021
@hameerabbasi
Copy link
Contributor

Assigning P-critical according to the WG-prioritization discussion on Zulip and removing I-prioritize.

@rustbot modify labels -I-prioritize +P-critical

@rustbot rustbot added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels May 30, 2021
@RalfJung
Copy link
Member

Good catch! Iterator debug implementations seem to be woefully under-audited in terms of soundness problems (this is not the first such issue).

@steffahn
Copy link
Member Author

steffahn commented May 30, 2021

@RalfJung maybe you can help me out with how to run miri while hacking std (a question that came up for the PR fixing this: #85814 (comment)). I have no trouble compiling or testing a modified std with x.py, or linking a stage1 toolchain for rustc usage (so that cargo check or cargo build are working) so that I can play around with it in a crate, but I don’t know how to run miri on such a local crate depending-on/using a modified std.

@RalfJung
Copy link
Member

RalfJung commented May 30, 2021

Miri uses xargo for sysroot compilation, so setting XARGO_RUST_SRC will tell Miri which standard library to use. (It must be set to the library folder in a rustc checkout.) You can then use a standard Miri, no rustup toolchain linking is required.

@RalfJung
Copy link
Member

Oh, also, if you want to run the alloc/core test suites in Miri, see https://github.com/RalfJung/miri-test-libstd.

@bors bors closed this as completed in 6a3dce9 May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: std::collections. C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-critical Critical priority T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants