cprover
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 
19 
21 
23 {
24  const irep_idt identifier="$stack_depth";
25  unsignedbv_typet type(sizeof(std::size_t)*8);
26 
27  symbolt new_symbol;
28  new_symbol.name=identifier;
29  new_symbol.base_name=identifier;
30  new_symbol.pretty_name=identifier;
31  new_symbol.type=type;
32  new_symbol.is_static_lifetime=true;
33  new_symbol.value=from_integer(0, type);
34  new_symbol.mode=ID_C;
35  new_symbol.is_thread_local=true;
36  new_symbol.is_lvalue=true;
37 
38  symbol_table.insert(std::move(new_symbol));
39 
40  return symbol_exprt(identifier, type);
41 }
42 
44  goto_programt &goto_program,
45  const symbol_exprt &symbol,
46  const std::size_t i_depth,
47  const exprt &max_depth)
48 {
49  assert(!goto_program.instructions.empty());
50 
51  goto_programt::targett first=goto_program.instructions.begin();
52 
53  binary_relation_exprt guard(symbol, ID_le, max_depth);
54  goto_programt::targett assert_ins = goto_program.insert_before(
55  first, goto_programt::make_assertion(guard, first->source_location));
56 
57  assert_ins->source_location.set_comment(
58  "Stack depth exceeds "+std::to_string(i_depth));
59  assert_ins->source_location.set_property_class("stack-depth");
60 
61  goto_program.insert_before(
62  first,
64  code_assignt(symbol, plus_exprt(symbol, from_integer(1, symbol.type()))),
65  first->source_location));
66 
67  goto_programt::targett last=--goto_program.instructions.end();
68  assert(last->is_end_function());
69 
71  code_assignt(symbol, minus_exprt(symbol, from_integer(1, symbol.type()))),
72  last->source_location);
73 
74  goto_program.insert_before_swap(last, minus_ins);
75 }
76 
78  goto_modelt &goto_model,
79  const std::size_t depth)
80 {
81  const symbol_exprt sym=
83 
84  const exprt depth_expr(from_integer(depth, sym.type()));
85 
86  Forall_goto_functions(f_it, goto_model.goto_functions)
87  if(f_it->second.body_available() &&
88  f_it->first != INITIALIZE_FUNCTION &&
89  f_it->first!=goto_functionst::entry_point())
90  stack_depth(f_it->second.body, sym, depth, depth_expr);
91 
92  // initialize depth to 0
93  goto_functionst::function_mapt::iterator i_it =
96  i_it!=goto_model.goto_functions.function_map.end(),
97  INITIALIZE_FUNCTION " must exist");
98 
99  goto_programt &init=i_it->second.body;
100  goto_programt::targett first=init.instructions.begin();
101  init.insert_before(
102  first,
104  code_assignt(sym, from_integer(0, sym.type()))));
105  // no suitable value for source location -- omitted
106 
107  // update counters etc.
108  goto_model.goto_functions.update();
109 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
arith_tools.h
stack_depth
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:43
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_model.h
Symbol Table + CFG.
plus_exprt
The plus expression Associativity is not specified.
Definition: std_expr.h:881
exprt
Base class for all expressions.
Definition: expr.h:53
goto_modelt
Definition: goto_model.h:26
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
goto_programt::make_assignment
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
Definition: goto_program.h:1024
unsignedbv_typet
Fixed-width bit-vector with unsigned binary interpretation.
Definition: std_types.h:1216
symbolt::pretty_name
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
symbolt::is_thread_local
bool is_thread_local
Definition: symbol.h:65
goto_programt::insert_before
targett insert_before(const_targett target)
Insertion before the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:639
goto_programt::make_assertion
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:893
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:23
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:19
minus_exprt
Binary minus.
Definition: std_expr.h:940
Forall_goto_functions
#define Forall_goto_functions(it, functions)
Definition: goto_functions.h:117
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:585
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
stack_depth.h
Stack depth checks.
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
symbolt
Symbol table entry.
Definition: symbol.h:28
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
binary_relation_exprt
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:725
goto_functionst::update
void update()
Definition: goto_functions.h:81
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
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
static_lifetime_init.h
goto_programt::insert_before_swap
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:606
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
add_stack_depth_symbol
symbol_exprt add_stack_depth_symbol(symbol_tablet &symbol_table)
Definition: stack_depth.cpp:22
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:179
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:254
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:579