From 1d72037dd39e3e53172d7605f22851b75f4e1a1b Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Thu, 7 Mar 2019 03:50:50 +0000 Subject: [PATCH] Fix segfaults in release build C-variadic fns `va_start` and `va_end` must be called to initialize/cleanup the "spoofed" `VaList` in a Rust defined C-variadic function even if the `VaList` is not used. --- src/librustc_codegen_ssa/mir/block.rs | 9 +++++++-- src/librustc_codegen_ssa/mir/mod.rs | 8 +------- src/test/codegen/c-variadic-opt.rs | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/test/codegen/c-variadic-opt.rs diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 02086c7730cee..35fd30f34ad24 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -233,8 +233,13 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { mut bx: Bx, ) { if self.fn_ty.c_variadic { - if let Some(va_list) = self.va_list_ref { - bx.va_end(va_list.llval); + match self.va_list_ref { + Some(va_list) => { + bx.va_end(va_list.llval); + } + None => { + bug!("C-variadic function must have a `va_list_ref`"); + } } } let llval = match self.fn_ty.ret.mode { diff --git a/src/librustc_codegen_ssa/mir/mod.rs b/src/librustc_codegen_ssa/mir/mod.rs index 870b97401f47a..c1b7502cd8fb2 100644 --- a/src/librustc_codegen_ssa/mir/mod.rs +++ b/src/librustc_codegen_ssa/mir/mod.rs @@ -532,13 +532,7 @@ fn arg_local_refs<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>( PassMode::Ignore(IgnoreMode::Zst) => { return local(OperandRef::new_zst(bx.cx(), arg.layout)); } - PassMode::Ignore(IgnoreMode::CVarArgs) => { - let backend_type = bx.cx().immediate_backend_type(arg.layout); - return local(OperandRef { - val: OperandValue::Immediate(bx.cx().const_undef(backend_type)), - layout: arg.layout, - }); - } + PassMode::Ignore(IgnoreMode::CVarArgs) => {} PassMode::Direct(_) => { let llarg = bx.get_param(bx.llfn(), llarg_idx as c_uint); bx.set_value_name(llarg, &name); diff --git a/src/test/codegen/c-variadic-opt.rs b/src/test/codegen/c-variadic-opt.rs new file mode 100644 index 0000000000000..8594d309b0a5a --- /dev/null +++ b/src/test/codegen/c-variadic-opt.rs @@ -0,0 +1,19 @@ +// compile-flags: -C opt-level=3 + +#![crate_type = "lib"] +#![feature(c_variadic)] +#![no_std] +use core::ffi::VaList; + +extern "C" { + fn vprintf(fmt: *const i8, ap: VaList) -> i32; +} + +// Ensure that `va_start` and `va_end` are properly injected even +// when the "spoofed" `VaList` is not used. +#[no_mangle] +pub unsafe extern "C" fn c_variadic_no_use(fmt: *const i8, mut ap: ...) -> i32 { + // CHECK: call void @llvm.va_start + vprintf(fmt, ap) + // CHECK: call void @llvm.va_end +}