|
| 1 | +#![forbid(fuzzy_provenance_casts)] |
| 2 | + |
1 | 3 | use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
|
2 | 4 |
|
3 | 5 | use crate::collections::HashMap;
|
4 | 6 | use crate::error::Error as StdError;
|
5 | 7 | use crate::ffi::{OsStr, OsString};
|
6 | 8 | use crate::marker::PhantomData;
|
7 | 9 | use crate::path::{self, PathBuf};
|
8 |
| -use crate::sync::atomic::{AtomicUsize, Ordering}; |
9 |
| -use crate::sync::{Mutex, Once}; |
| 10 | +use crate::sync::{LazyLock, Mutex}; |
10 | 11 | use crate::sys::{decode_error_kind, sgx_ineffective, unsupported};
|
11 | 12 | use crate::{fmt, io, str, vec};
|
12 | 13 |
|
@@ -76,24 +77,9 @@ pub fn current_exe() -> io::Result<PathBuf> {
|
76 | 77 | // Specifying linkage/symbol name is solely to ensure a single instance between this crate and its unit tests
|
77 | 78 | #[cfg_attr(test, linkage = "available_externally")]
|
78 | 79 | #[unsafe(export_name = "_ZN16__rust_internals3std3sys3pal3sgx2os3ENVE")]
|
79 |
| -static ENV: AtomicUsize = AtomicUsize::new(0); |
80 |
| -// Specifying linkage/symbol name is solely to ensure a single instance between this crate and its unit tests |
81 |
| -#[cfg_attr(test, linkage = "available_externally")] |
82 |
| -#[unsafe(export_name = "_ZN16__rust_internals3std3sys3pal3sgx2os8ENV_INITE")] |
83 |
| -static ENV_INIT: Once = Once::new(); |
| 80 | +static ENV: LazyLock<EnvStore> = LazyLock::new(|| EnvStore::default()); |
84 | 81 | type EnvStore = Mutex<HashMap<OsString, OsString>>;
|
85 | 82 |
|
86 |
| -fn get_env_store() -> Option<&'static EnvStore> { |
87 |
| - unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() } |
88 |
| -} |
89 |
| - |
90 |
| -fn create_env_store() -> &'static EnvStore { |
91 |
| - ENV_INIT.call_once(|| { |
92 |
| - ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed) |
93 |
| - }); |
94 |
| - unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) } |
95 |
| -} |
96 |
| - |
97 | 83 | pub struct Env {
|
98 | 84 | iter: vec::IntoIter<(OsString, OsString)>,
|
99 | 85 | }
|
@@ -140,31 +126,22 @@ impl Iterator for Env {
|
140 | 126 | }
|
141 | 127 |
|
142 | 128 | pub fn env() -> Env {
|
143 |
| - let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> { |
144 |
| - map.iter().map(|(k, v)| (k.clone(), v.clone())).collect() |
145 |
| - }; |
146 |
| - |
147 |
| - let iter = get_env_store() |
148 |
| - .map(|env| clone_to_vec(&env.lock().unwrap())) |
149 |
| - .unwrap_or_default() |
150 |
| - .into_iter(); |
151 |
| - Env { iter } |
| 129 | + let env = ENV.lock().unwrap().iter().map(|(k, v)| (k.clone(), v.clone())).collect::<Vec<_>>(); |
| 130 | + Env { iter: env.into_iter() } |
152 | 131 | }
|
153 | 132 |
|
154 | 133 | pub fn getenv(k: &OsStr) -> Option<OsString> {
|
155 |
| - get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned()) |
| 134 | + ENV.lock().unwrap().get(k).cloned() |
156 | 135 | }
|
157 | 136 |
|
158 | 137 | pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
|
159 | 138 | let (k, v) = (k.to_owned(), v.to_owned());
|
160 |
| - create_env_store().lock().unwrap().insert(k, v); |
| 139 | + ENV.lock().unwrap().insert(k, v); |
161 | 140 | Ok(())
|
162 | 141 | }
|
163 | 142 |
|
164 | 143 | pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
|
165 |
| - if let Some(env) = get_env_store() { |
166 |
| - env.lock().unwrap().remove(k); |
167 |
| - } |
| 144 | + ENV.lock().unwrap().remove(k); |
168 | 145 | Ok(())
|
169 | 146 | }
|
170 | 147 |
|
|
0 commit comments