]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
D3: execve argv/envp userland test
authorTulio A M Mendes <[email protected]>
Sun, 8 Feb 2026 01:56:55 +0000 (22:56 -0300)
committerTulio A M Mendes <[email protected]>
Sun, 8 Feb 2026 01:56:55 +0000 (22:56 -0300)
Pass argv/envp from init.elf into execve() and extend echo.elf to parse argc/argv/envp from the initial user stack for validation.

user/echo.c
user/init.c

index 2e7a739c45503882a694a24b84db718bf229d8e0..2c70ea08aba021704e7b4bd74f51080482950318 100644 (file)
@@ -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"
+    );
+}
index 683d1731000881631c1dfc4c3ca7e3e03c9f31ad..8febdb73ad2f6bc68c08c78efe13dd633978f980 100644 (file)
@@ -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);