Commit Graph

35 Commits

Author SHA1 Message Date
Joel Dice
a2ed34e810
wasip2 support for close, poll, pselect (#486)
This enables `wasm32-wasip2` support for `close`, `poll`, and `pselect`.  I
cheated a bit for the latter by re-implementing `pselect` in terms of `poll` to
avoid having to implement wasip2 versions of both.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Dave Bakker <github@davebakker.io>
2024-03-27 12:24:10 -07:00
Joel Dice
6593687e25
add wasip2 implementations of more socket APIs (#482)
This adds `wasm32-wasip2` implementations of `shutdown`, `getsockopt`, and
`setsockopt`.  It also extends the existing `ioctl` implementation to handle
both p1 and p2 file descriptors since we can't know until runtime which kind we
have. Once we've moved `wasm32-wasip2` fully to WASI 0.2 and remove the need for
the p1 adapter, we'll be able to switch to separate p1 and p2 `ioctl`
implementations.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Dave Bakker <github@davebakker.io>
2024-03-18 17:54:07 -07:00
Dan Gohman
a6f8713433
Convert preopen initialization to be lazy. (#408)
* Convert preopen initialization to be lazy.

Insteead of eagerly initializing the preopens in a static constructor,
perform preopen initialization the first time it's needed, or before a
close or a renumber which might disrupt the file descriptor space.

And, use a weak symbol with a stub function for use by `close` or
`fd_renumber`, we that they can trigger preopen initialization only
if it's actually needed.

This way, if a program doesn't contain any calls to any function that
needs preopens, it can avoid linking in the preopen initialization code.
And if it contains calls but doesn't execute them at runtime, it can
avoid executing the preopen intiailization code.

A downside here is that this may cause problems for users that call
`__wasi_fd_close` or `__wasi_fd_renumber` directly and close over
overwrite some preopens before libc has a chance to scan them. To
partially address this, this PR does add a declaration for
`__wasilibc_populate_preopens` to <wasi/libc.h> so that users can call
it manually if they need to.

* Fix calling `internal_register_preopened_fd` with the lock held.

Factor out the lock acquisition from the implementation of
`internal_register_preopened_fd` so that we can call it from
`__wasilibc_populate_preopens` with the lock held.
2023-05-03 12:44:21 -07:00
Dan Gohman
6cd1be1f88
Fix logic errors in the zero-inode path. (#352)
I've now tested the zero-inode path on a Wasm engine specially-modified
to have `fd_readdir` set inode numbers to zero. Fix two bugs this turned up:
 - Increment `buffer_processed`, as noticed by @yamt
 - Don't do an `fstatat` on "..", because that references a path outside
   of the directory, which gets a permission-denied error.
2022-12-06 09:16:45 -08:00
Dan Gohman
be1ffd6a9e
If fd_readdir returns a zero inode, call fstatat to get the inode value. (#345)
* If `fd_readdir` returns a zero inode, call `fstatat` to get the inode value.

On some systems, `fd_readdir` may not implement the `d_ino` field and
may set it to zero. When this happens, have wasi-libc call `fstatat` to
get the inode number.

See the discussion in
https://github.com/WebAssembly/wasi-filesystem/issues/65 for details.

* Update the `d_type` field too, in case it changes.
2022-12-01 17:44:22 -08:00
Dan Gohman
099caae3eb Fix missing errno assignments.
In PR #294 I removed a little too much code; we still need to assign to
`errno` in the code in question here.
2022-10-06 14:19:26 -07:00
Andrew Brown
dcd28cf8f6
Fix make THREAD_MODEL=posix (#311)
* Fixes for the THREAD_MODEL=posix build

* Fix expected symbols from previous commit

* Enable `lock` in `random.c` when threads are enabled

This uses the `_REENTRANT` definition to indicate when the `lock` should
be available.

* Disable `aio.h` when compiling for threads

In talking to @sunfishcode about `aio.h`, this functionality is not yet
a primary concern (it was already disabled in the default,
single-threaded mode). Additionally, this change adds expectation lines
for the new symbols/includes added and removed by `pthread.h`.

This change was reached by running:
```console
$ git diff --no-index expected/wasm32-wasi sysroot/share/wasm32-wasi > patch.diff
# replace "sysroot/share" with "expected" in `patch.diff`
$ git apply patch.diff --reject
# manually fix any rejections
```

* Specify the TLS model until LLVM 15 is released

The `-ftls-model` configuration can be removed once https://reviews.llvm.org/D130053 makes its way into an upstream release.

* Rename `__wasi_libc_pthread_self` to `__wasilibc_pthread_self`

The symbol is still undefined, though.

* Add different sets of expected output based on THREAD_MODEL

* Re-add trailing whitespace to `predefined-macros.txt`

@sbc100 wanted to retain the whitespace trailing after certain
predefined macro lines. This change restores that whitespace from
upstream and re-generates the POSIX version using the following command:

```console
$ git diff --no-index expected/wasm32-wasi/posix/predefined-macros.txt sysroot/share/wasm32-wasi/predefined-macros.txt | sed 's/sysroot\/share\/wasm32-wasi/expected\/wasm32-wasi\/posix/' | git apply
```

* Protect `preopens.c` against concurrent access

* Only build thread-capable wasi-libc on latest version of Clang

* Use `thrd_sleep` from MUSL instead of aliasing `nanosleep`

* Define `pthread_setcancelstate` in `THREAD_MODEL=posix` builds

There are other options here (e.g., always define the `pthread_*`
symbols with stubs) but until we discuss that this is an intermediate
working step.

* Define a Wasm global to store `pthread_self`

* Remove `g_needs_dynamic_alloc` global

* Document the state of pthread support

* review: de-duplicate symbols based on #314

* review: only define `__wasilibc_cwd_{un}lock` when needed

* review: add #ifdefs to `__pthread_setcancelstate`

* review: add additional #ifdefs to `pthread_self.c`

* review: put lock definition behind #ifdef _REENTRANT

* review: remove pthread_setcancelstate.c

* review: re-fix indentation

* review: alias __clock_nanosleep in bottom half

* review: remove extra line

Co-authored-by: Sam Clegg <sbc@chromium.org>
2022-08-09 08:08:37 -07:00
Andrew Brown
87c2aa043d
Use MUSL's weak* feature in bottom half (#306)
This change extracts the `weak*`-related parts of #303 as a separate PR.
Note that this is slightly strange in that it uses some top-half MUSL
headers in the bottom-half code, but discussion around this led me to
believe that the advantages of, e.g., `LOCK` made this worthwhile.
Beyond just changing uses of `weak` to `__weak__`, we also MUSL's `weak`
and `weak_alias` macros in a few more places.
2022-07-26 14:15:12 -07:00
Dan Gohman
60f221a400
Delete several blocks of unused code. (#294)
* Delete several blocks of unused code.

Delete several pieces of code from libc-bottom-half/cloudlibc that aren't in
use on wasi-libc.

* Delete more of `_CLOCK_PROCESS_CPUTIME_ID` or `_CLOCK_THREAD_CPUTIME_ID`.
2022-07-20 15:46:06 -07:00
Dan Gohman
30094b6ed0 Fix gettimeofday to correctly handle a null argument.
`gettimeofday` is defined to do nothing if passed NULL.
2022-06-01 11:05:04 -07:00
Dan Gohman
225b285a61 Fix utimensat to avoid passing uninitialized values into WASI calls.
Previously, utimensat would leave the mtim and/or atim timestamps
uninitialized when the `MTIM_NOW` or `ATIM_NOW` were in use, because
that means the respective timestamps are not used.

However, clang now automatically adds `noundef` to the arguments in
functions like `__wasi_path_filestat_set_times`, and there are cases
where simplifycfg can see paths where the uninitialized values are
passed to those `noundef` arguments.

To fix this, change the utimens code to zero out the timestamps when
they aren't in use, to avoid passing uninitialized arguments.
2022-05-24 15:17:44 -07:00
Dan Gohman
b9b64a695a 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.
2021-03-23 10:34:46 -07:00
Alex Crichton
2b7e73ae7a
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.

* Update submodule to point to WebAssembly
2021-02-23 12:18:56 -06:00
Dan Gohman
f2e779e5f1 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.

Fixes #42.
2021-02-05 06:41:53 -08:00
Dan Gohman
cab0ec601e 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.
2021-02-04 21:22:15 -08:00
Dan Gohman
614d783e92
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.

[new-style command support]: https://reviews.llvm.org/D81689

Co-authored-by: Dan Gohman <sunfish@mozilla.com>
2020-10-03 14:18:39 -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
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
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
Dan Gohman
870a25121b
Remove unused cloudlibc headers. (#170) 2020-02-21 11:40:23 -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
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
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
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
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
Dan Gohman
1db93d1169 Switch from the cloudlibc inet_pton etc. to the musl versions.
These functions aren't specific to the underlying system call interface,
so they don't need to be in the "bottom half".

This also fixes src/functional/inet_pton.c and
src/regression/inet_pton-empty-last-field.c in musl's libc-test.
2019-05-09 09:10:57 -07:00
Dan Gohman
382b944a2e Rename __wasilibc_rmfileat to __wasilibc_unlinkat.
The POSIX terminology is that directories are "removed" while files
are "unlinked".
2019-05-07 15:04:17 -07:00
Dan Gohman
cf366c06d1 Optimize lseek in the tell case.
`lseek(x, 0, SEEK_CUR)` has no effect other than to return the current
file offset. The patch here uses a macro with `__builtin_constant_p` to
recognize this case and rewrite it to a library call that uses `fd_tell`
rather than `fd_seek`, so that programs that don't need actual seeking
don't end up importing `fd_seek`.

This is also the first usage of `__wasi_fd_tell` in WASI libc, so this
adds it to undefined-symbols.txt.
2019-04-30 16:19:05 -07:00
Dan Gohman
1cbedc6d7e Implement FD_SET, FD_CLR, etc.
Previously, FD_SET and friends were missing their actual definitions.
This provides definitions, entirely within the system headers in a
way that doesn't need instantiated out-of-line definitions.
2019-04-23 15:01:23 -07:00
Dan Gohman
538937e2c8 Remove capsicum.h; WASI libc doesn't support that API, even internally. 2019-04-23 15:01:05 -07:00
Dan Gohman
1cc98f27f5 Update to cloudlibc 8835639f27fc42d32096d59d294a0bbb857dc368.
There's no functional change here; the only change is the patch I
submitted upstream, so we can remove some local changes.
2019-04-15 09:49:02 -07:00
Dan Gohman
7ba6adfc61 Fix miscellaneous lint warnings. 2019-04-15 09:47:16 -07:00
Dan Gohman
320054e84f WASI libc prototype implementation.
This incoporates pieces from musl-libc, cloudlibc, cloudabi, libpreopen,
and dlmalloc, as well as a significant amount of new code.
2019-03-27 07:59:55 -07:00