fix argv handling

This commit is contained in:
nora 2023-10-01 11:35:53 +02:00
parent 21d3f12572
commit 613482b8c4
7 changed files with 28 additions and 5 deletions

View file

@ -4,7 +4,9 @@ use crate::utils::SharedThinCstr;
/// The entrypoint of the program. /// The entrypoint of the program.
/// This is called by a bit of assembly handling architecture-specific _start. /// This is called by a bit of assembly handling architecture-specific _start.
pub(crate) unsafe extern "C" fn start(argc: u64, argv: *const *const c_char, rsp: u64) -> ! { pub(crate) unsafe extern "C" fn start(rsp: u64) -> ! {
let argc = (rsp as *const u64).read();
let argv = (rsp + 8) as *const *const c_char;
let envp = (8 + 8 * argc + rsp + 8) as *mut Option<SharedThinCstr<'static>>; let envp = (8 + 8 * argc + rsp + 8) as *mut Option<SharedThinCstr<'static>>;
crate::env::init(envp); crate::env::init(envp);

View file

@ -8,9 +8,7 @@ core::arch::global_asm!(
"mov rbp, 0", "mov rbp, 0",
// We're off to a good start already. // We're off to a good start already.
// Pass the variables to the start function arguments. // Pass the variables to the start function arguments.
"mov rdi, [rsp]", // &argc = rsp "mov rdi, rsp",
"mov rsi, [rsp+8]", // &argv = rsp+8
"mov rdx, rsp",
// The stack will be 16-byte aligned at process entry already. // The stack will be 16-byte aligned at process entry already.
// So we're good to go! // So we're good to go!

View file

@ -46,7 +46,7 @@ pub unsafe extern "C" fn printf(format: *const u8, mut args: ...) -> c_int {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn __fprintf_chk(file: &FileStream, _flag: c_int, format: *const u8, mut args: ...) -> c_int { pub unsafe extern "C" fn __fprintf_chk(_flag: c_int, file: &FileStream, format: *const u8, mut args: ...) -> c_int {
let mut sink = WriteCounter(file, 0); let mut sink = WriteCounter(file, 0);
let result = libuwuc::fmt::printf::printf_generic( let result = libuwuc::fmt::printf::printf_generic(

View file

@ -7,5 +7,6 @@
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
''; '';
packages = (with pkgs; [ packages = (with pkgs; [
gef
]); ]);
} }

View file

@ -21,13 +21,18 @@ for test in tests/c/*; do
exit 1 exit 1
fi fi
printf "test $name "
OUTPUT=$("$test_dir/$name") OUTPUT=$("$test_dir/$name")
code="$?" code="$?"
if [ "$code" -ne "0" ]; then if [ "$code" -ne "0" ]; then
echo -e "\e[31mFAIL\e[0m"
echo "error: test failed with code $code: $test, running $test_dir/$name" echo "error: test failed with code $code: $test, running $test_dir/$name"
echo "------ output:" echo "------ output:"
echo "$OUTPUT" echo "$OUTPUT"
echo "-----" echo "-----"
else
echo -e "\e[32mPASS\e[0m"
fi fi
done done

9
tests/c/argv.c Normal file
View file

@ -0,0 +1,9 @@
#include<stdio.h>
int main(int argc, char* argv[]) {
char *self = argv[0];
char first = self[0];
if (first != '/') {
return 1;
}
}

8
tests/c/malloc.c Normal file
View file

@ -0,0 +1,8 @@
#include<stdlib.h>
int main(void) {
char *alloc = (char*) malloc(10);
*alloc = 1;
*(alloc + 9) = 2;
free(alloc);
}