From 0afd3e137a6fb52923810760858ed253235c6d49 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Tue, 9 Jan 2024 21:41:37 -0800 Subject: [PATCH 01/16] Add span_bug for locals larger than 1GB use ilog2 Update compiler/rustc_codegen_ssa/messages.ftl Co-authored-by: Michael Goulet Run test only on 64 bit --- compiler/rustc_codegen_ssa/messages.ftl | 2 ++ compiler/rustc_codegen_ssa/src/errors.rs | 8 ++++++++ compiler/rustc_codegen_ssa/src/mir/mod.rs | 20 ++++++++++++++++++- .../codegen/large-stack-size-issue-83060.rs | 20 +++++++++++++++++++ .../large-stack-size-issue-83060.stderr | 8 ++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/ui/codegen/large-stack-size-issue-83060.rs create mode 100644 tests/ui/codegen/large-stack-size-issue-83060.stderr diff --git a/compiler/rustc_codegen_ssa/messages.ftl b/compiler/rustc_codegen_ssa/messages.ftl index 56188714b44fd..c888118190d38 100644 --- a/compiler/rustc_codegen_ssa/messages.ftl +++ b/compiler/rustc_codegen_ssa/messages.ftl @@ -32,6 +32,8 @@ codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$e codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error} +codegen_ssa_dangerous_stack_allocation = dangerous stack allocation of size: {$output} exceeds most system architecture limits + codegen_ssa_dlltool_fail_import_library = Dlltool could not create import library with {$dlltool_path} {$dlltool_args}: {$stdout} diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 00f8654e670b1..05d57e9672a10 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -429,6 +429,14 @@ pub(crate) struct CheckInstalledVisualStudio; #[diag(codegen_ssa_insufficient_vs_code_product)] pub(crate) struct InsufficientVSCodeProduct; +#[derive(Diagnostic)] +#[diag(codegen_ssa_dangerous_stack_allocation)] +pub struct DangerousStackAllocation { + #[primary_span] + pub span: Span, + pub output: String, +} + #[derive(Diagnostic)] #[diag(codegen_ssa_processing_dymutil_failed)] #[note] diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index 62f69af3f2f75..b462b2009445d 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -10,8 +10,8 @@ use rustc_middle::{bug, mir, span_bug}; use rustc_target::callconv::{FnAbi, PassMode}; use tracing::{debug, instrument}; -use crate::base; use crate::traits::*; +use crate::{base, errors}; mod analyze; mod block; @@ -30,6 +30,8 @@ use self::debuginfo::{FunctionDebugContext, PerLocalVarDebugInfo}; use self::operand::{OperandRef, OperandValue}; use self::place::PlaceRef; +const MIN_DANGEROUS_SIZE: u64 = 1024 * 1024 * 1024 * 1; // 1 GB + // Used for tracking the state of generated basic blocks. enum CachedLlbb { /// Nothing created yet. @@ -245,6 +247,14 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( let layout = start_bx.layout_of(fx.monomorphize(decl.ty)); assert!(!layout.ty.has_erasable_regions()); + if layout.size.bytes() >= MIN_DANGEROUS_SIZE { + let (size_quantity, size_unit) = human_readable_bytes(layout.size.bytes()); + cx.tcx().dcx().emit_warn(errors::DangerousStackAllocation { + span: decl.source_info.span, + output: format!("{:.2} {}", size_quantity, size_unit), + }); + } + if local == mir::RETURN_PLACE { match fx.fn_abi.ret.mode { PassMode::Indirect { .. } => { @@ -310,6 +320,14 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( } } +/// Formats a number of bytes into a human readable SI-prefixed size. +/// Returns a tuple of `(quantity, units)`. +pub fn human_readable_bytes(bytes: u64) -> (u64, &'static str) { + static UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; + let i = ((bytes.checked_ilog2().unwrap_or(0) / 10) as usize).min(UNITS.len() - 1); + (bytes >> (10 * i), UNITS[i]) +} + /// Produces, for each argument, a `Value` pointing at the /// argument's value. As arguments are places, these are always /// indirect. diff --git a/tests/ui/codegen/large-stack-size-issue-83060.rs b/tests/ui/codegen/large-stack-size-issue-83060.rs new file mode 100644 index 0000000000000..b822d1b95ab1e --- /dev/null +++ b/tests/ui/codegen/large-stack-size-issue-83060.rs @@ -0,0 +1,20 @@ +// This test checks that allocating a stack size of 1GB or more results in a warning +//@build-pass +//@ only-64bit + +fn func() { + const CAP: usize = std::u32::MAX as usize; + let mut x: [u8; CAP>>1] = [0; CAP>>1]; + //~^ warning: dangerous stack allocation of size: 1 GiB exceeds most system architecture limits + x[2] = 123; + println!("{}", x[2]); +} + +fn main() { + std::thread::Builder::new() + .stack_size(3 * 1024 * 1024 * 1024) + .spawn(func) + .unwrap() + .join() + .unwrap(); +} diff --git a/tests/ui/codegen/large-stack-size-issue-83060.stderr b/tests/ui/codegen/large-stack-size-issue-83060.stderr new file mode 100644 index 0000000000000..e90b4e68a1333 --- /dev/null +++ b/tests/ui/codegen/large-stack-size-issue-83060.stderr @@ -0,0 +1,8 @@ +warning: dangerous stack allocation of size: 1 GiB exceeds most system architecture limits + --> $DIR/large-stack-size-issue-83060.rs:7:9 + | +LL | let mut x: [u8; CAP>>1] = [0; CAP>>1]; + | ^^^^^ + +warning: 1 warning emitted + From c9f5a23ef54a14f9ede8a1be3f9503812b416f40 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Sat, 26 Oct 2024 01:59:00 -0700 Subject: [PATCH 02/16] Lint instead of warn --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 18 +++++--- compiler/rustc_lint_defs/src/builtin.rs | 42 +++++++++++++++++++ .../large-stack-size-issue-83060.rs | 3 +- .../large-stack-size-issue-83060.stderr | 4 +- 4 files changed, 59 insertions(+), 8 deletions(-) rename tests/ui/{codegen => sanitizer}/large-stack-size-issue-83060.rs (79%) rename tests/ui/{codegen => sanitizer}/large-stack-size-issue-83060.stderr (50%) diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index b462b2009445d..f8bffae82221a 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -1,5 +1,6 @@ use std::iter; +use rustc_hir::CRATE_HIR_ID; use rustc_index::IndexVec; use rustc_index::bit_set::BitSet; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; @@ -8,10 +9,11 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, TyAndLayout}; use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; use rustc_middle::{bug, mir, span_bug}; use rustc_target::callconv::{FnAbi, PassMode}; +use rustc_session::lint; use tracing::{debug, instrument}; use crate::traits::*; -use crate::{base, errors}; +use crate::base; mod analyze; mod block; @@ -249,12 +251,16 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( if layout.size.bytes() >= MIN_DANGEROUS_SIZE { let (size_quantity, size_unit) = human_readable_bytes(layout.size.bytes()); - cx.tcx().dcx().emit_warn(errors::DangerousStackAllocation { - span: decl.source_info.span, - output: format!("{:.2} {}", size_quantity, size_unit), - }); + cx.tcx().node_span_lint( + lint::builtin::DANGEROUS_STACK_ALLOCATION, + CRATE_HIR_ID, + decl.source_info.span, + |lint| { + lint.primary_message(format!("allocation of size: {:.2} {} exceeds most system architecture limits", size_quantity, size_unit)); + }, + ); } - + if local == mir::RETURN_PLACE { match fx.fn_abi.ret.mode { PassMode::Indirect { .. } => { diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 54e927df3c42b..18672ec8ffc4b 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -32,6 +32,7 @@ declare_lint_pass! { CONFLICTING_REPR_HINTS, CONST_EVALUATABLE_UNCHECKED, CONST_ITEM_MUTATION, + DANGEROUS_STACK_ALLOCATION, DEAD_CODE, DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK, DEPRECATED, @@ -711,6 +712,47 @@ declare_lint! { "detect assignments that will never be read" } +declare_lint! { + /// The `dangerous_stack_allocation` lint detects stack allocations that are 1 GB or more. + /// + /// ### Example + /// + /// ``` fn func() { + /// const CAP: usize = std::u32::MAX as usize; + /// let mut x: [u8; CAP>>1] = [0; CAP>>1]; + /// x[2] = 123; + /// println!("{}", x[2]); + /// } + /// + /// fn main() { + /// std::thread::Builder::new() + /// .stack_size(3 * 1024 * 1024 * 1024) + /// .spawn(func) + /// .unwrap() + /// .join() + /// .unwrap(); + /// } + /// ``` + /// + /// {{produces}} + /// ``` + /// warning: allocation of size: 1 GiB exceeds most system architecture limits + /// --> $DIR/large-stack-size-issue-83060.rs:7:9 + /// | + /// LL | let mut x: [u8; CAP>>1] = [0; CAP>>1]; + /// | ^^^^^ + /// | + /// = note: `#[warn(dangerous_stack_allocation)]` on by default + /// ``` + /// ### Explanation + /// + /// Large arras may cause stack overflow due to the limited size of the + /// stack on most platforms. + pub DANGEROUS_STACK_ALLOCATION, + Warn, + "Detects dangerous stack allocations at the limit of most architectures" +} + declare_lint! { /// The `dead_code` lint detects unused, unexported items. /// diff --git a/tests/ui/codegen/large-stack-size-issue-83060.rs b/tests/ui/sanitizer/large-stack-size-issue-83060.rs similarity index 79% rename from tests/ui/codegen/large-stack-size-issue-83060.rs rename to tests/ui/sanitizer/large-stack-size-issue-83060.rs index b822d1b95ab1e..297b4bff29a21 100644 --- a/tests/ui/codegen/large-stack-size-issue-83060.rs +++ b/tests/ui/sanitizer/large-stack-size-issue-83060.rs @@ -5,7 +5,8 @@ fn func() { const CAP: usize = std::u32::MAX as usize; let mut x: [u8; CAP>>1] = [0; CAP>>1]; - //~^ warning: dangerous stack allocation of size: 1 GiB exceeds most system architecture limits + //~^ warning: allocation of size: 1 GiB exceeds most system architecture limits + //~| NOTE on by default x[2] = 123; println!("{}", x[2]); } diff --git a/tests/ui/codegen/large-stack-size-issue-83060.stderr b/tests/ui/sanitizer/large-stack-size-issue-83060.stderr similarity index 50% rename from tests/ui/codegen/large-stack-size-issue-83060.stderr rename to tests/ui/sanitizer/large-stack-size-issue-83060.stderr index e90b4e68a1333..da28cc7021827 100644 --- a/tests/ui/codegen/large-stack-size-issue-83060.stderr +++ b/tests/ui/sanitizer/large-stack-size-issue-83060.stderr @@ -1,8 +1,10 @@ -warning: dangerous stack allocation of size: 1 GiB exceeds most system architecture limits +warning: allocation of size: 1 GiB exceeds most system architecture limits --> $DIR/large-stack-size-issue-83060.rs:7:9 | LL | let mut x: [u8; CAP>>1] = [0; CAP>>1]; | ^^^^^ + | + = note: `#[warn(dangerous_stack_allocation)]` on by default warning: 1 warning emitted From f75765e104f4288b889bc983c17db258721f468a Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Sat, 26 Oct 2024 02:02:19 -0700 Subject: [PATCH 03/16] tidy --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 25 +++++++++++++---------- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index f8bffae82221a..b856d4222bd7f 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -8,12 +8,12 @@ use rustc_middle::mir::{UnwindTerminateReason, traversal}; use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, TyAndLayout}; use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; use rustc_middle::{bug, mir, span_bug}; -use rustc_target::callconv::{FnAbi, PassMode}; use rustc_session::lint; +use rustc_target::callconv::{FnAbi, PassMode}; use tracing::{debug, instrument}; -use crate::traits::*; use crate::base; +use crate::traits::*; mod analyze; mod block; @@ -251,16 +251,19 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( if layout.size.bytes() >= MIN_DANGEROUS_SIZE { let (size_quantity, size_unit) = human_readable_bytes(layout.size.bytes()); - cx.tcx().node_span_lint( - lint::builtin::DANGEROUS_STACK_ALLOCATION, - CRATE_HIR_ID, - decl.source_info.span, - |lint| { - lint.primary_message(format!("allocation of size: {:.2} {} exceeds most system architecture limits", size_quantity, size_unit)); - }, - ); + cx.tcx().node_span_lint( + lint::builtin::DANGEROUS_STACK_ALLOCATION, + CRATE_HIR_ID, + decl.source_info.span, + |lint| { + lint.primary_message(format!( + "allocation of size: {:.2} {} exceeds most system architecture limits", + size_quantity, size_unit + )); + }, + ); } - + if local == mir::RETURN_PLACE { match fx.fn_abi.ret.mode { PassMode::Indirect { .. } => { diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 18672ec8ffc4b..1a93bda2e87b0 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -747,7 +747,7 @@ declare_lint! { /// ### Explanation /// /// Large arras may cause stack overflow due to the limited size of the - /// stack on most platforms. + /// stack on most platforms. pub DANGEROUS_STACK_ALLOCATION, Warn, "Detects dangerous stack allocations at the limit of most architectures" From 1bc466ad560368398d7a07f0f84d2f9bb9d5b960 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Thu, 31 Oct 2024 16:49:01 -0700 Subject: [PATCH 04/16] mingw fix --- compiler/rustc_lint_defs/src/builtin.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 1a93bda2e87b0..1fa3672e6abe6 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -713,7 +713,8 @@ declare_lint! { } declare_lint! { - /// The `dangerous_stack_allocation` lint detects stack allocations that are 1 GB or more. + /// The `dangerous_stack_allocation` lint detects stack allocations that + /// are 1 GB or more. /// /// ### Example /// From 3f02139f345a37d63f8611452e973faa33ab9ca7 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Thu, 31 Oct 2024 16:53:27 -0700 Subject: [PATCH 05/16] tidy! --- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 1fa3672e6abe6..b90d19e96a909 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -713,7 +713,7 @@ declare_lint! { } declare_lint! { - /// The `dangerous_stack_allocation` lint detects stack allocations that + /// The `dangerous_stack_allocation` lint detects stack allocations that /// are 1 GB or more. /// /// ### Example From 5f4d7ce8de6cc01828e08d0dc28c07d6e6e336db Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Fri, 1 Nov 2024 10:34:53 -0700 Subject: [PATCH 06/16] fix doctest --- compiler/rustc_lint_defs/src/builtin.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index b90d19e96a909..d323bf794984e 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -718,7 +718,8 @@ declare_lint! { /// /// ### Example /// - /// ``` fn func() { + /// ``` rust + /// fn func() { /// const CAP: usize = std::u32::MAX as usize; /// let mut x: [u8; CAP>>1] = [0; CAP>>1]; /// x[2] = 123; From 27df2a67428b7a7283c054bc40a4d88eeeb2ad60 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Sat, 2 Nov 2024 08:40:39 -0700 Subject: [PATCH 07/16] text for doc --- compiler/rustc_lint_defs/src/builtin.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index d323bf794984e..d9cc00ba75359 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -736,8 +736,9 @@ declare_lint! { /// } /// ``` /// - /// {{produces}} - /// ``` + /// This will produce: + /// + /// ```text /// warning: allocation of size: 1 GiB exceeds most system architecture limits /// --> $DIR/large-stack-size-issue-83060.rs:7:9 /// | From 6f8c5772d3ec3280b51cdb220bf28b4f1bf03158 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Sat, 2 Nov 2024 11:20:48 -0700 Subject: [PATCH 08/16] Another attempt --- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index d9cc00ba75359..c757f79500a86 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -736,7 +736,7 @@ declare_lint! { /// } /// ``` /// - /// This will produce: + /// {{produces}} /// /// ```text /// warning: allocation of size: 1 GiB exceeds most system architecture limits From c4ddef960b419d3ccef94e87e92389ee82b646d3 Mon Sep 17 00:00:00 2001 From: Swapna Iyer <78382735+iSwapna@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:19:27 -0800 Subject: [PATCH 09/16] Update compiler/rustc_codegen_ssa/src/mir/mod.rs Co-authored-by: Esteban Kuber --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index b856d4222bd7f..dfb3a82f94bf6 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -257,7 +257,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( decl.source_info.span, |lint| { lint.primary_message(format!( - "allocation of size: {:.2} {} exceeds most system architecture limits", + "allocation of {:.2} {} exceeds most system architecture limits", size_quantity, size_unit )); }, From 6b4a161e44807365d0a5fb8bf390a2a4f0a25296 Mon Sep 17 00:00:00 2001 From: Swapna Iyer <78382735+iSwapna@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:23:23 -0800 Subject: [PATCH 10/16] Update compiler/rustc_lint_defs/src/builtin.rs Co-authored-by: Esteban Kuber --- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index c757f79500a86..b6ffcf01c161f 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -749,7 +749,7 @@ declare_lint! { /// ``` /// ### Explanation /// - /// Large arras may cause stack overflow due to the limited size of the + /// Large arrays may cause stack overflow due to the limited size of the /// stack on most platforms. pub DANGEROUS_STACK_ALLOCATION, Warn, From 171beb32eee3295fddd2c5e949dd6763682777c7 Mon Sep 17 00:00:00 2001 From: Swapna Iyer <78382735+iSwapna@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:23:36 -0800 Subject: [PATCH 11/16] Update compiler/rustc_lint_defs/src/builtin.rs Co-authored-by: Esteban Kuber --- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index b6ffcf01c161f..a16c6e4c8c6c9 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -753,7 +753,7 @@ declare_lint! { /// stack on most platforms. pub DANGEROUS_STACK_ALLOCATION, Warn, - "Detects dangerous stack allocations at the limit of most architectures" + "detects dangerously large stack allocations at the limit of most architectures" } declare_lint! { From f36c7ee9aec0c9f1b83994a6243f52fa2b070b20 Mon Sep 17 00:00:00 2001 From: Swapna Iyer <78382735+iSwapna@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:26:51 -0800 Subject: [PATCH 12/16] Update compiler/rustc_codegen_ssa/src/mir/mod.rs Co-authored-by: Esteban Kuber --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index dfb3a82f94bf6..622d4a761de6f 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -249,7 +249,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( let layout = start_bx.layout_of(fx.monomorphize(decl.ty)); assert!(!layout.ty.has_erasable_regions()); - if layout.size.bytes() >= MIN_DANGEROUS_SIZE { + if layout.size.bytes() >= MIN_DANGEROUS_ALLOC_SIZE { let (size_quantity, size_unit) = human_readable_bytes(layout.size.bytes()); cx.tcx().node_span_lint( lint::builtin::DANGEROUS_STACK_ALLOCATION, From a035e3a2d15cadace897b0677f6a457dc6a67c7f Mon Sep 17 00:00:00 2001 From: Swapna Iyer <78382735+iSwapna@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:28:58 -0800 Subject: [PATCH 13/16] Update compiler/rustc_lint_defs/src/builtin.rs Co-authored-by: Esteban Kuber --- compiler/rustc_lint_defs/src/builtin.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index a16c6e4c8c6c9..08fea927cc22c 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -740,12 +740,12 @@ declare_lint! { /// /// ```text /// warning: allocation of size: 1 GiB exceeds most system architecture limits - /// --> $DIR/large-stack-size-issue-83060.rs:7:9 - /// | + /// --> $DIR/large-stack-size-issue-83060.rs:7:9 + /// | /// LL | let mut x: [u8; CAP>>1] = [0; CAP>>1]; - /// | ^^^^^ - /// | - /// = note: `#[warn(dangerous_stack_allocation)]` on by default + /// | ^^^^^ + /// | + /// = note: `#[warn(dangerous_stack_allocation)]` on by default /// ``` /// ### Explanation /// From ec78d7b2c9f150606bc977976452ba372ba3b89f Mon Sep 17 00:00:00 2001 From: Swapna Iyer <78382735+iSwapna@users.noreply.github.com> Date: Sat, 14 Dec 2024 16:00:28 -0800 Subject: [PATCH 14/16] Update compiler/rustc_codegen_ssa/src/mir/mod.rs Co-authored-by: Esteban Kuber --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index 622d4a761de6f..c438089936a84 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -32,7 +32,7 @@ use self::debuginfo::{FunctionDebugContext, PerLocalVarDebugInfo}; use self::operand::{OperandRef, OperandValue}; use self::place::PlaceRef; -const MIN_DANGEROUS_SIZE: u64 = 1024 * 1024 * 1024 * 1; // 1 GB +const MIN_DANGEROUS_ALLOC_SIZE: u64 = 1024 * 1024 * 1024 * 1; // 1 GB // Used for tracking the state of generated basic blocks. enum CachedLlbb { From 2f56984a6fedb3a6f2038e2022ffef33996df790 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Sat, 14 Dec 2024 16:02:35 -0800 Subject: [PATCH 15/16] Remove original implementation --- compiler/rustc_codegen_ssa/messages.ftl | 2 -- compiler/rustc_codegen_ssa/src/errors.rs | 8 -------- 2 files changed, 10 deletions(-) diff --git a/compiler/rustc_codegen_ssa/messages.ftl b/compiler/rustc_codegen_ssa/messages.ftl index c888118190d38..56188714b44fd 100644 --- a/compiler/rustc_codegen_ssa/messages.ftl +++ b/compiler/rustc_codegen_ssa/messages.ftl @@ -32,8 +32,6 @@ codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$e codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error} -codegen_ssa_dangerous_stack_allocation = dangerous stack allocation of size: {$output} exceeds most system architecture limits - codegen_ssa_dlltool_fail_import_library = Dlltool could not create import library with {$dlltool_path} {$dlltool_args}: {$stdout} diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 05d57e9672a10..00f8654e670b1 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -429,14 +429,6 @@ pub(crate) struct CheckInstalledVisualStudio; #[diag(codegen_ssa_insufficient_vs_code_product)] pub(crate) struct InsufficientVSCodeProduct; -#[derive(Diagnostic)] -#[diag(codegen_ssa_dangerous_stack_allocation)] -pub struct DangerousStackAllocation { - #[primary_span] - pub span: Span, - pub output: String, -} - #[derive(Diagnostic)] #[diag(codegen_ssa_processing_dymutil_failed)] #[note] From ffda82c78471e264664c9aeebd5a2c0fe9eb86a7 Mon Sep 17 00:00:00 2001 From: Swapna Iyer Date: Sat, 14 Dec 2024 17:29:18 -0800 Subject: [PATCH 16/16] Add "size" in error str --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index c438089936a84..7d139050288e1 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -257,7 +257,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( decl.source_info.span, |lint| { lint.primary_message(format!( - "allocation of {:.2} {} exceeds most system architecture limits", + "allocation of size: {:.2} {} exceeds most system architecture limits", size_quantity, size_unit )); },