Skip to content

[Bug] Infinite unwinding of std::ptr::drop_in_place in program containing enum wrapping std::io::Error in very specific manner #2538

Open

Description

@roypat

I tried this code:

fn main() {
    println!("Hello, world!");
}

#[derive(Debug)]
pub enum MyError {
    Variant1 { field1: usize },
    Variant2 { field2: usize, field3: usize },
    IOError(std::io::Error),
}

#[kani::proof]
fn confusion() {
    Ok::<_, MyError>(23).unwrap();
    Err::<usize, _>(MyError::Variant1 { field1: kani::any() });
}

using the following command line invocation:

cargo kani

with Kani version: 0.30.0

I expected to see this happen: Verification to complete

Instead, this happened: When running kani, it gets stuck unwinding a recursion in std::ptr::drop_in_place. Particularly, the below lines are repeated ad infinitum (with increasing iteration numbers):

aborting path on assume(false) at file /github/home/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 280 column 13 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]> thread 0
aborting path on assume(false) at file /github/home/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs line 977 column 15 function std::option::Option::<std::io::ErrorKind>::unwrap_or_else::<[closure@std::io::error::repr_bitpacked::decode_repr<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0}]> thread 0
aborting path on assume(false) at file /github/home/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hint.rs line 105 column 9 function std::hint::unreachable_unchecked thread 0
aborting path on assume(false) at file /github/home/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 266 column 17 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0} thread 0
Unwinding recursion std::ptr::drop_in_place::<std::io::error::ErrorData<std::boxed::Box<std::io::error::Custom>>> iteration 2
Unwinding recursion std::ptr::drop_in_place::<std::boxed::Box<std::io::error::Custom>> iteration 2
Unwinding recursion std::ptr::drop_in_place::<std::io::error::Custom> iteration 2
Unwinding recursion std::ptr::drop_in_place::<std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>> iteration 2
aborting path on assume(false) at  thread 0
Unwinding recursion std::ptr::drop_in_place::<MyError> iteration 3
Unwinding recursion std::ptr::drop_in_place::<std::io::Error> iteration 3
Unwinding recursion std::ptr::drop_in_place::<std::io::error::repr_bitpacked::Repr> iteration 3
Unwinding recursion <std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop iteration 3

Now, normally this would not be a problem because I can just set #[kani::unwind(...)] and ignore this, but the problem is that even with a limited unwind value, this seems to cause any proof that contain code triggering this to explode in terms of memory usage and SAT complexity (causing it to run out of memory during SAT solving even on a .metal instance). Additional weirdness I could observe:

  • I for the live of me cannot find where code inside std::io::error would ever call std::ptr::drop_in_place. The entire standard library only has a handful of these, and none of them seem to lie on the code paths here (they are in some collections and in something called a ThinBox that I'm not really sure where its used, but doesnt seem to be used here) - maybe for these it would be useful to also print a file and line number?
  • Changing almost anything about the above snippet of code makes the unwinding go away (its the result of hours of minimizing a test case, and it has left me in tears), although there is some freedom of whether the kani::any() is placed in Variant1 or Variant2, or what type is used in the place of usize)
  • I am aware that drop_in_place is an intrinsic that kani does not support, but shouldn't that just give me an error that an unsupported construct was encountered? Instead it seems like kani tries to do some verification on the stdlib "implementation" of it, which is using infinite recursion (with a comment that the body doesnt matter since it gets replaced by some compiler pass) [1].
    I also tried stubbing it out, but that didn't seem to work either - I suppose because its an intrinsic?

