-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
saw-core: Fix bug involving incorrect maximum values in selectV
The `selectV` function had a `maxValue` argument that, in principle, was supposed to represent the largest possible value of the index argument (treated as a big-endian list of bits). In practice, however, this was always given the length of the vector being indexed into minus 1! This led to subtle issues where indexing vectors with seemingly out-of-bounds indices would succeed, since the index value would be clamped to the length of the vector minus 1, which is always in bounds. This patch fixes the bug. Moreover, it turns out that the `maxValue` argument is not actually needed to do `selectV`'s job, as its implementation always upholds the invariant that the bitmask that it computes never exceeds the largest possible value of the index argument. To make the code simpler, I have removed the `maxValue` argument entirely, and I have added some more comments in `selectV` in `saw-core`, as well as its cousins in `saw-core-sbv` and `saw-core-what4`, to explain why this works. Fixes #1807.
- Loading branch information
1 parent
acbf303
commit 761ee4f
Showing
8 changed files
with
124 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CC = clang | ||
CFLAGS = -g -emit-llvm -frecord-command-line -O0 | ||
|
||
all: test.bc | ||
|
||
test.bc: test.c | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f test.bc |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <stdint.h> | ||
|
||
uint8_t get_last(uint8_t* array, uint8_t size) { | ||
return array[size - 1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
m <- llvm_load_module "test.bc"; | ||
|
||
let ptr_to_fresh_readonly (name : String) (type : LLVMType) = do { | ||
x <- llvm_fresh_var name type; | ||
p <- llvm_alloc_readonly type; | ||
llvm_points_to p (llvm_term x); | ||
return (x, p); | ||
}; | ||
|
||
let get_spec size pos = do { | ||
// Generate a fresh array and pointer to that array | ||
(x, xp) <- ptr_to_fresh_readonly "x" (llvm_array size (llvm_int 8)); | ||
|
||
// Call the C function | ||
llvm_execute_func [xp, llvm_term {{`size : [8]}}]; | ||
|
||
// Use Cryptol to access the correct position | ||
let ret = {{x @ (`pos : [8])}}; | ||
// Return it using llvm_return | ||
llvm_return (llvm_term ret); | ||
}; | ||
|
||
last_100_ov <- llvm_verify m "get_last" [] true (get_spec 100 100) z3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
set -e | ||
|
||
! $SAW test.saw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters