Skip to content

Commit 214d830

Browse files
committed
refactor(profiling): ErrnoBackup::new is safe
1 parent a9caaa0 commit 214d830

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

profiling/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ features = ["env-filter", "fmt", "smallvec", "std"]
5151
allocator-api2 = { version = "0.2", default-features = false, features = ["alloc"] }
5252
criterion = { version = "0.5.1" }
5353
datadog-php-profiling = { path = ".", features = ["test"] }
54+
static_assertions = "1"
5455

5556
[target.'cfg(target_arch = "x86_64")'.dev-dependencies]
5657
criterion-perf-events = "0.4.0"

profiling/src/io/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,21 @@ struct ErrnoBackup {
3030
impl ErrnoBackup {
3131
/// Snapshots the current `errno` value.
3232
#[inline]
33-
unsafe fn new() -> Self {
34-
let location = libc::__errno_location();
35-
Self {
36-
errno: *location,
37-
location,
38-
}
33+
fn new() -> Self {
34+
// SAFETY: errno location is initialized in program/thread startup.
35+
let location = unsafe { libc::__errno_location() };
36+
// SAFETY: errno pointer is safe to read from same thread.
37+
let errno = unsafe { location.read() };
38+
Self { errno, location }
3939
}
4040
}
4141

4242
impl Drop for ErrnoBackup {
4343
/// Restores `errno` value.
4444
#[inline]
4545
fn drop(&mut self) {
46-
unsafe {
47-
*self.location = self.errno;
48-
}
46+
// SAFETY: points to thread's errno (cached in new), safe for writes.
47+
unsafe { self.location.write(self.errno) }
4948
}
5049
}
5150

@@ -700,3 +699,11 @@ pub fn io_prof_first_rinit() {
700699
};
701700
}
702701
}
702+
703+
#[cfg(test)]
704+
mod tests {
705+
use super::ErrnoBackup;
706+
use static_assertions::assert_not_impl_any;
707+
708+
assert_not_impl_any!(ErrnoBackup: Send, Sync);
709+
}

0 commit comments

Comments
 (0)