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

Miri test failure #19

Closed
bjorn3 opened this issue May 30, 2021 · 1 comment · Fixed by #20
Closed

Miri test failure #19

bjorn3 opened this issue May 30, 2021 · 1 comment · Fixed by #20

Comments

@bjorn3
Copy link
Contributor

bjorn3 commented May 30, 2021

Found by @evverx in systemd/systemd#19598 (comment):

test dlmalloc::tests::treemap_alloc_max ... error: Undefined Behavior: null pointer is not a valid pointer for this operation
    --> /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:242:18
     |
242  |         unsafe { intrinsics::offset(self, count) as *mut T }
     |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
     |
     = 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

     = note: inside `core::ptr::mut_ptr::<impl *mut u8>::offset` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:242:18
@evverx
Copy link

evverx commented May 30, 2021

I didn't post the full backtrace there:

     Running unittests (target/x86_64-unknown-linux-gnu/debug/deps/dlmalloc-b4f15f60020ff234)

running 2 tests
test dlmalloc::tests::treemap_alloc_max ... error: Undefined Behavior: null pointer is not a valid pointer for this operation
    --> /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:242:18
     |
242  |         unsafe { intrinsics::offset(self, count) as *mut T }
     |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
     |
     = 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

     = note: inside `core::ptr::mut_ptr::<impl *mut u8>::offset` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:242:18
note: inside `dlmalloc::Chunk::to_mem` at src/dlmalloc.rs:1726:9
    --> src/dlmalloc.rs:1726:9
     |
1726 |         (me as *mut u8).offset(2 * (mem::size_of::<usize>() as isize))
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `dlmalloc::Dlmalloc::<sys::System>::top_foot_size` at src/dlmalloc.rs:200:36
    --> src/dlmalloc.rs:200:36
     |
200  |         self.align_offset(unsafe { Chunk::to_mem(ptr::null_mut()) })
     |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `dlmalloc::Dlmalloc::<sys::System>::max_request` at src/dlmalloc.rs:167:43
    --> src/dlmalloc.rs:167:43
     |
167  |             ((!0 - (DEFAULT_GRANULARITY + self.top_foot_size() + self.malloc_alignment()) + 1)
     |                                           ^^^^^^^^^^^^^^^^^^^^
note: inside `dlmalloc::Dlmalloc::<sys::System>::malloc` at src/dlmalloc.rs:295:27
    --> src/dlmalloc.rs:295:27
     |
295  |         } else if size >= self.max_request() {
     |                           ^^^^^^^^^^^^^^^^^^
note: inside `dlmalloc::tests::setup_treemap::<sys::System>` at src/dlmalloc.rs:1791:30
    --> src/dlmalloc.rs:1791:30
     |
1791 |         let large_request1 = a.malloc(large_request_size);
     |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `dlmalloc::tests::treemap_alloc_max` at src/dlmalloc.rs:1816:13
    --> src/dlmalloc.rs:1816:13
     |
1816 |             setup_treemap(&mut a);
     |             ^^^^^^^^^^^^^^^^^^^^^
note: inside closure at src/dlmalloc.rs:1813:5
    --> src/dlmalloc.rs:1813:5
     |
1813 | /     fn treemap_alloc_max() {
1814 | |         let mut a = Dlmalloc::new(System::new());
1815 | |         unsafe {
1816 | |             setup_treemap(&mut a);
...    |
1819 | |         }
1820 | |     }
     | |_____^
     = note: inside `<[closure@src/dlmalloc.rs:1813:5: 1820:6] as core::ops::FnOnce<()>>::call_once - shim` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
     = note: inside `<fn() as core::ops::FnOnce<()>>::call_once - shim(fn())` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
     = note: inside `test::__rust_begin_short_backtrace::<fn()>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:577:5
     = note: inside closure at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:568:30
     = note: inside `<[closure@test::run_test::{closure#2}] as core::ops::FnOnce<()>>::call_once - shim(vtable)` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
     = note: inside `<alloc::boxed::Box<dyn core::ops::FnOnce() + core::marker::Send> as core::ops::FnOnce<()>>::call_once` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1575:9
     = note: inside `<std::panic::AssertUnwindSafe<alloc::boxed::Box<dyn core::ops::FnOnce() + core::marker::Send>> as core::ops::FnOnce<()>>::call_once` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:347:9
     = note: inside `std::panicking::try::do_call::<std::panic::AssertUnwindSafe<alloc::boxed::Box<dyn core::ops::FnOnce() + core::marker::Send>>, ()>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:401:40
     = note: inside `std::panicking::try::<(), std::panic::AssertUnwindSafe<alloc::boxed::Box<dyn core::ops::FnOnce() + core::marker::Send>>>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:365:19
     = note: inside `std::panic::catch_unwind::<std::panic::AssertUnwindSafe<alloc::boxed::Box<dyn core::ops::FnOnce() + core::marker::Send>>, ()>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:434:14
     = note: inside `test::run_test_in_process` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:600:18
     = note: inside closure at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:492:39
     = note: inside `test::run_test::run_test_inner` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:530:13
     = note: inside `test::run_test` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:564:28
     = note: inside `test::run_tests::<[closure@test::run_tests_console::{closure#2}]>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:305:17
     = note: inside `test::run_tests_console` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/console.rs:290:5
     = note: inside `test::test_main` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:122:15
     = note: inside `test::test_main_static` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:141:5
     = note: inside `main`
     = note: inside `<fn() as core::ops::FnOnce<()>>::call_once - shim(fn())` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
     = note: inside `std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:125:18
     = note: inside closure at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:49:18
     = note: inside `core::ops::function::impls::<impl core::ops::FnOnce<()> for &dyn core::ops::Fn() -> i32 + core::marker::Sync + std::panic::RefUnwindSafe>::call_once` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:259:13
     = note: inside `std::panicking::try::do_call::<&dyn core::ops::Fn() -> i32 + core::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:401:40
     = note: inside `std::panicking::try::<i32, &dyn core::ops::Fn() -> i32 + core::marker::Sync + std::panic::RefUnwindSafe>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:365:19
     = note: inside `std::panic::catch_unwind::<&dyn core::ops::Fn() -> i32 + core::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:434:14
     = note: inside `std::rt::lang_start_internal` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:34:21
     = note: inside `std::rt::lang_start::<()>` at /home/vagrant/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:48:5
     = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

sunfishcode added a commit to sunfishcode/rustix-dlmalloc that referenced this issue Oct 2, 2021
Split `Chunk::mem_offset` out of `Chunk::to_mem`, and use that to avoid
passing a null pointer to `Chunk::to_mem`, which calls `ptr::offset`,
which Miri diagnoses as Undefined Behavior.

Fixes alexcrichton#19.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants