Skip to content

Commit 2d448ab

Browse files
David Tolnaymeta-codesync[bot]
authored andcommitted
Replace lazy_static with std::sync::LazyLock in fbcode/hermetic_infra
Summary: The `lazy_static` crate is deprecated in favor of a plain `static` containing `std::sync::LazyLock` or `std::sync::OnceLock`. Reviewed By: diliop Differential Revision: D109464793 fbshipit-source-id: 1fc685e8cb2cdbffd8a76a7f61eb1d7cc8392656
1 parent aa8e5ac commit 2d448ab

7 files changed

Lines changed: 31 additions & 43 deletions

File tree

reverie-ptrace/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ close-err = "1.0.2"
1717
futures = { version = "0.3.31", features = ["async-await", "compat"] }
1818
goblin = { version = "0.10.7", features = ["elf32", "elf64", "endian_fd"] }
1919
iced-x86 = "1.21.0"
20-
lazy_static = "1.5"
2120
libc = "0.2.186"
2221
nix = { version = "0.30.1", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "signal", "term", "time", "user", "zerocopy"] }
2322
num-traits = { version = "0.2.19", default-features = false }

reverie-ptrace/src/perf.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
use core::ptr::NonNull;
2626
#[allow(unused_imports)] // only used if we have an error
2727
use std::compile_error;
28+
use std::sync::LazyLock;
2829

29-
use lazy_static::lazy_static;
3030
use nix::sys::signal::Signal;
3131
use nix::unistd::SysconfVar;
3232
use nix::unistd::sysconf;
@@ -40,9 +40,7 @@ use tracing::warn;
4040
use crate::validation::PmuValidationError;
4141
use crate::validation::check_for_pmu_bugs;
4242

43-
lazy_static! {
44-
static ref PMU_BUG: Result<(), PmuValidationError> = check_for_pmu_bugs();
45-
}
43+
static PMU_BUG: LazyLock<Result<(), PmuValidationError>> = LazyLock::new(check_for_pmu_bugs);
4644

4745
// Not available in the libc crate
4846
const F_SETOWN_EX: libc::c_int = 15;
@@ -533,9 +531,7 @@ fn test_perf_pmu_support() -> bool {
533531
false
534532
}
535533

536-
lazy_static! {
537-
static ref IS_PERF_SUPPORTED: bool = test_perf_pmu_support();
538-
}
534+
static IS_PERF_SUPPORTED: LazyLock<bool> = LazyLock::new(test_perf_pmu_support);
539535

540536
/// Returns true if the current system configuration supports use of perf for
541537
/// hardware events.

reverie-ptrace/src/vdso.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
//! Provides APIs to disable VDSOs at runtime.
1010
use std::collections::HashMap;
11+
use std::sync::LazyLock;
1112

1213
use goblin::elf::Elf;
13-
use lazy_static::lazy_static;
1414
use nix::sys::mman::ProtFlags;
1515
use nix::unistd;
1616
use reverie::Error;
@@ -168,33 +168,31 @@ fn align_up(value: usize, alignment: usize) -> usize {
168168
(value + alignment - 1) & alignment.wrapping_neg()
169169
}
170170

