Skip to content

Commit

Permalink
smt: ensure that updates are properly overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Nov 20, 2023
1 parent 97364cd commit 94b2be7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/mc/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fn parse_smt_store(
let mut inner = parse_smt_array(smt_ctx, array)?;
let (index_val, _) = parse_smt_bit_vec(smt_ctx, index)?;
let (data_val, _) = parse_smt_bit_vec(smt_ctx, value)?;
inner.updates.push((index_val, data_val));
inner.add_update(index_val, data_val);
Some(inner)
}

Expand Down Expand Up @@ -708,11 +708,13 @@ mod tests {
let default = ctx.binary(data_width, default_value);

// check the base expression
// ((as const (Array (_ BitVec 5) (_ BitVec 32))) #b00000000000000000000000000110011)
let base = ctx.const_array(tpe, default);
let base_val = parse_smt_array(&ctx, base).unwrap();
assert_eq!(base_val.default, Some(BigUint::from(default_value)));

// store
// (store <base> #b01110 #x00000000)
let store_index: usize = 14;
let store_data: usize = 0;
let store = ctx.store(
Expand All @@ -728,8 +730,9 @@ mod tests {
);

// two stores
// (store <store> #b01110 #x00000011)
let store2_index: usize = 14;
let store2_data: usize = 0;
let store2_data: usize = 3;
let store2 = ctx.store(
store,
ctx.binary(index_width, store2_index),
Expand All @@ -740,7 +743,7 @@ mod tests {
assert_eq!(
store2_val.updates,
vec![
(BigUint::from(store_index), BigUint::from(store_data)),
// should be overwritten
(BigUint::from(store2_index), BigUint::from(store2_data))
]
);
Expand Down
9 changes: 9 additions & 0 deletions src/mc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ pub struct WitnessArray {
pub updates: Vec<(BigUint, BigUint)>,
}

impl WitnessArray {
pub fn add_update(&mut self, index: BigUint, value: BigUint) {
// delete any previous update that mapped to the same index
self.updates.retain(|(i, _)| !i.eq(&index));
// add new update
self.updates.push((index, value));
}
}

pub fn parse_big_uint_from_bit_string(value: &str) -> (BigUint, WidthInt) {
let int_val = BigUint::from_str_radix(value, 2).unwrap();
(int_val, value.len() as WidthInt)
Expand Down

0 comments on commit 94b2be7

Please sign in to comment.