Skip to content

Commit 8269be1

Browse files
committed
Auto merge of rust-lang#129398 - matthiaskrgr:rollup-50l01ry, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#128432 (WASI: forbid `unsafe_op_in_unsafe_fn` for `std::{os, sys}`) - rust-lang#129373 (Add missing module flags for CFI and KCFI sanitizers) - rust-lang#129374 (Use `assert_unsafe_precondition!` in `AsciiChar::digit_unchecked`) - rust-lang#129376 (Change `assert_unsafe_precondition` docs to refer to `check_language_ub`) - rust-lang#129382 (Add `const_cell_into_inner` to `OnceCell`) - rust-lang#129387 (Advise against removing the remaining Python scripts from `tests/run-make`) - rust-lang#129388 (Do not rely on names to find lifetimes.) - rust-lang#129395 (Pretty-print own args of existential projections (dyn-Trait w/ GAT constraints)) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 739b1fd + 9d39b59 commit 8269be1

40 files changed

+204
-85
lines changed

Diff for: compiler/rustc_codegen_llvm/src/context.rs

+31
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_data_structures::base_n::{ToBaseN, ALPHANUMERIC_ONLY};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::small_c_str::SmallCStr;
1313
use rustc_hir::def_id::DefId;
14+
use rustc_middle::middle::codegen_fn_attrs::PatchableFunctionEntry;
1415
use rustc_middle::mir::mono::CodegenUnit;
1516
use rustc_middle::ty::layout::{
1617
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, LayoutError, LayoutOfHelpers,
@@ -226,6 +227,20 @@ pub unsafe fn create_module<'ll>(
226227
}
227228
}
228229

230+
// If we're normalizing integers with CFI, ensure LLVM generated functions do the same.
231+
// See https://github.com/llvm/llvm-project/pull/104826
232+
if sess.is_sanitizer_cfi_normalize_integers_enabled() {
233+
let cfi_normalize_integers = c"cfi-normalize-integers".as_ptr().cast();
234+
unsafe {
235+
llvm::LLVMRustAddModuleFlagU32(
236+
llmod,
237+
llvm::LLVMModFlagBehavior::Override,
238+
cfi_normalize_integers,
239+
1,
240+
);
241+
}
242+
}
243+
229244
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
230245
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
231246
let enable_split_lto_unit = c"EnableSplitLTOUnit".as_ptr();
@@ -245,6 +260,22 @@ pub unsafe fn create_module<'ll>(
245260
unsafe {
246261
llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
247262
}
263+
264+
// Add "kcfi-offset" module flag with -Z patchable-function-entry (See
265+
// https://reviews.llvm.org/D141172).
266+
let pfe =
267+
PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry);
268+
if pfe.prefix() > 0 {
269+
let kcfi_offset = c"kcfi-offset".as_ptr().cast();
270+
unsafe {
271+
llvm::LLVMRustAddModuleFlagU32(
272+
llmod,
273+
llvm::LLVMModFlagBehavior::Override,
274+
kcfi_offset,
275+
pfe.prefix().into(),
276+
);
277+
}
278+
}
248279
}
249280

250281
// Control Flow Guard is currently only supported by the MSVC linker on Windows.

Diff for: compiler/rustc_middle/src/ty/print/pretty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3119,7 +3119,10 @@ define_print! {
31193119

31203120
ty::ExistentialProjection<'tcx> {
31213121
let name = cx.tcx().associated_item(self.def_id).name;
3122-
p!(write("{} = ", name), print(self.term))
3122+
// The args don't contain the self ty (as it has been erased) but the corresp.
3123+
// generics do as the trait always has a self ty param. We need to offset.
3124+
let args = &self.args[cx.tcx().generics_of(self.def_id).parent_count - 1..];
3125+
p!(path_generic_args(|cx| write!(cx, "{name}"), args), " = ", print(self.term))
31233126
}
31243127

31253128
ty::ProjectionPredicate<'tcx> {

