Commit Graph

79 Commits

Author SHA1 Message Date
Dan Gohman
215adc8ac9
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.
2020-07-31 13:44:58 -07:00
Dan Gohman
4e45d2b5ad
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.
2020-06-08 15:21:18 -07:00
Dan Gohman
5a7ba74c19
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.

* Rewrite the comment.

* Fix code quotes.
2020-06-01 19:00:30 -07:00
Dan Gohman
753cc4344d
Merge the basics component into libc-bottom-half. (#199)
We no longer have a need to maintain basics as a separate component.
Folding it into libc-bottom-half eliminates a fair amount of redundancy.
2020-06-01 16:44:05 -07:00
Dan Gohman
84c0778bff
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.

* Fix missing #include.

* Fix a compilation error.
2020-06-01 14:32:24 -07:00
Dan Gohman
d1cd4f4836
Replace old <signal.h> support with emulated <signal.h> support. (#183)
wasm doesn't support signals, so provide an emulation layer, enabled
with -D_WASI_EMULATED_SIGNAL and -lwasi-emulated-signal.
2020-05-29 13:07:52 -07:00
Dan Gohman
a6d0b4c74b
Reactor support. (#74)
* Add support for the Reactor model.

* Mark _activate and _start as wasm exports.

* Rename _activate to _initialize.

* Don't define `_fini`.

* Rename reactor-crt1.c to crt1-reactor.c.
2020-05-29 09:09:58 -07:00
whitequark
3e9892fc41
Make mmap() possible to use in a conformant way. (#197) 2020-05-26 12:49:13 -07:00
whitequark
9b9d243bea
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.
2020-05-26 12:14:52 -07:00
Olaf Tomalka
7b92f334e6
Added utime.h (#188)
* Added utime.h

* Changes after code review

* Auto-generated expected files instead of manually editing
* Fix libpreopoen stat and utime not following symlinks correctly
2020-04-03 15:39:43 -07:00
Pat Hickey
156fdc476a regenerate api.h 2020-04-02 15:54:40 -07:00
Dan Gohman
9efc2f4283
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.
2020-03-19 09:32:41 -07:00
Dan Gohman
0cc57ac7b4
Multi-license wasi-libc under Apache and MIT licenses. (#174)
Multi-license wasi-libc under the Apache-2.0 WITH LLVM-exception,
Apache-2.0, and MIT licenses.
2020-02-29 10:52:28 +01:00
Dan Gohman
d9066a87c0 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.
2020-02-27 13:09:38 -08:00
Dan Gohman
79a9b40837 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.
2020-02-26 10:23:05 -08:00
Pat Hickey
3de8c71d06 libc: change to flattened event struct 2020-02-24 11:36:05 -08:00
Pat Hickey
7f8930168f update to WASI where snashot event_u flattened to struct 2020-02-24 11:36:05 -08:00
Pat Hickey
c2ae180dee 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
2020-02-24 11:36:05 -08:00
Pat Hickey
852d093a3c wasi/api.h: regenerated with tagged unions 2020-02-24 11:36:05 -08:00
Dan Gohman
870a25121b
Remove unused cloudlibc headers. (#170) 2020-02-21 11:40:23 -08:00
Dan Gohman
2c2fc9a2fd
Don't call free on paths which are about to call _Exit. (#161)
This is a minor code-size optimization.
2020-02-14 14:02:59 -08:00
Pat Hickey
c6f2c0517f
gen-headers: Generate assertions of layout from witx (#149)
* use pch/layout branch for witx; generate assertions of layout

* address review comments, add asserts for handle

* change wasm32 support comment to a preprocessor error

* expose `to_c_header` in wasi-headers crate for use in external test harness

* main.rs: inputs and output arguments are optional

so that generate-libc command works

* regen header
2020-02-07 13:21:18 -08:00
Daniel Bevenius
37c663f2f0
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
2020-02-07 05:18:10 -08:00
Dan Gohman
1fad33890a
Fix environment variable init to exit successfully. (#159)
This fixes https://github.com/bytecodealliance/wasmtime/issues/783.
2020-01-14 09:56:58 -08:00
Dan Gohman
dd010beea5
Avoid using cast expressions in WASI API constants. (#148)
Cast expressions aren't valid to use in some preprocessor constants, so
change the constants to use `UINT32_C` etc. instead of casts.

Fixes #146.
2019-12-16 13:29:06 -08:00
Sam Clegg
f645f498df Update signal macros after upgrade to snapshot1 (#144)
This should probably have been part of #140 but we don't actually
support signals so this doesn't get much testing I imagine.

The old names like `__WASI_SIGBUS` no longer exist and the new names
look like `__WASI_SIGNAL_BUS`.
2019-12-03 09:23:40 -08:00
Dan Gohman
410c66070a
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.
2019-11-25 14:04:45 -08:00
Pat Hickey
fe130532ae
c header generation updated for reorganized witx ast (#139) 2019-11-22 13:06:12 -05:00
Dan Gohman
cd74e1d988
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.
2019-11-21 20:55:26 -08:00
Dan Gohman
446cb3f1aa
Wasi snapshot preview1 (#140)
* Make __wasi_linkcount_t a uint64_t (#134)

Refs: https://github.com/WebAssembly/WASI/pull/127

* Generate the WASI interface from witx.

This replaces the hand-maintained <wasi/core.h> header with a
<wasi/api.h> generated from witx.

Most of the churn here is caused by upstream WASI renamings; hopefully
in the future ABI updates will be less noisy.
2019-11-21 20:06:00 -08:00
Dan Gohman
54102f06a9
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.

This is simpler overall, and requires less code.
2019-11-21 15:49:51 -08:00
Dan Gohman
8c9e1c64a8
Make the __original_main definition weak, fixing -flto. (#138) 2019-11-20 11:35:47 -08:00
Dan Gohman
deb8eae418
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.
2019-11-10 06:39:00 -08:00
Dan Gohman
ca9046d872
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.
2019-11-08 11:59:57 -08:00
Dan Gohman
951cc3eceb
Fix unintended recursion in __wasilibc_register_preopened_fd. (#133) 2019-11-08 11:44:18 -08:00
Dan Gohman
5216983ad7
Avoid a strdup call in __wasilibc_populate_libpreopen. (#128)
* Avoid a `strdup` call in `__wasilibc_populate_libpreopen`.

Optimize `__wasilibc_populate_libpreopen` to avoid calling `strdup` in
the common case where it's called from `__wasilibc_populate_libpreopen`.

* Convert an if into a ?:.
2019-11-08 11:31:44 -08:00
Dan Gohman
70099d4d1c
Don't link in libpreopen initialization code when it isn't needed. (#127)
* Avoid linking in `populate_libpreopen` when it isn't needed.

* Merge adjacent `if`s using `&&`.
2019-11-08 11:08:22 -08:00
Dan Gohman
a94d2d04e7
Avoid varargs conventions when calling open (#126)
* Add an entrypoint for calling open that bypasses the varargs.

* Add an entrypoint for calling openat that bypasses the varargs.
2019-11-04 16:37:45 -08:00
Dan Gohman
7fcc4f29df
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.
2019-11-04 14:56:35 -08:00
Dan Gohman
a8a5dbcf91
Ensure __environ is initialized even when it's empty. (#125)
POSIX requires `environ` to be a pointer to a NULL-terminated array of
pointers, so it itself can't be NULL.

This fixes a regression in src/functional/env.c in wasi-libc-test.
2019-10-28 10:34:57 -07:00
Dan Gohman
afbf94c39e
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`.
2019-10-24 17:30:46 -07:00
Dan Gohman
1f11b91e7f
Fix corner cases and style in the argv/argc code. (#118)
This fixes some potential memory leaks in pathological situations, and
updates the code to use `//`-style comments.
2019-10-22 11:25:19 -07:00
Dan Gohman
bdfbb54520
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.
2019-10-22 06:02:44 -07:00
vms
d253aa3de2 Make sbrk(0) deterministic (#115)
* fix the behavior of sbrk(0)

* review changes
2019-10-21 15:44:23 -07:00
Dan Gohman
b59b83cbc2
Miscellaneous cleanups (#113)
* Tidy up some #include names.

* Move non-cloudlibc sources out of the cloudlibc directory.
2019-10-18 13:53:13 -07:00
vms
c54004fcce fix the typo (#108)
* fix the typo
2019-10-14 14:52:10 -07:00
Dan Gohman
d498fc3ddf Fix a warning about a spurious redundant static keyword. 2019-08-29 10:27:41 -07:00
Dan Gohman
b7ae2fe33b WASI libc's dirent does have t_type, so define _DIRENT_HAVE_D_TYPE.
Also update predefined-macros.txt.
2019-08-29 10:22:03 -07:00
Sam Clegg
92eaf255a1 Ensure argv is null terminated.
This matches the behavior of populate __environ.

See #27
2019-05-30 08:15:23 -07:00
Dan Gohman
6cddc9250b Implement truncate(2).
This adds a simple implementation of truncate in terms of open and
ftruncate.
2019-05-15 11:53:11 -07:00