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 https://github.com/rust-lang/unsafe-code-guidelines/issues/385 (including an actual miscompilation that `@comex` managed to produce).
This commit is contained in:
bors 2023-01-03 04:54:03 +00:00
commit 442f997f98
2 changed files with 5 additions and 3 deletions

View file

@ -273,9 +273,11 @@ fn adjust_for_rust_scalar<'tcx>(
| PointerKind::UniqueBorrowed
| PointerKind::UniqueBorrowedPinned => false,
PointerKind::UniqueOwned => noalias_for_box,
PointerKind::Frozen => !is_return,
PointerKind::Frozen => true,
};
if no_alias {
// We can never add `noalias` in return position; that LLVM attribute has some very surprising semantics
// (see <https://github.com/rust-lang/unsafe-code-guidelines/issues/385#issuecomment-1368055745>).
if no_alias && !is_return {
attrs.set(ArgAttribute::NoAlias);
}

View file

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