Skip to content

Commit

Permalink
rename argv
Browse files Browse the repository at this point in the history
  • Loading branch information
KouweiLee committed Sep 14, 2023
1 parent 6e9ae30 commit 81e9be4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
2 changes: 1 addition & 1 deletion api/arceos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern crate axruntime;
extern crate alloc;

#[cfg(feature = "alloc")]
pub use axruntime::{environ, environ_iter, OUR_ENVIRON};
pub use axruntime::{environ, environ_iter, RX_ENVIRON};

#[macro_use]
mod utils;
Expand Down
3 changes: 2 additions & 1 deletion modules/axhal/src/platform/x86_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ unsafe extern "C" fn rust_entry(magic: usize, mbi: usize) {
self::uart16550::init();
self::dtables::init_primary();
self::time::init_early();
// find cmdline in multiboot info and save it in COMLINE_BUF
let mbi = mbi as *const u32;
let flag = mbi.read();
if (flag & (1 << 2)) > 0 {
let cmdline = *mbi.add(4) as *const u8; // cmdline的物理地址
let cmdline = *mbi.add(4) as *const u8;
let mut len = 0;
while cmdline.add(len).read() != 0 {
COMLINE_BUF[len] = cmdline.add(len).read();
Expand Down
31 changes: 17 additions & 14 deletions modules/axruntime/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ use core::{ptr, usize};
/// argv for C main function
#[allow(non_upper_case_globals)]
pub static mut argv: *mut *mut c_char = ptr::null_mut();
#[allow(non_upper_case_globals)]
static mut inner_argv: Vec<*mut c_char> = Vec::new();

/// A pointer pointing to OUR_ENVIRON
/// Save cmdline argments
static mut RX_ARGV: Vec<*mut c_char> = Vec::new();

/// A pointer pointing to RX_ENVIRON
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut environ: *mut *mut c_char = ptr::null_mut();

/// Save environment variables
pub static mut OUR_ENVIRON: Vec<*mut c_char> = Vec::new();
pub static mut RX_ENVIRON: Vec<*mut c_char> = Vec::new();

pub(crate) unsafe fn init_argv(args: Vec<&str>) {
for arg in args {
Expand All @@ -26,10 +27,10 @@ pub(crate) unsafe fn init_argv(args: Vec<&str>) {
*buf.add(i) = *arg.add(i) as i8;
}
*buf.add(len) = 0;
inner_argv.push(buf);
RX_ARGV.push(buf);
}
inner_argv.push(ptr::null_mut());
argv = inner_argv.as_mut_ptr();
RX_ARGV.push(ptr::null_mut());
argv = RX_ARGV.as_mut_ptr();
}

/// Generate an iterator for environment variables
Expand Down Expand Up @@ -63,16 +64,18 @@ unsafe fn buf_alloc(size: usize) -> *mut c_char {
alloc_ptr.add(1).cast()
}

pub(crate) unsafe fn boot_add_environ(env: &str) {
pub(crate) fn boot_add_environ(env: &str) {
let ptr = env.as_ptr() as *const i8;
let size = env.len() + 1; // 算上/0
let size = env.len() + 1;
if size == 1 {
return;
}
let buf = buf_alloc(size);
for i in 0..size - 1 {
core::ptr::write(buf.add(i), *ptr.add(i));
unsafe {
let buf = buf_alloc(size);
for i in 0..size - 1 {
core::ptr::write(buf.add(i), *ptr.add(i));
}
core::ptr::write(buf.add(size - 1), 0);
RX_ENVIRON.push(buf);
}
core::ptr::write(buf.add(size - 1), 0);
OUR_ENVIRON.push(buf);
}
10 changes: 5 additions & 5 deletions modules/axruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern crate alloc;
#[cfg(feature = "alloc")]
mod env;
#[cfg(feature = "alloc")]
pub use self::env::{argv, environ, environ_iter, OUR_ENVIRON};
pub use self::env::{argv, environ, environ_iter, RX_ENVIRON};
#[cfg(feature = "alloc")]
use self::env::{boot_add_environ, init_argv};
use core::ffi::{c_char, c_int};
Expand Down Expand Up @@ -114,8 +114,8 @@ fn is_init_ok() -> bool {
#[cfg(feature = "alloc")]
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
unsafe fn get_boot_str() -> &'static str {
let cmdline_buf: &[u8] = &axhal::COMLINE_BUF;
fn get_boot_str() -> &'static str {
let cmdline_buf: &[u8] = unsafe { &axhal::COMLINE_BUF };
let mut len = 0;
for c in cmdline_buf.iter() {
if *c == 0 {
Expand Down Expand Up @@ -245,8 +245,8 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! {
for i in envs {
boot_add_environ(i);
}
OUR_ENVIRON.push(core::ptr::null_mut());
environ = OUR_ENVIRON.as_mut_ptr();
RX_ENVIRON.push(core::ptr::null_mut());
environ = RX_ENVIRON.as_mut_ptr();
// set up argvs
let args: Vec<&str> = args.split(',').filter(|i| !i.is_empty()).collect();
let argc = args.len() as c_int;
Expand Down
36 changes: 18 additions & 18 deletions ulib/axlibc/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arceos_posix_api::{environ, environ_iter, OUR_ENVIRON};
use arceos_posix_api::{environ, environ_iter, RX_ENVIRON};
use core::ffi::{c_char, c_int, c_void};

use crate::malloc::{free, malloc};
Expand Down Expand Up @@ -32,17 +32,17 @@ unsafe fn find_env(search: *const c_char) -> Option<(usize, *mut c_char)> {
unsafe fn put_new_env(insert: *mut c_char) {
// XXX: Another problem is that `environ` can be set to any pointer, which means there is a
// chance of a memory leak. But we can check if it was the same as before, like musl does.
if environ == OUR_ENVIRON.as_mut_ptr() {
*OUR_ENVIRON.last_mut().unwrap() = insert;
OUR_ENVIRON.push(core::ptr::null_mut());
if environ == RX_ENVIRON.as_mut_ptr() {
*RX_ENVIRON.last_mut().unwrap() = insert;
RX_ENVIRON.push(core::ptr::null_mut());
// Likely a no-op but is needed due to Stacked Borrows.
environ = OUR_ENVIRON.as_mut_ptr();
environ = RX_ENVIRON.as_mut_ptr();
} else {
OUR_ENVIRON.clear();
OUR_ENVIRON.extend(environ_iter());
OUR_ENVIRON.push(insert);
OUR_ENVIRON.push(core::ptr::null_mut());
environ = OUR_ENVIRON.as_mut_ptr();
RX_ENVIRON.clear();
RX_ENVIRON.extend(environ_iter());
RX_ENVIRON.push(insert);
RX_ENVIRON.push(core::ptr::null_mut());
environ = RX_ENVIRON.as_mut_ptr();
}
}

Expand Down Expand Up @@ -97,27 +97,27 @@ pub unsafe extern "C" fn setenv(
#[no_mangle]
pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int {
if let Some((i, _)) = find_env(key) {
if environ == OUR_ENVIRON.as_mut_ptr() {
if environ == RX_ENVIRON.as_mut_ptr() {
// No need to worry about updating the pointer, this does not
// reallocate in any way. And the final null is already shifted back.
let rm = OUR_ENVIRON.remove(i);
let rm = RX_ENVIRON.remove(i);
free(rm as *mut c_void);
// My UB paranoia.
environ = OUR_ENVIRON.as_mut_ptr();
environ = RX_ENVIRON.as_mut_ptr();
} else {
let len = OUR_ENVIRON.len();
let len = RX_ENVIRON.len();
for _ in 0..len {
let rm = OUR_ENVIRON.pop().unwrap();
let rm = RX_ENVIRON.pop().unwrap();
free(rm as *mut c_void);
}
OUR_ENVIRON.extend(
RX_ENVIRON.extend(
environ_iter()
.enumerate()
.filter(|&(j, _)| j != i)
.map(|(_, v)| v),
);
OUR_ENVIRON.push(core::ptr::null_mut());
environ = OUR_ENVIRON.as_mut_ptr();
RX_ENVIRON.push(core::ptr::null_mut());
environ = RX_ENVIRON.as_mut_ptr();
}
}
0
Expand Down

0 comments on commit 81e9be4

Please sign in to comment.