Skip to content

Commit 7bea6a1

Browse files
author
Jethro Beekman
committed
SGX target: implement command-line arguments and environment variables
1 parent 6650f43 commit 7bea6a1

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed

Diff for: src/libstd/sys/sgx/args.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,67 @@
99
// except according to those terms.
1010

1111
use ffi::OsString;
12-
use fortanix_sgx_abi::ByteBuffer;
12+
use super::abi::usercalls::{copy_user_buffer, alloc, ByteBuffer};
13+
use sync::atomic::{AtomicUsize, Ordering};
14+
use sys::os_str::Buf;
15+
use sys_common::FromInner;
16+
use slice;
1317

14-
pub unsafe fn init(argc: isize, argv: *const *const u8) {
15-
// See ABI
16-
let _len: usize = argc as _;
17-
let _args: *const ByteBuffer = argv as _;
18+
static ARGS: AtomicUsize = AtomicUsize::new(0);
19+
type ArgsStore = Vec<OsString>;
1820

19-
// TODO
21+
pub unsafe fn init(argc: isize, argv: *const *const u8) {
22+
if argc != 0 {
23+
let args = alloc::User::<[ByteBuffer]>::from_raw_parts(argv as _, argc as _);
24+
let args = args.iter()
25+
.map( |a| OsString::from_inner(Buf { inner: copy_user_buffer(a) }) )
26+
.collect::<ArgsStore>();
27+
ARGS.store(Box::into_raw(Box::new(args)) as _, Ordering::Relaxed);
28+
}
2029
}
2130

2231
pub unsafe fn cleanup() {
32+
let args = ARGS.swap(0, Ordering::Relaxed);
33+
if args != 0 {
34+
drop(Box::<ArgsStore>::from_raw(args as _))
35+
}
2336
}
2437

2538
pub fn args() -> Args {
26-
Args
39+
let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() };
40+
if let Some(args) = args {
41+
Args(args.iter())
42+
} else {
43+
Args([].iter())
44+
}
2745
}
2846

29-
pub struct Args;
47+
pub struct Args(slice::Iter<'static, OsString>);
3048

3149
impl Args {
3250
pub fn inner_debug(&self) -> &[OsString] {
33-
&[]
51+
self.0.as_slice()
3452
}
3553
}
3654

3755
impl Iterator for Args {
3856
type Item = OsString;
3957
fn next(&mut self) -> Option<OsString> {
40-
None
58+
self.0.next().cloned()
4159
}
4260
fn size_hint(&self) -> (usize, Option<usize>) {
43-
(0, Some(0))
61+
self.0.size_hint()
4462
}
4563
}
4664

4765
impl ExactSizeIterator for Args {
4866
fn len(&self) -> usize {
49-
0
67+
self.0.len()
5068
}
5169
}
5270

5371
impl DoubleEndedIterator for Args {
5472
fn next_back(&mut self) -> Option<OsString> {
55-
None
73+
self.0.next_back().cloned()
5674
}
5775
}

Diff for: src/libstd/sys/sgx/os.rs

+39-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ use io;
1717
use path::{self, PathBuf};
1818
use str;
1919
use sys::{unsupported, Void, sgx_ineffective, decode_error_kind};
20+
use collections::HashMap;
21+
use vec;
22+
use sync::Mutex;
23+
use sync::atomic::{AtomicUsize, Ordering};
24+
use sync::Once;
2025

2126
pub fn errno() -> i32 {
2227
RESULT_SUCCESS
@@ -78,29 +83,51 @@ pub fn current_exe() -> io::Result<PathBuf> {
7883
unsupported()
7984
}
8085

81-
pub struct Env;
86+
static ENV: AtomicUsize = AtomicUsize::new(0);
87+
static ENV_INIT: Once = Once::new();
88+
type EnvStore = Mutex<HashMap<OsString, OsString>>;
8289

83-
impl Iterator for Env {
84-
type Item = (OsString, OsString);
85-
fn next(&mut self) -> Option<(OsString, OsString)> {
86-
None
90+
fn get_env_store() -> Option<&'static EnvStore> {
91+
unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
92+
}
93+
94+
fn create_env_store() -> &'static EnvStore {
95+
ENV_INIT.call_once(|| {
96+
ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
97+
});
98+
unsafe {
99+
&*(ENV.load(Ordering::Relaxed) as *const EnvStore)
87100
}
88101
}
89102

103+
pub type Env = vec::IntoIter<(OsString, OsString)>;
104+
90105
pub fn env() -> Env {
91-
Env
106+
let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
107+
map.iter().map(|(k, v)| (k.clone(), v.clone()) ).collect()
108+
};
109+
110+
get_env_store()
111+
.map(|env| clone_to_vec(&env.lock().unwrap()) )
112+
.unwrap_or_default()
113+
.into_iter()
92114
}
93115

94-
pub fn getenv(_k: &OsStr) -> io::Result<Option<OsString>> {
95-
Ok(None)
116+
pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
117+
Ok(get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned() ))
96118
}
97119

98-
pub fn setenv(_k: &OsStr, _v: &OsStr) -> io::Result<()> {
99-
sgx_ineffective(()) // FIXME: this could trigger a panic higher up the stack
120+
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
121+
let (k, v) = (k.to_owned(), v.to_owned());
122+
create_env_store().lock().unwrap().insert(k, v);
123+
Ok(())
100124
}
101125

102-
pub fn unsetenv(_k: &OsStr) -> io::Result<()> {
103-
sgx_ineffective(()) // FIXME: this could trigger a panic higher up the stack
126+
pub fn unsetenv(k: &OsStr) -> io::Result<()> {
127+
if let Some(env) = get_env_store() {
128+
env.lock().unwrap().remove(k);
129+
}
130+
Ok(())
104131
}
105132

106133
pub fn temp_dir() -> PathBuf {

0 commit comments

Comments
 (0)