Skip to content

Commit 4a740c1

Browse files
Petr BauchPetr Bauch
Petr Bauch
authored and
Petr Bauch
committed
Well-formedness check for goto instructions
Check that targets are well-formed and consistent.
1 parent 495c0d8 commit 4a740c1

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/goto-programs/goto_program.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,23 @@ void goto_programt::instructiont::validate(
748748
"asserting non-boolean condition\n" + guard.pretty(),
749749
source_location);
750750
break;
751+
case GOTO:
752+
DATA_CHECK_WITH_DIAGNOSTICS(
753+
has_target(),
754+
"goto instruction expects at least one target",
755+
source_location);
756+
for(const auto &t : targets)
757+
{
758+
DATA_CHECK_WITH_DIAGNOSTICS(
759+
t->is_target() && t->target_number != 0,
760+
"goto target has to be a target",
761+
source_location);
762+
}
763+
DATA_CHECK_WITH_DIAGNOSTICS(
764+
evaluates_to_boolean(guard),
765+
"goto with non-boolean condition\n" + guard.pretty(),
766+
source_location);
767+
break;
751768
case FUNCTION_CALL:
752769
DATA_CHECK_WITH_DIAGNOSTICS(
753770
code.get_statement() == ID_function_call,

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SRC += analyses/ai/ai.cpp \
1616
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
1717
goto-programs/goto_trace_output.cpp \
1818
goto-programs/goto_program_assume.cpp \
19+
goto-programs/goto_program_goto_target.cpp \
1920
goto-programs/goto_program_function_call.cpp \
2021
goto-programs/goto_program_declaration.cpp \
2122
interpreter/interpreter.cpp \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for goto_program::validate
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <goto-programs/goto_function.h>
10+
#include <testing-utils/catch.hpp>
11+
#include <util/arith_tools.h>
12+
13+
SCENARIO(
14+
"Validation of well-formed goto codes",
15+
"[core][goto-programs][validate]")
16+
{
17+
GIVEN("A program with one assertion")
18+
{
19+
symbol_tablet symbol_table;
20+
const typet type1 = signedbv_typet(32);
21+
symbolt symbol;
22+
irep_idt symbol_name = "a";
23+
symbol.name = symbol_name;
24+
symbol_exprt varx(symbol_name, type1);
25+
exprt val10 = from_integer(10, type1);
26+
binary_relation_exprt x_le_10(varx, ID_le, val10);
27+
28+
goto_functiont goto_function;
29+
auto &instructions = goto_function.body.instructions;
30+
instructions.emplace_back(goto_program_instruction_typet::ASSERT);
31+
instructions.back().make_assertion(x_le_10);
32+
33+
instructions.emplace_back(goto_program_instruction_typet::GOTO);
34+
instructions.back().make_goto(instructions.begin());
35+
36+
symbol.type = type1;
37+
symbol_table.insert(symbol);
38+
namespacet ns(symbol_table);
39+
40+
WHEN("Target is a target")
41+
{
42+
instructions.front().target_number = 1;
43+
THEN("The consistency check succeeds")
44+
{
45+
goto_function.body.validate(symbol_table, validation_modet::INVARIANT);
46+
REQUIRE(true);
47+
}
48+
}
49+
50+
WHEN("Target is not a target")
51+
{
52+
THEN("The consistency check fails")
53+
{
54+
bool caught = false;
55+
try
56+
{
57+
goto_function.body.validate(
58+
symbol_table, validation_modet::EXCEPTION);
59+
}
60+
catch(incorrect_goto_program_exceptiont &e)
61+
{
62+
caught = true;
63+
}
64+
REQUIRE(caught);
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)