Skip to content

debug_assert is disabled in the standard library due to poor assertion messages #1740

Description

@celinval

The custom sysroot build for the MIR Linker introduced by #1717 builds the standard library in debug mode. This enables a bunch of safety checks and debug assertions from the standard library. Some of these checks are for intrinsics safety checks. The issues here are:

  1. A lot of these checks make our intrinsics checks redundant.
  2. The std checks are added before actually calling the intrinsic and they are not super user friendly.

For example, the following testcase from the expected suite:

#[kani::proof]
fn test_copy_nonoverlapping_unaligned() {
let arr: [i32; 3] = [0, 1, 0];
let src: *const i32 = arr.as_ptr();
// Passing `max_count` is guaranteed to overflow
// the count in bytes for `i32` pointers
let max_count = usize::MAX / 4 + 1;
unsafe {
let dst = src.add(1) as *mut i32;
core::intrinsics::copy_nonoverlapping(src, dst, max_count);
}
}

Without the MIR Linker, it fails with the following error:

Failed Checks: copy_nonoverlapping: attempt to compute number in bytes which would overflow

With the MIR Linker, the error is:

Failed Checks: called `Option::unwrap()` on a `None` value 

This happens because of the std check is_nonoverlapping which has a call to checked_mul().unwrap(). The checked_mul() is None in the overflow scenario.

Activity

  1. celinval commented on Oct 3, 2022

    @celinval
    ContributorAuthor

    Another interesting one is that the checks are not encoded as normal debug_assert!() which panic!(), they call abort instead to optimize the compilation size. So other failures will look like:

    Failed Checks: reached intrinsic::abort
    
  2. self-assigned this
    on Oct 26, 2022
  3. added
    [C] Feature / Enhancement
    [E] User Experience
    on Nov 14, 2022
  4. changed the title [-]Intrinsics safety failures are not as friendly with `--mir-linker`[/-] [+]`debug_assert` is disabled in the standard library due to poor assertion messages[/+] on Nov 14, 2022
  5. celinval commented on Jan 17, 2023

    @celinval
    ContributorAuthor

    The current behavior implemented for MIR Linker keeps the same behavior as before the MIR Linker. Enabling these checks are desirable but they are orthogonal to the MIR Linker work. Hence, I'm removing the link with the MIR Linker milestone.

  6. cameron1024 commented on Feb 4, 2023

    @cameron1024

    Hi, I've just run into this issue trying to verify a program that depends on unicode-bidi, which has this snippet:

    #[cfg(feature = "std")]
    std::debug_assert!(
        false,
        "Found broken indices in level run: found indices {}..{} for string of length {}",
        level_run.start,
        level_run.end,
        text.len()
    );

    This fails with an error:

    error: cannot find macro `__kani__workaround_core_assert` in this scope
    

    I'm very new to kani, so I'm not exactly sure if this is actually the same issue, or just user error. I just stumbled upon this issue when googling.

    Are there steps a user can take to work around this error? (Apologies if this is the wrong place to ask these sorts of questions 😅 )

  7. zhassan-aws commented on Feb 4, 2023

    @zhassan-aws
    Contributor

    Thanks for the bug report @cameron1024. Can you please file a separate issue and include information on how to reproduce the error, and the Kani version? Thanks!

  8. cameron1024 commented on Feb 4, 2023

    @cameron1024

    Opened #2187

  9. celinval commented on Jan 24, 2025

    @celinval
    ContributorAuthor

    We should revisit this issue. One thing to keep it mind is how to distinguish UB checks and assertion failures.

  10. removed their assignment
    on Jan 24, 2025
  11. self-assigned this
    on Nov 5, 2026
  12. added
    Z-Kani Compiler
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Z-Kani Compiler[C] Feature / Enhancement[E] User Experience

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions