cprover
cover.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Coverage Instrumentation
4 
5 Author: Daniel Kroening
6 
7 Date: May 2016
8 
9 \*******************************************************************/
10 
13 
14 #include "cover.h"
15 
16 #include <util/config.h>
17 #include <util/message.h>
18 #include <util/make_unique.h>
19 #include <util/cmdline.h>
20 #include <util/options.h>
21 
23 
24 #include "cover_basic_blocks.h"
25 
38  const irep_idt &function_id,
39  goto_programt &goto_program,
40  const cover_instrumenterst &instrumenters,
41  const irep_idt &mode,
42  message_handlert &message_handler,
44 {
45  const std::unique_ptr<cover_blocks_baset> basic_blocks =
46  mode == ID_java ? std::unique_ptr<cover_blocks_baset>(
47  new cover_basic_blocks_javat(goto_program))
48  : std::unique_ptr<cover_blocks_baset>(
49  new cover_basic_blockst(goto_program));
50 
51  basic_blocks->report_block_anomalies(
52  function_id, goto_program, message_handler);
53  instrumenters(function_id, goto_program, *basic_blocks, make_assertion);
54 }
55 
61  coverage_criteriont criterion,
63  const goal_filterst &goal_filters)
64 {
65  switch(criterion)
66  {
68  instrumenters.push_back(
69  util_make_unique<cover_location_instrumentert>(
70  symbol_table, goal_filters));
71  break;
73  instrumenters.push_back(
74  util_make_unique<cover_branch_instrumentert>(symbol_table, goal_filters));
75  break;
77  instrumenters.push_back(
78  util_make_unique<cover_decision_instrumentert>(
79  symbol_table, goal_filters));
80  break;
82  instrumenters.push_back(
83  util_make_unique<cover_condition_instrumentert>(
84  symbol_table, goal_filters));
85  break;
87  instrumenters.push_back(
88  util_make_unique<cover_path_instrumentert>(symbol_table, goal_filters));
89  break;
91  instrumenters.push_back(
92  util_make_unique<cover_mcdc_instrumentert>(symbol_table, goal_filters));
93  break;
95  instrumenters.push_back(
96  util_make_unique<cover_assertion_instrumentert>(
97  symbol_table, goal_filters));
98  break;
100  instrumenters.push_back(
101  util_make_unique<cover_cover_instrumentert>(symbol_table, goal_filters));
102  }
103 }
104 
109 parse_coverage_criterion(const std::string &criterion_string)
110 {
112 
113  if(criterion_string == "assertion" || criterion_string == "assertions")
115  else if(criterion_string == "path" || criterion_string == "paths")
117  else if(criterion_string == "branch" || criterion_string == "branches")
119  else if(criterion_string == "location" || criterion_string == "locations")
121  else if(criterion_string == "decision" || criterion_string == "decisions")
123  else if(criterion_string == "condition" || criterion_string == "conditions")
125  else if(criterion_string == "mcdc")
127  else if(criterion_string == "cover")
129  else
130  {
131  std::stringstream s;
132  s << "unknown coverage criterion " << '\'' << criterion_string << '\'';
133  throw invalid_command_line_argument_exceptiont(s.str(), "--cover");
134  }
135 
136  return c;
137 }
138 
142 void parse_cover_options(const cmdlinet &cmdline, optionst &options)
143 {
144  options.set_option("cover", cmdline.get_values("cover"));
145 
146  // allow retrieving full traces
147  options.set_option("simple-slice", false);
148 
149  options.set_option(
150  "cover-include-pattern", cmdline.get_value("cover-include-pattern"));
151  options.set_option("no-trivial-tests", cmdline.isset("no-trivial-tests"));
152 
153  std::string cover_only = cmdline.get_value("cover-only");
154 
155  if(!cover_only.empty() && cmdline.isset("cover-function-only"))
157  "at most one of --cover-only and --cover-function-only can be used",
158  "--cover-only");
159 
160  options.set_option("cover-only", cmdline.get_value("cover-only"));
161  if(cmdline.isset("cover-function-only"))
162  options.set_option("cover-only", "function");
163 
164  options.set_option(
165  "cover-traces-must-terminate",
166  cmdline.isset("cover-traces-must-terminate"));
167 }
168 
177  const optionst &options,
179  message_handlert &message_handler)
180 {
181  cover_configt cover_config;
182  function_filterst &function_filters =
183  cover_config.cover_configt::function_filters;
184  std::unique_ptr<goal_filterst> &goal_filters = cover_config.goal_filters;
185  cover_instrumenterst &instrumenters = cover_config.cover_instrumenters;
186 
187  function_filters.add(
188  util_make_unique<internal_functions_filtert>(message_handler));
189 
190  goal_filters->add(util_make_unique<internal_goals_filtert>(message_handler));
191 
192  optionst::value_listt criteria_strings = options.get_list_option("cover");
193 
194  cover_config.keep_assertions = false;
195  for(const auto &criterion_string : criteria_strings)
196  {
197  coverage_criteriont c = parse_coverage_criterion(criterion_string);
198 
200  cover_config.keep_assertions = true;
201 
202  instrumenters.add_from_criterion(c, symbol_table, *goal_filters);
203  }
204 
205  if(cover_config.keep_assertions && criteria_strings.size() > 1)
206  {
207  std::stringstream s;
208  s << "assertion coverage cannot currently be used together with other"
209  << "coverage criteria";
210  throw invalid_command_line_argument_exceptiont(s.str(), "--cover");
211  }
212 
213  std::string cover_include_pattern =
214  options.get_option("cover-include-pattern");
215  if(!cover_include_pattern.empty())
216  {
217  function_filters.add(
218  util_make_unique<include_pattern_filtert>(
219  message_handler, cover_include_pattern));
220  }
221 
222  if(options.get_bool_option("no-trivial-tests"))
223  function_filters.add(
224  util_make_unique<trivial_functions_filtert>(message_handler));
225 
226  cover_config.traces_must_terminate =
227  options.get_bool_option("cover-traces-must-terminate");
228 
229  return cover_config;
230 }
231 
240  const optionst &options,
241  const irep_idt &main_function_id,
243  message_handlert &message_handler)
244 {
245  cover_configt cover_config =
246  get_cover_config(options, symbol_table, message_handler);
247 
248  std::string cover_only = options.get_option("cover-only");
249 
250  // cover entry point function only
251  if(cover_only == "function")
252  {
253  const symbolt &main_symbol = symbol_table.lookup_ref(main_function_id);
254  cover_config.function_filters.add(util_make_unique<single_function_filtert>(
255  message_handler, main_symbol.name));
256  }
257  else if(cover_only == "file")
258  {
259  const symbolt &main_symbol = symbol_table.lookup_ref(main_function_id);
260  cover_config.function_filters.add(util_make_unique<file_filtert>(
261  message_handler, main_symbol.location.get_file()));
262  }
263  else if(!cover_only.empty())
264  {
265  std::stringstream s;
266  s << "Argument to --cover-only not recognized: " << cover_only;
267  throw invalid_command_line_argument_exceptiont(s.str(), "--cover-only");
268  }
269 
270  return cover_config;
271 }
272 
279  const cover_configt &cover_config,
280  const symbolt &function_symbol,
282  message_handlert &message_handler)
283 {
284  if(!cover_config.keep_assertions)
285  {
286  Forall_goto_program_instructions(i_it, function.body)
287  {
288  // Simplify the common case where we have ASSERT(x); ASSUME(x):
289  if(i_it->is_assert())
290  {
291  auto successor = std::next(i_it);
292  if(
293  successor != function.body.instructions.end() &&
294  successor->is_assume() &&
295  successor->get_condition() == i_it->get_condition())
296  {
297  successor->turn_into_skip();
298  }
299 
301  }
302  }
303  }
304 
305  bool changed = false;
306 
307  if(cover_config.function_filters(function_symbol, function))
308  {
309  messaget msg(message_handler);
310  msg.debug() << "Instrumenting coverage for function "
311  << id2string(function_symbol.name) << messaget::eom;
313  function_symbol.name,
314  function.body,
315  cover_config.cover_instrumenters,
316  function_symbol.mode,
317  message_handler,
318  cover_config.make_assertion);
319  changed = true;
320  }
321 
322  if(
323  cover_config.traces_must_terminate &&
324  function_symbol.name == goto_functionst::entry_point())
325  {
327  function_symbol.name, function.body, cover_config.make_assertion);
328  changed = true;
329  }
330 
331  if(changed)
332  remove_skip(function.body);
333 }
334 
340  const cover_configt &cover_config,
341  goto_model_functiont &function,
342  message_handlert &message_handler)
343 {
344  const symbolt function_symbol =
345  function.get_symbol_table().lookup_ref(function.get_function_id());
347  cover_config,
348  function_symbol,
349  function.get_goto_function(),
350  message_handler);
351 
352  function.compute_location_numbers();
353 }
354 
361  const cover_configt &cover_config,
364  message_handlert &message_handler)
365 {
366  messaget msg(message_handler);
367  msg.status() << "Rewriting existing assertions as assumptions"
368  << messaget::eom;
369 
370  if(
371  cover_config.traces_must_terminate &&
373  {
374  msg.error() << "cover-traces-must-terminate: invalid entry point ["
376  return true;
377  }
378 
380  {
381  const symbolt function_symbol = symbol_table.lookup_ref(f_it->first);
383  cover_config, function_symbol, f_it->second, message_handler);
384  }
386 
387  cover_config.function_filters.report_anomalies();
388  cover_config.goal_filters->report_anomalies();
389 
390  return false;
391 }
392 
398  const cover_configt &cover_config,
399  goto_modelt &goto_model,
400  message_handlert &message_handler)
401 {
402  return instrument_cover_goals(
403  cover_config,
404  goto_model.symbol_table,
405  goto_model.goto_functions,
406  message_handler);
407 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
Forall_goto_program_instructions
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:1201
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
cover.h
Coverage Instrumentation.
coverage_criteriont::COVER
@ COVER
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
function_filterst::report_anomalies
void report_anomalies() const
Can be called after final filter application to report on unexpected situations encountered.
Definition: cover_filter.h:101
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:29
cover_instrument_end_of_function
void cover_instrument_end_of_function(const irep_idt &function_id, goto_programt &goto_program, const cover_instrumenter_baset::assertion_factoryt &)
Definition: cover_instrument_other.cpp:75
cover_configt::goal_filters
std::unique_ptr< goal_filterst > goal_filters
Definition: cover.h:44
optionst
Definition: options.h:23
optionst::get_option
const std::string get_option(const std::string &option) const
Definition: options.cpp:67
messaget::status
mstreamt & status() const
Definition: message.h:414
remove_skip
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
Definition: remove_skip.cpp:85
coverage_criteriont::PATH
@ PATH
goal_filterst::add
void add(std::unique_ptr< goal_filter_baset > filter)
Adds a function filter.
Definition: cover_filter.h:117
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:18
goto_modelt
Definition: goto_model.h:26
parse_coverage_criterion
coverage_criteriont parse_coverage_criterion(const std::string &criterion_string)
Parses a coverage criterion.
Definition: cover.cpp:109
options.h
Options.
cover_instrumenterst::add_from_criterion
void add_from_criterion(coverage_criteriont, const symbol_tablet &, const goal_filterst &)
Create and add an instrumenter based on the given criterion.
Definition: cover.cpp:60
optionst::set_option
void set_option(const std::string &option, const bool value)
Definition: options.cpp:28
messaget::eom
static eomt eom
Definition: message.h:297
optionst::value_listt
std::list< std::string > value_listt
Definition: options.h:25
instrument_cover_goals
static void instrument_cover_goals(const irep_idt &function_id, goto_programt &goto_program, const cover_instrumenterst &instrumenters, const irep_idt &mode, message_handlert &message_handler, const cover_instrumenter_baset::assertion_factoryt &make_assertion)
Applies instrumenters to given goto program.
Definition: cover.cpp:37
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
coverage_criteriont
coverage_criteriont
Definition: cover.h:26
coverage_criteriont::ASSERTION
@ ASSERTION
coverage_criteriont::CONDITION
@ CONDITION
coverage_criteriont::LOCATION
@ LOCATION
get_cover_config
cover_configt get_cover_config(const optionst &options, const symbol_tablet &symbol_table, message_handlert &message_handler)
Build data structures controlling coverage from command-line options.
Definition: cover.cpp:176
cover_basic_blocks_javat
Definition: cover_basic_blocks.h:138
cover_instrumenterst::instrumenters
std::vector< std::unique_ptr< cover_instrumenter_baset > > instrumenters
Definition: cover_instrument.h:126
cover_configt::cover_instrumenters
cover_instrumenterst cover_instrumenters
Definition: cover.h:46
cmdlinet
Definition: cmdline.h:21
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
make_unique.h
messaget::error
mstreamt & error() const
Definition: message.h:399
coverage_criteriont::DECISION
@ DECISION
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
cover_configt::traces_must_terminate
bool traces_must_terminate
Definition: cover.h:40
cover_configt::make_assertion
cover_instrumenter_baset::assertion_factoryt make_assertion
Definition: cover.h:47
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:47
message_handlert
Definition: message.h:28
parse_cover_options
void parse_cover_options(const cmdlinet &cmdline, optionst &options)
Parses coverage-related command line options.
Definition: cover.cpp:142
goal_filterst
A collection of goal filters to be applied in conjunction.
Definition: cover_filter.h:113
cover_blocks_baset::report_block_anomalies
virtual void report_block_anomalies(const irep_idt &function_id, const goto_programt &goto_program, message_handlert &message_handler)
Output warnings about ignored blocks.
Definition: cover_basic_blocks.h:51
cover_instrumenter_baset::assertion_factoryt
std::function< goto_programt::instructiont(const exprt &, const source_locationt &)> assertion_factoryt
The type of function used to make goto_program assertions.
Definition: cover_instrument.h:41
goto_modelt::get_goto_function
const goto_functionst::goto_functiont & get_goto_function(const irep_idt &id) override
Get a GOTO function by name, or throw if no such function exists.
Definition: goto_model.h:82
Forall_goto_functions
#define Forall_goto_functions(it, functions)
Definition: goto_functions.h:117
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:25
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
cover_basic_blockst
Definition: cover_basic_blocks.h:64
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
cover_configt::function_filters
function_filterst function_filters
Definition: cover.h:42
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
cmdline.h
symbolt
Symbol table entry.
Definition: symbol.h:28
ASSUME
@ ASSUME
Definition: goto_program.h:35
optionst::get_bool_option
bool get_bool_option(const std::string &option) const
Definition: options.cpp:44
coverage_criteriont::BRANCH
@ BRANCH
cover_basic_blocks.h
Basic blocks detection for Coverage Instrumentation.
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
coverage_criteriont::MCDC
@ MCDC
config.h
cover_configt
Definition: cover.h:38
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:36
messaget::debug
mstreamt & debug() const
Definition: message.h:429
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:90
remove_skip.h
Program Transformation.
message.h
function_filterst
A collection of function filters to be applied in conjunction.
Definition: cover_filter.h:76
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition: exception_utils.h:38
cmdlinet::get_values
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:108
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
function_filterst::add
void add(std::unique_ptr< function_filter_baset > filter)
Adds a function filter.
Definition: cover_filter.h:80
optionst::get_list_option
const value_listt & get_list_option(const std::string &option) const
Definition: options.cpp:80
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
goto_model_functiont
Interface providing access to a single function in a GOTO model, plus its associated symbol table.
Definition: goto_model.h:183
cover_configt::keep_assertions
bool keep_assertions
Definition: cover.h:39
cover_instrumenterst
A collection of instrumenters to be run.
Definition: cover_instrument.h:102