Skip to content

Commit 7145230

Browse files
Petr BauchPetr Bauch
authored andcommitted
Reworked based on the well-formedness checking skeleton of PR #3123
The condition to be checked is the same. Interface function name was changed to follow the above PR. Also the check is now optional via command line.
1 parent 7439930 commit 7145230

File tree

7 files changed

+35
-50
lines changed

7 files changed

+35
-50
lines changed

src/cbmc/cbmc_parse_options.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,6 @@ int cbmc_parse_optionst::doit()
515515
if(get_goto_program_ret!=-1)
516516
return get_goto_program_ret;
517517

518-
INVARIANT(
519-
!goto_model.check_internal_invariants(*this),
520-
"goto program internal invariants failed");
521-
522518
if(cmdline.isset("show-claims") || // will go away
523519
cmdline.isset("show-properties")) // use this one
524520
{
@@ -530,6 +526,12 @@ int cbmc_parse_optionst::doit()
530526
if(set_properties())
531527
return CPROVER_EXIT_SET_PROPERTIES_FAILED;
532528

529+
if(cmdline.isset("validate-goto-model"))
530+
{
531+
namespacet ns(goto_model.symbol_table);
532+
goto_model.validate(ns, validation_modet::INVARIANT);
533+
}
534+
533535
return bmct::do_language_agnostic_bmc(
534536
path_strategy_chooser, options, goto_model, ui_message_handler);
535537
}
@@ -976,6 +978,7 @@ void cbmc_parse_optionst::help()
976978
" --xml-ui use XML-formatted output\n"
977979
" --xml-interface bi-directional XML interface\n"
978980
" --json-ui use JSON-formatted output\n"
981+
" --validate-goto-model enables additional well-formedness checks on the goto program\n" // NOLINT(*)
979982
HELP_GOTO_TRACE
980983
HELP_FLUSH
981984
" --verbosity # verbosity level\n"

src/cbmc/cbmc_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class optionst;
7272
OPT_FLUSH \
7373
"(localize-faults)(localize-faults-method):" \
7474
OPT_GOTO_TRACE \
75+
"(validate-goto-model)" \
7576
"(claim):(show-claims)(floatbv)(all-claims)(all-properties)" // legacy, and will eventually disappear // NOLINT(whitespace/line_length)
7677
// clang-format on
7778

src/goto-programs/goto_function.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ class goto_functiont
111111
return *this;
112112
}
113113

114-
bool
115-
check_internal_invariants(const symbol_tablet &table, messaget &msg) const
114+
void validate(const symbol_tablet &table, const validation_modet &vm) const
116115
{
117-
return body.check_internal_invariants(table, msg);
116+
return body.validate(table, vm);
118117
}
119118
};
120119

src/goto-programs/goto_model.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,14 @@ class goto_modelt : public abstract_goto_modelt
9999

100100
/// Iterates over the functions inside the goto model and checks invariants
101101
/// in all of them. Prints out error message collected.
102-
/// \param msg message instance to collect errors
103-
/// \return true if any violation was found
104-
bool check_internal_invariants(messaget &msg) const
102+
/// \param ns namespace for the environment
103+
/// \param vm validation mode to be used for error reporting
104+
void validate(const namespacet &ns, const validation_modet &vm) const
105105
{
106-
bool found_violation = false;
107106
forall_goto_functions(it, goto_functions)
108107
{
109-
found_violation = found_violation ||
110-
it->second.check_internal_invariants(symbol_table, msg);
108+
it->second.validate(symbol_table, vm);
111109
}
112-
return found_violation;
113110
}
114111
};
115112

src/goto-programs/goto_program.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,25 +669,29 @@ bool goto_programt::instructiont::equals(const instructiont &other) const
669669
// clang-format on
670670
}
671671

