Skip to content

Commit 2498ceb

Browse files
committed
WIP attempt to implement user_fpsimd_struct.
Potentially fixes #2524, see the comments in the patch for details.
1 parent 1708299 commit 2498ceb

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

libc-test/semver/android-aarch64.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ SYS_syscalls
1212
SYS_fcntl
1313
__system_property_wait
1414
user_regs_struct
15+
user_fpsimd_struct

src/fixed_width_ints.rs

+72
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,75 @@ pub type uint16_t = u16;
1818
pub type uint32_t = u32;
1919
#[deprecated(since = "0.2.55", note = "Use u64 instead.")]
2020
pub type uint64_t = u64;
21+
22+
23+
/// This module introduces partial support for FFI with __int128 and
24+
/// equivalent types on platforms where Rust's definition is validated
25+
/// to match the standard C ABI of that platform.
26+
///
27+
/// Rust does not guarantee u128/i128 are sound for FFI, and its
28+
/// definitions are in fact known to be incompatible. [0]
29+
///
30+
/// However these problems aren't fundamental, and are just platform
31+
/// inconsistencies. Specifically at the time of this writing:
32+
///
33+
/// * For x64 SysV ABIs (everything but Windows), the types are underaligned.
34+
/// * For all Windows ABIs, Microsoft doesn't actually officially define __int128,
35+
/// and as a result different implementations don't actually agree on its ABI.
36+
///
37+
/// But on the major aarch64 platforms (android, macos, ios) we have validated
38+
/// that rustc has the right ABI for these types. This is important because
39+
/// aarch64 uses these types in some fundamental OS types like user_fpsimd_struct,
40+
/// which represents saved simd registers.
41+
///
42+
/// Any API which uses these types will need to `#[ignore(improper_ctypes)]`
43+
/// until the upstream rust issue is resolved, but this at least lets us make
44+
/// progress on platforms where this type is important.
45+
///
46+
/// The supported architectures and OSes is intentionally very restricted,
47+
/// as careful work needs to be done to verify that a particular platform
48+
/// has a conformant ABI.
49+
///
50+
/// [0]: https://github.com/rust-lang/rust/issues/54341
51+
mod supported_128 {
52+
cfg_if! {
53+
if #[cfg(all(target_arch = "aarch64",
54+
any(target_os = "android", target_os="macos", target_os = "ios")))] {
55+
56+
/// C `__int128` (a GCC extension that's part of many ABIs)
57+
pub type __int128 = i128;
58+
/// C `unsigned __int128` (a GCC extension that's part of many ABIs)
59+
pub type __uint128 = u128;
60+
/// C __int128_t (alternate name for [__int128][])
61+
pub type __int128_t = i128;
62+
/// C __uint128_t (alternate name for [__uint128][])
63+
pub type __uint128_t = u128;
64+
65+
// NOTE: if you add more platforms to here, you made need to cfg
66+
// these consts. They should always match the platform's values
67+
// for `sizeof(__int128)` and `_Alignof(__int128)`.
68+
const SIZE_128: usize = 16;
69+
const ALIGN_128: usize = 16;
70+
71+
/// Since Rust doesn't officially guarantee that these types
72+
/// have compatible ABIs, we const assert that these values have the
73+
/// known size/align of the target platform's libc. If rustc ever
74+
/// tries to regress things, it will cause a compilation error.
75+
const _ASSERT_128_COMPAT: () = {
76+
assert!(std::mem::size_of::<__int128>() == SIZE_128);
77+
assert!(std::mem::align_of::<__int128>() == ALIGN_128);
78+
79+
assert!(std::mem::size_of::<__uint128>() == SIZE_128);
80+
assert!(std::mem::align_of::<__uint128>() == ALIGN_128);
81+
82+
assert!(std::mem::size_of::<__int128_t>() == SIZE_128);
83+
assert!(std::mem::align_of::<__int128_t>() == ALIGN_128);
84+
85+
assert!(std::mem::size_of::<__uint128_t>() == SIZE_128);
86+
assert!(std::mem::align_of::<__uint128_t>() == ALIGN_128);
87+
};
88+
}
89+
}
90+
}
91+
92+
pub use self::supported_128::*;

src/unix/bsd/apple/b64/aarch64/align.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ s! {
3939
pub __pad: u32,
4040
}
4141

42-
#[repr(align(16))]
4342
pub struct __darwin_arm_neon_state64 {
44-
pub __v: [[u64; 2]; 32],
43+
pub __v: [__uint128_t; 32],
4544
pub __fpsr: u32,
4645
pub __fpcr: u32,
4746
}

src/unix/linux_like/android/b64/aarch64/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ s! {
5555
pub pc: u64,
5656
pub pstate: u64,
5757
}
58+
59+
pub struct user_fpsimd_struct {
60+
pub vregs: [__uint128_t; 32],
61+
pub fpsr: u32,
62+
pub fpcr: u32,
63+
}
5864
}
5965

6066
pub const O_DIRECT: ::c_int = 0x10000;

0 commit comments

Comments
 (0)