-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathcall_cbmc.rs
More file actions
540 lines (475 loc) · 20.8 KB
/
call_cbmc.rs
File metadata and controls
540 lines (475 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
use anyhow::{Result, bail};
use kani_metadata::{CbmcSolver, HarnessMetadata};
use regex::Regex;
use rustc_demangle::demangle;
use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::ffi::OsString;
use std::fmt::Write;
use std::path::Path;
use std::process::Command;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use crate::args::{OutputFormat, VerificationArgs};
use crate::cbmc_output_parser::{
CheckStatus, Property, VerificationOutput, extract_results, process_cbmc_output,
};
use crate::cbmc_property_renderer::{format_coverage, format_result, kani_cbmc_output_filter};
use crate::coverage::cov_results::{CoverageCheck, CoverageResults};
use crate::coverage::cov_results::{CoverageRegion, CoverageTerm};
use crate::session::KaniSession;
/// We will use Cadical by default since it performed better than MiniSAT in our analysis.
/// Note: Kissat was marginally better, but it is an external solver which could be more unstable.
static DEFAULT_SOLVER: CbmcSolver = CbmcSolver::Cadical;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VerificationStatus {
Success,
Failure,
}
/// Represents failed properties in three different categories.
/// This simplifies the process to determine and format verification results.
#[derive(Clone, Copy, Debug)]
pub enum FailedProperties {
// No failures
None,
// One or more panic-related failures
PanicsOnly,
// One or more failures that aren't panic-related
Other,
}
/// Our (kani-driver) notions of CBMC results.
#[derive(Debug)]
pub struct VerificationResult {
/// Whether verification should be considered to have succeeded, or have failed.
pub status: VerificationStatus,
/// The compact representation for failed properties
pub failed_properties: FailedProperties,
/// The `Result` properties in detail or the exit_status of CBMC.
/// Note: CBMC process exit status is only potentially useful if `status` is `Failure`.
/// Kani will see CBMC report "failure" that's actually success (interpreting "failed"
/// checks like coverage as expected and desirable.)
pub results: Result<Vec<Property>, i32>,
/// The runtime duration of this CBMC invocation.
pub runtime: Duration,
/// Whether concrete playback generated a test
pub generated_concrete_test: bool,
/// The coverage results
pub coverage_results: Option<CoverageResults>,
}
impl KaniSession {
/// Verify a goto binary that's been prepared with goto-instrument
pub fn run_cbmc(&self, file: &Path, harness: &HarnessMetadata) -> Result<VerificationResult> {
let args: Vec<OsString> = self.cbmc_flags(file, harness)?;
// TODO get cbmc path from self
let mut cmd = Command::new("cbmc");
cmd.args(args);
let start_time = Instant::now();
let verification_results = if self.args.output_format == crate::args::OutputFormat::Old {
if self.run_terminal(cmd).is_err() {
VerificationResult::mock_failure()
} else {
VerificationResult::mock_success()
}
} else {
// Add extra argument to receive the output in JSON format.
// Done here because `--visualize` uses the XML format instead.
cmd.arg("--json-ui");
// Spawn the CBMC process and process its output below
let cbmc_process_opt = self.run_piped(cmd)?;
let cbmc_process = cbmc_process_opt.ok_or(anyhow::Error::msg("Failed to run cbmc"))?;
let output = process_cbmc_output(cbmc_process, |i| {
kani_cbmc_output_filter(
i,
self.args.extra_pointer_checks,
self.args.common_args.quiet,
&self.args.output_format,
)
})?;
VerificationResult::from(output, harness.attributes.should_panic, start_time)
};
Ok(verification_results)
}
/// used by call_cbmc_viewer, invokes different variants of CBMC.
// TODO: this could use some cleanup and refactoring.
pub fn call_cbmc(&self, args: Vec<OsString>, output: &Path) -> Result<()> {
// TODO get cbmc path from self
let mut cmd = Command::new("cbmc");
cmd.args(args);
let result = self.run_redirect(cmd, output)?;
if !result.success() {
bail!("cbmc exited with status {}", result);
}
// TODO: We 'bail' above, but then ignore it in 'call_cbmc_viewer' ...
Ok(())
}
/// "Internal," but also used by call_cbmc_viewer
pub fn cbmc_flags(
&self,
file: &Path,
harness_metadata: &HarnessMetadata,
) -> Result<Vec<OsString>> {
let mut args = self.cbmc_check_flags();
if let Some(object_bits) = self.args.cbmc_object_bits() {
args.push("--object-bits".into());
args.push(object_bits.to_string().into());
}
if let Some(unwind_value) = resolve_unwind_value(&self.args, harness_metadata) {
args.push("--unwind".into());
args.push(unwind_value.to_string().into());
}
self.handle_solver_args(&harness_metadata.attributes.solver, &mut args)?;
if self.args.run_sanity_checks {
args.push("--validate-goto-model".into());
args.push("--validate-ssa-equation".into());
}
if !self.args.visualize
&& self.args.concrete_playback.is_none()
&& !self.args.no_slice_formula
{
args.push("--slice-formula".into());
}
if self.args.concrete_playback.is_some() {
args.push("--trace".into());
}
args.extend(self.args.cbmc_args.iter().cloned());
args.push(file.to_owned().into_os_string());
// Make CBMC verbose by default to tell users about unwinding progress. This should be
// reviewed as CBMC's verbosity defaults evolve.
args.push("--verbosity".into());
args.push("9".into());
Ok(args)
}
/// Just the flags to CBMC that enable property checking of any sort.
pub fn cbmc_check_flags(&self) -> Vec<OsString> {
let mut args = Vec::new();
// We assume that malloc cannot fail, see https://github.com/model-checking/kani/issues/891
args.push("--no-malloc-may-fail".into());
// With PR #2630 we generate the appropriate checks directly rather than relying on CBMC's
// checks (which are for C semantics).
args.push("--no-undefined-shift-check".into());
// With PR #647 we use Rust's `-C overflow-checks=on` instead of:
// --unsigned-overflow-check
// --signed-overflow-check
// So these options are deliberately skipped to avoid erroneously re-checking operations.
args.push("--no-signed-overflow-check".into());
if !self.args.checks.memory_safety_on() {
args.push("--no-bounds-check".into());
args.push("--no-pointer-check".into());
}
if self.args.checks.overflow_on() {
args.push("--float-overflow-check".into());
args.push("--nan-check".into());
// TODO: Implement conversion checks as an optional check.
// They are a well defined operation in rust, but they may yield unexpected results to
// many users. https://github.com/model-checking/kani/issues/840
// We might want to create a transformation pass instead of enabling CBMC since Kani
// compiler sometimes rely on the bitwise conversion of signed <-> unsigned.
// args.push("--conversion-check".into());
} else {
args.push("--no-div-by-zero-check".into());
}
if !self.args.checks.unwinding_on() {
args.push("--no-unwinding-assertions".into());
} else {
args.push("--no-self-loops-to-assumptions".into());
}
if self.args.extra_pointer_checks {
// This was adding a lot of false positives with std dangling pointer. We should
// still catch any invalid dereference with --pointer-check. Thus, only enable them
// if the user explicitly request them.
args.push("--pointer-overflow-check".into());
} else {
args.push("--no-pointer-primitive-check".into());
}
args
}
pub fn handle_solver_args(
&self,
harness_solver: &Option<CbmcSolver>,
args: &mut Vec<OsString>,
) -> Result<()> {
let solver = if let Some(solver) = &self.args.solver {
// `--solver` option takes precedence over attributes
solver
} else if let Some(solver) = harness_solver {
solver
} else {
&DEFAULT_SOLVER
};
match solver {
CbmcSolver::Cadical => {
args.push("--sat-solver".into());
args.push("cadical".into());
}
CbmcSolver::Kissat => {
args.push("--external-sat-solver".into());
args.push("kissat".into());
}
CbmcSolver::Minisat => {
// Minisat is currently CBMC's default solver, so no need to
// pass any arguments
}
CbmcSolver::Binary(solver_binary) => {
// Check if the specified binary exists in path
if which::which(solver_binary).is_err() {
bail!("the specified solver \"{solver_binary}\" was not found in path")
}
args.push("--external-sat-solver".into());
args.push(solver_binary.into());
}
}
Ok(())
}
}
impl VerificationResult {
/// Computes a `VerificationResult` (kani-driver's notion of the result of a CBMC call) from a
/// `VerificationOutput` (cbmc_output_parser's idea of CBMC results).
///
/// NOTE: We actually ignore the CBMC exit status, in favor of two checks:
/// 1. Examining the actual results of CBMC properties.
/// (CBMC will regularly report "failure" but that's just our cover checks.)
/// 2. Positively checking for the presence of results.
/// (Do not mistake lack of results for success: report it as failure.)
fn from(
output: VerificationOutput,
should_panic: bool,
start_time: Instant,
) -> VerificationResult {
let runtime = start_time.elapsed();
let (_, results) = extract_results(output.processed_items);
if let Some(results) = results {
let (status, failed_properties) =
verification_outcome_from_properties(&results, should_panic);
let coverage_results = coverage_results_from_properties(&results);
VerificationResult {
status,
failed_properties,
results: Ok(results),
runtime,
generated_concrete_test: false,
coverage_results,
}
} else {
// We never got results from CBMC - something went wrong (e.g. crash) so it's failure
VerificationResult {
status: VerificationStatus::Failure,
failed_properties: FailedProperties::Other,
results: Err(output.process_status),
runtime,
generated_concrete_test: false,
coverage_results: None,
}
}
}
pub fn mock_success() -> VerificationResult {
VerificationResult {
status: VerificationStatus::Success,
failed_properties: FailedProperties::None,
results: Ok(vec![]),
runtime: Duration::from_secs(0),
generated_concrete_test: false,
coverage_results: None,
}
}
fn mock_failure() -> VerificationResult {
VerificationResult {
status: VerificationStatus::Failure,
failed_properties: FailedProperties::Other,
// on failure, exit codes in theory might be used,
// but `mock_failure` should never be used in a context where they will,
// so again use something weird:
results: Err(42),
runtime: Duration::from_secs(0),
generated_concrete_test: false,
coverage_results: None,
}
}
pub fn render(&self, output_format: &OutputFormat, should_panic: bool) -> String {
match &self.results {
Ok(results) => {
let status = self.status;
let failed_properties = self.failed_properties;
let show_checks = matches!(output_format, OutputFormat::Regular);
let mut result = if let Some(cov_results) = &self.coverage_results {
format_coverage(
results,
cov_results,
status,
should_panic,
failed_properties,
show_checks,
)
} else {
format_result(results, status, should_panic, failed_properties, show_checks)
};
writeln!(result, "Verification Time: {}s", self.runtime.as_secs_f32()).unwrap();
result
}
Err(exit_status) => {
let verification_result = console::style("FAILED").red();
let explanation = if *exit_status == 137 {
"CBMC appears to have run out of memory. You may want to rerun your proof in \
an environment with additional memory or use stubbing to reduce the size of the \
code the verifier reasons about.\n"
} else {
""
};
format!(
"\nCBMC failed with status {exit_status}\n\
VERIFICATION:- {verification_result}\n\
{explanation}",
)
}
}
}
/// Find the failed properties from this verification run
pub fn failed_properties(&self) -> Vec<&Property> {
if let Ok(properties) = &self.results {
properties.iter().filter(|prop| prop.status == CheckStatus::Failure).collect()
} else {
debug_assert!(false, "expected error to be handled before invoking this function");
vec![]
}
}
}
/// We decide if verification succeeded based on properties, not (typically) on exit code
fn verification_outcome_from_properties(
properties: &[Property],
should_panic: bool,
) -> (VerificationStatus, FailedProperties) {
let failed_properties = determine_failed_properties(properties);
let status = if should_panic {
match failed_properties {
FailedProperties::None | FailedProperties::Other => VerificationStatus::Failure,
FailedProperties::PanicsOnly => VerificationStatus::Success,
}
} else {
match failed_properties {
FailedProperties::None => VerificationStatus::Success,
FailedProperties::PanicsOnly | FailedProperties::Other => VerificationStatus::Failure,
}
};
(status, failed_properties)
}
/// Determines the `FailedProperties` variant that corresponds to an array of properties
fn determine_failed_properties(properties: &[Property]) -> FailedProperties {
let failed_properties: Vec<&Property> =
properties.iter().filter(|prop| prop.status == CheckStatus::Failure).collect();
// Return `FAILURE` if there isn't at least one failed property
if failed_properties.is_empty() {
FailedProperties::None
} else {
// Check if all failed properties correspond to the `assertion` class.
// Note: Panics caused by `panic!` and `assert!` fall into this class.
let all_failed_checks_are_panics =
failed_properties.iter().all(|prop| prop.property_class() == "assertion");
if all_failed_checks_are_panics {
FailedProperties::PanicsOnly
} else {
FailedProperties::Other
}
}
}
fn coverage_results_from_properties(properties: &[Property]) -> Option<CoverageResults> {
let cov_properties: Vec<&Property> =
properties.iter().filter(|p| p.is_code_coverage_property()).collect();
if cov_properties.is_empty() {
return None;
}
// Postprocessing the coverage results involves matching on the descriptions
// of code coverage properties with the `counter_re` regex. These are two
// real examples of such descriptions:
//
// ```
// CounterIncrement(0) $test_cov$ - src/main.rs:5:1 - 6:15
// ExpressionUsed(0) $test_cov$ - src/main.rs:6:19 - 6:28
// ```
//
// The span is further processed to extract the code region attributes.
// Ideally, we should have coverage mappings (i.e., the relation between
// counters and code regions) available in the coverage metadata:
// <https://github.com/model-checking/kani/issues/3445>. If that were the
// case, we would not need the spans in these descriptions.
let counter_re = {
static COUNTER_RE: OnceLock<Regex> = OnceLock::new();
COUNTER_RE.get_or_init(|| {
Regex::new(
r#"^(?<kind>CounterIncrement|ExpressionUsed)\((?<counter_num>[0-9]+)\) \$(?<func_name>[^\$]+)\$ - (?<span>.+)"#,
)
.unwrap()
})
};
let mut coverage_results: BTreeMap<String, Vec<CoverageCheck>> = BTreeMap::default();
for prop in cov_properties {
let mut prop_processed = false;
if let Some(captures) = counter_re.captures(&prop.description) {
let kind = &captures["kind"];
let counter_num = &captures["counter_num"];
let function = demangle(&captures["func_name"]).to_string();
let status = prop.status;
let span = captures["span"].to_string();
let counter_id = counter_num.parse().unwrap();
let term = match kind {
"CounterIncrement" => CoverageTerm::Counter(counter_id),
"ExpressionUsed" => CoverageTerm::Expression(counter_id),
_ => unreachable!("counter kind could not be recognized: {:?}", kind),
};
let region = CoverageRegion::from_str(span);
let cov_check = CoverageCheck::new(function, term, region, status);
let file = cov_check.region.file.clone();
if let Entry::Vacant(e) = coverage_results.entry(file.clone()) {
e.insert(vec![cov_check]);
} else {
coverage_results.entry(file).and_modify(|checks| checks.push(cov_check));
}
prop_processed = true;
}
assert!(prop_processed, "error: coverage property not processed\n{prop:?}");
}
Some(CoverageResults::new(coverage_results))
}
/// Solve Unwind Value from conflicting inputs of unwind values. (--default-unwind, annotation-unwind, --unwind)
pub fn resolve_unwind_value(
args: &VerificationArgs,
harness_metadata: &HarnessMetadata,
) -> Option<u32> {
// Check for which flag is being passed and prioritize extracting unwind from the
// respective flag/annotation.
args.unwind.or(harness_metadata.attributes.unwind_value).or(args.default_unwind)
}
#[cfg(test)]
mod tests {
use crate::args;
use crate::metadata::tests::mock_proof_harness;
use clap::Parser;
use super::*;
#[test]
fn check_resolve_unwind_value() {
// Command line unwind value for specific harnesses take precedence over default annotation value
let args_empty = ["kani", "x.rs"];
let args_only_default = ["kani", "x.rs", "--default-unwind", "2"];
let args_only_harness = ["kani", "x.rs", "--unwind", "1", "--harness", "check_one"];
let args_both =
["kani", "x.rs", "--default-unwind", "2", "--unwind", "1", "--harness", "check_one"];
let harness_none = mock_proof_harness("check_one", None, None, None);
let harness_some = mock_proof_harness("check_one", Some(3), None, None);
fn resolve(args: &[&str], harness: &HarnessMetadata) -> Option<u32> {
resolve_unwind_value(
&args::StandaloneArgs::try_parse_from(args).unwrap().verify_opts,
harness,
)
}
// test against no unwind annotation
assert_eq!(resolve(&args_empty, &harness_none), None);
assert_eq!(resolve(&args_only_default, &harness_none), Some(2));
assert_eq!(resolve(&args_only_harness, &harness_none), Some(1));
assert_eq!(resolve(&args_both, &harness_none), Some(1));
// test against unwind annotation
assert_eq!(resolve(&args_empty, &harness_some), Some(3));
assert_eq!(resolve(&args_only_default, &harness_some), Some(3));
assert_eq!(resolve(&args_only_harness, &harness_some), Some(1));
assert_eq!(resolve(&args_both, &harness_some), Some(1));
}
}