alnyan/yggdrasil: add args implementation

This commit is contained in:
2023-07-17 22:54:43 +03:00
parent defb036456
commit 8b39059da8
2 changed files with 60 additions and 11 deletions
+58 -10
View File
@@ -1,10 +1,40 @@
use crate::ffi::OsString;
use crate::fmt;
use crate::str::FromStr;
pub struct Args(!);
#[allow(dead_code)]
#[derive(Clone, Copy)]
pub struct Args {
base: usize,
len: usize,
index: usize,
}
// Argument structure layout:
//
// 0x000: args len
// 0x008: arg ptr 0
// 0x010: arg len 0
// 0x018: arg ptr 1
// 0x020: arg len 1
// ...
// .....: arg data 0
// .....: arg data 1
static mut ARGS: usize = 0;
pub fn args() -> Args {
todo!()
let base = unsafe { ARGS };
if base == 0 {
panic!("Program arguments not initialized");
}
let len = unsafe {
#[allow(fuzzy_provenance_casts)]
(base as *mut usize).read()
};
Args { base, len, index: 0 }
}
impl fmt::Debug for Args {
@@ -17,17 +47,36 @@ impl Iterator for Args {
type Item = OsString;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
if self.index == self.len {
return None;
}
fn size_hint(&self) -> (usize, Option<usize>) {
todo!()
let ptr_ptr_addr = self.base + (1 + self.index * 2) * 8;
let arg_ptr = unsafe {
#[allow(fuzzy_provenance_casts)]
(ptr_ptr_addr as *mut usize).read()
};
let arg_len = unsafe {
#[allow(fuzzy_provenance_casts)]
((ptr_ptr_addr + 8) as *mut usize).read()
};
let slice = unsafe {
#[allow(fuzzy_provenance_casts)]
crate::slice::from_raw_parts(arg_ptr as *const u8, arg_len)
};
let arg_str = crate::str::from_utf8(slice).unwrap();
let arg_os_string = OsString::from_str(arg_str).unwrap();
self.index += 1;
Some(arg_os_string)
}
}
impl ExactSizeIterator for Args {
fn len(&self) -> usize {
todo!()
self.len
}
}
@@ -37,7 +86,6 @@ impl DoubleEndedIterator for Args {
}
}
#[allow(dead_code)]
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
todo!()
pub unsafe fn init(program_arg: usize) {
ARGS = program_arg;
}
+2 -1
View File
@@ -99,7 +99,7 @@ pub fn unsupported() -> ! {
#[cfg(not(test))]
#[no_mangle]
pub unsafe extern "C" fn runtime_entry(_arg: usize) -> ! {
pub unsafe extern "C" fn runtime_entry(program_arg: usize) -> ! {
use crate::ffi::c_char;
use crate::ptr::null;
@@ -107,6 +107,7 @@ pub unsafe extern "C" fn runtime_entry(_arg: usize) -> ! {
fn main(argc: isize, argv: *const *const c_char) -> i32;
}
args::init(program_arg);
let result = main(0, null());
yggdrasil_rt::sys::exit(result);