Skip to content

Commit 40d2d70

Browse files
acfoltzersunfishcode
authored andcommitted
Address review comments and fix refactoring goof
1 parent c3770fc commit 40d2d70

3 files changed

Lines changed: 29 additions & 26 deletions

File tree

basics/libc/crt1.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
#include <sysexits.h>
33
#include <wasi/core.h>
44

5-
extern char **environ;
5+
extern char **__environ;
66
extern void __wasm_call_ctors(void);
77
extern int main(int, char *[]);
88
extern void __prepare_for_exit(void);
99
void _Exit(int) __attribute__((noreturn));
1010

11-
static __wasi_errno_t populate_args(size_t *argc, char **argv) {
11+
static __wasi_errno_t populate_args(size_t *argc, char ***argv) {
1212
__wasi_errno_t err;
1313

1414
/* 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);
15+
size_t argv_buf_size;
16+
err = __wasi_args_sizes_get(argc, &argv_buf_size);
1717
if (err != __WASI_ESUCCESS) {
1818
return err;
1919
}
@@ -22,43 +22,46 @@ static __wasi_errno_t populate_args(size_t *argc, char **argv) {
2222
}
2323

2424
/* Allocate memory for the array of pointers. */
25-
argv = malloc(sizeof(char *) * *argc);
25+
*argv = malloc(sizeof(char *) * *argc);
2626
/* Allocate memory for storing the argument chars. */
27-
char *argv_buf = malloc(sizeof(char) * argv_size);
28-
if (argv == NULL || argv_buf == NULL) {
27+
char *argv_buf = malloc(sizeof(char) * argv_buf_size);
28+
if (*argv == NULL || argv_buf == NULL) {
2929
return __WASI_ENOMEM;
3030
}
3131

3232
/* Fill the argument chars, and the argv array with pointers into those chars. */
33-
return __wasi_argv_get(argv, argv_buf);
33+
return __wasi_args_get(*argv, argv_buf);
3434
}
3535

3636
static __wasi_errno_t populate_environ() {
3737
__wasi_errno_t err;
3838

3939
/* Get the sizes of the arrays we'll have to create to copy in the environment. */
4040
size_t environ_count;
41-
size_t environ_size;
42-
err = __wasi_environ_sizes_get(&environ_count, &environ_size);
41+
size_t environ_buf_size;
42+
err = __wasi_environ_sizes_get(&environ_count, &environ_buf_size);
4343
if (err != __WASI_ESUCCESS) {
4444
return err;
4545
}
4646
/* If there's no environment pairs, make sure environ is null and return. */
4747
if (environ_count == 0) {
48-
environ = NULL;
48+
__environ = NULL;
4949
return __WASI_ESUCCESS;
5050
}
5151

5252
/* Allocate memory for the array of pointers, plus one terminating null pointer. */
53-
environ = malloc(sizeof(char *) * (environ_count + 1));
53+
__environ = malloc(sizeof(char *) * (environ_count + 1));
5454
/* Allocate memory for storing the environment chars. */
55-
char *environ_buf = malloc(sizeof(char) * environ_size);
56-
if (environ == NULL || environ_buf == NULL) {
55+
char *environ_buf = malloc(sizeof(char) * environ_buf_size);
56+
if (__environ == NULL || environ_buf == NULL) {
5757
return __WASI_ENOMEM;
5858
}
5959

60+
/* Make sure the last pointer in the array is NULL. */
61+
__environ[environ_count] = NULL;
62+
6063
/* Fill the environment chars, and the __environ array with pointers into those chars. */
61-
return __wasi_environ_get(environ, environ_buf);
64+
return __wasi_environ_get(__environ, environ_buf);
6265
}
6366

6467
void _start(void) {
@@ -73,7 +76,7 @@ void _start(void) {
7376
/* Fill in the arguments from WASI syscalls. */
7477
size_t argc;
7578
char **argv;
76-
if (populate_args(&argc, argv) != __WASI_ESUCCESS) {
79+
if (populate_args(&argc, &argv) != __WASI_ESUCCESS) {
7780
_Exit(EX_OSERR);
7881
}
7982

expected/wasm32-wasi/undefined-symbols.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ __subtf3
2323
__trunctfdf2
2424
__trunctfsf2
2525
__unordtf2
26-
__wasi_arg_sizes_get
27-
__wasi_argv_get
26+
__wasi_args_get
27+
__wasi_args_sizes_get
2828
__wasi_clock_res_get
2929
__wasi_clock_time_get
3030
__wasi_environ_get

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,15 @@ _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(
429+
__wasi_errno_t __wasi_args_get(
435430
char **argv,
436431
char *argv_buf
437-
) __WASI_SYSCALL_NAME(argv_get) __attribute__((__warn_unused_result__));
432+
) __WASI_SYSCALL_NAME(args_get) __attribute__((__warn_unused_result__));
433+
434+
__wasi_errno_t __wasi_args_sizes_get(
435+
size_t *argc,
436+
size_t *argv_buf_size
437+
) __WASI_SYSCALL_NAME(args_sizes_get) __attribute__((__warn_unused_result__));
438438

439439
__wasi_errno_t __wasi_clock_res_get(
440440
__wasi_clockid_t clock_id,
@@ -454,7 +454,7 @@ __wasi_errno_t __wasi_environ_get(
454454

455455
__wasi_errno_t __wasi_environ_sizes_get(
456456
size_t *environ_count,
457-
size_t *environ_size
457+
size_t *environ_buf_size
458458
) __WASI_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__));
459459

460460
__wasi_errno_t __wasi_fd_close(

0 commit comments

Comments
 (0)