forked from WebAssembly/wasi-libc
-
Notifications
You must be signed in to change notification settings - Fork 31
Add implementation of memfd_create #100
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
Open
zebreus
wants to merge
10
commits into
main
Choose a base branch
from
implement-missing-thing-for-dash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8925fa0
Implement memfs_create
zebreus e9b523b
Add memfd_create to expected symbols
zebreus 5fc3ef6
Apply review comments
zebreus 2fbcffe
Fix flag check
zebreus 5e49e07
Add name to memfd_create
zebreus 4f9cf69
Allow empty names in memfd_create
zebreus d1495fa
Apply minor review comments
zebreus bf4c188
Restore the placeholder after the first failed mkstemp
zebreus 191c385
Format with clang_format
zebreus 3860d9c
Rephrase comment
zebreus 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
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 |
|---|---|---|
|
|
@@ -1203,6 +1203,7 @@ memccpy | |
| memchr | ||
| memcmp | ||
| memcpy | ||
| memfd_create | ||
| memmem | ||
| memmove | ||
| mempcpy | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -1211,6 +1211,7 @@ memccpy | |
| memchr | ||
| memcmp | ||
| memcpy | ||
| memfd_create | ||
| memmem | ||
| memmove | ||
| mempcpy | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -1133,6 +1133,7 @@ memccpy | |
| memchr | ||
| memcmp | ||
| memcpy | ||
| memfd_create | ||
| memmem | ||
| memmove | ||
| mempcpy | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -1209,6 +1209,7 @@ memccpy | |
| memchr | ||
| memcmp | ||
| memcpy | ||
| memfd_create | ||
| memmem | ||
| memmove | ||
| mempcpy | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -1167,6 +1167,7 @@ memccpy | |
| memchr | ||
| memcmp | ||
| memcpy | ||
| memfd_create | ||
| memmem | ||
| memmove | ||
| mempcpy | ||
|
|
||
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,93 @@ | ||
| #define _GNU_SOURCE | ||
|
|
||
| #include <ctype.h> | ||
| #include <errno.h> | ||
| #include <fcntl.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/mman.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define MEMFD_NAME_MAX 249 | ||
|
|
||
| static int create_unlinked_temp(char *template_path, unsigned int flags) { | ||
| int fd; | ||
| if (flags & MFD_CLOEXEC) { | ||
| fd = mkostemp(template_path, O_CLOEXEC); | ||
| } else { | ||
| fd = mkstemp(template_path); | ||
| } | ||
| if (fd < 0) | ||
| return -1; | ||
|
|
||
| if (unlink(template_path) != 0) { | ||
| int saved_errno = errno; | ||
| close(fd); | ||
| errno = saved_errno; | ||
| return -1; | ||
| } | ||
|
|
||
| return fd; | ||
| } | ||
|
|
||
| // Returns the number of characters copied, excluding any null terminator. | ||
| // If the returned length == dst_len, src contained at least dst_len | ||
| // non-null characters and dst is not null-terminated (no room for a terminator). | ||
| static int sanitize_name(char *dst, size_t dst_len, const char *src) { | ||
| size_t i = 0; | ||
|
|
||
| while (i < dst_len && src[i] != '\0') { | ||
| unsigned char c = (unsigned char)src[i]; | ||
| if (isalnum(c) || c == '_' || c == '-' || c == '.') { | ||
| dst[i] = (char)c; | ||
| } else { | ||
| dst[i] = '_'; | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| return i; | ||
| } | ||
|
|
||
| int memfd_create(const char *name, unsigned int flags) { | ||
| if (flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)) { | ||
| errno = EINVAL; | ||
| return -1; | ||
| } | ||
|
|
||
| if (flags & (MFD_HUGETLB | MFD_ALLOW_SEALING)) { | ||
| /* Seals are not currently supported by the WASIX fcntl subset. */ | ||
| /* hugetlbfs is not currently supported by the WASIX filesystem subset. */ | ||
| errno = ENOTSUP; | ||
| return -1; | ||
| } | ||
|
|
||
| if (name == NULL) { | ||
| errno = EFAULT; | ||
| return -1; | ||
| } | ||
|
|
||
| // Construct a string like "/tmp/.memfd-<name>-XXXXXX" | ||
| char tmp_template[sizeof("/tmp/.memfd-") - 1 + MEMFD_NAME_MAX + | ||
| sizeof("-XXXXXX") - 1 + 1]; | ||
| memcpy(tmp_template, "/tmp/.memfd-", sizeof("/tmp/.memfd-") - 1); | ||
| int name_len = sanitize_name(tmp_template + sizeof("/tmp/.memfd-") - 1, | ||
| MEMFD_NAME_MAX + 1, name); | ||
| if (name_len > MEMFD_NAME_MAX) { | ||
| errno = EINVAL; | ||
| return -1; | ||
| } | ||
| memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", | ||
| sizeof("-XXXXXX")); | ||
|
|
||
| int fd = create_unlinked_temp(tmp_template, flags); | ||
| if (fd >= 0) | ||
| return fd; | ||
|
|
||
| // If that failed, try ".memfd-<name>-XXXXXX" in the current working | ||
| // directory. Restore the -XXXXXX placeholder because it got overwritten by | ||
| // the previous call to mkstemp. | ||
| memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", | ||
| sizeof("-XXXXXX")); | ||
| return create_unlinked_temp(tmp_template + sizeof("/tmp/") - 1, flags); | ||
|
zebreus marked this conversation as resolved.
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.