Skip to content

Commit

Permalink
Rollup merge of rust-lang#59577 - dlrobertson:fix_58881, r=nagisa
Browse files Browse the repository at this point in the history
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.

Fixes: rust-lang#58881
  • Loading branch information
Centril authored Mar 31, 2019
2 parents 6ee2c10 + 9095feb commit 3d308ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 2 additions & 13 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/test/codegen/c-variadic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// compile-flags: -C no-prepopulate-passes
// ignore-tidy-linelength

#![crate_type = "lib"]
#![feature(c_variadic)]
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}

0 comments on commit 3d308ee

Please sign in to comment.