generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 142
Handle copy on empty str/length zero data #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
avanhatt
merged 4 commits into
model-checking:main-152-2021-06-17
from
avanhatt:empty-str-memcpy
Jun 22, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Make sure we can handle explicit copy_nonoverlapping on empty string | ||
|
|
||
| #![feature(rustc_private)] | ||
|
|
||
| extern crate libc; | ||
|
|
||
| use std::mem::size_of; | ||
| use std::ptr::copy_nonoverlapping; | ||
| use std::slice::from_raw_parts; | ||
| use std::str; | ||
|
|
||
| fn copy_string(s: &str, l: usize) { | ||
| unsafe { | ||
| // Unsafe buffer | ||
| let size: libc::size_t = size_of::<u8>(); | ||
| let dest: *mut u8 = libc::malloc(size * l) as *mut u8; | ||
|
|
||
| // Copy | ||
| let src = from_raw_parts(s.as_ptr(), l).as_ptr(); | ||
| copy_nonoverlapping(src, dest, l); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| copy_string("x", 1); | ||
| copy_string("", 0); | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/test/cbmc/Strings/copy_empty_string_by_intrinsic_fixme.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Make sure we can handle explicit copy_nonoverlapping on empty string | ||
|
|
||
| // TODO: https://github.com/model-checking/rmc/issues/241 | ||
| // The copy_nonoverlapping succeeds, but the final copy back to a slice | ||
| // fails: | ||
| // [...copy_empty_string_by_intrinsic.assertion.2] line 1035 unreachable code: FAILURE | ||
| // [...copy_empty_string_by_intrinsic.assertion.1] line 1037 a panicking function std::result::unwrap_failed is invoked: FAILURE | ||
| // [...copy_string.assertion.2] line 28 assertion failed: dest_as_str.len() == l: FAILURE | ||
|
|
||
| #![feature(rustc_private)] | ||
|
|
||
| extern crate libc; | ||
|
|
||
| use std::mem::size_of; | ||
| use std::ptr::copy_nonoverlapping; | ||
| use std::slice::from_raw_parts; | ||
| use std::str; | ||
|
|
||
| fn copy_string(s: &str, l: usize) { | ||
| unsafe { | ||
| // Unsafe buffer | ||
| let size: libc::size_t = size_of::<u8>(); | ||
| let dest: *mut u8 = libc::malloc(size * l) as *mut u8; | ||
|
|
||
| // Copy | ||
| let src = from_raw_parts(s.as_ptr(), l).as_ptr(); | ||
| copy_nonoverlapping(src, dest, l); | ||
|
|
||
| // The chunk below causes the 3 failures at the top of the file | ||
| // Back to str, check length | ||
| let dest_slice: &[u8] = from_raw_parts(dest, l); | ||
| let dest_as_str: &str = str::from_utf8(dest_slice).unwrap(); | ||
| assert!(dest_as_str.len() == l); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| // Verification fails for both of these cases. | ||
| copy_string("x", 1); | ||
| copy_string("", 0); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Make sure we can handle implicit memcpy on the empty string | ||
|
|
||
| fn take_string_ref(s: &str, l: usize) { | ||
| assert!(s.len() == l) | ||
| } | ||
|
|
||
| fn main() { | ||
| take_string_ref(&"x".to_string(), 1); | ||
| take_string_ref(&"".to_string(), 0); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open an issue regarding these failures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, thanks!