Skip to content

Commit 4c548fc

Browse files
committed
SGX: Fix fuzzy provenance casts with AtomicUsize
1 parent 092a284 commit 4c548fc

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
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::atomic::{AtomicPtr, Ordering};
53
use crate::sys::os_str::Buf;
64
use crate::sys::pal::abi::usercalls::alloc;
75
use crate::sys::pal::abi::usercalls::raw::ByteBuffer;
86
use crate::sys_common::FromInner;
9-
use crate::{fmt, slice};
7+
use crate::{fmt, ptr, 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: AtomicPtr<ArgsStore> = AtomicPtr::new(ptr::null_mut());
1412
type ArgsStore = Vec<OsString>;
1513

1614
#[cfg_attr(test, allow(dead_code))]
@@ -21,12 +19,12 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
2119
.iter()
2220
.map(|a| OsString::from_inner(Buf { inner: a.copy_user_buffer() }))
2321
.collect::<ArgsStore>();
24-
ARGS.store(Box::into_raw(Box::new(args)) as _, Ordering::Relaxed);
22+
ARGS.store(Box::into_raw(Box::new(args)), Ordering::Relaxed);
2523
}
2624
}
2725

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

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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};
10+
use crate::sync::atomic::{AtomicPtr, Ordering};
911
use crate::sync::{Mutex, Once};
1012
use crate::sys::{decode_error_kind, sgx_ineffective, unsupported};
11-
use crate::{fmt, io, str, vec};
13+
use crate::{fmt, io, ptr, str, vec};
1214

1315
pub fn errno() -> i32 {
1416
RESULT_SUCCESS
@@ -75,21 +77,20 @@ pub fn current_exe() -> io::Result<PathBuf> {
7577

7678
#[cfg_attr(test, linkage = "available_externally")]
7779
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3sgx2os3ENVE")]
78-
static ENV: AtomicUsize = AtomicUsize::new(0);
80+
static ENV: AtomicPtr<EnvStore> = AtomicPtr::new(ptr::null_mut());
7981
#[cfg_attr(test, linkage = "available_externally")]
8082
#[unsafe(export_name = "_ZN16__rust_internals3std3sys3sgx2os8ENV_INITE")]
8183
static ENV_INIT: Once = Once::new();
8284
type EnvStore = Mutex<HashMap<OsString, OsString>>;
8385

8486
fn get_env_store() -> Option<&'static EnvStore> {
85-
unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
87+
unsafe { ENV.load(Ordering::Relaxed).as_ref() }
8688
}
8789

8890
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) }
91+
ENV_INIT
92+
.call_once(|| ENV.store(Box::into_raw(Box::new(EnvStore::default())), Ordering::Relaxed));
93+
unsafe { &*ENV.load(Ordering::Relaxed) }
9394
}
9495

9596
pub struct Env {

0 commit comments

Comments
 (0)