From 9095feb251191805399d2ac953a6d8483345c25a Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Wed, 27 Mar 2019 13:00:37 +0000 Subject: [PATCH] Fix LLVM IR generated for C-variadic arguments It is possible to create malformed LLVM IR given variadic arguments that are aggregate types. This occurs due to improper tracking of the current argument in the functions list of arguments. --- src/librustc_codegen_ssa/mir/block.rs | 15 ++------------- src/test/codegen/c-variadic.rs | 12 +++++++++++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 98da07a905e25..26442faa3215c 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -20,7 +20,7 @@ use syntax_pos::Pos; use super::{FunctionCx, LocalRef}; use super::place::PlaceRef; -use super::operand::{OperandValue, OperandRef}; +use super::operand::OperandRef; use super::operand::OperandValue::{Pair, Ref, Immediate}; /// Used by `FunctionCx::codegen_terminator` for emitting common patterns @@ -695,18 +695,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { // an "spoofed" `VaList`. This argument is ignored, but we need to // populate it with a dummy operand so that the users real arguments // are not overwritten. - let i = if sig.c_variadic && last_arg_idx.map(|x| x == i).unwrap_or(false) { - let layout = match self.cx.tcx().lang_items().va_list() { - Some(did) => bx.cx().layout_of(bx.tcx().type_of(did)), - None => bug!("`va_list` language item required for C-variadics"), - }; - let op = OperandRef { - val: OperandValue::Immediate( - bx.cx().const_undef(bx.cx().immediate_backend_type(layout) - )), - layout: layout, - }; - self.codegen_argument(&mut bx, op, &mut llargs, &fn_ty.args[i]); + let i = if sig.c_variadic && last_arg_idx.map(|x| i >= x).unwrap_or(false) { if i + 1 < fn_ty.args.len() { i + 1 } else { diff --git a/src/test/codegen/c-variadic.rs b/src/test/codegen/c-variadic.rs index 09c18ed90b216..cc2f95e1c28fd 100644 --- a/src/test/codegen/c-variadic.rs +++ b/src/test/codegen/c-variadic.rs @@ -1,4 +1,5 @@ // compile-flags: -C no-prepopulate-passes +// ignore-tidy-linelength #![crate_type = "lib"] #![feature(c_variadic)] @@ -57,7 +58,7 @@ pub unsafe extern "C" fn c_variadic(n: i32, mut ap: ...) -> i32 { // Ensure that we generate the correct `call` signature when calling a Rust // defined C-variadic. -pub unsafe fn test_c_variadic_call() { +pub unsafe fn test_c_variadic_call_0() { // CHECK: call i32 (i32, ...) @c_variadic(i32 0) c_variadic(0); // CHECK: call i32 (i32, ...) @c_variadic(i32 0, i32 42) @@ -67,3 +68,12 @@ pub unsafe fn test_c_variadic_call() { // CHECK: call i32 (i32, ...) @c_variadic(i32 0, i32 42, i32 1024, i32 0) c_variadic(0, 42i32, 1024i32, 0i32); } + +struct Foo(u64, u64); +struct Bar(u64, u64, u64); + +// Ensure that emit arguments of the correct type. +pub unsafe fn test_c_variadic_call_1() { + // CHECK: call void (i32, ...) @foreign_c_variadic_0(i32 0, { i64, i64 } %{{.*}}, %Bar* byval noalias nocapture dereferenceable(24) %{{.*}}) + foreign_c_variadic_0(0, Foo(0, 0), Bar(0, 0, 0)); +}