Skip to content

Replace the hook for realloc with a C implementation - #1088

Merged
zhassan-aws merged 10 commits into
model-checking:mainfrom
zhassan-aws:fix-realloc
Apr 22, 2022
Merged

zhassan-aws merged 10 commits into
model-checking:mainfrom
zhassan-aws:fix-realloc

Conversation

@zhassan-aws

Copy link
Copy Markdown
Contributor

Description of changes:

Kani currently has a hook for the low-level __rust_realloc function (see here) that calls CBMC's built-in realloc. CBMC's implementation of realloc (see here) seems to be causing an issue when it is called via the RawVec data structure, which is the underlying data structure for Vec. This PR replaces the hook with a C implementation of __rust_realloc. Providing an implementation for __rust_realloc is 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_realloc directly in goto as a hook, or in Rust in Kani's std overrides 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

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

Comment thread library/kani/kani_lib.c
@@ -1,2 +1,33 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you added one 7 months ago (scripts/run-clang-format.sh), and it caused CI to fail :) I fixed the formatting.

Comment thread library/kani/kani_lib.c Outdated
// 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to assert old_size == 0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread library/kani/kani_lib.c
}

uint8_t* result = malloc(new_size);
if (result) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't there some weirdness where CBMC was more efficient if we explicitly tested against NULL @tautschnig

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a conceptual question. Why can't this be implemented in rust instead?

Comment thread library/kani/kani_lib.c Outdated
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please clarify in the comments why we need our own?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread library/kani/kani_lib.c Outdated
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be an assert?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

Comment thread library/kani/kani_lib.c Outdated

// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here... assert (new_size > 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread library/kani/kani_lib.c
}

uint8_t *result = malloc(new_size);
if (result) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we special case this if new_size == old_size?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

@zhassan-aws

Copy link
Copy Markdown
Contributor Author

I have a conceptual question. Why can't this be implemented in rust instead?

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 is_null is a std library function that we're not linking :(

@zhassan-aws
zhassan-aws marked this pull request as ready for review April 22, 2022 02:05
@zhassan-aws
zhassan-aws requested a review from a team as a code owner April 22, 2022 02:05

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@celinval

Copy link
Copy Markdown
Contributor

I have a conceptual question. Why can't this be implemented in rust instead?

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 is_null is a std library function that we're not linking :(

Let's explore that later. Thanks

@zhassan-aws
zhassan-aws merged commit b8055fc into model-checking:main Apr 22, 2022
@zhassan-aws
zhassan-aws deleted the fix-realloc branch April 22, 2022 04:08
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 26, 2022
tedinski pushed a commit that referenced this pull request Apr 27, 2022
Co-authored-by:
Daniel Schwartz-Narbonne
@tautschnig tautschnig mentioned this pull request Jun 4, 2022
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bogus failures in simple vector example

3 participants