Skip to content

Commit d7e4a35

Browse files
author
thk123
committed
Fixed null pointer related bug
We now allow finding a null pointer at any point in the removal process (previously we were just excepting it in an array of pointers). This fixes diffblue/cbmc-toyota#128 This slightly changes the behaviour in the event we can precisely resolve a null pointer. Previously we would have replaced the function pointer with a branch on all valid function pointers. Now we will (providing --pointer-check is enabled) simply assert false. This behaviour is more desirable - if we know a pointer is NULL then the previous if statements that were generated would all be ignored anyway, so this just reduces the size of the goto program with no impact on its behaviour. Pointers to not functions still cause a rejection, so tests that exibit this have been modified to require this.
1 parent e2049b0 commit d7e4a35

File tree

10 files changed

+28
-9
lines changed

10 files changed

+28
-9
lines changed

regression/goto-analyzer/approx-const-fp-array-variable-const-fp-with-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ main.c
55
^\s*IF fp == f2 THEN GOTO [0-9]$
66
^\s*IF fp == f3 THEN GOTO [0-9]$
77
^\s*IF fp == f4 THEN GOTO [0-9]$
8+
replacing function pointer by 3 possible targets
89
^SIGNAL=0$
910
--
1011
^warning: ignoring

regression/goto-analyzer/approx-const-fp-array-variable-struct-const-fp-with-zero/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KNOWNBUG
1+
CORE
22
main.c
33
--show-goto-functions --verbosity 10 --pointer-check
44
^Removing function pointers and virtual functions$

regression/goto-analyzer/no-match-array-literal-const-fp-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ main.c
44
^Removing function pointers and virtual functions$
55
^\s*ASSERT FALSE // invalid function pointer$
66
^SIGNAL=0$
7+
function func: replacing function pointer by 0 possible targets
78
--
89
^warning: ignoring

regression/goto-analyzer/no-match-const-fp-const-fp-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ main.c
44
^Removing function pointers and virtual functions$
55
^\s*ASSERT FALSE // invalid function pointer$
66
^SIGNAL=0$
7+
replacing function pointer by 0 possible targets
78
--
89
^warning: ignoring

regression/goto-analyzer/no-match-const-fp-const-pointer-const-struct-const-fp-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ main.c
33
--show-goto-functions --verbosity 10 --pointer-check
44
^Removing function pointers and virtual functions$
55
^\s*ASSERT FALSE // invalid function pointer$
6+
replacing function pointer by 9 possible targets
67
^SIGNAL=0$
78
--
89
^warning: ignoring

regression/goto-analyzer/no-match-const-fp-dereference-const-pointer-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ main.c
33
--show-goto-functions --verbosity 10 --pointer-check
44
^Removing function pointers and virtual functions$
55
^\s*ASSERT FALSE // invalid function pointer$
6+
replacing function pointer by 9 possible targets
67
^SIGNAL=0$
78
--
89
^warning: ignoring

regression/goto-analyzer/no-match-const-fp-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ main.c
33
--show-goto-functions --verbosity 10 --pointer-check
44
^Removing function pointers and virtual functions$
55
^\s*ASSERT FALSE // invalid function pointer$
6+
function func: replacing function pointer by 0 possible targets
67
--
78
^warning: ignoring

regression/goto-analyzer/no-match-const-struct-non-const-fp-null/test.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ main.c
44
^Removing function pointers and virtual functions$
55
^\s*ASSERT FALSE // invalid function pointer$
66
^SIGNAL=0$
7+
replacing function pointer by 0 possible targets
78
--
89
^warning: ignoring

src/goto-programs/remove_const_function_pointers.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ bool remove_const_function_pointerst::try_resolve_function_call(
164164
resolved=false;
165165
}
166166
}
167+
else if(simplified_expr.id()==ID_constant)
168+
{
169+
if(simplified_expr.is_zero())
170+
{
171+
// We have the null pointer - no need to throw everything away
172+
// but we don't add any functions either
173+
resolved=true;
174+
}
175+
else
176+
{
177+
LOG("Non-zero constant value found", simplified_expr);
178+
resolved=false;
179+
}
180+
}
167181
else
168182
{
169183
LOG("Unrecognised expression", simplified_expr);
@@ -555,10 +569,7 @@ bool remove_const_function_pointerst::try_resolve_index_of(
555569

556570
for(const exprt &resolved_array_entry : array_contents)
557571
{
558-
if(!resolved_array_entry.is_zero())
559-
{
560-
out_expressions.push_back(resolved_array_entry);
561-
}
572+
out_expressions.push_back(resolved_array_entry);
562573
}
563574
}
564575
}

src/goto-programs/remove_function_pointers.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,11 @@ void remove_function_pointerst::remove_function_pointer(
296296

297297
found_functions=fpr(pointer, functions);
298298

299-
// Either found_functions is true therefore the functions should not
300-
// be empty
301-
// Or found_functions is false therefore the functions should be empty
302-
assert(found_functions != functions.empty());
299+
// if found_functions is false, functions should be empty
300+
// however, it is possible for found_functions to be true and functions
301+
// to be empty (this happens if the pointer can only resolve to the null
302+
// pointer)
303+
assert(found_functions || functions.empty());
303304

304305
if(functions.size()==1)
305306
{

0 commit comments

Comments
 (0)