Skip to content

Commit 063f44f

Browse files
authored
Unrolled build for rust-lang#131654
Rollup merge of rust-lang#131654 - betrusted-io:xous-various-fixes, r=thomcc Various fixes for Xous This patchset includes several fixes for Xous that have crept in over the last few months: * The `adjust_process()` syscall was incorrect * Warnings have started appearing in `alloc` -- adopt the same approach as wasm, until wasm figures out a workaround * Dead code warnings have appeared in the networking code. Add `allow(dead_code)` as these structs are used as IPC values * Add support for `args` and `env`, which have been useful for running tests * Update `unwinding` to `0.2.3` which fixes the recent regression due to changes in `asm!()` code
2 parents d9c4b8d + 99de67a commit 063f44f

File tree

11 files changed

+518
-40
lines changed

11 files changed

+518
-40
lines changed

library/Cargo.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is automatically @generated by Cargo.
22
# It is not intended for manual editing.
3-
version = 3
3+
version = 4
44

55
[[package]]
66
name = "addr2line"
@@ -124,9 +124,9 @@ dependencies = [
124124

125125
[[package]]
126126
name = "gimli"
127-
version = "0.30.0"
127+
version = "0.31.1"
128128
source = "registry+https://github.com/rust-lang/crates.io-index"
129-
checksum = "e2e1d97fbe9722ba9bbd0c97051c2956e726562b61f86a25a4360398a40edfc9"
129+
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
130130
dependencies = [
131131
"compiler_builtins",
132132
"rustc-std-workspace-alloc",
@@ -406,12 +406,12 @@ dependencies = [
406406

407407
[[package]]
408408
name = "unwinding"
409-
version = "0.2.2"
409+
version = "0.2.3"
410410
source = "registry+https://github.com/rust-lang/crates.io-index"
411-
checksum = "dc55842d0db6329a669d55a623c674b02d677b16bfb2d24857d4089d41eba882"
411+
checksum = "637d511437df708cee34bdec7ba2f1548d256b7acf3ff20e0a1c559f9bf3a987"
412412
dependencies = [
413413
"compiler_builtins",
414-
"gimli 0.30.0",
414+
"gimli 0.31.1",
415415
"rustc-std-workspace-core",
416416
]
417417

library/std/src/os/xous/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ pub(crate) fn thread_id() -> Result<ThreadId, Error> {
615615
/// An error is generated if the `knob` is not a valid limit, or if the call
616616
/// would not succeed.
617617
pub(crate) fn adjust_limit(knob: Limits, current: usize, new: usize) -> Result<usize, Error> {
618-
let mut a0 = Syscall::JoinThread as usize;
618+
let mut a0 = Syscall::AdjustProcessLimit as usize;
619619
let mut a1 = knob as usize;
620620
let a2 = current;
621621
let a3 = new;

library/std/src/sys/alloc/xous.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2+
#![allow(static_mut_refs)]
3+
14
use crate::alloc::{GlobalAlloc, Layout, System};
25

36
#[cfg(not(test))]

library/std/src/sys/pal/xous/args.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crate::ffi::OsString;
2+
use crate::sys::pal::xous::os::get_application_parameters;
3+
use crate::sys::pal::xous::os::params::ArgumentList;
4+
use crate::{fmt, vec};
5+
6+
pub struct Args {
7+
parsed_args_list: vec::IntoIter<OsString>,
8+
}
9+
10+
pub fn args() -> Args {
11+
let Some(params) = get_application_parameters() else {
12+
return Args { parsed_args_list: vec![].into_iter() };
13+
};
14+
15+
for param in params {
16+
if let Ok(args) = ArgumentList::try_from(&param) {
17+
let mut parsed_args = vec![];
18+
for arg in args {
19+
parsed_args.push(arg.into());
20+
}
21+
return Args { parsed_args_list: parsed_args.into_iter() };
22+
}
23+
}
24+
Args { parsed_args_list: vec![].into_iter() }
25+
}
26+
27+
impl fmt::Debug for Args {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
self.parsed_args_list.as_slice().fmt(f)
30+
}
31+
}
32+
33+
impl Iterator for Args {
34+
type Item = OsString;
35+
fn next(&mut self) -> Option<OsString> {
36+
self.parsed_args_list.next()
37+
}
38+
fn size_hint(&self) -> (usize, Option<usize>) {
39+
self.parsed_args_list.size_hint()
40+
}
41+
}
42+
43+
impl DoubleEndedIterator for Args {
44+
fn next_back(&mut self) -> Option<OsString> {
45+
self.parsed_args_list.next_back()
46+
}
47+
}
48+
49+
impl ExactSizeIterator for Args {
50+
fn len(&self) -> usize {
51+
self.parsed_args_list.len()
52+
}
53+
}

library/std/src/sys/pal/xous/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
22

3-
#[path = "../unsupported/args.rs"]
43
pub mod args;
54
#[path = "../unsupported/env.rs"]
65
pub mod env;

library/std/src/sys/pal/xous/net/dns.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::os::xous::ffi::lend_mut;
66
use crate::os::xous::services::{DnsLendMut, dns_server};
77

88
pub struct DnsError {
9+
#[allow(dead_code)]
910
pub code: u8,
1011
}
1112

library/std/src/sys/pal/xous/net/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub mod netc {
6060

6161
#[derive(Copy, Clone)]
6262
pub struct sockaddr_in {
63+
#[allow(dead_code)]
6364
pub sin_family: sa_family_t,
6465
pub sin_port: u16,
6566
pub sin_addr: in_addr,
@@ -72,6 +73,7 @@ pub mod netc {
7273

7374
#[derive(Copy, Clone)]
7475
pub struct sockaddr_in6 {
76+
#[allow(dead_code)]
7577
pub sin6_family: sa_family_t,
7678
pub sin6_port: u16,
7779
pub sin6_addr: in6_addr,

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

+105-31
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
use super::unsupported;
2+
use crate::collections::HashMap;
23
use crate::error::Error as StdError;
34
use crate::ffi::{OsStr, OsString};
45
use crate::marker::PhantomData;
56
use crate::os::xous::ffi::Error as XousError;
67
use crate::path::{self, PathBuf};
7-
use crate::{fmt, io};
8+
use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
9+
use crate::sync::{Mutex, Once};
10+
use crate::{fmt, io, vec};
11+
12+
pub(crate) mod params;
13+
14+
static PARAMS_ADDRESS: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
815

916
#[cfg(not(test))]
1017
#[cfg(feature = "panic_unwind")]
1118
mod eh_unwinding {
12-
pub(crate) struct EhFrameFinder(usize /* eh_frame */);
13-
pub(crate) static mut EH_FRAME_SETTINGS: EhFrameFinder = EhFrameFinder(0);
14-
impl EhFrameFinder {
15-
pub(crate) unsafe fn init(&mut self, eh_frame: usize) {
16-
unsafe {
17-
EH_FRAME_SETTINGS.0 = eh_frame;
18-
}
19-
}
20-
}
19+
pub(crate) struct EhFrameFinder;
20+
pub(crate) static mut EH_FRAME_ADDRESS: usize = 0;
21+
pub(crate) static EH_FRAME_SETTINGS: EhFrameFinder = EhFrameFinder;
22+
2123
unsafe impl unwind::EhFrameFinder for EhFrameFinder {
2224
fn find(&self, _pc: usize) -> Option<unwind::FrameInfo> {
23-
Some(unwind::FrameInfo {
24-
text_base: None,
25-
kind: unwind::FrameInfoKind::EhFrame(self.0),
26-
})
25+
if unsafe { EH_FRAME_ADDRESS == 0 } {
26+
None
27+
} else {
28+
Some(unwind::FrameInfo {
29+
text_base: None,
30+
kind: unwind::FrameInfoKind::EhFrame(unsafe { EH_FRAME_ADDRESS }),
31+
})
32+
}
2733
}
2834
}
2935
}
@@ -41,12 +47,21 @@ mod c_compat {
4147
}
4248

4349
#[no_mangle]
44-
pub extern "C" fn _start(eh_frame: usize) {
50+
pub extern "C" fn _start(eh_frame: usize, params_address: usize) {
4551
#[cfg(feature = "panic_unwind")]
46-
unsafe {
47-
super::eh_unwinding::EH_FRAME_SETTINGS.init(eh_frame);
52+
{
53+
unsafe { super::eh_unwinding::EH_FRAME_ADDRESS = eh_frame };
4854
unwind::set_custom_eh_frame_finder(&super::eh_unwinding::EH_FRAME_SETTINGS).ok();
4955
}
56+
57+
if params_address != 0 {
58+
let params_address = crate::ptr::with_exposed_provenance_mut::<u8>(params_address);
59+
if unsafe {
60+
super::params::ApplicationParameters::new_from_ptr(params_address).is_some()
61+
} {
62+
super::PARAMS_ADDRESS.store(params_address, core::sync::atomic::Ordering::Relaxed);
63+
}
64+
}
5065
exit(unsafe { main() });
5166
}
5267

@@ -116,44 +131,103 @@ pub fn current_exe() -> io::Result<PathBuf> {
116131
unsupported()
117132
}
118133

119-
pub struct Env(!);
134+
pub(crate) fn get_application_parameters() -> Option<params::ApplicationParameters> {
135+
let params_address = PARAMS_ADDRESS.load(Ordering::Relaxed);
136+
unsafe { params::ApplicationParameters::new_from_ptr(params_address) }
137+
}
138+
139+
// ---------- Environment handling ---------- //
140+
static ENV: AtomicUsize = AtomicUsize::new(0);
141+
static ENV_INIT: Once = Once::new();
142+
type EnvStore = Mutex<HashMap<OsString, OsString>>;
143+
144+
fn get_env_store() -> &'static EnvStore {
145+
ENV_INIT.call_once(|| {
146+
let env_store = EnvStore::default();
147+
if let Some(params) = get_application_parameters() {
148+
for param in params {
149+
if let Ok(envs) = params::EnvironmentBlock::try_from(&param) {
150+
let mut env_store = env_store.lock().unwrap();
151+
for env in envs {
152+
env_store.insert(env.key.into(), env.value.into());
153+
}
154+
break;
155+
}
156+
}
157+
}
158+
ENV.store(Box::into_raw(Box::new(env_store)) as _, Ordering::Relaxed)
159+
});
160+
unsafe { &*core::ptr::with_exposed_provenance::<EnvStore>(ENV.load(Ordering::Relaxed)) }
161+
}
162+
163+
pub struct Env {
164+
iter: vec::IntoIter<(OsString, OsString)>,
165+
}
166+
167+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
168+
pub struct EnvStrDebug<'a> {
169+
slice: &'a [(OsString, OsString)],
170+
}
171+
172+
impl fmt::Debug for EnvStrDebug<'_> {
173+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174+
let Self { slice } = self;
175+
f.debug_list()
176+
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
177+
.finish()
178+
}
179+
}
120180

121181
impl Env {
122182
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
123183
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
124-
let Self(inner) = self;
125-
match *inner {}
184+
let Self { iter } = self;
185+
EnvStrDebug { slice: iter.as_slice() }
126186
}
127187
}
128188

129189
impl fmt::Debug for Env {
130-
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
131-
let Self(inner) = self;
132-
match *inner {}
190+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191+
let Self { iter } = self;
192+
f.debug_list().entries(iter.as_slice()).finish()
133193
}
134194
}
135195

196+
impl !Send for Env {}
197+
impl !Sync for Env {}
198+
136199
impl Iterator for Env {
137200
type Item = (OsString, OsString);
138201
fn next(&mut self) -> Option<(OsString, OsString)> {
139-
self.0
202+
self.iter.next()
203+
}
204+
fn size_hint(&self) -> (usize, Option<usize>) {
205+
self.iter.size_hint()
140206
}
141207
}
142208

143209
pub fn env() -> Env {
144-
panic!("not supported on this platform")
210+
let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
211+
map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
212+
};
213+
214+
let iter = clone_to_vec(&*get_env_store().lock().unwrap()).into_iter();
215+
Env { iter }
145216
}
146217

147-
pub fn getenv(_: &OsStr) -> Option<OsString> {
148-
None
218+
pub fn getenv(k: &OsStr) -> Option<OsString> {
219+
get_env_store().lock().unwrap().get(k).cloned()
149220
}
150221

151-
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
152-
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
222+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
223+
let (k, v) = (k.to_owned(), v.to_owned());
224+
get_env_store().lock().unwrap().insert(k, v);
225+
Ok(())
153226
}
154227

155-
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
156-
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
228+
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
229+
get_env_store().lock().unwrap().remove(k);
230+
Ok(())
157231
}
158232

159233
pub fn temp_dir() -> PathBuf {

0 commit comments

Comments
 (0)