Commit Graph

358 Commits

Author SHA1 Message Date
YAMAMOTO Takashi
ebd3240030
wasi_thread_start: add a comment (#371) 2022-12-23 07:14:21 -07:00
Dan Gohman
b8aa39947d
Use ENOENT rather than ENOTCAPABLE for missing preopens. (#370)
When a user calls `open` with a path that does not have a corresponding
preopen, set errno to `ENOENT` rather than `ENOTCAPABLE`. This
conceptually represents an attempt to open a path which has not been
provided within the sandbox, so it's more accurately represented as
"not present" rather than "insufficient capabilities".
2022-12-21 15:47:53 -08:00
YAMAMOTO Takashi
b36b752bd7
Disable pthread_exit for now (#366)
The current wasi-threads has no thread-exit functionality.
Thus it isn't straightforward to implement pthread_exit
without leaking thread context. This commit simply disables
pthread_exit for now.

Also, instead of abusing `wasi_proc_exit` for thread exit,
make `wasi_thread_start` return.

Note: `wasi_proc_exit` is supposed to terminate all threads
in the "process", not only the calling thread.

Note: Depending on the conclusion of the discussion about
`wasi_thread_exit`, we might revisit this change later.

References:
https://github.com/WebAssembly/wasi-threads/issues/7
https://github.com/WebAssembly/wasi-threads/pull/17
2022-12-20 17:24:09 -08:00
Marcin Kolny
957c7113c3
threads: Retrieve default stack size from __heap_base/__data_end (#350)
When compiling with `-z stack-size` flag, only the main thread's stack
size is set to the specified value and other threads use musl's default value.
That's inconsistent with LLD's `-Wl,-stack_size`.

I think we can make it similar to MUSL's behavior, where thread's stack
size can be set via `PT_GNU_STACK` program header (via `-Wl,-z,stack-size`
flag).

Configuring stack size through `pthread_attr_t` still work as expected and
overrides the defaults ([pthread_create.c](be1ffd6a9e/libc-top-half/musl/src/thread/pthread_create.c (L362)))
default settings.
2022-12-19 04:18:19 -08:00
YAMAMOTO Takashi
dfad6fecc9
Reduce over-allocation of stack (#365)
* Disable stack guard
* Stop rounding up stack size to PAGE_SIZE
2022-12-19 04:16:54 -08:00
Petr Penzin
fb9c9223ca
Add a separate install target for threaded libc (#331)
Produce a different sysroot directory for threaded target. Restructure
`expected` directory to correspond to the target.
2022-12-13 17:57:57 -08:00
YAMAMOTO Takashi
f687048b70
__wait.c: fix a timeout (#361)
Note: The typical symptom of this bug is a busy waiting on a lock.

Note: 0 means immediate timeout. a negative value means no timeout.
2022-12-13 12:07:33 -08:00
YAMAMOTO Takashi
defd63129d
Enable flockfile and friends (#362) 2022-12-13 09:56:45 -08:00
Andrew Brown
dbcf819f4a
threads: enable access to pthread_barrier_* functions (#358)
In building some `libc-test` tests, I found these functions were not
compiled in. This change adds `pthread_barrier_init`,
`pthread_barrier_wait`, and `pthread_barrier_destroy` to the
`THREAD_MODEL=posix` build. As has been done with previous pthreads PRs,
this PR skips any inter-process locking by removing any calls to
`__vm_lock` and friends. If in the future WASI gains the "process"
concept, then these locations (and the pre-existing ones) will need to
be modified.
2022-12-13 09:19:32 -08:00
YAMAMOTO Takashi
a6e91a7b8c
Enable pthread_detach (#359) 2022-12-09 09:58:46 -08:00
Andrew Brown
0aa7a988f4
threads: enable access to pthread_attr_get functions (#357)
The pthreads API exposes functions for querying the attributes of a
thread. This change allows these functions to be compiled in the
`THREAD_MODEL=posix` build. Some functions are skipped (and documented);
they can be added if/when needed. This change is motivated by a
`libc-test` test that uses these functions.
2022-12-08 14:55:42 -08:00
Dan Gohman
7250bd4165
Don't define _POSIX_THREADS unless threads are enabled. (#356)
* Don't define `_POSIX_THREADS` unless threads are enabled.

Fixes #355.

* Remove `_POSIX_THREADS` from predefined-macros.txt.
2022-12-07 13:54:19 -08:00
YAMAMOTO Takashi
f32e25870f
wasi_thread_start: remove a useless cast (#354) 2022-12-07 09:31:41 -08:00
YAMAMOTO Takashi
0b3b1bb9f1
__wasi_thread_spawn: stop truncating the return value (#353)
as __wasi_errno_t is uint16_t, with the current coding,
__pthread_create will never see negative return values from
wasi:thread_spawn.
eg. (int)(uint16_t)-1 == 65535.
2022-12-07 09:18:28 -08:00
Mike Hommey
3a261b0380
Adjust Makefile for LLVM trunk (16) as of 2022-11-08 (#344)
1e4e2433bc
enabled sign-ext and mutable-globals by default, which adds
corresponding __wasm_-prefixed #defines.

9e956995db
changed the definition of __GNUC_VA_LIST to match that of GCC headers,
leaving it without a value.
2022-12-07 08:27:06 -08:00
Yuta Saito
8b7148f69a
Add -fstack-protector support to wasi-libc (#351)
Inlcude `__stack_chk_fail.c` and initialize `__stack_chk_guard` in ctor.

```
$ cat main.c
char input[] = "0123456789012345";
int main(void) {
    char buf[8];

    for (char *sp = input, *dp = buf; *sp != '\0'; sp++, dp++) {
        *dp = *sp;
    }
    return 0;
}

$ clang main.c -fstack-protector
$ wasmtime ./a.out
Error: failed to run main module `./a.out`

Caused by:
    0: failed to invoke command default
    1: wasm trap: wasm `unreachable` instruction executed
       wasm backtrace:
           0:  0x258 - <unknown>!__stack_chk_fail
           1:  0x21e - <unknown>!__original_main
           2:   0xca - <unknown>!_start
```
2022-12-06 09:19:49 -08: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
Andrew Brown
8098d8621e
test: run a subset of tests using libc-test (#346)
* test: run a subset of tests from `libc-test`

This change introduces a `test` directory that retrieves the `libc-test`
suite, compiles a subset of the tests using `wasi-libc`, and runs them
with Wasmtime.

* ci: run tests during CI

This change includes some fixups to the filesystem to place Clang's
runtime library for `wasm32-wasi` in the right location. Note that this
CI action is limited to a single OS--Linux.
2022-12-02 18:27:42 -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
Marcin Kolny
c718ee138b
Enable a few more pthread* files (pthread_key*, pthread_once) (#348)
* threads: enable TSD functions

* threads: Enable pthread_once
2022-11-30 14:16:56 -08:00
Dan Gohman
697d5f8425
Install libtinfo5 on ubuntu. (#349)
We download LLVM releases built for Ubuntu 18 because LLVM doesn't
always have builds for different versions, but the builds we use
depend on libtinfo5 which isn't installed on Ubuntu 20 by default.
So install it.
2022-11-30 11:17:08 -08:00
Dan Gohman
2bb5abedbd
Define an __errno_location function. (#347)
This function returns the address of `errno`, which makes it easier to
access from non-C languages since `errno` is a thread-local variable
which requires a special ABI.
2022-11-28 13:50:04 -08:00
韩朴宇
a00bf321ee
threads: implement init of TLS and stack pointer (#342)
* threads: implement init of TLS and stack pointer

* fix: rename wasi_snapshot_preview2_thread_spawn to wasi_thread_spawn

Signed-off-by: Harald Hoyer <harald@profian.com>

* fix: change signature of wasi_thread_start

Signed-off-by: Harald Hoyer <harald@profian.com>

* fix: pthread_exit for WASI

Can't use `exit()` because it is too high level.
Have to unlock the thread list.

Signed-off-by: Harald Hoyer <harald@profian.com>

* fix: initialize struct pthread for the main thread

Signed-off-by: Harald Hoyer <harald@profian.com>

* fix: store the aligned stack minus `struct start_args`

Signed-off-by: Harald Hoyer <harald@profian.com>

Signed-off-by: Harald Hoyer <harald@profian.com>
Co-authored-by: Harald Hoyer <harald@profian.com>
2022-11-10 14:12:53 -08:00
Dan Gohman
b99173e177
Support threads in the new crt1-command.c ctor check. (#339)
Use an atomic compare-and-swap for checking whether constructors have
been run, when threads are enabled.
2022-11-08 13:37:44 -08:00
Dan Gohman
a7af7c064b Port emmalloc to wasi-libc.
- Avoid using Emscripten-specific functions
 - Avoid using a constructor.
 - Add support for allocating memory at `__heap_base`.
 - Adjust the max-align value.
 - Disable functions that wasi-libc doesn't currently publish.
 - Add `__libc_` aliases.
2022-11-01 12:54:26 -07:00
Dan Gohman
f8eaf0028c Beginning porting Emscripten's emmalloc to wasi-libc.
emmalloc.c is derived from Emscripten at this path here:

https://github.com/emscripten-core/emscripten/blob/main/system/lib/emmalloc.c
2022-11-01 12:54:26 -07:00
Dan Gohman
21d93b927d
Add a check to command modules to ensure that they're only started once. (#329)
* Add a check to command modules to ensure that they're only started once.

Wasm command modules should only be called once per instance, because
the programming model doesn't leave linear memory in a reusable state
when the program exits. As use cases arise for loading wasm modules in
environments that want to treat them like reactors, add a safety check
to ensure that command modules are used according to their
expectations.
2022-10-13 17:58:14 -07:00
Marcin Kolny
9d2f5a8242
threads: enable pthread_join (#336) 2022-10-10 15:25:56 -07:00
Marcin Kolny
d02cec69e7
threads: Enable __lock file for posix thread model (#337) 2022-10-10 15:19:25 -07:00
Dan Gohman
1710f3c010 Avoid depending on max_align_t in <__struct_sockaddr.h>.
`max_align_t` is not declared in C99 mode, so use an aligned attribute
and `__BIGGEST_ALIGNMENT__` instead.

Fixes WebAssembly/wasi-sdk#111.
2022-10-10 11:04:26 -07: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
Dan Gohman
a02298043f Add braces to if statements whose bodies have multiple statements.
Add braces to `if` statements so that the whole intended body is covered
by the conditional.
2022-10-06 11:58:27 -07:00
Andrew Brown
241060c342
threads: implement pthread_create (#325)
* threads: implement `pthread_create`

As described in the [`wasi-threads`] proposal, this change implements
`pthread_create` using the new `wasi_thread_spawn(void *arg)` API. As
described there, `wasi-libc` exports the thread entry point with the
expected name, `wasi_thread_start`, and then unwraps the passed argument
`struct` to invoke the user function with the user argument `struct`.

[`wasi-threads`]: https://github.com/WebAssembly/wasi-threads/pull/5

Previously, the TID was only passed to the child thread entry point; the
parent thread was forced to wait until the child thread set the TID in
the pthread structure. With this change, the TID will be passed not only
to the child thread but also returned to the parent thread, so that
either side can make progress. The `i32.store` becomes an
`i32.atomic.store` to avoid concurrent writes.
2022-10-04 07:21:18 -07:00
Dan Gohman
f2a618feb2 Use quotes instead of angle brackets. 2022-09-29 13:15:32 -07:00
Dan Gohman
907d10fecb Don't run static constructors on arbitrary user exports.
Previously, "new-style commmands" considered every user-defined
export to be a potential command entrypoint, so wasi-libc and wasm-ld
cooperated to run the user's static constructors on each entrypoint.

This form of new-style command turned out not to be useful, and it
interferes with some use cases, so disable it.

This is done by making an explicit call to `__wasm_call_ctors`, which
tells wasm-ld that it shouldn't synthesize any calls to
`__wasm_call_ctors` on its own.
2022-09-29 13:15:32 -07:00
Marcin Kolny
05b3b876e6
make __get_tp() a static function (#327)
I'm not sure if the function really has to be exported. If so, we should
probably move it to a separate compilation unit, otherwise it will be defined
multiple times (e.g. in `strerror.o` and `__lctrans.o`) causing linker errors.
However, I don't see a reason (at least for now) to export this function,
therefore making it static in this PR.
2022-09-19 13:53:39 -07:00
Marcin Kolny
c40403f9b0
threads: implement support for conditional variables (#323)
The implementation is not as efficient as for native Linux platform due
to lack of FUTEX_REQUEUE-like system call in WASI.

For now we wake all the waiters which is inefficient; if that becomes
a bottleneck, I suggest we'll revisit the implementation.
2022-09-06 14:21:06 -07:00
Marcin Kolny
27ba71f95e
Implement support for pthread_rwlock (#321)
* threads: implement support for pthread_rwlockattr

* threads: implement support for pthread_rwlock
2022-09-06 10:14:07 -07:00
Marcin Kolny
3d00b969f2
threads: implement support for pthread_condattr (#320) 2022-09-06 10:13:21 -07:00
Andrew Brown
2057ce9262
threads: implement support for unnamed semaphores (#316)
[POSIX semaphores] come in two forms: named and unnamed. Roughly, named
semaphores use files to implement locking across processes; unnamed
semaphores use a shared memory region to implement locking across
threads in the same process. Since WASI currently has no process concept
(and it is relatively unclear how to map the WASI files as shared
memory), only the unnamed semaphores are supported by this changed. This
means that `sem_open`, `sem_close`, and `sem_unlink` will not available
to programs compiled with a threads-enabled `wasi-libc`.

[POSIX semaphores]: https://man7.org/linux/man-pages/man7/sem_overview.7.html
2022-08-22 14:22:09 -07:00
Andrew Brown
33c3753caf
threads: implement support for pthread mutexes (#315)
This change adds pthread's mutex support to the `THREAD_MODEL=posix`
build of wasi-libc. Some less-common features are unsupported and
documented here:
- mutex robust lists are disabled and their use will return a runtime
  error; currently WASI does not support the concept of multiple
  processes so maintaining robust mutexes across processes does not yet
  make sense in wasi-libc
- timed locks with priority inheritance (PI) are disabled and will act
  as any other mutex; this feature is related to task priorities which
  is not yet relevant in the WASI ecosystem (see [priority-inheritance
  futexes](https://man7.org/linux/man-pages/man2/futex.2.html))
- thread cancellation is ignored; this feature is difficult to support
  and @sunfishcode would likely want to discuss this before adding it at
  some later time.
2022-08-22 08:39:44 -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
Bernhard M. Wiedemann
69031b6370
Sort lists of input files (#313)
For reproducible .a files in spite of non-deterministic
filesystem readdir order

Without this patch, find returned files in filesystem order
and llvm-ar used that order to create .a files.
2022-08-08 21:54:37 -07:00
Andrew Brown
294b23c3fd
Remove duplicates from defined-symbols.txt (#314)
In #311, it became apparent that duplicate symbol definitions were
becoming unwieldy. This change merges all duplicates using `uniq`.
2022-08-01 15:52:16 -07:00
Andrew Brown
6575e7d848
Move weak attribute to front (#310)
This is a follow-up based on @sbc100's comments in #306.
2022-07-26 16:12:17 -07:00
Andrew Brown
613e154eb2 Fix typo 2022-07-26 15:59:35 -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
msirringhaus
7a21011e98
Update README and add CI-tests for minimal supported LLVM-version (10) (#302)
Fixes: #301
2022-07-13 08:49:17 -07:00
Dan Gohman
e066a8b9d9
Add a getpagesize function. (#300)
* Add a `getpagesize` function.

This adds a `getpagesize` function. This interface is deprecated in POSIX,
but it's sufficiently widely used and not problematic in practice.

* Use musl's `getpagesize`.

* Enable the `getpagesize` declaration in unistd.h.
2022-07-11 08:27:39 -07:00