forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for stubbing out foreign functions (model-checking#2658)
Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
- Loading branch information
1 parent
63f513f
commit 6a151f9
Showing
4 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// kani-flags: --enable-unstable --enable-stubbing | ||
// | ||
//! Check support for stubbing out foreign functions. | ||
|
||
#![feature(rustc_private)] | ||
extern crate libc; | ||
|
||
use libc::c_char; | ||
use libc::c_int; | ||
use libc::c_longlong; | ||
use libc::size_t; | ||
|
||
#[allow(dead_code)] // Avoid warning when using stubs. | ||
#[allow(unused_variables)] | ||
mod stubs { | ||
use super::*; | ||
|
||
pub unsafe extern "C" fn strlen(cs: *const c_char) -> size_t { | ||
4 | ||
} | ||
|
||
pub unsafe extern "C" fn sysconf(_input: c_int) -> c_longlong { | ||
10 | ||
} | ||
} | ||
|
||
fn dig_deeper(input: c_int) { | ||
unsafe { | ||
type FunctionPointerType = unsafe extern "C" fn(c_int) -> c_longlong; | ||
let ptr: FunctionPointerType = libc::sysconf; | ||
assert_eq!(ptr(input) as usize, 10); | ||
} | ||
} | ||
|
||
fn deeper_call() { | ||
dig_deeper(libc::_SC_PAGESIZE) | ||
} | ||
|
||
fn function_pointer_call(function_pointer: unsafe extern "C" fn(c_int) -> c_longlong) { | ||
assert_eq!(unsafe { function_pointer(libc::_SC_PAGESIZE) } as usize, 10); | ||
} | ||
|
||
#[kani::proof] | ||
#[kani::stub(libc::strlen, stubs::strlen)] | ||
fn standard() { | ||
let str: Box<i8> = Box::new(4); | ||
let str_ptr: *const i8 = &*str; | ||
assert_eq!(unsafe { libc::strlen(str_ptr) }, 4); | ||
} | ||
|
||
#[kani::proof] | ||
#[kani::stub(libc::strlen, stubs::strlen)] | ||
fn function_pointer_standard() { | ||
let str: Box<i8> = Box::new(4); | ||
let str_ptr: *const i8 = &*str; | ||
let new_ptr = libc::strlen; | ||
assert_eq!(unsafe { new_ptr(str_ptr) }, 4); | ||
} | ||
|
||
#[kani::proof] | ||
#[kani::stub(libc::sysconf, stubs::sysconf)] | ||
fn function_pointer_with_layers() { | ||
deeper_call(); | ||
} | ||
|
||
#[kani::proof] | ||
#[kani::stub(libc::sysconf, stubs::sysconf)] | ||
fn function_pointer_as_parameter() { | ||
type FunctionPointerType = unsafe extern "C" fn(c_int) -> c_longlong; | ||
let function_pointer: FunctionPointerType = libc::sysconf; | ||
function_pointer_call(function_pointer); | ||
function_pointer_call(libc::sysconf); | ||
} |