Skip to content

Add missing attributes to indirect calls for foreign functions #14982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/librustc/middle/trans/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use back::{link};
use lib::llvm::llvm;
use lib::llvm::{ValueRef, CallConv, StructRetAttribute, Linkage};
use lib::llvm::{ValueRef, CallConv, Linkage};
use lib;
use middle::weak_lang_items;
use middle::trans::base::push_ctxt;
Expand Down Expand Up @@ -373,18 +373,33 @@ pub fn trans_native_call<'a>(
};

// A function pointer is called without the declaration available, so we have to apply
// any attributes with ABI implications directly to the call instruction. Right now, the
// only attribute we need to worry about is `sret`.
// any attributes with ABI implications directly to the call instruction.
let mut attrs = Vec::new();
if fn_type.ret_ty.is_indirect() {
attrs.push((1, lib::llvm::StructRetAttribute as u64));

// Add attributes that are always applicable, independent of the concrete foreign ABI
if fn_type.ret_ty.is_indirect() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this accidentally removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's on purpose. StructRetAttribute is handled via the attr field of the ArgType.

Edit: The code to set attr to StructRetAttribute was already present and the value is used in add_argument_attributes. We just completely ignored attr here and the hard-coded attribute here had to make up for it.

// The outptr can be noalias and nocapture because it's entirely
// invisible to the program. We can also mark it as nonnull
attrs.push((1, lib::llvm::NoAliasAttribute as u64));
attrs.push((1, lib::llvm::NoCaptureAttribute as u64));
attrs.push((1, lib::llvm::NonNullAttribute as u64));
};

// Add attributes that depend on the concrete foreign ABI
let ret_offset = if fn_type.ret_ty.is_indirect() { 1 } else { 0 };
match fn_type.ret_ty.attr {
Some(attr) => attrs.push((ret_offset, attr as u64)),
_ => ()
}

let first_arg_offset = ret_offset + 1;
for (idx, &arg_ty) in fn_type.arg_tys.iter().enumerate() {
match arg_ty.attr {
Some(attr) => attrs.push((first_arg_offset + idx, attr as u64)),
_ => {}
}
}

let llforeign_retval = CallWithConv(bcx,
llfn,
llargs_foreign.as_slice(),
Expand Down
18 changes: 18 additions & 0 deletions src/rt/rust_test_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,21 @@ void
rust_dbg_static_mut_check_four() {
assert(rust_dbg_static_mut == 4);
}

struct S {
uint64_t x;
uint64_t y;
uint64_t z;
};

uint64_t get_x(struct S s) {
return s.x;
}

uint64_t get_y(struct S s) {
return s.y;
}

uint64_t get_z(struct S s) {
return s.z;
}
36 changes: 36 additions & 0 deletions src/test/run-pass/foreign-fn-with-byval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub struct S {
x: u64,
y: u64,
z: u64,
}

#[link(name = "rust_test_helpers")]
extern {
pub fn get_x(x: S) -> u64;
pub fn get_y(x: S) -> u64;
pub fn get_z(x: S) -> u64;
}

#[inline(never)]
fn indirect_call(func: unsafe extern fn(s: S) -> u64, s: S) -> u64 {
unsafe {
func(s)
}
}

fn main() {
let s = S { x: 1, y: 2, z: 3 };
assert_eq!(s.x, indirect_call(get_x, s));
assert_eq!(s.y, indirect_call(get_y, s));
assert_eq!(s.z, indirect_call(get_z, s));
}