Skip to content

Commit 2c9a5c3

Browse files
Petr BauchPetr Bauch
authored andcommitted
Well-formedness check for goto instructions
Check that targets are well-formed and consistent.
1 parent c742e85 commit 2c9a5c3

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/goto-programs/goto_program.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,17 @@ void goto_programt::instructiont::validate(
683683
case NO_INSTRUCTION_TYPE:
684684
break;
685685
case GOTO:
686+
DATA_CHECK_WITH_DIAGNOSTICS(
687+
vm,
688+
has_target(),
689+
"goto instruction expects at least one target",
690+
source_location);
691+
// get_target checks that targets.size()==1
692+
DATA_CHECK_WITH_DIAGNOSTICS(
693+
vm,
694+
get_target()->is_target() && get_target()->target_number != 0,
695+
"goto target has to be a target",
696+
source_location);
686697
break;
687698
case ASSUME:
688699
DATA_CHECK_WITH_DIAGNOSTICS(

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SRC += analyses/ai/ai.cpp \
1919
goto-programs/goto_program_dead.cpp \
2020
goto-programs/goto_program_declaration.cpp \
2121
goto-programs/goto_program_function_call.cpp \
22+
goto-programs/goto_program_goto_target.cpp \
2223
goto-programs/goto_trace_output.cpp \
2324
goto-symex/ssa_equation.cpp \
2425
interpreter/interpreter.cpp \
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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(ns, 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(ns, validation_modet::EXCEPTION);
58+
}
59+
catch(incorrect_goto_program_exceptiont &e)
60+
{
61+
caught = true;
62+
}
63+
REQUIRE(caught);
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)