Skip to content

Commit 02bb2d4

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 rust-lang#115199, this change stops emitting the CFI typecheck for consumers of the macro via the \#[no_sanitize(cfi)] attribute.
1 parent 2c6a12e commit 02bb2d4

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

Diff for: library/std/src/sys/fs/unix.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,18 @@ impl File {
14541454
Ok(())
14551455
}
14561456

1457+
#[cfg_attr(
1458+
any(
1459+
target_os = "android",
1460+
all(
1461+
target_os = "linux",
1462+
target_env = "gnu",
1463+
target_pointer_width = "32",
1464+
not(target_arch = "riscv32")
1465+
)
1466+
),
1467+
no_sanitize(cfi)
1468+
)]
14571469
pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
14581470
#[cfg(not(any(
14591471
target_os = "redox",

Diff for: 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

Diff for: library/std/src/sys/pal/unix/process/process_unix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ impl Command {
434434
target_os = "nto",
435435
target_vendor = "apple",
436436
))]
437+
#[cfg_attr(target_os = "linux", no_sanitize(cfi))]
437438
fn posix_spawn(
438439
&mut self,
439440
stdio: &ChildPipes,

Diff for: 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(

Diff for: 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;

Diff for: 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)