mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-28 13:00:26 +00:00
![bors[bot]](/assets/img/avatar_default.png)
280: Fix undefined behavior identified by Miri r=japaric a=jgallagher Hi! We ran into an exception triggered by new undefined behavior checks inserted into the nightly compiler (https://github.com/rust-lang/rust/pull/92686/files#diff-54110dcedc5a4d976321aa5d2a6767ac0744a3ef1363b75ffc62faf81cf14c30R230-L229). Running `heapless`'s test suite under Miri didn't flag anything at first, but it did once we added `MIRIFLAGS="-Zmiri-tag-raw-pointers"`. All three of the fixes in this PR were identified via ``` MIRIFLAGS="-Zmiri-tag-raw-pointers -Zmiri-ignore-leaks" cargo +nightly miri test -- --skip pool:: ``` and the fixes came from copying the implementations from the equivalent methods in `std`. Note that I skipped the `pool::` tests; there is at least one miri failure in them, but it wasn't immediately obvious how to fix it so I skipped it for now. It's probably worth adding the flag above to the CI miri run, but I didn't do that either (since it would immediately cause failures given I didn't fix the problem in `pool`). The specific output for `pool` is ``` test pool::singleton::tests::sanity ... error: Undefined Behavior: trying to reborrow <untagged> for SharedReadWrite permission at alloc36[0x1], but that tag does not exist in the borrow stack for this location --> /home/john/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/non_null.rs:380:18 | 380 | unsafe { &*self.as_ptr() } | ^^^^^^^^^^^^^^^ | | | trying to reborrow <untagged> for SharedReadWrite permission at alloc36[0x1], but that tag does not exist in the borrow stack for this location | this error occurs as part of a reborrow at alloc36[0x1..0x9] | = 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 = note: inside `std::ptr::NonNull::<pool::stack::Node<u8>>::as_ref` at /home/john/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/non_null.rs:380:18 note: inside `pool::stack::Stack::<u8>::push` at src/pool/cas.rs:43:17 --> src/pool/cas.rs:43:17 | 43 | / new_head 44 | | .as_raw() 45 | | .as_ref() | |_____________________________^ note: inside `pool::Pool::<u8>::grow` at src/pool/mod.rs:390:25 --> src/pool/mod.rs:390:25 | 390 | self.stack.push(p); | ^^^^^^^^^^^^^^^^^^ note: inside `<pool::singleton::tests::sanity::A as pool::singleton::Pool>::grow` at src/pool/singleton.rs:78:9 --> src/pool/singleton.rs:78:9 | 78 | Self::ptr().grow(memory) | ^^^^^^^^^^^^^^^^^^^^^^^^ note: inside `pool::singleton::tests::sanity` at src/pool/singleton.rs:362:9 --> src/pool/singleton.rs:362:9 | 362 | A::grow(unsafe { &mut MEMORY }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: inside closure at src/pool/singleton.rs:353:5 --> src/pool/singleton.rs:353:5 | 352 | #[test] | ------- in this procedural macro expansion 353 | / fn sanity() { 354 | | const SZ: usize = 2 * mem::size_of::<Node<u8>>() - 1; 355 | | static mut MEMORY: [u8; SZ] = [0; SZ]; 356 | | ... | 373 | | assert_eq!(*A::alloc().unwrap().init(1), 1); 374 | | } | |_____^ = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info) ``` Co-authored-by: John Gallagher <john@oxidecomputer.com>
heapless
static
friendly data structures that don't require dynamic memory allocation
Documentation
Change log
Tests
# run all
cargo test --features 'serde','x86-sync-pool'
# run only for example histbuf tests
cargo test histbuf --features 'serde','x86-sync-pool'
License
Licensed under either of
-
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
-
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Description
Languages
Rust
100%