Commit Graph

665 Commits

Author SHA1 Message Date
Xu Jun
d75cb3224f
Fix dead lock in source debugger (#2040) 2023-03-20 08:17:22 +08:00
Enrico Loparco
83659fa42a
Use wasi-sdk 20 pre-release for tests with threads (#2021)
`wasi-sdk-20` pre-release can be used to avoid building `wasi-libc` to enable threads.
It's not possible to use `wasi-sdk-20` pre-release on Ubuntu 20.04 because of
incompatibility with the glibc version:
```bash
/opt/wasi-sdk/bin/clang: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found
(required by /opt/wasi-sdk/bin/clang)
```
2023-03-17 20:02:03 +08:00
liang.he
915b4d2a89
Fix few wasm-c-api python binding issues (#2029)
How to python language binding test cases:
```
$ cd <wamr_dir>/language-bindings/python
$ python -m pip install -e .
$ cd wasm-c-api
$ python -m unittest tests/test_basic.py
$ python -m unittest tests/test_advanced.py
```
2023-03-15 12:34:34 +08:00
Wenyong Huang
bab2402b6e
Fix atomic.wait, get wasi_ctx exit code and thread mgr issues (#2024)
- Remove notify_stale_threads_on_exception and change atomic.wait
  to be interruptible by keep waiting and checking every one second,
  like the implementation of poll_oneoff in libc-wasi
- Wait all other threads exit and then get wasi exit_code to avoid
  getting invalid value
- Inherit suspend_flags of parent thread while creating new thread to
  avoid terminated flag isn't set for new thread
- Fix wasi-threads test case update_shared_data_and_alloc_heap
- Add "Lib wasi-threads enabled" prompt for cmake
- Fix aot get exception, use aot_copy_exception instead
2023-03-15 07:47:36 +08:00
Georgii Rylov
2de24587a8
Fix wait_info data race for deletion and fix atomic_wait logic (#2016)
Fix a data race for test main_proc_exit_wait.c from #1963.
And fix atomic_wait logic that was wrong before:
- a thread 1 started executing wasm instruction wasm_atomic_wait
  but hasn't reached waiting on condition variable
- a main thread calls proc_exit and notifies all the threads that reached
  waiting on condition variable
Which leads to thread 1 hang on waiting on condition variable after that

Now it's atomically checked whether proc_exit was already called.
2023-03-13 10:19:17 +08:00
Wenyong Huang
578fbc5a55
Fix fast-jit build error (#2023) 2023-03-12 20:17:49 +08:00
Enrico Loparco
ea6cef7e51
Fix malloc non-thread-safe usage in lib-wasi-threads test (#2022)
In the WASI thread test modified in this PR, malloc was used in multiple threads
without a lock. But wasi-libc implementation of malloc is not thread-safe.
2023-03-11 10:44:37 +08:00
Hritik Gupta
ff3887757e
Add internal tests for socket apis (#1900) 2023-03-09 12:31:06 +08:00
Enrico Loparco
128c0ea899
Add internal tests for WASI threads (#1963)
Add internal tests for WASI threads. These tests are run in addition to
the ones in the proposal:
https://github.com/WebAssembly/wasi-threads/tree/main/test/testsuite.

The purpose is to test additional and more complex scenarios.
2023-03-09 09:03:16 +08:00
tonibofarull
a15a731e12
wasi-nn: Support multiple TFLite models (#2002)
Remove restrictions:
- Only 1 WASM app at a time
- Only 1 model at a time
   - `graph` and `graph-execution-context` are ignored

Refer to previous document:
e8d718096d/core/iwasm/libraries/wasi-nn/README.md
2023-03-08 15:54:06 +08:00
Wenyong Huang
f279ba84ee
Fix multi-threading issues (#2013)
- Implement atomic.fence to ensure a proper memory synchronization order
- Destroy exec_env_singleton first in wasm/aot deinstantiation
- Change terminate other threads to wait for other threads in
  wasm_exec_env_destroy
- Fix detach thread in thread_manager_start_routine
- Fix duplicated lock cluster->lock in wasm_cluster_cancel_thread
- Add lib-pthread and lib-wasi-threads compilation to Windows CI
2023-03-08 10:57:22 +08:00
Wenyong Huang
04616d398d
Fix wasm_cluster_create_thread issue (#2004)
In wasm_cluster_create_thread, the new_exec_env is added into the cluster's
exec_env list before the thread is created, so other threads can access the
fields of new_exec_env once the cluster->lock is unlocked, while the
new_exec_env's handle is set later inside the thread routine. This may result
in the new_exec_env's handle be invalidly accessed by other threads.
2023-03-06 18:51:13 +08:00
Enrico Loparco
e8d718096d
Add/reorganize locks for thread synchronization (#1995)
Attempt to fix data races when using threads.
- Protect access (from multiple threads) to exception and memory
- Fix shared memory lock usage
2023-03-04 08:15:26 +08:00
Wenyong Huang
1be202fad8
Fix several issues found (#1996)
- CMakeLists.txt: add lib_export.h to install list
- Fast JIT: enlarge spill cache size to enable several standalone cases
                when hw bound check is disabled
- Thread manager: wasm_cluster_exit_thread may destroy an invalid
               exec_env->module_inst when exec_env was destroyed before
- samples/socket-api: fix failure to run timeout_client.wasm
- enhance CI build wasi-libc and sample/wasm-c-api-imports CMakeLlist.txt
2023-03-03 15:00:54 +08:00
Wenyong Huang
9b9ae0cfac
Update cmake files and wamr-test-suites to support collect code coverage (#1992)
Support collecting code coverage with wamr-test-suites script by using
lcov and genhtml tools, eg.:
  cd tests/wamr-test-suites
  ./test_wamr.sh -s spec -b -P -C

The default code coverage and html files are generated at:
  tests/wamr-test-suites/workspace/wamr.lcov
  tests/wamr-test-suites/workspace/wamr-lcov.zip

And update wamr-test-suites scripts to support testing GC spec cases to
avoid frequent synchronization conflicts between branch main and dev/gc.
2023-02-28 17:38:18 +08:00
Enrico Loparco
52e26e59cf
Add lock to protect the operations of accessing exec env (#1991)
Data race may occur when accessing exec_env's fields, e.g. suspend_flags
and handle. Add lock `exec_env->wait_lock` for them to resolve the issue.
2023-02-27 19:53:41 +08:00
Wenyong Huang
38c67b3f48
thread-mgr: Fix spread "wasi proc exit" exception and atomic.wait issues (#1988)
Raising "wasi proc exit" exception, spreading it to other threads and then
clearing it in all threads may result in unexpected behavior: the sub thread
may end first, handle the "wasi proc exit" exception and clear exceptions
of other threads, including the main thread. And when main thread's
exception is cleared, it may continue to run and throw "unreachable"
exception. This also leads to some assertion failed.

Ignore exception spreading for "wasi proc exit" and don't clear exception
of other threads to resolve the issue.

And add suspend flag check after atomic wait since the atomic wait may
be notified by other thread when exception occurs.
2023-02-24 20:05:39 +08:00
Enrico Loparco
63273a1673
Fix issues in the libc-wasi poll_oneoff when thread-mgr is enabled (#1980)
Fix issues in the libc-wasi `poll_oneoff` when thread manager is enabled:
-  The exception of a thread may be cleared when other thread runs into
   `proc_exit` and then calls `clear_wasi_proc_exit_exception`, so should not
   use `wasm_runtime_get_exception` to check whether an exception was
    thrown, use `wasm_cluster_is_thread_terminated` instead
- We divided one time poll_oneoff into many times poll_oneoff to check
   the exception to avoid long time waiting in previous PR, but if all events
   returned by one time poll are all waiting events, we need to continue to
   wait but not return directly.

Follow-up on #1951. Tested with multiple timeout values, with and without
interruption and measured the time spent sleeping.
2023-02-23 16:11:33 +08:00
Wenyong Huang
d2772c4153
Re-org calling post instantiation functions (#1972)
- Use execute_post_instantiate_functions to call start, _initialize,
  __post_instantiate, __wasm_call_ctors functions after instantiation
- Always call start function for both main instance and sub instance
- Only call _initialize and __post_instantiate for main instance
- Only call ___wasm_call_ctors for main instance and when bulk memory
  is enabled and wasi import functions are not found
- When hw bound check is enabled, use the existing exec_env_tls
  to call func for sub instance, and switch exec_env_tls's module inst
  to current module inst to avoid checking failure and using the wrong
  module inst
2023-02-22 12:24:11 +08:00
Enrico Loparco
bc60064de5
Fix data races in atomic wait/notify and interp goto table (#1971)
Add shared memory lock when accessing the address to atomic wait/notify
inside linear memory to resolve its data race issue.

And statically initialize the goto table of interpreter labels to resolve the
data race issue of accessing the table.
2023-02-22 11:43:49 +08:00
Wenyong Huang
0fa0813a5a
Fix issues reported by Coverity static analysis (#1974) 2023-02-22 11:10:21 +08:00
liang.he
91eafa1ead
Make a workaround for EGO when fstat returns NOT_SUPPORT (#1970)
The problem was found by a `Golang + WAMR (as CGO)` wrapped by EGO
in SGX Enclave.

`fstat()` in EGO returns dummy values:
- EGO uses a `mount` configuration to define the mount points that apply
  the host file system presented to the Encalve.
- EGO has a different programming model: the entire application runs inside
  the enclave. Manual ECALLs/OCALLs by application code are neither
  required nor possible.

Add platform ego and add macro control for the return value checking of
`fd_determine_type_rights` in libc-wasi to resolve the issue.
2023-02-21 11:11:27 +08:00
YAMAMOTO Takashi
37b09d0f24
Expose wasm_runtime_call_indirect (#1969)
The function has been there for long. While what it does look a bit unsafe
as it calls a function which may be not wasm-wise exported explicitly, it's
useful and widely used when implementing callback-taking APIs, including
our pthread_create's implementation.
2023-02-20 18:56:55 +08:00
Wenyong Huang
e516de8ec7
Fix possible data race in thread manager (#1973)
Destroy child thread's exec_env before destroying its module instance and
add the process into cluster's lock to avoid possible data race: if exec_env
is removed from custer's exec_env_list and destroyed later, the main thread
may not wait it and start to destroy the wasm runtime, and the destroying
of the sub thread's exec_env may free or overread/written an destroyed or
re-initialized resource.

And fix an issue in wasm_cluster_cancel_thread.
2023-02-20 17:11:21 +08:00
Wenyong Huang
739acfc908
Merge pull request #1966 from bytecodealliance/dev/wasi_threads
Merge wasi_threads to main
2023-02-17 17:42:47 +08:00
Wenyong Huang
ef3a683392
Don't call start/initialize in child thread's instantiation (#1967)
The start/initialize functions of wasi module are to do some initialization work
during instantiation, which should be only called one time in the instantiation
of main instance. For example, they may initialize the data in linear memory,
if the data is changed later by the main instance, and re-initialized again by
the child instance, unexpected behaviors may occur.

And clear a shadow warning in classic interpreter.
2023-02-17 15:11:05 +08:00
Wenyong Huang
e170c355a2 Merge branch main into dev/wasi_threads 2023-02-17 08:46:12 +08:00
Hritik Gupta
50650e4634
Modify poll_oneoff in libc-wasi to make it interruptible (#1951) 2023-02-16 21:03:33 +08:00
Enrico Loparco
216dc43ab4
Use shared memory lock for threads generated from same module (#1960)
Multiple threads generated from the same module should use the same
lock to protect the atomic operations.

Before this PR, each thread used a different lock to protect atomic
operations (e.g. atomic add), making the lock ineffective.

Fix #1958.
2023-02-16 11:54:19 +08:00
Charles Liu
1c17665f68
Fix libc-wasi build failure when using clang (#1950)
Resolves #1949
2023-02-14 18:28:00 +08:00
Xu Jun
531771eb11
Don't terminate other threads when create thread failed (#1955) 2023-02-13 16:23:24 +08:00
liang.he
3698f2279b
Improve wasm-c-api instantiation-time linking (#1902)
Add APIs to help prepare the imports for the wasm-c-api `wasm_instance_new`:
- wasm_importtype_is_linked
- wasm_runtime_is_import_func_linked
- wasm_runtime_is_import_global_linked
- wasm_extern_new_empty

For wasm-c-api, developer may use `wasm_module_imports` to get the import
types info, check whether an import func/global is linked with the above API,
and ignore the linking of an import func/global with `wasm_extern_new_empty`.

Sample `wasm-c-api-import` is added and document is updated.
2023-02-13 15:06:04 +08:00
ocfox
427abf02c8
Fix potential integer overflow issue in wasm-c-api (#1954)
Fix potential integer overflow issue in wasm-c-api reported by CodeQL
2023-02-13 13:55:50 +08:00
Hritik Gupta
f3c1ad4864
Fix terminating stale threads on trap/proc_exit (#1929)
This is to terminate suspended threads in case an atomic wait occurs with
a huge or indefinite (-1) timeout, followed by a proc exit or trap.
2023-02-07 20:56:23 +08:00
Marcin Kolny
e2c754f0a6
Fix data race when terminating or waiting for a thread (#1924)
There is a possibility that while iterating through a list of threads, the threads
finish by themselves, making a list of exec_env invalid.
2023-02-06 19:08:45 +08:00
Enrico Loparco
42ced88424
Fix stack alignment issue on ia32 (#1934)
The stack of Unix-like (GCC) system should be aligned on 16-byte boundary
according to the x86-32 ABI specification.
2023-02-06 09:07:00 +08:00
Wenyong Huang
c3e9b66b2a
Fix jit memory overwritten after instance deinstantiate (#1936)
When de-instantiating the wasm module instance, remove it from the module's
instance list before freeing func_ptrs and fast_jit_func_ptrs of the instance, to avoid
accessing these freed memory in the JIT backend compilation threads.
2023-02-03 11:15:03 +08:00
Marcin Kolny
17f3375d53
Enable gcc-4.8 compilation (#1928) 2023-02-02 19:34:20 +08:00
Wenyong Huang
40a14b51c5
Enable running mode control for runtime and module instance (#1923)
Enable setting running mode when executing a wasm bytecode file
- Four running modes are supported: interpreter, fast-jit, llvm-jit and multi-tier-jit
- Add APIs to set/get the default running mode of the runtime
- Add APIs to set/get the running mode of a wasm module instance
- Add running mode options for iwasm command line tool

And add size/opt level options for LLVM JIT
2023-02-02 18:16:01 +08:00
Wenyong Huang
7bb78dc260
Remove fast jit macro in enum WASMExceptionID (#1933)
The definitions `enum WASMExceptionID` in the compilation of wamrc and the compilation
of Fast JIT are different, since the latter enables the Fast JIT macro while the former doesn't.
This causes that the exception ID in AOT file generated by wamrc may be different from
iwasm binary compiled with Fast JIT enabled, and may result in unexpected behavior.
Remove the macro control to resolve it.
2023-02-02 15:23:44 +08:00
Wenyong Huang
edee2eb956
Loosen the wasi abi compatibility check in loader (#1932)
Change an error to warning when checking wasi abi compatibility in loader, for rust case below:
#[no_mangle]
pub extern "C" fn main() {
    println!("foo");
}
compile it with `cargo build --target wasm32-wasi`, a wasm file is generated with wasi apis imported
and a "void main(void)" function exported.

Other runtime e.g. wasmtime allows to load it and execute the main function with `--invoke` option.
2023-02-02 13:06:30 +08:00
tonibofarull
1614ce12fa
wasi-nn: Enable GPU support (#1922)
- Split logic in several dockers
  - runtime: wasi-nn-cpu and wasi-nn- Nvidia-gpu.
  - compilation: wasi-nn-compile. Prepare the testing wasm and generates the TFLites.
- Implement GPU support for TFLite with Opencl.
2023-02-02 08:09:46 +08:00
HongxiaWangSSSS
fe3347d5d2
Fixed the undef error about WAMR_BUILD_MEMORY_PROFILING (#1926) 2023-02-01 17:52:46 +08:00
YAMAMOTO Takashi
7d3b2a8773
Make memory profiling show native stack usage (#1917) 2023-02-01 11:52:15 +08:00
Huang Qi
58316635d4
ci: Enable libc-wasi compilation test on NuttX (#1918) 2023-01-28 12:39:14 +08:00
Enrico Loparco
2eed50b303
Document how to use WASI threads in AOT mode (#1905)
Describe how to use WASI threads in AOT mode, following the discussion below:
  https://github.com/bytecodealliance/wasm-micro-runtime/pull/1867#discussion_r1070268062

Make aux stack boundary checks of wasi-threads always successful by setting
`exec_env->aux_stack_bottom` to UINT32_MAX and `exec_env->aux_stack_boundary` to 0
2023-01-27 18:32:33 +08:00
YAMAMOTO Takashi
42f8fed20e
Rename thread_spawn import (#1907)
Following the wit-defined ABI:
https://github.com/WebAssembly/wasi-threads/pull/26

cf. https://github.com/WebAssembly/wasi-libc/pull/387
2023-01-27 06:45:34 +08:00
tonibofarull
9eed6686df
Refactor WASI-NN to simplify the support for multiple frameworks (#1834)
- Reorganize the library structure
- Use the latest version of `wasi-nn` wit (Oct 25, 2022):
    0f77c48ec1/wasi-nn.wit.md
- Split logic that converts WASM structs to native structs in a separate file
- Simplify addition of new frameworks
2023-01-25 18:32:40 +08:00
Enrico Loparco
b0b8843719
Implement opcode atomic.wait and atomic.notify for Fast JIT (#1914) 2023-01-25 09:53:40 +08:00
Wenyong Huang
c7141894fb Merge branch main into dev/wasi_threads 2023-01-21 13:18:55 +08:00