@@ -27,6 +27,8 @@ use self::debuginfo::{FunctionDebugContext, PerLocalVarDebugInfo};
27
27
use self :: operand:: { OperandRef , OperandValue } ;
28
28
use self :: place:: PlaceRef ;
29
29
30
+ const MIN_DANGEROUS_SIZE : u64 = 1024 * 1024 * 1024 * 1 ; // 1 GB
31
+
30
32
// Used for tracking the state of generated basic blocks.
31
33
enum CachedLlbb < T > {
32
34
/// Nothing created yet.
@@ -226,6 +228,16 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
226
228
let layout = start_bx. layout_of ( fx. monomorphize ( decl. ty ) ) ;
227
229
assert ! ( !layout. ty. has_erasable_regions( ) ) ;
228
230
231
+ if layout. size . bytes ( ) >= MIN_DANGEROUS_SIZE {
232
+ let size_str = || {
233
+ let ( size_quantity, size_unit) = human_readable_bytes ( layout. size . bytes ( ) ) ;
234
+ format ! ( "{:.2} {}" , size_quantity, size_unit)
235
+ } ;
236
+ span_bug ! ( decl. source_info. span, "Dangerous stack allocation, size: {:?} of local: {:?} exceeds typical limits on most architectures" ,
237
+ size_str( ) , local) ;
238
+
239
+ }
240
+
229
241
if local == mir:: RETURN_PLACE && fx. fn_abi . ret . is_indirect ( ) {
230
242
debug ! ( "alloc: {:?} (return place) -> place" , local) ;
231
243
let llretptr = start_bx. get_param ( 0 ) ;
@@ -266,6 +278,18 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
266
278
}
267
279
}
268
280
281
+ /// Formats a number of bytes into a human readable SI-prefixed size.
282
+ /// Returns a tuple of `(quantity, units)`.
283
+ //
284
+ // Taken from Cargo:
285
+ // https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/util/mod.rs#L88-L95
286
+ pub fn human_readable_bytes ( bytes : u64 ) -> ( f32 , & ' static str ) {
287
+ static UNITS : [ & str ; 7 ] = [ "B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" , "EiB" ] ;
288
+ let bytes = bytes as f32 ;
289
+ let i = ( ( bytes. log2 ( ) / 10.0 ) as usize ) . min ( UNITS . len ( ) - 1 ) ;
290
+ ( bytes / 1024_f32 . powi ( i as i32 ) , UNITS [ i] )
291
+ }
292
+
269
293
/// Produces, for each argument, a `Value` pointing at the
270
294
/// argument's value. As arguments are places, these are always
271
295
/// indirect.
0 commit comments