rust-lang / hashbrown

Rust port of Google's SwissTable hash map
https://rust-lang.github.io/hashbrown
Apache License 2.0
2.39k stars 276 forks source link

UB on aarch64_be-unknown-linux-gnu_ilp32 #474

Closed Noratrieb closed 11 months ago

Noratrieb commented 11 months ago
use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();
    map.insert(1, "a");

    println!("map = {:?}", map);
}
error: Undefined Behavior: out-of-bounds pointer arithmetic: alloc1009 has size 60, so pointer to 12 bytes starting at offset -12 is out-of-bounds
   --> /home/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.0/src/raw/mod.rs:510:22
    |
510 |             unsafe { self.ptr.as_ptr().sub(1) }
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: alloc1009 has size 60, so pointer to 12 bytes starting at offset -12 is out-of-bounds
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: alloc1009 was allocated here:
   --> src/main.rs:5:5
    |
5   |     map.insert(1, "a");
    |     ^^^^^^^^^^^^^^^^^^
    = note: BACKTRACE (of the first span):
    = note: inside `hashbrown::raw::Bucket::<(i32, &str)>::as_ptr` at /home/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.0/src/raw/mod.rs:510:22: 510:46
    = note: inside `hashbrown::raw::Bucket::<(i32, &str)>::as_ref::<'_>` at /home/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.0/src/raw/mod.rs:674:11: 674:24
    = note: inside `<hashbrown::map::Iter<'_, i32, &str> as std::iter::Iterator>::next` at /home/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/hashbrown-0.14.0/src/map.rs:4737:25: 4737:35
    = note: inside `<std::collections::hash_map::Iter<'_, i32, &str> as std::iter::Iterator>::next` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:2232:9: 2232:25
    = note: inside `std::fmt::DebugMap::<'_, '_>::entries::<&i32, &&str, std::collections::hash_map::Iter<'_, i32, &str>>` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/builders.rs:893:23: 893:30
    = note: inside `<std::collections::HashMap<i32, &str> as std::fmt::Debug>::fmt` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:1309:9: 1309:43

https://github.com/rust-lang/rust/issues/116880

Amanieu commented 11 months ago

Fixed by #475.