Skip to content

Commit

Permalink
Rewrite the preopen functionality.
Browse files Browse the repository at this point in the history
Rewrite the preopen functionality to be simpler, better organized,
and better integrated into WASI libc. Preopen support has diverged so
much from libpreopen that it no longer makes sense to track libpreopen
as an explicit upstream. And add more documentation.
  • Loading branch information
sunfishcode committed Feb 26, 2020
1 parent e1149ab commit 7edebdf
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 590 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ LIBC_BOTTOM_HALF_CLOUDLIBC_SRC = $(LIBC_BOTTOM_HALF_DIR)/cloudlibc/src
LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC = $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/include
LIBC_BOTTOM_HALF_HEADERS_PUBLIC = $(LIBC_BOTTOM_HALF_DIR)/headers/public
LIBC_BOTTOM_HALF_HEADERS_PRIVATE = $(LIBC_BOTTOM_HALF_DIR)/headers/private
LIBC_BOTTOM_HALF_LIBPREOPEN_DIR = $(LIBC_BOTTOM_HALF_DIR)/libpreopen
LIBC_BOTTOM_HALF_SOURCES = $(LIBC_BOTTOM_HALF_DIR)/sources
LIBC_BOTTOM_HALF_ALL_SOURCES = \
$(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \
$(LIBC_BOTTOM_HALF_LIBPREOPEN_DIR)/libpreopen.c \
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c)
LIBWASI_EMULATED_MMAN_SOURCES = \
$(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c)
Expand Down
29 changes: 13 additions & 16 deletions libc-bottom-half/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
"WASI" the WebAssembly System Interface.
# WASI libc "bottom half".

WASI libc is conceptually the lower half of a traditional libc implementation.
It provides C interfaces to the low-level WASI syscalls.
The WASI libc "bottom half" is conceptually the lower half of a traditional libc
implementation, consisting of C interfaces to the low-level WASI syscalls.

This is largely based on [CloudABI], [cloudlibc], and [libpreopen], however we
use just the low-level syscall wrappers rather than all of cloudlibc and
libpreopen, and we have several customizations for use in a WebAssembly sysroot.
This implementation is partially derived from the "bottom half" of [cloudlibc],
revision 8835639f27fc42d32096d59d294a0bbb857dc368.

[CloudABI]: https://github.com/NuxiNL/cloudabi
[cloudlibc]: https://github.com/NuxiNL/cloudlibc
[libpreopen]: https://github.com/musec/libpreopen

The upstream repositories and versions used here are:

cloudlibc - https://github.com/NuxiNL/cloudlibc 8835639f27fc42d32096d59d294a0bbb857dc368
libpreopen - https://github.com/musec/libpreopen b29e9287cc75a7db7291ce3eb468a3d2bad8ceb1
This implementation includes preopen functionality, which emulates POSIX APIs
accepting absolute paths by translating them into pre-opened directory handles
and relative paths that can be opened with `openat`. This technique is inspired
by [libpreopen], however the implementation here is designed to be built into
libc rather than to be a layer on top of libc.

Whole files which are unused are omitted. Changes to upstream code are wrapped
in preprocessor directives controlled by the macro `__wasilibc_unmodified_upstream`,
except that CloudABI names have also been renamed to WASI names without annotations.
[libpreopen]: https://github.com/musec/libpreopen

WASI libc currently depends on the basics and dlmalloc components of reference-sysroot.
The WASI libc "bottom half" depends on the basics and dlmalloc components of
wasi-libc.
3 changes: 2 additions & 1 deletion libc-bottom-half/headers/public/wasi/libc-find-relpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ extern "C" {
*
* Returns -1 if no directories were suitable.
*/
int __wasilibc_find_relpath(const char *path, const char **relative_path);
int __wasilibc_find_relpath(const char *path,
const char **__restrict__ relative_path);

#ifdef __cplusplus
}
Expand Down
19 changes: 18 additions & 1 deletion libc-bottom-half/headers/public/wasi/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@
extern "C" {
#endif

int __wasilibc_register_preopened_fd(int fd, const char *path);
/// Register the given pre-opened file descriptor under the given path.
///
/// This function does not take ownership of `prefix` (it makes its own copy).
int __wasilibc_register_preopened_fd(int fd, const char *prefix);

/// Renumber `fd` to `newfd`; similar to `dup2` but does a move rather than a
/// copy.
int __wasilibc_fd_renumber(int fd, int newfd);

/// Like `unlinkat`, but without depending on `__wasi_path_remove_directory`.
int __wasilibc_unlinkat(int fd, const char *path);

/// An `*at` version of rmdir.
int __wasilibc_rmdirat(int fd, const char *path);

/// Like `open`, but without the varargs in the signature.
int __wasilibc_open_nomode(const char *path, int oflag);

/// Like `openat`, but without the varargs in the signature.
int __wasilibc_openat_nomode(int fd, const char *path, int oflag);

/// Return the current file offset. Like `lseek(fd, 0, SEEK_CUR)`, but without
/// depending on `lseek`.
off_t __wasilibc_tell(int fd);

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 7edebdf

Please sign in to comment.