Skip to content

Commit 8911c0a

Browse files
author
Thomas Kiley
authored
Merge pull request #5290 from thk123/goto-harness-void-star-pointers
Add support for void* pointers in goto-harness
2 parents 5c1f7bb + 2f2a233 commit 8911c0a

File tree

9 files changed

+102
-6
lines changed

9 files changed

+102
-6
lines changed

regression/goto-harness/do_not_nondet_globals_by_default/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <assert.h>
2+
13
int a_global;
24

35
void entry_function(void)

regression/goto-harness/havoc-global-int-01/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <assert.h>
2+
13
int x = 1;
24

35
int main()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
#include <stdbool.h>
3+
4+
void test_function(void *input)
5+
{
6+
assert(input == 0);
7+
}
8+
9+
void test_void_array(void *input_array[10])
10+
{
11+
__CPROVER_assume(input_array != 0);
12+
assert(input_array[0] == 0);
13+
assert(false);
14+
}
15+
16+
void test_ptr_array(void **input_array)
17+
{
18+
__CPROVER_assume(input_array != 0);
19+
assert(input_array[0] == 0);
20+
assert(false);
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function test_ptr_array --treat-pointer-as-array input_array
4+
\[test_ptr_array\.assertion\.1\] line \d+ assertion input_array\[0\] == 0: SUCCESS
5+
\[test_ptr_array\.assertion\.2\] line \d+ assertion .*: FAILURE
6+
^VERIFICATION FAILED$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
--
10+
--
11+
Ensure the harness is able to initalize an array of void* pointers where
12+
the array is non-null, but the elements are null, if the void** is treated as an
13+
array.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function test_void_array
4+
\[test_void_array\.assertion\.1\] line \d+ assertion input_array\[0\] == 0: SUCCESS
5+
\[test_void_array\.assertion\.2\] line \d+ assertion .*: FAILURE
6+
^VERIFICATION FAILED$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
--
10+
--
11+
Ensure the harness is able to initalize an array of void* pointers where
12+
the array is non-null, but the elements are null.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function test_function --treat-pointer-as-array input
4+
\[test_function\.assertion\.1\] line \d+ assertion input == 0: SUCCESS
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
--
10+
Ensure the harness is able to initalize a void* ptr to a null value, even when
11+
the parameter is specified to be treated as an array.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function test_function --treat-pointer-as-cstring input
4+
\[test_function\.assertion\.1\] line \d+ assertion input == 0: SUCCESS
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
--
10+
Ensure the harness is able to initalize a void* ptr to a null value, even when
11+
the parameter is specified to be treated as a string.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function test_function
4+
\[test_function\.assertion\.1\] line \d+ assertion input == 0: SUCCESS
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
--
10+
Ensure the harness is able to initalize a void* ptr to a null value.

src/goto-harness/recursive_initialization.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ void recursive_initializationt::initialize(
230230
{depth, address_of_exprt{lhs}}});
231231
}
232232

233+
code_blockt build_null_pointer(const symbol_exprt &result_symbol)
234+
{
235+
const typet &type_to_construct = result_symbol.type().subtype();
236+
const null_pointer_exprt nullptr_expr{to_pointer_type(type_to_construct)};
237+
const code_assignt assign_null{dereference_exprt{result_symbol},
238+
nullptr_expr};
239+
return code_blockt{{assign_null, code_returnt{}}};
240+
}
241+
233242
code_blockt recursive_initializationt::build_constructor_body(
234243
const exprt &depth_symbol,
235244
const symbol_exprt &result_symbol,
@@ -249,6 +258,11 @@ code_blockt recursive_initializationt::build_constructor_body(
249258
{
250259
return build_function_pointer_constructor(result_symbol, is_nullable);
251260
}
261+
if(type.subtype().id() == ID_empty)
262+
{
263+
// always initalize void* pointers as NULL
264+
return build_null_pointer(result_symbol);
265+
}
252266
if(lhs_name.has_value())
253267
{
254268
if(should_be_treated_as_cstring(*lhs_name) && type == char_type())
@@ -620,15 +634,16 @@ code_blockt recursive_initializationt::build_pointer_constructor(
620634
PRECONDITION(result.type().id() == ID_pointer);
621635
const typet &type = result.type().subtype();
622636
PRECONDITION(type.id() == ID_pointer);
637+
PRECONDITION(type.subtype().id() != ID_empty);
623638

624639
code_blockt body{};
625-
626640
// build:
627641
// void type_constructor_ptr_T(int depth, T** result)
628642
// {
629643
// if(has_seen && depth >= max_depth)
630644
// *result=NULL;
631-
// else if(depth < min_null_tree_depth || nondet()) {
645+
// return
646+
// if(nondet()) {
632647
// size_t has_seen_prev;
633648
// has_seen_prev = T_has_seen;
634649
// T_has_seen = 1;
@@ -655,10 +670,8 @@ code_blockt recursive_initializationt::build_pointer_constructor(
655670
}
656671

657672
null_pointer_exprt nullptr_expr{pointer_type(type.subtype())};
658-
code_blockt null_and_return{};
659-
code_assignt assign_null{dereference_exprt{result}, nullptr_expr};
660-
null_and_return.add(assign_null);
661-
null_and_return.add(code_returnt{});
673+
const code_assignt assign_null{dereference_exprt{result}, nullptr_expr};
674+
code_blockt null_and_return{{assign_null, code_returnt{}}};
662675
body.add(code_ifthenelset{conjunction(should_not_recurse), null_and_return});
663676

664677
exprt::operandst should_recurse_ops{
@@ -787,6 +800,7 @@ code_blockt recursive_initializationt::build_dynamic_array_constructor(
787800
const typet &pointer_type = result.type().subtype();
788801
PRECONDITION(pointer_type.id() == ID_pointer);
789802
const typet &element_type = pointer_type.subtype();
803+
PRECONDITION(element_type.id() != ID_empty);
790804

791805
// builds:
792806
// void type_constructor_ptr_T(int depth, T** result, int* size)

0 commit comments

Comments
 (0)