Skip to content

Commit d3e3b90

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 aa5dbee commit d3e3b90

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-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)]
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
@@ -229,6 +229,7 @@
229229
#![feature(never_type)]
230230
#![feature(no_core)]
231231
#![feature(no_coverage)] // rust-lang/rust#84605
232+
#![feature(no_sanitize)]
232233
#![feature(platform_intrinsics)]
233234
#![feature(prelude_import)]
234235
#![feature(repr_simd)]

library/core/src/ops/function.rs

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ mod impls {
280280
{
281281
type Output = F::Output;
282282

283+
// FIXME: Rust's "try catch" construct (i.e., std::panicking::r#try) use
284+
// of FnOnce explicitly violates CFI.
285+
#[no_sanitize(cfi)]
283286
extern "rust-call" fn call_once(self, args: A) -> F::Output {
284287
(*self).call(args)
285288
}

library/std/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
#![feature(allow_internal_unstable)]
246246
#![feature(c_unwind)]
247247
#![feature(cfg_target_thread_local)]
248+
#![feature(cfi_encoding)]
248249
#![feature(concat_idents)]
249250
#![feature(const_mut_refs)]
250251
#![feature(const_trait_impl)]
@@ -267,6 +268,7 @@
267268
#![feature(needs_panic_runtime)]
268269
#![feature(negative_impls)]
269270
#![feature(never_type)]
271+
#![feature(no_sanitize)]
270272
#![feature(platform_intrinsics)]
271273
#![feature(prelude_import)]
272274
#![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"))]
1516
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
1617
use crate::mem;
1718
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
1819

20+
/// This is necessary because the __cxa_thread_atexit_impl implementation
21+
/// std links to by default may be a C or C++ implementation that was not
22+
/// compiled using the Clang integer normalization option.
23+
#[cfg(not(sanitizer_cfi_normalize_integers))]
24+
#[cfi_encoding = "i"]
25+
#[repr(transparent)]
26+
pub struct c_int(pub libc::c_int);
27+
1928
extern "C" {
2029
#[linkage = "extern_weak"]
2130
static __dso_handle: *mut u8;
2231
#[linkage = "extern_weak"]
23-
static __cxa_thread_atexit_impl: *const libc::c_void;
32+
fn __cxa_thread_atexit_impl(
33+
dtor: unsafe extern "C" fn(*mut libc::c_void),
34+
arg: *mut libc::c_void,
35+
dso_handle: *mut libc::c_void,
36+
) -> c_int;
37+
}
38+
39+
// __cxa_thread_atexit_impl may be null because of the extern_weak linkage
40+
fn is_null(f: *mut libc::c_void) -> bool {
41+
f.is_null()
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 !is_null(__cxa_thread_atexit_impl as *mut libc::c_void) {
45+
unsafe {
46+
__cxa_thread_atexit_impl(
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)