Skip to content

Commit

Permalink
fix missing constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Aug 29, 2024
1 parent 735c296 commit b05c574
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ jobs:
submodules: 'true'
- name: Build
run: cargo build --verbose
- name: Run on Shift Register w/ Depth 16
run: cargo run -- hwmcc19/hwmcc19-single-benchmarks/btor2/bv/mann/data-integrity/unsafe/shift_register_top_w64_d16_e0.btor2
- name: Run on Shift Register w/ Depth 16 for 100k cycles
run: cargo run -- --max-cycles=100000 hwmcc19/hwmcc19-single-benchmarks/btor2/bv/mann/data-integrity/unsafe/shift_register_top_w64_d16_e0.btor2

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
patronus = { version = "0.18.3" }
patronus = { version = "0.18.5" }
clap = { version = "4.5.14", features = ["derive"] }
smallvec = { version = "1.13.2", features = ["union"] }
petgraph = { version = "0.6.5" , features = []}
Expand Down
4 changes: 2 additions & 2 deletions src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ pub fn analyze_constraints(
) -> Vec<ConstraintCluster> {
use petgraph::visit::NodeIndexable;
let graph = extract_constraint_graph(ctx, sys, init);
//println!("{:?}", petgraph::dot::Dot::new(&graph));

// extract connected components from graph
let groups = connected_components(&graph);
//println!("{:?}", groups);

// turn components into constraint clusters
let state_map = sys.state_map();
Expand Down Expand Up @@ -133,6 +131,8 @@ fn extract_constraint_graph(
// constraint creates a connection
out.add_edge(var_to_node[&leaf], var_to_node[other], expr_ref);
}
// we always need a self edge (in case there is only one leaf)
out.add_edge(var_to_node[&leaf], var_to_node[&leaf], expr_ref);
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::fmt::{Debug, Formatter};
struct Args {
#[arg(short, long)]
verbose: bool,
#[arg(long)]
max_cycles: Option<u64>,
#[arg(value_name = "BTOR2", index = 1)]
filename: String,
}
Expand All @@ -29,6 +31,7 @@ static RANDOM_OPTS: RandomOptions = RandomOptions {
small_k: 50,
large_k: 1_000,
large_k_prob: 0.01,
max_cycles: None,
};

fn main() {
Expand All @@ -45,7 +48,9 @@ fn main() {
simplify_expressions(&mut ctx, &mut sys);

// try random testing
match random_testing(ctx, sys, RANDOM_OPTS) {
let mut options = RANDOM_OPTS.clone();
options.max_cycles = args.max_cycles;
match random_testing(ctx, sys, options) {
ModelCheckResult::Unknown => {
// print nothing
}
Expand Down Expand Up @@ -110,6 +115,10 @@ impl Witness {
if sys.states().count() > 0 {
writeln!(out, "#0")?;
for (ii, (_, state)) in sys.states().enumerate() {
if state.init.is_some() {
// the state has a computed init value
continue;
}
let name = state
.symbol
.get_symbol_name(ctx)
Expand Down
27 changes: 27 additions & 0 deletions src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use patronus::mc::Simulator;
use patronus::sim::interpreter::{InitKind, Interpreter};
use rand::{Rng, SeedableRng};
use std::collections::HashSet;
use std::num::NonZeroU64;

#[derive(Debug, Copy, Clone)]
pub struct RandomOptions {
Expand All @@ -21,13 +22,17 @@ pub struct RandomOptions {
pub large_k: u64,
/// probability of sampling a large instead of a small k
pub large_k_prob: f64,
/// maximum number of cycles to execute
pub max_cycles: Option<u64>,
}

pub fn random_testing(
mut ctx: Context,
sys: TransitionSystem,
opts: RandomOptions,
) -> ModelCheckResult {
// println!("{}", sys.serialize_to_str(&ctx));

// collect constraints for input randomization
let constraints = analyze_constraints(&mut ctx, &sys, false);

Expand Down Expand Up @@ -65,6 +70,7 @@ pub fn random_testing(
let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(1);

// main loop
let mut cycle_count = 0;
loop {
let k_max = sample_k_max(&mut rng, &opts);

Expand Down Expand Up @@ -105,6 +111,13 @@ pub fn random_testing(

// advance the system
sim.step();
cycle_count += 1;
if let Some(max_cycles) = opts.max_cycles {
if max_cycles <= cycle_count {
println!("Exciting after executing {} cycles.", cycle_count);
return ModelCheckResult::Unknown;
}
}
}
}
ModelCheckResult::Unknown
Expand Down Expand Up @@ -153,6 +166,20 @@ fn record_witness(

// TODO: remove
sim.update();

// sanity check constraints
for cluster in constraints.iter() {
for expr in cluster.exprs() {
let is_ok = sim.get(*expr).unwrap().to_u64().unwrap() == 1;
debug_assert!(
is_ok,
"{k}: failed {} in {:?}",
ctx.get(*expr).serialize_to_str(ctx),
cluster
);
}
}

if k == k_bad {
// sanity check bad
let bads = check_for_bad_states(ctx, bad_states, sim);
Expand Down

0 comments on commit b05c574

Please sign in to comment.