Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate the argv buffers with alloca instead of malloc. #121

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions libc-bottom-half/sources/__original_main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <wasi/core.h>
#include <wasi/libc.h>
#include <alloca.h>
#include <stdlib.h>
#include <sysexits.h>

Expand Down Expand Up @@ -27,27 +28,24 @@ int __original_main(void) {
}

// Allocate memory for storing the argument chars.
char *argv_buf = malloc(argv_buf_size);
if (argv_buf == NULL) {
_Exit(EX_SOFTWARE);
}
char *argv_buf = alloca(argv_buf_size);

// Allocate memory for the array of pointers. This uses `calloc` both to
// handle overflow and to initialize the NULL pointer at the end.
char **argv = calloc(num_ptrs, sizeof(char *));
if (argv == NULL) {
free(argv_buf);
// Allocate memory for the array of pointers.
size_t num_ptrs_size;
if (__builtin_mul_overflow(num_ptrs, sizeof(char *), &num_ptrs_size)) {
_Exit(EX_SOFTWARE);
}
char **argv = alloca(num_ptrs_size);

// Fill the argument chars, and the argv array with pointers into those chars.
err = __wasi_args_get(argv, argv_buf);
if (err != __WASI_ESUCCESS) {
free(argv_buf);
free(argv);
_Exit(EX_OSERR);
}

// Make sure the last pointer in the array is NULL.
argv[argc] = NULL;

// Call main with the arguments!
return main(argc, argv);
}