Skip to content

Commit 419c372

Browse files
authored
Merge pull request #4565 from karkhaz/kk-mangle-basename
Mangle static functions using basename, not path
2 parents 89d349b + 236c257 commit 419c372

File tree

18 files changed

+160
-24
lines changed

18 files changed

+160
-24
lines changed

regression/goto-cc-file-local/chain.sh

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,78 @@ goto_instrument=$2
2323
cbmc=$3
2424
is_windows=$4
2525

26-
for src in *.c; do
26+
if [[ "${is_windows}" != "true" && "${is_windows}" != "false" ]]; then
27+
(>&2 echo "\$is_windows should be true or false (got '" "${is_windows}" "')")
28+
exit 1
29+
fi
30+
31+
if is_in use_find "$ALL_ARGS"; then
32+
SRC=$(find . -name "*.c")
33+
else
34+
SRC=*.c
35+
fi
36+
37+
echo "Source files are ${SRC}"
38+
39+
wall=""
40+
if is_in wall "$ALL_ARGS"; then
41+
if [[ "${is_windows}" == "true" ]]; then
42+
wall="/Wall"
43+
else
44+
wall="-Wall"
45+
fi
46+
fi
47+
48+
cnt=0
49+
for src in ${SRC}; do
50+
cnt=$((cnt + 1))
51+
out_suffix=""
52+
if is_in out-file-counter "$ALL_ARGS"; then
53+
out_suffix="_$cnt"
54+
if is_in suffix "$ALL_ARGS"; then
55+
suffix="--mangle-suffix custom_$cnt"
56+
fi
57+
fi
58+
2759
base=${src%.c}
28-
OUT_FILE="${base}.gb"
60+
OUT_FILE=$(basename "${base}${out_suffix}")".gb"
2961

3062
if [[ "${is_windows}" == "true" ]]; then
3163
"${goto_cc}" \
32-
/export-function-local-symbols \
33-
/verbosity 10 \
64+
--export-function-local-symbols \
65+
--verbosity 10 \
66+
${wall} \
3467
${suffix} \
35-
/c"${base}.c" \
68+
/c "${base}.c" \
3669
/Fo"${OUT_FILE}"
3770

38-
elif [[ "${is_windows}" == "false" ]]; then
71+
else
3972
"${goto_cc}" \
4073
--export-function-local-symbols \
4174
--verbosity 10 \
75+
${wall} \
4276
${suffix} \
4377
-c "${base}.c" \
4478
-o "${OUT_FILE}"
45-
else
46-
(>&2 echo "\$is_windows should be true or false (got '" "${is_windows}" "')")
47-
exit 1
4879
fi
4980
done
5081

