Skip to content

Commit b12b836

Browse files
committed
Auto merge of #105525 - matthiaskrgr:rollup-ricyw5s, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - #98391 (Reimplement std's thread parker on top of events on SGX) - #104019 (Compute generator sizes with `-Zprint_type_sizes`) - #104512 (Set `download-ci-llvm = "if-available"` by default when `channel = dev`) - #104901 (Implement masking in FileType comparison on Unix) - #105082 (Fix Async Generator ABI) - #105109 (Add LLVM KCFI support to the Rust compiler) - #105505 (Don't warn about unused parens when they are used by yeet expr) - #105514 (Introduce `Span::is_visible`) - #105516 (Update cargo) - #105522 (Remove wrong note for short circuiting operators) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents cbc70ff + f6c2add commit b12b836

File tree

58 files changed

+913
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+913
-284
lines changed

Cargo.lock

+12
Original file line numberDiff line numberDiff line change
@@ -4404,6 +4404,7 @@ dependencies = [
44044404
"rustc_span",
44054405
"rustc_target",
44064406
"tracing",
4407+
"twox-hash",
44074408
]
44084409

44094410
[[package]]
@@ -5394,6 +5395,17 @@ dependencies = [
53945395
"tracing-subscriber",
53955396
]
53965397

5398+
[[package]]
5399+
name = "twox-hash"
5400+
version = "1.6.3"
5401+
source = "registry+https://github.com/rust-lang/crates.io-index"
5402+
checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
5403+
dependencies = [
5404+
"cfg-if 1.0.0",
5405+
"rand 0.8.5",
5406+
"static_assertions",
5407+
]
5408+
53975409
[[package]]
53985410
name = "type-map"
53995411
version = "0.4.0"

compiler/rustc_codegen_gcc/src/type_.rs

+4
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,8 @@ impl<'gcc, 'tcx> TypeMembershipMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
300300
// Unsupported.
301301
self.context.new_rvalue_from_int(self.int_type, 0)
302302
}
303+
304+
fn set_kcfi_type_metadata(&self, _function: RValue<'gcc>, _kcfi_typeid: u32) {
305+
// Unsupported.
306+
}
303307
}

compiler/rustc_codegen_llvm/src/allocator.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ pub(crate) unsafe fn codegen(
8888
callee,
8989
args.as_ptr(),
9090
args.len() as c_uint,
91-
None,
91+
[].as_ptr(),
92+
0 as c_uint,
9293
);
9394
llvm::LLVMSetTailCall(ret, True);
9495
if output.is_some() {
@@ -132,8 +133,15 @@ pub(crate) unsafe fn codegen(
132133
.enumerate()
133134
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
134135
.collect::<Vec<_>>();
135-
let ret =
136-
llvm::LLVMRustBuildCall(llbuilder, ty, callee, args.as_ptr(), args.len() as c_uint, None);
136+
let ret = llvm::LLVMRustBuildCall(
137+
llbuilder,
138+
ty,
139+
callee,
140+
args.as_ptr(),
141+
args.len() as c_uint,
142+
[].as_ptr(),
143+
0 as c_uint,
144+
);
137145
llvm::LLVMSetTailCall(ret, True);
138146
llvm::LLVMBuildRetVoid(llbuilder);
139147
llvm::LLVMDisposeBuilder(llbuilder);

compiler/rustc_codegen_llvm/src/builder.rs

+43-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_middle::ty::layout::{
2020
};
2121
use rustc_middle::ty::{self, Ty, TyCtxt};
2222
use rustc_span::Span;
23+
use rustc_symbol_mangling::typeid::kcfi_typeid_for_fnabi;
2324
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2425
use rustc_target::spec::{HasTargetSpec, Target};
2526
use std::borrow::Cow;
@@ -225,9 +226,25 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
225226
debug!("invoke {:?} with args ({:?})", llfn, args);
226227

227228
let args = self.check_call("invoke", llty, llfn, args);
228-
let bundle = funclet.map(|funclet| funclet.bundle());
229-
let bundle = bundle.as_ref().map(|b| &*b.raw);
229+
let funclet_bundle = funclet.map(|funclet| funclet.bundle());
230+
let funclet_bundle = funclet_bundle.as_ref().map(|b| &*b.raw);
231+
let mut bundles = vec![funclet_bundle];
232+
233+
// Set KCFI operand bundle
234+
let is_indirect_call = unsafe { llvm::LLVMIsAFunction(llfn).is_none() };
235+
let kcfi_bundle =
236+
if self.tcx.sess.is_sanitizer_kcfi_enabled() && fn_abi.is_some() && is_indirect_call {
237+
let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi.unwrap());
238+
Some(llvm::OperandBundleDef::new("kcfi", &[self.const_u32(kcfi_typeid)]))
239+
} else {
240+
None
241+
};
242+
if kcfi_bundle.is_some() {
243+
let kcfi_bundle = kcfi_bundle.as_ref().map(|b| &*b.raw);
244+
bundles.push(kcfi_bundle);
245+
}
230246

247+
bundles.retain(|bundle| bundle.is_some());
231248
let invoke = unsafe {
232249
llvm::LLVMRustBuildInvoke(
233250
self.llbuilder,
@@ -237,7 +254,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
237254
args.len() as c_uint,
238255
then,
239256
catch,
240-
bundle,
257+
bundles.as_ptr(),
258+
bundles.len() as c_uint,
241259
UNNAMED,
242260
)
243261
};
@@ -1143,7 +1161,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11431161
llfn,
11441162
args.as_ptr() as *const &llvm::Value,
11451163
args.len() as c_uint,
1146-
None,
1164+
[].as_ptr(),
1165+
0 as c_uint,
11471166
);
11481167
}
11491168
}
@@ -1159,17 +1178,34 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11591178
debug!("call {:?} with args ({:?})", llfn, args);
11601179

11611180
let args = self.check_call("call", llty, llfn, args);
1162-
let bundle = funclet.map(|funclet| funclet.bundle());
1163-
let bundle = bundle.as_ref().map(|b| &*b.raw);
1181+
let funclet_bundle = funclet.map(|funclet| funclet.bundle());
1182+
let funclet_bundle = funclet_bundle.as_ref().map(|b| &*b.raw);
1183+
let mut bundles = vec![funclet_bundle];
1184+
1185+
// Set KCFI operand bundle
1186+
let is_indirect_call = unsafe { llvm::LLVMIsAFunction(llfn).is_none() };
1187+
let kcfi_bundle =
1188+
if self.tcx.sess.is_sanitizer_kcfi_enabled() && fn_abi.is_some() && is_indirect_call {
1189+
let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi.unwrap());
1190+
Some(llvm::OperandBundleDef::new("kcfi", &[self.const_u32(kcfi_typeid)]))
1191+
} else {
1192+
None
1193+
};
1194+
if kcfi_bundle.is_some() {
1195+
let kcfi_bundle = kcfi_bundle.as_ref().map(|b| &*b.raw);
1196+
bundles.push(kcfi_bundle);
1197+
}
11641198

1199+
bundles.retain(|bundle| bundle.is_some());
11651200
let call = unsafe {
11661201
llvm::LLVMRustBuildCall(
11671202
self.llbuilder,
11681203
llty,
11691204
llfn,
11701205
args.as_ptr() as *const &llvm::Value,
11711206
args.len() as c_uint,
1172-
bundle,
1207+
bundles.as_ptr(),
1208+
bundles.len() as c_uint,
11731209
)
11741210
};
11751211
if let Some(fn_abi) = fn_abi {

compiler/rustc_codegen_llvm/src/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ pub unsafe fn create_module<'ll>(
250250
);
251251
}
252252