672-
bool goto_programt::instructiont::check_internal_invariants(
672+
void goto_programt::instructiont::validate(
673673
const symbol_tablet &table,
674-
messaget &msg) const
674+
const validation_modet &vm) const
675675
{
676676
namespacet ns(table);
677-
bool found_violation = false;
678677
std::vector<std::vector<std::string>> type_collector;
678+
std::stringstream location_stream;
679+
location_stream << source_location;
680+
679681
auto type_finder = [&](const exprt &e) {
680682
if(e.id() == ID_symbol)
681683
{
682684
auto symbol_expr = to_symbol_expr(e);
683685
const auto &symbol_id = symbol_expr.get_identifier();
684-
if(
685-
table.has_symbol(symbol_id) &&
686-
!base_type_eq(symbol_expr.type(), table.lookup_ref(symbol_id).type, ns))
687-
type_collector.push_back(
688-
{id2string(symbol_id),
689-
symbol_expr.type().id_string(),
690-
table.lookup_ref(symbol_id).type.id_string()});
686+
687+
if(table.has_symbol(symbol_id))
688+
DATA_CHECK(
689+
base_type_eq(
690+
symbol_expr.type(), table.lookup_ref(symbol_id).type, ns),
691+
id2string(symbol_id) + " type inconsistency (" +
692+
location_stream.str() + ")\n" + "goto program type: " +
693+
symbol_expr.type().id_string() + "\n " + "symbol table type: " +
694+
table.lookup_ref(symbol_id).type.id_string());
691695
}
692696
};
693697

@@ -719,20 +723,6 @@ bool goto_programt::instructiont::check_internal_invariants(
719723
case INCOMPLETE_GOTO:
720724
break;
721725
}
722-
723-
if(!type_collector.empty())
724-
{
725-
for(const auto &type_triple : type_collector)
726-
{
727-
INVARIANT(type_triple.size() > 2, "should have 3 elements");
728-
msg.error() << type_triple[0] << " type inconsistency ("
729-
<< source_location << ")\n"
730-
<< "goto program type: " << type_triple[1] << "\n "
731-
<< "symbol table type: " << type_triple[2] << messaget::eom;
732-
}
733-
found_violation = true;
734-
}
735-
return found_violation;
736726
}
737727

738728
bool goto_programt::equals(const goto_programt &other) const
@@ -766,17 +756,14 @@ bool goto_programt::equals(const goto_programt &other) const
766756
return true;
767757
}
768758

769-
bool goto_programt::check_internal_invariants(
759+
void goto_programt::validate(
770760
const symbol_tablet &table,
771-
messaget &msg) const
761+
const validation_modet &vm) const
772762
{
773-
bool found_violation = false;
774763
forall_goto_program_instructions(it, (*this))
775764
{
776-
found_violation =
777-
found_violation || it->check_internal_invariants(table, msg);
765+
it->validate(table, vm);
778766
}
779-
return found_violation;
780767
}
781768

782769
/// Outputs a string representation of a `goto_program_instruction_typet`

src/goto-programs/goto_program.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,8 @@ class goto_programt
403403
/// Iterate over code and guard and collect inconsistencies with symbol
404404
/// table.
405405
/// \param table the symbol table
406-
/// \param msg container to store the error messages
407-
/// \return true if any violation was found
408-
bool
409-
check_internal_invariants(const symbol_tablet &table, messaget &msg) const;
406+
/// \param vm validation mode
407+
void validate(const symbol_tablet &table, const validation_modet &vm) const;
410408
};
411409

412410
// Never try to change this to vector-we mutate the list while iterating
@@ -687,8 +685,7 @@ class goto_programt
687685
/// and relative jumps have the same distance.
688686
bool equals(const goto_programt &other) const;
689687

690-
bool
691-
check_internal_invariants(const symbol_tablet &table, messaget &msg) const;
688+
void validate(const symbol_tablet &table, const validation_modet &vm) const;
692689
};
693690

694691
/// Get control-flow successors of a given instruction. The instruction is

src/util/expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#define CPROVER_UTIL_EXPR_H
1111

1212
#include "type.h"
13+
#include "validate.h"
1314

1415
#include <functional>
1516
#include <list>

0 commit comments

Comments
 (0)