Skip to content

Commit 442f997

Browse files
committedJan 3, 2023
Auto merge of #106371 - RalfJung:no-ret-position-noalias, r=nikic
do not add noalias in return position `noalias` as a return attribute in LLVM indicates that the returned pointer does not alias anything else that is reachable from the caller, *including things reachable before this function call*. This is clearly not the case with a function like `fn id(Box<T>) -> Box<T>`, so we cannot use this attribute. Fixes rust-lang/unsafe-code-guidelines#385 (including an actual miscompilation that `@comex` managed to produce).
2 parents 481c9ba + e7cad62 commit 442f997

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
 

‎compiler/rustc_ty_utils/src/abi.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ fn adjust_for_rust_scalar<'tcx>(
273273
| PointerKind::UniqueBorrowed
274274
| PointerKind::UniqueBorrowedPinned => false,
275275
PointerKind::UniqueOwned => noalias_for_box,
276-
PointerKind::Frozen => !is_return,
276+
PointerKind::Frozen => true,
277277
};
278-
if no_alias {
278+
// We can never add `noalias` in return position; that LLVM attribute has some very surprising semantics
279+
// (see <https://github.com/rust-lang/unsafe-code-guidelines/issues/385#issuecomment-1368055745>).
280+
if no_alias && !is_return {
279281
attrs.set(ArgAttribute::NoAlias);
280282
}
281283

‎src/test/codegen/function-arguments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn raw_struct(_: *const S) {
145145

146146
// `Box` can get deallocated during execution of the function, so it should
147147
// not get `dereferenceable`.
148-
// CHECK: noalias noundef nonnull align 4 {{i32\*|ptr}} @_box({{i32\*|ptr}} noalias noundef nonnull align 4 %x)
148+
// CHECK: noundef nonnull align 4 {{i32\*|ptr}} @_box({{i32\*|ptr}} noalias noundef nonnull align 4 %x)
149149
#[no_mangle]
150150
pub fn _box(x: Box<i32>) -> Box<i32> {
151151
x

0 commit comments

Comments
 (0)
Please sign in to comment.