Skip to content

Commit ab3cb60

Browse files
author
Sonny Martin
committed
Adjustments for new location of program checks
1 parent ca11166 commit ab3cb60

17 files changed

+259
-202
lines changed

src/cbmc/cbmc_parse_options.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,10 @@ int cbmc_parse_optionst::doit()
553553

554554
if(cmdline.isset("validate-goto-model"))
555555
{
556-
goto_model_validation_optionst goto_model_validation_options;
557-
goto_model_validation_options.function_pointer_calls_removed = true;
558-
goto_model_validation_options.check_returns_removed = true;
559-
goto_model_validation_options.check_called_functions = true;
560-
goto_model_validation_options.check_last_instruction = true;
556+
goto_model_validation_optionst goto_model_validation_options{true};
557+
// this option is temporarily disabled until all source locations
558+
// are reliably set correctly
559+
goto_model_validation_options.check_source_location = false;
561560
goto_model.validate(
562561
validation_modet::INVARIANT, goto_model_validation_options);
563562
}

src/goto-programs/abstract_goto_model.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ class abstract_goto_modelt
5454
///
5555
/// The validation mode indicates whether well-formedness check failures are
5656
/// reported via DATA_INVARIANT violations or exceptions.
57-
virtual void validate(const validation_modet vm) const = 0;
57+
// virtual void validate(const validation_modet vm) const = 0;
58+
virtual void validate(
59+
const validation_modet vm,
60+
const goto_model_validation_optionst &goto_model_validation_options) const = 0;
5861
};
5962

6063
#endif

src/goto-programs/goto_function.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,21 @@ void get_local_identifiers(
3333
///
3434
/// The validation mode indicates whether well-formedness check failures are
3535
/// reported via DATA_INVARIANT violations or exceptions.
36-
void goto_functiont::validate(const namespacet &ns, const validation_modet vm)
37-
const
36+
void goto_functiont::validate(
37+
const namespacet &ns,
38+
const validation_modet vm,
39+
const goto_model_validation_optionst &goto_model_validation_options) const
3840
{
39-
body.validate(ns, vm);
41+
// function body must end with an END_FUNCTION instruction
42+
if(body_available())
43+
{
44+
DATA_CHECK(
45+
vm,
46+
body.instructions.back().is_end_function(),
47+
"last instruction should be of end function type");
48+
}
49+
50+
body.validate(ns, vm, goto_model_validation_options);
4051

4152
find_symbols_sett typetags;
4253
find_type_symbols(type, typetags);
@@ -61,3 +72,4 @@ void goto_functiont::validate(const namespacet &ns, const validation_modet vm)
6172

6273
validate_full_type(type, ns, vm);
6374
}
75+

src/goto-programs/goto_function.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Date: May 2018
2121
#include <util/std_types.h>
2222

2323
#include "goto_program.h"
24+
#include "validate_goto_model.h"
2425

2526
/// A goto function, consisting of function type (see #type), function body (see
2627
/// #body), and parameter identifiers (see #parameter_identifiers).
@@ -119,7 +120,10 @@ class goto_functiont
119120
///
120121
/// The validation mode indicates whether well-formedness check failures are
121122
/// reported via DATA_INVARIANT violations or exceptions.
122-
void validate(const namespacet &ns, const validation_modet vm) const;
123+
void validate(
124+
const namespacet &ns,
125+
const validation_modet vm,
126+
const goto_model_validation_optionst &goto_model_validation_options) const;
123127
};
124128

125129
void get_local_identifiers(const goto_functiont &, std::set<irep_idt> &dest);

src/goto-programs/goto_functions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class goto_functionst
153153
++it;
154154
}
155155

156-
goto_function.validate(ns, vm);
156+
goto_function.validate(ns, vm, goto_model_validation_options);
157157
}
158158
}
159159
};

src/goto-programs/goto_model.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ class wrapper_goto_modelt : public abstract_goto_modelt
144144
///
145145
/// The validation mode indicates whether well-formedness check failures are
146146
/// reported via DATA_INVARIANT violations or exceptions.
147-
void validate(const validation_modet vm) const override
147+
void validate(
148+
const validation_modet vm,
149+
const goto_model_validation_optionst &goto_model_validation_options) const override
148150
{
149151
symbol_table.validate(vm);
150152

151153
const namespacet ns(symbol_table);
152-
goto_functions.validate(ns, vm);
154+
goto_functions.validate(ns, vm, goto_model_validation_options);
153155
}
154156

