Repository navigation
Replace the hook for realloc with a C implementation - #1088
Conversation
| @@ -1,2 +1,33 @@ | |||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | |||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | |||
|
|
|||
There was a problem hiding this comment.
Do we have a C formatter in place yet? If not, lets use one to ensure that we don't keep doing whitespace changes on these things
There was a problem hiding this comment.
Looks like you added one 7 months ago (scripts/run-clang-format.sh), and it caused CI to fail :) I fixed the formatting.
| // https://github.com/diffblue/cbmc/blob/94b81b22799876b52632ba9cdc3f486f87abadb8/src/ansi-c/library/stdlib.c#L472 | ||
| 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) |
There was a problem hiding this comment.
do we want to assert old_size == 0?
| } | ||
|
|
||
| uint8_t* result = malloc(new_size); | ||
| if (result) { |
There was a problem hiding this comment.
Wasn't there some weirdness where CBMC was more efficient if we explicitly tested against NULL @tautschnig
celinval
left a comment
There was a problem hiding this comment.
I have a conceptual question. Why can't this be implemented in rust instead?
| // This is a C implementation of the __rust_realloc function that has the following signature | ||
| // fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8; | ||
| // The implementation pretty much follows CBMC's implementation: | ||
| // https://github.com/diffblue/cbmc/blob/94b81b22799876b52632ba9cdc3f486f87abadb8/src/ansi-c/library/stdlib.c#L472 |
There was a problem hiding this comment.
Can you please clarify in the comments why we need our own?
| 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) { |
There was a problem hiding this comment.
should this be an assert?
|
|
||
| // if malloc-size is 0, free original object and return malloc(0), which in | ||
| // CBMC returns a pointer to a zero-sized object | ||
| if (new_size == 0) { |
There was a problem hiding this comment.
same here... assert (new_size > 0)
| } | ||
|
|
||
| uint8_t *result = malloc(new_size); | ||
| if (result) { |
There was a problem hiding this comment.
should we special case this if new_size == old_size?
There was a problem hiding this comment.
I haven't seen that done in any of the implementations I looked at. For performance, it seems desirable to check for this special case, but for verification purposes, I'm not sure if it somehow complicates the logic. Something to experiment with...
I made an attempt in https://github.com/zhassan-aws/kani/blob/realloc-stub/library/std/src/alloc.rs, but it didn't work because |
Let's explore that later. Thanks |
…1088) Co-authored-by: Daniel Schwartz-Narbonne
Co-authored-by: Daniel Schwartz-Narbonne
Description of changes:
Kani currently has a hook for the low-level
__rust_reallocfunction (see here) that calls CBMC's built-inrealloc. CBMC's implementation of realloc (see here) seems to be causing an issue when it is called via theRawVecdata structure, which is the underlying data structure forVec. This PR replaces the hook with a C implementation of__rust_realloc. Providing an implementation for__rust_reallocis desirable since the function has more parameters than the C version (the old size and the alignment) that should enable a more reliable and efficient implementation.Resolved issues:
Resolves #702
Call-outs:
It might be better to implement
__rust_reallocdirectly in goto as a hook, or in Rust in Kani'sstdoverrides library, but we're going with a C implementation for now since it's the easiest.Testing:
How is this change tested? Test that was fixed. Need to add more tests before turning this into a PR
Is this a refactor change? No
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.