Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off-by-one error in What4 implementation of (@) #1361

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# next

## Bug fixes

* Fix a bug in the What4 backend that could cause applications of `(@)` with
symbolic `Integer` indices to become out of bounds (#1359).

# 2.13.0

## Language changes
Expand Down
17 changes: 7 additions & 10 deletions src/Cryptol/Eval/What4.hs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ indexFront_int ::
TValue ->
SInteger (What4 sym) ->
SEval (What4 sym) (Value sym)
indexFront_int sym mblen _a xs ix idx
indexFront_int sym mblen _a xs _ix idx
| Just i <- W4.asInteger idx
= lookupSeqMap xs i

Expand Down Expand Up @@ -535,16 +535,13 @@ indexFront_int sym mblen _a xs ix idx
_ -> Nothing
)

-- Maximum possible in-bounds index given `Z m`
-- type information and the length
-- of the sequence. If the sequences is infinite and the
-- integer is unbounded, there isn't much we can do.
-- Maximum possible in-bounds index given the length
-- of the sequence. If the sequence is infinite, there
-- isn't much we can do.
maxIdx =
case (mblen, ix) of
(Nat n, TVIntMod m) -> Just (min (toInteger n) (toInteger m))
(Nat n, _) -> Just n
(_ , TVIntMod m) -> Just m
_ -> Nothing
case mblen of
Nat n -> Just (n - 1)
Inf -> Nothing
indexFront_segs ::
W4.IsSymExprBuilder sym =>
What4 sym ->
Expand Down
7 changes: 7 additions & 0 deletions tests/issues/issue1359.icry
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:set prover=sbv-z3
:safe \a -> sortBy (\c1 c2 -> c2 != ' ') (split`{8} a)
:safe \(a : [64]) (i : Integer) -> (split`{8} a)@(max 0 (min i 7))

:set prover=w4-z3
:safe \a -> sortBy (\c1 c2 -> c2 != ' ') (split`{8} a)
:safe \(a : [64]) (i : Integer) -> (split`{8} a)@(max 0 (min i 7))
5 changes: 5 additions & 0 deletions tests/issues/issue1359.icry.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Loading module Cryptol
Safe
Safe
Safe
Safe