|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | + |
| 4 | +// Make sure we can handle explicit copy_nonoverlapping on empty string |
| 5 | + |
| 6 | +// TODO: https://github.com./model-checking/rmc/issues/241 |
| 7 | +// The copy_nonoverlapping succeeds, but the final copy back to a slice |
| 8 | +// fails: |
| 9 | +// [...copy_empty_string_by_intrinsic.assertion.2] line 1035 unreachable code: FAILURE |
| 10 | +// [...copy_empty_string_by_intrinsic.assertion.1] line 1037 a panicking function std::result::unwrap_failed is invoked: FAILURE |
| 11 | +// [...copy_string.assertion.2] line 28 assertion failed: dest_as_str.len() == l: FAILURE |
| 12 | + |
| 13 | +#![feature(rustc_private)] |
| 14 | + |
| 15 | +extern crate libc; |
| 16 | + |
| 17 | +use std::mem::size_of; |
| 18 | +use std::ptr::copy_nonoverlapping; |
| 19 | +use std::slice::from_raw_parts; |
| 20 | +use std::str; |
| 21 | + |
| 22 | +fn copy_string(s: &str, l: usize) { |
| 23 | + unsafe { |
| 24 | + // Unsafe buffer |
| 25 | + let size: libc::size_t = size_of::<u8>(); |
| 26 | + let dest: *mut u8 = libc::malloc(size * l) as *mut u8; |
| 27 | + |
| 28 | + // Copy |
| 29 | + let src = from_raw_parts(s.as_ptr(), l).as_ptr(); |
| 30 | + copy_nonoverlapping(src, dest, l); |
| 31 | + |
| 32 | + // The chunk below causes the 3 failures at the top of the file |
| 33 | + // Back to str, check length |
| 34 | + let dest_slice: &[u8] = from_raw_parts(dest, l); |
| 35 | + let dest_as_str: &str = str::from_utf8(dest_slice).unwrap(); |
| 36 | + assert!(dest_as_str.len() == l); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn main() { |
| 41 | + // Verification fails for both of these cases. |
| 42 | + copy_string("x", 1); |
| 43 | + copy_string("", 0); |
| 44 | +} |
0 commit comments