Skip to content

Commit f817d04

Browse files
acfoltzersunfishcode
authored andcommitted
Add command-line argument and environment syscalls
1 parent dfa74ca commit f817d04

3 files changed

Lines changed: 88 additions & 2 deletions

File tree

basics/libc/crt1.c

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,76 @@
1+
#include <stdlib.h>
2+
#include <sysexits.h>
3+
#include <wasi/core.h>
4+
5+
extern char **environ;
16
extern void __wasm_call_ctors(void);
27
extern int main(int, char *[]);
38
extern void __prepare_for_exit(void);
49
void _Exit(int) __attribute__((noreturn));
510

11+
static __wasi_errno_t populate_args(size_t *argc, char **argv) {
12+
__wasi_errno_t err;
13+
14+
/* Get the sizes of the arrays we'll have to create to copy in the args. */
15+
size_t argv_size;
16+
err = __wasi_arg_sizes_get(argc, &argv_size);
17+
if (err != __WASI_ESUCCESS) {
18+
return err;
19+
}
20+
21+
/* Allocate memory for the array of pointers. */
22+
argv = malloc(sizeof(char *) * *argc);
23+
/* Allocate memory for storing the argument chars. */
24+
char *argv_buf = malloc(sizeof(char) * argv_size);
25+
if (argv == NULL || argv_buf == NULL) {
26+
return __WASI_ENOMEM;
27+
}
28+
29+
/* Fill the argument chars, and the argv array with pointers into those chars. */
30+
return __wasi_argv_get(argv, argv_buf);
31+
}
32+
33+
static __wasi_errno_t populate_environ() {
34+
__wasi_errno_t err;
35+
36+
/* Get the sizes of the arrays we'll have to create to copy in the environment. */
37+
size_t environ_count;
38+
size_t environ_size;
39+
err = __wasi_environ_sizes_get(&environ_count, &environ_size);
40+
if (err != __WASI_ESUCCESS) {
41+
return err;
42+
}
43+
44+
/* Allocate memory for the array of pointers. */
45+
environ = malloc(sizeof(char *) * environ_count);
46+
/* Allocate memory for storing the environment chars. */
47+
char *environ_buf = malloc(sizeof(char) * environ_size);
48+
if (environ == NULL || environ_buf == NULL) {
49+
return __WASI_ENOMEM;
50+
}
51+
52+
/* Fill the environment chars, and the __environ array with pointers into those chars. */
53+
return __wasi_environ_get(environ, environ_buf);
54+
}
55+
656
void _start(void) {
757
/* The linker synthesizes this to call constructors. */
858
__wasm_call_ctors();
959

10-
/* For now, just call main with no arguments. */
11-
int r = main(0, 0);
60+
/* Fill in the environment from WASI syscalls. */
61+
if (populate_environ() != __WASI_ESUCCESS) {
62+
_Exit(EX_OSERR);
63+
}
64+
65+
/* Fill in the arguments from WASI syscalls. */
66+
size_t argc;
67+
char **argv;
68+
if (populate_args(&argc, argv) != __WASI_ESUCCESS) {
69+
_Exit(EX_OSERR);
70+
}
71+
72+
/* Call main with the arguments. */
73+
int r = main(argc, argv);
1274

1375
/* Call atexit functions, destructors, stdio cleanup, etc. */
1476
__prepare_for_exit();

expected/wasm32-wasi/undefined-symbols.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ __subtf3
2323
__trunctfdf2
2424
__trunctfsf2
2525
__unordtf2
26+
__wasi_arg_sizes_get
27+
__wasi_argv_get
2628
__wasi_clock_res_get
2729
__wasi_clock_time_get
30+
__wasi_environ_get
31+
__wasi_environ_sizes_get
2832
__wasi_fd_advise
2933
__wasi_fd_allocate
3034
__wasi_fd_close

libc-bottom-half/headers/public/wasi/core.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,16 @@ _Static_assert(_Alignof(__wasi_subscription_t) == 8, "non-wasi data layout");
426426
#define __WASI_SYSCALL_NAME(name) \
427427
__attribute__((__import_module__("wasi_unstable"), __import_name__(#name)))
428428

429+
__wasi_errno_t __wasi_arg_sizes_get(
430+
size_t *argc,
431+
size_t *argv_size
432+
) __WASI_SYSCALL_NAME(arg_sizes_get) __attribute__((__warn_unused_result__));
433+
434+
__wasi_errno_t __wasi_argv_get(
435+
char **argv,
436+
char *argv_buf
437+
) __WASI_SYSCALL_NAME(argv_get) __attribute__((__warn_unused_result__));
438+
429439
__wasi_errno_t __wasi_clock_res_get(
430440
__wasi_clockid_t clock_id,
431441
__wasi_timestamp_t *resolution
@@ -437,6 +447,16 @@ __wasi_errno_t __wasi_clock_time_get(
437447
__wasi_timestamp_t *time
438448
) __WASI_SYSCALL_NAME(clock_time_get) __attribute__((__warn_unused_result__));
439449

450+
__wasi_errno_t __wasi_environ_get(
451+
char **environ,
452+
char *environ_buf
453+
) __WASI_SYSCALL_NAME(environ_get) __attribute__((__warn_unused_result__));
454+
455+
__wasi_errno_t __wasi_environ_sizes_get(
456+
size_t *environ_count,
457+
size_t *environ_size
458+
) __WASI_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__));
459+
440460
__wasi_errno_t __wasi_fd_close(
441461
__wasi_fd_t fd
442462
) __WASI_SYSCALL_NAME(fd_close) __attribute__((__warn_unused_result__));

0 commit comments

Comments
 (0)