Commit Graph

410 Commits

Author SHA1 Message Date
Mike Hommey
574b88da48
Adjust Makefile for LLVM trunk (20) as of 2024-12-02 (#556)
c3536b263f
2024-12-09 15:51:08 -08:00
Andrew Brown
2b853ff079
test: improve test infrastructure (#554)
This change represents a rather large re-design in how `wasi-libc`
builds and runs its tests. Initially, #346 retrieved the `libc-test`
repository and built a subset of those tests to give us some amount of
test coverage. Later, because there was no way to add custom C tests,
#522 added a `smoke` directory which allowed this. But (a) each of these
test suites was built and run separately and (b) it was unclear how to
add more tests flexibly--some tests should only run on `*p2` targets or
`*-threads` targets, e.g.

This change reworks all of this so that all tests are built the same
way, in the same place. For downloaded tests like those from
`libc-test`, I chose to add "stub tests" that `#include` the original
version. This not only keeps all enabled tests in one place, it also
allows us to add "directives," C comments that the `Makefile` uses to
filter out tests for certain targets or add special compile, link or run
flags. These rudimentary scripts, along with other Bash logic I moved
out of the Makefile now live in the `scripts` directory.

Finally, all of this is explained more clearly in an updated
`README.md`. The hope with documenting this a bit better is that it
would be easier for drive-by contributors to be able to either dump in
new C tests for regressions they may find or enable more libc-tests. As
of my current count, we only enable 40/75 of libc-test's functional
tests, 0/228 math tests, 0/69 regression tests, and 0/79 API tests.
Though many of these may not apply to WASI programs, it would be nice to
explore how many more of these tests can be enabled to increase
wasi-libc's test coverage. This change should explain how to do that
and, with directives, make it possible to condition how the tests
compile and run.
2024-12-09 13:45:31 -08:00
Andrew Brown
29c22a4b68
Move installation of include headers into a separate script (NFC) (#552)
This change is not meant to change any functionality, only move some
Bash-specific logic out of the `Makefile` into its own script:
`install-include-headers.sh`. This reduces the perceived complexity of
the `Makefile` but the complexity is still there, tucked away in this
script. This script also has the advantage that it can be run separately
if needed.

This commit comes after a few different attempts at building up
`Makefile` lists of headers to copy over along with the various
locations they must be copied from. It is certainly possible to do this,
but due to how we need to remove some headers from the list, it ends up
being easier to just `cp` and then `rm`, which this script retains.
2024-12-03 17:28:05 -08:00
Andrew Brown
33c208377b
Move all bindings-related code together (NFC) (#553)
This change is an effort to move all dependencies of the phony
`bindings` target into one place, for clarity.
2024-11-27 13:13:52 -08:00
Andrew Brown
777361ddc7
Avoid re-copying the startup object files (#555)
This adds a `.stamp` file to the process of copying over the `crt` files
to the sysroot directory to avoid re-copying on each `make` invocation.
2024-11-27 13:13:06 -08:00
Andrew Brown
c47daaf6c6
Avoid re-copying include headers (#549)
This adds a `.stamp` file to the build directory that tracks when all
headers have been copied over to the sysroot `include` directory.
Previously, the `include_dirs` target was phony, which `make` will run
every time, regardless of whether it is needed. This was part of the
problem with all of libc being rebuilt at each `make` invocation.
2024-11-20 14:25:44 -08:00
Andrew Brown
913e58e63f
Avoid rebuilding empty placeholder libraries (#550)
Previously, these were rebuilt each time as a part of the phone
`dummy_libs` target. This change only rebuilds them if they don't exist.
2024-11-20 13:28:45 -08:00
Andrew Brown
fe15980ff0
test: update all tools used for running tests (#548)
This updates:
- `wasi-sdk` to v24
- Wasmtime to v26.0.1
- `wasm-tools` to v1.220.0
2024-11-19 10:34:03 -08:00
Mike Hommey
dfecba3f06
Adjust Makefile for LLVM trunk (20) as of 2024-10-25 (#546)
1bc2cd98c5
2024-11-12 10:37:11 -08:00
Yuta Saito
98897e29fc
Fix fts build for shared library (#544)
https://github.com/WebAssembly/wasi-libc/pull/522 did not include the
necessary changes to the Makefile for libc_so build.
Additionally, updated CI to check `libc_so` build too to avoid future
breakage.
2024-10-16 09:02:17 -07:00
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
Andrew Brown
7d4d3b83fc
ci: update actions (#535)
This updates the GitHub actions to avoid errors related to deprecations;
see [this example].

[this example]:
https://github.com/WebAssembly/wasi-libc/actions/runs/10839597105/job/30080296576
2024-09-17 17:05:06 -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
YAMAMOTO Takashi
3f812abfb2
fix shared library build with llvm-19 (#526) 2024-08-27 17:01:21 -07:00
YAMAMOTO Takashi
8279f9591a
Exclude setjmp runtime from LTO (#529)
This fixes errors like:
```
wasm-ld: error: /Volumes/PortableSSD/git/wasi-sdk/build/install/bin/../share/wasi-sysroot/lib/wasm32-wasi/llvm-lto/19.1.0-wasi-sdk/libsetjmp.a(rt.o): attempt to add bitcode file after LTO (__wasm_longjmp)
```

Note: Any symbol that the compiler might generate at bitcode compile
time either need to be unconditionally included at LTO time, or not
built as LTO. This is because LTO object files cannot be added to the
link after LTO time.
2024-08-27 16:42:48 -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
YAMAMOTO Takashi
b9ef79d7db
Use the correct compiler-rt (#517)
eg. when a user specifies non-default -resource-dir.

cf. https://github.com/WebAssembly/wasi-sdk/pull/446
2024-07-24 13:14:31 -05:00
YAMAMOTO Takashi
b9e15a8af9
Add LTO build option (#505)
* Add LTO build option

An old PR which I used as a base:
https://github.com/WebAssembly/wasi-libc/pull/150

Co-Authored-by: Dan Gohman <dev@sunfishcode.online>

* Exclude atexit.c from LTO

This fixes a failure in wasi-sdk "make check".
("undefined symbol: __cxa_atexit" for ctors_dtors.c test)

* avoid specifying multiple lto flags for LIBC_NONLTO_OBJS

---------

Co-authored-by: Dan Gohman <dev@sunfishcode.online>
2024-07-12 11:21:34 -07:00
Mike Hommey
d43dcc63d2
Adjust Makefile for LLVM trunk (19) as of 2024-07-10 (#512)
0e7590a25c
2024-07-11 16:11:35 -07:00
Mrmaxmeier
3f43ea9abb
iconv/wctomb: fix memory corruption related to CURRENT_UTF8 implementation (#511) 2024-07-08 08:30:39 -07:00
Luca Versari
5667be15d6
0-initialize thread-specific data upon thread creation. (#508)
If thread-specific data is not set to 0 upon thread creation,
`__pthread_tsd_run_dtors` will end up running destructors passing
uninitialized memory as memory addresses, which can lead to memory
corruption.

This issue can be triggered when malloc() returns a memory address that
was freed before, as in that case memory is not zeroed out.
2024-07-03 16:09:00 -07:00
Yuta Saito
67080fa045
times should always return 0 for tms_cutime (#510)
`tms_cutime` is the sum of the user times of child processes *excluding
the current process*. Since WASI doesn't provide a way to spawn a new
process, this value should always be 0.
2024-07-02 22:46:25 -07:00
Mike Hommey
320bbbcced
Adjust Makefile for LLVM trunk (19) as of 2024-06-20 (#509)
41c6e43792
2024-06-21 09:49:33 -07:00
Pat Hickey
ebac9aee23
timezone __secs_to_zone stub: guard against null pointer dereference (#507)
Closes #506
2024-06-17 09:48:52 -07:00
YAMAMOTO Takashi
31845366d4
Makefile: separate the target to create empty dummy libraries (#502)
* Makefile: separate the target to create empty dummy libraries

During develompment, it's sometimes convenient to have a separate target.

* ensure to create the destination directory
2024-05-30 16:10:57 -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
44c4b1e3a5
Use a different makefile variable for -D__wasilibc_use_wasip2 (#499)
This switches from `EXTRA_CFLAGS` to `CFLAGS` so when `EXTRA_CFLAGS` is
in the environment to specify `-g`, for example, it doesn't override
p2-handling logic.
2024-05-21 09:36:55 -05: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
YAMAMOTO Takashi
a3ef1520ec
dlmalloc: account the footprint of the initial heap (#496)
While malloc_stats and friends are disabled and unused for wasi-libc,
it's neater to be consistent.

Background: My colleagues for some reasons enabled malloc_stats
and asked me why it reports negative values.

Note: Depending __heap_base, init_top() adjusts the address for
alignment. I think the amount of this adjustment is reported as
"used" by malloc_stats. I don't bother to "fix" it.
2024-05-17 10:53:56 -07: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
Andrew Brown
887613873d
Remove extra lock-taking in preopen setup (#491)
Issue [#8392] in Wasmtime highlights how preopen registration can result
in a hang when compiled for a threaded environment. The problem is that
`internal_register_preopened_fd_unlocked` assumes it will not touch the
global lock because its caller, `__wasilibc_populate_preopens`, already
has taken the lock. Unfortunately, a refactoring in #408 (which
introduces `internal_register_preopened_fd_unlocked`) did not catch that
the `resize` function called internally also takes the global lock. This
change removes that locking in `resize` under the assumption that it
will only be called when the lock is already taken.

[#8392]: https://github.com/bytecodealliance/wasmtime/issues/8392
2024-05-02 14:07:27 -07:00
Mike Hommey
129ee9b64b
Adjust Makefile for LLVM trunk (19) as of 2024-04-30 (#493)
5bbf1ea8f1
added __wasm_multivalue__ and __wasm_reference_types__, and the latter
also makes libraries reference the undefined __indirect_function_table
symbol, which is provided by the linker.
2024-05-01 07:30:33 -07:00
Mike Hommey
13ed98026d
Adjust Makefile for LLVM trunk (19) as of 2024-04-26 (#492)
72c373bfdc
dded __GCC_(CON|DE)STRUCTIVE_SIZE macros.
2024-04-29 14:46:03 -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
YAMAMOTO Takashi
1ab654e2f5
Add libsetjmp.a/so (#483)
* Add libsetjmp.a/so

Add setjmp/longjump support based on Wasm EH proposal.

It's provided as a separate library (libsetjmp) from libc so that
runtimes w/o EH support can still load libc.so.

To use this setjmp/longjmp implementation, an application should
be compiled with `-mllvm -wasm-enable-sjlj` and linked with `-lsetjmp`.
(You need an LLVM with the change mentioned below.)

Also, you need a runtime with EH support to run such an application.

If you want to use the latest EH instructions, you can use
`binaryen --translate-eh-old-to-new` on your application.

Note: You don't need to translate libsetjmp.a/so to the new EH.
While LLVM currently produces bytecode for an old version of the EH
proposal, luckily for us, the bytecode used in this library (ie. the tag
definition and the "throw" instruction) is compatible with the latest
version of the proposal.

The runtime logic is basically copy-and-paste from:
    https://github.com/yamt/garbage/tree/wasm-sjlj-alt2/wasm/longjmp

The corresponding LLVM change:
    https://github.com/llvm/llvm-project/pull/84137
    (Note: you need this change to build setjmp/longjmp using code.
    otoh, you don't need this to build libsetjmp.)

A similar change for emscripten:
    https://github.com/emscripten-core/emscripten/pull/21502

An older version of this PR, which doesn't require LLVM changes:
    https://github.com/WebAssembly/wasi-libc/pull/467

Discussion:
    https://docs.google.com/document/d/1ZvTPT36K5jjiedF8MCXbEmYjULJjI723aOAks1IdLLg/edit

An example to use the latest EH instructions:
```
clang -mllvm -wasm-enable-sjlj -o your_app.wasm your_app.c -lsetjmp
wasm-opt --translate-eh-old-to-new -o your_app.wasm your_app.wasm
toywasm --wasi your_app.wasm
```
Note: use toywasm built with `-DTOYWASM_ENABLE_WASM_EXCEPTION_HANDLING=ON`.

An example to use the older EH instructions, which LLVM currently produces:
```
clang -mllvm -wasm-enable-sjlj -o your_app.wasm your_app.c -lsetjmp
iwasm your_app.wasm
```
Note: use wasm-micro-runtime built with `-DWAMR_BUILD_EXCE_HANDLING=1`.
Note: as of writing this, only the classic interpreter supports EH.

* Make libsetjmp build optional

* CI: Disable libsetjmp for old LLVM

* libc-top-half/musl/include/setjmp.h: fix a rebase botch
2024-04-01 15:36:28 -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
YAMAMOTO Takashi
a963040f0a
crt1-command.c: fix whitespace issues (#480) 2024-03-11 17:04:46 -07:00
Milek7
c8ef60ad9b
Add support for pthread_getattr_np (#470) 2024-03-11 08:48:13 -07:00
韩朴宇
a1b4def387
fix #ifdef __cplusplus guard in dirent.h (#479)
fix: https://github.com/WebAssembly/wasi-sdk/issues/390
2024-03-08 06:08:11 -08: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