Skip to content

Commit b454182

Browse files
committed
Stack-depth instrumentation: update the symbol table
Update the symbol table and auto-generate __CPROVER_initialize instead of hand-tweaking the function. This will ensure that any further rebuilding of __CPROVER_initialize preserves those changes.
1 parent edf6a5c commit b454182

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,8 @@ void goto_instrument_parse_optionst::instrument_goto_program()
13491349
log.status() << "Adding check for maximum call stack size" << messaget::eom;
13501350
stack_depth(
13511351
goto_model,
1352-
safe_string2size_t(cmdline.get_value("stack-depth")));
1352+
safe_string2size_t(cmdline.get_value("stack-depth")),
1353+
ui_message_handler);
13531354
}
13541355

13551356
// ignore default/user-specified initialization of variables with static

src/goto-instrument/stack_depth.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ Date: November 2011
1616
#include <util/arith_tools.h>
1717
#include <util/bitvector_types.h>
1818

19+
#include <goto-programs/goto_convert_functions.h>
1920
#include <goto-programs/goto_model.h>
2021

2122
#include <linking/static_lifetime_init.h>
2223

23-
symbol_exprt add_stack_depth_symbol(symbol_tablet &symbol_table)
24+
static symbol_exprt add_stack_depth_symbol(
25+
goto_modelt &goto_model,
26+
message_handlert &message_handler)
2427
{
2528
const irep_idt identifier="$stack_depth";
2629
unsignedbv_typet type(sizeof(std::size_t)*8);
@@ -36,12 +39,26 @@ symbol_exprt add_stack_depth_symbol(symbol_tablet &symbol_table)
3639
new_symbol.is_thread_local=true;
3740
new_symbol.is_lvalue=true;
3841

39-
symbol_table.insert(std::move(new_symbol));
42+
bool failed = goto_model.symbol_table.add(new_symbol);
43+
CHECK_RETURN(!failed);
4044

41-
return symbol_exprt(identifier, type);
45+
if(goto_model.goto_functions.function_map.erase(INITIALIZE_FUNCTION) != 0)
46+
{
47+
static_lifetime_init(
48+
goto_model.symbol_table,
49+
goto_model.symbol_table.lookup_ref(INITIALIZE_FUNCTION).location);
50+
goto_convert(
51+
INITIALIZE_FUNCTION,
52+
goto_model.symbol_table,
53+
goto_model.goto_functions,
54+
message_handler);
55+
goto_model.goto_functions.update();
56+
}
57+
58+
return new_symbol.symbol_expr();
4259
}
4360

44-
void stack_depth(
61+
static void stack_depth(
4562
goto_programt &goto_program,
4663
const symbol_exprt &symbol,
4764
const std::size_t i_depth,
@@ -77,10 +94,10 @@ void stack_depth(
7794

7895
void stack_depth(
7996
goto_modelt &goto_model,
80-
const std::size_t depth)
97+
const std::size_t depth,
98+
message_handlert &message_handler)
8199
{
82-
const symbol_exprt sym=
83-
add_stack_depth_symbol(goto_model.symbol_table);
100+
const symbol_exprt sym = add_stack_depth_symbol(goto_model, message_handler);
84101

85102
const exprt depth_expr(from_integer(depth, sym.type()));
86103

@@ -95,21 +112,6 @@ void stack_depth(
95112
}
96113
}
97114

98-
// initialize depth to 0
99-
goto_functionst::function_mapt::iterator i_it =
100-
goto_model.goto_functions.function_map.find(INITIALIZE_FUNCTION);
101-
DATA_INVARIANT(
102-
i_it!=goto_model.goto_functions.function_map.end(),
103-
INITIALIZE_FUNCTION " must exist");
104-
105-
goto_programt &init=i_it->second.body;
106-
goto_programt::targett first=init.instructions.begin();
107-
init.insert_before(
108-
first,
109-
goto_programt::make_assignment(
110-
code_assignt(sym, from_integer(0, sym.type()))));
111-
// no suitable value for source location -- omitted
112-
113115
// update counters etc.
114116
goto_model.goto_functions.update();
115117
}

src/goto-instrument/stack_depth.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ Date: November 2011
1717
#include <cstddef>
1818

1919
class goto_modelt;
20+
class message_handlert;
2021

22+
/// Add assertions to all user-defined functions in \p goto_model that the call
23+
/// stack depth does not exceed \p depth.
24+
/// Warnings are reported via \p message_handler.
2125
void stack_depth(
22-
goto_modelt &,
23-
const std::size_t depth);
26+
goto_modelt &goto_model,
27+
const std::size_t depth,
28+
message_handlert &message_handler);
2429

2530
#endif // CPROVER_GOTO_INSTRUMENT_STACK_DEPTH_H

0 commit comments

Comments
 (0)