From: Tulio A M Mendes Date: Sun, 8 Feb 2026 01:56:55 +0000 (-0300) Subject: D3: execve argv/envp userland test X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=06d7dd0142e07063215f6acfba0bbe321bbb48b1;p=AdrOS.git D3: execve argv/envp userland test Pass argv/envp from init.elf into execve() and extend echo.elf to parse argc/argv/envp from the initial user stack for validation. --- diff --git a/user/echo.c b/user/echo.c index 2e7a739..2c70ea0 100644 --- a/user/echo.c +++ b/user/echo.c @@ -16,6 +16,33 @@ static int sys_write(int fd, const void* buf, uint32_t len) { return ret; } +static uint32_t ustrlen(const char* s) { + uint32_t n = 0; + while (s && s[n]) n++; + return n; +} + +static void write_str(const char* s) { + (void)sys_write(1, s, ustrlen(s)); +} + +static void u32_to_dec(uint32_t v, char out[16]) { + char tmp[16]; + uint32_t n = 0; + if (v == 0) { + out[0] = '0'; + out[1] = 0; + return; + } + while (v && n < sizeof(tmp) - 1) { + tmp[n++] = (char)('0' + (v % 10)); + v /= 10; + } + uint32_t i = 0; + while (n) out[i++] = tmp[--n]; + out[i] = 0; +} + static __attribute__((noreturn)) void sys_exit(int status) { __asm__ volatile( "int $0x80" @@ -28,8 +55,56 @@ static __attribute__((noreturn)) void sys_exit(int status) { } } -void _start(void) { - static const char m[] = "[echo] hello from echo.elf\n"; - (void)sys_write(1, m, (uint32_t)(sizeof(m) - 1)); +static void echo_main(uint32_t* sp0) { + write_str("[echo] hello from echo.elf\n"); + + uint32_t argc = sp0 ? sp0[0] : 0; + const char* const* argv = (const char* const*)(sp0 + 1); + + const char* const* envp = argv; + for (uint32_t i = 0; argv && argv[i]; i++) { + envp = &argv[i + 1]; + } + if (envp) envp++; + + char num[16]; + u32_to_dec(argc, num); + write_str("[echo] argc="); + write_str(num); + write_str("\n"); + + if (argv && argv[0]) { + write_str("[echo] argv0="); + write_str(argv[0]); + write_str("\n"); + } + if (argv && argv[1]) { + write_str("[echo] argv1="); + write_str(argv[1]); + write_str("\n"); + } + if (argv && argv[2]) { + write_str("[echo] argv2="); + write_str(argv[2]); + write_str("\n"); + } + if (envp && envp[0]) { + write_str("[echo] env0="); + write_str(envp[0]); + write_str("\n"); + } sys_exit(0); } + +__attribute__((naked)) void _start(void) { + __asm__ volatile( + "mov %esp, %eax\n" + "push %eax\n" + "call echo_main\n" + "add $4, %esp\n" + "mov $0, %ebx\n" + "mov $2, %eax\n" + "int $0x80\n" + "hlt\n" + ); +} diff --git a/user/init.c b/user/init.c index 683d173..8febdb7 100644 --- a/user/init.c +++ b/user/init.c @@ -598,7 +598,9 @@ void _start(void) { (void)sys_write(1, "[init] execve(/bin/echo.elf)\n", (uint32_t)(sizeof("[init] execve(/bin/echo.elf)\n") - 1)); - (void)sys_execve("/bin/echo.elf", (const char* const*)0, (const char* const*)0); + static const char* const argv[] = {"echo.elf", "arg1", "arg2", 0}; + static const char* const envp[] = {"FOO=bar", "HELLO=world", 0}; + (void)sys_execve("/bin/echo.elf", argv, envp); (void)sys_write(1, "[init] execve returned (unexpected)\n", (uint32_t)(sizeof("[init] execve returned (unexpected)\n") - 1)); sys_exit(1);