Skip to content

Commit 62b9c96

Browse files
authored
Merge pull request #5373 from NlightNFotis/fix-weird-identifier-names
Filter out illegal characters from identifiers in typecheck_expr.
2 parents 70ca8a6 + fca9cc2 commit 62b9c96

File tree

9 files changed

+127
-3
lines changed

9 files changed

+127
-3
lines changed

regression/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_subdirectory(smt2_strings)
3838
add_subdirectory(strings)
3939
add_subdirectory(invariants)
4040
add_subdirectory(goto-diff)
41+
add_subdirectory(goto-cc-instrument-cbmc)
4142
add_subdirectory(test-script)
4243
add_subdirectory(goto-analyzer-taint)
4344
if(NOT WIN32)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if(WIN32)
2+
set(is_windows true)
3+
else()
4+
set(is_windows false)
5+
endif()
6+
7+
add_test_pl_tests(
8+
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $<TARGET_FILE:goto-cc> $<TARGET_FILE:cbmc> $<TARGET_FILE:goto-instrument> ${is_windows}"
9+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
default: tests.log
2+
3+
include ../../src/config.inc
4+
include ../../src/common
5+
6+
ifeq ($(BUILD_ENV_),MSVC)
7+
exe=../../../src/goto-cc/goto-cl
8+
is_windows=true
9+
else
10+
exe=../../../src/goto-cc/goto-cc
11+
is_windows=false
12+
endif
13+
14+
cbmc=../../../src/cbmc/cbmc
15+
goto-instrument=../../../src/goto-instrument/goto-instrument
16+
17+
test:
18+
@../test.pl -e -p -c '../chain.sh $(exe) $(cbmc) $(goto-instrument) $(is_windows)'
19+
20+
tests.log:
21+
@../test.pl -e -p -c '../chain.sh $(exe) $(cbmc) $(goto-instrument) $(is_windows)'
22+
23+
show:
24+
@for dir in *; do \
25+
if [ -d "$$dir" ]; then \
26+
vim -o "$$dir/*.c" "$$dir/*.out"; \
27+
fi; \
28+
done;
29+
30+
clean:
31+
@for dir in *; do \
32+
$(RM) tests.log; \
33+
if [ -d "$$dir" ]; then \
34+
cd "$$dir"; \
35+
$(RM) *.out *.gb; \
36+
cd ..; \
37+
fi \
38+
done
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
goto_cc=$1
4+
cbmc=$2
5+
goto_instrument=$3
6+
is_windows=$4
7+
8+
name=${*:$#}
9+
base_name=${name%.c}
10+
modified_name=${base_name}.modified.c
11+
12+
if [[ "${is_windows}" == "true" ]]; then
13+
"${goto_cc}" "${name}"
14+
mv "${base_name}.exe" "${base_name}.gb"
15+
else
16+
"${goto_cc}" "${name}" -o "${base_name}.gb"
17+
fi
18+
19+
"${goto_instrument}" "${base_name}.gb" --dump-c "${modified_name}"
20+
21+
"${cbmc}" "${modified_name}"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int main(void)
2+
{
3+
void *v, *x, *x_before;
4+
x_before = x;
5+
__atomic_store_n(&x, 42, 0);
6+
__atomic_exchange_n(&x, 42, 0);
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CORE
2+
test.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^PARSING ERROR$
9+
^CONVERSION ERROR$
10+
--
11+
Example of an illegal identifier name we crashed on before:
12+
__atomic_compare_exchange_n_*{V}$V$
13+
14+
Example of the substitutes we are producing now.
15+
__atomic_compare_exchange__ptr__start_sub_V_end_sub_

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,16 +1890,18 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
18901890
INVARIANT(
18911891
!parameters.empty(),
18921892
"GCC polymorphic built-ins should have at least one parameter");
1893+
/// XXX why are we only looking at the type of the first parameter?
18931894
if(parameters.front().type().id() == ID_pointer)
18941895
{
18951896
identifier_with_type =
18961897
id2string(identifier) + "_" +
1897-
type2name(parameters.front().type().subtype(), *this);
1898+
type2identifier(parameters.front().type().subtype(), *this);
18981899
}
18991900
else
19001901
{
1901-
identifier_with_type = id2string(identifier) + "_" +
1902-
type2name(parameters.front().type(), *this);
1902+
identifier_with_type =
1903+
id2string(identifier) + "_" +
1904+
type2identifier(parameters.front().type(), *this);
19031905
}
19041906
gcc_polymorphic->set_identifier(identifier_with_type);
19051907

src/ansi-c/type2name.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,32 @@ std::string type2name(const typet &type, const namespacet &ns)
272272
symbol_numbert symbol_number;
273273
return type2name(type, ns, symbol_number);
274274
}
275+
276+
/// type2name generates strings that aren't valid C identifiers
277+
/// If we want utilities like dump_c to work properly identifiers
278+
/// should ideally always be valid C identifiers
279+
/// This replaces some invalid characters that can appear in type2name output.
280+
std::string type2identifier(const typet &type, const namespacet &ns)
281+
{
282+
auto type2name_res = type2name(type, ns);
283+
std::string result{};
284+
for(char c : type2name_res)
285+
{
286+
switch(c)
287+
{
288+
case '*':
289+
result += "_ptr_";
290+
break;
291+
case '{':
292+
result += "_start_sub_";
293+
break;
294+
case '}':
295+
result += "_end_sub_";
296+
break;
297+
default:
298+
result += c;
299+
break;
300+
}
301+
}
302+
return result;
303+
}

src/ansi-c/type2name.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ class namespacet;
2020

2121
std::string type2name(const typet &type, const namespacet &ns);
2222

23+
std::string type2identifier(const typet &type, const namespacet &ns);
24+
2325
#endif // CPROVER_ANSI_C_TYPE2NAME_H

0 commit comments

Comments
 (0)