Skip to content

Commit

Permalink
fix(BV): Do not lose explanations in bvmul (OCamlPro#1170)
Browse files Browse the repository at this point in the history
* fix(BV): Do not lose explanations in bvmul

The implementation of bvmul from OCamlPro#1144 introduced a soundness bug: when
we do not know anything about the result, the explanation is dropped.

This is because the implementation was performing mixing bitlist
computation and creation of raw bitlist values. This patch fixes the
implementation by performing all computations in [Z] and only adding the
explanation at the end.

* Add a test
  • Loading branch information
bclement-ocp authored and Halbaroth committed Jul 24, 2024
1 parent 9e63382 commit 5d39cfd
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/lib/reasoners/bitlist.ml
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,19 @@ let mul a b =
let low_a_known = Z.trailing_zeros @@ a.bits_unk in
let low_b_known = Z.trailing_zeros @@ b.bits_unk in
let low_known = min low_a_known low_b_known in
let mid_bits = exact Z.(value a * value b) ex in
let mid_bits = Z.(value a * value b) in
let mid_bits =
if low_known = max_int then mid_bits
else extract mid_bits 0 low_known
else if low_known = 0 then Z.zero
else Z.extract mid_bits 0 low_known
in
if low_known = max_int then
mid_bits lsl zeroes
exact Z.(mid_bits lsl zeroes) ex
else
let high_bits =
{ bits_set = Z.zero
; bits_unk = Z.minus_one
; ex = Ex.empty }
in
((high_bits lsl low_known) lor mid_bits) lsl zeroes
(* High bits are unknown *)
{ bits_set = Z.(mid_bits lsl zeroes)
; bits_unk = Z.(minus_one lsl Stdlib.(low_known + zeroes))
; ex }

let bvshl ~size:sz a b =
(* If the minimum value for [b] is larger than the bitwidth, the result is
Expand Down
249 changes: 249 additions & 0 deletions tests/dune.inc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/issues/1170.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

unknown
6 changes: 6 additions & 0 deletions tests/issues/1170.smt2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(set-logic QF_BV)
(declare-const A (_ BitVec 2))
(declare-const B (_ BitVec 1))
(declare-const C (_ BitVec 3))
(assert (= (bvmul (_ bv3 4) (concat A (concat #b0 B))) (concat C #b1)))
(check-sat)

0 comments on commit 5d39cfd

Please sign in to comment.