Skip to content

Commit 7b45674

Browse files
committed
Disable CFI for core and std CFI violations
Works around #115199 by temporarily disabling CFI for core and std CFI violations to allow the user rebuild and use both core and std with CFI enabled using the Cargo build-std feature.
1 parent 2ffeb46 commit 7b45674

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

library/core/src/fmt/rt.rs

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ impl<'a> Argument<'a> {
133133
Self::new(x, USIZE_MARKER)
134134
}
135135

136+
// FIXME: Transmuting formatter in new and indirectly branching to/calling
137+
// it here is an explicit CFI violation.
138+
#[allow(inline_no_sanitize)]
139+
#[no_sanitize(cfi, kcfi)]
136140
#[inline(always)]
137141
pub(super) fn fmt(&self, f: &mut Formatter<'_>) -> Result {
138142
(self.formatter)(self.value, f)

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
#![feature(negative_impls)]
239239
#![feature(never_type)]
240240
#![feature(no_core)]
241+
#![feature(no_sanitize)]
241242
#![feature(platform_intrinsics)]
242243
#![feature(prelude_import)]
243244
#![feature(repr_simd)]

library/std/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
#![feature(allow_internal_unstable)]
271271
#![feature(c_unwind)]
272272
#![feature(cfg_target_thread_local)]
273+
#![feature(cfi_encoding)]
273274
#![feature(concat_idents)]
274275
#![feature(const_mut_refs)]
275276
#![feature(const_trait_impl)]
@@ -292,6 +293,7 @@
292293
#![feature(needs_panic_runtime)]
293294
#![feature(negative_impls)]
294295
#![feature(never_type)]
296+
#![feature(no_sanitize)]
295297
#![feature(platform_intrinsics)]
296298
#![feature(prelude_import)]
297299
#![feature(rustc_attrs)]

library/std/src/sys/unix/thread_local_dtor.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,47 @@
1111
// Note, however, that we run on lots older linuxes, as well as cross
1212
// compiling from a newer linux to an older linux, so we also have a
1313
// fallback implementation to use as well.
14+
#[allow(unexpected_cfgs)]
1415
#[cfg(any(target_os = "linux", target_os = "fuchsia", target_os = "redox", target_os = "hurd"))]
16+
// FIXME: The Rust compiler currently omits weakly function definitions (i.e.,
17+
// __cxa_thread_atexit_impl) and its metadata from LLVM IR.
18+
#[no_sanitize(cfi, kcfi)]
1519
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
1620
use crate::mem;
1721
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
1822

23+
/// This is necessary because the __cxa_thread_atexit_impl implementation
24+
/// std links to by default may be a C or C++ implementation that was not
25+
/// compiled using the Clang integer normalization option.
26+
#[cfg(not(sanitizer_cfi_normalize_integers))]
27+
#[cfi_encoding = "i"]
28+
#[repr(transparent)]
29+
pub struct c_int(pub libc::c_int);
30+
1931
extern "C" {
2032
#[linkage = "extern_weak"]
2133
static __dso_handle: *mut u8;
2234
#[linkage = "extern_weak"]
23-
static __cxa_thread_atexit_impl: *const libc::c_void;
35+
static __cxa_thread_atexit_impl: Option<
36+
extern "C" fn(
37+
unsafe extern "C" fn(*mut libc::c_void),
38+
*mut libc::c_void,
39+
*mut libc::c_void,
40+
) -> c_int,
41+
>;
2442
}
25-
if !__cxa_thread_atexit_impl.is_null() {
26-
type F = unsafe extern "C" fn(
27-
dtor: unsafe extern "C" fn(*mut u8),
28-
arg: *mut u8,
29-
dso_handle: *mut u8,
30-
) -> libc::c_int;
31-
mem::transmute::<*const libc::c_void, F>(__cxa_thread_atexit_impl)(
32-
dtor,
33-
t,
34-
&__dso_handle as *const _ as *mut _,
35-
);
43+
44+
if let Some(f) = __cxa_thread_atexit_impl {
45+
unsafe {
46+
f(
47+
mem::transmute::<
48+
unsafe extern "C" fn(*mut u8),
49+
unsafe extern "C" fn(*mut libc::c_void),
50+
>(dtor),
51+
t.cast(),
52+
&__dso_handle as *const _ as *mut _,
53+
);
54+
}
3655
return;
3756
}
3857
register_dtor_fallback(t, dtor);

0 commit comments

Comments
 (0)