If using a custom toolchain not on the path, explicitly invoking
ccache/sccache is required (also easier to debug that the right compiler
is being invoked). Fixes #253.
Add a build flag MALLOC_IMPL that can be set to dlmalloc or none
(defaulting to dlmalloc) which controls the malloc implementation to
use. The dlmalloc option is the same as before, but selecting none
removes dlmalloc from the libc build.
This flag replaces the BUILD_DLMALLOC flag, which never worked and thus
can be removed without breaking any builds. By switching to MALLOC_IMPL,
there is a clear path towards a different heap implementation, such as
mimalloc.
Dan Gohman [Thu, 11 Mar 2021 15:26:31 +0000 (07:26 -0800)]
Add a function to de-initialize the environment-variable state.
Add a `__wasilibc_deinit_environ` function which clears the current
environment variable state to the state where next time the environment
variable functions are called, they'll reinitialize the environment.
And add a `__wasilibc_maybe_reinitialize_environ_eagerly` function to
reinitialize the environment variable state if `environ` or `_environ`
are needed.
These functions are needed by wizer to be able to suspend and resume
a program and have it read new environment variables from the host
environment; see bytecodealliance/wizer#8 for background.
Dan Gohman [Mon, 22 Mar 2021 16:33:30 +0000 (09:33 -0700)]
Emulate `clock`, `times`, and `getrusage` using the monotonic clock.
Upcoming WASI snapshots omit the `PROCESS_CPUTIME` clock, since WASI has
no inherent concept of processes, and since implementations which don't
use a process for each instance don't have a way to implement it
efficiently.
However, `clock`, `times`, and `getrusage` are useful functions, so
provide optional emulated version of them, using the `MONOTONIC` clock.
This means these implementations will measure not just the program's
own CPU time, but also time spent suspended while other programs are
running.
Due to this difference in behavior, put these implementations behind
a flag. Users must pass `-D_WASI_EMULATED_PROCESS_CLOCK` and link with
`-lwasi-emulated-process-clocks` to enable them.
Alex Crichton [Tue, 23 Feb 2021 18:18:56 +0000 (12:18 -0600)]
Update to the next version of the `witx` crate (#234)
* Update to the next version of the `witx` crate
* Generate adapter functions instead of simply a header file to have a
place where adapter code can go.
* Implement adapters in terms of the instructions that the `witx` crate
tells us about.
* Update the interface of functions to what `witx` expects, notably
string arguments are now only taken as `char*` and `strlen` happens in
the adapter function.
* Update defined/predefined/undefined symbol lists for types that have
been updated.
Some precise generated code has changed but the actual APIs should all
be the same except for the change to not take the length of the string
in the raw WASI call, since idiomatically C doesn't pass the length of
strings around.
Eventually it's expected that the shim functions, while sometimes not
necessary today, will implement more checks and more conversions as
necessary for new APIs.
* Actually update witx submodule
* Comment how to regenerate files
* Tweak name of actual function imports
Make it a bit clearer that they're the ones that we're importing and
calling.
Dan Gohman [Thu, 4 Feb 2021 17:29:21 +0000 (09:29 -0800)]
AT_FDCWD support.
AT_FDCWD is a special constant in POSIX that can be passed to *at
functions to indicate the current working directory. Since the
current working directory is emulated in wasi libc, add emulated
AT_FDCWD support as well.
Dan Gohman [Fri, 5 Feb 2021 00:36:38 +0000 (16:36 -0800)]
Remove __wasilibc_unmodified_upstream markers from libc-bottom-half.
We've already started removing this; this just removes all remaining
ones under the libc-bottom-half directory.
These markers were originally intended to help track upstream changes,
however in practice they created a lot of clutter and weren't that
helpful. And now, upstream cloudlibc is no longer active.
Albert Cervin [Thu, 10 Dec 2020 16:31:21 +0000 (17:31 +0100)]
Use separate makefile var for CFLAGS (#226)
CFLAGS now get initialized with the provided value for WASM_CFLAGS, or
its default.
This seems to be the intended use case of having `WASM_CFLAGS ?=` at the
top of the Makefile. Otherwise, if WASM_CFLAGS is set on the command
line, it overrides all CFLAGS and for example `--sysroot` is not added,
rendering the build invalid unless it is supplied on the make command
line.
Dan Gohman [Mon, 30 Nov 2020 21:25:28 +0000 (13:25 -0800)]
Fix sinh's sign handling
In the musl 1.2.1 update, I made a change to disable the new code in
sinh for handling directed rounding modes, but I only incompletely
disabled it. This led to `sinh(-inf)` computing `inf` instead of `-inf`,
detected in [wasi-libc-test]. This patch fixes it.
Dan Gohman [Mon, 30 Nov 2020 20:47:45 +0000 (12:47 -0800)]
Update to musl 1.2.1. (#222)
See the WHATSNEW file for details. WASI libc currently uses a separate
malloc, so the new mallocng is not currently used.
This includes a few new custom changes to disable code for handling
directed rounding modes. Wasm doesn't have directed rounding modes,
so disabling this code size saves code size, something the WASI libc
project cares about!
Alex Crichton [Mon, 30 Nov 2020 20:40:41 +0000 (14:40 -0600)]
Add basic emulation of getcwd/chdir (#214)
* Add basic emulation of getcwd/chdir
This commit adds basic emulation of a current working directory to
wasi-libc. The `getcwd` and `chdir` symbols are now implemented and
available for use. The `getcwd` implementation is pretty simple in that
it just copies out of a new global, `__wasilibc_cwd`, which defaults to
`"/"`. The `chdir` implementation is much more involved and has more
ramification, however.
A new function, `make_absolute`, was added to the preopens object. Paths
stored in the preopen table are now always stored as absolute paths
instead of relative paths, and initial relative paths are interpreted as
being relative to `/`. Looking up a path to preopen now always turns it
into an absolute path, relative to the current working directory, and an
appropriate path is then returned.
The signature of `__wasilibc_find_relpath` has changed as well. It now
returns two path components, one for the absolute part and one for the
relative part. Additionally the relative part is always dynamically
allocated since it may no longer be a substring of the original input
path.
This has been tested lightly against the Rust standard library so far,
but I'm not a regular C developer so there's likely a few things to
improve!
* Amortize mallocs made in syscalls
* Avoid size bloat on programs that don't use `chdir`
* Add threading compat
* Collect `link`/`renameat` second path lookup
* Update comments about chdir.c in makefile
* Move definition of `__wasilibc_find_relpath_alloc` to header
Dan Gohman [Sat, 3 Oct 2020 21:18:39 +0000 (14:18 -0700)]
New-style command support. (#203)
This adds a new crt1-command.c startup file, which uses
[new-style command support]. Instead of calling `__wasm_call_ctors`
and `__wasm_call_dtors` directly, this lets wasm-ld automatically call
them.
This preserves the existing crt1.c, so that the same wasi-libc build
can support old-style and new-style commands, for compatibility during
the transition.
Dan Gohman [Fri, 31 Jul 2020 20:44:58 +0000 (13:44 -0700)]
Redefine the `access` bits to be compatible with other systems (#210)
Normally bits like this would be considered implementation details, but
in this case, `X_OK`, `W_OK`, and `R_OK` line up with `S_IXOTH`,
`S_IWOTH`, and `S_IROTH` on other systems, and those bits do have
well-known values.
Dan Gohman [Mon, 8 Jun 2020 22:21:18 +0000 (15:21 -0700)]
Rename `__prepare_for_exit` to `__wasm_call_dtors`. (#201)
This emphasizes the relationship with `__wasm_call_ctors`. Note however
that while `__wasm_call_ctors` is synthesized by the linker,
`__wasm_call_dtors` is still defined by libc.
Static constructors are registered statically, but static destructors
need to be registered dynamically so that they only run if their
corresponding static constructors have run, and so that they're
ordered with respect to interleaved `atexit` calls.
Dan Gohman [Tue, 2 Jun 2020 02:00:30 +0000 (19:00 -0700)]
Avoid calling `poll_oneoff` with zero subscriptions. (#162)
* Avoid calling `poll_oneoff` with zero subscriptions.
With https://github.com/WebAssembly/WASI/pull/193 merged, WASI is moving
to make `poll_oneoff` with no arguments an error. Even though that's in
ephemeral and not yet in a snapshot, we can start to anticipate it in
libc:
- Remove the `pause` function, since WASI has no signals and thus no
way to ever wake it up short of having the host terminate it.
- Make `poll` and `pselect` return `ENOTSUP` in the case of having no
events to wait for.
* Remove `pause` from the defined-symbols.txt list.
* Fix __wasilibc_unmodified_upstream markers.
* Check for zero subscriptions, rather than zero events.
Make `poll` and `pselect` return `ENOTSUP` when asked to poll on zero
subscriptions, rather than when the systerm returns zero events.
While here, drop the `__wasilibc_unmodified_upstream` markers, which
were already pretty noisy here, and would be significantly worse with
this change.
* Add comments about the subtle relationship between nfds and nsubscriptions.
Dan Gohman [Mon, 1 Jun 2020 21:32:24 +0000 (14:32 -0700)]
Rewrite the preopen functionality. (#173)
* Rewrite the preopen functionality.
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.
whitequark [Tue, 26 May 2020 19:14:52 +0000 (19:14 +0000)]
Make munmap() actually work. (#198)
Before this commit, he header of a mapped area, `struct map`, was
defined as follows:
struct map {
int prot;
int flags;
off_t offset;
size_t length;
char body[];
};
Because the size and alignment of an `off_t` is 8 bytes, the entire
structure was padded to 24 bytes. However, the offset of `body` into
`struct map` was only 20 bytes. Therefore the code in mmap() and
munmap() did not agree on the offset from header to body.
This commit changes mmap() to skip the entire header, which is what
munmap() expects and what the size calculation uses.
Dan Gohman [Fri, 20 Mar 2020 22:45:37 +0000 (15:45 -0700)]
Makefile refactoring; NFC (#186)
* Use ?= for variables intended to be overridable.
* Use `override` consistently for variables not meant to be overridden.
* Omit miscellaneous comments from the build output.
* Tidy up some comments.
* Use `addprefix` to factor out a common prefix.
* Add a comment.
* Reorganize.
* Adjust indentation.
* Simplify the logic for the `check` rule.
* Remove the `override` keywords.
They theoretically protect what the Makefile considers to be
implementation details from being overridden on the command-line,
but in practice this isn't super important, and they add a lot of
clutter.
Dan Gohman [Thu, 19 Mar 2020 16:32:41 +0000 (09:32 -0700)]
Lazy-initialize the environment variables. (#184)
* Lazy-initialize the environment variables.
This is the first in a series of PRs to make it easier to use WASI libc
in Wasm modules that don't have a `main` function. By initializing the
environment on demand, we avoid depending on having `__wasm_call_ctors`
run.
This uses weak symbols strategically to ensure that if `environ` is
used, it is initialized eagerly, but if only `getenv` and friends
are used, the environment is initialized lazily.
Eventually, I expect we'll have a convention for wasm modules without
main functions which will allow the `__wasm_call_ctors` function to be
called automatically, but this helps in simple cases for now.
Fixes #180.
* Add comments explaining the libc-environ-compat.h header usage.
Dan Gohman [Tue, 10 Mar 2020 23:35:50 +0000 (16:35 -0700)]
Update WASI Libc's README.md.
Update the description, point users to wasi-sdk as a simpler place to
get started using this library, and remove old text about being a
"reference" implementation.
Dan Gohman [Fri, 20 Dec 2019 23:14:02 +0000 (15:14 -0800)]
Add support for `__main_argc_argv`.
This adds support for the `__main_argc_argv` change, while preserving
compatibility with `__original_main`. This is needed by the LTO build
because the `__original_main` hack works in LLVM codegen, which is after
LTO. The `__main_argc_argv` change is implemented in clang, which makes
it properly visible to LTO.
Dan Gohman [Wed, 26 Feb 2020 16:56:59 +0000 (08:56 -0800)]
Update to musl 1.2.0.
See the WHATSNEW file for details. The biggest change in musl is the
switch to 64-bit time_t for 32-bit targets, however WASI libc was already
using 64-bit time_t. The main change affecting WASI is an update to
Unicode 12.1.0.
Dan Gohman [Mon, 14 Oct 2019 16:00:02 +0000 (09:00 -0700)]
Update to musl 1.1.24.
See the WHATSNEW file for details; this doesn't have any major changes
for wasi-libc; in particular, the new catgets and GLOB_TILDE features
are disabled.
Pat Hickey [Thu, 6 Feb 2020 21:21:11 +0000 (13:21 -0800)]
cloudlibc & libpreopen: changes for tagged unions
we decided to abandon the upstream code guarded by
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
because these changes are sprawling and those guards are of diminishing
importance
Daniel Bevenius [Fri, 7 Feb 2020 13:18:10 +0000 (14:18 +0100)]
Correct minor typo in c_headers.rs (#166)
The header api.h was update using the following command:
$ cd tools/wasi-headers
$ cargo run -- WASI/phases/snapshot/witx/typenames.witx \
WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx \
--output ../../libc-bottom-half/headers/public/wasi/api.h
Dan Gohman [Fri, 20 Dec 2019 23:43:31 +0000 (15:43 -0800)]
Move math source files. (#151)
This is split out from the LTO change. It's needed by the LTO build so
that we can easily build these files in non-LTO mode, since they satisfy
calls emitted by LLVM codegen after LTO.
Dan Gohman [Mon, 25 Nov 2019 22:04:45 +0000 (14:04 -0800)]
Use constructor functions for optional init routines. (#142)
* Use constructor functions for optional init routines.
Instead of using weak symbols, use constructor function attributes for the
environment and preopen initialization routines. This is simpler, uses
less code, and is more LTO-friendly.
* Change the constructor priorities to start at 50.
We don't currently have specific plans for other levels in the reserved
range (0-100), so leave room for both lower and higher priorities.
Dan Gohman [Fri, 22 Nov 2019 04:55:26 +0000 (20:55 -0800)]
Correct the version of #136 on master (#141)
* Add the WASI repo as a submodule.
Also, add the witx filenames to the generated output, and just have
`cargo run` auto-generate the api.h header, rather than using clap.
* Switch witx to a path dependency.
* Add a test.
* Add a test that the generated file is in sync with the generator.
* Enable CI testing with Github Actions.
* Fix the name of the wasi-headers directory.
* Enable submodules.
* Add a diff mechanism to help explain failures.
* Sort the inputs for display.
* More debugging.
* More debugging.
* Add a .gitattributes file forcing text files to be eol=lf.
Most editors these days can deal with eof=lf files, even on Windows, and
this avoids trouble with headers and other generated files differing in
line endings.