Skip to content

Commit 0fbabca

Browse files
Merge pull request #5220 from karkhaz/kk-fix-ignored-return-contract
Revamp function contracts command line interface
2 parents ef623c2 + 28900c7 commit 0fbabca

File tree

19 files changed

+487
-85
lines changed

19 files changed

+487
-85
lines changed

regression/contracts/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if(WIN32)
2-
set(is_windows true)
2+
set(is_windows true)
33
else()
4-
set(is_windows false)
4+
set(is_windows false)
55
endif()
66

77
add_test_pl_tests(

regression/contracts/function_apply_01/main.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
#include <assert.h>
88

9-
int foo()
10-
__CPROVER_ensures(__CPROVER_return_value == 0)
9+
int foo() __CPROVER_ensures(__CPROVER_return_value == 0)
1110
{
1211
return 1;
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
KNOWNBUG
1+
CORE
22
main.c
3-
--apply-code-contracts
3+
--replace-all-calls-with-contracts
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$
77
--
8+
^warning: ignoring
89
--
9-
Applying code contracts currently also checks them.
10+
This code is purposely unsound (the function does not abide by its
11+
contract). Verifying the function in isolation should fail, and
12+
verifying its caller should succeed.

regression/contracts/function_check_01/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
KNOWNBUG
22
main.c
33
--apply-code-contracts
44
^EXIT=0$

regression/contracts/function_check_04/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
KNOWNBUG
22
main.c
33
--apply-code-contracts
44
^EXIT=10$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// function_apply_01
2+
3+
// Note that this test is supposed to have an incorrect contract.
4+
// We verify that applying (without checking) the contract yields success,
5+
// and that checking the contract yields failure.
6+
7+
#include <assert.h>
8+
9+
int foo() __CPROVER_ensures(__CPROVER_return_value == 0)
10+
{
11+
return 1;
12+
}
13+
14+
int main()
15+
{
16+
int x = foo();
17+
assert(x == 0);
18+
return 0;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main.assertion.1\] line 17 assertion x == 0: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring
10+
--
11+
We don't actually replace the function call with its contract here, so
12+
CBMC should notice that the program is unsound.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdlib.h>
2+
3+
int get_at_idx(int const *const arr, const size_t len, const size_t idx)
4+
__CPROVER_requires(__CPROVER_r_ok(arr, len) && idx < len)
5+
__CPROVER_ensures(__CPROVER_return_value == arr[idx])
6+
{
7+
return arr[idx];
8+
}
9+
10+
void main()
11+
{
12+
int a[5] = {0};
13+
get_at_idx(a, 5, 3);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--replace-all-calls-with-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
This test exposes a bug where CBMC would crash on a program where a function
11+
with a return value has a postcondition that mentions __CPROVER_return_value,
12+
but the caller does not assign the return value to anything.

regression/contracts/invar_check_01/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
KNOWNBUG
22
main.c
33
--apply-code-contracts
44
^EXIT=0$

regression/contracts/invar_check_02/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
KNOWNBUG
22
main.c
33
--apply-code-contracts
44
^EXIT=0$

regression/contracts/invar_check_03/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
KNOWNBUG
22
main.c
33
--apply-code-contracts
44
^EXIT=0$

regression/contracts/invar_check_04/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
KNOWNBUG
22
main.c
33
--apply-code-contracts
44
^EXIT=10$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
int get_at_idx(int const *const arr, const size_t len, const size_t idx)
5+
__CPROVER_requires(__CPROVER_r_ok(arr, len) && idx < len)
6+
__CPROVER_ensures(__CPROVER_return_value == arr[idx])
7+
{
8+
return arr[idx];
9+
}
10+
11+
void main()
12+
{
13+
int a[5] = {0};
14+
a[3] = 7;
15+
int x = get_at_idx(a, 5, 3);
16+
assert(x == 7);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--replace-all-calls-with-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--

0 commit comments

Comments
 (0)