Skip to content

Commit

Permalink
Auto merge of rust-lang#137726 - tmiasko:rm-nocapture, r=<try>
Browse files Browse the repository at this point in the history
Remove incorrect use of nocapture attribute

When using indirect pass mode, a callee can obtain an address of an
argument and capture it. The nocapture attribute promises this does not
happen and so it cannot be added unconditionally.
  • Loading branch information
bors committed Feb 27, 2025
2 parents 96cfc75 + 43b0a27 commit 4edc0f4
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 15 deletions.
9 changes: 2 additions & 7 deletions compiler/rustc_target/src/callconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,8 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
let mut attrs = ArgAttributes::new();

// For non-immediate arguments the callee gets its own copy of
// the value on the stack, so there are no aliases. It's also
// program-invisible so can't possibly capture
attrs
.set(ArgAttribute::NoAlias)
.set(ArgAttribute::NoCapture)
.set(ArgAttribute::NonNull)
.set(ArgAttribute::NoUndef);
// the value on the stack, so there are no aliases.
attrs.set(ArgAttribute::NoAlias).set(ArgAttribute::NonNull).set(ArgAttribute::NoUndef);
attrs.pointee_size = layout.size;
attrs.pointee_align = Some(layout.align.abi);

Expand Down
4 changes: 2 additions & 2 deletions tests/codegen/uninhabited-transparent-return-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "Rust" {
pub fn test_uninhabited_ret_by_ref() {
// CHECK: %_1 = alloca [24 x i8], align {{8|4}}
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %_1)
// CHECK-NEXT: call void @opaque(ptr noalias nocapture noundef nonnull sret([24 x i8]) align {{8|4}} dereferenceable(24) %_1) #2
// CHECK-NEXT: call void @opaque(ptr noalias noundef nonnull sret([24 x i8]) align {{8|4}} dereferenceable(24) %_1) #2
// CHECK-NEXT: unreachable
unsafe {
opaque();
Expand All @@ -36,7 +36,7 @@ pub fn test_uninhabited_ret_by_ref() {
pub fn test_uninhabited_ret_by_ref_with_arg(rsi: u32) {
// CHECK: %_2 = alloca [24 x i8], align {{8|4}}
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %_2)
// CHECK-NEXT: call void @opaque_with_arg(ptr noalias nocapture noundef nonnull sret([24 x i8]) align {{8|4}} dereferenceable(24) %_2, i32 noundef %rsi) #2
// CHECK-NEXT: call void @opaque_with_arg(ptr noalias noundef nonnull sret([24 x i8]) align {{8|4}} dereferenceable(24) %_2, i32 noundef %rsi) #2
// CHECK-NEXT: unreachable
unsafe {
opaque_with_arg(rsi);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/abi/c-zst.powerpc-linux.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: fn_abi_of(pass_zst) = FnAbi {
},
mode: Indirect {
attrs: ArgAttributes {
regular: NoAlias | NoCapture | NonNull | NoUndef,
regular: NoAlias | NonNull | NoUndef,
arg_ext: None,
pointee_size: Size(0 bytes),
pointee_align: Some(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/abi/c-zst.s390x-linux.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: fn_abi_of(pass_zst) = FnAbi {
},
mode: Indirect {
attrs: ArgAttributes {
regular: NoAlias | NoCapture | NonNull | NoUndef,
regular: NoAlias | NonNull | NoUndef,
arg_ext: None,
pointee_size: Size(0 bytes),
pointee_align: Some(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/abi/c-zst.sparc64-linux.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: fn_abi_of(pass_zst) = FnAbi {
},
mode: Indirect {
attrs: ArgAttributes {
regular: NoAlias | NoCapture | NonNull | NoUndef,
regular: NoAlias | NonNull | NoUndef,
arg_ext: None,
pointee_size: Size(0 bytes),
pointee_align: Some(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/abi/c-zst.x86_64-pc-windows-gnu.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: fn_abi_of(pass_zst) = FnAbi {
},
mode: Indirect {
attrs: ArgAttributes {
regular: NoAlias | NoCapture | NonNull | NoUndef,
regular: NoAlias | NonNull | NoUndef,
arg_ext: None,
pointee_size: Size(0 bytes),
pointee_align: Some(
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/abi/debug.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ error: ABIs are not compatible
},
mode: Indirect {
attrs: ArgAttributes {
regular: NoAlias | NoCapture | NonNull | NoUndef,
regular: NoAlias | NonNull | NoUndef,
arg_ext: None,
pointee_size: Size(32 bytes),
pointee_align: Some(
Expand Down Expand Up @@ -540,7 +540,7 @@ error: ABIs are not compatible
},
mode: Indirect {
attrs: ArgAttributes {
regular: NoAlias | NoCapture | NonNull | NoUndef,
regular: NoAlias | NonNull | NoUndef,
arg_ext: None,
pointee_size: Size(128 bytes),
pointee_align: Some(
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/codegen/indirect-nocapture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Regression test for issue #137668 where an indirect argument have been marked as nocapture
// despite the fact that callee did in fact capture the address.
//
//@ run-pass
//@ compile-flags: -Copt-level=2

#[inline(never)]
pub fn f(a: [u32; 64], b: [u32; 64]) -> bool {
&a as *const _ as usize != &b as *const _ as usize
}

fn main() {
static S: [u32; 64] = [0; 64];
assert!(f(S, S));
}

0 comments on commit 4edc0f4

Please sign in to comment.