Diff for: compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -1099,16 +1099,8 @@ fn msg_span_from_named_region<'tcx>(
10991099
) -> (String, Option<Span>) {
11001100
match *region {
11011101
ty::ReEarlyParam(br) => {
1102-
let scope = tcx
1103-
.parent(tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id)
1104-
.expect_local();
1105-
let span = if let Some(param) =
1106-
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
1107-
{
1108-
param.span
1109-
} else {
1110-
tcx.def_span(scope)
1111-
};
1102+
let param_def_id = tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id;
1103+
let span = tcx.def_span(param_def_id);
11121104
let text = if br.has_name() {
11131105
format!("the lifetime `{}` as defined here", br.name)
11141106
} else {
@@ -1124,16 +1116,8 @@ fn msg_span_from_named_region<'tcx>(
11241116
("the anonymous lifetime defined here".to_string(), Some(ty.span))
11251117
} else {
11261118
match fr.bound_region {
1127-
ty::BoundRegionKind::BrNamed(_, name) => {
1128-
let span = if let Some(param) = tcx
1129-
.hir()
1130-
.get_generics(generic_param_scope)
1131-
.and_then(|generics| generics.get_named(name))
1132-
{
1133-
param.span
1134-
} else {
1135-
tcx.def_span(generic_param_scope)
1136-
};
1119+
ty::BoundRegionKind::BrNamed(param_def_id, name) => {
1120+
let span = tcx.def_span(param_def_id);
11371121
let text = if name == kw::UnderscoreLifetime {
11381122
"the anonymous lifetime as defined here".to_string()
11391123
} else {

Diff for: library/core/src/ascii/ascii_char.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! suggestions from rustc if you get anything slightly wrong in here, and overall
44
//! helps with clarity as we're also referring to `char` intentionally in here.
55
6-
use crate::fmt;
76
use crate::mem::transmute;
7+
use crate::{assert_unsafe_precondition, fmt};
88

99
/// One of the 128 Unicode characters from U+0000 through U+007F,
1010
/// often known as the [ASCII] subset.
@@ -497,14 +497,18 @@ impl AsciiChar {
497497
/// Notably, it should not be expected to return hex digits, or any other
498498
/// reasonable extension of the decimal digits.
499499
///
500-
/// (This lose safety condition is intended to simplify soundness proofs
500+
/// (This loose safety condition is intended to simplify soundness proofs
501501
/// when writing code using this method, since the implementation doesn't
502502
/// need something really specific, not to make those other arguments do
503503
/// something useful. It might be tightened before stabilization.)
504504
#[unstable(feature = "ascii_char", issue = "110998")]
505505
#[inline]
506506
pub const unsafe fn digit_unchecked(d: u8) -> Self {
507-
debug_assert!(d < 10);
507+
assert_unsafe_precondition!(
508+
check_language_ub,
509+
"`AsciiChar::digit_unchecked` input cannot exceed 9.",
510+
(d: u8 = d) => d < 10
511+
);
508512

509513
// SAFETY: `'0'` through `'9'` are U+00030 through U+0039,
510514
// so because `d` must be 64 or less the addition can return at most

Diff for: library/core/src/cell/once.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ impl<T> OnceCell<T> {
309309
/// ```
310310
#[inline]
311311
#[stable(feature = "once_cell", since = "1.70.0")]
312-
pub fn into_inner(self) -> Option<T> {
312+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
313+
pub const fn into_inner(self) -> Option<T> {
313314
// Because `into_inner` takes `self` by value, the compiler statically verifies
314315
// that it is not currently borrowed. So it is safe to move out `Option<T>`.
315316
self.inner.into_inner()

Diff for: library/core/src/ub_checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::intrinsics::{self, const_eval_select};
1010
/// macro for language UB are always ignored.
1111
///
1212
/// This macro should be called as
13-
/// `assert_unsafe_precondition!(check_{library,lang}_ub, "message", (ident: type = expr, ident: type = expr) => check_expr)`
13+
/// `assert_unsafe_precondition!(check_{library,language}_ub, "message", (ident: type = expr, ident: type = expr) => check_expr)`
1414
/// where each `expr` will be evaluated and passed in as function argument `ident: type`. Then all
1515
/// those arguments are passed to a function with the body `check_expr`.
1616
/// Pick `check_language_ub` when this is guarding a violation of language UB, i.e., immediate UB

Diff for: library/std/src/os/wasi/fs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//!
33
//! [`std::fs`]: crate::fs
44
5-
#![deny(unsafe_op_in_unsafe_fn)]
65
#![unstable(feature = "wasi_ext", issue = "71213")]
76

87
// Used for `File::read` on intra-doc links

Diff for: library/std/src/os/wasi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
3131
#![cfg_attr(not(target_env = "p2"), stable(feature = "rust1", since = "1.0.0"))]
3232
#![cfg_attr(target_env = "p2", unstable(feature = "wasip2", issue = "none"))]
33-
#![deny(unsafe_op_in_unsafe_fn)]
33+
#![forbid(unsafe_op_in_unsafe_fn)]
3434
#![doc(cfg(target_os = "wasi"))]
3535

3636
pub mod ffi;

Diff for: library/std/src/os/wasip2/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
//!
33
//! This module is currently empty, but will be filled over time as wasi-libc support for WASI Preview 2 is stabilized.
44
5+
#![forbid(unsafe_op_in_unsafe_fn)]
56
#![stable(feature = "raw_ext", since = "1.1.0")]

Diff for: library/std/src/sys/pal/wasi/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use crate::ffi::{CStr, OsStr, OsString};
44
use crate::os::wasi::ffi::OsStrExt;

Diff for: library/std/src/sys/pal/wasi/env.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
2+
13
pub mod os {
24
pub const FAMILY: &str = "";
35
pub const OS: &str = "";

Diff for: library/std/src/sys/pal/wasi/fd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22
#![allow(dead_code)]
33

44
use super::err2io;

Diff for: library/std/src/sys/pal/wasi/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use super::fd::WasiFd;
44
use crate::ffi::{CStr, OsStr, OsString};

Diff for: library/std/src/sys/pal/wasi/helpers.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
2+
13
use crate::{io as std_io, mem};
24

35
#[inline]

Diff for: library/std/src/sys/pal/wasi/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use crate::marker::PhantomData;
44
use crate::os::fd::{AsFd, AsRawFd};

Diff for: library/std/src/sys/pal/wasi/net.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use super::err2io;
44
use super::fd::WasiFd;

Diff for: library/std/src/sys/pal/wasi/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use core::slice::memchr;
44

Diff for: library/std/src/sys/pal/wasi/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use super::fd::WasiFd;
44
use crate::io::{self, IoSlice, IoSliceMut};

Diff for: library/std/src/sys/pal/wasi/thread.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::CStr;
24
use crate::num::NonZero;
35
use crate::sys::unsupported;
@@ -73,13 +75,13 @@ impl Thread {
7375
if #[cfg(target_feature = "atomics")] {
7476
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
7577
let p = Box::into_raw(Box::new(p));
76-
let mut native: libc::pthread_t = mem::zeroed();
77-
let mut attr: libc::pthread_attr_t = mem::zeroed();
78-
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
78+
let mut native: libc::pthread_t = unsafe { mem::zeroed() };
79+
let mut attr: libc::pthread_attr_t = unsafe { mem::zeroed() };
80+
assert_eq!(unsafe { libc::pthread_attr_init(&mut attr) }, 0);
7981

8082
let stack_size = cmp::max(stack, DEFAULT_MIN_STACK_SIZE);
8183

82-
match libc::pthread_attr_setstacksize(&mut attr, stack_size) {
84+
match unsafe { libc::pthread_attr_setstacksize(&mut attr, stack_size) } {
8385
0 => {}
8486
n => {
8587
assert_eq!(n, libc::EINVAL);
@@ -90,20 +92,20 @@ impl Thread {
9092
let page_size = os::page_size();
9193
let stack_size =
9294
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
93-
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
95+
assert_eq!(unsafe { libc::pthread_attr_setstacksize(&mut attr, stack_size) }, 0);
9496
}
9597
};
9698

97-
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
99+
let ret = unsafe { libc::pthread_create(&mut native, &attr, thread_start, p as *mut _) };
98100
// Note: if the thread creation fails and this assert fails, then p will
99101
// be leaked. However, an alternative design could cause double-free
100102
// which is clearly worse.
101-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
103+
assert_eq!(unsafe {libc::pthread_attr_destroy(&mut attr) }, 0);
102104

103105
return if ret != 0 {
104106
// The thread failed to start and as a result p was not consumed. Therefore, it is
105107
// safe to reconstruct the box so that it gets deallocated.
106-
drop(Box::from_raw(p));
108+
unsafe { drop(Box::from_raw(p)); }
107109
Err(io::Error::from_raw_os_error(ret))
108110
} else {
109111
Ok(Thread { id: native })

Diff for: library/std/src/sys/pal/wasi/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
1+
#![forbid(unsafe_op_in_unsafe_fn)]
22

33
use crate::time::Duration;
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Verifies that "cfi-normalize-integers" module flag is added.
2+
//
3+
//@ needs-sanitizer-cfi
4+
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers
5+
6+
#![crate_type = "lib"]
7+
8+
pub fn foo() {}
9+
10+
// CHECK: !{{[0-9]+}} = !{i32 4, !"cfi-normalize-integers", i32 1}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Verifies that "cfi-normalize-integers" module flag is added.
2+
//
3+
//@ revisions: aarch64 x86_64
4+
//@ [aarch64] compile-flags: --target aarch64-unknown-none
5+
//@ [aarch64] needs-llvm-components: aarch64
6+
//@ [x86_64] compile-flags: --target x86_64-unknown-none
7+
//@ [x86_64] needs-llvm-components: x86
8+
//@ compile-flags: -Ctarget-feature=-crt-static -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
9+
10+
#![feature(no_core, lang_items)]
11+
#![crate_type = "lib"]
12+
#![no_core]
13+
14+
#[lang = "sized"]
15+
trait Sized {}
16+
#[lang = "copy"]
17+
trait Copy {}
18+
19+
pub fn foo() {}
20+
21+
// CHECK: !{{[0-9]+}} = !{i32 4, !"cfi-normalize-integers", i32 1}

Diff for: tests/codegen/sanitizer/kcfi/add-kcfi-offset-flag.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Verifies that "kcfi-offset" module flag is added.
2+
//
3+
//@ revisions: aarch64 x86_64
4+
//@ [aarch64] compile-flags: --target aarch64-unknown-none
5+
//@ [aarch64] needs-llvm-components: aarch64
6+
//@ [x86_64] compile-flags: --target x86_64-unknown-none
7+
//@ [x86_64] needs-llvm-components: x86
8+
//@ compile-flags: -Ctarget-feature=-crt-static -Zsanitizer=kcfi -Z patchable-function-entry=4,3
9+
10+
#![feature(no_core, lang_items, patchable_function_entry)]
11+
#![crate_type = "lib"]
12+
#![no_core]
13+
14+
#[lang = "sized"]
15+
trait Sized {}
16+
#[lang = "copy"]
17+
trait Copy {}
18+
19+
pub fn foo() {}
20+
21+
// CHECK: !{{[0-9]+}} = !{i32 4, !"kcfi-offset", i32 3}

Diff for: tests/run-make/debugger-visualizer-dep-info/foo.py

-1
This file was deleted.

Diff for: tests/run-make/debugger-visualizer-dep-info/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![debugger_visualizer(gdb_script_file = "foo.py")]
1+
#![debugger_visualizer(gdb_script_file = "my_gdb_script.py")]
22

33
fn main() {
44
const _UNUSED: u32 = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is a Python script, but we don't actually run it.
2+
# So if you're trying to remove Python scripts from the test suite,
3+
# be aware that there's no value in trying to get rid of this one.
4+
#
5+
# It just needs to exist so that the compiler can embed it via
6+
# `#![debugger_visualizer(gdb_script_file = "...")]`.

Diff for: tests/run-make/debugger-visualizer-dep-info/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ use run_make_support::{invalid_utf8_contains, rustc};
66

77
fn main() {
88
rustc().emit("dep-info").input("main.rs").run();
9-
invalid_utf8_contains("main.d", "foo.py");
9+
invalid_utf8_contains("main.d", "my_gdb_script.py");
1010
invalid_utf8_contains("main.d", "my_visualizers/bar.natvis");
1111
}

Diff for: tests/run-make/libtest-junit/validate_junit.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#!/usr/bin/env python
22

3+
# Trivial Python script that reads lines from stdin, and checks that each line
4+
# is a well-formed XML document.
5+
#
6+
# This takes advantage of the fact that Python has a built-in XML parser,
7+
# whereas doing the same check in Rust would require us to pull in an XML
8+
# crate just for this relatively-minor test.
9+
#
10+
# If you're trying to remove Python scripts from the test suite, think twice
11+
# before removing this one. You could do so, but it's probably not worth it.
12+
313
import sys
414
import xml.etree.ElementTree as ET
515

0 commit comments

Comments
 (0)