5182
if is_in final-link "$ALL_ARGS"; then
5283
OUT_FILE=final-link.gb
5384
if [[ "${is_windows}" == "true" ]]; then
5485
"${goto_cc}" \
55-
/export-function-local-symbols \
56-
/verbosity 10 \
86+
--export-function-local-symbols \
87+
--verbosity 10 \
88+
${wall} \
5789
${suffix} \
5890
./*.gb \
59-
/Fe "${OUT_FILE}"
91+
/Fe"${OUT_FILE}"
6092

61-
elif [[ "${is_windows}" == "false" ]]; then
93+
else
6294
"${goto_cc}" \
6395
--export-function-local-symbols \
6496
--verbosity 10 \
97+
${wall} \
6598
${suffix} \
6699
./*.gb \
67100
-o "${OUT_FILE}"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static int static_fun()
2+
{
3+
return 3;
4+
}
5+
6+
int bar()
7+
{
8+
return static_fun();
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <assert.h>
2+
3+
int bar();
4+
5+
static int static_fun()
6+
{
7+
return bar();
8+
}
9+
10+
int main()
11+
{
12+
assert(static_fun() == 3);
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CORE
2+
foo/bar/baz/main.c
3+
use_find out-file-counter final-link wall suffix assertion-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^.*warning: function `__CPROVER_file_local_main_c_static_fun' in module `main' is shadowed by a definition in module `main'
8+
^warning: ignoring
9+
^\*\*\*\* WARNING: no body for function
10+
--
11+
This test contains two identically-named static functions in two
12+
identically-named files in different directories. By default, the
13+
name-mangling scheme would have the static functions identical names when
14+
they got exported. This test ensures the functions each get a unique
15+
suffix.
16+
17+
CBMC should work on the finally-linked binary because the functions have
18+
been correctly mangled.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static int static_fun()
2+
{
3+
return 3;
4+
}
5+
6+
int bar()
7+
{
8+
return static_fun();
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <assert.h>
2+
3+
int bar();
4+
5+
static int static_fun()
6+
{
7+
return bar();
8+
}
9+
10+
int main()
11+
{
12+
assert(static_fun() == 3);
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CORE
2+
foo/bar/baz/main.c
3+
use_find out-file-counter final-link wall
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^.*warning: function `__CPROVER_file_local_main_c_static_fun' in module `main' is shadowed by a definition in module `main'
7+
--
8+
^warning: ignoring
9+
^\*\*\*\* WARNING: no body for function
10+
--
11+
This test contains two identically-named static functions in two
12+
identically-named files in different directories. The name-mangling
13+
scheme will give the static functions identical names when they get
14+
exported. This test ensures that the compiler emits a warning about
15+
this.
16+
17+
CBMC spins forever when run on the final object file, since the `foo'
18+
implementation of `static_fun` is the one that is kept. There is thus an
19+
infinite loop where foo/static_fun() calls bar(), and bar() calls
20+
foo/static_fun() instead of bar/static_fun().
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static int foo()
2+
{
3+
return 3;
4+
}
5+
6+
int main()
7+
{
8+
int x = foo();
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CORE
2+
foo/bar/baz/main.c
3+
show-symbol-table use_find
4+
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
^Symbol......: __CPROVER_file_local_main_c_foo$
8+
--
9+
^warning: ignoring
10+
^\*\*\*\* WARNING: no body for function
11+
^Symbol......: __CPROVER_file_local_foo_bar_baz_main_c_foo$
12+
--
13+
This test checks that a file that we compile several directories up from
14+
its actual location will get mangled based on its basename, not its
15+
entire path.

regression/goto-cc-file-local/result-multi-file-bad/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
final-link assertion-check
4-
54
^EXIT=10$
65
^SIGNAL=0$
76
^VERIFICATION FAILED$

regression/goto-cc-file-local/result-multi-file-good/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
final-link assertion-check
4-
54
^EXIT=0$
65
^SIGNAL=0$
76
^VERIFICATION SUCCESSFUL$

regression/goto-cc-file-local/result-multi-file-transitive/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
final-link assertion-check
4-
54
^EXIT=10$
65
^SIGNAL=0$
76
^VERIFICATION FAILED$

regression/goto-cc-file-local/result-suffix/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
final-link assertion-check suffix
4-
54
^EXIT=10$
65
^SIGNAL=0$
76
^VERIFICATION FAILED$

regression/goto-cc-file-local/symbol-compiled/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
show-symbol-table
4-
54
^EXIT=0$
65
^SIGNAL=0$
76
^Symbol......: __CPROVER_file_local_main_c_foo$

regression/goto-cc-file-local/symbol-fully-linked/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
final-link show-symbol-table
4-
54
^EXIT=0$
65
^SIGNAL=0$
76
^Symbol......: __CPROVER_file_local_main_c_foo$

regression/goto-cc-file-local/symbol-multi-file/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
CORE
22
main.c
33
final-link show-symbol-table
4-
54
^EXIT=0$
65
^SIGNAL=0$
76
^Symbol......: __CPROVER_file_local_foo_c_foo$

src/goto-cc/ms_cl_cmdline.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ bool ms_cl_cmdlinet::parse(const std::vector<std::string> &arguments)
6161
{
6262
process_non_cl_option(arguments[i]);
6363

64-
if(arguments[i] == "--verbosity" || arguments[i] == "--function")
64+
if(
65+
arguments[i] == "--verbosity" || arguments[i] == "--function" ||
66+
arguments[i] == "--mangle-suffix")
6567
{
6668
if(i < arguments.size() - 1)
6769
{

src/goto-programs/name_mangler.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ Author: Kareem Khazem <[email protected]>, 2019
88

99
#include "name_mangler.h"
1010

11+
#include <util/get_base_name.h>
12+
1113
#include <iomanip>
1214
#include <sstream>
1315

1416
irep_idt file_name_manglert::
1517
operator()(const symbolt &src, const std::string &extra_info)
1618
{
19+
std::string basename = get_base_name(src.location.get_file().c_str(), false);
20+
1721
std::stringstream ss;
1822
ss << CPROVER_PREFIX << "file_local_";
1923
ss << std::regex_replace(
20-
std::regex_replace(src.location.get_file().c_str(), forbidden, "_"),
21-
multi_under,
22-
"_")
24+
std::regex_replace(basename, forbidden, "_"), multi_under, "_")
2325
<< "_";
2426

2527
if(extra_info != "")

0 commit comments

Comments
 (0)