Skip to content

Commit 907b9cc

Browse files
Petr BauchPetr Bauch
Petr Bauch
authored and
Petr Bauch
committed
Function type consistency between goto programs and symbol table
A simple iteration over goto-functions to check that it's base type is the same as the one stored in the symbol table. Includes a unit test.
1 parent 97d028f commit 907b9cc

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/goto-programs/goto_functions.h

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ class goto_functionst
127127
for(const auto &entry : function_map)
128128
{
129129
const goto_functiont &goto_function = entry.second;
130+
const auto &function_name = entry.first;
131+
132+
DATA_CHECK(
133+
goto_function.type == ns.lookup(function_name).type,
134+
id2string(function_name) + " type inconsistency\ngoto program type: " +
135+
goto_function.type.id_string() +
136+
"\nsymbol table type: " + ns.lookup(function_name).type.id_string());
137+
130138
goto_function.validate(ns, vm);
131139
}
132140
}

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SRC += analyses/ai/ai.cpp \
1515
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
1616
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
1717
compound_block_locations.cpp \
18+
goto-programs/goto_model_function_type_consistency.cpp \
1819
goto-programs/goto_program_assume.cpp \
1920
goto-programs/goto_program_dead.cpp \
2021
goto-programs/goto_program_declaration.cpp \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for goto_model::validate
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <goto-programs/goto_model.h>
10+
#include <testing-utils/catch.hpp>
11+
#include <util/arith_tools.h>
12+
13+
SCENARIO(
14+
"Validation of consistent program/table pair (function type)",
15+
"[core][goto-programs][validate]")
16+
{
17+
GIVEN("A model with one function")
18+
{
19+
const typet type1 = signedbv_typet(32);
20+
const typet type2 = signedbv_typet(64);
21+
code_typet fun_type1({}, type1);
22+
code_typet fun_type2({}, type2);
23+
24+
symbolt function_symbol;
25+
irep_idt function_symbol_name = "foo";
26+
function_symbol.name = function_symbol_name;
27+
28+
goto_modelt goto_model;
29+
goto_model.goto_functions.function_map[function_symbol.name] =
30+
goto_functiont();
31+
goto_model.goto_functions.function_map[function_symbol.name].type =
32+
fun_type1;
33+
34+
WHEN("Symbol table has the right type")
35+
{
36+
function_symbol.type = fun_type1;
37+
goto_model.symbol_table.insert(function_symbol);
38+
39+
THEN("The consistency check succeeds")
40+
{
41+
goto_model.validate(validation_modet::INVARIANT);
42+
43+
REQUIRE(true);
44+
}
45+
}
46+
47+
WHEN("Symbol table has the wrong type")
48+
{
49+
function_symbol.type = fun_type2;
50+
goto_model.symbol_table.insert(function_symbol);
51+
52+
THEN("The consistency check fails")
53+
{
54+
bool caught = false;
55+
try
56+
{
57+
goto_model.validate(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)