Skip to content

Commit

Permalink
Missing index check for null array.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jul 6, 2023
1 parent e78bf0f commit 9a1fdd9
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ static tree_t bounds_check_call_args(tree_t t)
static bool index_in_range(tree_t index, int64_t low, int64_t high,
int64_t *value)
{
if (low > high)
return true; // Null range allows any index

int64_t folded;
if (folded_int(index, &folded)) {
*value = folded;
Expand All @@ -195,7 +192,7 @@ static bool bounds_check_index(tree_t index, type_t type, range_kind_t kind,
const char *what, int64_t low, int64_t high)
{
int64_t folded;
if (!index_in_range(index, low, high, &folded)) {
if (!index_in_range(index, low, high, &folded) && low <= high) {
LOCAL_TEXT_BUF tb = tb_new();
tb_cat(tb, what);
tb_printf(tb, " index ");
Expand Down
13 changes: 2 additions & 11 deletions src/vcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -5537,11 +5537,6 @@ static void emit_bounds_check(vcode_op_t kind, vcode_reg_t reg,
vcode_reg_t dir, vcode_reg_t locus,
vcode_reg_t hint)
{
if (reg == left || reg == right) {
emit_comment("Elided trivial bounds check for r%d", reg);
return;
}

VCODE_FOR_EACH_MATCHING_OP(other, kind) {
if (other->args.items[0] == reg && other->args.items[1] == left
&& other->args.items[2] == right && other->args.items[3] == dir)
Expand All @@ -5555,18 +5550,14 @@ static void emit_bounds_check(vcode_op_t kind, vcode_reg_t reg,
const bool is_null = (dconst == RANGE_TO && lconst > rconst)
|| (dconst == RANGE_DOWNTO && rconst > lconst);

if (is_null) {
emit_comment("Elided bounds check for null range");
return;
}

vtype_t *bounds = vcode_type_data(vcode_reg_bounds(reg));

const bool ok_static =
(dconst == RANGE_TO
&& bounds->low >= lconst && bounds->high <= rconst)
|| (dconst == RANGE_DOWNTO
&& bounds->low >= rconst && bounds->high <= lconst);
&& bounds->low >= rconst && bounds->high <= lconst)
|| (!is_null && (reg == left || reg == right));

if (ok_static) {
emit_comment("Elided bounds check for r%d", reg);
Expand Down
15 changes: 15 additions & 0 deletions test/bounds/issue734.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
entity issue734 is
end entity ;

architecture arch of issue734 is
signal val : bit_vector(0 downto 1) ;
begin

tb : process
begin
val(0) <= '0' ; -- Error
val(1 downto 0) <= "00"; -- Error
wait ;
end process ;

end architecture ;
1 change: 1 addition & 0 deletions test/regress/gold/issue734.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1ns+0: index 0 outside of INTEGER range 0 downto 1
16 changes: 16 additions & 0 deletions test/regress/issue734.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
entity issue734 is
end entity ;

architecture arch of issue734 is
signal val : bit_vector(0 downto 1) ;
begin

tb : process
variable n : integer := 0;
begin
wait for 1 ns;
val(n) <= '0' ; -- Error
wait ;
end process ;

end architecture ;
3 changes: 2 additions & 1 deletion test/regress/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ view5 normal,2019
issue705 normal,2008
array17 normal,2008
stdenv5 normal,2019
assert7 fail,2008
assert7 fail,gold,2008
vhpi7 normal,2008,vhpi
vhpi8 normal,2008,vhpi
issue704 wave,no-collapse
Expand All @@ -814,3 +814,4 @@ protected10 normal,2019
reflect1 normal,2019
signal32 normal,2019
reflect2 normal,2019
issue734 fail,gold,2008
20 changes: 20 additions & 0 deletions test/test_bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,25 @@ START_TEST(test_issue617)
}
END_TEST

START_TEST(test_issue734)
{
input_from_file(TESTDIR "/bounds/issue734.vhd");

const error_t expect[] = {
{ 10, "array VAL index 0 outside of NATURAL range 0 downto 1" },
{ 11, "VAL slice left index 1 outside of NATURAL range 0 downto 1" },
{ -1, NULL }
};
expect_errors(expect);

tree_t a = parse_check_and_simplify(T_ENTITY, T_ARCH);
fail_unless(error_count() == 0);

bounds_check(a);
check_expected_errors();
}
END_TEST

Suite *get_bounds_tests(void)
{
Suite *s = suite_create("bounds");
Expand Down Expand Up @@ -642,6 +661,7 @@ Suite *get_bounds_tests(void)
tcase_add_test(tc_core, test_driver1);
tcase_add_test(tc_core, test_nullrange);
tcase_add_test(tc_core, test_issue617);
tcase_add_test(tc_core, test_issue734);
suite_add_tcase(s, tc_core);

return s;
Expand Down
2 changes: 2 additions & 0 deletions test/test_lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,8 @@ START_TEST(test_issue149)
{ VCODE_OP_DEBUG_LOCUS },
{ VCODE_OP_TRAP_SUB },
{ VCODE_OP_CONST, .value = 0 },
{ VCODE_OP_DEBUG_LOCUS },
{ VCODE_OP_RANGE_CHECK },
{ VCODE_OP_CONST, .value = -1 },
{ VCODE_OP_DEBUG_LOCUS },
{ VCODE_OP_DEBUG_LOCUS },
Expand Down

0 comments on commit 9a1fdd9

Please sign in to comment.