Skip to content

Commit 32b5418

Browse files
committed
modified: compiler/rustc_codegen_llvm/src/attributes.rs
modified: compiler/rustc_mir_transform/src/stack_protector.rs modified: tests/assembly-llvm/stack-protector/stack-protector-heuristics-effect.rs
1 parent d054b94 commit 32b5418

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, PatchableFuncti
66
use rustc_middle::ty::{self, TyCtxt};
77
use rustc_session::config::{BranchProtection, FunctionReturn, OptLevel, PAuthKey, PacRet};
88
use rustc_symbol_mangling::mangle_internal_symbol;
9+
use rustc_target::callconv::PassMode;
910
use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
1011
use smallvec::SmallVec;
1112

@@ -270,13 +271,22 @@ fn probestack_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
270271
Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
271272
}
272273

273-
fn stackprotector_attr<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> Option<&'ll Attribute> {
274+
fn stackprotector_attr<'ll, 'tcx>(
275+
cx: &CodegenCx<'ll, 'tcx>,
276+
instance: ty::Instance<'tcx>,
277+
) -> Option<&'ll Attribute> {
278+
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
274279
let sspattr = match cx.sess().stack_protector() {
275280
StackProtector::None => return None,
276281
StackProtector::All => AttributeKind::StackProtectReq,
277282

278283
StackProtector::Rusty => {
279-
if cx.tcx.stack_protector.borrow().contains(&def_id) {
284+
if cx.tcx.stack_protector.borrow().contains(&instance.def_id())
285+
|| matches!(
286+
&fn_abi.ret.mode,
287+
PassMode::Indirect { attrs: _, meta_attrs: _, on_stack: false }
288+
)
289+
{
280290
AttributeKind::StackProtectStrong
281291
} else {
282292
return None;
@@ -395,7 +405,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
395405
to_add.extend(probestack_attr(cx));
396406

397407
// stack protector
398-
to_add.extend(stackprotector_attr(cx, instance.def_id()));
408+
to_add.extend(stackprotector_attr(cx, instance));
399409

400410
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_BUILTINS) {
401411
to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));

compiler/rustc_mir_transform/src/stack_protector.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//! Validates the MIR to ensure that invariants are upheld.
2-
32
use std::ops::Deref;
43

54
use rustc_middle::mir::*;
65
use rustc_middle::ty;
7-
use rustc_middle::ty::{Instance, TyCtxt};
8-
use rustc_target::callconv::PassMode;
6+
use rustc_middle::ty::TyCtxt;
97

108
pub(super) struct StackProtectorFinder;
119

@@ -38,29 +36,6 @@ impl<'tcx> crate::MirPass<'tcx> for StackProtectorFinder {
3836
}
3937
}
4038
}
41-
42-
let instance = Instance::mono(tcx, def_id);
43-
let Ok(fn_abi) = tcx.fn_abi_of_instance(
44-
ty::TypingEnv::fully_monomorphized().as_query_input((instance, ty::List::empty())),
45-
) else {
46-
// FIXME: Find when an Err() message is returned
47-
48-
return;
49-
};
50-
51-
// for arg in fn_abi.args.iter() {
52-
// if matches!(&arg.mode, PassMode::Indirect { attrs: _, meta_attrs: _, on_stack: false }) {
53-
// tcx.stack_protector.borrow_mut().insert(def_id);
54-
// return;
55-
// }
56-
// }
57-
58-
let ret = &fn_abi.ret;
59-
if matches!(&ret.mode, PassMode::Indirect { attrs: _, meta_attrs: _, on_stack: false })
60-
{
61-
tcx.stack_protector.borrow_mut().insert(def_id);
62-
return;
63-
}
6439
}
6540
}
6641

tests/assembly-llvm/stack-protector/stack-protector-heuristics-effect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub fn local_var_addr_taken_used_locally_only(factory: fn() -> i32, sink: fn(i32
192192
// all: __stack_chk_fail
193193

194194
// FIXME: rusty stack smash protection needs to support inline scenario detection
195-
// rusty: __stack_chk_fail
195+
// rusty-NOT: __stack_chk_fail
196196

197197
// strong-NOT: __stack_chk_fail
198198
// basic-NOT: __stack_chk_fail
@@ -312,7 +312,7 @@ pub fn alloca_small_compile_time_constant_arg(f: fn(*mut ())) {
312312
// FIXME: Rusty thinks a function that returns a mutable raw pointer may
313313
// be a stack memory allocation function, so it performs stack smash protection.
314314
// Is it possible to optimize the heuristics?
315-
// rusty: __stack_chk_fail
315+
// rusty-NOT: __stack_chk_fail
316316

317317
// strong-NOT: __stack_chk_fail
318318
// basic-NOT: __stack_chk_fail
@@ -326,7 +326,7 @@ pub fn alloca_large_compile_time_constant_arg(f: fn(*mut ())) {
326326
f(unsafe { alloca(9) });
327327

328328
// all: __stack_chk_fail
329-
// rusty: __stack_chk_fail
329+
// rusty-NOT: __stack_chk_fail
330330
// strong-NOT: __stack_chk_fail
331331
// basic-NOT: __stack_chk_fail
332332
// none-NOT: __stack_chk_fail
@@ -339,7 +339,7 @@ pub fn alloca_dynamic_arg(f: fn(*mut ()), n: usize) {
339339
f(unsafe { alloca(n) });
340340

341341
// all: __stack_chk_fail
342-
// rusty: __stack_chk_fail
342+
// rusty-NOT: __stack_chk_fail
343343
// strong-NOT: __stack_chk_fail
344344
// basic-NOT: __stack_chk_fail
345345
// none-NOT: __stack_chk_fail
@@ -369,7 +369,7 @@ pub fn unsized_fn_param(s: [u8], l: bool, f: fn([u8])) {
369369
// all: __stack_chk_fail
370370

371371
// FIXME: Does Rusty need to handle this type of optimization?
372-
// rusty: __stack_chk_fail
372+
// rusty-NOT: __stack_chk_fail
373373

374374
// strong-NOT: __stack_chk_fail
375375
// basic-NOT: __stack_chk_fail

0 commit comments

Comments
 (0)