-
Notifications
You must be signed in to change notification settings - Fork 212
Call populate_args
only if we actually need command-line arguments
#112
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,7 @@ __ofl_lock | |
__ofl_unlock | ||
__optpos | ||
__optreset | ||
__original_main | ||
__overflow | ||
__p1evll | ||
__pio2_hi | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <wasi/core.h> | ||
#include <wasi/libc.h> | ||
#include <stdlib.h> | ||
#include <sysexits.h> | ||
|
||
// The user's `main` function, expecting arguments. | ||
int main(int argc, char *argv[]); | ||
|
||
// If the user's `main` function expects arguments, the compiler won't emit | ||
// an `__original_main` function so this version will get linked in, which | ||
// initializes the argument data and calls `main`. | ||
int __original_main(void) { | ||
__wasi_errno_t err; | ||
|
||
// Get the sizes of the arrays we'll have to create to copy in the args. | ||
size_t argv_buf_size; | ||
size_t argc; | ||
err = __wasi_args_sizes_get(&argc, &argv_buf_size); | ||
if (err != __WASI_ESUCCESS) { | ||
_Exit(EX_OSERR); | ||
} | ||
|
||
// Add 1 for the NULL pointer to mark the end, and check for overflow. | ||
size_t num_ptrs = argc + 1; | ||
if (num_ptrs == 0) { | ||
_Exit(EX_SOFTWARE); | ||
} | ||
|
||
// Allocate memory for storing the argument chars. | ||
char *argv_buf = malloc(argv_buf_size); | ||
if (argv_buf == NULL) { | ||
_Exit(EX_SOFTWARE); | ||
} | ||
|
||
// 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); | ||
_Exit(EX_SOFTWARE); | ||
} | ||
|
||
// 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); | ||
} | ||
|
||
// Call main with the arguments! | ||
return main(argc, argv); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment accurate?
Is this more like.. ".. will either be the application's zero-argument main function (renamed by the compiler) or a libc routine which populates
argv
andargc
and calls the application's two-argumentmain
."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I guess I was trying to keep it abstract here, but it isn't essential. I've now updated the comment to your suggested wording.