You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I expected to see this happen: Verification successful
Instead, this happened: All four assertions failed:
[main.assertion.1] line 8 assertion failed: v[0] == 0: FAILURE
[main.assertion.2] line 9 assertion failed: v[1] == 0: FAILURE
[main.assertion.3] line 10 assertion failed: v[2] == 0: FAILURE
[main.assertion.4] line 11 assertion failed: v[3] == 0: FAILURE
** 4 of 545 failed (2 iterations)
VERIFICATION FAILED
If I remove one of the push calls, verification passes:
[main.assertion.1] line 8 assertion failed: v[0] == 0: SUCCESS
[main.assertion.2] line 9 assertion failed: v[1] == 0: SUCCESS
[main.assertion.3] line 10 assertion failed: v[2] == 0: SUCCESS
[main.assertion.4] line 11 assertion failed: v[3] == 0: SUCCESS
** 0 of 545 failed (1 iterations)
VERIFICATION SUCCESSFUL
I suspect this is a bug in reallocation; if I print the vector's capacity after each push, I see that it changes from 0 to 4 after the first push (initial allocation) and changes from 4 to 8 after the last push (reallocation).
<snip>
SUMMARY:
** 1 of 511 failed
Failed Checks: assertion failed: v[0] == 5
File: "/home/ubuntu/examples/debug/iss702/no_unwrap/vecdeque/test.rs", line 10, in main
If I remove any of the 3 reserve statements, the assertion passes.
@tautschnig: @danielsn and I wrote a C implementation of the low-level __rust_realloc that is very similar to the CBMC one:
uint8_t* __rust_realloc(uint8_t* ptr, size_t old_size, size_t align, size_t new_size) {
// if current ptr is NULL, this behaves like malloc
if (ptr == 0)
return malloc(new_size);
// if malloc-size is 0, free original object and return malloc(0) which
// returns an invalid pointer
if (new_size == 0) {
free(ptr);
return malloc(0);
}
uint8_t* result = malloc(new_size);
if (result) {
size_t bytes_to_copy = new_size < old_size ? new_size : old_size;
memcpy(result, ptr, bytes_to_copy);
free(ptr);
}
return result;
}
The two differences w.r.t the CBMC version are:
It has access to the new and old sizes, so it passes the right size to copy to memcpy
When new_size == 0, it returns malloc(0) and not malloc(1). Interestingly, if I change it to return malloc(1), verification fails on the examples above, so I'm suspecting that the malloc(1) is somehow causing an issue. We might want to try changing the corresponding line in the CBMC implementation of realloc to malloc(0) instead (which if I understand correctly, returns an invalid pointer), and check if it fixes the bug.
Activity
tautschnig commentedon Dec 16, 2021
The following C variant thereof seems to be working fine:
[-]Bogs failures in simple vector example[/-][+]Bogus failures in simple vector example[/+]zhassan-aws commentedon Dec 17, 2021
The following Rust program that uses
libc::reallocdirectly works correctly with RMC:Output:
zhassan-aws commentedon Dec 17, 2021
And the following one that uses
std::alloc::reallocdirectly also passes with RMC:Output:
danielsn commentedon Feb 10, 2022
Using
v.reserve()to explicitly do the realloc also seems to workdanielsn commentedon Feb 10, 2022
But this fails
danielsn commentedon Feb 10, 2022
Even smaller reproduce
zhassan-aws commentedon Apr 6, 2022
I was able to reproduce a similar bug with
VecDeque:running with
kani test.rsproduces:If I remove any of the 3 reserve statements, the assertion passes.
zhassan-aws commentedon Apr 20, 2022
@tautschnig: @danielsn and I wrote a C implementation of the low-level
__rust_reallocthat is very similar to the CBMC one:The two differences w.r.t the CBMC version are:
memcpynew_size == 0, it returnsmalloc(0)and notmalloc(1). Interestingly, if I change it to returnmalloc(1), verification fails on the examples above, so I'm suspecting that themalloc(1)is somehow causing an issue. We might want to try changing the corresponding line in the CBMC implementation ofrealloctomalloc(0)instead (which if I understand correctly, returns an invalid pointer), and check if it fixes the bug.