Skip to content

Commit 7215f24

Browse files
committed
SGX: Use OnceLock for std::env::{Vars, Args}
Fixes fuzzy provenance casts with `AtomicUsize`.
1 parent 092a284 commit 7215f24

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

library/std/src/sys/args/sgx.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#![allow(fuzzy_provenance_casts)] // FIXME: this module systematically confuses pointers and integers
2-
31
use crate::ffi::OsString;
4-
use crate::sync::atomic::{AtomicUsize, Ordering};
2+
use crate::sync::OnceLock;
53
use crate::sys::os_str::Buf;
64
use crate::sys::pal::abi::usercalls::alloc;
75
use crate::sys::pal::abi::usercalls::raw::ByteBuffer;
@@ -10,23 +8,23 @@ use crate::{fmt, slice};
108

119
#[cfg_attr(test, linkage = "available_externally")]
1210
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3sgx4args4ARGSE")]
13-
static ARGS: AtomicUsize = AtomicUsize::new(0);
11+
static ARGS: OnceLock<ArgsStore> = OnceLock::new();
1412
type ArgsStore = Vec<OsString>;
1513

1614
#[cfg_attr(test, allow(dead_code))]
1715
pub unsafe fn init(argc: isize, argv: *const *const u8) {
1816
if argc != 0 {
19-
let args = unsafe { alloc::User::<[ByteBuffer]>::from_raw_parts(argv as _, argc as _) };
20-
let args = args
21-
.iter()
22-
.map(|a| OsString::from_inner(Buf { inner: a.copy_user_buffer() }))
23-
.collect::<ArgsStore>();
24-
ARGS.store(Box::into_raw(Box::new(args)) as _, Ordering::Relaxed);
17+
ARGS.get_or_init(|| {
18+
let args = unsafe { alloc::User::<[ByteBuffer]>::from_raw_parts(argv as _, argc as _) };
19+
args.iter()
20+
.map(|a| OsString::from_inner(Buf { inner: a.copy_user_buffer() }))
21+
.collect::<ArgsStore>()
22+
});
2523
}
2624
}
2725

2826
pub fn args() -> Args {
29-
let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() };
27+
let args = ARGS.get();
3028
if let Some(args) = args { Args(args.iter()) } else { Args([].iter()) }
3129
}
3230

library/std/src/sys/pal/sgx/os.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
#![forbid(fuzzy_provenance_casts)]
2+
13
use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
24

35
use crate::collections::HashMap;
46
use crate::error::Error as StdError;
57
use crate::ffi::{OsStr, OsString};
68
use crate::marker::PhantomData;
79
use crate::path::{self, PathBuf};
8-
use crate::sync::atomic::{AtomicUsize, Ordering};
9-
use crate::sync::{Mutex, Once};
10+
use crate::sync::{Mutex, OnceLock};
1011
use crate::sys::{decode_error_kind, sgx_ineffective, unsupported};
1112
use crate::{fmt, io, str, vec};
1213

@@ -75,21 +76,15 @@ pub fn current_exe() -> io::Result<PathBuf> {
7576

7677
#[cfg_attr(test, linkage = "available_externally")]
7778
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3sgx2os3ENVE")]
78-
static ENV: AtomicUsize = AtomicUsize::new(0);
79-
#[cfg_attr(test, linkage = "available_externally")]
80-
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3sgx2os8ENV_INITE")]
81-
static ENV_INIT: Once = Once::new();
79+
static ENV: OnceLock<EnvStore> = OnceLock::new();
8280
type EnvStore = Mutex<HashMap<OsString, OsString>>;
8381

8482
fn get_env_store() -> Option<&'static EnvStore> {
85-
unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
83+
ENV.get()
8684
}
8785

8886
fn create_env_store() -> &'static EnvStore {
89-
ENV_INIT.call_once(|| {
90-
ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
91-
});
92-
unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
87+
ENV.get_or_init(|| EnvStore::default())
9388
}
9489

9590
pub struct Env {

0 commit comments

Comments
 (0)