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.
Dan Gohman [Thu, 21 Nov 2019 23:49:51 +0000 (15:49 -0800)]
Ignore rights in libpreopen. (#129)
Don't ignore paths which don't have the required rights. This means
that if the lookup finds a path that doesn't have the required
rights, it'll just proceed to the actual operation which will fail
with `ENOTCAPABLE`.
Intuitively, use cases which would depend on having multiple
overlapping matching paths for a given lookup and intelligently
picking the one with the required rights seems like they should
be uncommon.
Dan Gohman [Sun, 10 Nov 2019 14:39:00 +0000 (06:39 -0800)]
Don't pre-check capabilities in `openat`. (#130)
Rely on the WASI implementation to check capabilities flags, rather
than also checking them in the userspace `openat` implementation.
This code is admittedly getting fairly dense with `#ifdef`s, so it
may soon make sense to move this file out of the `cloudlibc`
directory and removing the upstream change markers.
Dan Gohman [Fri, 8 Nov 2019 19:59:57 +0000 (11:59 -0800)]
Use consistent style for wasi-libc C source files. (#131)
For now, this means using `//`-style comments in .c source files (though
not public header files), and spaces rather than tabs. No strong opinion
here; this is just what the majority of the current code is using.
This also synchronizes basics/crt/crt1.c with libc-bottom-half's
version, though this is just a cleanup as the former isn't currently used
by the main wasi-libc build.
Dan Gohman [Thu, 7 Nov 2019 21:09:31 +0000 (13:09 -0800)]
Temporarily disable the use of `__heap_base`. (#132)
This temporarily disables the feature in a214f1c0b167608ab4ec3e63f8144c3430af9372, since it hits LLVM bug
43613, which is fixed on LLVM master and awaiting a backport to
the 9.0 branch.
Dan Gohman [Mon, 4 Nov 2019 22:56:35 +0000 (14:56 -0800)]
Revamp and simplify the libpreopen code. (#110)
wasi-libc's copy of libpreopen has evolved so many local changes that
it's no longer worth keeping the upstream code structure and marking
changes with __wasilibc_unmodified_upstream.
This PR merges the source files into a single file, removes all
__wasilibc_unmodified_upstream code, eliminates the ability to
allocate multiple preopen lists, eliminates the need for
__wasilibc_init_preopen, eliminates the non-standard eaccess, and
makes several other cleanups. It also enables NDEBUG so that internal
assertions are disabled in release builds.
Dan Gohman [Mon, 4 Nov 2019 21:17:03 +0000 (13:17 -0800)]
Remove more unsupported headers. (#123)
utime.h, sysmacros.h, and libintl.h are all currently unsupported.
Removing them helps programs that autodetect features based on the
existence of headers.
Dan Gohman [Fri, 25 Oct 2019 00:30:46 +0000 (17:30 -0700)]
Call `populate_args` only if we actually need command-line arguments (#112)
* Link `populate_args` only if we actually need command-line arguments.
This avoids linking in the argv/argc initialization code,
and the __wasi_args_sizes_get and __wasi_args_get imports, in
programs that don't use command-line arguments. The way this works is,
if the user writes `int main(int argc, char *argv[])`, the argument
initialization code is loaded, and if they write `int main(void)`,
it's not loaded.
This promotes the `__original_main` mechanism into an effective contract
between the compiler and libc, which wasn't its original purpose,
however it seems to fit this purpose quite well.
* Document that `__original_main` may be the user's zero-arg `main`.
Dan Gohman [Tue, 22 Oct 2019 13:02:44 +0000 (06:02 -0700)]
Call `populate_environ` only if we actually need environment variables. (#109)
* Link `populate_environ` only if we actually need environment variables.
This avoids linking in the environment variable initialization code,
and the __wasi_environ_sizes_get and __wasi_environ_get imports, in
programs that don't use environment variables.
This also removes the "___environ" (three underscores) alias symbol,
which is only in musl for backwards compatibility.
* Switch to //-style comments.
* If malloc fails, don't leave `__environ` pointing to an uninitialized buffer.
* Fix a memory leak if one malloc succeeds and the other fails.
* Use calloc to handle multiplication overflow.
This also handles the NULL terminator.
* Don't initialize __environ until everything has succeeded.
* Avoid leaking in case __wasi_environ_get fails.
* Handle overflow in the add too.
* Add #include <stdlib.h> for malloc etc.
* If the environment is empty, don't allocate any memory.
Dan Gohman [Fri, 11 Oct 2019 12:07:34 +0000 (05:07 -0700)]
Remove -fno-builtin. (#104)
* Remove -fno-builtin.
-fno-builtin suppresses optimizations such as turning calls to `sqrt`
or `fabs` into `f64.sqrt` or `f64.abs` instructions inline. Libc code
itself benefits from these optimizations.
I originally added this flag because historically it was needed when
building libc to avoid the compiler pattern-matching the body of memcpy
into a memcpy call, however clang no longer requires this.
* Expand the comment about why we use USE_DL_PREFIX in dlmalloc.
Dan Gohman [Mon, 29 Apr 2019 12:52:26 +0000 (05:52 -0700)]
Omit musl's stdarg.h and stddef.h from the sysroot.
We already use the compiler's versions of these; this just moves from
having libc #include_next them to having libc just omit them entirely,
which is simpler.
This removes the special code to define musl's include guard, however
I originally added that when I was still working out how WASI's stddef.h
would interact with other code. I believe it's no longer important.
Dan Gohman [Fri, 19 Jul 2019 22:06:57 +0000 (15:06 -0700)]
Set the MALLOC_ALIGNMENT to 16.
This isn't strictly required, as wasm SIMD loads and stores work on
unaligned memory. However, it may provide better performance. That said,
this isn't currently studied by any benchmarking.
Dan Gohman [Wed, 10 Jul 2019 00:21:38 +0000 (17:21 -0700)]
Disable the lseek optimization in C++.
The lseek optimization turns lseek calls into __wasilibc_tell calls when
possible, using macros and __builtin_constant_p. However, this isn't
safe in C++ code in the presence of namespaces and `using` declarations,
to just disable it in C++ for now.
Dan Gohman [Fri, 3 May 2019 20:55:41 +0000 (13:55 -0700)]
Use builtin functions rather than portable libm implementations.
For functions like sqrt, fabs, and others, use the builtin functions,
which provide single-instruction implementations, rather than using
musl's portable implementations.
Sam Clegg [Wed, 15 May 2019 16:54:54 +0000 (09:54 -0700)]
Don't create dummy libs for libc++/libc++abi (#68)
My understanding is that these dummy libs represent libraries who's
contents, under musl, live in libc itself rather than being split out
into separate libs.
libc++/libc++abi are not provided my musl so doesn't make sense to
pretend that we provide them.
This replaces our custom `unlink` wrapper with an upstream one. We still
end up replacing the entire body with local changes, but this makes it
easier to see what those changes are.
The other change here is a fix to ignore repeated '/'s in paths.
Dan Gohman [Wed, 8 May 2019 16:13:47 +0000 (09:13 -0700)]
Move the SEEK_* macros into their own header and use it from fcntl.h.
POSIX requires fcntl.h to define the SEEK_* macros, so this satisfies
that requirement. Also, this allows <stdio.h> to avoid including as much
unnecessary content.