-
Notifications
You must be signed in to change notification settings - Fork 273
Consistency between goto programs and symbol table #3118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tautschnig
merged 3 commits into
diffblue:develop
from
xbauch:program_table_identifier_consistency
Dec 5, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -14,6 +14,8 @@ Author: Daniel Kroening, [email protected] | |
#include <ostream> | ||
#include <iomanip> | ||
|
||
#include <util/expr_iterator.h> | ||
#include <util/find_symbols.h> | ||
#include <util/std_expr.h> | ||
#include <util/validate.h> | ||
|
||
|
@@ -677,6 +679,45 @@ void goto_programt::instructiont::validate( | |
validate_full_expr(guard, ns, vm); | ||
|
||
const symbolt *table_symbol; | ||
DATA_CHECK_WITH_DIAGNOSTICS( | ||
vm, | ||
!ns.lookup(function, table_symbol), | ||
id2string(function) + " not found", | ||
source_location); | ||
|
||
auto expr_symbol_finder = [&](const exprt &e) { | ||
find_symbols_sett typetags; | ||
find_type_symbols(e.type(), typetags); | ||
find_symbols(e, typetags); | ||
const symbolt *symbol; | ||
for(const auto &identifier : typetags) | ||
{ | ||
DATA_CHECK_WITH_DIAGNOSTICS( | ||
vm, | ||
!ns.lookup(identifier, symbol), | ||
id2string(identifier) + " not found", | ||
source_location); | ||
} | ||
}; | ||
|
||
auto ¤t_source_location = source_location; | ||
auto type_finder = | ||
[&ns, vm, &table_symbol, ¤t_source_location](const exprt &e) { | ||
if(e.id() == ID_symbol) | ||
{ | ||
const auto &goto_symbol_expr = to_symbol_expr(e); | ||
const auto &goto_id = goto_symbol_expr.get_identifier(); | ||
|
||
if(!ns.lookup(goto_id, table_symbol)) | ||
DATA_CHECK_WITH_DIAGNOSTICS( | ||
vm, | ||
base_type_eq(goto_symbol_expr.type(), table_symbol->type, ns), | ||
id2string(goto_id) + " type inconsistency\n" + | ||
"goto program type: " + goto_symbol_expr.type().id_string() + | ||
"\n" + "symbol table type: " + table_symbol->type.id_string(), | ||
current_source_location); | ||
} | ||
}; | ||
|
||
switch(type) | ||
{ | ||
|
@@ -708,6 +749,9 @@ void goto_programt::instructiont::validate( | |
targets.empty(), | ||
"assert instruction should not have a target", | ||
source_location); | ||
|
||
std::for_each(guard.depth_begin(), guard.depth_end(), expr_symbol_finder); | ||
std::for_each(guard.depth_begin(), guard.depth_end(), type_finder); | ||
break; | ||
case OTHER: | ||
break; | ||
|
@@ -772,6 +816,9 @@ void goto_programt::instructiont::validate( | |
code.get_statement() == ID_function_call, | ||
"function call instruction should contain a call statement", | ||
source_location); | ||
|
||
std::for_each(code.depth_begin(), code.depth_end(), expr_symbol_finder); | ||
std::for_each(code.depth_begin(), code.depth_end(), type_finder); | ||
break; | ||
case THROW: | ||
break; | ||
|
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
unit/goto-programs/goto_model_function_type_consistency.cpp
This file contains hidden or 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,67 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Unit tests for goto_model::validate | ||
|
||
Author: Diffblue Ltd. | ||
|
||
\*******************************************************************/ | ||
|
||
#include <goto-programs/goto_model.h> | ||
#include <testing-utils/catch.hpp> | ||
#include <util/arith_tools.h> | ||
|
||
SCENARIO( | ||
"Validation of consistent program/table pair (function type)", | ||
"[core][goto-programs][validate]") | ||
{ | ||
GIVEN("A model with one function") | ||
{ | ||
const typet type1 = signedbv_typet(32); | ||
const typet type2 = signedbv_typet(64); | ||
code_typet fun_type1({}, type1); | ||
code_typet fun_type2({}, type2); | ||
|
||
symbolt function_symbol; | ||
irep_idt function_symbol_name = "foo"; | ||
function_symbol.name = function_symbol_name; | ||
|
||
goto_modelt goto_model; | ||
goto_model.goto_functions.function_map[function_symbol.name] = | ||
goto_functiont(); | ||
goto_model.goto_functions.function_map[function_symbol.name].type = | ||
fun_type1; | ||
|
||
WHEN("Symbol table has the right type") | ||
{ | ||
function_symbol.type = fun_type1; | ||
goto_model.symbol_table.insert(function_symbol); | ||
|
||
THEN("The consistency check succeeds") | ||
{ | ||
goto_model.validate(validation_modet::INVARIANT); | ||
|
||
REQUIRE(true); | ||
} | ||
} | ||
|
||
WHEN("Symbol table has the wrong type") | ||
{ | ||
function_symbol.type = fun_type2; | ||
goto_model.symbol_table.insert(function_symbol); | ||
|
||
THEN("The consistency check fails") | ||
{ | ||
bool caught = false; | ||
try | ||
{ | ||
goto_model.validate(validation_modet::EXCEPTION); | ||
} | ||
catch(incorrect_goto_program_exceptiont &e) | ||
{ | ||
caught = true; | ||
} | ||
REQUIRE(caught); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
75 changes: 75 additions & 0 deletions
75
unit/goto-programs/goto_program_symbol_type_table_consistency.cpp
This file contains hidden or 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,75 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Unit tests for goto_program::validate | ||
|
||
Author: Diffblue Ltd. | ||
|
||
\*******************************************************************/ | ||
|
||
#include <goto-programs/goto_function.h> | ||
#include <testing-utils/catch.hpp> | ||
#include <util/arith_tools.h> | ||
|
||
SCENARIO( | ||
"Validation of consistent program/table pair (type-wise)", | ||
"[core][goto-programs][validate]") | ||
{ | ||
GIVEN("A program with one assertion") | ||
{ | ||
symbol_tablet symbol_table; | ||
const typet type1 = signedbv_typet(32); | ||
const typet type2 = signedbv_typet(64); | ||
symbolt symbol; | ||
irep_idt symbol_name = "a"; | ||
symbol.name = symbol_name; | ||
symbol_exprt varx(symbol_name, type1); | ||
symbolt function_symbol; | ||
irep_idt function_name = "fun"; | ||
function_symbol.name = function_name; | ||
|
||
exprt val10 = from_integer(10, type1); | ||
binary_relation_exprt x_le_10(varx, ID_le, val10); | ||
|
||
goto_functiont goto_function; | ||
auto &instructions = goto_function.body.instructions; | ||
instructions.emplace_back(goto_program_instruction_typet::ASSERT); | ||
instructions.back().make_assertion(x_le_10); | ||
instructions.back().function = function_symbol.name; | ||
|
||
symbol_table.insert(function_symbol); | ||
WHEN("Symbol table has the right symbol type") | ||
{ | ||
symbol.type = type1; | ||
symbol_table.insert(symbol); | ||
const namespacet ns(symbol_table); | ||
|
||
THEN("The consistency check succeeds") | ||
{ | ||
goto_function.validate(ns, validation_modet::INVARIANT); | ||
|
||
REQUIRE(true); | ||
} | ||
} | ||
|
||
WHEN("Symbol table has the wrong symbol type") | ||
{ | ||
symbol.type = type2; | ||
symbol_table.insert(symbol); | ||
const namespacet ns(symbol_table); | ||
|
||
THEN("The consistency check fails") | ||
{ | ||
bool caught = false; | ||
try | ||
{ | ||
goto_function.validate(ns, validation_modet::EXCEPTION); | ||
} | ||
catch(incorrect_goto_program_exceptiont &e) | ||
{ | ||
caught = true; | ||
} | ||
REQUIRE(caught); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I don't get the impression that this is doing much of a "find." Couldn't you actually use one of the
find_*
functions to find the identifiers, and then check all of those that you have found?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tautschnig Ok, but this last bit required a bit more code change so I pushed it as a new commit and will squash once approved.