Skip to content

Commit 319d859

Browse files
authored
Adds support for sysconf (rust-lang#2557)
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent 5bc61f5 commit 319d859

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

cprover_bindings/src/goto_program/builtin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub enum BuiltinFn {
6161
Sinf,
6262
Sqrt,
6363
Sqrtf,
64+
Sysconf,
6465
Trunc,
6566
Truncf,
6667
Unlink,
@@ -124,6 +125,7 @@ impl ToString for BuiltinFn {
124125
Sinf => "sinf",
125126
Sqrt => "sqrt",
126127
Sqrtf => "sqrtf",
128+
Sysconf => "sysconf",
127129
Trunc => "trunc",
128130
Truncf => "truncf",
129131
Unlink => "unlink",
@@ -188,6 +190,7 @@ impl BuiltinFn {
188190
Sinf => vec![Type::float()],
189191
Sqrt => vec![Type::double()],
190192
Sqrtf => vec![Type::float()],
193+
Sysconf => vec![Type::c_int()],
191194
Trunc => vec![Type::double()],
192195
Truncf => vec![Type::float()],
193196
Unlink => vec![Type::c_char().to_pointer()],
@@ -251,6 +254,7 @@ impl BuiltinFn {
251254
Sinf => Type::float(),
252255
Sqrt => Type::double(),
253256
Sqrtf => Type::float(),
257+
Sysconf => Type::c_long_int(),
254258
Trunc => Type::double(),
255259
Truncf => Type::float(),
256260
Unlink => Type::c_int(),
@@ -314,6 +318,7 @@ impl BuiltinFn {
314318
Sinf,
315319
Sqrt,
316320
Sqrtf,
321+
Sysconf,
317322
Trunc,
318323
Truncf,
319324
Unlink,

cprover_bindings/src/goto_program/typ.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum Type {
2727
Bool,
2828
/// `typ x : width`. e.g. `unsigned int x: 3`.
2929
CBitField { typ: Box<Type>, width: u64 },
30-
/// Machine dependent integers: `bool`, `char`, `int`, `size_t`, etc.
30+
/// Machine dependent integers: `bool`, `char`, `int`, `long int`, `size_t`, etc.
3131
CInteger(CIntType),
3232
/// `return_type x(parameters)`
3333
Code { parameters: Vec<Parameter>, return_type: Box<Type> },
@@ -83,6 +83,8 @@ pub enum CIntType {
8383
Char,
8484
/// `int`
8585
Int,
86+
/// `long int`
87+
LongInt,
8688
/// `size_t`
8789
SizeT,
8890
/// `ssize_t`
@@ -232,6 +234,7 @@ impl CIntType {
232234
CIntType::Bool => st.machine_model().bool_width,
233235
CIntType::Char => st.machine_model().char_width,
234236
CIntType::Int => st.machine_model().int_width,
237+
CIntType::LongInt => st.machine_model().long_int_width,
235238
CIntType::SizeT => st.machine_model().pointer_width,
236239
CIntType::SSizeT => st.machine_model().pointer_width,
237240
}
@@ -287,6 +290,7 @@ impl Type {
287290
CInteger(CIntType::Bool) => Some(mm.bool_width),
288291
CInteger(CIntType::Char) => Some(mm.char_width),
289292
CInteger(CIntType::Int) => Some(mm.int_width),
293+
CInteger(CIntType::LongInt) => Some(mm.long_int_width),
290294
Signedbv { width } | Unsignedbv { width } => Some(*width),
291295
_ => None,
292296
}
@@ -450,6 +454,14 @@ impl Type {
450454
}
451455
}
452456

457+
pub fn is_long_int(&self) -> bool {
458+
let concrete = self.unwrap_typedef();
459+
match concrete {
460+
Type::CInteger(CIntType::LongInt) => true,
461+
_ => false,
462+
}
463+
}
464+
453465
pub fn is_c_size_t(&self) -> bool {
454466
let concrete = self.unwrap_typedef();
455467
match concrete {
@@ -637,7 +649,10 @@ impl Type {
637649
pub fn is_signed(&self, mm: &MachineModel) -> bool {
638650
let concrete = self.unwrap_typedef();
639651
match concrete {
640-
CInteger(CIntType::Int) | CInteger(CIntType::SSizeT) | Signedbv { .. } => true,
652+
CInteger(CIntType::Int)
653+
| CInteger(CIntType::LongInt)
654+
| CInteger(CIntType::SSizeT)
655+
| Signedbv { .. } => true,
641656
CInteger(CIntType::Char) => !mm.char_is_unsigned,
642657
_ => false,
643658
}
@@ -963,6 +978,10 @@ impl Type {
963978
CInteger(CIntType::Int)
964979
}
965980

981+
pub fn c_long_int() -> Self {
982+
CInteger(CIntType::LongInt)
983+
}
984+
966985
pub fn c_size_t() -> Self {
967986
CInteger(CIntType::SizeT)
968987
}
@@ -1471,6 +1490,7 @@ mod type_tests {
14711490
assert_eq!(type_def.is_empty(), src_type.is_empty());
14721491
assert_eq!(type_def.is_double(), src_type.is_double());
14731492
assert_eq!(type_def.is_bool(), src_type.is_bool());
1493+
assert_eq!(type_def.is_long_int(), src_type.is_long_int());
14741494
assert_eq!(type_def.is_array(), src_type.is_array());
14751495
assert_eq!(type_def.is_array_like(), src_type.is_array_like());
14761496
assert_eq!(type_def.is_union(), src_type.is_union());

cprover_bindings/src/irep/to_irep.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@ impl ToIrep for Type {
589589
sub: vec![],
590590
named_sub: linear_map![(IrepId::Width, Irep::just_int_id(mm.int_width),)],
591591
},
592+
Type::CInteger(CIntType::LongInt) => Irep {
593+
id: IrepId::Signedbv,
594+
sub: vec![],
595+
named_sub: linear_map![(IrepId::Width, Irep::just_int_id(mm.long_int_width),)],
596+
},
592597
Type::CInteger(CIntType::SizeT) => Irep {
593598
id: IrepId::Unsignedbv,
594599
sub: vec![],

kani-compiler/src/codegen_cprover_gotoc/codegen/typ.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl<'tcx> GotocCtx<'tcx> {
157157
cbmc::goto_program::CIntType::Bool => "bool",
158158
cbmc::goto_program::CIntType::Char => "char",
159159
cbmc::goto_program::CIntType::Int => "int",
160+
cbmc::goto_program::CIntType::LongInt => "long int",
160161
cbmc::goto_program::CIntType::SizeT => "size_t",
161162
cbmc::goto_program::CIntType::SSizeT => "ssize_t",
162163
};

tests/kani/LibC/sysconf.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
//
4+
//! Check support for `sysconf`.
5+
6+
#![feature(rustc_private)]
7+
extern crate libc;
8+
9+
#[kani::proof]
10+
fn main() {
11+
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
12+
}

0 commit comments

Comments
 (0)