253+
if sess.is_sanitizer_kcfi_enabled() {
254+
let kcfi = "kcfi\0".as_ptr().cast();
255+
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
256+
}
257+
253258
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
254259
if sess.target.is_like_msvc {
255260
match sess.opts.cg.control_flow_guard {

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-67
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ use rustc_codegen_ssa::traits::*;
2727
use rustc_fs_util::path_to_c_string;
2828
use rustc_hir::def::CtorKind;
2929
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
30-
use rustc_index::vec::{Idx, IndexVec};
3130
use rustc_middle::bug;
32-
use rustc_middle::mir::{self, GeneratorLayout};
3331
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
3432
use rustc_middle::ty::subst::GenericArgKind;
3533
use rustc_middle::ty::{
@@ -1026,33 +1024,6 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10261024
// Tuples
10271025
//=-----------------------------------------------------------------------------
10281026

1029-
/// Returns names of captured upvars for closures and generators.
1030-
///
1031-
/// Here are some examples:
1032-
/// - `name__field1__field2` when the upvar is captured by value.
1033-
/// - `_ref__name__field` when the upvar is captured by reference.
1034-
///
1035-
/// For generators this only contains upvars that are shared by all states.
1036-
fn closure_saved_names_of_captured_variables(tcx: TyCtxt<'_>, def_id: DefId) -> SmallVec<String> {
1037-
let body = tcx.optimized_mir(def_id);
1038-
1039-
body.var_debug_info
1040-
.iter()
1041-
.filter_map(|var| {
1042-
let is_ref = match var.value {
1043-
mir::VarDebugInfoContents::Place(place) if place.local == mir::Local::new(1) => {
1044-
// The projection is either `[.., Field, Deref]` or `[.., Field]`. It
1045-
// implies whether the variable is captured by value or by reference.
1046-
matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
1047-
}
1048-
_ => return None,
1049-
};
1050-
let prefix = if is_ref { "_ref__" } else { "" };
1051-
Some(prefix.to_owned() + var.name.as_str())
1052-
})
1053-
.collect()
1054-
}
1055-
10561027
/// Builds the DW_TAG_member debuginfo nodes for the upvars of a closure or generator.
10571028
/// For a generator, this will handle upvars shared by all states.
10581029
fn build_upvar_field_di_nodes<'ll, 'tcx>(
@@ -1083,7 +1054,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
10831054
.all(|&t| t == cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t))
10841055
);
10851056

1086-
let capture_names = closure_saved_names_of_captured_variables(cx.tcx, def_id);
1057+
let capture_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
10871058
let layout = cx.layout_of(closure_or_generator_ty);
10881059

10891060
up_var_tys
@@ -1229,43 +1200,6 @@ fn build_union_type_di_node<'ll, 'tcx>(
12291200
)
12301201
}
12311202

1232-
// FIXME(eddyb) maybe precompute this? Right now it's computed once
1233-
// per generator monomorphization, but it doesn't depend on substs.
1234-
fn generator_layout_and_saved_local_names<'tcx>(
1235-
tcx: TyCtxt<'tcx>,
1236-
def_id: DefId,
1237-
) -> (&'tcx GeneratorLayout<'tcx>, IndexVec<mir::GeneratorSavedLocal, Option<Symbol>>) {
1238-
let body = tcx.optimized_mir(def_id);
1239-
let generator_layout = body.generator_layout().unwrap();
1240-
let mut generator_saved_local_names = IndexVec::from_elem(None, &generator_layout.field_tys);
1241-
1242-
let state_arg = mir::Local::new(1);
1243-
for var in &body.var_debug_info {
1244-
let mir::VarDebugInfoContents::Place(place) = &var.value else { continue };
1245-
if place.local != state_arg {
1246-
continue;
1247-
}
1248-
match place.projection[..] {
1249-
[
1250-
// Deref of the `Pin<&mut Self>` state argument.
1251-
mir::ProjectionElem::Field(..),
1252-
mir::ProjectionElem::Deref,
1253-
// Field of a variant of the state.
1254-
mir::ProjectionElem::Downcast(_, variant),
1255-
mir::ProjectionElem::Field(field, _),
1256-
] => {
1257-
let name = &mut generator_saved_local_names
1258-
[generator_layout.variant_fields[variant][field]];
1259-
if name.is_none() {
1260-
name.replace(var.name);
1261-
}
1262-
}
1263-
_ => {}
1264-
}
1265-
}
1266-
(generator_layout, generator_saved_local_names)
1267-
}
1268-
12691203
/// Computes the type parameters for a type, if any, for the given metadata.
12701204
fn build_generic_type_param_di_nodes<'ll, 'tcx>(
12711205
cx: &CodegenCx<'ll, 'tcx>,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use crate::{
2222
common::CodegenCx,
2323
debuginfo::{
2424
metadata::{
25-
build_field_di_node, closure_saved_names_of_captured_variables,
25+
build_field_di_node,
2626
enums::{tag_base_type, DiscrResult},
27-
file_metadata, generator_layout_and_saved_local_names, size_and_align_of, type_di_node,
27+
file_metadata, size_and_align_of, type_di_node,
2828
type_map::{self, Stub, UniqueTypeId},
2929
unknown_file_metadata, DINodeCreationResult, SmallVec, NO_GENERICS, NO_SCOPE_METADATA,
3030
UNKNOWN_LINE_NUMBER,
@@ -677,9 +677,9 @@ fn build_union_fields_for_direct_tag_generator<'ll, 'tcx>(
677677
};
678678

679679
let (generator_layout, state_specific_upvar_names) =
680-
generator_layout_and_saved_local_names(cx.tcx, generator_def_id);
680+
cx.tcx.generator_layout_and_saved_local_names(generator_def_id);
681681

682-
let common_upvar_names = closure_saved_names_of_captured_variables(cx.tcx, generator_def_id);
682+
let common_upvar_names = cx.tcx.closure_saved_names_of_captured_variables(generator_def_id);
683683
let variant_range = generator_substs.variant_range(generator_def_id, cx.tcx);
684684
let variant_count = (variant_range.start.as_u32()..variant_range.end.as_u32()).len();
685685

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use crate::{
44
common::CodegenCx,
55
debuginfo::{
66
metadata::{
7-
closure_saved_names_of_captured_variables,
87
enums::tag_base_type,
9-
file_metadata, generator_layout_and_saved_local_names, size_and_align_of, type_di_node,
8+
file_metadata, size_and_align_of, type_di_node,
109
type_map::{self, Stub, StubInfo, UniqueTypeId},
1110
unknown_file_metadata, DINodeCreationResult, SmallVec, NO_GENERICS,
1211
UNKNOWN_LINE_NUMBER,
@@ -157,7 +156,7 @@ pub(super) fn build_generator_di_node<'ll, 'tcx>(
157156
),
158157
|cx, generator_type_di_node| {
159158
let (generator_layout, state_specific_upvar_names) =
160-
generator_layout_and_saved_local_names(cx.tcx, generator_def_id);
159+
cx.tcx.generator_layout_and_saved_local_names(generator_def_id);
161160

162161
let Variants::Multiple { tag_encoding: TagEncoding::Direct, ref variants, .. } = generator_type_and_layout.variants else {
163162
bug!(
@@ -167,7 +166,7 @@ pub(super) fn build_generator_di_node<'ll, 'tcx>(
167166
};
168167

169168
let common_upvar_names =
170-
closure_saved_names_of_captured_variables(cx.tcx, generator_def_id);
169+
cx.tcx.closure_saved_names_of_captured_variables(generator_def_id);
171170

172171
// Build variant struct types
173172
let variant_struct_type_di_nodes: SmallVec<_> = variants

compiler/rustc_codegen_llvm/src/declare.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::type_::Type;
2020
use crate::value::Value;
2121
use rustc_codegen_ssa::traits::TypeMembershipMethods;
2222
use rustc_middle::ty::Ty;
23-
use rustc_symbol_mangling::typeid::typeid_for_fnabi;
23+
use rustc_symbol_mangling::typeid::{kcfi_typeid_for_fnabi, typeid_for_fnabi};
2424
use smallvec::SmallVec;
2525

2626
/// Declare a function.
@@ -136,6 +136,11 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
136136
self.set_type_metadata(llfn, typeid);
137137
}
138138

139+
if self.tcx.sess.is_sanitizer_kcfi_enabled() {
140+
let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi);
141+
self.set_kcfi_type_metadata(llfn, kcfi_typeid);
142+
}
143+
139144
llfn
140145
}
141146

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ pub enum MetadataType {
427427
MD_type = 19,
428428
MD_vcall_visibility = 28,
429429
MD_noundef = 29,
430+
MD_kcfi_type = 36,
430431
}
431432

432433
/// LLVMRustAsmDialect
@@ -1063,6 +1064,7 @@ extern "C" {
10631064
pub fn LLVMGlobalSetMetadata<'a>(Val: &'a Value, KindID: c_uint, Metadata: &'a Metadata);
10641065
pub fn LLVMRustGlobalAddMetadata<'a>(Val: &'a Value, KindID: c_uint, Metadata: &'a Metadata);
10651066
pub fn LLVMValueAsMetadata(Node: &Value) -> &Metadata;
1067+
pub fn LLVMIsAFunction(Val: &Value) -> Option<&Value>;
10661068

10671069
// Operations on constants of any type
10681070
pub fn LLVMConstNull(Ty: &Type) -> &Value;
@@ -1273,7 +1275,8 @@ extern "C" {
12731275
NumArgs: c_uint,
12741276
Then: &'a BasicBlock,
12751277
Catch: &'a BasicBlock,
1276-
Bundle: Option<&OperandBundleDef<'a>>,
1278+
OpBundles: *const Option<&OperandBundleDef<'a>>,
1279+
NumOpBundles: c_uint,
12771280
Name: *const c_char,
12781281
) -> &'a Value;
12791282
pub fn LLVMBuildLandingPad<'a>(
@@ -1643,7 +1646,8 @@ extern "C" {
16431646
Fn: &'a Value,
16441647
Args: *const &'a Value,
16451648
NumArgs: c_uint,
1646-
Bundle: Option<&OperandBundleDef<'a>>,
1649+
OpBundles: *const Option<&OperandBundleDef<'a>>,
1650+
NumOpBundles: c_uint,
16471651
) -> &'a Value;
16481652
pub fn LLVMRustBuildMemCpy<'a>(
16491653
B: &Builder<'a>,

compiler/rustc_codegen_llvm/src/type_.rs

+15
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,19 @@ impl<'ll, 'tcx> TypeMembershipMethods<'tcx> for CodegenCx<'ll, 'tcx> {
316316
)
317317
}
318318
}
319+
320+
fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
321+
let kcfi_type_metadata = self.const_u32(kcfi_typeid);
322+
unsafe {
323+
llvm::LLVMGlobalSetMetadata(
324+
function,
325+
llvm::MD_kcfi_type as c_uint,
326+
llvm::LLVMMDNodeInContext2(
327+
self.llcx,
328+
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
329+
1,
330+
),
331+
)
332+
}
333+
}
319334
}

0 commit comments

Comments
 (0)