Skip to content

Kani encodes string slices as null-terminated C slices #1665

Closed

Description

@giltho

In Kani's regression test suite, in offset_u8_ok.rs, an out-of-bound pointer is dereferenced, but not detected by Kani. Here's a reduced code

#![feature(core_intrinsics)]
use std::intrinsics::offset;

#[kani::proof]
fn test_offset() {
    let s: &str = "123";
    let ptr: *const u8 = s.as_ptr();
    unsafe {
        let x = *offset(ptr, 3);
    }
}

The pointer is 1-passed the bound of the object, so it is "weak_valid", in the sense defined by CompCert, meaning that it is either in-bound or one-passed-the bound of the object. According to C standard, that is not undefined behaviour, and shouldn't be detected as such. However, it seems that, in addition to check that the pointer can be created, this test also checks that it can be dereferenced which is always UB.

@danielsn mentioned it might be on purpose because the stdlib does that sometimes, but I haven't been able to find an example

Activity

  1. added
    [C] Bug
    [F] Soundness
    on Sep 12, 2022
  2. zhassan-aws commented on Sep 12, 2022

    @zhassan-aws
    Contributor

    Interestingly, Kani reports the dereference failure if I change it to an array:

    #![feature(core_intrinsics)]
    use std::intrinsics::offset;
    
    #[kani::proof]
    fn test_offset() {
        let s = [1u8, 2, 3];
        let ptr: *const u8 = s.as_ptr();
        unsafe {
            let x = *offset(ptr, 3);
        }
    }
    $ kani iss1665.rs
    <snip>
    SUMMARY:
     ** 1 of 8 failed
    Failed Checks: dereference failure: pointer outside object bounds
     File: "/home/ubuntu/examples/iss1665.rs", line 9, in test_offset
    
    VERIFICATION:- FAILED
    
  3. changed the title [-]Kani is not detecting out-of-bounds dereferencing[/-] [+]Kani encodes string slices as null-terminated C slices[/+] on Sep 12, 2022
  4. giltho commented on Sep 12, 2022

    @giltho
    ContributorAuthor

    This is due to the fact that Kani encodes rust str constants into string constants for CBMC
    However, CBMC automatically null-terminates C strings, and therefore, this is indeed not out-of-bound access for CBMC which is why it's not detected.

    The solution might be to just have CBMC make non-null terminated constants

  5. self-assigned this
    on Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[C] Bug[F] Soundness

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions