Skip to content

How to verify a target different to the host environment? #2086

Closed

Description

@zpzigi754

Currently, kani supports a limited number of targets (x86_64-unknown-linux-gnu, x86_64-apple-darwin and aarch64-apple-darwin ) and the verification target should be the same with the host environment.
I think that being able to verify a code for the target different to the host environment would enhance the kani's overall applicability especially for the embedded environment.

To verify a code for the target different to the host environment, I've tried the following steps.

  1. prepare for the CBMC machine model by modifying machine_model_from_session()
  2. prepare for the kani session for the target environment by modifying cargo_build() so that kani's cargo build could be called with the custom target triple (e.g., 32bit architecture).
  3. build kani-library (libkani.rlib and libstd.rlib) for the target environment while building kani-compiler and kani-driver for the host environment

I initially thought the the above steps might be enough, but I've encountered the following error.

$ cargo kani --verbose
[Kani] Running: `KANIFLAGS="--goto-c --log-level=info --assertion-reach-checks -C symbol-mangling-version=v0" RUSTC="/home/xxx/kani/target/debug/kani-compiler" RUSTFLAGS="--kani-flags" cargo rustc --target armv7-unknown-linux-gnueabi --target-dir /home/xxx/test/kani-test/size/target/kani -v -p size --lib -- --reachability=harnesses`

Running `/home/xxx/kani/target/debug/kani-compiler --crate-name size --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --reachability=harnesses -C metadata=0446ecd2f07a989c -C extra-filename=-0446ecd2f07a989c --out-dir /home/xxx/test/kani-test/size/target/kani/armv7-unknown-linux-gnueabi/debug/deps --target armv7-unknown-linux-gnueabi -C incremental=/home/xxx/test/kani-test/size/target/kani/armv7-unknown-linux-gnueabi/debug/incremental -L dependency=/home/xxx/test/kani-test/size/target/kani/armv7-unknown-linux-gnueabi/debug/deps -L dependency=/home/xxx/test/kani-test/size/target/kani/debug/deps --kani-flags`

error[E0460]: found possibly newer version of crate `std` which `std` depends on
  |
  = note: perhaps that crate needs to be recompiled?
  = note: the following crate versions were found:
          crate `std`: /home/xxx/kani/target/lib/libstd.rlib
          crate `std`: /home/xxx/kani/target/lib/libstd.rlib

Am I miss something or what additional steps needs to be done?

Activity

  1. celinval commented on Jan 6, 2023

    @celinval
    Contributor

    Hi @zpzigi754, how did you build the kani-library? Kani also rebuild the standard library. Also in the process of doing so, there is a name conflict since we are building 2 std. You can see how we build our libraries here:

    pub fn build_lib() {

    Also, be aware that we have never tested Kani with 32bit architectures, so there could be hidden assumptions in our code that consider that the platform is 64bits.

  2. zpzigi754 commented on Jan 9, 2023

    @zpzigi754
    Author

    Thank you for the reference, @celinval. By that time, I built the kani-library with cargo build -Z build-std in each of the sub-directory (kani and std). Using the mentioned link about build_lib(), I could resolve the initial std-related error and verify a tiny code for 32 bit architecture. However, it seems that more engineering effort is needed because of the hidden assumptions about 64 bit architecture.

  3. zpzigi754 commented on Jan 10, 2023

    @zpzigi754
    Author

    During the work for 32bit architecture support, one problem appeared when using a reference operator.

    // File: main.rs
    #[kani::proof]
    fn main() {
        let i:i32 = 3;
        let a = &i;
    }
    

    With the above harness, it generates a cbmc fault like

    Checking harness main...
    CBMC 5.72.0 (cbmc-5.72.0)
    CBMC version 5.72.0 (cbmc-5.72.0) 64-bit x86_64 linux
    Reading GOTO program from file /home/xxx/test/kani-test/update32-simple/target/kani/armv7-unknown-linux-gnueabi/debug/deps/update-f565c83c6014376a.for-main.out
    Generating GOTO Program
    Adding CPROVER library (x86_64)
    Removal of function pointers and virtual functions
    Generic Property Instrumentation
    --- begin invariant violation report ---
    Invariant check failed
    File: ../src/goto-symex/goto_symex.cpp:46 function: symex_assign
    Condition: assignments must be type consistent, got
    Reason: lhs.type() == rhs.type()
    Backtrace:
    cbmc(+0xa955f0) [0x55cd0b2115f0]
    cbmc(+0xa95b99) [0x55cd0b211b99]
    cbmc(+0x224de4) [0x55cd0a9a0de4]
    cbmc(+0x4b9638) [0x55cd0ac35638]
    cbmc(+0x542b6e) [0x55cd0acbeb6e]
    cbmc(+0x542eb8) [0x55cd0acbeeb8]
    cbmc(+0x380136) [0x55cd0aafc136]
    cbmc(+0x542f08) [0x55cd0acbef08]
    cbmc(+0x543167) [0x55cd0acbf167]
    cbmc(+0x5440ad) [0x55cd0acc00ad]
    cbmc(+0x35a8ba) [0x55cd0aad68ba]
    cbmc(+0x359f04) [0x55cd0aad5f04]
    cbmc(+0x2004f9) [0x55cd0a97c4f9]
    cbmc(+0x1fca1e) [0x55cd0a978a1e]
    cbmc(+0x1f420f) [0x55cd0a97020f]
    cbmc(+0x1e0239) [0x55cd0a95c239]
    /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x7fc8f569a083]
    cbmc(+0x1f5b8e) [0x55cd0a971b8e]
    
    Diagnostics: 
    << EXTRA DIAGNOSTICS >>
    pointer
      * width: 32
      0: signedbv
          * width: 32
    pointer
      * width: 64                               // the problematic point
      0: signedbv
          * width: 32
    source location: file /home/xxx/test/kani-test/update32-simple/src/main.rs line 5 column 13 function main
    << END EXTRA DIAGNOSTICS >>
    
    --- end invariant violation report ---
    Running with 16 object bits, 48 offset bits (user-specified)
    
    CBMC failed with status 134
    VERIFICATION:- FAILED
    

    I'm not sure why there is the width of 64 while the place and pointer's width is 32.

  4. zhassan-aws commented on Jan 11, 2023

    @zhassan-aws
    Contributor

    There might be a hard-coded assumption about the pointer size. Can you point to the branch you're using so that we can debug this issue?

  5. zpzigi754 commented on Jan 12, 2023

    @zpzigi754
    Author

    Sure. I've made a branch here. Kani could be built and run with the normal commands there (cargo build-dev for building kani and cargo kani for the verfication).

    With that branch, the below harness would be successfully verified.

    // File: main.rs
    #[kani::proof]
    fn main() {
        let i:i32 = 32;
        
        assert!(32 == i);
        let j:i64 = 64;
        assert!(64 == j);
        let q:usize = 92;
        assert!(92 == q);
        let r:isize = 128;
        assert!(128 == r);
        let x:bool = false;
        assert!(x == false);
    
        let tuple: (i32, i32) = (3, 4);
        assert!(tuple.0 == 3);
        assert!(tuple.1 == 4);
    
        let mut array:[i32;4] = [1,2,3,4];
        assert!(array[0] == 1);
        assert!(array[1] == 2);
        assert!(array[2] == 3);
        assert!(array[3] == 4);
        
        let _isize = std::mem::size_of::<isize>();
        assert!(_isize == 4);
        let _usize = std::mem::size_of::<usize>();
        assert!(_usize == 4);
    
        let size = std::mem::size_of::<[i32;4]>();
        assert!(size == 16);
    }
    
    SUMMARY:
     ** 0 of 18 failed
    
    VERIFICATION:- SUCCESSFUL
    Verification Time: 0.08172447s
    

    However, the reference operator would not work as mentioned.

  6. zhassan-aws commented on Jan 12, 2023

    @zhassan-aws
    Contributor

    Thanks for the branch! It turns out that the 64-bit value is introduced by CBMC (specifically the goto-cc invokation). Apparently, goto-cc needs to be invoked with -m32 to target a 32-bit architecture.

    This diff on top of your branch makes the program go through:

    $ git diff
    diff --git a/kani-driver/src/call_goto_cc.rs b/kani-driver/src/call_goto_cc.rs
    index 1d36bfccb74..cb227d53ffc 100644
    --- a/kani-driver/src/call_goto_cc.rs
    +++ b/kani-driver/src/call_goto_cc.rs
    @@ -29,6 +29,7 @@ pub fn link_goto_binary(&self, inputs: &[PathBuf], output: &Path) -> Result<()>
             // TODO think about this: kani_lib_c is just an empty c file. Maybe we could just
             // create such an empty file ourselves instead of having to look up this path.
             args.push(self.kani_lib_c.clone().into_os_string());
    +        args.push("-m32".into());
     
             args.push("-o".into());
             args.push(output.to_owned().into_os_string());
  7. zpzigi754 commented on Jan 13, 2023

    @zpzigi754
    Author

    @zhassan-aws, thanks a lot for the help! I've confirmed that it works with the fix.

  8. DianaNites commented on Apr 23, 2023

    @DianaNites

    Could this feature be supported upstream with a command-line flag?

  9. zhassan-aws commented on Apr 23, 2023

    @zhassan-aws
    Contributor

    @DianaNites can you open a separate issue describing what you want the command-line flag to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [C] Feature / EnhancementA new feature request or enhancement to an existing feature.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions