Commit Graph

246 Commits

Author SHA1 Message Date
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
40d33d806b
Add compilation flag to enable/disable heap corruption check (#2766)
Heap corruption check in ems memory allocator is enabled by default
to improve the security, but it may impact the performance a lot, this
PR adds cmake variable and compiler flag to enable/disable it.
2023-11-15 17:20:50 +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
Daniel Mangum
314ecba388
Fix CMSIS import with Zephyr 3.4+ (#2744)
Fixes the import path for CMSIS in Zephyr versions > 3.4.

See https://github.com/zephyrproject-rtos/zephyr/pull/59855 for more
information.

Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
2023-11-10 11:43:48 +08:00
Wenyong Huang
20f1a8c86c Merge branch main into dev/wasi-libc-windows 2023-11-09 10:13:59 +08:00
zoraaver
fa2839a805
Fix default vprintf on UWP (#2730)
`platform_common.h` already has a declaration for BH_VPRINTF so we can
get rid of the one in `platform_internal.h`. Also add some explicit
casts to avoid MSVC compiler warnings.
2023-11-08 20:28:34 +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
77b6bce679
Move WASI types into separate header (#2724)
To allow anything to depend on WASI types, including platform-specific
data structures, move the WASI libc filesystem/clock interface into
`platform_api_extension.h`, which leaves just WASI types in
`platform_wasi.h`. And `platform_wasi.h` has been renamed to
`platform_wasi_types.h` to reflect that it only defines types now and no
function declarations. Finally, these changes allow us to remove the
`windows_fdflags` type which was essentially a duplicate of
`__wasi_fdflags_t`.
2023-11-07 09:39:15 +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
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
zoraaver
3624895204
Fix windows compilation on C++20 (#2670)
Since C++20, std::memory_order is defined as an enum class so requires
explicit casting to an int when adding enum values.
See https://en.cppreference.com/w/cpp/atomic/memory_order.
2023-10-25 20:47:56 +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
4f5ad4dc12
Apply no_sanitize_address for clang compiler in several places (#2663)
Apply `no_sanitize_address` for clang compiler in several places in which
it has been applied to gcc compiler.

And refine the comment.
2023-10-25 08:05:26 +08:00
zoraaver
75208073c0
Partial windows filesystem implementation (#2657)
Implement the necessary os_ filesystem functions to enable successful
WASI initialization on Windows. Some small changes were also required to
the sockets implementation to use the new windows_handle type. The
remaining functions will be implemented in a future PR.
2023-10-25 08:01:11 +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
Enrico Loparco
3668093053
Enable AOT usage on M1 mac (#2618) 2023-10-07 16:05:10 +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
Wenyong Huang
a50a438461
Ignore handling SIG_DFL/SIG_IGN for previous sig action (#2589) 2023-09-26 19:59:48 +08:00
Wenyong Huang
ade73f6142
Fix compilation error on Android platform (#2594)
The CI might use clang-17 to build iwasm for Android platform and it may
report compilation error:
https://github.com/bytecodealliance/wasm-micro-runtime/actions/runs/6308980430/job/17128073777

/home/runner/work/wasm-micro-runtime/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c:45:19: error: call to undeclared function 'preadv'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    ssize_t ret = preadv(fd, iov, iovcnt, offset);
                  ^

Explicitly declare preadv and pwritev in android platform header file to resolve it.
2023-09-26 16:13:12 +08:00
YAMAMOTO Takashi
6fca48d8e6
Fix inverted WASM_DISABLE_WAKEUP_BLOCKING_OP checks (#2571) 2023-09-21 11:56:21 +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
Enrico Loparco
132378f30b
Allow using custom signal handler from non-main thread (#2551)
Remove thread local attribute of prev_sig_act_SIGSEGV/SIGBUS to allow using
custom signal handler from non-main thread since in a thread spawned by
embedder, embedder may be unable to call wasm_runtime_init_thread_env to
initialize them.

And fix the handling of prev_sig_act when its sa_handler is SIG_DFL, SIG_IGN,
or a user customized handler.
2023-09-15 18:55:16 +08:00
Cengizhan Pasaoglu
af2f3c8759
Fix typo for IP address buffer (#2532) 2023-09-07 14:29:52 +08:00
YAMAMOTO Takashi
1ff41ebdc2
Implement os_usleep for posix (#2517) 2023-09-01 12:10:54 +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
Cengizhan Pasaoglu
81fbfbfcc0
Remove unnecessary and extra zero length check in mem functions' macro (#2428)
In macro bh_memcpy_s, bh_memcy_wa and bh_memmove_s, no need to do extra check
for length is zero or not because it was already done inside of the functions called.
2023-08-08 08:52:25 +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
b1fa27e91d
bh_atomic.h: Add BH_ATOMIC_32_FETCH_ADD/BH_ATOMIC_32_FETCH_SUB (#2408) 2023-07-31 17:56:15 +08:00
YAMAMOTO Takashi
6d6cea1a73
Fix non-builtin BH_ATOMIC_32_FETCH_OR and BH_ATOMIC_32_FETCH_AND (#2400) 2023-07-30 19:23:30 +08:00
YAMAMOTO Takashi
7db4815e83
bh_atomic.h: Add comments (#2398) 2023-07-28 20:36:35 +08:00
YAMAMOTO Takashi
228417ab8c
Move generic parts of wasm_suspend_flags.h to bh_atomic.h (#2393) 2023-07-27 21:37:23 +08:00
dongheng
ada7e3fe88
ESP-IDF platform supports to load AOT to PSRAM and run it (#2385) 2023-07-27 10:17:21 +08:00
Marcin Kolny
5463ea4a20
Temporarily disable code that does not compile on windows (#2386)
And return ENOSYS. We do that so we can at least compile the code on CI.
We'll be gradually enabling more and more functions.

Also, enabled `proc_raise()` for windows.
2023-07-25 10:35:05 +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
dongsheng28849455
fbe072c0d3
Bring up WAMR on esp32-s3 device (#2348)
esp32-s3's instruction memory and data memory can be accessed through mutual mirroring way,
so we define a new feature named as WASM_MEM_DUAL_BUS_MIRROR.
2023-07-21 07:54:13 +08:00
Marcin Kolny
662e38e9b0
[Partial] Build wasi-libc for Windows (#2338)
Build wasi-libc library on Windows since libuv may be not supported. This PR is a first step
to make it working, but there's still a number of changes to get it fully working.
2023-07-13 21:02:29 +08:00
Marcin Kolny
569f702ec2
Fix sanitizer errors in posix socket (#2331) 2023-07-03 14:36:01 +08:00
Misha Gridnev
1456512754
Fix a compile warning due to missing include (#2293)
Compilation in strict mode fails with
```
wasm_micro_runtime/core/shared/platform/android/platform_init.c:122:30:
  error: declaration of 'struct epoll_event` will not be visible outside of this
  function [-Werror,-Wvisibility]

  epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout,
                               ^
1 error generated.
```

Co-authored-by: Misha Gridnev <gridman@google.com>
2023-06-14 18:39:26 +08:00
Wenyong Huang
fe830d805d
Add cmake variable to disable writing gs register (#2284)
Support to disable writing x86-64 GS segment register by
  `cmake -DWAMR_DISABLE_WRITE_GS_BASE=1`
and update document. Issue was reported in #2273.
2023-06-13 10:26:25 +08:00
YAMAMOTO Takashi
4fcc056178
Fix a heap corruption bug in ems realloc (#2279) 2023-06-09 21:36:00 +08:00
YAMAMOTO Takashi
59e18ca3c0
Make hmu_tree_node 4 byte aligned to reduce compiler warning (#2268) 2023-06-07 13:02:55 +08:00
Wenyong Huang
5fb5119239
Disable writting GS register on linux-sgx platform (#2255)
Writing GS segment register is not allowed on linux-sgx since it is used as
the base address of thread data in 64-bit hw mode. Reported in #2252.
Disable writing it and disable segue optimization for linux-sgx platform.
2023-06-05 09:39:26 +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
Wenyong Huang
1e5f206464
Fix compile warnings on windows platform (#2208) 2023-05-15 13:48:48 +08:00
Wenyong Huang
5c497e5a14
Fix ems allocator unaligned memory access on riscv64 (#2140)
Make `hmu_tree_node` struct packed and add 4 padding bytes before `kfc_tree_root_buf`
field in `gc_heap_struct` struct to ensure the `left/right/parent` fields in `hmu_tree_node`
are 8-byte aligned on the 64-bit target which doesn't support unaligned memory access.

Fix the issue reported in #2136.
2023-04-26 18:13:11 +08:00