forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle copy on empty str/length zero data (rust-lang#235)
For consistency with LLVM semantics, only codegen an implicit memcpy when the length is nonzero.
- Loading branch information
Showing
6 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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); | ||
} |