Skip to content

Commit 334eafa

Browse files
committed
Hack to make naked functions with arguments work
1 parent db59942 commit 334eafa

File tree

1 file changed

+13
-0
lines changed
  • src/librustc_codegen_ssa/mir

1 file changed

+13
-0
lines changed

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::base;
22
use crate::traits::*;
3+
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
34
use rustc::mir;
45
use rustc::ty::layout::{FnAbiExt, HasTyCtxt, TyLayout};
56
use rustc::ty::{self, Instance, Ty, TypeFoldable};
@@ -338,13 +339,25 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
338339
let mir = fx.mir;
339340
let mut idx = 0;
340341
let mut llarg_idx = fx.fn_abi.ret.is_indirect() as usize;
342+
let codegen_fn_attrs = fx.cx.tcx().codegen_fn_attrs(fx.instance.def_id());
341343

342344
let args = mir
343345
.args_iter()
344346
.enumerate()
345347
.map(|(arg_index, local)| {
346348
let arg_decl = &mir.local_decls[local];
347349

350+
// Naked functions can't contain an alloca. This is a horrible hack,
351+
// but at least it makes naked functions with arguments work
352+
// correctly in debug build (they won't corrupt the stack any more).
353+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
354+
let layout = bx.layout_of(arg_decl.ty);
355+
return LocalRef::Place(PlaceRef::new_sized(
356+
bx.const_undef(bx.cx().backend_type(layout)),
357+
layout,
358+
));
359+
}
360+
348361
if Some(local) == mir.spread_arg {
349362
// This argument (e.g., the last argument in the "rust-call" ABI)
350363
// is a tuple that was spread at the ABI level and now we have

0 commit comments

Comments
 (0)