Commit Graph

187 Commits

Author SHA1 Message Date
Wenyong Huang
e70c5219c0 Merge branch main into dev/gc_refactor 2024-01-26 16:43:51 +08:00
Wenyong Huang
313ce8cb61
Fix memory/table segment checks in memory.init/table.init (#3081)
According to the wasm core spec, the checks for the table segments in
`table.init` opcode are similar to the checks for `memory.init` opcode:
- The size of a passive segment is shrunk to zero after `data.drop`
  (or `elem.drop`) opcode is executed, and the segment can be used to do
  `memory.init` (or `table.init`) again
- The `memory.init` only traps when `s+n > len(data.data)` or `d+n > len(mem.data)`
  and `table.init` only traps when `s+n > len(elem.elem)` or `d+n > len(tab.elem)`
- The active segment can also be used to do `memory.init` (or `table.init`),
  while it behaves like a dropped passive segment

https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
```
Segments can also be shrunk to size zero by using the following new instructions:
- data.drop: discard the data in an data segment
- elem.drop: discard the data in an element segment

An active segment is equivalent to a passive segment, but with an implicit
memory.init followed by a data.drop (or table.init followed by a elem.drop)
that is prepended to the module's start function.
```
ps.
https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-memorymathsfmemoryinitx%E2%91%A0
https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-tablemathsftableinitxy%E2%91%A0
https://github.com/bytecodealliance/wasm-micro-runtime/issues/3020
2024-01-26 09:45:59 +08:00
liang.he
5c8b8a17a6
Enhancements on wasm function execution time statistic (#2985)
Enhance the statistic of wasm function execution time, or the performance
profiling feature:
- Add os_time_thread_cputime_us() to get the cputime of a thread,
  and use it to calculate the execution time of a wasm function
- Support the statistic of the children execution time of a function,
  and dump it in wasm_runtime_dump_perf_profiling
- Expose two APIs:
  wasm_runtime_sum_wasm_exec_time
  wasm_runtime_get_wasm_func_exec_time

And rename os_time_get_boot_microsecond to os_time_get_boot_us.
2024-01-17 09:51:54 +08:00
Marcin Kolny
ffa131b5ac
Allow using mmap for shared memory if hw bound check is disabled (#3029)
For shared memory, the max memory size must be defined in advanced. Re-allocation
for growing memory can't be used as it might change the base address, therefore when
OS_ENABLE_HW_BOUND_CHECK is enabled the memory is mmaped, and if the flag is
disabled, the memory is allocated. This change introduces a flag that allows users to use
mmap for reserving memory address space even if the OS_ENABLE_HW_BOUND_CHECK
is disabled.
2024-01-16 22:15:55 +08:00
Wenyong Huang
e7bbf88a1a
Refine the format of call stack dump (#2996)
For interpreter, jit and aot, the format is like:
```
#00: 0x1330 - __main_argc_argv
#01: 0x4195 - __main_void
#02: 0x11dc - _start
```
For fast-jit and multi-tier jit, the format is like:
```
#00 __main_argc_argv
#01 __main_void
#02 _start
```
Since fast-jit hasn't supported commit ip to stack frame now.
2024-01-11 14:27:28 +08:00
Wenyong Huang
d31455fc4e
Refactor aot stack frame commit (#2976)
- Commit locals, stacks and stack pointer to aot frame only when gc is enabled
- Commit instruction pointer to aot frame when stack frame is enabled
- Refine alloc/free aot frame when gc isn't enabled: use fixed frame size
- Support dump call stack with bytecode offset
2024-01-08 11:18:49 +08:00
Wenyong Huang
cc23a09d5e
Fix test script error and related wamrc and GC issues (#2975)
The wamrc execution errors were skipped in the runtest.py due to `sys.exit(1)`
was caught by upper try-catch sentence and ignored with ret_code 0 returned.

This PR fixed this script issue and fixed the related wamrc/GC issues found.
2024-01-08 09:41:56 +08:00
Wenyong Huang
e6d210a67f Merge branch main into dev/gc_refactor 2024-01-03 12:11:57 +08:00
Wenyong Huang
3637f2df79
Refine LLVM JIT function call process (#2925)
- Don't allocate the implicit/unused frame when calling the LLVM JIT function
- Don't set exec_env's thread handle and stack boundary in the recursive
  calling from host, since they have been set in the first time calling
- Fix frame not freed in llvm_jit_call_func_bytecode
2024-01-02 18:46:02 +08:00
Wenyong Huang
cf7350f069
Implement aot_alloc_frame/aot_free_frame with LLVM IRs (#2830) 2023-12-12 12:12:02 +08:00
Xu Jun
92176be7d6
Sync up with the latest GC MVP spec proposal (#2836) 2023-12-12 11:49:44 +08:00
Wenyong Huang
9c71ca6c46 Merge branch main into dev/gc_refactor 2023-11-29 09:23:00 +08:00
Wenyong Huang
5d9cbfe109
Implement GC AOT object reclaim process (#2762) 2023-11-22 14:39:07 +08:00
Wenyong Huang
4d5eb346fc
Change is_shared_memory type from bool to uint8 (#2800)
Change WASMMemoryInstance's field is_shared_memory's type from bool
to uint8 whose size is fixed, so as to make WASMMemoryInstance's size
and layout fixed and not break AOT ABI.

See discussion in https://github.com/bytecodealliance/wasm-micro-runtime/pull/2682.
2023-11-22 10:38:08 +08:00
Huang Qi
9ad42290d8
Fix formatting in wasm_dump_perf_profiling (#2799)
Changes %d to %PRIu32.
2023-11-20 18:06:35 +08:00
YAMAMOTO Takashi
562a5dd1b6
Fix data/elem drop (#2747)
Currently, `data.drop` instruction is implemented by directly modifying the
underlying module. It breaks use cases where you have multiple instances
sharing a single loaded module. `elem.drop` has the same problem too.

This PR  fixes the issue by keeping track of which data/elem segments have
been dropped by using bitmaps for each module instances separately, and
add a sample to demonstrate the issue and make the CI run it.

Also add a missing check of dropped elements to the fast-jit `table.init`.

Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2735
Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2772
2023-11-18 08:50:16 +08:00
Huang Qi
24aa1cb408
Extend os_mmap to support map file from fd (#2763)
Add an extra argument `os_file_handle file` for `os_mmap` to support
mapping file from a file fd, and remove `os_get_invalid_handle` from
`posix_file.c` and `win_file.c`, instead, add it in the `platform_internal.h`
files to remove the dependency on libc-wasi.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2023-11-16 08:28:54 +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
Wenyong Huang
64e40c13a9
Fix more GC AOT/JIT issues (#2727)
Fix opcode translation for br_on_null/br_on_non_null/br_on_cast/br_on_cast_fail
Fix global data size/offset calculation for 32-bit/64-bit targets
Fix issues in AOT file emitting and AOT loader, refine AOT file format
Fix invalid table element address used for table.get/table.set
Fix invalid struct field offset used for struct.get/struct.set
Fix aot stack frame commit for function call/call_indirect/call_ref
Add GC AOT/JIT to CI test
2023-11-09 08:52:56 +08:00
Wenyong Huang
7f8292ffd1
Add more buffer boundary checks in wasm loader (#2734)
And fix exception not printed in `iwasm --repl` mode and resize the memory
data size to UINT32_MAX if the initial page number is 65536.
2023-11-09 08:42:05 +08:00
Wenyong Huang
348d82b923 Merge branch main into dev/gc_refactor 2023-11-03 15:39:28 +08:00
Wenyong Huang
68a627ea2c
Fix several AOT compiler issues (#2697)
- Fix potential invalid push param phis and add incoming phis to a un-existed basic block
- Fix potential invalid shift count int rotl/rotr opcodes
- Resize memory_data_size to UINT32_MAX if it is 4G when hw bound check is enabled
- Fix negative linear memory offset is used for 64-bit target it is const and larger than INT32_MAX
2023-11-02 20:36:21 +08:00
Wenyong Huang
52db362b89
Refine lock/unlock shared memory (#2682)
Split memory instance's field `uint32 ref_count` into `bool is_shared_memory`
and `uint16 ref_count`, and lock the memory only when `is_shared_memory`
flag is true, no need to acquire a lock for non-shared memory when shared
memory feature is enabled.
2023-10-31 11:46:03 +08:00
Wenyong Huang
4b1a6e5017
Fix repeatedly initialize shared memory data and protect the memory's fields (#2673)
Avoid repeatedly initializing the shared memory data when creating the child
thread in lib-pthread or lib-wasi-threads.

Add shared memory lock when accessing some fields of the memory instance
if the memory instance is shared.

Init shared memory's memory_data_size/memory_data_end fields according to
the current page count but not max page count.

Add wasm_runtime_set_mem_bound_check_bytes, and refine the error message
when shared memory flag is found but the feature isn't enabled.
2023-10-30 11:07:01 +08:00
Wenyong Huang
ae508e25fa Merge branch dev/aot_stack_frame into dev/gc_refactor 2023-10-09 14:21:39 +08:00
Wenyong Huang
e222955f31 Merge branch main into dev/wasi-libc-windows 2023-10-09 09:24:00 +08:00
TianlongLiang
760505e607
AOT compiler: Implement left of GC opcode compilation (#2487)
Implement the compilation to LLVM IRs for the left GC opcodes (struct/array related):
- WASM_OP_STRUCT_NEW_CANON, WASM_OP_STRUCT_NEW_CANON_DEFAULT
- WASM_OP_STRUCT_GET, WASM_OP_STRUCT_GET_S, WASM_OP_STRUCT_GET_U,
  WASM_OP_STRUCT_SET
- WASM_OP_ARRAY_NEW_CANON, WASM_OP_ARRAY_NEW_CANON_DEFAULT
  WASM_OP_ARRAY_NEW_CANON_FIXED, WASM_OP_ARRAY_NEW_CANON_DATA
- WASM_OP_ARRAY_GET, WASM_OP_ARRAY_GET_S, WASM_OP_ARRAY_GET_U
  WASM_OP_ARRAY_SET, WASM_OP_ARRAY_LEN, WASM_OP_ARRAY_COPY
2023-10-08 14:07:12 +08:00
Wenyong Huang
85869ed604 Merge branch main into dev/gc_refactor 2023-10-08 10:53:13 +08:00
Wenyong Huang
7a9ed07411 Merge branch main into dev/aot_stack_frame 2023-10-08 10:01:45 +08:00
Enrico Loparco
00539620e9
Improve stack trace dump and fix coding guideline CI (#2599)
Avoid the stack traces getting mixed up together when multi-threading is enabled
by using exception_lock/unlock in dumping the call stacks.

And remove duplicated call stack dump in wasm_application.c.

Also update coding guideline CI to fix the clang-format-12 not found issue.
2023-09-29 10:52:54 +08:00
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
TianlongLiang
a9cac2ec0a
AOT compiler: Implement part of GC opcode compilation (#2486)
Implement the compilation to LLVM IRs for the GC opcodes below:
- WASM_OP_REF_EQ, WASM_OP_CALL_REF, WASM_OP_RETURN_CALL_REF
- WASM_OP_REF_AS_NON_NULL, WASM_OP_BR_ON_NULL, WASM_OP_BR_ON_NON_NULL
- WASM_OP_I31_NEW, WASM_OP_I31_GET_S, WASM_OP_I31_GET_U
- WASM_OP_REF_TEST, WASM_OP_REF_CAST
- WASM_OP_REF_TEST_NULLABLE, WASM_OP_REF_CAST_NULLABLE
- WASM_OP_BR_ON_CAST, WASM_OP_BR_ON_CAST_FAIL
- WASM_OP_BR_ON_CAST_NULLABLE, WASM_OP_BR_ON_CAST_FAIL_NULLABLE
- WASM_OP_EXTERN_INTERNALIZE, WASM_OP_EXTERN_EXTERNALIZE
2023-09-19 16:19:56 +08:00
Wenyong Huang
4b09e283ef
Add option for JIT stack frame and update documents (#2565)
Allow `cmake -DWAMR_BUILD_JIT_STACK_FRAME=1` to enable stack frame
for JIT mode.

And fix some issues in doc/build_wamr.md.
2023-09-19 16:07:38 +08:00
Huang Qi
067a86a774
Implement AOT file emitting and loading for GC (#2366) 2023-09-19 15:51:32 +08:00
Wenyong Huang
fd5862fbe0
Enhance AOT stack frame dump (#2541)
Add cmake varaible WAMR_BUILD_AOT_STACK_FRAME and auto enable it for
dump-call-stack and perf-profiling features. Extend WASMCApiFrame and
commit more data to it in dump-call-stack.

Commit stack pointer and instruction pointer when calling functions.
Enable setting callback when exception is thrown for debug purpose.
2023-09-15 18:58:17 +08:00
Wenyong Huang
6cacbc6b14
Merge pull request #2540 from bytecodealliance/main
Merge branch main into dev/aot_stack_frame
2023-09-11 11:39:14 +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
Wenyong Huang
33ac031a3b
Enhance LLVM AOT/JIT stack frame dump (#2350)
Implement a full LLVM AOT/JIT stack frame dump:
commit the function arguments, locals, stack operands from LLVM values to the stack frame,
which is required by the GC AOT/JIT feature, and may be required by the AOT debugger,
AOT snapshot and other features.

Refer to:
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2144
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2333
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2506
2023-08-28 09:45:02 +08:00
Wenyong Huang
b7a9da1620 Merge branch main into dev/wasi-libc-windows 2023-08-23 17:05:19 +08:00
Wenyong Huang
03155cf22b Merge branch main into dev/gc_refactor 2023-08-23 16:40:13 +08:00
TianlongLiang
7d86111a53
Refactor the LLVM IR translation of the existing opcodes for GC AOT (#2376)
Refactor the LLVM IR translation of below opcodes:
  CALL_INDIRECT, SELECT_T, TABLE_GET, TABLE_SET,
  REF_NULL, REF_IS_NULL, REF_FUNC,
  TABLE_INIT, TABLE_GROW, TABLE_FILL,
  GET_LOCAL, SET_LOCAL, TEE_LOCAL

And refactor aot_drop_table_seg and add aot_create_func_obj.
2023-08-23 16:04:10 +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
Marcin Kolny
ea763009b7
Enable running spec tests on Windows (#2423)
Update wamr-test-suites scripts to enable running spec tests on Windows.
We don't enable those tests in CI yet as not all of them are passing.
2023-08-09 09:40:59 +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