Commit Graph

728 Commits

Author SHA1 Message Date
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
Huang Qi
44f4b4f062
Add "--enable-llvm-passes=<passes>" option to wamrc (#2335)
Add "--enable-llvm-passes=<passes>" option to wamrc for customizing LLVM passes
2023-07-04 12:20:52 +08:00
Marcin Kolny
6ed4c9c908
Increase default native stack size (#2332)
Calling `__wasi_sock_addr_resolve` syscall causes native stack overflow.
Given this is a standard function available in WAMR, we should have at least
the default stack size large enough to handle this case.

The socket tests were updated so they also run in separate thread, but
the simple retro program is:

```C
void *th(void *p)
{
    struct addrinfo *res;
    getaddrinfo("amazon.com", NULL, NULL, &res);
    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t pt;
    pthread_create(&pt, NULL, th, NULL);
    pthread_join(pt, NULL);
    return 0;
}
```
2023-07-03 21:02:10 +08:00
Marcin Kolny
569f702ec2
Fix sanitizer errors in posix socket (#2331) 2023-07-03 14:36:01 +08:00
YAMAMOTO Takashi
c39eb46b6f
Add a few more assertions on structures to which aot abi is sensitive (#2326) 2023-06-30 10:22:46 +08:00
YAMAMOTO Takashi
03418ef5ac
aot: Avoid possible relocations around "stack_sizes" for XIP mode (#2322)
Fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/2316

Lightly tested on riscv64 qemu.
2023-06-29 18:45:33 +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
YAMAMOTO Takashi
0a0739ef23
aot: Implement a few more relocation types for riscv (#2318)
This PR partly fixes #2312. Lightly tested on qemu riscv64.
2023-06-27 16:33:05 +08:00
YAMAMOTO Takashi
5831531449
aot: Move stack_sizes table to a dedicated section (#2317)
To solve the "AOT module load failed: resolve symbol stack_sizes failed" issue.

This PR partly fixes #2312 and was lightly tested on qemu armhf.
2023-06-27 16:18:14 +08:00
Wenyong Huang
ea78b89965
Fix wamrc build issues with LLVM 13 and LLVM 16 (#2313)
Fix some build errors when building wamrc with LLVM-13, reported in #2311
Fix some build warnings when building wamrc with LLVM-16:
```
  core/iwasm/compilation/aot_llvm_extra2.cpp:26:26: warning:
  ‘llvm::None’ is deprecated: Use std::nullopt instead. [-Wdeprecated-declarations]
     26 |             return llvm::None;
```
Fix a maybe-uninitialized compile warning:
```
  core/iwasm/compilation/aot_llvm.c:413:9: warning:
  ‘update_top_block’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    413 |         LLVMPositionBuilderAtEnd(b, update_top_block);
```
2023-06-27 08:59:49 +08:00
tonibofarull
f5c5a83331
wasi-nn: Improve tests paths for local dev (#2309)
## Context

Path to models use `/assets` for testing inside docker. While testing directly from
the repo we are forced to use soft-links or modify the paths.

## Proposed Changes

Use relative path and adjust docker volumes in docs.
2023-06-27 08:07:30 +08:00
tonibofarull
ac9e789951
wasi-nn: Simplify cmake and headers' location (#2308)
Major changes:
- Public headers inside `wasi-nn/include`
- Put cmake files in `cmake` folder
- Make linux iwasm link with `${WASI_NN_LIBS}` so iwasm can enable wasi-nn
2023-06-26 09:29:05 +08:00
Blaine Bublitz
f51d98f850
Use system libuv if available (#1861)
This PR attempts to search for the system libuv and use it if found instead of
downloading it. As reported in #1831, this is needed because some tools
build in a sandbox and clear the extra sources.
2023-06-26 08:37:10 +08:00
YAMAMOTO Takashi
cd7941cc39
AOT/JIT native stack bound check improvement (#2244)
Move the native stack overflow check from the caller to the callee because the
former doesn't work for call_indirect and imported functions.

Make the stack usage estimation more accurate. Instead of making a guess from
the number of wasm locals in the function, use the LLVM's idea of the stack size
of each MachineFunction. The former is inaccurate because a) it doesn't reflect
optimization passes, and b) wasm locals are not the only reason to use stack.

To use the post-compilation stack usage information without requiring 2-pass
compilation or machine-code imm rewriting, introduce a global array to store
stack consumption of each functions:
For JIT, use a custom IRCompiler with an extra pass to fill the array.
For AOT, use `clang -fstack-usage` equivalent because we support external llc.

Re-implement function call stack usage estimation to reflect the real calling
conventions better. (aot_estimate_stack_usage_for_function_call)

Re-implement stack estimation logic (--enable-memory-profiling) based on the new
machinery.

Discussions: #2105.
2023-06-22 07:27:07 +08:00
Wenyong Huang
0ac5f206b8
Enable windows x86-32 AOT relocations (#2285)
Implement relocation types IMAGE_REL_I386_DIR32 and IMAGE_REL_I386_REL32,
fix failure to find AOT function symbol, and implement symbol __aulldiv.
2023-06-16 11:48:12 +08:00
TianlongLiang
efaf37c577
Fix fast-interp issue of LAST_OP_OUTPUT_I32/64 check (#2295) 2023-06-16 10:00:24 +08:00
TianlongLiang
8f82b1e8a2
Fix dockerfile linter warnings (#2291) 2023-06-15 16:52:48 +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
YAMAMOTO Takashi
92e073b8ce
AOTFuncContext: Remove a stale comment (#2283) 2023-06-09 22:31:08 +08:00
YAMAMOTO Takashi
cabcb177c8
dwarf_extractor: Constify a bit (#2278) 2023-06-09 09:52:03 +08:00
YAMAMOTO Takashi
bd96696236
Appease unused warning on min_uint64 (#2277) 2023-06-09 08:40:21 +08:00
YAMAMOTO Takashi
6e3c3fe9ec
Fix build error with LLVM 16 (#2259) 2023-06-06 13:45:18 +08:00
YAMAMOTO Takashi
5d69f364db
aot/jit: Set module layout (#2260)
LLVM 15 and later sometimes perform wrong optimizations without this.
2023-06-06 10:18:16 +08:00
Wenyong Huang
8ef09be604
Fix compile error of wamrc with llvm-13/llvm-14 (#2261) 2023-06-06 08:33:15 +08:00
Wenyong Huang
8d88471c46
Implement AOT static PGO (#2243)
LLVM PGO (Profile-Guided Optimization) allows the compiler to better optimize code
for how it actually runs. This PR implements the AOT static PGO, and is tested on
Linux x86-64 and x86-32. The basic steps are:

1. Use `wamrc --enable-llvm-pgo -o <aot_file_of_pgo> <wasm_file>`
   to generate an instrumented aot file.
2. Compile iwasm with `cmake -DWAMR_BUILD_STATIC_PGO=1` and run
      `iwasm --gen-prof-file=<raw_profile_file> <aot_file_of_pgo>`
    to generate the raw profile file.
3. Run `llvm-profdata merge -output=<profile_file> <raw_profile_file>`
    to merge the raw profile file into the profile file.
4. Run `wamrc --use-prof-file=<profile_file> -o <aot_file> <wasm_file>`
    to generate the optimized aot file.
5. Run the optimized aot_file: `iwasm <aot_file>`.

The test scripts are also added for each benchmark, run `test_pgo.sh` under
each benchmark's folder to test the AOT static pgo.
2023-06-05 09:17:39 +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
YAMAMOTO Takashi
94204b90ad
aot_compile_op_call: Remove a wrong optimization (#2233)
Unlike a tail-call, the caller of an ordinary recursive call doesn't
necessarily return immediately.
2023-05-25 07:44:54 +08:00
YAMAMOTO Takashi
670567f8b3
core/iwasm/compilation: constify a bit (#2223)
Just to make the code a bit easier to read.
2023-05-20 11:55:02 +08:00
YAMAMOTO Takashi
f759a1f960
A few changes related to WAMRC_LLC_COMPILER (#2218)
Print `target triple` for wamrc and set target triple for the LLVM module.
And update document.
2023-05-17 09:56:35 +08:00
Wenyong Huang
1e5f206464
Fix compile warnings on windows platform (#2208) 2023-05-15 13:48:48 +08:00
Georgii Rylov
0899638ece
Use a manual flag to disable clock_nanosleep on the unsupported platforms (#2176)
For some platforms WAMR gets compiled with `CONFIG_HAS_CLOCK_NANOSLEEP=1`,
while `clock_nanosleep` is not present at the platform, which causes compilation error.

Add check for macro `DISABLE_CLOCK_NANOSLEEP` to resolve the issue, only when
the macro isn't defined can the macro `CONFIG_HAS_CLOCK_NANOSLEEP` take effect.
2023-05-12 07:49:21 +08:00
ayakoakasaka
89be5622a5
wasi-nn: Add external delegation to support several NPU/GPU (#2162)
Add VX delegation as an external delegation of TFLite, so that several NPU/GPU
(from VeriSilicon, NXP, Amlogic) can be controlled via WASI-NN.

Test Code can work with the X86 simulator.
2023-05-05 16:29:36 +08:00
Christof Petig
5a23ae465c
Fix three multi-threading and wasm-c-api-imports issues (#2173)
Fix issue reported in #2172: wasm-c-api `wasm_func_call` may use a wrong exec_env
when multi-threading is enabled, with error "invalid exec env" reported

Fix issue reported in #2149: main instance's `c_api_func_imports` are not passed to
the counterpart of new thread's instance in wasi-threads mode

Fix issue of invalid size calculated to copy `c_api_func_imports` in pthread mode

And refactor the code to use `wasm_cluster_dup_c_api_imports` to copy the
`c_api_func_imports` to new thread for wasi-threads mode and pthread mode.
2023-05-05 10:01:58 +08:00
Enrico Loparco
71d43f3ca1
Return error when exception was raised after main thread finishes (#2169)
Currently, if a thread is spawned and raises an exception after the main thread
has finished, iwasm returns with success instead of returning 1 (i.e. error).

Since wasm_runtime_get_wasi_exit_code waits for all threads to finish and only
returns the wasi exit code, this PR performs the exception check again and
returns error if an exception was raised.
2023-05-05 09:20:05 +08:00
YAMAMOTO Takashi
2b896c80ef
wamrc: Add --stack-usage option (#2158) 2023-04-28 13:56:44 +08:00
ayakoakasaka
ed6b8efade
Avoid re-installing if Tensorflow is already installed for WASI-NN (#2148)
Since the Tensorflow library is already installed in many cases(especially in the
case of the embedded system), move the installation code to find_package.
2023-04-27 08:19:18 +08:00
Georgii Rylov
c1723b8f3e
libc_wasi_wrapper.c: Fix min func issue for size_t < 8 bytes on some platforms (#2152)
According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of
at least 16 bit (see sections 7.17 and 7.18.3), it may be uint32 in 32-bit platforms:
https://en.cppreference.com/w/cpp/types/size_t

Calling function `size_t min(size_t, size_t)` with two uint64 arguments may get
invalid result.

Co-authored-by: Georgii Rylov <godjan@amazon.co.uk>
2023-04-26 21:03:02 +08:00
Wenyong Huang
7e9bf9cdf5
Implement Fast JIT multi-threading feature (#2134)
- Translate all the opcodes of threads spec proposal for Fast JIT
- Add the atomic flag for Fast JIT load/store IRs to support atomic load/store
- Add new atomic related Fast JIT IRs and translate them in the codegen
- Add suspend_flags check in branch opcodes and before/after call function
- Modify CI to enable Fast JIT multi-threading test

Co-authored-by: TianlongLiang <tianlong.liang@intel.com>
2023-04-20 10:09:34 +08:00
Marcin Kolny
a2d4744a2b
Add test for validating linear memory size updates (#2078) 2023-04-08 19:07:20 +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
Enrico Loparco
156318f0d4
Use wasi-sdk-20 to build wasi-threads cases in CI (#2095)
wasi-sdk-20 supports older versions of glibc and allow us to use it in the CI
with Ubuntu 20.04.

Refer to
  https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-20
And #2021 for previous upgrade to wasi-sdk-20 pre-release.
2023-04-03 08:43:11 +08:00
TianlongLiang
aaf671d688
Fix typo in Fast JIT's BUILD_COND_BR Macro (#2092) 2023-03-31 16:18:43 +08:00
zoraaver
eaf1897a70
Add support for universal binaries on OSX (#2060)
When building for multiple architectures on OSX, it's necessary to use
compiler macros to conditionally include architecture-specific code
rather than conditionally including architecture-specific assembly files
via cmake. See
  https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary
and
  https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_ARCHITECTURES.html
for more details.

Co-authored-by: Zoraaver Singh <zoraaver@amazon.co.uk>
2023-03-30 09:53:07 +08:00
Andy
5aa22d41e9
Fixing use after free when dumping call stack (#2084)
In multi-threading, this line will eventually call `wasm_cluster_wait_for_all_except_self`:
`DEINIT_VEC(store->instances, wasm_instance_vec_delete)`

As the threads are joining they can call `wasm_interp_dump_call_stack` which tries to
use the module frames but they were already freed by this line:
`DEINIT_VEC(store->modules, wasm_module_vec_delete)`

This PR swaps the order that these are deleted so module is deleted after the instances.

Co-authored-by: Andrew Chambers <ncham@amazon.com>
2023-03-30 09:01:16 +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
dependabot[bot]
0ce39a1368
Bump tensorflow in /core/iwasm/libraries/wasi-nn/test (#2061)
Bumps [tensorflow](https://github.com/tensorflow/tensorflow) from 2.10.1 to 2.11.1.
- [Release notes](https://github.com/tensorflow/tensorflow/releases)
- [Changelog](https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md)
- [Commits](https://github.com/tensorflow/tensorflow/compare/v2.10.1...v2.11.1)

---
updated-dependencies:
- dependency-name: tensorflow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-28 16:36:59 +08:00