Commit Graph

265 Commits

Author SHA1 Message Date
dongsheng28849455
79b27c1934
Support muti-module for AOT mode (#2482)
Support muti-module for AOT mode, currently only implement the
multi-module's function import feature for AOT, the memory/table/
global import are not implemented yet.

And update wamr-test-suites scripts, multi-module sample and some
CIs accordingly.
2023-09-28 08:56:11 +08:00
Wenyong Huang
6382162711
Fix loader push_pop_frame_ref_offset (#2590)
`wasm_loader_push_pop_frame_offset` may pop n operands by using
`loader_ctx->stack_cell_num` to check whether the operand can be
popped or not. While `loader_ctx->stack_cell_num` is updated in the
later `wasm_loader_push_pop_frame_ref`, the check may fail if the stack
is in polymorphic state and lead to `ctx->frame_offset` underflow.

Fix issue #2577 and #2586.
2023-09-26 10:17:54 +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
Enrico Loparco
709127d631
Add callback to handle memory.grow failures (#2522)
When embedding WAMR, this PR allows to register a callback that is
invoked when memory.grow fails.

In case of memory allocation failures, some languages allow to handle
the error (e.g. by checking the return code of malloc/calloc in C), some
others (e.g. Rust) just panic.
2023-09-05 16:41:52 +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
Xu Jun
7baaed2fb8
Fix opcode overwrite issue in fast interp (#2476) 2023-08-17 19:49:35 +08:00
Wenyong Huang
e2f8721ec9
Fix issues reported by Coverity and clear windows warnings (#2467) 2023-08-17 10:54:02 +08:00
YAMAMOTO Takashi
e360b7a919
wasm_instantiate: Fix a potential integer overflow issue (#2459)
Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2450
2023-08-14 17:27:14 +08:00
YAMAMOTO Takashi
5c6613b2b1
Correct --heap-size option in messages (#2458) 2023-08-14 15:12:59 +08:00
Wenyong Huang
8b5bb0009d
wasm_export.h: Fix struct wasm_val_t (#2435)
Struct wasm_val_t should be same in wasm_export.h and wasm_c_api.h.

And fix some invalid calls to aot function in LLVM JIT mode.
2023-08-09 09:43:20 +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
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
YAMAMOTO Takashi
28125ec538
Move wasm_runtime_destroy_wasi and wasi_nn_destroy calls together (#2418)
And remove obsolete comment.
2023-08-03 08:46:56 +08:00
Wenyong Huang
edea32b629
Fix result arity check on select_t opcode (#2406)
Typed select must have exactly one result. Reported in issue #2402.
2023-07-31 18:20:11 +08:00
Wenyong Huang
59b2099b68
Fix some check issues on table operations (#2392)
Fix some check issues on table.init, table.fill and table.copy, and unify the check method
for all running modes.
Fix issue #2390 and #2096.
2023-07-27 21:53:48 +08:00
Marcin Kolny
0f4edf9735
Implement suspend flags as atomic variable (#2361)
We have observed a significant performance degradation after merging
https://github.com/bytecodealliance/wasm-micro-runtime/pull/1991
Instead of protecting suspend flags with a mutex, we implement the flags
as atomic variable and only use mutex when atomics are not available
on a given platform.
2023-07-21 08:27:09 +08:00
Xu Jun
fbcf8c2c60
Fix some static scan issues (#2362) 2023-07-18 16:06:21 +08:00
YAMAMOTO Takashi
228a3bed53
Fix unused warnings on disable_bounds_checks (#2347) 2023-07-06 15:31:22 +08:00
Huang Qi
18092f86cc
Make memory access boundary check behavior configurable (#2289)
Allow to use `cmake -DWAMR_CONFIGURABLE_BOUNDS_CHECKS=1` to
build iwasm, and then run `iwasm --disable-bounds-checks` to disable the
memory access boundary checks.

And add two APIs:
`wasm_runtime_set_bounds_checks` and `wasm_runtime_is_bounds_checks_enabled`
2023-07-04 16:21:30 +08:00
tonibofarull
ab96e01f5e
wasi-nn: Add support of wasi-nn as shared lib (#2310)
## Context

Currently, WAMR supports compiling iwasm with flag `WAMR_BUILD_WASI_NN`.
However, there are scenarios where the user might prefer having it as a shared library.

## Proposed Changes

Decouple wasi-nn context management by internally managing the context given
a module instance reference.
2023-06-27 18:18:26 +08:00
TianlongLiang
efaf37c577
Fix fast-interp issue of LAST_OP_OUTPUT_I32/64 check (#2295) 2023-06-16 10:00:24 +08:00
Huang Qi
7ec77598dd
Fix format warning by PRIu32 in [wasm|aot] dump call stack (#2251) 2023-06-11 11:30:25 +08:00
Wenyong Huang
76be848ec3
Implement the segue optimization for LLVM AOT/JIT (#2230)
Segue is an optimization technology which uses x86 segment register to store
the WebAssembly linear memory base address, so as to remove most of the cost
of SFI (Software-based Fault Isolation) base addition and free up a general
purpose register, by this way it may:
- Improve the performance of JIT/AOT
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
- Reduce the compilation time of JIT/AOT

This PR uses the x86-64 GS segment register to apply the optimization, currently
it supports linux and linux-sgx platforms on x86-64 target. By default it is disabled,
developer can use the option below to enable it for wamrc and iwasm(with LLVM
JIT enabled):
```bash
wamrc --enable-segue=[<flags>] -o output_file wasm_file
iwasm --enable-segue=[<flags>] wasm_file [args...]
```
`flags` can be:
    i32.load, i64.load, f32.load, f64.load, v128.load,
    i32.store, i64.store, f32.store, f64.store, v128.store
Use comma to separate them, e.g. `--enable-segue=i32.load,i64.store`,
and `--enable-segue` means all flags are added.

Acknowledgement:
Many thanks to Intel Labs, UC San Diego and UT Austin teams for introducing this
technology and the great support and guidance!

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
Co-authored-by: Vahldiek-oberwagner, Anjo Lucas <anjo.lucas.vahldiek-oberwagner@intel.com>
2023-05-26 10:13:33 +08:00
Zzzabiyaka
27239723a9
Add asan and ubsan to WAMR CI (#2161)
Add nightly (UTC time) checks with asan and ubsan, and also put gcc-4.8 build
to nightly run since we don't need to run it with every PR.

Co-authored-by: Maksim Litskevich <makslit@amazon.co.uk>
2023-05-26 09:45:37 +08:00
Wenyong Huang
1e5f206464
Fix compile warnings on windows platform (#2208) 2023-05-15 13:48:48 +08:00
Wenyong Huang
62fc486c20
Refine aot compiler check suspend_flags and fix issue of multi-tier jit (#2111)
In LLVM AOT/JIT compiler, only need to check the suspend_flags when memory is
a shared memory since the shared memory must be enabled for multi-threading,
so as not to impact the performance in non-multi-threading memory mode. Also
refine the LLVM IRs to check the suspend_flags.

And fix an issue of multi-tier jit for multi-threading, the instance of the child thread
should be removed from the instance list before it is de-instantiated.
2023-04-07 06:47:24 +08:00
Wenyong Huang
5fc48e3584
Fix interpreter read linear memory size for multi-threading (#2088)
Load memory data size in each time memory access boundary check in
multi-threading mode since it may be changed by other threads when
memory growing.

And use `memory->memory_data_size` instead of
`memory->num_bytes_per_page * memory->cur_page_count` to refine
the code.
2023-04-04 09:05:52 +08:00
Wenyong Huang
e1d0c27ef9
Fix ref.func forward-declared function check (#2099)
When ref.func opcode refers to a function whose function index no smaller than
current function, the destination func should be forward-declared: it is declared
in the table element segments, or is declared in the export list.
2023-04-03 15:55:24 +08:00
Wenyong Huang
b0736e2e88
Fix issues reported by Coverity (#2083)
Get exec_env_tls at the beginning of execute_post_instantiate_functions
to avoid it is uninitialized when is_sub_inst is false.
2023-03-29 19:40:52 +08:00
Wenyong Huang
10f1bf3af7
Fix module_malloc/module_free issues (#2072)
Try using existing exec_env to execute wasm app's malloc/free func and
execute post instantiation functions. Create a new exec_env only when
no existing exec_env was found.
2023-03-28 18:31:09 +08:00
Wenyong Huang
605c8b07dc
Fix issue of Multi-tier JIT (#2056) 2023-03-25 11:15:05 +08:00
Wenyong Huang
3977f0b22a
Use pre-created exec_env for instantiation and module_malloc/free (#2047)
Use pre-created exec_env for instantiation and module_malloc/free,
use the same exec_env of the current thread to avoid potential
unexpected behavior.

And remove unnecessary shared_mem_lock in wasm_module_free,
which may cause dead lock.
2023-03-23 19:19:47 +08:00
Wenyong Huang
49d439a3bc
Fix/Simplify the atomic.wait/nofity implementations (#2044)
Use the shared memory's shared_mem_lock to lock the whole atomic.wait and
atomic.notify processes, and use it for os_cond_reltimedwait and os_cond_notify,
so as to make the whole processes actual atomic operations:
the original implementation accesses the wait address with shared_mem_lock
and uses wait_node->wait_lock for os_cond_reltimedwait, which is not an atomic
operation.

And remove the unnecessary wait_map_lock and wait_lock, since the whole
processes are already locked by shared_mem_lock.
2023-03-23 09:21:16 +08:00
Xu Jun
d75cb3224f
Fix dead lock in source debugger (#2040) 2023-03-20 08:17:22 +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
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
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
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
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
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
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
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
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
YAMAMOTO Takashi
7d3b2a8773
Make memory profiling show native stack usage (#1917) 2023-02-01 11:52:15 +08:00