-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(
c2rust-analyze
) Support ptr-to-ptr casts between safely transmutab…
…le types, for now limited to same-sized integers (#839) - Fixes #840 This introduces the concept of equivalent/compatible/safely transmutable types: https://github.com/immunant/c2rust/blob/2915b8d0c71add21dee1f9d540958ea863478212/c2rust-analyze/src/util.rs#L356-L380 Thus, we can now allow ptr-to-ptr casts between safely transmutable pointee types, whereas previously they were only allowed for equal types. In particular, this enables support for string casts, which are produced by `c2rust transpile` as `b"" as *const u8 as *const core::ffi::c_char`, where `c_char = i8`. Thus, this fixes #840. New tests are added in `string_casts.rs` to cover various ptr casts, though some of them crash in the rewriter due to having implicitly inserted MIR statements like implicit `&raw`s, which are inserted with `addr_of!`s. Thus, for some of these (where it works), there are versions with explicit `addr_of!`s that succeed end-to-end.
- Loading branch information
Showing
4 changed files
with
166 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
#[cfg(any())] | ||
pub fn cast_only(s: *const u8) { | ||
pub fn cast_ptr_to_ptr(s: *const u8) { | ||
s as *const core::ffi::c_char; | ||
} | ||
|
||
pub fn deep_cast_ptr_to_ptr(x: *const *const u8) { | ||
x as *const *const i8; | ||
} | ||
|
||
/// For the below disabled (`#[cfg(any())]`ed) tests, they currently crash in the rewriter | ||
/// due to it not being able to handle implicitly inserted `&raw` MIR statements yet. | ||
/// Thus, they also have `*_explicit` versions where | ||
/// a `std::ptr::addr_of!` is used to make the `&raw` explicit. | ||
/// | ||
/// Also note that `addr_of!` (with a `use std::ptr::addr_of`) | ||
/// and `::core::ptr::addr_of!` don't work either, | ||
/// though `std::ptr::addr_of`, `::std::ptr::addr_of!`, | ||
/// and `core::ptr::addr_of!` do work. | ||
|
||
#[cfg(any())] | ||
pub fn cast_array_to_ptr(s: &[u8; 1]) { | ||
s as *const u8; | ||
} | ||
|
||
pub fn cast_array_to_ptr_explicit(s: &[u8; 1]) { | ||
std::ptr::addr_of!(*s) as *const u8; | ||
} | ||
|
||
#[cfg(any())] | ||
pub fn cast_from_literal() { | ||
b"" as *const u8 as *const core::ffi::c_char; | ||
b"\0" as *const u8 as *const core::ffi::c_char; | ||
} | ||
|
||
pub fn cast_from_literal_explicit() { | ||
std::ptr::addr_of!(*b"\0") as *const u8 as *const core::ffi::c_char; | ||
} |
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