155157
private:

src/goto-programs/goto_program.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ bool goto_programt::instructiont::equals(const instructiont &other) const
674674

675675
void goto_programt::instructiont::validate(
676676
const namespacet &ns,
677-
const validation_modet vm) const
677+
const validation_modet vm,
678+
const goto_model_validation_optionst &goto_model_validation_options) const
678679
{
679680
validate_full_code(code, ns, vm);
680681
validate_full_expr(guard, ns, vm);
@@ -776,6 +777,25 @@ void goto_programt::instructiont::validate(
776777
}
777778
};
778779

780+
if(goto_model_validation_options.check_source_location)
781+
{
782+
DATA_CHECK(
783+
vm,
784+
source_location.is_not_nil(),
785+
"each instruction source location must not be nil");
786+
787+
DATA_CHECK(
788+
vm,
789+
code.source_location().is_not_nil(),
790+
"each instruction \"code\" field, must have non nil source location");
791+
792+
DATA_CHECK(
793+
vm,
794+
source_location == code.source_location(),
795+
"instruction source location and"
796+
" instruction \"code\" field source location must be the same");
797+
}
798+
779799
const symbolt *table_symbol;
780800
switch(type)
781801
{

src/goto-programs/goto_program.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <sstream>
1919
#include <string>
2020

21+
#include "validate_goto_model.h"
2122
#include <util/invariant.h>
2223
#include <util/namespace.h>
2324
#include <util/source_location.h>
@@ -554,7 +555,11 @@ class goto_programt
554555
///
555556
/// The validation mode indicates whether well-formedness check failures are
556557
/// reported via DATA_INVARIANT violations or exceptions.
557-
void validate(const namespacet &ns, const validation_modet vm) const;
558+
void validate(
559+
const namespacet &ns,
560+
const validation_modet vm,
561+
const goto_model_validation_optionst &goto_model_validation_options)
562+
const;
558563

559564
/// Apply given transformer to all expressions; no return value
560565
/// means no change needed.
@@ -814,11 +819,14 @@ class goto_programt
814819
///
815820
/// The validation mode indicates whether well-formedness check failures are
816821
/// reported via DATA_INVARIANT violations or exceptions.
817-
void validate(const namespacet &ns, const validation_modet vm) const
822+
void validate(
823+
const namespacet &ns,
824+
const validation_modet vm,
825+
const goto_model_validation_optionst &goto_model_validation_options) const
818826
{
819827
for(const instructiont &ins : instructions)
820828
{
821-
ins.validate(ns, vm);
829+
ins.validate(ns, vm, goto_model_validation_options);
822830
}
823831
}
824832

src/goto-programs/lazy_goto_model.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,11 @@ class lazy_goto_modelt : public abstract_goto_modelt
247247
///
248248
/// The validation mode indicates whether well-formedness check failures are
249249
/// reported via DATA_INVARIANT violations or exceptions.
250-
void validate(const validation_modet vm) const override
250+
void validate(
251+
const validation_modet vm,
252+
const goto_model_validation_optionst &goto_model_validation_options) const override
251253
{
252-
goto_model->validate(vm);
254+
goto_model->validate(vm, goto_model_validation_options);
253255
}
254256

255257
private:

src/goto-programs/validate_goto_model.cpp

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,8 @@ class validate_goto_modelt
5959
/// a corresponding entry exists in the function_map
6060
void check_called_functions();
6161

62-
/// Check that goto-programs that have a body end with an END_FUNCTION
63-
/// instruction.
64-
///
65-
/// NB functions that are only declared, and not used will not appear in the
66-
/// function map. Functions that are declared only and used to e.g.
67-
/// initialise a function pointer will have no body.
68-
void check_last_instruction();
69-
70-
/// Check both
71-
/// 1. every instruction \"code\" field, has a non nil source location"
72-
/// 2. every instruction source_location field is nil
73-
/// NB this check is not expected to pass until setting these locations is
74-
/// patched up.
75-
void check_sourcecode_location();
76-
7762
const validation_modet vm;
7863
const function_mapt &function_map;
79-
const goto_model_validation_optionst validation_options;
8064
};
8165
} // namespace
8266

