Commit Graph

151 Commits

Author SHA1 Message Date
R
a05277a680
Implement a stub pthreads library for THREAD_MODEL=single (#518)
~~This patch series first starts with a number of commits stubbing out
functions in the existing `THREAD_model=posix` code. According to "The
Open Group Base Specifications Issue 7, 2018 edition", there are a
number of mandatory functions which have not been provided. There are
also some optional functions that have been partially provided in a
not-useful way (e.g. get but no set function). For these, I have chosen
to clean them up and remove the get functions for consistency.~~ EDIT:
These have been split off into separate PRs and merged.

The remainder of the patches then build up a stub implementation of
pthreads for `THREAD_MODEL=single`. I have done my best to try to make
sure that all functions are as conforming as possible (under the
assumption that another thread cannot ever be launched). This means that
objects such as mutexes and rwlocks actually do update their state and
will correctly fail when locks cannot be acquired.

When an inevitable deadlock occurs, I have chosen to return EDEADLK when
it has been explicitly listed as a permissible return value, and to
invoke `__builtin_trap` otherwise.

I have tested this by rebuilding libc++ with threads enabled and then
smoke-testing Clang/LLVM-on-WASI to make sure that it can compile a
simple program. I have not run any more-extensive conformance testing.

Fixes #501
2024-10-10 09:27:48 -07:00
Yuta Saito
5ed3ec5701
Initial FTS support (#522)
Close https://github.com/WebAssembly/wasi-libc/issues/520

Add FTS implementation derived from musl-fts with a few modifications.
The compiled fts.o is included in the libc.a archive, and the fts.h
header is installed in the sysroot (`include/fts.h`).

* fts/musl-fts: Add a copy of the musl-fts sources with modifications.
* fts/patches: A set of patches to apply to the musl-fts sources.
* Upstream pull request: https://github.com/void-linux/musl-fts/pull/14
* fts/update-musl-fts.sh: A script to update the musl-fts sources with
the patches applied.
* fts/config.h: A configuration header included by the musl-fts sources.
* test/smoke: Add a test suite for wasi-libc specific features that
libc-test does not cover.
2024-09-25 16:16:34 -07:00
Henri Nurmi
1b19fc65ad
getaddrinfo: improve the service/port resolution (#524)
Hello,

While experimenting with the `wasm32-wasip2` target and CPython, I
discovered an issue with the `getaddrinfo()` implementation: it fails to
resolve the provided service into a port number, causing `sin_port` to
always be set to 0. This issue leads to failures in network-related
functions that rely on `getaddrinfo()`, such as Python's `urllib3`
library, which passes the result directly to `connect()`. This results
in connection attempts using a port value of 0, which naturally fails.

### Minimal example to reproduce the problem
```c
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

int main(void) {
    struct addrinfo *res = NULL;
    getaddrinfo("google.com", "443", NULL, &res);

    for (struct addrinfo *i = res; i != NULL; i = i->ai_next) {
        char str[INET6_ADDRSTRLEN];
        if (i->ai_addr->sa_family == AF_INET) {
            struct sockaddr_in *p = (struct sockaddr_in *)i->ai_addr;
            int port = ntohs(p->sin_port);
            printf("%s: %i\n", inet_ntop(AF_INET, &p->sin_addr, str, sizeof(str)), port);
        } else if (i->ai_addr->sa_family == AF_INET6) {
            struct sockaddr_in6 *p = (struct sockaddr_in6 *)i->ai_addr;
            int port = ntohs(p->sin6_port);
            printf("%s: %i\n", inet_ntop(AF_INET6, &p->sin6_addr, str, sizeof(str)), port);
        }
    }

    return 0;
}
```
```
$ /opt/wasi-sdk/bin/clang -target wasm32-wasip2 -o foo foo.c
$ wasmtime run -S allow-ip-name-lookup=y foo
216.58.211.238: 0
2a00:1450:4026:808::200e: 0
```
Expected output:
```
216.58.211.238: 443
2a00:1450:4026:808::200e: 443
```
### Root Cause

The root cause is that `getaddrinfo()` does not correctly translate the
provided service into a port number. As described in the `getaddrinfo()`
man [page](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html),
the function should:

> service sets the port in each returned address structure. If
this argument is a service name (see
[services(5)](https://man7.org/linux/man-pages/man5/services.5.html)),
it is
translated to the corresponding port number. This argument can
also be specified as a decimal number, which is simply converted
to binary. If service is NULL, then the port number of the
returned socket addresses will be left uninitialized.

### Proposed Fix

This pull request addresses the issue by implementing the following
behavior for `getaddrinfo()`:

* If the service is `NULL`, the port number in the returned socket
addresses remains uninitialized.
* The value is converted to an integer and validated if the service is
numeric.

The PR does not currently add support for translating named services
into port numbers because `getservbyname()` has not been implemented. In
cases where a named service is provided, the `EAI_NONAME` error code is
returned.
2024-08-27 17:39:34 -07:00
R
230d4be6c5
Improve some pthreads stub functions, batch 1 (#525)
This is the next part of breaking up #518 into smaller PRs.

This is the rest of the commits which change the existing
`THREAD_MODEL=posix` functionality. It:

* _removes_ some functions which are optional and which were already
nonfunctional.
* (Continues to) establish a precedent of trying to remain as compatible
with Open Group specifications as possible, even when there are major
differences in WASI capabilities (i.e. thread cancellation)

Compared to the RFC PR, the `pthread_atfork` stub has been dropped as it
is now officially obsolete as of the very recent Issue 8 of the
specifications.
2024-08-06 11:04:02 -07:00
R
5d3c5e918c
Improve some pthreads stub functions, batch 0 (#519)
This is one part of breaking up #518 into smaller PRs. This should be
(IMO) the least-controversial batch of commits
2024-08-02 09:56:28 -07:00
Joel Dice
acd0a6e352
include pthread.h for all targets (#504)
Per #501, this restores the pre-WASI-SDK-22 behavior of including a copy of
pthread.h for all targets -- not just the `*-threads` targets. This helps
minimize the number of preprocessor guards required to port existing POSIX
software to WASI. It also make it easier for projects using WASI-SDK versions
earlier than 22 to upgrade.

Note that this includes the pthread.h header, but no stub function definitions
to link against. We should probably provide the latter as well, but I'll leave
that for a separate PR.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-05-29 09:22:38 -05:00
Catherine
7528b13170
Extend wasi-emulated-mman with mprotect. (#500)
This implementation never changes actual memory protection, but it does
verify that the address range and flags are valid. The direct motivation
is fixing a linker error where LLVM links to `mprotect` in dead code.
2024-05-22 10:09:22 -07:00
Alex Crichton
153f6321bb
Ignore the __tls_base undefined symbol (#498)
Currently if `-g` is specified with build flags then wasi-libc fails to
build. I'm not certain why it shows up in object files, but it appears
to be a spurious error so this commit adds it to the list of variables
to ignore.
2024-05-20 16:59:53 -05:00
Yuta Saito
2f088a99d8
Update _POSIX_THREAD_XX macro definitions (#494)
This is a follow-up to https://github.com/WebAssembly/wasi-libc/pull/356
2024-05-06 16:56:34 -07:00
Joel Dice
9e8c542319
add __wasilibc_reset_preopens (#489)
This resets the preopens table to an uninitialized state, forcing it to be
reinitialized next time it is needed.  This is useful when pre-initializing
using e.g. `wizer` or `component-init`.  Such tools are capable of taking a
snapshot of a running application which may later be resumed on an unrelated
runtime (which may have its own, unrelated preopens).

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-04-11 15:27:43 -07:00
Joel Dice
d038294899
implement getsockname, getpeername, and getaddrinfo (#488)
This also includes stubs for `gethostbyname`, `gethostbyaddr`, etc. which were
necessary to get CPython to build.  I believe it will be possible to implement
them all properly at some point, but don't have the bandwidth at the moment.

Finally, this includes a few fixes for issues I missed in earlier PRs that
surfaced when running the CPython `asyncio` test suite.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Dave Bakker <github@davebakker.io>
2024-04-02 16:54:41 -07:00
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
Joel Dice
f493dc284d
implement basic TCP/UDP server support (#481)
This adds `bind`, `listen`, and `accept` implementations based on
`wasi-sockets` for the `wasm32-wasip2` target.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Dave Bakker <github@davebakker.io>
2024-03-13 14:50:55 -07:00
Joel Dice
684f155664
implement basic TCP/UDP client support (#477)
* implement basic TCP/UDP client support

This implements `socket`, `connect`, `recv`, `send`, etc. in terms of
`wasi-sockets` for the `wasm32-wasip2` target.

I've introduced a new public header file: `__wasi_snapshot.h`, which will define
a preprocessor symbol `__wasilibc_use_wasip2` if using the `wasm32-wasip2`
version of the header, in which case we provide features only available for that
target.

Co-authored-by: Dave Bakker <github@davebakker.io>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* fix grammar in __wasi_snapshot.h comment

Co-authored-by: Dan Gohman <dev@sunfishcode.online>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Dave Bakker <github@davebakker.io>
Co-authored-by: Dan Gohman <dev@sunfishcode.online>
2024-03-13 10:59:27 -07:00
Milek7
c8ef60ad9b
Add support for pthread_getattr_np (#470) 2024-03-11 08:48:13 -07:00
Alex Crichton
c9c7d0616e
Start renaming preview1 to p1 and preview2 to p2 (#478)
* Start renaming preview1 to p1 and preview2 to p2

This is an initial start at renaming the "preview" terminology in WASI
targets to "pX". For example the `wasm32-wasi` target should transition
to `wasm32-wasip1`, `wasm32-wasi-preview2` should transition to
`wasm32-wasip2`, and `wasm32-wasi-threads` should transition to
`wasm32-wasip1-threads`. This commit applies a few renames in the
`Makefile` such as:

* `WASI_SNAPSHOT` is now either "p1" or "p2"
* The default p2 target triple is now `wasm32-wasip2` instead of
  `wasm32-wasi-preview2` (in the hopes that it's early enough to change
  the default).
* Bindings for WASIp2 were renamed from "preview2" terminology to "wasip2".
* The expected-defines files are renamed and the logic of which
  expectation was used has been updated slightly.

With this commit the intention is that non-preview2 defaults do not
change. For example the default build still produces a `wasm32-wasi`
sysroot. If `TARGET_TRIPLE=wasm32-wasip1` is passed, however, then that
sysroot is produced instead. Similarly a `THREAD_MODEL=posix` build
produces a `wasm32-wasi-threads` sysroot target but you can now also
pass `TARGET_TRIPLE=wasm32-wasip1-threads` to rename the sysroot.

My hope is to integrate this into the wasi-sdk repository and build a
dual sysroot for these new targets for a release or two so both are
supported and then in the future the defaults can be switched away from
`wasm32-wasi` to `wasm32-wasip1` as built-by-default.

* Update builds in CI

* Update test workflow

* Fix test for wasm32-wasip1-threads

* Make github actions rules a bit more readable
2024-03-04 15:57:34 -08:00
Joel Dice
09683b3623
add descriptor table for mapping fds to handles (#464)
* add descriptor table for mapping fds to handles

This introduces `descriptor_table.h` and `descriptor_table.c`, providing a
global hashtable for tracking `wasi-libc`-managed file descriptors.

WASI Preview 2 has no notion of file descriptors and instead uses unforgeable
resource handles.  Moreover, there's not necessarily a one-to-one correspondence
between POSIX file descriptors and resource handles (e.g. a TCP connection may
require separate handles for reading, writing, and polling the same connection).
We use this table to map each POSIX descriptor to a set of one or more handles
and any extra state which libc needs to track.

Note that we've added `descriptor_table.h` to the
libc-bottom-half/headers/public/wasi directory, making it part of the public
API.  The intention is to give applications access to the mapping, enabling them
to convert descriptors to handles and vice-versa should they need to
interoperate with both libc and WASI directly.

Co-authored-by: Dave Bakker <github@davebakker.io>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* add dummy fields to otherwise empty structs

The C standard doesn't allow empty structs.  Clang doesn't currently complain,
but we might as well stick to the spec in case it becomes more strict in the
future.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* move descriptor_table.h to headers/private

We're not yet ready to commit to making this API public, so we'll make it
private for now.

I've also expanded a comment in descriptor_table.c to explain the current ABI
for resource handles.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* re-run clang-format to fix indentation

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Co-authored-by: Dave Bakker <github@davebakker.io>
2024-02-27 12:57:54 -08:00
Joel Dice
0fe51d2582
add preview2_component_type.o to libc.a and libc.so (#472)
This file adds a custom section to each core module linked with wasi-libc.  That
custom section contains component type information needed by e.g. `wasm-tools
component new` to generate a component from the module.  It will be required
once we start using any part of WASI 0.2.0 directly (vs. via a Preview 1
adapter).  In addition, it allows developers to `#include <wasi/preview2.h>` in
their code and make use of those APIs directly even if wasi-libc is not using
them yet.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-02-22 14:09:50 -08:00
Joel Dice
55df1f54e2
Update bindings and dependencies to WASI 0.2.0 (#471)
* make the Makefiles a bit more robust

- Escape "." character in `sed` regex
- Ensure that %.wasm target fails cleanly (i.e. without generating the target file) if `wasm-tools` fails

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* split `component new` rule out of link rule for Preview 2

We now explicitly distinquish between core module files (%.core.wasm) and
component files (%.wasm), which helps avoid the trickery in my previous commit.

In order to test this properly, I needed to update the Wasmtime URL to point to
v17.0.0 instead of dev (which we needed to do anyway), and that in turn required
updating the bindings to use the final WASI 0.2.0 release.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-02-21 17:57:58 -08:00
Joel Dice
212296e4fa
add realpath.c to LIBC_TOP_HALF_MUSL_SOURCES (#473)
* provide a `realpath` stub

In https://github.com/WebAssembly/wasi-libc/pull/463, I added stubs for
`statvfs`, `chmod`, etc. but forgot to add one for `realpath`, which is also
required by `libc++`'s `<filesystem>` implementation.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* remove `realpath` stub and use musl's version instead

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-02-13 09:54:59 -08:00
Joel Dice
cc62fa82c2
add stubs for statvfs, chmod, etc. (#463)
Per https://github.com/WebAssembly/wasi-sdk/issues/373, LLVM's libc++ no longer
allows us to enable `<fstream>` and `<filesystem>` separately -- it's both or
neither.  Consequently, we either need to patch libc++ to not use `statvfs`,
`chmod`, etc. or add stub functions for those features to `wasi-libc`.  Since
we're planning to eventually support those features with WASI Preview 2 and
beyond, it makes sense to do the latter.

Note that since libc++ uses `DT_SOCK`, I've added a definition for it -- even
though WASI Preview 1 does not define it.  No Preview 1 file will ever have that
type, so code that handles that type will never be reached, but defining it
allows us to avoid WASI-specific patches to libc++.

Related to `DT_SOCK`, I had to change the `S_IFIFO` value so it does not
conflict with `S_IFSOCK`, thereby avoiding ambiguity in `__wasilibc_iftodt`.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-01-11 12:16:59 -08:00
Joel Dice
47b9db6d15
add WASI Preview 2 bindings (#460)
* add WASI Preview 2 bindings

This adds C bindings generated from the `wasi:cli/imports@0.2.0-rc-2023-12-05`
world, plus a makefile target to regenerate them from the WIT source files.

We'll use these bindings to call Preview 2 host functions when building for the
`wasm32-wasi-preview2` target.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* update to pre-release of `wit-bindgen` 0.17.0

This includes https://github.com/bytecodealliance/wit-bindgen/pull/804 (fix
broken indentation in generated code) and
https://github.com/bytecodealliance/wit-bindgen/pull/805 (support overriding
world name and adding a suffix to the component type custom section).

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* test all targets; update preview2 expected output files

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* build for `wasm32-wasi-threads` before testing it

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* move generated bindings per review feedback

Since these files aren't part of cloudlibc, no reason to put them under the
cloudlibc directory.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* move preview2.h to wasi directory

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2024-01-11 11:53:15 -08:00
Joel Dice
5a693184e9
add wasm32-wasi-preview2 target (#457)
Currently, this is identical to the `wasm32-wasi` in all but name.  See #449 for
the next step, which is to incrementally add Preview 2 features,
e.g. `wasi-sockets`.  Per the discussion in that PR, I've split the
`wasi-sysroot/include` directory into per-target directories.  Eventually, we'll
want to build a separate sysroot for each target, but there's currently
uncertainty about how to configure the default sysroot for e.g. clang, so we're
not tackling that yet.

See also #447 for further details.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2023-12-22 14:03:07 -08:00
Joel Dice
b85d65528d
add stubs for dlopen, dlsym, etc. (#443)
* add stubs for dlopen, dlsym, etc.

This adds weak exports for the POSIX `dlopen`, `dlsym`, `dlclose`, and `dlerror`
functions, allowing code which uses those features to compile.  The
implementations are stubs which always fail since there is currently no official
standard for runtime dynamic linking.

Since the symbols are weak, they can be overriden with useful, runtime-specific
implementations, e.g. based on host functions or statically-generated tables
(see https://github.com/dicej/component-linking-demo for an example of the
latter).

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* move `dlopen` stubs out of libc and into libdl

Per review feedback, it's easier to simply replace libdl.so with a working
implementation at runtime than it is to override a handful of symbols in libc.

Note that I've both added libdl.so and replaced the empty libdl.a we were
previously creating with one that contains the stubs.  I'm thinking we might as
well be consistent about what symbols the .so and the .a contain.  Otherwise,
e.g. the CPython build gets confused when the dlfcn.h says `dlopen` etc. exist
but libdl.a is empty.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* customize dlfcn.h for WASI

For WASI, we use flag values which match MacOS rather than musl.  This gives
`RTLD_LOCAL` a non-zero value, avoiding ambiguity and allowing us to defer the
decision of whether `RTLD_LOCAL` or `RTLD_GLOBAL` should be the default when
neither is specified.

We also avoid declaring `dladdr`, `dlinfo`, and friends on WASI since they are
neither supported nor stubbed at this time.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* use musl's RTLD_* flags except for RTLD_LOCAL

This minimizes the divergence from upstream while still giving us the
flexibility to choose a default value later.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* use `NULL` instead of `0` for null pointers

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2023-11-14 16:34:57 -08:00
Joel Dice
d4dae89648
add shared library support (#429)
* add shared library support

This adds support for building WASI shared libraries per
https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md.

For the time being, the goal is to allow "pseudo-dynamic" linking using the
Component Model per
https://github.com/WebAssembly/component-model/blob/main/design/mvp/examples/SharedEverythingDynamicLinking.md.
This requires all libraries to be available when the component is created, but
still allows runtime symbol resolution via `dlopen`/`dlsym` backed by a static
lookup table.  This is sufficient to support Python native extensions, for
example.  A complete demo using `wit-component` is available at
https://github.com/dicej/component-linking-demo.

This commit adds support for building `libc.so`, `libc++.so`, and `libc++abi.so`
alongside their static counterparts.

Notes:

- I had to refactor `errno` support a bit to avoid a spurious `_ZTH5errno` (AKA "thread-local initialization routine for errno") import in `libc++.so`.
- Long double print and scan are included by default in `libc.so` rather than in a separate library.
- `__main_argc_argv` is now a weak symbol since it's not relevant for reactors.
- `dlopen`/`dlsym` rely on a lookup table provided by the "dynamic" linker via `__wasm_set_libraries`.  Not all flags are supported yet, and unrecognized flags will result in an error.
- This requires https://reviews.llvm.org/D153293, which we will need to backport to LLVM 16 until 17 is released.  I'll open a `wasi-sdk` PR with that change and various Makefile tweaks to support shared libraries.
- `libc.so` is temporarily disabled for the `wasi-threads` build until someone can make `wasi_thread_start.s` position-independent.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

build `-fPIC` .o files separately from non-`-fPIC` ones

This allows us to build both libc.so and libc.a without incurring indirection
penalties in the latter.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

only build libc.so when explicitly requested

Shared library support in LLVM for non-Emscripten Wasm targets will be added in
version 17, which has not yet been released, so we should not attempt to build
libc.so by default (at least not yet).

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

remove dl.c

I'll open a separate PR for this later.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

update `check-symbols` files

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* generate separate .so files for emulated features

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* revert errno changes in favor of a smaller change

@yamt pointed out there's an easier way to address the `_ZTH5errno` issue I
described in an earlier commit: use `_Thread_local` for both C and C++.  This
gives us a simpler ABI and avoids needing to import a thread-local initializer
for `errno` in libc++.so.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* remove redundant `$(OBJDIR)/%.long-double.pic.o` rule in Makefile

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* consolidate libwasi-emulated-*.so into a single library

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* add comment explaining use of `--whole-archive`

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* Revert "remove redundant `$(OBJDIR)/%.long-double.pic.o` rule in Makefile"

This reverts commit dbe2cb10541dd27e4e0ed71d30ce304b9c9133d6.

* move `__main_void` from __main_void.c to crt1-command.c

This and `__main_argc_argv` are only relevant for commands (not reactors), so it
makes sense to scope them accordingly.  In addition, the latter was being
imported from libc.so, forcing applications to provide it even if it wasn't
relevant.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* Revert "consolidate libwasi-emulated-*.so into a single library"

This reverts commit c6518223a49f60e4bb254a3e77a411fdade18df2.

* build crt1-*.o with `-fPIC`

This ensures they can be used in a PIE or PIC context.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* ignore `__memory_base` when checking undefined symbols

Whether this symbol appears varies between LLVM versions.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* Revert "move `__main_void` from __main_void.c to crt1-command.c"

This reverts commit f3038354610b7eb18bfd39092a2ccc3b72842dc4.

* add explanatory comments to __main_void.c

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* add `__wasilibc_unmodified_upstream` and comment to `__lctrans_cur`

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
2023-09-28 06:11:09 -07:00
Marcin Kolny
9f51a71020
Add definitions for PF_INET, PF_INET6 and PF_UNSPEC (#426)
Given there are already AF_* definitions, and they are (now) essentially
synonyms, we add those definitions to enable compilation of code that
already use PF_* macros.
2023-07-13 09:18:08 -07:00
YAMAMOTO Takashi
d8abbaac1b
dlmalloc: require __heap_end (#394)
This commit effectively drops the support of older wasm-ld. (LLVM <15.0.7).

We have two relevant use cases:

* `memory.grow` use outside of malloc
  (eg. used by polyfill preview1 binaries)

* `--init-memory` to somehow preallocate heap
  (eg. avoid dynamic allocations, especially on small environments)

While https://github.com/WebAssembly/wasi-libc/pull/377
fixed the former, it broke the latter if you are using
an older LLVM, which doesn't provide the `__heap_end` symbol,
to link your module.

As we couldn't come up with a solution which satisfies all parties,
this commit simply makes it require new enough LLVM which provides
`__heap_end`. After all, a link-time failure is more friendly to users
than failing later in a subtle way.
2023-07-11 15:32:08 -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
Alex Crichton
38f48942fa
Fix debug build's predefined-macros.txt (#407)
This commit fixes the ability to build `wasi-libc` with `-g` options and
possibly without `-O2` options as well. I've found this useful when
debugging issues as historically that the build fails when `-g` is
passed or optimizations are removed due to the checks against these
expectation files. This commit adds more filters to the list of macros
to ensure that optimization/debug related ones are all removed from the
expectation lists.
2023-04-05 12:13:39 -07:00
YAMAMOTO Takashi
8daaba387c
Fix MSG_TRUNC (#391) 2023-02-04 07:45:56 -08:00
Marcin Kolny
b4814997f6
threads: implement support for spinlock (#324) 2023-01-26 09:42:48 -08:00
Andrew Brown
4362b1885f
threads: change wasm32-wasi-pthread to wasm32-wasi-threads (#381)
* Change `wasm32-wasi-pthread` to `wasm32-wasi-threads`

After some thought, I think that we should rename the `THREAD_MODEL=posix` build to avoid confusion. Why? Though in this project the use of this target does involve pthreads, it will not be so in other standard libraries or languages (see, e.g., https://github.com/rust-lang/compiler-team/issues/574). I think it would be preferable to emphasize the "threads" Wasm-level proposal and the "wasi-threads" proposal rather than the specific details of which threading API is being exposed.

* fix: rename the `expected` output directory as well
2023-01-12 18:56:52 -08:00
Andrew Brown
451065469a
threads: add pthread_attr_setdetachstate (#382)
This API may not make a lot of sense in a WebAssembly world but it
seemed helpful to include it, even if it doesn't have much effect.
2023-01-11 19:47:44 -08:00
Mike Hommey
04431e56b2
Remove hacks for clang 8 (#384)
__FLOAT128__ has been defined since clang 9
__FLT16_* were defined until clang 9
2023-01-11 18:09:45 -08:00
YAMAMOTO Takashi
35fee1d900
Implement the critical part of wasi_thread_start in asm (#376)
* Implement the critical part of wasi_thread_start in asm

It's fragile to set up the critical part of C environment in C.

* Specify --target for asm files as well

* wasi_thread_start: Move __tls_base initialization to asm as well
2023-01-06 11:34:22 -08:00
Shengyun Zhou
082a15c5a9
Enable pthread_equal function definition (#374) 2022-12-27 02:34:03 -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
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
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
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
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
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