Skip to content

Commit 6e475af

Browse files
committed
Merge ref 'd087f112b7d1:/library/compiler-builtins' from https://github.com/rust-lang/rust
Pull recent changes from rust-lang/rust via Josh. Upstream ref: d087f112b7d1323446c7b39a8b616aee7fa56b3d Filtered ref: 2d43ce8
2 parents fc6b151 + 2d43ce8 commit 6e475af

File tree

7 files changed

+206
-292
lines changed

7 files changed

+206
-292
lines changed

compiler-builtins/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ doctest = false
1515
test = false
1616

1717
[dependencies]
18-
# For more information on this dependency see
19-
# https://github.com/rust-lang/rust/tree/master/library/rustc-std-workspace-core
20-
core = { version = "1.0.1", optional = true, package = "rustc-std-workspace-core" }
18+
core = { path = "../../core", optional = true }
2119

2220
[build-dependencies]
2321
cc = { optional = true, version = "1.2" }

compiler-builtins/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ fn main() {
1919

2020
println!("cargo:compiler-rt={}", cwd.join("compiler-rt").display());
2121

22+
println!("cargo::rustc-check-cfg=cfg(kernel_user_helpers)");
23+
println!("cargo::rustc-check-cfg=cfg(feature, values(\"mem-unaligned\"))");
24+
2225
// Emscripten's runtime includes all the builtins
2326
if target.os == "emscripten" {
2427
return;
@@ -44,7 +47,6 @@ fn main() {
4447
}
4548

4649
// These targets have hardware unaligned access support.
47-
println!("cargo::rustc-check-cfg=cfg(feature, values(\"mem-unaligned\"))");
4850
if target.arch.contains("x86_64")
4951
|| target.arch.contains("x86")
5052
|| target.arch.contains("aarch64")
@@ -75,7 +77,6 @@ fn main() {
7577
// Only emit the ARM Linux atomic emulation on pre-ARMv6 architectures. This
7678
// includes the old androideabi. It is deprecated but it is available as a
7779
// rustc target (arm-linux-androideabi).
78-
println!("cargo::rustc-check-cfg=cfg(kernel_user_helpers)");
7980
if llvm_target[0] == "armv4t"
8081
|| llvm_target[0] == "armv5te"
8182
|| target.triple == "arm-linux-androideabi"

compiler-builtins/src/aarch64_linux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! To avoid breaking backwards compat, C toolchains introduced a concept of "outlined atomics",
55
//! where atomic operations call into the compiler runtime to dispatch between two depending on
66
//! which is supported on the current CPU.
7-
//! See https://community.arm.com/arm-community-blogs/b/tools-software-ides-blog/posts/making-the-most-of-the-arm-architecture-in-gcc-10#:~:text=out%20of%20line%20atomics for more discussion.
7+
//! See <https://community.arm.com/arm-community-blogs/b/tools-software-ides-blog/posts/making-the-most-of-the-arm-architecture-in-gcc-10#:~:text=out%20of%20line%20atomics> for more discussion.
88
//!
99
//! Currently we only support LL/SC, because LSE requires `getauxval` from libc in order to do runtime detection.
1010
//! Use the `compiler-rt` intrinsics if you want LSE support.

compiler-builtins/src/arm_linux.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ use core::{arch, mem};
44
// Kernel-provided user-mode helper functions:
55
// https://www.kernel.org/doc/Documentation/arm/kernel_user_helpers.txt
66
unsafe fn __kuser_cmpxchg(oldval: u32, newval: u32, ptr: *mut u32) -> bool {
7-
let f: extern "C" fn(u32, u32, *mut u32) -> u32 = mem::transmute(0xffff0fc0usize as *const ());
7+
// FIXME(volatile): the third parameter is a volatile pointer
8+
// SAFETY: kernel docs specify a known address with the given signature
9+
let f = unsafe {
10+
mem::transmute::<_, extern "C" fn(u32, u32, *mut u32) -> u32>(0xffff0fc0usize as *const ())
11+
};
812
f(oldval, newval, ptr) == 0
913
}
1014

1115
unsafe fn __kuser_memory_barrier() {
12-
let f: extern "C" fn() = mem::transmute(0xffff0fa0usize as *const ());
16+
// SAFETY: kernel docs specify a known address with the given signature
17+
let f = unsafe { mem::transmute::<_, extern "C" fn()>(0xffff0fa0usize as *const ()) };
1318
f();
1419
}
1520

@@ -67,8 +72,10 @@ fn insert_aligned(aligned: u32, val: u32, shift: u32, mask: u32) -> u32 {
6772
/// - if `size_of::<T>() == 2`, `ptr` or `ptr` offset by 2 bytes must be valid for a relaxed atomic
6873
/// read of 2 bytes.
6974
/// - if `size_of::<T>() == 4`, `ptr` must be valid for a relaxed atomic read of 4 bytes.
75+
// FIXME: assert some of the preconditions in debug mode
7076
unsafe fn atomic_load_aligned<T>(ptr: *mut u32) -> u32 {
71-
if mem::size_of::<T>() == 4 {
77+
const { assert!(size_of::<T>() <= 4) };
78+
if size_of::<T>() == 4 {
7279
// SAFETY: As `T` has a size of 4, the caller garantees this is sound.
7380
unsafe { AtomicU32::from_ptr(ptr).load(Ordering::Relaxed) }
7481
} else {
@@ -100,11 +107,13 @@ unsafe fn atomic_rmw<T, F: Fn(u32) -> u32, G: Fn(u32, u32) -> u32>(ptr: *mut T,
100107
let (shift, mask) = get_shift_mask(ptr);
101108

102109
loop {
103-
let curval_aligned = atomic_load_aligned::<T>(aligned_ptr);
110+
// FIXME(safety): preconditions review needed
111+
let curval_aligned = unsafe { atomic_load_aligned::<T>(aligned_ptr) };
104112
let curval = extract_aligned(curval_aligned, shift, mask);
105113
let newval = f(curval);
106114
let newval_aligned = insert_aligned(curval_aligned, newval, shift, mask);
107-
if __kuser_cmpxchg(curval_aligned, newval_aligned, aligned_ptr) {
115+
// FIXME(safety): preconditions review needed
116+
if unsafe { __kuser_cmpxchg(curval_aligned, newval_aligned, aligned_ptr) } {
108117
return g(curval, newval);
109118
}
110119
}
@@ -116,13 +125,15 @@ unsafe fn atomic_cmpxchg<T>(ptr: *mut T, oldval: u32, newval: u32) -> u32 {
116125
let (shift, mask) = get_shift_mask(ptr);
117126

118127
loop {
119-
let curval_aligned = atomic_load_aligned::<T>(aligned_ptr);
128+
// FIXME(safety): preconditions review needed
129+
let curval_aligned = unsafe { atomic_load_aligned::<T>(aligned_ptr) };
120130
let curval = extract_aligned(curval_aligned, shift, mask);
121131
if curval != oldval {
122132
return curval;
123133
}
124134
let newval_aligned = insert_aligned(curval_aligned, newval, shift, mask);
125-
if __kuser_cmpxchg(curval_aligned, newval_aligned, aligned_ptr) {
135+
// FIXME(safety): preconditions review needed
136+
if unsafe { __kuser_cmpxchg(curval_aligned, newval_aligned, aligned_ptr) } {
126137
return oldval;
127138
}
128139
}
@@ -132,7 +143,14 @@ macro_rules! atomic_rmw {
132143
($name:ident, $ty:ty, $op:expr, $fetch:expr) => {
133144
intrinsics! {
134145
pub unsafe extern "C" fn $name(ptr: *mut $ty, val: $ty) -> $ty {
135-
atomic_rmw(ptr, |x| $op(x as $ty, val) as u32, |old, new| $fetch(old, new)) as $ty
146+
// FIXME(safety): preconditions review needed
147+
unsafe {
148+
atomic_rmw(
149+
ptr,
150+
|x| $op(x as $ty, val) as u32,
151+
|old, new| $fetch(old, new)
152+
) as $ty
153+
}
136154
}
137155
}
138156
};
@@ -149,7 +167,8 @@ macro_rules! atomic_cmpxchg {
149167
($name:ident, $ty:ty) => {
150168
intrinsics! {
151169
pub unsafe extern "C" fn $name(ptr: *mut $ty, oldval: $ty, newval: $ty) -> $ty {
152-
atomic_cmpxchg(ptr, oldval as u32, newval as u32) as $ty
170+
// FIXME(safety): preconditions review needed
171+
unsafe { atomic_cmpxchg(ptr, oldval as u32, newval as u32) as $ty }
153172
}
154173
}
155174
};
@@ -285,6 +304,7 @@ atomic_cmpxchg!(__sync_val_compare_and_swap_4, u32);
285304

286305
intrinsics! {
287306
pub unsafe extern "C" fn __sync_synchronize() {
288-
__kuser_memory_barrier();
307+
// SAFETY: preconditions are the same as the calling function.
308+
unsafe { __kuser_memory_barrier() };
289309
}
290310
}

compiler-builtins/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(naked_functions)]
1010
#![feature(repr_simd)]
1111
#![feature(macro_metavar_expr_concat)]
12+
#![feature(rustc_attrs)]
1213
#![cfg_attr(f16_enabled, feature(f16))]
1314
#![cfg_attr(f128_enabled, feature(f128))]
1415
#![no_builtins]

compiler-builtins/src/mem/x86_64.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, count: usize) {
6969
"rep movsb",
7070
"sub $7, %rsi",
7171
"sub $7, %rdi",
72-
"mov {qword_count}, %rcx",
72+
"mov {qword_count:r}, %rcx",
7373
"rep movsq",
7474
"test {pre_byte_count:e}, {pre_byte_count:e}",
7575
"add $7, %rsi",
@@ -212,7 +212,7 @@ pub unsafe fn c_string_length(mut s: *const core::ffi::c_char) -> usize {
212212
let x = {
213213
let r;
214214
asm!(
215-
"movdqa ({addr}), {dest}",
215+
"movdqa ({addr:r}), {dest}",
216216
addr = in(reg) s,
217217
dest = out(xmm_reg) r,
218218
options(att_syntax, nostack),
@@ -232,7 +232,7 @@ pub unsafe fn c_string_length(mut s: *const core::ffi::c_char) -> usize {
232232
let x = {
233233
let r;
234234
asm!(
235-
"movdqa ({addr}), {dest}",
235+
"movdqa ({addr:r}), {dest}",
236236
addr = in(reg) s,
237237
dest = out(xmm_reg) r,
238238
options(att_syntax, nostack),

0 commit comments

Comments
 (0)