Various fixups to unsafe code (#37651)

A collection of fixups of possibly-unsound code and removing some small
useless writes.

Release Notes:

- N/A
This commit is contained in:
Nia
2025-09-06 00:27:14 +02:00
committed by GitHub
parent 64b6e8ba0f
commit 59bdbf5a5d
8 changed files with 183 additions and 173 deletions
+7 -7
View File
@@ -22,7 +22,7 @@ pub const LEVEL_ENABLED_MAX_DEFAULT: log::LevelFilter = log::LevelFilter::Info;
/// crate that the max level is everything, so that we can dynamically enable
/// logs that are more verbose than this level without the `log` crate throwing
/// them away before we see them
static mut LEVEL_ENABLED_MAX_STATIC: log::LevelFilter = LEVEL_ENABLED_MAX_DEFAULT;
static LEVEL_ENABLED_MAX_STATIC: AtomicU8 = AtomicU8::new(LEVEL_ENABLED_MAX_DEFAULT as u8);
/// A cache of the true maximum log level that _could_ be printed. This is based
/// on the maximally verbose level that is configured by the user, and is used
@@ -46,7 +46,7 @@ const DEFAULT_FILTERS: &[(&str, log::LevelFilter)] = &[
pub fn init_env_filter(filter: env_config::EnvFilter) {
if let Some(level_max) = filter.level_global {
unsafe { LEVEL_ENABLED_MAX_STATIC = level_max }
LEVEL_ENABLED_MAX_STATIC.store(level_max as u8, Ordering::Release)
}
if ENV_FILTER.set(filter).is_err() {
panic!("Environment filter cannot be initialized twice");
@@ -54,7 +54,7 @@ pub fn init_env_filter(filter: env_config::EnvFilter) {
}
pub fn is_possibly_enabled_level(level: log::Level) -> bool {
level as u8 <= LEVEL_ENABLED_MAX_CONFIG.load(Ordering::Relaxed)
level as u8 <= LEVEL_ENABLED_MAX_CONFIG.load(Ordering::Acquire)
}
pub fn is_scope_enabled(scope: &Scope, module_path: Option<&str>, level: log::Level) -> bool {
@@ -66,7 +66,7 @@ pub fn is_scope_enabled(scope: &Scope, module_path: Option<&str>, level: log::Le
// scope map
return false;
}
let is_enabled_by_default = level <= unsafe { LEVEL_ENABLED_MAX_STATIC };
let is_enabled_by_default = level as u8 <= LEVEL_ENABLED_MAX_STATIC.load(Ordering::Acquire);
let global_scope_map = SCOPE_MAP.read().unwrap_or_else(|err| {
SCOPE_MAP.clear_poison();
err.into_inner()
@@ -92,13 +92,13 @@ pub fn is_scope_enabled(scope: &Scope, module_path: Option<&str>, level: log::Le
pub fn refresh_from_settings(settings: &HashMap<String, String>) {
let env_config = ENV_FILTER.get();
let map_new = ScopeMap::new_from_settings_and_env(settings, env_config, DEFAULT_FILTERS);
let mut level_enabled_max = unsafe { LEVEL_ENABLED_MAX_STATIC };
let mut level_enabled_max = LEVEL_ENABLED_MAX_STATIC.load(Ordering::Acquire);
for entry in &map_new.entries {
if let Some(level) = entry.enabled {
level_enabled_max = level_enabled_max.max(level);
level_enabled_max = level_enabled_max.max(level as u8);
}
}
LEVEL_ENABLED_MAX_CONFIG.store(level_enabled_max as u8, Ordering::Release);
LEVEL_ENABLED_MAX_CONFIG.store(level_enabled_max, Ordering::Release);
{
let mut global_map = SCOPE_MAP.write().unwrap_or_else(|err| {
+18 -20
View File
@@ -4,7 +4,7 @@ use std::{
path::PathBuf,
sync::{
Mutex, OnceLock,
atomic::{AtomicU64, Ordering},
atomic::{AtomicBool, AtomicU64, Ordering},
},
};
@@ -19,17 +19,17 @@ const ANSI_GREEN: &str = "\x1b[32m";
const ANSI_BLUE: &str = "\x1b[34m";
const ANSI_MAGENTA: &str = "\x1b[35m";
/// Whether stdout output is enabled.
static mut ENABLED_SINKS_STDOUT: bool = false;
/// Whether stderr output is enabled.
static mut ENABLED_SINKS_STDERR: bool = false;
/// Is Some(file) if file output is enabled.
static ENABLED_SINKS_FILE: Mutex<Option<std::fs::File>> = Mutex::new(None);
static SINK_FILE_PATH: OnceLock<&'static PathBuf> = OnceLock::new();
static SINK_FILE_PATH_ROTATE: OnceLock<&'static PathBuf> = OnceLock::new();
// NB: Since this can be accessed in tests, we probably should stick to atomics here.
/// Whether stdout output is enabled.
static ENABLED_SINKS_STDOUT: AtomicBool = AtomicBool::new(false);
/// Whether stderr output is enabled.
static ENABLED_SINKS_STDERR: AtomicBool = AtomicBool::new(false);
/// Atomic counter for the size of the log file in bytes.
// TODO: make non-atomic if writing single threaded
static SINK_FILE_SIZE_BYTES: AtomicU64 = AtomicU64::new(0);
/// Maximum size of the log file before it will be rotated, in bytes.
const SINK_FILE_SIZE_BYTES_MAX: u64 = 1024 * 1024; // 1 MB
@@ -42,15 +42,13 @@ pub struct Record<'a> {
}
pub fn init_output_stdout() {
unsafe {
ENABLED_SINKS_STDOUT = true;
}
// Use atomics here instead of just a `static mut`, since in the context
// of tests these accesses can be multi-threaded.
ENABLED_SINKS_STDOUT.store(true, Ordering::Release);
}
pub fn init_output_stderr() {
unsafe {
ENABLED_SINKS_STDERR = true;
}
ENABLED_SINKS_STDERR.store(true, Ordering::Release);
}
pub fn init_output_file(
@@ -79,7 +77,7 @@ pub fn init_output_file(
if size_bytes >= SINK_FILE_SIZE_BYTES_MAX {
rotate_log_file(&mut file, Some(path), path_rotate, &SINK_FILE_SIZE_BYTES);
} else {
SINK_FILE_SIZE_BYTES.store(size_bytes, Ordering::Relaxed);
SINK_FILE_SIZE_BYTES.store(size_bytes, Ordering::Release);
}
*enabled_sinks_file = Some(file);
@@ -108,7 +106,7 @@ static LEVEL_ANSI_COLORS: [&str; 6] = [
// PERF: batching
pub fn submit(record: Record) {
if unsafe { ENABLED_SINKS_STDOUT } {
if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) {
let mut stdout = std::io::stdout().lock();
_ = writeln!(
&mut stdout,
@@ -123,7 +121,7 @@ pub fn submit(record: Record) {
},
record.message
);
} else if unsafe { ENABLED_SINKS_STDERR } {
} else if ENABLED_SINKS_STDERR.load(Ordering::Acquire) {
let mut stdout = std::io::stderr().lock();
_ = writeln!(
&mut stdout,
@@ -173,7 +171,7 @@ pub fn submit(record: Record) {
},
record.message
);
SINK_FILE_SIZE_BYTES.fetch_add(writer.written, Ordering::Relaxed) + writer.written
SINK_FILE_SIZE_BYTES.fetch_add(writer.written, Ordering::AcqRel) + writer.written
};
if file_size_bytes > SINK_FILE_SIZE_BYTES_MAX {
rotate_log_file(
@@ -187,7 +185,7 @@ pub fn submit(record: Record) {
}
pub fn flush() {
if unsafe { ENABLED_SINKS_STDOUT } {
if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) {
_ = std::io::stdout().lock().flush();
}
let mut file = ENABLED_SINKS_FILE.lock().unwrap_or_else(|handle| {
@@ -265,7 +263,7 @@ fn rotate_log_file<PathRef>(
// according to the documentation, it only fails if:
// - the file is not writeable: should never happen,
// - the size would cause an overflow (implementation specific): 0 should never cause an overflow
atomic_size.store(0, Ordering::Relaxed);
atomic_size.store(0, Ordering::Release);
}
#[cfg(test)]
@@ -298,7 +296,7 @@ mod tests {
std::fs::read_to_string(&rotation_log_file_path).unwrap(),
contents,
);
assert_eq!(size.load(Ordering::Relaxed), 0);
assert_eq!(size.load(Ordering::Acquire), 0);
}
/// Regression test, ensuring that if log level values change we are made aware