@@ -86,9 +70,6 @@ void validate_goto_modelt::do_goto_program_checks(
8670
if(validation_options.entry_point_exists)
8771
entry_point_exists();
8872

89-
if(validation_options.check_last_instruction)
90-
check_last_instruction();
91-
9273
/// NB function pointer calls must have been removed before removing
9374
/// returns - so 'check_returns_removed' also enables
9475
// 'function_pointer_calls_removed'
@@ -102,9 +83,6 @@ void validate_goto_modelt::do_goto_program_checks(
10283

10384
if(validation_options.check_called_functions)
10485
check_called_functions();
105-
106-
if(validation_options.check_sourcecode_location)
107-
check_sourcecode_location();
10886
}
10987

11088
void validate_goto_modelt::entry_point_exists()
@@ -158,7 +136,10 @@ void validate_goto_modelt::check_returns_removed()
158136
"function call return should be nil");
159137

160138
const auto &callee = to_code_type(function_call.function().type());
161-
DATA_CHECK(vm, callee.return_type().id() == ID_empty, "");
139+
DATA_CHECK(
140+
vm,
141+
callee.return_type().id() == ID_empty,
142+
"called function must have empty return type");
162143
}
163144
}
164145
}
@@ -222,41 +203,6 @@ void validate_goto_modelt::check_called_functions()
222203
}
223204
}
224205

225-
void validate_goto_modelt::check_last_instruction()
226-
{
227-
for(const auto &fun : function_map)
228-
{
229-
if(fun.second.body_available())
230-
{
231-
DATA_CHECK(
232-
vm,
233-
fun.second.body.instructions.back().is_end_function(),
234-
"last instruction should be of end function type");
235-
}
236-
}
237-
}
238-
239-
void validate_goto_modelt::check_sourcecode_location()
240-
{
241-
// Assumption is that if every instruction is checked - then there is no
242-
// need to check targets.
243-
for(const auto &fun : function_map)
244-
{
245-
for(auto &instr : fun.second.body.instructions)
246-
{
247-
DATA_CHECK(
248-
vm,
249-
instr.code.source_location().is_not_nil(),
250-
"each instruction \"code\" field, must have non nil source location");
251-
252-
DATA_CHECK(
253-
vm,
254-
instr.source_location.is_not_nil(),
255-
"each instruction source location must not be nil");
256-
}
257-
}
258-
}
259-
260206
void validate_goto_model(
261207
const goto_functionst &goto_functions,
262208
const validation_modet vm,

src/goto-programs/validate_goto_model.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ struct goto_model_validation_optionst
1818
bool function_pointer_calls_removed;
1919
bool check_returns_removed;
2020
bool check_called_functions;
21-
bool check_last_instruction;
22-
bool check_sourcecode_location;
21+
bool check_source_location;
2322

2423
goto_model_validation_optionst()
2524
: entry_point_exists{true},
2625
function_pointer_calls_removed{false},
2726
check_returns_removed{false},
2827
check_called_functions{false},
29-
check_last_instruction{false},
30-
check_sourcecode_location{false}
28+
check_source_location{false}
3129
{
3230
}
3331

@@ -36,8 +34,7 @@ struct goto_model_validation_optionst
3634
function_pointer_calls_removed{options_value},
3735
check_returns_removed{options_value},
3836
check_called_functions{options_value},
39-
check_last_instruction{options_value},
40-
check_sourcecode_location{options_value}
37+
check_source_location{options_value}
4138
{
4239
}
4340
};

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SRC += analyses/ai/ai.cpp \
2222
goto-programs/goto_program_declaration.cpp \
2323
goto-programs/goto_program_function_call.cpp \
2424
goto-programs/goto_program_goto_target.cpp \
25+
goto-programs/goto_program_instruction.cpp \
2526
goto-programs/goto_program_symbol_type_table_consistency.cpp \
2627
goto-programs/goto_program_table_consistency.cpp \
2728
goto-programs/goto_program_validate.cpp \

unit/goto-programs/goto_model_function_type_consistency.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ SCENARIO(
3535
goto_model.goto_functions.function_map[function_symbol.name].type =
3636
fun_type1;
3737

38-
goto_model_validation_optionst validation_options{false};
38+
goto_model_validation_optionst validation_options;
39+
// required as this test has no entry point, but calls the top-level
40+
// 'goto_model.validate()'
41+
validation_options.entry_point_exists = false;
3942

4043
WHEN("Symbol table has the right type")
4144
{

0 commit comments

Comments
 (0)