CBMC
stack_depth.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Stack depth checks
4 
5 Author: Daniel Kroening, Michael Tautschnig
6 
7 Date: November 2011
8 
9 \*******************************************************************/
10 
13 
14 #include "stack_depth.h"
15 
16 #include <util/arith_tools.h>
17 #include <util/bitvector_types.h>
18 #include <util/c_types.h>
19 
21 
24 
26  goto_modelt &goto_model,
27  message_handlert &message_handler)
28 {
29  const irep_idt identifier="$stack_depth";
30  typet type = size_type();
31 
32  symbolt new_symbol{identifier, type, ID_C};
33  new_symbol.base_name=identifier;
34  new_symbol.pretty_name=identifier;
35  new_symbol.is_static_lifetime=true;
36  new_symbol.value=from_integer(0, type);
37  new_symbol.is_thread_local=true;
38  new_symbol.is_lvalue=true;
39 
40  bool failed = goto_model.symbol_table.add(new_symbol);
42 
44  recreate_initialize_function(goto_model, message_handler);
45 
46  return new_symbol.symbol_expr();
47 }
48 
49 static void stack_depth(
50  goto_programt &goto_program,
51  const symbol_exprt &symbol,
52  const std::size_t i_depth,
53  const exprt &max_depth)
54 {
55  PRECONDITION(!goto_program.instructions.empty());
56 
57  goto_programt::targett first=goto_program.instructions.begin();
58 
59  binary_relation_exprt guard(symbol, ID_le, max_depth);
60  source_locationt annotated_location = first->source_location();
61  annotated_location.set_comment(
62  "Stack depth exceeds " + std::to_string(i_depth));
63  annotated_location.set_property_class("stack-depth");
64  goto_program.insert_before(
65  first, goto_programt::make_assertion(guard, annotated_location));
66 
67  goto_program.insert_before(
68  first,
70  code_assignt(symbol, plus_exprt(symbol, from_integer(1, symbol.type()))),
71  first->source_location()));
72 
73  goto_programt::targett last=--goto_program.instructions.end();
74  DATA_INVARIANT(last->is_end_function(), "must be end of function");
75 
77  code_assignt(symbol, minus_exprt(symbol, from_integer(1, symbol.type()))),
78  last->source_location());
79 
80  goto_program.insert_before_swap(last, minus_ins);
81 }
82 
84  goto_modelt &goto_model,
85  const std::size_t depth,
86  message_handlert &message_handler)
87 {
88  const symbol_exprt sym = add_stack_depth_symbol(goto_model, message_handler);
89 
90  const exprt depth_expr(from_integer(depth, sym.type()));
91 
92  for(auto &gf_entry : goto_model.goto_functions.function_map)
93  {
94  if(
95  gf_entry.second.body_available() &&
96  gf_entry.first != INITIALIZE_FUNCTION &&
97  gf_entry.first != goto_functionst::entry_point())
98  {
99  stack_depth(gf_entry.second.body, sym, depth, depth_expr);
100  }
101  }
102 
103  // update counters etc.
104  goto_model.goto_functions.update();
105 }
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
Pre-defined bitvector types.
static exprt guard(const exprt::operandst &guards, exprt cond)
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:762
A goto_instruction_codet representing an assignment in the program.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
const source_locationt & source_location() const
Definition: expr.h:231
typet & type()
Return the type of the expression.
Definition: expr.h:84
function_mapt function_map
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
bool can_produce_function(const irep_idt &id) const override
Determines if this model can produce a body for the given function.
Definition: goto_model.h:95
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:181
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:622
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:643
instructionst::iterator targett
Definition: goto_program.h:614
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:933
targett insert_before(const_targett target)
Insertion before the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:692
Binary minus.
Definition: std_expr.h:1061
The plus expression Associativity is not specified.
Definition: std_expr.h:1002
void set_comment(const irep_idt &comment)
void set_property_class(const irep_idt &property_class)
Expression to hold a symbol (variable)
Definition: std_expr.h:131
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Symbol table entry.
Definition: symbol.h:28
The type of an expression, extends irept.
Definition: type.h:29
Goto Programs with Functions.
Symbol Table + CFG.
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:534
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
static void stack_depth(goto_programt &goto_program, const symbol_exprt &symbol, const std::size_t i_depth, const exprt &max_depth)
Definition: stack_depth.cpp:49
static symbol_exprt add_stack_depth_symbol(goto_modelt &goto_model, message_handlert &message_handler)
Definition: stack_depth.cpp:25
Stack depth checks.
void recreate_initialize_function(goto_modelt &goto_model, message_handlert &message_handler)
Regenerates the CPROVER_INITIALIZE function, which initializes all non-function symbols of the goto m...
#define INITIALIZE_FUNCTION
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
static bool failed(bool error_indicator)
#define size_type
Definition: unistd.c:347