171-
lazy_static! {
172-
static ref VDSO_PATCH_INFO: HashMap<&'static str, (u64, usize, &'static [u8])> = {
173-
let info = vdso_get_symbols_info();
174-
let mut res = HashMap::new();
175-
176-
for (k, v) in VDSO_SYMBOLS {
177-
if let Some(&(base, size)) = info.get(*k) {
178-
// NOTE: There is padding at the end of every VDSO entry to
179-
// bring it up to a 16-byte size alignment. The dynamic symbol
180-
// table doesn't report the aligned size, so we must do the same
181-
// alignment here. For example, some VDSO entries might only be
182-
// 5 bytes, but they have padding to align them up to 16 bytes.
183-
let aligned_size = align_up(size, 16);
184-
assert!(
185-
v.len() <= aligned_size,
186-
"vdso symbol {}'s real size is {} bytes, but trying to replace it with {} bytes",
187-
k,
188-
size,
189-
v.len()
190-
);
191-
res.insert(*k, (base, aligned_size, *v));
192-
}
171+
static VDSO_PATCH_INFO: LazyLock<HashMap<&str, (u64, usize, &[u8])>> = LazyLock::new(|| {
172+
let info = vdso_get_symbols_info();
173+
let mut res = HashMap::new();
174+
175+
for (k, v) in VDSO_SYMBOLS {
176+
if let Some(&(base, size)) = info.get(*k) {
177+
// NOTE: There is padding at the end of every VDSO entry to
178+
// bring it up to a 16-byte size alignment. The dynamic symbol
179+
// table doesn't report the aligned size, so we must do the same
180+
// alignment here. For example, some VDSO entries might only be
181+
// 5 bytes, but they have padding to align them up to 16 bytes.
182+
let aligned_size = align_up(size, 16);
183+
assert!(
184+
v.len() <= aligned_size,
185+
"vdso symbol {}'s real size is {} bytes, but trying to replace it with {} bytes",
186+
k,
187+
size,
188+
v.len()
189+
);
190+
res.insert(*k, (base, aligned_size, *v));
193191
}
192+
}
194193

195-
res
196-
};
197-
}
194+
res
195+
});
198196

199197
// get vdso symbols offset/size from current process
200198
// assuming vdso binary is the same for all processes

reverie/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ anyhow = "1.0.102"
1414
async-trait = "0.1.86"
1515
bitflags = { version = "2.11.1", features = ["serde", "std"], default-features = false }
1616
byteorder = "1.5"
17-
lazy_static = "1.5"
1817
libc = "0.2.186"
1918
linked-hash-map = { version = "0.5", features = ["serde_impl"] }
2019
memmap2 = "0.9.10"

reverie/src/backtrace/cache.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
use std::sync::Arc;
10+
use std::sync::LazyLock;
1011
use std::sync::Mutex;
1112
use std::sync::MutexGuard;
1213

@@ -22,9 +23,7 @@ use super::symbols::Symbols;
2223
/// symbols will be removed.
2324
const DEFAULT_MAX_CACHE_SIZE: usize = 1 << 30; // 1 GiB
2425

25-
lazy_static::lazy_static! {
26-
static ref CACHE: Mutex<DebugInfoCache> = Mutex::new(DebugInfoCache::new());
27-
}
26+
static CACHE: LazyLock<Mutex<DebugInfoCache>> = LazyLock::new(|| Mutex::new(DebugInfoCache::new()));
2827

2928
/// An LRU cache of loaded symbols. This is shared by all processes since we
3029
/// only need to load symbols once for a particular inode. However, each process

safeptrace/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ license = "BSD-2-Clause"
1111
[dependencies]
1212
bitflags = { version = "2.11.1", features = ["serde", "std"], default-features = false }
1313
futures = { version = "0.3.31", features = ["async-await", "compat"] }
14-
lazy_static = "1.5"
1514
libc = "0.2.186"
1615
nix = { version = "0.30.1", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "signal", "term", "time", "user", "zerocopy"] }
1716
parking_lot = { version = "0.12.1", features = ["arc_lock", "send_guard"] }

safeptrace/src/notifier.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use std::collections::hash_map::Entry;
6868
use std::future::Future;
6969
use std::pin::Pin;
7070
use std::sync::Arc;
71+
use std::sync::LazyLock;
7172
use std::sync::atomic::AtomicI32;
7273
use std::sync::atomic::Ordering;
7374
use std::task::Context;
@@ -77,7 +78,6 @@ use std::thread;
7778
use std::thread::JoinHandle;
7879

7980
use futures::task::AtomicWaker;
80-
use lazy_static::lazy_static;
8181
use nix::sys::wait::WaitPidFlag;
8282
use nix::sys::wait::WaitStatus;
8383
use parking_lot::Mutex;
@@ -90,9 +90,7 @@ use super::Stopped;
9090
use super::Wait;
9191
use super::waitid;
9292

93-
lazy_static! {
94-
static ref NOTIFIER: Notifier = Notifier::new();
95-
}
93+
static NOTIFIER: LazyLock<Notifier> = LazyLock::new(Notifier::new);
9694

9795
/// A place-holder status used to indicate that no status has been set.
9896
const INVALID_STATUS: i32 = -1;

0 commit comments

Comments
 (0)