Activity

  1. added
    [C] Bug
    on Jun 16, 2023
  2. changed the title [-][Bug] Infinite unwinding of `std::ptr::drop_in_place` in program containing enum wrapping std::io::Error in specific very manner[/-] [+][Bug] Infinite unwinding of `std::ptr::drop_in_place` in program containing enum wrapping std::io::Error in very specific manner[/+] on Jun 16, 2023
  3. zhassan-aws commented on Jun 16, 2023

    @zhassan-aws
    Contributor

    Thanks for reporting the issue @roypat. It is reproducible without kani::any() as well:

    #[derive(Debug)]
    pub enum MyError {
        Variant1 { field1: usize },
        Variant2 { field2: usize, field3: usize },
        IOError(std::io::Error),
    }
    
    #[kani::proof]
    fn confusion() {
        Ok::<_, MyError>(23).unwrap();
        Err::<usize, _>(MyError::Variant1 { field1: 0 });
    }
  4. zhassan-aws commented on Jun 16, 2023

    @zhassan-aws
    Contributor

    The infinite unwinding does not occur with function pointer restrictions:

    $ kani iss2538.rs --restrict-vtable --enable-unstable
    ...
    Checking harness confusion...
    CBMC 5.85.0 (cbmc-5.85.0)
    CBMC version 5.85.0 (cbmc-5.85.0) 64-bit x86_64 linux
    Reading GOTO program from file /home/ubuntu/examples/iss2538__RNvCs2zsFmUdw7d7_7iss25389confusion.out
    Generating GOTO Program
    Adding CPROVER library (x86_64)
    Removal of function pointers and virtual functions
    Generic Property Instrumentation
    Running with 16 object bits, 48 offset bits (user-specified)
    Starting Bounded Model Checking
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 280 column 13 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]> thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs line 977 column 15 function std::option::Option::<std::io::ErrorKind>::unwrap_or_else::<[closure@std::io::error::repr_bitpacked::decode_repr<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0}]> thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hint.rs line 105 column 9 function std::hint::unreachable_unchecked thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 266 column 17 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0} thread 0
    aborting path on assume(false) at  thread 0
    Runtime Symex: 0.55823s
    size of program expression: 28138 steps
    slicing removed 21746 assignments
    Generated 157 VCC(s), 75 remaining after simplification
    Runtime Postprocess Equation: 0.010702s
    Passing problem to propositional reduction
    converting SSA
    Runtime Convert SSA: 0.0299071s
    Running propositional reduction
    Post-processing
    Runtime Post-process: 0.00166438s
    Solving with MiniSAT 2.2.1 with simplifier
    4573 variables, 9135 clauses
    SAT checker: instance is UNSATISFIABLE
    Runtime Solver: 0.0054038s
    Runtime decision procedure: 0.0360704s
    ...
    SUMMARY:
     ** 0 of 230 failed (3 unreachable)
    
    VERIFICATION:- SUCCESSFUL
    Verification Time: 0.8520346s
    
  5. zhassan-aws commented on Jun 16, 2023

    @zhassan-aws
    Contributor

    The drop_in_place seems to be coming from formatting code associated with the Debug trait. If I replace the derive(Debug) with an empty implementation:

    use std::fmt::{Debug, Formatter};
    
    pub enum MyError {
        Variant1 { field1: usize },
        Variant2 { field2: usize, field3: usize },
        IOError(std::io::Error),
    }
    
    impl Debug for MyError {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            Ok(())
        }
    }
    
    #[kani::proof]
    fn confusion() {
        Ok::<_, MyError>(23).unwrap();
        Err::<usize, _>(MyError::Variant1 { field1: 0 });
    }

    the generated MIR does not reference drop_in_place:

    $ grep -c drop_in iss2538.kani.mir 
    0
    

    However, the unwind log from CBMC mysteriously still refers to drop_in_place!

    Starting Bounded Model Checking
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 280 column 13 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]> thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs line 977 column 15 function std::option::Option::<std::io::ErrorKind>::unwrap_or_else::<[closure@std::io::error::repr_bitpacked::decode_repr<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0}]> thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hint.rs line 105 column 9 function std::hint::unreachable_unchecked thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 266 column 17 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0} thread 0
    aborting path on assume(false) at  thread 0
    Unwinding recursion std::ptr::drop_in_place::<MyError> iteration 1
    Unwinding recursion std::ptr::drop_in_place::<std::io::Error> iteration 1
    Unwinding recursion std::ptr::drop_in_place::<std::io::error::repr_bitpacked::Repr> iteration 1
    Unwinding recursion <std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop iteration 1
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 280 column 13 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]> thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs line 977 column 15 function std::option::Option::<std::io::ErrorKind>::unwrap_or_else::<[closure@std::io::error::repr_bitpacked::decode_repr<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0}]> thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hint.rs line 105 column 9 function std::hint::unreachable_unchecked thread 0
    aborting path on assume(false) at file /home/ubuntu/.rustup/toolchains/nightly-2023-04-30-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/error/repr_bitpacked.rs line 266 column 17 function std::io::error::repr_bitpacked::decode_repr::<std::boxed::Box<std::io::error::Custom>, [closure@<std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop::{closure#0}]>::{closure#0} thread 0
    Unwinding recursion std::ptr::drop_in_place::<std::io::error::ErrorData<std::boxed::Box<std::io::error::Custom>>> iteration 1
    Unwinding recursion std::ptr::drop_in_place::<std::boxed::Box<std::io::error::Custom>> iteration 1
    Unwinding recursion std::ptr::drop_in_place::<std::io::error::Custom> iteration 1
    Unwinding recursion std::ptr::drop_in_place::<std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>> iteration 1
    aborting path on assume(false) at  thread 0
    Unwinding recursion std::ptr::drop_in_place::<MyError> iteration 2
    Unwinding recursion std::ptr::drop_in_place::<std::io::Error> iteration 2
    Unwinding recursion std::ptr::drop_in_place::<std::io::error::repr_bitpacked::Repr> iteration 2
    Unwinding recursion <std::io::error::repr_bitpacked::Repr as std::ops::Drop>::drop iteration 2
    
  6. roypat commented on Jun 16, 2023

    @roypat
    Author

    Hey @zhassan-aws, thanks for looking into this so quickly! I just checked and can confirm that --restrict-vtable --enable-unstable also makes the more complex proof from which this example is derived work! :D

  7. added
    T-User
    on Jun 16, 2023
  8. celinval commented on Jun 19, 2023

    @celinval
    Contributor

    The drop_in_place seems to be coming from formatting code associated with the Debug trait. If I replace the derive(Debug) with an empty implementation:

    use std::fmt::{Debug, Formatter};
    
    pub enum MyError {
        Variant1 { field1: usize },
        Variant2 { field2: usize, field3: usize },
        IOError(std::io::Error),
    }
    
    impl Debug for MyError {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            Ok(())
        }
    }
    
    #[kani::proof]
    fn confusion() {
        Ok::<_, MyError>(23).unwrap();
        Err::<usize, _>(MyError::Variant1 { field1: 0 });
    }

    the generated MIR does not reference drop_in_place:

    $ grep -c drop_in iss2538.kani.mir 
    0
    

    However, the unwind log from CBMC mysteriously still refers to drop_in_place!

    Unfortunately, not everything gets dumped into the *kani.mir files. The rust compiler doesn't support dumping the MIR generated for some items such as DropGlue. That's why you are not seeing any occurrence of drop_in_place. However, that is still being generated by the compiler and it will be invoked when we handle Drop statements.

  9. self-assigned this
    on Aug 22, 2023
  10. roypat commented on Aug 22, 2023

    @roypat
    Author

    This issue is fairly prominent in the harnesses included in firecracker-microvm/firecracker#4066, where verification only completes without going out of memory thanks to low unwind values (2). Increasing the bounds will often leads to out of memory or severely increased runtime, which seems to be related to unwinding in std::ptr::drop_in_place and the std::io::error module.

  11. added a commit that references this issue on Sep 7, 2023
  12. added
    [E] Performance
    and removed
    [C] Bug
    on Sep 22, 2023
  13. added this to Kani 2023-10-24 and removed this from Kani 2023-09-12on Oct 3, 2023
  14. added
    [C] Bug
    T-High Priority
    on Nov 5, 2026
  15. added theissue type on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-High PriorityT-User[C] Bug[E] Performance[F] Crash

Type

Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions