im-rs icon indicating copy to clipboard operation
im-rs copied to clipboard

Undefined Behavior with `im::OrdSet`

Open JustForFun88 opened this issue 2 years ago • 1 comments

If you try run this code:

fn main() {
    let iter = (0..11).map(|x| x.to_string());
    let container = iter.collect::<im::OrdSet<String>>();
}

with

cargo miri run

it causes:

error: Undefined Behavior: attempting a read access using <20271> at alloc2231[0x40], but that tag does not exist in the borrow stack for this location
   --> C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\sized-chunks-0.6.5\src\sized_chunk\mod.rs:289:13
    |
289 |             ptr::copy(chunk.ptr(from), chunk.mut_ptr(to), count)
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |             |
    |             attempting a read access using <20271> at alloc2231[0x40], but that tag does not exist in the borrow stack for this location
    |             this error occurs as part of an access at alloc2231[0x40..0x100]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows 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: <20271> was created by a SharedReadOnly retag at offsets [0x10..0x610]
   --> src\main.rs:3:21
    |
3   |     let container = iter.collect::<im::OrdSet<String>>();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: <20271> was later invalidated at offsets [0x10..0x620] by a Unique FnEntry retag inside this call
   --> src\main.rs:3:21
    |
3   |     let container = iter.collect::<im::OrdSet<String>>();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: BACKTRACE:
    = note: inside `sized_chunks::sized_chunk::Chunk::<im::ordset::Value<std::string::String>>::force_copy` at C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\sized-chunks-0.6.5\src\sized_chunk\mod.rs:289:13
    = note: inside `sized_chunks::sized_chunk::Chunk::<im::ordset::Value<std::string::String>>::insert` at C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\sized-chunks-0.6.5\src\sized_chunk\mod.rs:567:17
    = note: inside `im::nodes::btree::Node::<im::ordset::Value<std::string::String>>::insert` at C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\im-15.1.0\./src\nodes\btree.rs:607:29
    = note: inside `im::OrdSet::<std::string::String>::insert` at C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\im-15.1.0\./src\ord\set.rs:494:19        
    = note: inside `<im::OrdSet<std::string::String> as std::iter::FromIterator<std::string::String>>::from_iter::<std::iter::Map<std::ops::Range<i32>, [closure@src\main.rs:2:28: 2:31]>>` at C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\im-15.1.0\./src\ord\set.rs:1070:13
    = note: inside `<std::iter::Map<std::ops::Range<i32>, [closure@src\main.rs:2:28: 2:31]> as std::iter::Iterator>::collect::<im::OrdSet<std::string::String>>` at C:\Users\user\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\iter\traits\iterator.rs:1836:9
note: inside `main` at src\main.rs:3:21
   --> src\main.rs:3:21
    |
3   |     let container = iter.collect::<im::OrdSet<String>>();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error; 1 warning emitted

JustForFun88 avatar Feb 04 '23 09:02 JustForFun88

same for me

raiscui avatar Feb 22 '23 09:02 raiscui