Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix args ptr alignment #359

Merged
merged 2 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2022-04-15"
channel = "nightly-2022-06-21"
components = ["rust-src", "llvm-tools-preview"]
32 changes: 15 additions & 17 deletions src/sys/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,32 +306,30 @@ impl Process {

// Switch to user mode and execute the program
fn exec(&self, args_ptr: usize, args_len: usize) {
let heap_addr = self.code_addr + (self.stack_addr - self.code_addr) / 2;
sys::allocator::alloc_pages(heap_addr, 1);

let args_ptr = ptr_from_addr(args_ptr as u64) as usize;
let args: &[&str] = unsafe { core::slice::from_raw_parts(args_ptr as *const &str, args_len) };
let heap_addr = self.code_addr + (self.stack_addr - self.code_addr) / 2;
let mut ptr = heap_addr;
let mut addr = heap_addr;
let vec: Vec<&str> = args.iter().map(|arg| {
let src_len = arg.len();
let src_ptr = arg.as_ptr();
let dst_ptr = ptr as *mut u8;
//let dst_ptr_translated = ((dst_ptr as u64) - self.code_addr) as *const u8; // Userspace address
ptr = ((dst_ptr as usize) + src_len) as u64;
let ptr = addr as *mut u8;
addr += arg.len() as u64;
unsafe {
core::ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
//core::str::from_utf8_unchecked(core::slice::from_raw_parts(dst_ptr_translated, src_len))
core::str::from_utf8_unchecked(core::slice::from_raw_parts(dst_ptr, src_len))
let s = core::slice::from_raw_parts_mut(ptr, arg.len());
s.copy_from_slice(arg.as_bytes());
core::str::from_utf8_unchecked(s)
}
}).collect();
let align = core::mem::align_of::<&str>() as u64;
addr += align - (addr % align);
let args = vec.as_slice();
let src_len = args.len();
let src_ptr = args.as_ptr();
let dst_ptr = ptr as *mut &str;
let ptr = addr as *mut &str;
let args: &[&str] = unsafe {
core::ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
core::slice::from_raw_parts(dst_ptr, src_len)
let s = core::slice::from_raw_parts_mut(ptr, args.len());
s.copy_from_slice(args);
s
};

//let args_ptr = (args.as_ptr() as u64) - self.code_addr; // userspace address
let args_ptr = args.as_ptr() as u64;

set_id(self.id); // Change PID
Expand Down