Skip to content

Commit 82a83e6

Browse files
committed
Add --strip-static flag to goto-instrument
This flag allows the user to supply a list of functions that will have the `file_local` attribute removed. This is aimed at users wishing to verify static (in the C sense) functions. Using this flag ensures that those functions are visible outside the compilation unit.
1 parent 79ce175 commit 82a83e6

File tree

12 files changed

+124
-12
lines changed

12 files changed

+124
-12
lines changed

regression/goto-cc-static/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ else()
55
endif()
66

77
add_test_pl_tests(
8-
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $<TARGET_FILE:goto-cc> $<TARGET_FILE:cbmc> ${is_windows}"
8+
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $<TARGET_FILE:goto-cc> $<TARGET_FILE:goto-instrument> ${is_windows}"
99
)

regression/goto-cc-static/chain.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
#!/usr/bin/env bash
22

33
goto_cc=$1
4-
cbmc=$2
4+
goto_instrument=$2
55
is_windows=$3
66

77
to_keep=""
88
if [ $# -gt 3 ]; then
9-
if [ $4!='' ]; then
9+
if [[ $4 != '' ]]; then
1010
to_keep="--keep-implementations $4"
11+
strip_static="--strip-static $4"
1112
fi
1213
fi
1314

14-
cbmc_options=""
15-
if [ $# -gt 4 ]; then
16-
cbmc_options=${*:5:$#-5}
17-
fi
18-
1915
name=${*:$#}
2016
name=${name%.c}
2117

@@ -26,4 +22,10 @@ else
2622
"${goto_cc}" ${to_keep} "${name}.c" -o "${name}.gb"
2723
fi
2824

29-
"${cbmc}" ${cbmc_options} "${name}.gb" ${cbmc_options}
25+
if [[ "$to_keep" = '' ]]; then
26+
cp "${name}.gb" "${name}-mod.gb"
27+
else
28+
${goto_instrument} ${strip_static} "${name}.gb" "${name}-mod.gb"
29+
fi
30+
31+
"${goto_instrument}" --show-symbol-table "${name}-mod.gb" "${name}-mod-2.gb"

regression/goto-cc-static/keep_static-1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
main.c
3-
foo --show-symbol-table
3+
foo
44

55
^EXIT=0$
66
^SIGNAL=0$

regression/goto-cc-static/keep_static-2/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CORE
22
main.c
3-
main --show-symbol-table
43

54
^EXIT=0$
65
^SIGNAL=0$

regression/goto-cc-static/keep_static-3/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
main.c
3-
foo,bar --show-symbol-table
3+
foo,bar
44

55
^EXIT=0$
66
^SIGNAL=0$
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
static int foo()
2+
{
3+
return 3;
4+
}
5+
6+
int main()
7+
{
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
foo
4+
activate-multi-line-match
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
Symbol......: foo\nPretty name.: .+\nModule......: .+\nBase name...: .+\nMode........: .+\nType........: .+\nValue.......: .+\nFlags.......: lvalue
8+
--
9+
Symbol......: foo\nPretty name.: .+\nModule......: .+\nBase name...: .+\nMode........: .+\nType........: .+\nValue.......: .+\nFlags.......: .*file_local.*
10+
--

src/goto-instrument/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ SRC = accelerate/accelerate.cpp \
6060
source_lines.cpp \
6161
splice_call.cpp \
6262
stack_depth.cpp \
63+
strip_static.cpp \
6364
thread_instrumentation.cpp \
6465
undefined_functions.cpp \
6566
uninitialized.cpp \

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ int goto_instrument_parse_optionst::doit()
232232
}
233233
}
234234

235+
if(cmdline.isset("strip-static"))
236+
{
237+
std::list<std::string> funs_to_strip =
238+
cmdline.get_comma_separated_values("strip-static");
239+
if(funs_to_strip.empty())
240+
{
241+
error() << "Provide a comma-delimited list of functions to remove"
242+
" the static attribute from"
243+
<< eom;
244+
return CPROVER_EXIT_USAGE_ERROR;
245+
}
246+
247+
static_strippert ss(goto_model, *this);
248+
ss.strip_statics_from_funs(funs_to_strip);
249+
}
250+
235251
if(cmdline.isset("show-threaded"))
236252
{
237253
namespacet ns(goto_model.symbol_table);
@@ -1668,6 +1684,7 @@ void goto_instrument_parse_optionst::help()
16681684
" --remove-function-pointers replace function pointers by case statement over function calls\n" // NOLINT(*)
16691685
HELP_REMOVE_CALLS_NO_BODY
16701686
HELP_REMOVE_CONST_FUNCTION_POINTERS
1687+
HELP_STRIP_STATIC
16711688
" --add-library add models of C library functions\n"
16721689
" --model-argc-argv <n> model up to <n> command line arguments\n"
16731690
// NOLINTNEXTLINE(whitespace/line_length)

src/goto-instrument/goto_instrument_parse_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Author: Daniel Kroening, [email protected]
3333
#include "generate_function_bodies.h"
3434

3535
#include "count_eloc.h"
36+
#include "strip_static.h"
3637

3738
// clang-format off
3839
#define GOTO_INSTRUMENT_OPTIONS \
@@ -83,6 +84,7 @@ Author: Daniel Kroening, [email protected]
8384
"(show-symbol-table)(show-points-to)(show-rw-set)" \
8485
"(cav11)" \
8586
OPT_TIMESTAMP \
87+
OPT_STRIP_STATIC \
8688
"(show-natural-loops)(accelerate)(havoc-loops)" \
8789
"(error-label):(string-abstraction)" \
8890
"(verbosity):(version)(xml-ui)(json-ui)(show-loops)" \

src/goto-instrument/strip_static.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************\
2+
3+
Module: Strip static
4+
5+
Author: Kareem Khazem <[email protected]>
6+
7+
\*******************************************************************/
8+
9+
#include "strip_static.h"
10+
11+
bool static_strippert::strip_statics_from_funs(
12+
const std::list<std::string> &funs)
13+
{
14+
bool fail = false;
15+
16+
for(const auto fun : funs)
17+
{
18+
if(!model.symbol_table.has_symbol(fun))
19+
{
20+
log.error() << "Could not find symbol '" << fun << "' to remove static"
21+
<< log.eom;
22+
fail = true;
23+
continue;
24+
}
25+
symbolt &sym = model.symbol_table.get_writeable_ref(fun);
26+
if(!sym.is_file_local)
27+
{
28+
log.warning() << "Not removing static attribute from symbol '" << fun
29+
<< "': symbol is already externally visible" << log.eom;
30+
continue;
31+
}
32+
sym.is_file_local = false;
33+
}
34+
35+
return fail;
36+
}

src/goto-instrument/strip_static.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// \file strip_static.h
2+
/// \brief Remove static attribute from functions
3+
/// \author Kareem Khazem <[email protected]>
4+
5+
#ifndef CPROVER_GOTO_INSTRUMENT_STRIP_STATIC_H
6+
#define CPROVER_GOTO_INSTRUMENT_STRIP_STATIC_H
7+
8+
#include <goto-programs/goto_model.h>
9+
#include <util/message.h>
10+
11+
#include <forward_list>
12+
#include <string>
13+
14+
/// \brief Remove static attribute from functions
15+
class static_strippert
16+
{
17+
public:
18+
explicit static_strippert(goto_modelt &goto_model, messaget &log)
19+
: model(goto_model), log(log)
20+
{
21+
}
22+
23+
/// \brief Removes the static attribute from functions
24+
///
25+
/// \return `true` on failure, `false` otherwise
26+
bool strip_statics_from_funs(const std::list<std::string> &);
27+
28+
protected:
29+
goto_modelt &model;
30+
messaget &log;
31+
};
32+
33+
#define OPT_STRIP_STATIC "(strip-static):"
34+
#define HELP_STRIP_STATIC \
35+
" --strip-static fun[,fun...] remove static from functions\n"
36+
37+
#endif /* CPROVER_GOTO_INSTRUMENT_STRIP_STATIC_H */

0 commit comments

Comments
 (0)