Skip to content

Commit ddfa9d3

Browse files
committed
Disable CFI for weakly linked syscalls
Currently, when enabling CFI via -Zsanitizer=cfi and executing e.g. std::sys::random::getrandom, we can observe a CFI violation. This is the case for all consumers of the std::sys::pal::weak::weak macro, as it is defining weak functions which don't show up in LLVM IR metadata. CFI fails for all these functions. Similar to other such cases in #115199, this change stops emitting the CFI typecheck for consumers of the macro via the \#[no_sanitize(cfi)] attribute.
1 parent fd17dea commit ddfa9d3

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

library/std/src/sys/pal/unix/fd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ impl FileDesc {
251251
}
252252

253253
#[cfg(all(target_os = "android", target_pointer_width = "32"))]
254+
#[no_sanitize(cfi)]
254255
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
255256
super::weak::weak!(fn preadv64(libc::c_int, *const libc::iovec, libc::c_int, off64_t) -> isize);
256257

library/std/src/sys/pal/unix/fs.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,18 @@ impl File {
14501450
Ok(())
14511451
}
14521452

1453+
#[cfg_attr(
1454+
any(
1455+
target_os = "android",
1456+
all(
1457+
target_os = "linux",
1458+
target_env = "gnu",
1459+
target_pointer_width = "32",
1460+
not(target_arch = "riscv32")
1461+
)
1462+
),
1463+
no_sanitize(cfi)
1464+
)]
14531465
pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
14541466
#[cfg(not(any(
14551467
target_os = "redox",

library/std/src/sys/pal/unix/process/process_unix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ impl Command {
432432
target_os = "nto",
433433
target_vendor = "apple",
434434
))]
435+
#[cfg_attr(target_os = "linux", no_sanitize(cfi))]
435436
fn posix_spawn(
436437
&mut self,
437438
stdio: &ChildPipes,

library/std/src/sys/pal/unix/thread.rs

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ impl Thread {
188188
}
189189

190190
#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto"))]
191+
#[no_sanitize(cfi)]
191192
pub fn set_name(name: &CStr) {
192193
weak! {
193194
fn pthread_setname_np(

library/std/src/sys/pal/unix/time.rs

+9
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ impl Timespec {
9696
}
9797
}
9898

99+
#[cfg_attr(
100+
all(
101+
target_os = "linux",
102+
target_env = "gnu",
103+
target_pointer_width = "32",
104+
not(target_arch = "riscv32")
105+
),
106+
no_sanitize(cfi)
107+
)]
99108
pub fn now(clock: libc::clockid_t) -> Timespec {
100109
use crate::mem::MaybeUninit;
101110
use crate::sys::cvt;

library/std/src/sys/pal/unix/weak.rs

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ unsafe fn fetch(name: &str) -> *mut libc::c_void {
144144
#[cfg(not(any(target_os = "linux", target_os = "android")))]
145145
pub(crate) macro syscall {
146146
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
147+
// FIXME: Rust currently omits weak function definitions
148+
// and its metadata from LLVM IR.
149+
#[no_sanitize(cfi)]
147150
unsafe fn $name($($arg_name: $t),*) -> $ret {
148151
weak! { fn $name($($t),*) -> $ret }
149152

0 commit comments

Comments
 (0)