Commit Graph

281 Commits

Author SHA1 Message Date
Marcin Kolny
5f7079f0f5
Return error when shutdown() fails (#2801)
Fix issue reported in #2787.
2023-11-24 21:03:59 +08:00
Jämes Ménétrey
f9e8b9535e
Attestation: Free JSON from the Wasm module heap (#2803)
The JSON evidence is allocated on the module instance heap, but no API
was given to dispose of this memory buffer. The sample mentions using
the function free, which behaves differently depending on the
execution context.

This fix provides a new function called librats_dispose_evidence_json,
enabling freeing the JSON evidence directly from the Wasm app.
2023-11-22 10:48:14 +08:00
zoraaver
b39fd516d3
Use next generation crypto API on Windows (#2769)
CryptGenRandom is deprecated by Microsoft and may be removed in future
releases. They recommend to use the next generation API instead. See
https://learn.microsoft.com/en-us/windows/win32/seccng/cng-portal for
more details. Also, refactor the random functions to return error codes
rather than aborting the program if they fail.
2023-11-17 18:40:29 +08:00
zoraaver
8c27676718
Return uint32 from WASI functions (#2749)
Returning uint16 from WASI functions is technically correct. However,
the smallest integer type in WASM is int32 and since we don't guarantee
that the upper 16 bits of the result are zero'ed, it can result in
tricky bugs if the language SDK being used in the WASM app does not cast
back immediately to uint16. To prevent this, we directly return uint32
instead, so that the result is well-defined as a 32-bit number.
2023-11-13 20:07:32 +08:00
Haiyue Wang
625c95816d
Fix switch-case fallthrough compilation warning (#2753)
The commit fa5e9d72b0 ("Abstract POSIX filesystem functions") introduces
the build warning:

./core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c: In function ‘fd_object_release’:
./core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c:545:20: warning: this statement may fall through [-Wimplicit-fallthrough=]
  545 |                 if (os_is_dir_stream_valid(&fo->directory.handle)) {
      |                    ^
./core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c:549:13: note: here
  549 |             default:
      |             ^~~~~~~

Refer to the commit fb4afc7ca4 ("Apply clang-format for core/iwasm compilation and libraries"),
add one line "// Fallthrough." to make compiler happy.

Signed-off-by: Haiyue Wang <haiyue.wang@intel.com>
2023-11-13 15:01:13 +08:00
Wenyong Huang
71340b79a6
Merge pull request #2740 from bytecodealliance/dev/wasi-libc-windows
The implementation is already in a stage where it's possible to compile WAMR
with wasi libc enabled and run wasi modules without errors.
2023-11-10 16:58:31 +08:00
YAMAMOTO Takashi
24c4d256b3
Grab cluster->lock when modifying exec_env->module_inst (#2685)
Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2680

And when switching back to the original module_inst, propagate exception if any.

cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/2512
2023-11-09 18:56:02 +08:00
Wenyong Huang
20f1a8c86c Merge branch main into dev/wasi-libc-windows 2023-11-09 10:13:59 +08:00
zoraaver
3c9cd40aa6
Provide default vprintf on UWP (#2725)
UWP apps do not have a console attached so any output to stdout/stderr
is lost. Therefore, provide a default BH_VPRINTF in that case for debug
builds which redirects output to the debugger.
2023-11-07 20:37:43 +08:00
zoraaver
13875f43c6
Enable WASI tests on Windows CI (#2699)
Most of the WASI filesystem tests require at least creating/deleting a
file to test filesystem functionality so some additional filesystem APIs
have been implemented on Windows so we can test what has been
implemented so far. For those WASI functions which haven't been
implemented, we skip the tests. These will be implemented in a future PR
after which we can remove the relevant filters.

Additionally, in order to run the WASI socket and thread tests, we need
to install the wasi-sdk in CI and build the test source code prior to
running the tests.
2023-11-06 19:24:06 +08:00
Gavin Hayes
51a6b069d2
Fix use getrandom on cosmopolitan libc (#2674)
Fixes the Cosmopolitan Libc platform attempting to use `/dev/urandom`
on operating systems that do not have it.

Signed-off-by: G4Vi <gavin@dylibso.com>
2023-10-27 14:07:16 +08:00
zoraaver
e7a62d2099
Refactor clock functions to use WASI types (#2666)
Refactoring the clock functions to use WASI types so we can simplify the
code and remove some unnecessary boilerplate. See
https://github.com/bytecodealliance/wasm-micro-runtime/pull/2637#discussion_r1362202879
for details.
2023-10-25 18:06:04 +08:00
Wenyong Huang
9d7931e1d2
Fix several typo/warning/unused-code issues (#2655)
- Fix typo in wamr-test-suites script
- Fix compilation warnings in libc-wasi posix.c
- Remove unused code fast-jit jit_frontend.c
- Remove duplicated exception print in `iwasm -f <function>`
- Fix return value in void function wasm_runtime_set_wasi_ctx
2023-10-24 09:19:45 +08:00
fadumina1
a874bf0ff8
Implement wasi clock_time/clock_res get (#2637)
Add os_clock_res_get and os_clock_time_get in platform_api_extension.h,
and implement them in posix like platforms and windows platform.
2023-10-22 21:11:29 +08:00
zoraaver
fa5e9d72b0
Abstract POSIX filesystem functions (#2585)
To allow non-POSIX platforms such as Windows to support WASI libc
filesystem functionality, create a set of wrapper functions which provide a
platform-agnostic interface to interact with the host filesystem. For now,
the Windows implementation is stubbed but this will be implemented
properly in a future PR. There are no functional changes in this change,
just a reorganization of code to move any direct POSIX references out of
posix.c in the libc implementation into posix_file.c under the shared
POSIX sources.

See https://github.com/bytecodealliance/wasm-micro-runtime/issues/2495 for a
more detailed overview of the plan to port the WASI libc filesystem to Windows.
2023-10-19 19:19:39 +08:00
Wenyong Huang
e222955f31 Merge branch main into dev/wasi-libc-windows 2023-10-09 09:24:00 +08:00
Wenyong Huang
7c22bde8dc
CI: Enable testing AOT multi-module feature (#2621)
And refine some code pieces.
2023-10-08 08:36:49 +08:00
Gavin Hayes
d8ee771e28
Add Cosmopolitan Libc Platform (#2598)
This PR adds the Cosmopolitan Libc platform enabling compatibility with multiple
x86_64 operating systems with the same binary. The platform is similar to the
Linux platform, but for now only x86_64 with interpreter modes are supported.

The only major change to the core is `posix.c/convert_errno()` was rewritten to use
a switch statement. With Cosmopolitan errno values depend on the currently
running operating system, and so they are non-constant and cannot be used in array
designators. However, the `cosmocc` compiler allows non-constant case labels in
switch statements, enabling the new version.

And updated wamr-test-suites script to add `-j <platform>` option. The spec tests
can be ran via `CC=cosmocc ./test_wamr.sh -j cosmopolitan -t classic-interp`
or `CC=cosmocc ./test_wamr.sh -j cosmopolitan -t fast-interp`.
2023-10-04 21:55:37 +08:00
zoraaver
8987432f36
libc-wasi: Conditionally support SYNC flags (#2581)
To make it clearer to users when synchronization behaviour is not
supported, return ENOTSUP when O_RSYNC, O_DSYNC or O_SYNC are
respectively not defined. Linux also doesn't support O_RSYNC despite the
O_RSYNC flag being defined.
2023-10-04 21:02:25 +08:00
Maks Litskevich
28ebd57400
Refactor stress tests to make them runnable in reactor mode (#2614) 2023-10-04 08:10:10 +08:00
Zhen Kong
1a88104160
Remove module instance from hashmap in wasi_nn_destroy (#2613)
When destroying wasi-nn context, module instance should be also
removed from hashmap to avoid memory leak.
2023-10-03 08:33:11 +08:00
zoraaver
913a1414ad
Add support for closing/renumbering preopen fds (#2578)
There doesn't appear to be a clear reason not to support this behavior.
It seems it was disallowed previously as a precaution. See
67e2e57b02
for more context.
2023-09-26 09:31:32 +08:00
zoraaver
f474f3d668
Return __WASI_EINVAL from fd_prestat_dir_name (#2580)
Return a WASI error code (rather than a host POSIX one). In addition,
there is no need to return an error in the case that the provided buffer
is too large.
2023-09-25 18:40:12 +08:00
zoraaver
06527f724e
Remove provision of unnecessary fd rights (#2579)
The WASI docs allow for fewer rights to be applied to an fd than requested but
not more. This behavior is also asserted in the rust WASI tests, so it's necessary
for those to pass as well.
2023-09-22 09:11:25 +08:00
YAMAMOTO Takashi
444b159963
Implement async termination of blocking thread (#2516)
Send a signal whose handler is no-op to a blocking thread to wake up
the blocking syscall with either EINTR equivalent or partial success.

Unlike the approach taken in the `dev/interrupt_block_insn` branch (that is,
signal + longjmp similarly to `OS_ENABLE_HW_BOUND_CHECK`), this PR
does not use longjmp because:
* longjmp from signal handler doesn't work on nuttx
  refer to https://github.com/apache/nuttx/issues/10326
* the singal+longjmp approach may be too difficult for average programmers
  who might implement host functions to deal with

See also https://github.com/bytecodealliance/wasm-micro-runtime/issues/1910
2023-09-20 18:11:52 +08:00
YAMAMOTO Takashi
a6fda9b7ab
Add an API to terminate instance (#2538)
Add API wasm_runtime_terminate to terminate a module instance
by setting "terminated by user" exception to the module instance.

And update the product-mini of posix platforms.

Note: this doesn't work for some situations like blocking system calls.
2023-09-13 17:09:31 +08:00
YAMAMOTO Takashi
f128150d43
fd_object_release: Preserve errno (#2535)
Preserve errno because this function is often used like
the following. The caller wants to report the error from the main
operation (`lseek` in this example), not from fd_object_release.

```
    off_t ret = lseek(fd_number(fo), offset, nwhence);
    fd_object_release(fo);
    if (ret < 0)
        return convert_errno(errno);
```
2023-09-08 12:26:32 +08:00
YAMAMOTO Takashi
6c846acc59
Implement module instance context APIs (#2436)
Introduce module instance context APIs which can set one or more contexts created
by the embedder for a wasm module instance:
```C
    wasm_runtime_create_context_key
    wasm_runtime_destroy_context_key
    wasm_runtime_set_context
    wasm_runtime_set_context_spread
    wasm_runtime_get_context
```

And make libc-wasi use it and set wasi context as the first context bound to the wasm
module instance.

Also add samples.

Refer to https://github.com/bytecodealliance/wasm-micro-runtime/issues/2460.
2023-09-07 14:54:11 +08:00
Cengizhan Pasaoglu
af2f3c8759
Fix typo for IP address buffer (#2532) 2023-09-07 14:29:52 +08:00
YAMAMOTO Takashi
151dfae49c
Unifdef -U WASMTIME_SSP_STATIC_CURFDS (#2533)
The macro WASMTIME_SSP_STATIC_CURFDS isn't used in the source code,
remove the related macro control code.
2023-09-06 15:25:58 +08:00
YAMAMOTO Takashi
48b71a05fb
libc-wasi: Remove unused code (#2528) 2023-09-04 19:14:56 +08:00
YAMAMOTO Takashi
9e3904337e
Revert "Return error when exception was raised after main thread finishes" (#2524)
This reverts commit 71d43f3ca1.
2023-09-04 18:01:09 +08:00
YAMAMOTO Takashi
2e78230e06
set_exception_visitor: Remove the special case for wasi proc exit (#2525)
While wasi proc exit is not a real trap, what the runtime does on it is mostly same as
real traps. That is, kill the siblings threads and represent the exit/trap as the result of
the "process" to the user api. There seems no reason to distinguish it from real traps
here.

Note that:
- The target thread either doesn't care the specific exception type or ignore wasi
  proc exit by themselves. (clear_wasi_proc_exit_exception)
- clear_wasi_proc_exit_exception only clears local exception.
2023-09-04 16:52:19 +08:00
Marcin Kolny
8c2dc1d011
Add unit tests for the tid allocator (#2519)
Add simple infrastructure to add more unit tests in the future. At the moment tests
are only executed on Linux, but can be extended to other platforms if needed.

Use https://github.com/google/googletest/ as a framework.
2023-09-04 13:21:10 +08:00
YAMAMOTO Takashi
382d52fc05
Stop abusing shared memory lock to protect exception (#2509)
Use a separate global lock instead.

Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2407
2023-08-31 20:39:08 +08:00
Marcin Kolny
53d7027de0
Implement strict validation of thread IDs according to the specification (#2521) 2023-08-31 20:23:54 +08:00
Maks Litskevich
411b903cee
Add mutex stress test (#2472)
As a part of stress-testing we want to ensure that mutex implementation is working
correctly and protecting shared resource to be allocated from other threads when
mutex is locked.

This test covers the most common situations that happen when some program uses
mutexes like locks from various threads, locks from the same thread etc.
2023-08-30 19:01:44 +08:00
Jämes Ménétrey
ff151fb7ba
sgx-ra: Disable the building of samples (#2507) 2023-08-28 09:42:16 +08:00
Wenyong Huang
b7a9da1620 Merge branch main into dev/wasi-libc-windows 2023-08-23 17:05:19 +08:00
Wenyong Huang
e2f8721ec9
Fix issues reported by Coverity and clear windows warnings (#2467) 2023-08-17 10:54:02 +08:00
Maks Litskevich
4ce675aacd
Add another wamr test (#2411)
Follows up #2364 where we discussed that we might want to have a test
which has really short thread function and creates many threads.
2023-08-17 10:31:05 +08:00
Maks Litskevich
a9bd6af0ac
Build wasi-libc from source in WAMR CI (#2465)
We need to apply some bug fixes that were merged to wasi-libc because wasi-sdk-20
is about half a year old.

It is a temporary solution and the code will be removed when wasi-sdk 21 is released.
2023-08-15 10:36:10 +08:00
Jämes Ménétrey
365cdfeb71
Upgrade SGX-RA integration for 0.1.2 and Ubuntu 20.04 (#2454) 2023-08-15 10:12:43 +08:00
tonibofarull
b45d014112
wasi-nn: Improve TPU support (#2447)
1. Allow TPU and GPU support at the same time.
2. Add Dockerfile to run example with [Coral USB](https://coral.ai/products/accelerator/).
2023-08-14 20:03:56 +08:00
tonibofarull
0b0af1b3df
wasi-nn: Support uint8 quantized networks (#2433)
Support (non-full) uint8 quantized networks.
Inputs and outputs are still required to be `float`. The (de)quantization is done internally by wasi-nn.

Example generated from `quantized_model.py`:
![Screenshot from 2023-08-07 17-57-05](https://github.com/bytecodealliance/wasm-micro-runtime/assets/80318361/91f12ff6-870c-427a-b1dc-e307f7d1f5ee)

Visualization with [netron](https://netron.app/).
2023-08-11 07:55:40 +08:00
YAMAMOTO Takashi
51714c41c0
Introduce WASMModuleInstanceExtraCommon (#2429)
Move the common parts of WASMModuleInstanceExtra and
AOTModuleInstanceExtra into the new structure.
2023-08-08 09:35:29 +08:00
Wenyong Huang
a07d8160f9
Merge pull request #2426 from bytecodealliance/main
Merge branch main into dev/wasi-libc-windows
2023-08-06 08:50:21 +08:00
YAMAMOTO Takashi
91592429f4
Fix memory sharing (#2415)
- Inherit shared memory from the parent instance, instead of
  trying to look it up by the underlying module. The old method
  works correctly only when every cluster uses different module.
- Use reference count in WASMMemoryInstance/AOTMemoryInstance
  to mark whether the memory is shared or not
- Retire WASMSharedMemNode
- For atomic opcode implementations in the interpreters, use
  a global lock for now
- Update the internal API users
  (wasi-threads, lib-pthread, wasm_runtime_spawn_thread)

Fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/1962
2023-08-04 10:18:13 +08:00
Wenyong Huang
cb6d85069e
Fix lib-pthread issues (#2410)
- Avoid destroying module instance repeatedly in pthread_exit_wrapper and
  wasm_thread_cluster_exit.
- Wait enough time in pthread_join_wrapper for target thread to exit and
  destroy its resources.
2023-08-01 18:11:58 +08:00
Maks Litskevich
b88f2c06c6
Add initial stress test (#2364)
We need to make a test that runs longer than the tests we had before to check
some problems that might happen after running for some time (e.g. memory
corruption or something else).
2023-08-01 17:38:37 +08:00