-
Notifications
You must be signed in to change notification settings - Fork 274
Function type consistency between goto programs and symbol table #3127
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -12,8 +12,10 @@ Author: Daniel Kroening, [email protected] | |
#ifndef CPROVER_GOTO_PROGRAMS_GOTO_MODEL_H | ||
#define CPROVER_GOTO_PROGRAMS_GOTO_MODEL_H | ||
|
||
#include <util/symbol_table.h> | ||
#include <util/base_type.h> | ||
#include <util/journalling_symbol_table.h> | ||
#include <util/message.h> | ||
#include <util/symbol_table.h> | ||
|
||
#include "abstract_goto_model.h" | ||
#include "goto_functions.h" | ||
|
@@ -95,6 +97,23 @@ class goto_modelt : public abstract_goto_modelt | |
return goto_functions.function_map.find(id) != | ||
goto_functions.function_map.end(); | ||
} | ||
|
||
/// Iterates over the functions inside the goto model and checks invariants | ||
/// in all of them. Prints out error message collected. | ||
/// \param ns namespace for the environment | ||
/// \param vm validation mode to be used for error reporting | ||
void validate(const namespacet &ns, const validation_modet &vm) const | ||
{ | ||
forall_goto_functions(it, goto_functions) | ||
{ | ||
DATA_CHECK( | ||
base_type_eq( | ||
it->second.type, symbol_table.lookup_ref(it->first).type, ns), | ||
id2string(it->first) + " type inconsistency\ngoto program type: " + | ||
it->second.type.id_string() + "\nsymbol table type: " + | ||
symbol_table.lookup_ref(it->first).type.id_string()); | ||
} | ||
} | ||
}; | ||
|
||
/// Class providing the abstract GOTO model interface onto an unrelated | ||
|
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected] | |
#define CPROVER_UTIL_EXPR_H | ||
|
||
#include "type.h" | ||
#include "validate.h" | ||
|
||
#include <functional> | ||
#include <list> | ||
|
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,61 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Goto program validation | ||
|
||
Author: Daniel Poetzl | ||
|
||
\*******************************************************************/ | ||
|
||
#ifndef CPROVER_UTIL_VALIDATE_H | ||
#define CPROVER_UTIL_VALIDATE_H | ||
|
||
#include <type_traits> | ||
|
||
#include "exception_utils.h" | ||
#include "invariant.h" | ||
#include "irep.h" | ||
|
||
enum class validation_modet | ||
{ | ||
INVARIANT, | ||
EXCEPTION | ||
}; | ||
|
||
#define GET_FIRST(A, ...) A | ||
|
||
/// This macro takes a condition which denotes a well-formedness criterion on | ||
/// goto programs, expressions, instructions, etc. Based on the value of the | ||
/// variable vm (the validation mode), it either uses DATA_INVARIANT() to check | ||
/// those conditions, or throws an exception when a condition does not hold. | ||
#define DATA_CHECK(condition, message) \ | ||
do \ | ||
{ \ | ||
switch(vm) \ | ||
{ \ | ||
case validation_modet::INVARIANT: \ | ||
DATA_INVARIANT(condition, message); \ | ||
break; \ | ||
case validation_modet::EXCEPTION: \ | ||
if(!(condition)) \ | ||
throw incorrect_goto_program_exceptiont(message); \ | ||
break; \ | ||
} \ | ||
} while(0) | ||
|
||
#define DATA_CHECK_WITH_DIAGNOSTICS(condition, message, ...) \ | ||
do \ | ||
{ \ | ||
switch(vm) \ | ||
{ \ | ||
case validation_modet::INVARIANT: \ | ||
DATA_INVARIANT_WITH_DIAGNOSTICS(condition, message, __VA_ARGS__); \ | ||
break; \ | ||
case validation_modet::EXCEPTION: \ | ||
if(!(condition)) \ | ||
throw incorrect_goto_program_exceptiont( \ | ||
message, GET_FIRST(__VA_ARGS__, dummy)); \ | ||
break; \ | ||
} \ | ||
} while(0) | ||
|
||
#endif /* CPROVER_UTIL_VALIDATE_H */ |
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
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
/*******************************************************************\ | ||
|
||
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); | ||
namespacet ns(goto_model.symbol_table); | ||
|
||
THEN("The consistency check succeeds") | ||
{ | ||
goto_model.validate(ns, validation_modet::INVARIANT); | ||
|
||
REQUIRE(true); | ||
} | ||
} | ||
|
||
WHEN("Symbol table has the wrong type") | ||
{ | ||
function_symbol.type = fun_type2; | ||
goto_model.symbol_table.insert(function_symbol); | ||
namespacet ns(goto_model.symbol_table); | ||
|
||
THEN("The consistency check fails") | ||
{ | ||
bool caught = false; | ||
try | ||
{ | ||
goto_model.validate(ns, validation_modet::EXCEPTION); | ||
} | ||
catch(incorrect_goto_program_exceptiont &e) | ||
{ | ||
caught = true; | ||
} | ||
REQUIRE(caught); | ||
} | ||
} | ||
} | ||
} |
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.
I'm unclear why we want this. Either something is an invariant or it isn't, I don't see the value in having a single construct for both cases.