Skip to content

Commit 3c47542

Browse files
authored
Add safety comments to the thread_pointer implementations. (#104)
Add `SAFETY:` comments to the unsafe blocks in the `thread_pointer` implementations.
1 parent 6ab244c commit 3c47542

6 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/arch/aarch64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
192192
#[inline]
193193
pub(super) fn thread_pointer() -> *mut c_void {
194194
let ptr;
195+
// SAFETY: This reads the thread register.
195196
unsafe {
196197
asm!("mrs {}, tpidr_el0", out(reg) ptr, options(nostack, preserves_flags, readonly));
197198
}

src/arch/arm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
193193
#[inline]
194194
pub(super) fn thread_pointer() -> *mut c_void {
195195
let ptr;
196+
// SAFETY: This reads the thread register.
196197
unsafe {
197198
asm!("mrc p15, 0, {}, c13, c0, 3", out(reg) ptr);
198199
}

src/arch/riscv64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
192192
#[inline]
193193
pub(super) fn thread_pointer() -> *mut c_void {
194194
let ptr;
195+
// SAFETY: This reads the thread register.
195196
unsafe {
196197
asm!("mv {}, tp", out(reg) ptr, options(nostack, preserves_flags, readonly));
197198
}

src/arch/x86.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
259259
#[inline]
260260
pub(super) fn thread_pointer() -> *mut c_void {
261261
let ptr;
262+
// SAFETY: On x86, reading the thread register itself is expensive, so the
263+
// ABI specifies that the thread pointer value is also stored in memory at
264+
// offset 0 from the thread pointer value, where it can be read with just a
265+
// load.
262266
unsafe {
263267
asm!("mov {}, gs:0", out(reg) ptr, options(nostack, preserves_flags, readonly));
264268
}

src/arch/x86_64.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
199199
#[inline]
200200
pub(super) fn thread_pointer() -> *mut c_void {
201201
let ptr;
202+
// SAFETY: On x86_64, reading the thread register itself is expensive, so
203+
// the ABI specifies that the thread pointer value is also stored in memory
204+
// at offset 0 from the thread pointer value, where it can be read with
205+
// just a load.
202206
unsafe {
203207
asm!("mov {}, fs:0", out(reg) ptr, options(nostack, preserves_flags, readonly));
204208
}

src/thread/linux_raw.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ pub fn current_id() -> ThreadId {
963963
// Don't use the `id` function here because it returns an `Option` to
964964
// handle the case where the thread has exited. We're querying the current
965965
// thread which we know is still running because we're on it.
966+
//
966967
// SAFETY: All threads have been initialized, including the main thread
967968
// with `initialize_main`, so `current()` returns a valid pointer.
968969
let tid = unsafe { ThreadId::from_raw_unchecked(current().0.as_ref().thread_id.load(SeqCst)) };

0 commit comments

Comments
 (0)