mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-18 15:41:13 +00:00
Merge pull request #3369 from bytecodealliance/main
Merge branch main into dev/checkpoint_and_restore
This commit is contained in:
commit
2a630c9589
35
.github/dependabot.yml
vendored
Normal file
35
.github/dependabot.yml
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
|
||||
- package-ecosystem: "docker"
|
||||
directory: "/.devcontainer"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
|
||||
- package-ecosystem: "devcontainers"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
|
||||
- package-ecosystem: "pip"
|
||||
directory: "/build-scripts"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
|
||||
- package-ecosystem: "pip"
|
||||
directory: "/language-bindings/python/wasm-c-api"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
|
||||
- package-ecosystem: "pip"
|
||||
directory: "/language-bindings/python/wamr-api"
|
||||
schedule:
|
||||
interval: "weekly"
|
|
@ -1,5 +1,10 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
#
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#
|
||||
|
||||
sudo apt update
|
||||
|
||||
sudo apt install -y build-essential cmake g++-multilib libgcc-11-dev lib32gcc-11-dev ccache ninja-build ccache
|
124
.github/scripts/codeql_fail_on_error.py
vendored
Executable file
124
.github/scripts/codeql_fail_on_error.py
vendored
Executable file
|
@ -0,0 +1,124 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
#
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
import requests
|
||||
|
||||
|
||||
def fetch_dismissed_alerts(repo_name, github_token):
|
||||
headers = {
|
||||
"Authorization": f"token {github_token}",
|
||||
"Accept": "application/vnd.github.v3+json",
|
||||
}
|
||||
url = (
|
||||
f"https://api.github.com/repos/{repo_name}/code-scanning/alerts?state=dismissed"
|
||||
)
|
||||
response = requests.get(url, headers=headers)
|
||||
return response.json() # This assumes a successful API call
|
||||
|
||||
|
||||
def parse_location(location):
|
||||
path = location.get("physicalLocation", {}).get("artifactLocation", {}).get("uri")
|
||||
start_line = location.get("physicalLocation", {}).get("region", {}).get("startLine")
|
||||
column_range = (
|
||||
location.get("physicalLocation", {}).get("region", {}).get("startColumn"),
|
||||
location.get("physicalLocation", {}).get("region", {}).get("endColumn"),
|
||||
)
|
||||
return (path, start_line, column_range)
|
||||
|
||||
|
||||
def is_dismissed(rule_id, path, start_line, column_range, dismissed_alerts):
|
||||
for alert in dismissed_alerts:
|
||||
alert_rule_id = alert.get("rule", {}).get("id")
|
||||
alert_path = alert.get("location", {}).get("path")
|
||||
alert_start_line = alert.get("location", {}).get("start_line")
|
||||
alert_column_range = (
|
||||
alert.get("location", {}).get("start_column"),
|
||||
alert.get("location", {}).get("end_column"),
|
||||
)
|
||||
|
||||
if (
|
||||
rule_id == alert_rule_id
|
||||
and path == alert_path
|
||||
and start_line == alert_start_line
|
||||
and column_range == alert_column_range
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# Return whether SARIF file contains error-level results
|
||||
def codeql_sarif_contain_error(filename, dismissed_alerts):
|
||||
has_error = False
|
||||
|
||||
with open(filename, "r") as f:
|
||||
s = json.load(f)
|
||||
|
||||
for run in s.get("runs", []):
|
||||
rules_metadata = run["tool"]["driver"]["rules"]
|
||||
if not rules_metadata:
|
||||
rules_metadata = run["tool"]["extensions"][0]["rules"]
|
||||
|
||||
for res in run.get("results", []):
|
||||
if "ruleIndex" in res:
|
||||
rule_index = res["ruleIndex"]
|
||||
elif "rule" in res and "index" in res["rule"]:
|
||||
rule_index = res["rule"]["index"]
|
||||
else:
|
||||
continue
|
||||
|
||||
# check whether it's dismissed before
|
||||
rule_id = res["ruleId"]
|
||||
path, start_line, column_range = parse_location(res["locations"][0])
|
||||
# the source code is from dependencies
|
||||
if "_deps" in path:
|
||||
continue
|
||||
if is_dismissed(rule_id, path, start_line, column_range, dismissed_alerts):
|
||||
print(
|
||||
f"====== Finding a dismissed entry: {rule_id} at {path}:{start_line} is dismissed.======"
|
||||
)
|
||||
print(res)
|
||||
continue
|
||||
|
||||
try:
|
||||
rule_level = rules_metadata[rule_index]["defaultConfiguration"]["level"]
|
||||
except IndexError as e:
|
||||
print(e, rule_index, len(rules_metadata))
|
||||
else:
|
||||
if rule_level == "error":
|
||||
# very likely to be an actual error
|
||||
if rules_metadata[rule_index]["properties"].get("precision") in [
|
||||
"high",
|
||||
"very-high",
|
||||
]:
|
||||
# the security severity is above medium(Common Vulnerability Scoring System (CVSS) >= 4.0)
|
||||
if "security-severity" in rules_metadata[rule_index][
|
||||
"properties"
|
||||
] and (
|
||||
float(
|
||||
rules_metadata[rule_index]["properties"][
|
||||
"security-severity"
|
||||
]
|
||||
)
|
||||
> 4.0
|
||||
):
|
||||
print("====== Finding a likely error. ======")
|
||||
print(res)
|
||||
has_error = True
|
||||
|
||||
return has_error
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
||||
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY")
|
||||
dismissed_alerts = fetch_dismissed_alerts(GITHUB_REPOSITORY, GITHUB_TOKEN)
|
||||
|
||||
if codeql_sarif_contain_error(sys.argv[1], dismissed_alerts):
|
||||
sys.exit(1)
|
9
.github/scripts/fetch_and_compare_version.py
vendored
9
.github/scripts/fetch_and_compare_version.py
vendored
|
@ -42,9 +42,12 @@ def fetch_version_from_code():
|
|||
|
||||
|
||||
def fetch_latest_git_tag():
|
||||
list_tag_cmd = (
|
||||
'git tag --list WAMR-*.*.* --sort=committerdate --format="%(refname:short)"'
|
||||
)
|
||||
"""
|
||||
Get the most recent tag from the HEAD,
|
||||
if it's main branch, it should be the latest release tag.
|
||||
if it's release/x.x.x branch, it should be the latest release tag of the branch.
|
||||
"""
|
||||
list_tag_cmd = "git describe --tags --abbrev=0 HEAD"
|
||||
p = subprocess.run(shlex.split(list_tag_cmd), capture_output=True, check=True)
|
||||
|
||||
all_tags = p.stdout.decode().strip()
|
||||
|
|
4
.github/workflows/build_wamr_lldb.yml
vendored
4
.github/workflows/build_wamr_lldb.yml
vendored
|
@ -82,9 +82,7 @@ jobs:
|
|||
- name: install utils macos
|
||||
if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'macos')
|
||||
run: |
|
||||
brew remove swig
|
||||
brew install swig@4.1 cmake ninja libedit
|
||||
brew link --overwrite swig@4.1
|
||||
brew install swig cmake ninja libedit
|
||||
sudo rm -rf /Library/Developer/CommandLineTools
|
||||
|
||||
- name: install utils ubuntu
|
||||
|
|
6
.github/workflows/build_wamr_sdk.yml
vendored
6
.github/workflows/build_wamr_sdk.yml
vendored
|
@ -58,6 +58,12 @@ jobs:
|
|||
sudo rm ${basename}
|
||||
sudo mv wasi-sdk-* wasi-sdk
|
||||
|
||||
- name: download dependencies
|
||||
run: |
|
||||
cd ./wamr-app-framework/deps
|
||||
./download.sh
|
||||
working-directory: wamr-sdk
|
||||
|
||||
- name: generate wamr-sdk release
|
||||
run: |
|
||||
cd ./wamr-app-framework/wamr-sdk
|
||||
|
|
2
.github/workflows/build_wamr_vscode_ext.yml
vendored
2
.github/workflows/build_wamr_vscode_ext.yml
vendored
|
@ -21,7 +21,7 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Use Node.js 16.x
|
||||
uses: actions/setup-node@v3
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 16.x
|
||||
|
||||
|
|
17
.github/workflows/codeql.yml
vendored
17
.github/workflows/codeql.yml
vendored
|
@ -28,7 +28,7 @@ jobs:
|
|||
# - https://gh.io/supported-runners-and-hardware-resources
|
||||
# - https://gh.io/using-larger-runners
|
||||
# Consider using larger runners for possible analysis time improvements.
|
||||
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-20.04' }}
|
||||
runs-on: ${{ (matrix.language == 'swift' && 'macos-13') || 'ubuntu-20.04' }}
|
||||
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
|
||||
permissions:
|
||||
actions: read
|
||||
|
@ -49,7 +49,7 @@ jobs:
|
|||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
|
||||
|
@ -64,9 +64,9 @@ jobs:
|
|||
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
|
||||
|
||||
- run: |
|
||||
./.github/workflows/codeql_buildscript.sh
|
||||
./.github/scripts/codeql_buildscript.sh
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
uses: github/codeql-action/analyze@v3
|
||||
with:
|
||||
category: "/language:${{matrix.language}}"
|
||||
upload: false
|
||||
|
@ -95,14 +95,14 @@ jobs:
|
|||
output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif
|
||||
|
||||
- name: Upload CodeQL results to code scanning
|
||||
uses: github/codeql-action/upload-sarif@v2
|
||||
uses: github/codeql-action/upload-sarif@v3
|
||||
with:
|
||||
sarif_file: ${{ steps.step1.outputs.sarif-output }}
|
||||
category: "/language:${{matrix.language}}"
|
||||
|
||||
- name: Upload CodeQL results as an artifact
|
||||
if: success() || failure()
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: codeql-results
|
||||
path: ${{ steps.step1.outputs.sarif-output }}
|
||||
|
@ -110,5 +110,8 @@ jobs:
|
|||
|
||||
- name: Fail if an error is found
|
||||
run: |
|
||||
./.github/workflows/codeql_fail_on_error.py \
|
||||
./.github/scripts/codeql_fail_on_error.py \
|
||||
${{ steps.step1.outputs.sarif-output }}/cpp.sarif
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
GITHUB_REPOSITORY: ${{ github.repository }}
|
||||
|
|
34
.github/workflows/codeql_fail_on_error.py
vendored
34
.github/workflows/codeql_fail_on_error.py
vendored
|
@ -1,34 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
# Return whether SARIF file contains error-level results
|
||||
def codeql_sarif_contain_error(filename):
|
||||
with open(filename, 'r') as f:
|
||||
s = json.load(f)
|
||||
|
||||
for run in s.get('runs', []):
|
||||
rules_metadata = run['tool']['driver']['rules']
|
||||
if not rules_metadata:
|
||||
rules_metadata = run['tool']['extensions'][0]['rules']
|
||||
|
||||
for res in run.get('results', []):
|
||||
if 'ruleIndex' in res:
|
||||
rule_index = res['ruleIndex']
|
||||
elif 'rule' in res and 'index' in res['rule']:
|
||||
rule_index = res['rule']['index']
|
||||
else:
|
||||
continue
|
||||
try:
|
||||
rule_level = rules_metadata[rule_index]['defaultConfiguration']['level']
|
||||
except IndexError as e:
|
||||
print(e, rule_index, len(rules_metadata))
|
||||
else:
|
||||
if rule_level == 'error':
|
||||
return True
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
if codeql_sarif_contain_error(sys.argv[1]):
|
||||
sys.exit(1)
|
|
@ -389,14 +389,14 @@ jobs:
|
|||
cd /opt
|
||||
sudo wget ${{ matrix.wasi_sdk_release }}
|
||||
sudo tar -xzf wasi-sdk-*.tar.gz
|
||||
sudo mv wasi-sdk-20.0 wasi-sdk
|
||||
sudo ln -sf wasi-sdk-20.0 wasi-sdk
|
||||
|
||||
- name: download and install wabt
|
||||
run: |
|
||||
cd /opt
|
||||
sudo wget ${{ matrix.wabt_release }}
|
||||
sudo tar -xzf wabt-1.0.31-*.tar.gz
|
||||
sudo mv wabt-1.0.31 wabt
|
||||
sudo ln -sf wabt-1.0.31 wabt
|
||||
- name: Get LLVM libraries
|
||||
id: retrieve_llvm_libs
|
||||
uses: actions/cache@v4
|
||||
|
@ -491,6 +491,13 @@ jobs:
|
|||
./iwasm wasm-apps/trap.aot | grep "#" > call_stack_aot.txt
|
||||
bash -x ../symbolicate.sh
|
||||
|
||||
- name: Build Sample [native-stack-overflow]
|
||||
run: |
|
||||
cd samples/native-stack-overflow
|
||||
./build.sh
|
||||
./run.sh test1
|
||||
./run.sh test2
|
||||
|
||||
test:
|
||||
needs:
|
||||
[
|
||||
|
|
29
.github/workflows/compilation_on_macos.yml
vendored
29
.github/workflows/compilation_on_macos.yml
vendored
|
@ -56,7 +56,7 @@ jobs:
|
|||
build_llvm_libraries_on_intel_macos:
|
||||
uses: ./.github/workflows/build_llvm_libraries.yml
|
||||
with:
|
||||
os: "macos-latest"
|
||||
os: "macos-13"
|
||||
arch: "X86"
|
||||
build_llvm_libraries_on_arm_macos:
|
||||
uses: ./.github/workflows/build_llvm_libraries.yml
|
||||
|
@ -70,7 +70,7 @@ jobs:
|
|||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: macos-latest
|
||||
- os: macos-13
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}
|
||||
steps:
|
||||
- name: checkout
|
||||
|
@ -131,7 +131,7 @@ jobs:
|
|||
"-DWAMR_BUILD_TAIL_CALL=1",
|
||||
"-DWAMR_DISABLE_HW_BOUND_CHECK=1",
|
||||
]
|
||||
os: [macos-latest]
|
||||
os: [macos-13]
|
||||
platform: [darwin]
|
||||
exclude:
|
||||
# uncompatiable feature and platform
|
||||
|
@ -173,7 +173,7 @@ jobs:
|
|||
- make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
|
||||
make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
|
||||
include:
|
||||
- os: macos-latest
|
||||
- os: macos-13
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}
|
||||
steps:
|
||||
- name: checkout
|
||||
|
@ -218,7 +218,7 @@ jobs:
|
|||
#$LLVM_EAGER_JIT_BUILD_OPTIONS,
|
||||
#$AOT_BUILD_OPTIONS,
|
||||
]
|
||||
os: [macos-latest]
|
||||
os: [macos-13]
|
||||
wasi_sdk_release:
|
||||
[
|
||||
"https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-macos.tar.gz",
|
||||
|
@ -250,7 +250,7 @@ jobs:
|
|||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest, macos-14]
|
||||
os: [macos-13, macos-14]
|
||||
wasi_sdk_release:
|
||||
[
|
||||
"https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-macos.tar.gz",
|
||||
|
@ -260,7 +260,7 @@ jobs:
|
|||
"https://github.com/WebAssembly/wabt/releases/download/1.0.31/wabt-1.0.31-macos-12.tar.gz",
|
||||
]
|
||||
include:
|
||||
- os: macos-latest
|
||||
- os: macos-13
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}
|
||||
- os: macos-14
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_arm_macos.outputs.cache_key }}
|
||||
|
@ -273,14 +273,14 @@ jobs:
|
|||
cd /opt
|
||||
sudo wget ${{ matrix.wasi_sdk_release }}
|
||||
sudo tar -xzf wasi-sdk-*.tar.gz
|
||||
sudo mv wasi-sdk-20.0 wasi-sdk
|
||||
sudo ln -sf wasi-sdk-20.0 wasi-sdk
|
||||
|
||||
- name: download and install wabt
|
||||
run: |
|
||||
cd /opt
|
||||
sudo wget ${{ matrix.wabt_release }}
|
||||
sudo tar -xzf wabt-1.0.31-*.tar.gz
|
||||
sudo mv wabt-1.0.31 wabt
|
||||
sudo ln -sf wabt-1.0.31 wabt
|
||||
|
||||
- name: Build Sample [basic]
|
||||
run: |
|
||||
|
@ -346,7 +346,7 @@ jobs:
|
|||
cmake ..
|
||||
cmake --build . --config Release --parallel 4
|
||||
working-directory: wamr-compiler
|
||||
|
||||
|
||||
- name: Build Sample [wasi-threads]
|
||||
run: |
|
||||
cd samples/wasi-threads
|
||||
|
@ -378,4 +378,11 @@ jobs:
|
|||
cmake --build . --config Debug --parallel 4
|
||||
./iwasm wasm-apps/trap.wasm | grep "#" > call_stack.txt
|
||||
./iwasm wasm-apps/trap.aot | grep "#" > call_stack_aot.txt
|
||||
bash -x ../symbolicate.sh
|
||||
bash -x ../symbolicate.sh
|
||||
|
||||
- name: Build Sample [native-stack-overflow]
|
||||
run: |
|
||||
cd samples/native-stack-overflow
|
||||
./build.sh
|
||||
./run.sh test1
|
||||
./run.sh test2
|
||||
|
|
25
.github/workflows/compilation_on_nuttx.yml
vendored
25
.github/workflows/compilation_on_nuttx.yml
vendored
|
@ -61,8 +61,6 @@ jobs:
|
|||
"boards/arm/rp2040/raspberrypi-pico/configs/nsh",
|
||||
# cortex-m7
|
||||
"boards/arm/stm32h7/nucleo-h743zi/configs/nsh",
|
||||
# riscv32imc
|
||||
"boards/risc-v/espressif/esp32c3-generic/configs/nsh",
|
||||
# riscv32gc
|
||||
"boards/risc-v/qemu-rv/rv-virt/configs/nsh",
|
||||
# riscv64gc
|
||||
|
@ -114,3 +112,26 @@ jobs:
|
|||
cd nuttx
|
||||
tools/configure.sh ${{ matrix.nuttx_board_config }}
|
||||
make -j$(nproc) EXTRAFLAGS=-Werror
|
||||
|
||||
- name: Checkout Bloaty
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
repository: google/bloaty
|
||||
submodules: recursive
|
||||
path: bloaty
|
||||
|
||||
- name: Build Bloaty
|
||||
run: |
|
||||
cmake -Bbuild -GNinja bloaty
|
||||
cmake --build build
|
||||
|
||||
- name: Size Report
|
||||
run: |
|
||||
echo "Build target: ${{ matrix.nuttx_board_config }}"
|
||||
echo "WAMR build config: ${{ matrix.wamr_config_option }}"
|
||||
echo "WAMR size:"
|
||||
build/bloaty -d compileunits --source-filter wamr nuttx/nuttx
|
||||
echo "libc-builtin size (if enabled):"
|
||||
build/bloaty -d compileunits --source-filter libc-builtin nuttx/nuttx
|
||||
echo "libc-wasi size (if enabled):"
|
||||
build/bloaty -d compileunits --source-filter libc-wasi nuttx/nuttx
|
||||
|
|
18
.github/workflows/create_tag.yml
vendored
18
.github/workflows/create_tag.yml
vendored
|
@ -32,8 +32,22 @@ jobs:
|
|||
- name: prepare
|
||||
id: preparation
|
||||
run: |
|
||||
# show latest 3 versions
|
||||
git tag --list WAMR-*.*.* --sort=committerdate --format="%(refname:short)" | tail -n 3
|
||||
# show latest 3 versions on the branch that create release
|
||||
# Set the initial commit to the head of the branch
|
||||
commit="HEAD"
|
||||
#
|
||||
# Loop to get the three most recent tags
|
||||
for i in {1..3}
|
||||
do
|
||||
# Get the most recent tag reachable from the current commit
|
||||
tag=$(git describe --tags --abbrev=0 $commit)
|
||||
|
||||
# Print the tag
|
||||
echo "$tag"
|
||||
|
||||
# Move to the commit before the found tag to find the next tag in the next iteration
|
||||
commit=$(git rev-list -n 1 $tag^)
|
||||
done
|
||||
# compare latest git tag and semantic version definition
|
||||
result=$(python3 ./.github/scripts/fetch_and_compare_version.py)
|
||||
echo "script result is ${result}"
|
||||
|
|
40
.github/workflows/nightly_run.yml
vendored
40
.github/workflows/nightly_run.yml
vendored
|
@ -8,7 +8,7 @@ on:
|
|||
types:
|
||||
- opened
|
||||
- synchronize
|
||||
# running nightly pipeline if you're changing it
|
||||
# running nightly pipeline if you're changing it
|
||||
# stress tests are run only in nightly at the moment, so running them in they are changed
|
||||
paths:
|
||||
- ".github/workflows/nightly_run.yml"
|
||||
|
@ -54,7 +54,7 @@ jobs:
|
|||
with:
|
||||
os: "ubuntu-22.04"
|
||||
arch: "X86"
|
||||
|
||||
|
||||
build_wamrc:
|
||||
needs:
|
||||
[
|
||||
|
@ -65,7 +65,7 @@ jobs:
|
|||
matrix:
|
||||
include:
|
||||
- os: ubuntu-20.04
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_ubuntu_2004.outputs.cache_key }}
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_ubuntu_2004.outputs.cache_key }}
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v4
|
||||
|
@ -459,13 +459,13 @@ jobs:
|
|||
cd /opt
|
||||
sudo wget ${{ matrix.wasi_sdk_release }}
|
||||
sudo tar -xzf wasi-sdk-*.tar.gz
|
||||
sudo mv wasi-sdk-20.0 wasi-sdk
|
||||
sudo ln -sf wasi-sdk-20.0 wasi-sdk
|
||||
- name: download and install wabt
|
||||
run: |
|
||||
cd /opt
|
||||
sudo wget ${{ matrix.wabt_release }}
|
||||
sudo tar -xzf wabt-1.0.31-*.tar.gz
|
||||
sudo mv wabt-1.0.31 wabt
|
||||
sudo ln -sf wabt-1.0.31 wabt
|
||||
|
||||
- name: Get LLVM libraries
|
||||
id: retrieve_llvm_libs
|
||||
|
@ -547,6 +547,34 @@ jobs:
|
|||
cd samples/terminate
|
||||
./build.sh
|
||||
./run.sh
|
||||
|
||||
- name: Build Sample [native-stack-overflow]
|
||||
run: |
|
||||
cd samples/native-stack-overflow
|
||||
./build.sh
|
||||
./run.sh test1
|
||||
./run.sh test2
|
||||
|
||||
- name: Build Sample [native-lib]
|
||||
run: |
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
cmake --build . --config Release --parallel 4
|
||||
./iwasm --native-lib=./libtest_add.so --native-lib=./libtest_sqrt.so --native-lib=./libtest_hello.so --native-lib=./libtest_hello2.so wasm-app/test.wasm
|
||||
working-directory: ./samples/native-lib
|
||||
|
||||
- name: checkout wamr-app-framework
|
||||
run: git clone https://github.com/bytecodealliance/wamr-app-framework.git
|
||||
- name: download wamr-app-framework dependencies
|
||||
run: LVGL=0 LV_DRIVERS=0 ./download.sh
|
||||
working-directory: ./wamr-app-framework/deps
|
||||
- name: Build Sample [simple]
|
||||
run: |
|
||||
./build.sh -p host-interp
|
||||
python3 ./sample_test_run.py $(pwd)/out
|
||||
exit $?
|
||||
working-directory: ./wamr-app-framework/samples/simple
|
||||
|
||||
test:
|
||||
needs:
|
||||
[
|
||||
|
@ -643,7 +671,7 @@ jobs:
|
|||
sudo tar -xzf wasi-sdk-*.tar.gz
|
||||
sudo mv wasi-sdk-20.0 wasi-sdk
|
||||
|
||||
# It is a temporary solution until new wasi-sdk that includes bug fixes is released
|
||||
# It is a temporary solution until new wasi-sdk that includes bug fixes is released
|
||||
- name: build wasi-libc from source
|
||||
if: matrix.test_option == '$WASI_TEST_OPTIONS'
|
||||
run: |
|
||||
|
|
10
.github/workflows/release_process.yml
vendored
10
.github/workflows/release_process.yml
vendored
|
@ -69,7 +69,7 @@ jobs:
|
|||
needs: [create_tag, create_release]
|
||||
uses: ./.github/workflows/build_llvm_libraries.yml
|
||||
with:
|
||||
os: "macos-latest"
|
||||
os: "macos-13"
|
||||
arch: "AArch64 ARM Mips RISCV X86"
|
||||
|
||||
#
|
||||
|
@ -100,7 +100,7 @@ jobs:
|
|||
with:
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_macos.outputs.cache_key }}
|
||||
release: true
|
||||
runner: macos-latest
|
||||
runner: macos-13
|
||||
upload_url: ${{ needs.create_release.outputs.upload_url }}
|
||||
ver_num: ${{ needs.create_tag.outputs.new_ver }}
|
||||
|
||||
|
@ -132,7 +132,7 @@ jobs:
|
|||
with:
|
||||
cwd: product-mini/platforms/darwin
|
||||
llvm_cache_key: ${{ needs.build_llvm_libraries_on_macos.outputs.cache_key }}
|
||||
runner: macos-latest
|
||||
runner: macos-13
|
||||
upload_url: ${{ needs.create_release.outputs.upload_url }}
|
||||
ver_num: ${{ needs.create_tag.outputs.new_ver}}
|
||||
|
||||
|
@ -165,7 +165,7 @@ jobs:
|
|||
uses: ./.github/workflows/build_wamr_sdk.yml
|
||||
with:
|
||||
config_file: wamr_config_macos_release.cmake
|
||||
runner: macos-latest
|
||||
runner: macos-13
|
||||
upload_url: ${{ needs.create_release.outputs.upload_url }}
|
||||
ver_num: ${{ needs.create_tag.outputs.new_ver}}
|
||||
wasi_sdk_url: https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-19/wasi-sdk-19.0-macos.tar.gz
|
||||
|
@ -212,7 +212,7 @@ jobs:
|
|||
needs: [create_tag, create_release]
|
||||
uses: ./.github/workflows/build_wamr_lldb.yml
|
||||
with:
|
||||
runner: macos-latest
|
||||
runner: macos-13
|
||||
arch: universal
|
||||
upload_url: ${{ needs.create_release.outputs.upload_url }}
|
||||
ver_num: ${{ needs.create_tag.outputs.new_ver}}
|
||||
|
|
5
.github/workflows/spec_test_on_nuttx.yml
vendored
5
.github/workflows/spec_test_on_nuttx.yml
vendored
|
@ -22,11 +22,14 @@ on:
|
|||
|
||||
workflow_dispatch:
|
||||
|
||||
# Note on INTERPRETERS_WAMR_STACK_GUARD_SIZE:
|
||||
# https://github.com/apache/nuttx-apps/pull/2241 is not included in
|
||||
# releases/12.4 branch as of writing this.
|
||||
env:
|
||||
LLVM_CACHE_SUFFIX: "build-llvm_libraries_ex"
|
||||
WASI_SDK_PATH: "/opt/wasi-sdk"
|
||||
WAMR_COMMON_OPTION:
|
||||
"CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_STACKSIZE=327680\\nCONFIG_INTERPRETERS_WAMR_LOG=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\nCONFIG_INTERPRETERS_WAMR_REF_TYPES=y\\nCONFIG_INTERPRETERS_WAMR_ENABLE_SPEC_TEST=y\\nCONFIG_INTERPRETERS_WAMR_SHARED_MEMORY=y\\nCONFIG_INTERPRETERS_WAMR_BULK_MEMORY=y\\nCONFIG_EOL_IS_LF=y\\nCONFIG_ARM_SEMIHOSTING_HOSTFS=y\\nCONFIG_ARM_SEMIHOSTING_HOSTFS_CACHE_COHERENCE=y\\nCONFIG_RISCV_SEMIHOSTING_HOSTFS=y\\nCONFIG_FS_HOSTFS=y\\nCONFIG_LIBC_FLOATINGPOINT=y\\n"
|
||||
"CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_STACKSIZE=327680\\nCONFIG_INTERPRETERS_WAMR_LOG=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\nCONFIG_INTERPRETERS_WAMR_REF_TYPES=y\\nCONFIG_INTERPRETERS_WAMR_ENABLE_SPEC_TEST=y\\nCONFIG_INTERPRETERS_WAMR_SHARED_MEMORY=y\\nCONFIG_INTERPRETERS_WAMR_BULK_MEMORY=y\\nCONFIG_EOL_IS_LF=y\\nCONFIG_ARM_SEMIHOSTING_HOSTFS=y\\nCONFIG_ARM_SEMIHOSTING_HOSTFS_CACHE_COHERENCE=y\\nCONFIG_RISCV_SEMIHOSTING_HOSTFS=y\\nCONFIG_FS_HOSTFS=y\\nCONFIG_LIBC_FLOATINGPOINT=y\\nCONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE=1024\\n"
|
||||
|
||||
jobs:
|
||||
build_llvm_libraries:
|
||||
|
|
36
ADOPTERS.md
Normal file
36
ADOPTERS.md
Normal file
|
@ -0,0 +1,36 @@
|
|||
# WAMR adopters
|
||||
|
||||
_If you are using WAMR in production/pre-production at your organization, please add your company name to this list.
|
||||
The list is in alphabetical order._
|
||||
|
||||
| Organization | Contact | Status | Description of Use |
|
||||
| -------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
||||
| [Alibaba](https://www.alibaba.com) | [@johnlanni](https://github.com/johnlanni) |  | Higress is a next-generation cloud-native gateway built on the core of open-source Istio + Envoy based on Alibaba's internal Envoy Gateway practice. |
|
||||
| [Amazon](https://www.amazon.com) | [@loganek](https://github.com/loganek) |  | Prime Video is a global streaming service by Amazon that provides on-demand access to a vast library of movies, TV shows, and original programming, as well as live content. |
|
||||
| [Ant Group](https://www.antgroup.com) | [@wei-tang](https://github.com/wei-tang) |  | AntChain is a blockchain technology platform owned by Ant Group, a Chinese tech conglomerate that also runs Alipay, China's largest digital payment system. |
|
||||
| [Bosch](https://www.bosch.com) | emily.ruppel@us.bosch.com |  | Silverline Cloud-Edge platform |
|
||||
| [Disney](https://www.disney.com) | |  | Disney+ Streaming |
|
||||
| [Intel](https://www.intel.com) | [@wenyongh](https://github.com/wenyongh) |  | Edge and the embedded environments |
|
||||
| [Moonbit](https://www.moonbitlang.com) | [@peter-jerry-ye](https://github.com/peter-jerry-ye) |  | MoonBit is an end-to-end programming language toolchain for cloud and edge computing using WebAssembly. |
|
||||
| [Microsoft](https://www.microsoft.com) | [@Mossaka](https://github.com/Mossaka) |  | Hyperlight runs Wasm workloads in VMs without OS and kernel, it is a solution for improving the management and security of Wasm workloads on Azure. |
|
||||
| [Midokura](https://www.midokura.com) | [@yamt](https://github.com/yamt) |  | The next-generation Edge AI sensing platform |
|
||||
| [Siemens](https://www.siemens.com) | [@ttrenner](https://github.com/ttrenner) |  | Industrial, IoT |
|
||||
| [Sony Semiconductor Solutions](https://www.sony-semicon.com) | [@dongsheng28849455](https://github.com/dongsheng28849455) |  | AI digital camera |
|
||||
| [Xiaomi](https://www.mi.com) | [@no1wudi](https://github.com/no1wudi) |  | Xiaomi Vela is Xiaomi's IoT embedded software platform based on the open-source, real-time operating system NuttX. |
|
||||
| [Xiaomi](https://www.mi.com) | [@no1wudi](https://github.com/no1wudi) |  | TEE (trusted execution environment) app engine. |
|
||||
|
||||
# Adopted in open-source projects
|
||||
|
||||
_The list is in alphabetical order._
|
||||
|
||||
| Project | Reference |
|
||||
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
||||
| [Apache Teaclave](https://github.com/apache/incubator-teaclave) | https://github.com/apache/incubator-teaclave/blob/master/docs/executing-wasm.md |
|
||||
| [Envoy](https://github.com/envoyproxy/envoy) | https://github.com/envoyproxy/envoy/blob/main/docs/root/configuration/other_features/wasm.rst |
|
||||
| [faasm](https://github.com/faasm/faasm) | https://github.com/faasm/faasm/blob/main/docs/source/wamr.md |
|
||||
| [fluent-bit](https://github.com/fluent/fluent-bit) | https://github.com/fluent/fluent-bit/tree/master/lib/wasm-micro-runtime-WAMR-1.3.0 |
|
||||
| [harfbuzz](https://github.com/harfbuzz/harfbuzz) | https://github.com/harfbuzz/harfbuzz/blob/main/docs/wasm-shaper.md#enabling-the-wasm-shaper-when-building-harfbuzz |
|
||||
| [inclave-containers](https://github.com/inclavare-containers/inclavare-containers) | https://github.com/inclavare-containers/inclavare-containers |
|
||||
| [private-data-objects](https://github.com/hyperledger-labs/private-data-objects) | https://github.com/hyperledger-labs/private-data-objects/blob/main/common/interpreter/wawaka_wasm/README.md |
|
||||
| [runwasi](https://github.com/containerd/runwasi) | https://github.com/containerd/runwasi/pull/508 (WIP) |
|
||||
| [Wasmnizer-ts](https://github.com/web-devkits/Wasmnizer-ts) | https://github.com/web-devkits/Wasmnizer-ts |
|
|
@ -3,6 +3,11 @@
|
|||
|
||||
cmake_minimum_required (VERSION 3.0)
|
||||
|
||||
if(ESP_PLATFORM)
|
||||
include (${COMPONENT_DIR}/build-scripts/esp-idf/wamr/CMakeLists.txt)
|
||||
return()
|
||||
endif()
|
||||
|
||||
project (iwasm)
|
||||
|
||||
set (CMAKE_VERBOSE_MAKEFILE OFF)
|
||||
|
|
18
README.md
18
README.md
|
@ -10,18 +10,23 @@
|
|||
[Build WAMR](./doc/build_wamr.md) | [Build AOT Compiler](./wamr-compiler/README.md) | [Embed WAMR](./doc/embed_wamr.md) | [Export Native API](./doc/export_native_api.md) | [Build Wasm Apps](./doc/build_wasm_app.md) | [Samples](./samples/README.md)
|
||||
|
||||
WebAssembly Micro Runtime (WAMR) is a lightweight standalone WebAssembly (Wasm) runtime with small footprint, high performance and highly configurable features for applications cross from embedded, IoT, edge to Trusted Execution Environment (TEE), smart contract, cloud native and so on. It includes a few parts as below:
|
||||
- [**VMcore**](./core/iwasm/): A set of runtime libraries for loading and running Wasm modules. It supports several execution modes including interpreter, Ahead-of-Time compilation(AoT) and Just-in-Time compilation (JIT). The WAMR supports two JIT tiers - Fast JIT, LLVM JIT, and dynamic tier-up from Fast JIT to LLVM JIT.
|
||||
- [**iwasm**](./product-mini/): The executable binary built with WAMR VMcore supports WASI and command line interface.
|
||||
- [**VMcore**](./core/iwasm/): A set of runtime libraries for loading and running Wasm modules. It supports rich running modes including interpreter, Ahead-of-Time compilation(AoT) and Just-in-Time compilation (JIT). WAMR supports two JIT tiers - Fast JIT, LLVM JIT, and dynamic tier-up from Fast JIT to LLVM JIT.
|
||||
- [**iwasm**](./product-mini/): The executable binary built with WAMR VMcore which supports WASI and command line interface.
|
||||
- [**wamrc**](./wamr-compiler/): The AOT compiler to compile Wasm file into AOT file
|
||||
- Useful components and tools for building real solutions with WAMR vmcore:
|
||||
- [App-framework](https://github.com/bytecodealliance/wamr-app-framework/blob/main/app-framework/README.md): A framework for supporting APIs for the Wasm applications
|
||||
- [App-manager](https://github.com/bytecodealliance/wamr-app-framework/blob/main/app-mgr/README.md): a framework for dynamical loading the Wasm module remotely
|
||||
- [App-manager](https://github.com/bytecodealliance/wamr-app-framework/blob/main/app-mgr/README.md): A framework for dynamical loading the Wasm module remotely
|
||||
- [WAMR-IDE](./test-tools/wamr-ide): An experimental VSCode extension for developping WebAssembly applications with C/C++
|
||||
|
||||
|
||||
### Key features
|
||||
- Full compliant to the W3C Wasm MVP
|
||||
- Small runtime binary size (~85K for interpreter and ~50K for AOT) and low memory usage
|
||||
- Small runtime binary size (core vmlib on cortex-m4f with tail-call/bulk memroy/shared memroy support, text size from bloaty)
|
||||
* ~58.9K for fast interpreter
|
||||
* ~56.3K for classic interpreter
|
||||
* ~29.4K for aot runtime
|
||||
* ~21.4K for libc-wasi library
|
||||
* ~3.7K for libc-builtin library
|
||||
- Near to native speed by AOT and JIT
|
||||
- Self-implemented AOT module loader to enable AOT working on Linux, Windows, MacOS, Android, SGX and MCU systems
|
||||
- Choices of Wasm application libc support: the built-in libc subset for the embedded environment or [WASI](https://github.com/WebAssembly/WASI) for the standard libc
|
||||
|
@ -60,14 +65,14 @@ The following platforms are supported, click each link below for how to build iw
|
|||
## Getting started
|
||||
- [Build VM core](./doc/build_wamr.md) and [Build wamrc AOT compiler](./wamr-compiler/README.md)
|
||||
- [Build iwasm (mini product)](./product-mini/README.md): [Linux](./product-mini/README.md#linux), [SGX](./doc/linux_sgx.md), [MacOS](./product-mini/README.md#macos) and [Windows](./product-mini/README.md#windows)
|
||||
- [Embed into C/C++](./doc/embed_wamr.md), [Embed into Python](./language-bindings/python), [Embed into Go](./language-bindings/go)
|
||||
- [Embed into C/C++](./doc/embed_wamr.md), [Embed into Python](./language-bindings/python), [Embed into Go](./language-bindings/go), [Embed in Rust](./language-bindings/rust)
|
||||
- [Register native APIs for Wasm applications](./doc/export_native_api.md)
|
||||
- [Build wamrc AOT compiler](./wamr-compiler/README.md)
|
||||
- [Build Wasm applications](./doc/build_wasm_app.md)
|
||||
- [Port WAMR to a new platform](./doc/port_wamr.md)
|
||||
- [VS Code development container](./doc/devcontainer.md)
|
||||
- [Samples](./samples) and [Benchmarks](./tests/benchmarks)
|
||||
|
||||
- [End-user APIs documentation](https://bytecodealliance.github.io/wamr.dev/apis/)
|
||||
|
||||
|
||||
### Performance and memory
|
||||
|
@ -81,7 +86,6 @@ The following platforms are supported, click each link below for how to build iw
|
|||
- [Performance and footprint data](https://github.com/bytecodealliance/wasm-micro-runtime/wiki/Performance): the performance and footprint data
|
||||
|
||||
|
||||
|
||||
Project Technical Steering Committee
|
||||
====================================
|
||||
The [WAMR PTSC Charter](./TSC_Charter.md) governs the operations of the project TSC.
|
||||
|
|
|
@ -1,3 +1,102 @@
|
|||
## WAMR-2.0.0
|
||||
|
||||
### Breaking Changes
|
||||
- The AOT ABI was changed after GC and memory64 features were introduced:
|
||||
- Implement GC feature for interpreter, AOT and LLVM-JIT (#3125)
|
||||
- Implement memory64 for classic interpreter (#3266)
|
||||
- Always allocate linear memory using mmap (#3052)
|
||||
- Refactor APIs and data structures as preliminary work for Memory64 (#3209)
|
||||
- Remove unused argument in wasm_runtime_lookup_function (#3218)
|
||||
- Separate app-manager and app-framework from WAMR (#3129)
|
||||
|
||||
### New Features
|
||||
- Implement GC feature for interpreter, AOT and LLVM-JIT (#3125)
|
||||
- Implement memory64 for classic interpreter (#3266)
|
||||
- Add wasi_ephemeral_nn module support (#3241)
|
||||
|
||||
### Bug Fixes
|
||||
- EH: Fix broken stack usage calculation (#3121)
|
||||
- Fix loader check_wasi_abi_compatibility (#3126)
|
||||
- Fix possible integer overflow in loader target block check (#3133)
|
||||
- Fix locel.set in polymorphic stack (#3135)
|
||||
- Fix threads opcodes' boundary check in classic-interp and fast-interp (#3136)
|
||||
- fast-interp: Fix copy_stack_top_i64 overlap issue (#3146)
|
||||
- Fix a ubsan complaint "applying zero offset to null pointer" (#3160)
|
||||
- fast-interp: Fix GC opcode ref.as_non_null (#3156)
|
||||
- Fix llvm jit push funcref/externref result type issue (#3169)
|
||||
- Fix wasm loader handling opcode br_table (#3176)
|
||||
- Fix ref.func opcode check when GC is enabled (#3181)
|
||||
- lldb_function_to_function_dbi: Fix a null dereference (#3189)
|
||||
- Fix compilation errors on MinGW (#3217)
|
||||
- Fix compilation errors on esp-idf platform (#3224)
|
||||
- Fix aot relocation symbols not found on windows 32-bit (#3231)
|
||||
- posix_file.c: Correct the dirfd argument that passes to fstatat (#3244)
|
||||
- Fix compilation errors on zephyr platform (#3255)
|
||||
- Fix dynamic offset not updated in op_br for block with ret type (#3269)
|
||||
- aot debug: Fix a NULL dereference (#3274)
|
||||
- thread mgr: Free aux stack only when it was allocated (#3282)
|
||||
- interp: Restore context from prev_frame after tail calling a native function (#3283)
|
||||
- Sync simd opcode definitions spec (#3290)
|
||||
- Fix posix_fadvise error handling (#3323)
|
||||
- Fix windows relocation string parsing issue (#3333)
|
||||
|
||||
### Enhancements
|
||||
- Zero the memory mapped from os_mmap in NuttX (#3132)
|
||||
- Use logger for runtime error/debug prints (#3097)
|
||||
- aot_compile_op_call: Stop setting calling convention explicitly (#3140)
|
||||
- aot compiler: Place precheck wrapper before the corresponding wrapped function (#3141)
|
||||
- Fix null pointer access in fast-interp when configurable soft bound check is enabled (#3150)
|
||||
- Clarify how to verify SGX evidence without an Intel SGX-enabled platform (#3158)
|
||||
- zephyr: Use zephyr sys_cache instead of CMSIS (#3162)
|
||||
- VSCode IDE enhancement and readme update (#3172)
|
||||
- Add vprintf override for android and esp-idf (#3174)
|
||||
- zephyr: Include math only with minimal libc (#3177)
|
||||
- zephyr: Implement Alloc_With_System_Allocator (#3179)
|
||||
- Use indirect call in pre-checker function to avoid relocation in XIP mode (#3142)
|
||||
- Implement the remaining Windows filesystem functions (#3166)
|
||||
- Fix LLVM assertion failure and update CONTRIBUTING.md (#3197)
|
||||
- Allow overriding max memory on module instantiation (#3198)
|
||||
- Get location info from function indexes in addr2line script (#3206)
|
||||
- Demangle function names in stack trace when using addr2line script (#3211)
|
||||
- Refactor APIs and data structures as preliminary work for Memory64 (#3209)
|
||||
- Allow converting the zero wasm address to native (#3215)
|
||||
- Small refactor on WASMModuleInstance and fix Go/Python language bindings (#3227)
|
||||
- Add esp32c6 support (#3234)
|
||||
- Make android platform's cmake flags configurable (#3239)
|
||||
- Go binding: Change C.long to C.int64_t when call wasm_runtime_set_wasi_args_ex (#3235)
|
||||
- Implement apis to set and get the name of a wasm module (#3254)
|
||||
- Append '\0' to every name string in aot name section (#3249)
|
||||
- Add cmake flag to control aot intrinsics (#3261)
|
||||
- Add lock and ref_count for runtime init (#3263)
|
||||
- nuttx: Migrate NuttX CMake build for WAMR (#3256)
|
||||
- LLVM 19: Switch to debug records (#3272)
|
||||
- aot debug: Process lldb_function_to_function_dbi only for C (#3278)
|
||||
- Fix warnings/issues reported in Windows and by CodeQL/Coverity (#3275)
|
||||
- Enhance wasm loading with LoadArgs and support module names (#3265)
|
||||
- Add wamr to esp-idf components registry (#3287)
|
||||
- zephyr: Add missing pthread library functions (#3291)
|
||||
- Add more checks in wasm loader (#3300)
|
||||
- Log warning if growing table failed (#3310)
|
||||
- Enhance GC subtyping checks (#3317)
|
||||
- User defined memory allocator for different purposes (#3316)
|
||||
- Add a comment on WASM_STACK_GUARD_SIZE (#3332)
|
||||
- Allow executing malloc/free from native in memory64 mode (#3315)
|
||||
- Add functions to expose module import/export info (#3330)
|
||||
|
||||
### Others
|
||||
- Add ARM MacOS to the CI (#3120)
|
||||
- Download jetstream src from github instead of browserbench.org (#3196)
|
||||
- Update document to add wamr-rust-sdk introduction (#3204)
|
||||
- Fix nightly run tsan ASLR issue (#3233)
|
||||
- Add CodeQL Workflow for Code Security Analysis (#2812)
|
||||
- Add issue templates (#3248)
|
||||
- Fix CI error when install packages for macos-14 (#3270)
|
||||
- Update document for GC, exception handling and memory64 features (#3284)
|
||||
- Update release CI (#3295)
|
||||
- Add native-stack-overflow sample (#3321)
|
||||
|
||||
---
|
||||
|
||||
## WAMR-1.3.2
|
||||
|
||||
### Breaking Changes
|
||||
|
|
|
@ -134,7 +134,9 @@ endif ()
|
|||
|
||||
# Sanitizers
|
||||
|
||||
set(WAMR_BUILD_SANITIZER $ENV{WAMR_BUILD_SANITIZER})
|
||||
if (NOT DEFINED WAMR_BUILD_SANITIZER)
|
||||
set(WAMR_BUILD_SANITIZER $ENV{WAMR_BUILD_SANITIZER})
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_SANITIZER)
|
||||
set(WAMR_BUILD_SANITIZER "")
|
||||
|
@ -554,3 +556,9 @@ else ()
|
|||
# Disable aot intrinsics for interp, fast-jit and llvm-jit
|
||||
add_definitions (-DWASM_ENABLE_AOT_INTRINSICS=0)
|
||||
endif ()
|
||||
if (WAMR_BUILD_ALLOC_WITH_USAGE EQUAL 1)
|
||||
add_definitions(-DWASM_MEM_ALLOC_WITH_USAGE=1)
|
||||
endif()
|
||||
if (NOT WAMR_BUILD_SANITIZER STREQUAL "")
|
||||
message (" Sanitizer ${WAMR_BUILD_SANITIZER} enabled")
|
||||
endif ()
|
||||
|
|
|
@ -2,56 +2,102 @@
|
|||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
# Set WAMR's build options
|
||||
if("${IDF_TARGET}" STREQUAL "esp32c3" OR "${IDF_TARGET}" STREQUAL "esp32c6")
|
||||
set(WAMR_BUILD_TARGET "RISCV32")
|
||||
else()
|
||||
set(WAMR_BUILD_TARGET "XTENSA")
|
||||
endif()
|
||||
|
||||
set(WAMR_BUILD_PLATFORM "esp-idf")
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_INTERP)
|
||||
set (WAMR_BUILD_INTERP 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
|
||||
set (WAMR_BUILD_FAST_INTERP 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_AOT)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
|
||||
set (WAMR_BUILD_LIBC_BUILTIN 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK)
|
||||
set (WAMR_BUILD_APP_FRAMEWORK 0)
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_EARLY_EXPANSION)
|
||||
if (WAMR_BUILD_TARGET STREQUAL "XTENSA")
|
||||
idf_build_set_property(COMPILE_DEFINITIONS "-DBUILD_TARGET_XTENSA=1" APPEND)
|
||||
endif ()
|
||||
if (WAMR_BUILD_INTERP)
|
||||
idf_build_set_property(COMPILE_DEFINITIONS "-DWASM_ENABLE_INTERP=1" APPEND)
|
||||
endif ()
|
||||
if (WAMR_BUILD_AOT)
|
||||
idf_build_set_property(COMPILE_DEFINITIONS "-DWASM_ENABLE_AOT=1" APPEND)
|
||||
endif ()
|
||||
|
||||
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
|
||||
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
endif()
|
||||
if (CONFIG_IDF_TARGET_ARCH_RISCV)
|
||||
set (WAMR_BUILD_TARGET "RISCV32")
|
||||
elseif (CONFIG_IDF_TARGET_ARCH_XTENSA)
|
||||
set (WAMR_BUILD_TARGET "XTENSA")
|
||||
else ()
|
||||
message (FATAL_ERROR "Arch ${CONFIG_IDF_TARGET_ARCH} is not supported")
|
||||
endif ()
|
||||
|
||||
idf_component_register(SRCS ${WAMR_RUNTIME_LIB_SOURCE} ${PLATFORM_SHARED_SOURCE}
|
||||
INCLUDE_DIRS ${IWASM_DIR}/include ${UTILS_SHARED_DIR} ${PLATFORM_SHARED_DIR} ${PLATFORM_SHARED_DIR}/../include
|
||||
REQUIRES pthread lwip esp_timer
|
||||
)
|
||||
set (WAMR_BUILD_PLATFORM "esp-idf")
|
||||
|
||||
if (CONFIG_WAMR_BUILD_DEBUG)
|
||||
set (CMAKE_BUILD_TYPE Debug)
|
||||
else ()
|
||||
set (CMAKE_BUILD_TYPE Release)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_INTERP)
|
||||
set (WAMR_BUILD_INTERP 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_INTERP_FAST)
|
||||
set (WAMR_BUILD_FAST_INTERP 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_AOT)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_LIBC_BUILTIN)
|
||||
set (WAMR_BUILD_LIBC_BUILTIN 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_INTERP_LOADER_MINI)
|
||||
set (WAMR_BUILD_MINI_LOADER 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_MULTI_MODULE)
|
||||
set (WAMR_BUILD_MULTI_MODULE 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_SHARED_MEMORY)
|
||||
set (WAMR_BUILD_SHARED_MEMORY 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_MEMORY_PROFILING)
|
||||
set (WAMR_BUILD_MEMORY_PROFILING 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_PERF_PROFILING)
|
||||
set (WAMR_BUILD_PERF_PROFILING 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_REF_TYPES)
|
||||
set (WAMR_BUILD_REF_TYPES 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_LIBC_WASI)
|
||||
set (WAMR_BUILD_LIBC_WASI 1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_LIB_PTHREAD)
|
||||
set (WAMR_BUILD_LIB_PTHREAD 1)
|
||||
endif ()
|
||||
|
||||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
|
||||
list (APPEND srcs "${WAMR_RUNTIME_LIB_SOURCE}"
|
||||
"${PLATFORM_SHARED_SOURCE}")
|
||||
|
||||
set (include_dirs "${IWASM_DIR}/include"
|
||||
"${UTILS_SHARED_DIR}"
|
||||
"${PLATFORM_SHARED_DIR}"
|
||||
"${PLATFORM_SHARED_DIR}/../include"
|
||||
"${IWASM_COMMON_DIR}")
|
||||
endif ()
|
||||
|
||||
idf_component_register(SRCS ${srcs}
|
||||
INCLUDE_DIRS ${include_dirs}
|
||||
REQUIRES pthread lwip esp_timer
|
||||
KCONFIG ${CMAKE_CURRENT_LIST_DIR}/Kconfig)
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
||||
if (CONFIG_IDF_TARGET_ARCH_RISCV)
|
||||
target_compile_definitions(${COMPONENT_LIB} PUBLIC -DBUILD_TARGET_RISCV32_ILP32=1)
|
||||
elseif (CONFIG_IDF_TARGET_ARCH_XTENSA)
|
||||
target_compile_definitions(${COMPONENT_LIB} PUBLIC -DBUILD_TARGET_XTENSA=1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_AOT)
|
||||
target_compile_definitions(${COMPONENT_LIB} PUBLIC -DWASM_ENABLE_AOT=1)
|
||||
endif ()
|
||||
|
||||
if (CONFIG_WAMR_ENABLE_INTERP)
|
||||
target_compile_definitions(${COMPONENT_LIB} PUBLIC -DWASM_ENABLE_INTERP=1)
|
||||
endif ()
|
||||
|
|
77
build-scripts/esp-idf/wamr/Kconfig
Normal file
77
build-scripts/esp-idf/wamr/Kconfig
Normal file
|
@ -0,0 +1,77 @@
|
|||
menu "WASM Micro Runtime"
|
||||
choice WAMR_BUILD_TYPE
|
||||
prompt "Build type"
|
||||
default WAMR_BUILD_RELEASE
|
||||
|
||||
config WAMR_BUILD_RELEASE
|
||||
bool "Release"
|
||||
|
||||
config WAMR_BUILD_DEBUG
|
||||
bool "Debug"
|
||||
endchoice
|
||||
|
||||
config WAMR_ENABLE_AOT
|
||||
bool "AOT"
|
||||
default y
|
||||
|
||||
menuconfig WAMR_ENABLE_INTERP
|
||||
bool "Interpreter"
|
||||
default y
|
||||
|
||||
if WAMR_ENABLE_INTERP
|
||||
|
||||
choice WAMR_INTERP_MODE
|
||||
prompt "Interpreter mode"
|
||||
default WAMR_INTERP_FAST
|
||||
|
||||
config WAMR_INTERP_CLASSIC
|
||||
bool "Classic"
|
||||
|
||||
config WAMR_INTERP_FAST
|
||||
bool "Fast"
|
||||
endchoice
|
||||
|
||||
choice WAMR_INTERP_LOADER_MODE
|
||||
prompt "Loader mode"
|
||||
default WAMR_INTERP_LOADER_NORMAL
|
||||
|
||||
config WAMR_INTERP_LOADER_NORMAL
|
||||
bool "Normal"
|
||||
|
||||
config WAMR_INTERP_LOADER_MINI
|
||||
bool "Mini"
|
||||
endchoice
|
||||
endif
|
||||
|
||||
config WAMR_ENABLE_LIB_PTHREAD
|
||||
bool "Lib pthread"
|
||||
default y
|
||||
|
||||
config WAMR_ENABLE_LIBC_BUILTIN
|
||||
bool "Libc builtin"
|
||||
default y
|
||||
|
||||
config WAMR_ENABLE_LIBC_WASI
|
||||
bool "Libc WASI"
|
||||
default y
|
||||
|
||||
config WAMR_ENABLE_MEMORY_PROFILING
|
||||
bool "Memory profiling"
|
||||
default n
|
||||
|
||||
config WAMR_ENABLE_MULTI_MODULE
|
||||
bool "Multi module"
|
||||
default n
|
||||
|
||||
config WAMR_ENABLE_PERF_PROFILING
|
||||
bool "Performance profiling"
|
||||
default n
|
||||
|
||||
config WAMR_ENABLE_REF_TYPES
|
||||
bool "Reference types"
|
||||
default n
|
||||
|
||||
config WAMR_ENABLE_SHARED_MEMORY
|
||||
bool "Shared memory"
|
||||
default n
|
||||
endmenu
|
|
@ -445,20 +445,91 @@
|
|||
#endif
|
||||
|
||||
/* Reserved bytes to the native thread stack boundary, throw native
|
||||
stack overflow exception if the guard boudary is reached */
|
||||
* stack overflow exception if the guard boudary is reached
|
||||
*
|
||||
* WASM_STACK_GUARD_SIZE needs to be large enough for:
|
||||
*
|
||||
* - native functions
|
||||
*
|
||||
* w/o hw bound check, the overhead (aot_call_function etc) + the native
|
||||
* function itself. as of writing this, the former is about 1000 bytes
|
||||
* on macOS amd64.
|
||||
*
|
||||
* with hw bound check, theoretically, only needs to cover the logic to
|
||||
* set up the jmp_buf stack.
|
||||
*
|
||||
* - aot runtime functions
|
||||
* eg. aot_enlarge_memory.
|
||||
*
|
||||
* - w/o hw bound check, the intepreter loop
|
||||
*
|
||||
* the stack consumption heavily depends on compiler settings,
|
||||
* especially for huge functions like the classic interpreter's
|
||||
* wasm_interp_call_func_bytecode:
|
||||
*
|
||||
* 200 bytes (release build, macOS/amd64)
|
||||
* 2600 bytes (debug build, macOS/amd64)
|
||||
*
|
||||
* libc snprintf (used by eg. wasm_runtime_set_exception) consumes about
|
||||
* 1600 bytes stack on macOS/amd64, about 2000 bytes on Ubuntu amd64 20.04.
|
||||
*
|
||||
* - stack check wrapper functions generated by the aot compiler
|
||||
* (--stack-bounds-checks=1)
|
||||
*
|
||||
* wamrc issues a warning
|
||||
* "precheck functions themselves consume relatively large amount of stack"
|
||||
* when it detects wrapper functions requiring more than 1KB.
|
||||
*
|
||||
* Note: on platforms with lazy function binding, don't forget to consider
|
||||
* the symbol resolution overhead on the first call. For example,
|
||||
* on Ubuntu amd64 20.04, it seems to consume about 1500 bytes.
|
||||
* For some reasons, macOS amd64 12.7.4 seems to resolve symbols eagerly.
|
||||
* (Observed with a binary with traditional non-chained fixups.)
|
||||
* The latest macOS seems to apply chained fixups in kernel on page-in time.
|
||||
* (thus it wouldn't consume userland stack.)
|
||||
*/
|
||||
#ifndef WASM_STACK_GUARD_SIZE
|
||||
#if WASM_ENABLE_UVWASI != 0
|
||||
/* UVWASI requires larger native stack */
|
||||
#define WASM_STACK_GUARD_SIZE (4096 * 6)
|
||||
#else
|
||||
#define WASM_STACK_GUARD_SIZE (1024)
|
||||
/*
|
||||
* Use a larger default for platforms like macOS/Linux.
|
||||
*
|
||||
* For example, the classic intepreter loop which ended up with a trap
|
||||
* (wasm_runtime_set_exception) would consume about 2KB stack on x86-64
|
||||
* macOS. On Ubuntu amd64 20.04, it seems to consume a bit more.
|
||||
*
|
||||
* Although product-mini/platforms/nuttx always overrides
|
||||
* WASM_STACK_GUARD_SIZE, exclude NuttX here just in case.
|
||||
*/
|
||||
#if defined(__APPLE__) || (defined(__unix__) && !defined(__NuttX__))
|
||||
#if BH_DEBUG != 0 /* assumption: BH_DEBUG matches CMAKE_BUILD_TYPE=Debug */
|
||||
#define WASM_STACK_GUARD_SIZE (1024 * 5)
|
||||
#else
|
||||
#define WASM_STACK_GUARD_SIZE (1024 * 3)
|
||||
#endif
|
||||
#else
|
||||
/*
|
||||
* Otherwise, assume very small requirement for now.
|
||||
*
|
||||
* Embedders for very small devices likely fine-tune WASM_STACK_GUARD_SIZE
|
||||
* for their specific applications anyway.
|
||||
*/
|
||||
#define WASM_STACK_GUARD_SIZE 1024
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Guard page count for stack overflow check with hardware trap */
|
||||
#ifndef STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT
|
||||
#if defined(__APPLE__) && defined(__aarch64__)
|
||||
/* Note: on macOS/iOS arm64, the user page size is 16KB */
|
||||
#define STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT 1
|
||||
#else
|
||||
#define STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT 3
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Default wasm block address cache size and conflict list size */
|
||||
#ifndef BLOCK_ADDR_CACHE_SIZE
|
||||
|
@ -587,4 +658,8 @@
|
|||
#define WASM_TABLE_MAX_SIZE 1024
|
||||
#endif
|
||||
|
||||
#ifndef WASM_MEM_ALLOC_WITH_USAGE
|
||||
#define WASM_MEM_ALLOC_WITH_USAGE 0
|
||||
#endif
|
||||
|
||||
#endif /* end of _CONFIG_H_ */
|
||||
|
|
|
@ -1525,30 +1525,42 @@ fail:
|
|||
return false;
|
||||
}
|
||||
|
||||
static void
|
||||
destroy_type(AOTType *type)
|
||||
{
|
||||
#if WASM_ENABLE_GC != 0
|
||||
if (type->ref_count > 1) {
|
||||
/* The type is referenced by other types
|
||||
of current aot module */
|
||||
type->ref_count--;
|
||||
return;
|
||||
}
|
||||
|
||||
if (type->type_flag == WASM_TYPE_FUNC) {
|
||||
AOTFuncType *func_type = (AOTFuncType *)type;
|
||||
if (func_type->ref_type_maps != NULL) {
|
||||
bh_assert(func_type->ref_type_map_count > 0);
|
||||
wasm_runtime_free(func_type->ref_type_maps);
|
||||
}
|
||||
}
|
||||
else if (type->type_flag == WASM_TYPE_STRUCT) {
|
||||
AOTStructType *struct_type = (AOTStructType *)type;
|
||||
if (struct_type->ref_type_maps != NULL) {
|
||||
bh_assert(struct_type->ref_type_map_count > 0);
|
||||
wasm_runtime_free(struct_type->ref_type_maps);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
wasm_runtime_free(type);
|
||||
}
|
||||
|
||||
static void
|
||||
destroy_types(AOTType **types, uint32 count)
|
||||
{
|
||||
uint32 i;
|
||||
for (i = 0; i < count; i++) {
|
||||
|
||||
if (types[i]) {
|
||||
#if WASM_ENABLE_GC != 0
|
||||
if (types[i]->type_flag == WASM_TYPE_FUNC) {
|
||||
AOTFuncType *func_type = (AOTFuncType *)types[i];
|
||||
if (func_type->ref_type_maps != NULL) {
|
||||
bh_assert(func_type->ref_type_map_count > 0);
|
||||
wasm_runtime_free(func_type->ref_type_maps);
|
||||
}
|
||||
}
|
||||
else if (types[i]->type_flag == WASM_TYPE_STRUCT) {
|
||||
AOTStructType *struct_type = (AOTStructType *)types[i];
|
||||
if (struct_type->ref_type_maps != NULL) {
|
||||
bh_assert(struct_type->ref_type_map_count > 0);
|
||||
wasm_runtime_free(struct_type->ref_type_maps);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
wasm_runtime_free(types[i]);
|
||||
destroy_type(types[i]);
|
||||
}
|
||||
}
|
||||
wasm_runtime_free(types);
|
||||
|
@ -1556,14 +1568,17 @@ destroy_types(AOTType **types, uint32 count)
|
|||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
static void
|
||||
init_base_type(AOTType *base_type, uint16 type_flag, bool is_sub_final,
|
||||
uint32 parent_type_idx, uint16 rec_count, uint16 rec_idx)
|
||||
init_base_type(AOTType *base_type, uint32 type_idx, uint16 type_flag,
|
||||
bool is_sub_final, uint32 parent_type_idx, uint16 rec_count,
|
||||
uint16 rec_idx)
|
||||
{
|
||||
base_type->type_flag = type_flag;
|
||||
base_type->ref_count = 1;
|
||||
base_type->is_sub_final = is_sub_final;
|
||||
base_type->parent_type_idx = parent_type_idx;
|
||||
base_type->rec_count = rec_count;
|
||||
base_type->rec_idx = rec_idx;
|
||||
base_type->rec_begin_type_idx = type_idx - rec_idx;
|
||||
}
|
||||
|
||||
static bool
|
||||
|
@ -1576,7 +1591,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
uint32 i, j;
|
||||
uint32 type_flag, param_cell_num, ret_cell_num;
|
||||
uint16 param_count, result_count, ref_type_map_count, rec_count, rec_idx;
|
||||
bool is_sub_final;
|
||||
bool is_equivalence_type, is_sub_final;
|
||||
uint32 parent_type_idx;
|
||||
WASMRefType ref_type;
|
||||
|
||||
|
@ -1590,12 +1605,31 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
|
||||
/* Create each type */
|
||||
for (i = 0; i < module->type_count; i++) {
|
||||
|
||||
buf = align_ptr(buf, 4);
|
||||
|
||||
/* Read base type info */
|
||||
read_uint16(buf, buf_end, type_flag);
|
||||
read_uint16(buf, buf_end, is_sub_final);
|
||||
|
||||
read_uint8(buf, buf_end, is_equivalence_type);
|
||||
/* If there is an equivalence type, re-use it */
|
||||
if (is_equivalence_type) {
|
||||
uint8 u8;
|
||||
/* padding */
|
||||
read_uint8(buf, buf_end, u8);
|
||||
(void)u8;
|
||||
|
||||
read_uint32(buf, buf_end, j);
|
||||
if (module->types[j]->ref_count == UINT16_MAX) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"wasm type's ref count too large");
|
||||
goto fail;
|
||||
}
|
||||
module->types[j]->ref_count++;
|
||||
module->types[i] = module->types[j];
|
||||
continue;
|
||||
}
|
||||
|
||||
read_uint8(buf, buf_end, is_sub_final);
|
||||
read_uint32(buf, buf_end, parent_type_idx);
|
||||
read_uint16(buf, buf_end, rec_count);
|
||||
read_uint16(buf, buf_end, rec_idx);
|
||||
|
@ -1620,7 +1654,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
|
||||
types[i] = (AOTType *)func_type;
|
||||
|
||||
init_base_type((AOTType *)func_type, type_flag, is_sub_final,
|
||||
init_base_type((AOTType *)func_type, i, type_flag, is_sub_final,
|
||||
parent_type_idx, rec_count, rec_idx);
|
||||
func_type->param_count = param_count;
|
||||
func_type->result_count = result_count;
|
||||
|
@ -1726,7 +1760,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
offset = (uint32)offsetof(WASMStructObject, field_data);
|
||||
types[i] = (AOTType *)struct_type;
|
||||
|
||||
init_base_type((AOTType *)struct_type, type_flag, is_sub_final,
|
||||
init_base_type((AOTType *)struct_type, i, type_flag, is_sub_final,
|
||||
parent_type_idx, rec_count, rec_idx);
|
||||
struct_type->field_count = field_count;
|
||||
struct_type->ref_type_map_count = ref_type_map_count;
|
||||
|
@ -1812,7 +1846,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
|
||||
types[i] = (AOTType *)array_type;
|
||||
|
||||
init_base_type((AOTType *)array_type, type_flag, is_sub_final,
|
||||
init_base_type((AOTType *)array_type, i, type_flag, is_sub_final,
|
||||
parent_type_idx, rec_count, rec_idx);
|
||||
read_uint16(buf, buf_end, array_type->elem_flags);
|
||||
read_uint8(buf, buf_end, array_type->elem_type);
|
||||
|
@ -1841,7 +1875,6 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
if (rec_count == 0) {
|
||||
bh_assert(rec_idx == 0);
|
||||
}
|
||||
|
||||
for (j = i - rec_idx; j <= i; j++) {
|
||||
AOTType *cur_type = module->types[j];
|
||||
parent_type_idx = cur_type->parent_type_idx;
|
||||
|
@ -1850,6 +1883,11 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
|
||||
module->types[j]->parent_type = parent_type;
|
||||
module->types[j]->root_type = parent_type->root_type;
|
||||
if (parent_type->inherit_depth == UINT16_MAX) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"parent type's inherit depth too large");
|
||||
goto fail;
|
||||
}
|
||||
module->types[j]->inherit_depth =
|
||||
parent_type->inherit_depth + 1;
|
||||
}
|
||||
|
@ -1867,7 +1905,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
|||
AOTType *parent_type = module->types[parent_type_idx];
|
||||
/* subtyping has been checked during compilation */
|
||||
bh_assert(wasm_type_is_subtype_of(
|
||||
module->types[j], parent_type, module->types, i));
|
||||
module->types[j], parent_type, module->types, i + 1));
|
||||
(void)parent_type;
|
||||
}
|
||||
}
|
||||
|
@ -3257,37 +3295,26 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
|
|||
bh_memcpy_s(symbol_name_buf, (uint32)sizeof(symbol_name_buf),
|
||||
symbol_name, symbol_name_len);
|
||||
|
||||
if ((group_name_len == strlen(".text")
|
||||
|| (module->is_indirect_mode
|
||||
&& group_name_len == strlen(".text") + 1))
|
||||
/* aot compiler emits string with '\0' since 2.0.0 */
|
||||
if (group_name_len == strlen(".text") + 1
|
||||
&& !strncmp(group_name, ".text", strlen(".text"))) {
|
||||
if ((symbol_name_len == strlen(YMM_PLT_PREFIX) + 64
|
||||
|| (module->is_indirect_mode
|
||||
&& symbol_name_len == strlen(YMM_PLT_PREFIX) + 64 + 1))
|
||||
/* aot compiler emits string with '\0' since 2.0.0 */
|
||||
if (symbol_name_len == strlen(YMM_PLT_PREFIX) + 64 + 1
|
||||
&& !strncmp(symbol_name, YMM_PLT_PREFIX,
|
||||
strlen(YMM_PLT_PREFIX))) {
|
||||
module->ymm_plt_count++;
|
||||
}
|
||||
else if ((symbol_name_len == strlen(XMM_PLT_PREFIX) + 32
|
||||
|| (module->is_indirect_mode
|
||||
&& symbol_name_len
|
||||
== strlen(XMM_PLT_PREFIX) + 32 + 1))
|
||||
else if (symbol_name_len == strlen(XMM_PLT_PREFIX) + 32 + 1
|
||||
&& !strncmp(symbol_name, XMM_PLT_PREFIX,
|
||||
strlen(XMM_PLT_PREFIX))) {
|
||||
module->xmm_plt_count++;
|
||||
}
|
||||
else if ((symbol_name_len == strlen(REAL_PLT_PREFIX) + 16
|
||||
|| (module->is_indirect_mode
|
||||
&& symbol_name_len
|
||||
== strlen(REAL_PLT_PREFIX) + 16 + 1))
|
||||
else if (symbol_name_len == strlen(REAL_PLT_PREFIX) + 16 + 1
|
||||
&& !strncmp(symbol_name, REAL_PLT_PREFIX,
|
||||
strlen(REAL_PLT_PREFIX))) {
|
||||
module->real_plt_count++;
|
||||
}
|
||||
else if ((symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8
|
||||
|| (module->is_indirect_mode
|
||||
&& symbol_name_len
|
||||
== strlen(REAL_PLT_PREFIX) + 8 + 1))
|
||||
else if (symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8 + 1
|
||||
&& !strncmp(symbol_name, REAL_PLT_PREFIX,
|
||||
strlen(REAL_PLT_PREFIX))) {
|
||||
module->float_plt_count++;
|
||||
|
|
|
@ -143,6 +143,7 @@ typedef struct {
|
|||
REG_SYM(aot_array_init_with_data), \
|
||||
REG_SYM(aot_create_func_obj), \
|
||||
REG_SYM(aot_obj_is_instance_of), \
|
||||
REG_SYM(aot_func_type_is_super_of), \
|
||||
REG_SYM(aot_rtt_type_new), \
|
||||
REG_SYM(wasm_array_obj_copy), \
|
||||
REG_SYM(wasm_array_obj_new), \
|
||||
|
|
|
@ -1721,6 +1721,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
|
|||
|
||||
bh_assert(table_init_data);
|
||||
|
||||
bh_assert(table_init_data->table_index < module_inst->table_count);
|
||||
table = module_inst->tables[table_init_data->table_index];
|
||||
bh_assert(table);
|
||||
|
||||
|
@ -1728,8 +1729,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
|
|||
bh_assert(table_data);
|
||||
|
||||
wasm_runtime_get_table_inst_elem_type(
|
||||
(WASMModuleInstanceCommon *)module_inst, i, &tbl_elem_type,
|
||||
&tbl_elem_ref_type, &tbl_init_size, &tbl_max_size);
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
table_init_data->table_index, &tbl_elem_type, &tbl_elem_ref_type,
|
||||
&tbl_init_size, &tbl_max_size);
|
||||
|
||||
if (!wasm_elem_is_declarative(table_init_data->mode)
|
||||
&& !wasm_reftype_is_subtype_of(
|
||||
|
@ -1965,8 +1967,6 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
|
|||
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
|
||||
uint32 page_size = os_getpagesize();
|
||||
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
|
||||
#ifdef BH_PLATFORM_WINDOWS
|
||||
int result;
|
||||
bool has_exception;
|
||||
|
@ -1977,10 +1977,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
|
|||
/* Check native stack overflow firstly to ensure we have enough
|
||||
native stack to run the following codes before actually calling
|
||||
the aot function in invokeNative function. */
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&module_inst);
|
||||
if ((uint8 *)&module_inst < exec_env->native_stack_boundary
|
||||
+ page_size * (guard_page_count + 1)) {
|
||||
aot_set_exception_with_id(module_inst, EXCE_NATIVE_STACK_OVERFLOW);
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -2788,9 +2785,7 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
|
|||
exec_env->native_stack_boundary must have been set, we don't set
|
||||
it again */
|
||||
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&module_inst);
|
||||
if ((uint8 *)&module_inst < exec_env->native_stack_boundary) {
|
||||
aot_set_exception_with_id(module_inst, EXCE_NATIVE_STACK_OVERFLOW);
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
|
@ -3366,39 +3361,49 @@ aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length,
|
|||
}
|
||||
|
||||
uint32
|
||||
aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx,
|
||||
uint32 inc_entries, table_elem_type_t init_val)
|
||||
aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 inc_size,
|
||||
table_elem_type_t init_val)
|
||||
{
|
||||
uint32 entry_count, i, orig_tbl_sz;
|
||||
AOTTableInstance *tbl_inst;
|
||||
uint32 i, orig_size, total_size;
|
||||
|
||||
tbl_inst = module_inst->tables[tbl_idx];
|
||||
if (!tbl_inst) {
|
||||
return (uint32)-1;
|
||||
}
|
||||
|
||||
orig_tbl_sz = tbl_inst->cur_size;
|
||||
orig_size = tbl_inst->cur_size;
|
||||
|
||||
if (!inc_entries) {
|
||||
return orig_tbl_sz;
|
||||
if (!inc_size) {
|
||||
return orig_size;
|
||||
}
|
||||
|
||||
if (tbl_inst->cur_size > UINT32_MAX - inc_entries) {
|
||||
if (tbl_inst->cur_size > UINT32_MAX - inc_size) {
|
||||
#if WASM_ENABLE_SPEC_TEST == 0
|
||||
LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
|
||||
") failed because of integer overflow",
|
||||
tbl_inst->cur_size, inc_size);
|
||||
#endif
|
||||
return (uint32)-1;
|
||||
}
|
||||
|
||||
entry_count = tbl_inst->cur_size + inc_entries;
|
||||
if (entry_count > tbl_inst->max_size) {
|
||||
total_size = tbl_inst->cur_size + inc_size;
|
||||
if (total_size > tbl_inst->max_size) {
|
||||
#if WASM_ENABLE_SPEC_TEST == 0
|
||||
LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
|
||||
") failed because of over max size",
|
||||
tbl_inst->cur_size, inc_size);
|
||||
#endif
|
||||
return (uint32)-1;
|
||||
}
|
||||
|
||||
/* fill in */
|
||||
for (i = 0; i < inc_entries; ++i) {
|
||||
for (i = 0; i < inc_size; ++i) {
|
||||
tbl_inst->elems[tbl_inst->cur_size + i] = init_val;
|
||||
}
|
||||
|
||||
tbl_inst->cur_size = entry_count;
|
||||
return orig_tbl_sz;
|
||||
tbl_inst->cur_size = total_size;
|
||||
return orig_size;
|
||||
}
|
||||
#endif /* WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
|
||||
|
||||
|
@ -4477,6 +4482,22 @@ aot_obj_is_instance_of(AOTModuleInstance *module_inst, WASMObjectRef gc_obj,
|
|||
return wasm_obj_is_instance_of(gc_obj, type_index, types, type_count);
|
||||
}
|
||||
|
||||
bool
|
||||
aot_func_type_is_super_of(AOTModuleInstance *module_inst, uint32 type_idx1,
|
||||
uint32 type_idx2)
|
||||
{
|
||||
AOTModule *aot_module = (AOTModule *)module_inst->module;
|
||||
AOTType **types = aot_module->types;
|
||||
|
||||
if (type_idx1 == type_idx2)
|
||||
return true;
|
||||
|
||||
bh_assert(types[type_idx1]->type_flag == WASM_TYPE_FUNC);
|
||||
bh_assert(types[type_idx2]->type_flag == WASM_TYPE_FUNC);
|
||||
return wasm_func_type_is_super_of((WASMFuncType *)types[type_idx1],
|
||||
(WASMFuncType *)types[type_idx2]);
|
||||
}
|
||||
|
||||
WASMRttTypeRef
|
||||
aot_rtt_type_new(AOTModuleInstance *module_inst, uint32 type_index)
|
||||
{
|
||||
|
|
|
@ -752,6 +752,11 @@ bool
|
|||
aot_obj_is_instance_of(AOTModuleInstance *module_inst, WASMObjectRef gc_obj,
|
||||
uint32 type_index);
|
||||
|
||||
/* Whether func type1 is one of super types of func type2 */
|
||||
bool
|
||||
aot_func_type_is_super_of(AOTModuleInstance *module_inst, uint32 type_idx1,
|
||||
uint32 type_idx2);
|
||||
|
||||
WASMRttTypeRef
|
||||
aot_rtt_type_new(AOTModuleInstance *module_inst, uint32 type_index);
|
||||
|
||||
|
|
|
@ -304,7 +304,12 @@ wasm_defined_type_equal(WASMType *const def_type1, WASMType *const def_type2,
|
|||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
/* TODO */
|
||||
if (module->module_type == Wasm_Module_AoT) {
|
||||
AOTModule *aot_module = (AOTModule *)module;
|
||||
|
||||
types = aot_module->types;
|
||||
type_count = aot_module->type_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
bh_assert(types);
|
||||
|
|
|
@ -250,6 +250,51 @@ wasm_value_types_is_subtype_of(const uint8 *types1,
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
rec_ref_type_equal(const WASMRefType *ref_type1, const WASMRefType *ref_type2,
|
||||
uint32 rec_begin_type_idx1, uint32 rec_begin_type_idx2,
|
||||
uint32 rec_count, const WASMTypePtr *types,
|
||||
uint32 type_count)
|
||||
{
|
||||
uint32 type_idx1, type_idx2;
|
||||
|
||||
if (!wasm_is_refheaptype_typeidx(&ref_type1->ref_ht_common)
|
||||
|| !wasm_is_refheaptype_typeidx(&ref_type2->ref_ht_common))
|
||||
return ref_type1->ref_ht_common.heap_type
|
||||
== ref_type2->ref_ht_common.heap_type
|
||||
? true
|
||||
: false;
|
||||
|
||||
/* Now both ref types are type of (ref type_idx) */
|
||||
type_idx1 = ref_type1->ref_ht_typeidx.type_idx;
|
||||
type_idx2 = ref_type2->ref_ht_typeidx.type_idx;
|
||||
|
||||
if (type_idx1 >= rec_begin_type_idx1
|
||||
&& type_idx1 < rec_begin_type_idx1 + rec_count) {
|
||||
/* The converted iso-recursive types should be the same */
|
||||
bool ret = (type_idx2 >= rec_begin_type_idx2
|
||||
&& type_idx2 < rec_begin_type_idx2 + rec_count
|
||||
&& type_idx1 - rec_begin_type_idx1
|
||||
== type_idx2 - rec_begin_type_idx2)
|
||||
? true
|
||||
: false;
|
||||
return ret;
|
||||
}
|
||||
else if (type_idx2 >= rec_begin_type_idx2
|
||||
&& type_idx2 < rec_begin_type_idx2 + rec_count) {
|
||||
/* The converted iso-recursive types should be the same */
|
||||
bool ret = (type_idx1 >= rec_begin_type_idx1
|
||||
&& type_idx1 < rec_begin_type_idx1 + rec_count
|
||||
&& type_idx1 - rec_begin_type_idx1
|
||||
== type_idx2 - rec_begin_type_idx2)
|
||||
? true
|
||||
: false;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return types[type_idx1] == types[type_idx2] ? true : false;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_func_type_equal(const WASMFuncType *type1, const WASMFuncType *type2,
|
||||
const WASMTypePtr *types, uint32 type_count)
|
||||
|
@ -277,9 +322,11 @@ wasm_func_type_equal(const WASMFuncType *type1, const WASMFuncType *type2,
|
|||
|
||||
ref_type1 = type1->ref_type_maps[j].ref_type;
|
||||
ref_type2 = type2->ref_type_maps[j].ref_type;
|
||||
if (!wasm_reftype_equal(ref_type1->ref_type, ref_type1,
|
||||
ref_type2->ref_type, ref_type2, types,
|
||||
type_count))
|
||||
|
||||
if (!rec_ref_type_equal(
|
||||
ref_type1, ref_type2, type1->base_type.rec_begin_type_idx,
|
||||
type2->base_type.rec_begin_type_idx,
|
||||
type1->base_type.rec_count, types, type_count))
|
||||
return false;
|
||||
|
||||
j++;
|
||||
|
@ -316,9 +363,11 @@ wasm_struct_type_equal(const WASMStructType *type1, const WASMStructType *type2,
|
|||
|
||||
ref_type1 = type1->ref_type_maps[j].ref_type;
|
||||
ref_type2 = type2->ref_type_maps[j].ref_type;
|
||||
if (!wasm_reftype_equal(ref_type1->ref_type, ref_type1,
|
||||
ref_type2->ref_type, ref_type2, types,
|
||||
type_count))
|
||||
|
||||
if (!rec_ref_type_equal(
|
||||
ref_type1, ref_type2, type1->base_type.rec_begin_type_idx,
|
||||
type2->base_type.rec_begin_type_idx,
|
||||
type1->base_type.rec_count, types, type_count))
|
||||
return false;
|
||||
|
||||
j++;
|
||||
|
@ -338,21 +387,67 @@ wasm_array_type_equal(const WASMArrayType *type1, const WASMArrayType *type2,
|
|||
if (type1->elem_flags != type2->elem_flags)
|
||||
return false;
|
||||
|
||||
return wasm_reftype_equal(type1->elem_type, type1->elem_ref_type,
|
||||
type2->elem_type, type2->elem_ref_type, types,
|
||||
type_count);
|
||||
if (type1->elem_type != type2->elem_type)
|
||||
return false;
|
||||
|
||||
if (!wasm_is_type_multi_byte_type(type1->elem_type))
|
||||
return true;
|
||||
|
||||
return rec_ref_type_equal(type1->elem_ref_type, type2->elem_ref_type,
|
||||
type1->base_type.rec_begin_type_idx,
|
||||
type2->base_type.rec_begin_type_idx,
|
||||
type1->base_type.rec_count, types, type_count);
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_type_equal(const WASMType *type1, const WASMType *type2,
|
||||
const WASMTypePtr *types, uint32 type_count)
|
||||
{
|
||||
uint32 rec_begin_type_idx1 = type1->rec_begin_type_idx;
|
||||
uint32 rec_begin_type_idx2 = type2->rec_begin_type_idx;
|
||||
uint32 parent_type_idx1, parent_type_idx2, rec_count;
|
||||
|
||||
if (type1 == type2)
|
||||
return true;
|
||||
|
||||
if (type1->type_flag != type2->type_flag)
|
||||
if (!(type1->type_flag == type2->type_flag
|
||||
&& type1->is_sub_final == type2->is_sub_final
|
||||
&& type1->rec_count == type2->rec_count
|
||||
&& type1->rec_idx == type2->rec_idx))
|
||||
return false;
|
||||
|
||||
rec_count = type1->rec_count;
|
||||
|
||||
parent_type_idx1 = type1->parent_type_idx;
|
||||
parent_type_idx2 = type2->parent_type_idx;
|
||||
|
||||
if (parent_type_idx1 >= rec_begin_type_idx1
|
||||
&& parent_type_idx1 < rec_begin_type_idx1 + rec_count) {
|
||||
/* The converted iso-recursive types should be the same */
|
||||
if (!(parent_type_idx2 >= rec_begin_type_idx2
|
||||
&& parent_type_idx2 < rec_begin_type_idx2 + rec_count
|
||||
&& parent_type_idx1 - rec_begin_type_idx1
|
||||
== parent_type_idx2 - rec_begin_type_idx2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (parent_type_idx2 >= rec_begin_type_idx2
|
||||
&& parent_type_idx2 < rec_begin_type_idx2 + rec_count) {
|
||||
/* The converted iso-recursive types should be the same */
|
||||
if (!(parent_type_idx1 >= rec_begin_type_idx1
|
||||
&& parent_type_idx1 < rec_begin_type_idx1 + rec_count
|
||||
&& parent_type_idx1 - rec_begin_type_idx1
|
||||
== parent_type_idx2 - rec_begin_type_idx2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (type1->parent_type != type2->parent_type) {
|
||||
/* The parent types should be same since they have been
|
||||
normalized and equivalence types with different type
|
||||
indexes are referring to a same WASMType */
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wasm_type_is_func_type(type1))
|
||||
return wasm_func_type_equal((WASMFuncType *)type1,
|
||||
(WASMFuncType *)type2, types, type_count);
|
||||
|
@ -653,12 +748,6 @@ wasm_reftype_struct_size(const WASMRefType *ref_type)
|
|||
return (uint32)sizeof(RefHeapType_Common);
|
||||
}
|
||||
|
||||
static bool
|
||||
type_idx_equal(uint32 type_idx1, uint32 type_idx2)
|
||||
{
|
||||
return (type_idx1 == type_idx2) ? true : false;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_refheaptype_equal(const RefHeapType_Common *ref_heap_type1,
|
||||
const RefHeapType_Common *ref_heap_type2,
|
||||
|
@ -673,8 +762,16 @@ wasm_refheaptype_equal(const RefHeapType_Common *ref_heap_type1,
|
|||
if (ref_heap_type1->heap_type != ref_heap_type2->heap_type) {
|
||||
if (wasm_is_refheaptype_typeidx(ref_heap_type1)
|
||||
&& wasm_is_refheaptype_typeidx(ref_heap_type2)) {
|
||||
return type_idx_equal(ref_heap_type1->heap_type,
|
||||
ref_heap_type2->heap_type);
|
||||
if (ref_heap_type1->heap_type == ref_heap_type2->heap_type)
|
||||
return true;
|
||||
else
|
||||
/* the type_count may be 0 when called from reftype_equal */
|
||||
return ((uint32)ref_heap_type1->heap_type < type_count
|
||||
&& (uint32)ref_heap_type2->heap_type < type_count
|
||||
&& types[ref_heap_type1->heap_type]
|
||||
== types[ref_heap_type2->heap_type])
|
||||
? true
|
||||
: false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -835,6 +932,13 @@ wasm_type_is_supers_of(const WASMType *type1, const WASMType *type2)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_func_type_is_super_of(const WASMFuncType *type1, const WASMFuncType *type2)
|
||||
{
|
||||
return wasm_type_is_supers_of((const WASMType *)type1,
|
||||
(const WASMType *)type2);
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_reftype_is_subtype_of(uint8 type1, const WASMRefType *ref_type1,
|
||||
uint8 type2, const WASMRefType *ref_type2,
|
||||
|
@ -914,14 +1018,15 @@ wasm_reftype_is_subtype_of(uint8 type1, const WASMRefType *ref_type1,
|
|||
#endif
|
||||
else if (type1 == REF_TYPE_HT_NULLABLE) {
|
||||
if (wasm_is_refheaptype_typeidx(&ref_type1->ref_ht_common)) {
|
||||
bh_assert((uint32)ref_type1->ref_ht_typeidx.type_idx < type_count);
|
||||
/* reftype1 is (ref null $t) */
|
||||
if (type2 == REF_TYPE_HT_NULLABLE && ref_type2 != NULL
|
||||
&& wasm_is_refheaptype_typeidx(&ref_type2->ref_ht_common)) {
|
||||
return type_idx_equal(ref_type1->ref_ht_typeidx.type_idx,
|
||||
ref_type2->ref_ht_typeidx.type_idx)
|
||||
|| wasm_type_is_supers_of(
|
||||
types[ref_type2->ref_ht_typeidx.type_idx],
|
||||
types[ref_type1->ref_ht_typeidx.type_idx]);
|
||||
bh_assert((uint32)ref_type2->ref_ht_typeidx.type_idx
|
||||
< type_count);
|
||||
return wasm_type_is_supers_of(
|
||||
types[ref_type2->ref_ht_typeidx.type_idx],
|
||||
types[ref_type1->ref_ht_typeidx.type_idx]);
|
||||
}
|
||||
else if (types[ref_type1->ref_ht_typeidx.type_idx]->type_flag
|
||||
== WASM_TYPE_STRUCT)
|
||||
|
@ -963,16 +1068,17 @@ wasm_reftype_is_subtype_of(uint8 type1, const WASMRefType *ref_type1,
|
|||
else if (type1 == REF_TYPE_HT_NON_NULLABLE) {
|
||||
bh_assert(ref_type1);
|
||||
if (wasm_is_refheaptype_typeidx(&ref_type1->ref_ht_common)) {
|
||||
bh_assert((uint32)ref_type1->ref_ht_typeidx.type_idx < type_count);
|
||||
/* reftype1 is (ref $t) */
|
||||
if ((type2 == REF_TYPE_HT_NULLABLE
|
||||
|| type2 == REF_TYPE_HT_NON_NULLABLE)
|
||||
&& ref_type2 != NULL
|
||||
&& wasm_is_refheaptype_typeidx(&ref_type2->ref_ht_common)) {
|
||||
return type_idx_equal(ref_type1->ref_ht_typeidx.type_idx,
|
||||
ref_type2->ref_ht_typeidx.type_idx)
|
||||
|| wasm_type_is_supers_of(
|
||||
types[ref_type2->ref_ht_typeidx.type_idx],
|
||||
types[ref_type1->ref_ht_typeidx.type_idx]);
|
||||
bh_assert((uint32)ref_type2->ref_ht_typeidx.type_idx
|
||||
< type_count);
|
||||
return wasm_type_is_supers_of(
|
||||
types[ref_type2->ref_ht_typeidx.type_idx],
|
||||
types[ref_type1->ref_ht_typeidx.type_idx]);
|
||||
}
|
||||
else if (types[ref_type1->ref_ht_typeidx.type_idx]->type_flag
|
||||
== WASM_TYPE_STRUCT) {
|
||||
|
|
|
@ -47,6 +47,12 @@ wasm_func_type_is_subtype_of(const WASMFuncType *type1,
|
|||
const WASMFuncType *type2,
|
||||
const WASMTypePtr *types, uint32 type_count);
|
||||
|
||||
/* Whether func type1 is one of super types of func type2,
|
||||
used for the func type check in call_indirect/call_ref opcodes */
|
||||
bool
|
||||
wasm_func_type_is_super_of(const WASMFuncType *type1,
|
||||
const WASMFuncType *type2);
|
||||
|
||||
/* Whether func type1's result types are subtype of
|
||||
func type2's result types */
|
||||
bool
|
||||
|
|
|
@ -578,8 +578,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
|
|||
is_anyref = true;
|
||||
}
|
||||
|
||||
if (wasm_is_type_multi_byte_type(
|
||||
type->types[type->param_count + i])) {
|
||||
if (wasm_is_type_multi_byte_type(type->types[i])) {
|
||||
WASMRefType *ref_type = ref_type_map->ref_type;
|
||||
if (wasm_is_refheaptype_common(
|
||||
&ref_type->ref_ht_common)) {
|
||||
|
|
|
@ -29,16 +29,35 @@ static void *enlarge_memory_error_user_data;
|
|||
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
static void *allocator_user_data = NULL;
|
||||
static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
|
||||
static void *(*realloc_func)(void *user_data, void *ptr,
|
||||
unsigned int size) = NULL;
|
||||
static void (*free_func)(void *user_data, void *ptr) = NULL;
|
||||
#else
|
||||
static void *(*malloc_func)(unsigned int size) = NULL;
|
||||
static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
|
||||
static void (*free_func)(void *ptr) = NULL;
|
||||
#endif
|
||||
|
||||
static void *(*malloc_func)(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
mem_alloc_usage_t usage,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
void *user_data,
|
||||
#endif
|
||||
unsigned int size) = NULL;
|
||||
|
||||
static void *(*realloc_func)(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
mem_alloc_usage_t usage, bool full_size_mmaped,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
void *user_data,
|
||||
#endif
|
||||
void *ptr, unsigned int size) = NULL;
|
||||
|
||||
static void (*free_func)(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
mem_alloc_usage_t usage,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
void *user_data,
|
||||
#endif
|
||||
void *ptr) = NULL;
|
||||
|
||||
static unsigned int global_pool_size;
|
||||
|
||||
static uint64
|
||||
|
@ -177,11 +196,14 @@ wasm_runtime_malloc_internal(unsigned int size)
|
|||
return mem_allocator_malloc(pool_allocator, size);
|
||||
}
|
||||
else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
return malloc_func(allocator_user_data, size);
|
||||
#else
|
||||
return malloc_func(size);
|
||||
return malloc_func(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
Alloc_For_Runtime,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
allocator_user_data,
|
||||
#endif
|
||||
size);
|
||||
}
|
||||
else {
|
||||
return os_malloc(size);
|
||||
|
@ -201,11 +223,14 @@ wasm_runtime_realloc_internal(void *ptr, unsigned int size)
|
|||
}
|
||||
else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
|
||||
if (realloc_func)
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
return realloc_func(allocator_user_data, ptr, size);
|
||||
#else
|
||||
return realloc_func(ptr, size);
|
||||
return realloc_func(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
Alloc_For_Runtime, false,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
allocator_user_data,
|
||||
#endif
|
||||
ptr, size);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
@ -233,11 +258,14 @@ wasm_runtime_free_internal(void *ptr)
|
|||
mem_allocator_free(pool_allocator, ptr);
|
||||
}
|
||||
else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
free_func(allocator_user_data, ptr);
|
||||
#else
|
||||
free_func(ptr);
|
||||
free_func(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
Alloc_For_Runtime,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
allocator_user_data,
|
||||
#endif
|
||||
ptr);
|
||||
}
|
||||
else {
|
||||
os_free(ptr);
|
||||
|
@ -765,6 +793,29 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
|
|||
bh_assert(total_size_new
|
||||
<= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
|
||||
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
if (!(memory_data_new =
|
||||
realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
NULL,
|
||||
#endif
|
||||
memory_data_old, total_size_new))) {
|
||||
ret = false;
|
||||
goto return_func;
|
||||
}
|
||||
if (heap_size > 0) {
|
||||
if (mem_allocator_migrate(memory->heap_handle,
|
||||
(char *)heap_data_old
|
||||
+ (memory_data_new - memory_data_old),
|
||||
heap_size)
|
||||
!= 0) {
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
|
||||
memory->heap_data_end = memory->heap_data + heap_size;
|
||||
memory->memory_data = memory_data_new;
|
||||
#else
|
||||
if (full_size_mmaped) {
|
||||
#ifdef BH_PLATFORM_WINDOWS
|
||||
if (!os_mem_commit(memory->memory_data_end,
|
||||
|
@ -823,6 +874,7 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
|
|||
os_writegsbase(memory_data_new);
|
||||
#endif
|
||||
}
|
||||
#endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
|
||||
|
||||
memory->num_bytes_per_page = num_bytes_per_page;
|
||||
memory->cur_page_count = total_page_count;
|
||||
|
@ -903,8 +955,19 @@ wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
|
|||
#else
|
||||
map_size = 8 * (uint64)BH_GB;
|
||||
#endif
|
||||
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
(void)map_size;
|
||||
free_func(Alloc_For_LinearMemory,
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
NULL,
|
||||
#endif
|
||||
memory_inst->memory_data);
|
||||
#else
|
||||
wasm_munmap_linear_memory(memory_inst->memory_data,
|
||||
memory_inst->memory_data_size, map_size);
|
||||
#endif
|
||||
|
||||
memory_inst->memory_data = NULL;
|
||||
}
|
||||
|
||||
|
@ -954,9 +1017,20 @@ wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
|
|||
*memory_data_size = align_as_and_cast(*memory_data_size, page_size);
|
||||
|
||||
if (map_size > 0) {
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
(void)wasm_mmap_linear_memory;
|
||||
if (!(*data = malloc_func(Alloc_For_LinearMemory,
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
NULL,
|
||||
#endif
|
||||
*memory_data_size))) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
#else
|
||||
if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
|
|
|
@ -84,9 +84,9 @@ compare_type_with_signautre(uint8 type, const char signature)
|
|||
if ('r' == signature
|
||||
#if WASM_ENABLE_GC != 0
|
||||
#if WASM_ENABLE_STRINGREF != 0
|
||||
&& (type >= REF_TYPE_STRINGVIEWITER && type <= REF_TYPE_FUNCREF)
|
||||
&& (type >= REF_TYPE_STRINGVIEWITER && type <= REF_TYPE_NULLFUNCREF)
|
||||
#else
|
||||
&& (type >= REF_TYPE_NULLREF && type <= REF_TYPE_FUNCREF)
|
||||
&& (type >= REF_TYPE_HT_NULLABLE && type <= REF_TYPE_NULLFUNCREF)
|
||||
#endif
|
||||
#else
|
||||
&& type == VALUE_TYPE_EXTERNREF
|
||||
|
|
|
@ -219,7 +219,7 @@ runtime_signal_handler(void *sig_addr)
|
|||
os_longjmp(jmpbuf_node->jmpbuf, 1);
|
||||
}
|
||||
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
|
||||
else if (stack_min_addr - page_size <= (uint8 *)sig_addr
|
||||
else if (stack_min_addr <= (uint8 *)sig_addr
|
||||
&& (uint8 *)sig_addr
|
||||
< stack_min_addr + page_size * guard_page_count) {
|
||||
/* The address which causes segmentation fault is inside
|
||||
|
@ -3727,6 +3727,213 @@ static union {
|
|||
|
||||
#define is_little_endian() (__ue.b == 1) /* NOLINT */
|
||||
|
||||
int32
|
||||
wasm_runtime_get_import_count(WASMModuleCommon *const module)
|
||||
{
|
||||
if (!module) {
|
||||
bh_assert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module->module_type == Wasm_Module_AoT) {
|
||||
const AOTModule *aot_module = (const AOTModule *)module;
|
||||
return (int32)(aot_module->import_func_count
|
||||
+ aot_module->import_global_count
|
||||
+ aot_module->import_table_count
|
||||
+ aot_module->import_memory_count);
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module->module_type == Wasm_Module_Bytecode) {
|
||||
const WASMModule *wasm_module = (const WASMModule *)module;
|
||||
return (int32)wasm_module->import_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
|
||||
wasm_import_type *import_type)
|
||||
{
|
||||
if (!import_type) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(import_type, 0, sizeof(wasm_import_type));
|
||||
|
||||
if (!module) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module->module_type == Wasm_Module_AoT) {
|
||||
const AOTModule *aot_module = (const AOTModule *)module;
|
||||
|
||||
uint32 func_index = (uint32)import_index;
|
||||
if (func_index < aot_module->import_func_count) {
|
||||
const AOTImportFunc *aot_import_func =
|
||||
&aot_module->import_funcs[func_index];
|
||||
import_type->module_name = aot_import_func->module_name;
|
||||
import_type->name = aot_import_func->func_name;
|
||||
import_type->kind = WASM_IMPORT_EXPORT_KIND_FUNC;
|
||||
import_type->linked =
|
||||
aot_import_func->func_ptr_linked ? true : false;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32 global_index = func_index - aot_module->import_func_count;
|
||||
if (global_index < aot_module->import_global_count) {
|
||||
const AOTImportGlobal *aot_import_global =
|
||||
&aot_module->import_globals[global_index];
|
||||
import_type->module_name = aot_import_global->module_name;
|
||||
import_type->name = aot_import_global->global_name;
|
||||
import_type->kind = WASM_IMPORT_EXPORT_KIND_GLOBAL;
|
||||
import_type->linked = aot_import_global->is_linked;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32 table_index = global_index - aot_module->import_global_count;
|
||||
if (table_index < aot_module->import_table_count) {
|
||||
const AOTImportTable *aot_import_table =
|
||||
&aot_module->import_tables[table_index];
|
||||
import_type->module_name = aot_import_table->module_name;
|
||||
import_type->name = aot_import_table->table_name;
|
||||
import_type->kind = WASM_IMPORT_EXPORT_KIND_TABLE;
|
||||
import_type->linked = false;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32 memory_index = table_index - aot_module->import_table_count;
|
||||
if (memory_index < aot_module->import_memory_count) {
|
||||
const AOTImportMemory *aot_import_memory =
|
||||
&aot_module->import_memories[memory_index];
|
||||
import_type->module_name = aot_import_memory->module_name;
|
||||
import_type->name = aot_import_memory->memory_name;
|
||||
import_type->kind = WASM_IMPORT_EXPORT_KIND_MEMORY;
|
||||
import_type->linked = false;
|
||||
return;
|
||||
}
|
||||
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module->module_type == Wasm_Module_Bytecode) {
|
||||
const WASMModule *wasm_module = (const WASMModule *)module;
|
||||
|
||||
if ((uint32)import_index >= wasm_module->import_count) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const WASMImport *wasm_import = &wasm_module->imports[import_index];
|
||||
|
||||
import_type->module_name = wasm_import->u.names.module_name;
|
||||
import_type->name = wasm_import->u.names.field_name;
|
||||
import_type->kind = wasm_import->kind;
|
||||
switch (import_type->kind) {
|
||||
case WASM_IMPORT_EXPORT_KIND_FUNC:
|
||||
import_type->linked = wasm_import->u.function.func_ptr_linked;
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
|
||||
import_type->linked = wasm_import->u.global.is_linked;
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_TABLE:
|
||||
/* not supported */
|
||||
import_type->linked = false;
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_MEMORY:
|
||||
/* not supported */
|
||||
import_type->linked = false;
|
||||
break;
|
||||
default:
|
||||
bh_assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int32
|
||||
wasm_runtime_get_export_count(WASMModuleCommon *const module)
|
||||
{
|
||||
if (!module) {
|
||||
bh_assert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module->module_type == Wasm_Module_AoT) {
|
||||
const AOTModule *aot_module = (const AOTModule *)module;
|
||||
return (int32)aot_module->export_count;
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module->module_type == Wasm_Module_Bytecode) {
|
||||
const WASMModule *wasm_module = (const WASMModule *)module;
|
||||
return (int32)wasm_module->export_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
|
||||
wasm_export_type *export_type)
|
||||
{
|
||||
if (!export_type) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(export_type, 0, sizeof(wasm_export_type));
|
||||
|
||||
if (!module) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module->module_type == Wasm_Module_AoT) {
|
||||
const AOTModule *aot_module = (const AOTModule *)module;
|
||||
|
||||
if ((uint32)export_index >= aot_module->export_count) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const AOTExport *aot_export = &aot_module->exports[export_index];
|
||||
export_type->name = aot_export->name;
|
||||
export_type->kind = aot_export->kind;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module->module_type == Wasm_Module_Bytecode) {
|
||||
const WASMModule *wasm_module = (const WASMModule *)module;
|
||||
|
||||
if ((uint32)export_index >= wasm_module->export_count) {
|
||||
bh_assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const WASMExport *wasm_export = &wasm_module->exports[export_index];
|
||||
export_type->name = wasm_export->name;
|
||||
export_type->kind = wasm_export->kind;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_register_natives(const char *module_name,
|
||||
NativeSymbol *native_symbols,
|
||||
|
@ -6808,3 +7015,59 @@ wasm_runtime_get_module_name(wasm_module_t module)
|
|||
|
||||
return "";
|
||||
}
|
||||
|
||||
/*
|
||||
* wasm_runtime_detect_native_stack_overflow
|
||||
*
|
||||
* - raise "native stack overflow" exception if available native stack
|
||||
* at this point is less than WASM_STACK_GUARD_SIZE. in that case,
|
||||
* return false.
|
||||
*
|
||||
* - update native_stack_top_min.
|
||||
*/
|
||||
bool
|
||||
wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env)
|
||||
{
|
||||
uint8 *boundary = exec_env->native_stack_boundary;
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&boundary);
|
||||
if (boundary == NULL) {
|
||||
/* the platfrom doesn't support os_thread_get_stack_boundary */
|
||||
return true;
|
||||
}
|
||||
#if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
|
||||
uint32 page_size = os_getpagesize();
|
||||
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
|
||||
boundary = boundary + page_size * guard_page_count;
|
||||
#endif
|
||||
if ((uint8 *)&boundary < boundary) {
|
||||
wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
|
||||
"native stack overflow");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env,
|
||||
uint32 requested_size)
|
||||
{
|
||||
uint8 *boundary = exec_env->native_stack_boundary;
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&boundary);
|
||||
if (boundary == NULL) {
|
||||
/* the platfrom doesn't support os_thread_get_stack_boundary */
|
||||
return true;
|
||||
}
|
||||
#if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
|
||||
uint32 page_size = os_getpagesize();
|
||||
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
|
||||
boundary = boundary + page_size * guard_page_count;
|
||||
#endif
|
||||
/* adjust the boundary for the requested size */
|
||||
boundary = boundary - WASM_STACK_GUARD_SIZE + requested_size;
|
||||
if ((uint8 *)&boundary < boundary) {
|
||||
wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
|
||||
"native stack overflow");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1189,6 +1189,13 @@ wasm_runtime_end_blocking_op(WASMExecEnv *exec_env);
|
|||
void
|
||||
wasm_runtime_interrupt_blocking_op(WASMExecEnv *exec_env);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env,
|
||||
uint32 requested_size);
|
||||
|
||||
#if WASM_ENABLE_LINUX_PERF != 0
|
||||
bool
|
||||
wasm_runtime_get_linux_perf(void);
|
||||
|
|
|
@ -3452,16 +3452,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_narrow_i64x2_s:
|
||||
case SIMD_i32x4_narrow_i64x2_u:
|
||||
{
|
||||
if (!aot_compile_simd_i32x4_narrow_i64x2(
|
||||
comp_ctx, func_ctx,
|
||||
SIMD_i32x4_narrow_i64x2_s == opcode))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_extend_low_i16x8_s:
|
||||
case SIMD_i32x4_extend_high_i16x8_s:
|
||||
{
|
||||
|
@ -3501,16 +3491,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_add_sat_s:
|
||||
case SIMD_i32x4_add_sat_u:
|
||||
{
|
||||
if (!aot_compile_simd_i32x4_saturate(
|
||||
comp_ctx, func_ctx, V128_ADD,
|
||||
opcode == SIMD_i32x4_add_sat_s))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_sub:
|
||||
{
|
||||
if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
|
||||
|
@ -3519,16 +3499,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_sub_sat_s:
|
||||
case SIMD_i32x4_sub_sat_u:
|
||||
{
|
||||
if (!aot_compile_simd_i32x4_saturate(
|
||||
comp_ctx, func_ctx, V128_SUB,
|
||||
opcode == SIMD_i32x4_add_sat_s))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_mul:
|
||||
{
|
||||
if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
|
||||
|
@ -3565,13 +3535,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_avgr_u:
|
||||
{
|
||||
if (!aot_compile_simd_i32x4_avgr_u(comp_ctx, func_ctx))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_extmul_low_i16x8_s:
|
||||
case SIMD_i32x4_extmul_high_i16x8_s:
|
||||
{
|
||||
|
@ -3728,13 +3691,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_f32x4_round:
|
||||
{
|
||||
if (!aot_compile_simd_f32x4_round(comp_ctx, func_ctx))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_f32x4_sqrt:
|
||||
{
|
||||
if (!aot_compile_simd_f32x4_sqrt(comp_ctx, func_ctx))
|
||||
|
@ -3788,13 +3744,6 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_f64x2_round:
|
||||
{
|
||||
if (!aot_compile_simd_f64x2_round(comp_ctx, func_ctx))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_f64x2_sqrt:
|
||||
{
|
||||
if (!aot_compile_simd_f64x2_sqrt(comp_ctx, func_ctx))
|
||||
|
|
|
@ -484,15 +484,15 @@ static uint32
|
|||
get_func_type_size(AOTCompContext *comp_ctx, AOTFuncType *func_type)
|
||||
{
|
||||
#if WASM_ENABLE_GC != 0
|
||||
/* type flag + is_sub_final + parent_type_idx + rec_count + rec_idx + param
|
||||
* count + result count
|
||||
* + ref_type_map_count + types + context of ref_type_map */
|
||||
/* type flag + equivalence type flag + is_sub_final + parent_type_idx
|
||||
+ rec_count + rec_idx + param count + result count
|
||||
+ ref_type_map_count + types + context of ref_type_map */
|
||||
if (comp_ctx->enable_gc) {
|
||||
uint32 size = 0;
|
||||
|
||||
/* type flag */
|
||||
size += sizeof(func_type->base_type.type_flag);
|
||||
/* is_sub_final */
|
||||
/* equivalence type flag + is_sub_final */
|
||||
size += sizeof(uint16);
|
||||
/* parent_type_idx */
|
||||
size += sizeof(func_type->base_type.parent_type_idx);
|
||||
|
@ -529,12 +529,12 @@ static uint32
|
|||
get_struct_type_size(AOTCompContext *comp_ctx, AOTStructType *struct_type)
|
||||
{
|
||||
uint32 size = 0;
|
||||
/* type flag + is_sub_final + parent_type_idx + rec_count + rec_idx + field
|
||||
* count + fields */
|
||||
/* type flag + equivalence type flag + is_sub_final + parent_type_idx
|
||||
+ rec_count + rec_idx + field count + fields */
|
||||
|
||||
/* type flag */
|
||||
size += sizeof(struct_type->base_type.type_flag);
|
||||
/* is_sub_final */
|
||||
/* equivalence type flag + is_sub_final */
|
||||
size += sizeof(uint16);
|
||||
/* parent_type_idx */
|
||||
size += sizeof(struct_type->base_type.parent_type_idx);
|
||||
|
@ -558,12 +558,12 @@ static uint32
|
|||
get_array_type_size(AOTCompContext *comp_ctx, AOTArrayType *array_type)
|
||||
{
|
||||
uint32 size = 0;
|
||||
/* type flag + is_sub_final + parent_type_idx + rec_count + rec_idx +
|
||||
elem_flags + elem_type + elem_ref_type */
|
||||
/* type flag + equivalence type flag + is_sub_final + parent_type_idx
|
||||
+ rec_count + rec_idx + elem_flags + elem_type + elem_ref_type */
|
||||
|
||||
/* type flag */
|
||||
size += sizeof(array_type->base_type.type_flag);
|
||||
/* is_sub_final */
|
||||
/* equivalence type flag + is_sub_final */
|
||||
size += sizeof(uint16);
|
||||
/* parent_type_idx (u32) */
|
||||
size += sizeof(array_type->base_type.parent_type_idx);
|
||||
|
@ -597,7 +597,22 @@ get_type_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
|
|||
#if WASM_ENABLE_GC != 0
|
||||
if (comp_ctx->enable_gc) {
|
||||
for (i = 0; i < comp_data->type_count; i++) {
|
||||
uint32 j;
|
||||
|
||||
size = align_uint(size, 4);
|
||||
|
||||
/* Emit simple info if there is an equivalence type */
|
||||
for (j = 0; j < i; j++) {
|
||||
if (comp_data->types[j] == comp_data->types[i]) {
|
||||
/* type_flag (2 bytes) + equivalence type flag (1 byte)
|
||||
+ padding (1 byte) + equivalence type index */
|
||||
size += 8;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j < i)
|
||||
continue;
|
||||
|
||||
if (comp_data->types[i]->type_flag == WASM_TYPE_FUNC)
|
||||
size += get_func_type_size(comp_ctx,
|
||||
(AOTFuncType *)comp_data->types[i]);
|
||||
|
@ -2093,13 +2108,32 @@ aot_emit_type_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
|
|||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
if (comp_ctx->enable_gc) {
|
||||
int32 idx;
|
||||
AOTType **types = comp_data->types;
|
||||
int32 idx;
|
||||
uint32 j;
|
||||
|
||||
for (i = 0; i < comp_data->type_count; i++) {
|
||||
offset = align_uint(offset, 4);
|
||||
|
||||
/* Emit simple info if there is an equivalence type */
|
||||
for (j = 0; j < i; j++) {
|
||||
if (types[j] == types[i]) {
|
||||
EMIT_U16(types[i]->type_flag);
|
||||
/* equivalence type flag is true */
|
||||
EMIT_U8(1);
|
||||
EMIT_U8(0);
|
||||
/* equivalence type index */
|
||||
EMIT_U32(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j < i)
|
||||
continue;
|
||||
|
||||
EMIT_U16(types[i]->type_flag);
|
||||
EMIT_U16(types[i]->is_sub_final);
|
||||
/* equivalence type flag is false */
|
||||
EMIT_U8(0);
|
||||
EMIT_U8(types[i]->is_sub_final);
|
||||
EMIT_U32(types[i]->parent_type_idx);
|
||||
|
||||
EMIT_U16(types[i]->rec_count);
|
||||
|
@ -2593,7 +2627,7 @@ aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
|
|||
if (comp_ctx->enable_gc) {
|
||||
/* emit func_local_ref_flag arrays for both import and AOTed funcs */
|
||||
AOTFuncType *func_type;
|
||||
uint32 j, local_ref_flags_cell_num;
|
||||
uint32 j, local_ref_flags_cell_num, paddings;
|
||||
|
||||
for (i = 0; i < comp_data->import_func_count; i++) {
|
||||
func_type = comp_data->import_funcs[i].func_type;
|
||||
|
@ -2603,6 +2637,8 @@ aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
|
|||
local_ref_flags_cell_num += wasm_value_type_cell_num_internal(
|
||||
func_type->types[j], comp_ctx->pointer_size);
|
||||
}
|
||||
paddings =
|
||||
local_ref_flags_cell_num < 2 ? 2 - local_ref_flags_cell_num : 0;
|
||||
local_ref_flags_cell_num =
|
||||
local_ref_flags_cell_num > 2 ? local_ref_flags_cell_num : 2;
|
||||
|
||||
|
@ -2614,7 +2650,7 @@ aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
|
|||
func_type->types[j]))
|
||||
return false;
|
||||
}
|
||||
for (; j < 2; j++)
|
||||
for (j = 0; j < paddings; j++)
|
||||
EMIT_U8(0);
|
||||
}
|
||||
|
||||
|
|
|
@ -1826,6 +1826,52 @@ fail:
|
|||
return ret;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
static LLVMValueRef
|
||||
call_aot_func_type_is_super_of_func(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx,
|
||||
LLVMValueRef type_idx1,
|
||||
LLVMValueRef type_idx2)
|
||||
{
|
||||
LLVMValueRef param_values[3], ret_value, value, func;
|
||||
LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
|
||||
|
||||
param_types[0] = comp_ctx->aot_inst_type;
|
||||
param_types[1] = I32_TYPE;
|
||||
param_types[2] = I32_TYPE;
|
||||
ret_type = INT8_TYPE;
|
||||
|
||||
#if WASM_ENABLE_JIT != 0
|
||||
if (comp_ctx->is_jit_mode)
|
||||
GET_AOT_FUNCTION(llvm_jit_func_type_is_super_of, 3);
|
||||
else
|
||||
#endif
|
||||
GET_AOT_FUNCTION(aot_func_type_is_super_of, 3);
|
||||
|
||||
param_values[0] = func_ctx->aot_inst;
|
||||
param_values[1] = type_idx1;
|
||||
param_values[2] = type_idx2;
|
||||
|
||||
if (!(ret_value =
|
||||
LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
|
||||
3, "call_aot_func_type_is_super_of"))) {
|
||||
aot_set_last_error("llvm build call failed.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, ret_value,
|
||||
I8_ZERO, "check_fail"))) {
|
||||
aot_set_last_error("llvm build icmp failed.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
|
||||
fail:
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool
|
||||
call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
|
||||
AOTFuncType *aot_func_type,
|
||||
|
@ -2018,15 +2064,23 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
|
|||
return false;
|
||||
}
|
||||
|
||||
/* Find the equivalent function type whose type index is the smallest:
|
||||
the callee function's type index is also converted to the smallest
|
||||
one in wasm loader, so we can just check whether the two type indexes
|
||||
are equal (the type index of call_indirect opcode and callee func),
|
||||
we don't need to check whether the whole function types are equal,
|
||||
including param types and result types. */
|
||||
type_idx =
|
||||
wasm_get_smallest_type_idx((WASMTypePtr *)comp_ctx->comp_data->types,
|
||||
comp_ctx->comp_data->type_count, type_idx);
|
||||
if (!comp_ctx->enable_gc) {
|
||||
/* Find the equivalent function type whose type index is the smallest:
|
||||
the callee function's type index is also converted to the smallest
|
||||
one in wasm loader, so we can just check whether the two type indexes
|
||||
are equal (the type index of call_indirect opcode and callee func),
|
||||
we don't need to check whether the whole function types are equal,
|
||||
including param types and result types. */
|
||||
type_idx = wasm_get_smallest_type_idx(
|
||||
(WASMTypePtr *)comp_ctx->comp_data->types,
|
||||
comp_ctx->comp_data->type_count, type_idx);
|
||||
}
|
||||
else {
|
||||
/* Call aot_func_type_is_super_of to check whether the func type
|
||||
provided in the bytecode is a super type of the func type of
|
||||
the function to call */
|
||||
}
|
||||
|
||||
ftype_idx_const = I32_CONST(type_idx);
|
||||
CHECK_LLVM_CONST(ftype_idx_const);
|
||||
|
||||
|
@ -2254,11 +2308,23 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
|
|||
goto fail;
|
||||
}
|
||||
|
||||
/* Check if function type index not equal */
|
||||
if (!(cmp_ftype_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
|
||||
ftype_idx_const, "cmp_ftype_idx"))) {
|
||||
aot_set_last_error("llvm build icmp failed.");
|
||||
goto fail;
|
||||
#if WASM_ENABLE_GC != 0
|
||||
if (comp_ctx->enable_gc) {
|
||||
if (!(cmp_ftype_idx = call_aot_func_type_is_super_of_func(
|
||||
comp_ctx, func_ctx, ftype_idx_const, ftype_idx))) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Check if function type index not equal */
|
||||
if (!(cmp_ftype_idx =
|
||||
LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, ftype_idx,
|
||||
ftype_idx_const, "cmp_ftype_idx"))) {
|
||||
aot_set_last_error("llvm build icmp failed.");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* Throw exception if ftype_idx != ftype_idx_const */
|
||||
|
|
|
@ -226,15 +226,6 @@ aot_compile_simd_i16x8_narrow_i32x4(AOTCompContext *comp_ctx,
|
|||
}
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_narrow_i64x2(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx, bool is_signed)
|
||||
{
|
||||
/* TODO: x86 intrinsics */
|
||||
return simd_integer_narrow_common(comp_ctx, func_ctx, e_sat_i64x2,
|
||||
is_signed);
|
||||
}
|
||||
|
||||
enum integer_extend_type {
|
||||
e_ext_i8x16,
|
||||
e_ext_i16x8,
|
||||
|
|
|
@ -20,10 +20,6 @@ bool
|
|||
aot_compile_simd_i16x8_narrow_i32x4(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx, bool is_signed);
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_narrow_i64x2(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx, bool is_signed);
|
||||
|
||||
bool
|
||||
aot_compile_simd_i16x8_extend_i8x16(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx, bool is_low,
|
||||
|
|
|
@ -129,20 +129,6 @@ aot_compile_simd_f64x2_abs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
|
|||
"llvm.fabs.v2f64");
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_f32x4_round(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
|
||||
{
|
||||
return simd_float_intrinsic(comp_ctx, func_ctx, V128_f32x4_TYPE,
|
||||
"llvm.round.v4f32");
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_f64x2_round(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
|
||||
{
|
||||
return simd_float_intrinsic(comp_ctx, func_ctx, V128_f64x2_TYPE,
|
||||
"llvm.round.v2f64");
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_f32x4_sqrt(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
|
||||
{
|
||||
|
|
|
@ -32,14 +32,6 @@ aot_compile_simd_f32x4_abs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
|
|||
bool
|
||||
aot_compile_simd_f64x2_abs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
|
||||
|
||||
bool
|
||||
aot_compile_simd_f32x4_round(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx);
|
||||
|
||||
bool
|
||||
aot_compile_simd_f64x2_round(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx);
|
||||
|
||||
bool
|
||||
aot_compile_simd_f32x4_sqrt(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
|
||||
|
||||
|
|
|
@ -243,7 +243,6 @@ aot_compile_simd_i64x2_abs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
|
|||
enum integer_avgr_u {
|
||||
e_avgr_u_i8x16,
|
||||
e_avgr_u_i16x8,
|
||||
e_avgr_u_i32x4,
|
||||
};
|
||||
|
||||
/* TODO: try int_x86_mmx_pavg_b and int_x86_mmx_pavg_w */
|
||||
|
@ -257,9 +256,8 @@ simd_v128_avg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
|
|||
LLVMTypeRef vector_type[] = {
|
||||
V128_i8x16_TYPE,
|
||||
V128_i16x8_TYPE,
|
||||
V128_i32x4_TYPE,
|
||||
};
|
||||
unsigned lanes[] = { 16, 8, 4 };
|
||||
unsigned lanes[] = { 16, 8 };
|
||||
|
||||
if (!(rhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx,
|
||||
vector_type[itype], "rhs"))
|
||||
|
@ -325,13 +323,6 @@ aot_compile_simd_i16x8_avgr_u(AOTCompContext *comp_ctx,
|
|||
return simd_v128_avg(comp_ctx, func_ctx, e_avgr_u_i16x8);
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_avgr_u(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx)
|
||||
{
|
||||
return simd_v128_avg(comp_ctx, func_ctx, e_avgr_u_i32x4);
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_dot_i16x8(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx)
|
||||
|
|
|
@ -76,10 +76,6 @@ bool
|
|||
aot_compile_simd_i16x8_avgr_u(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx);
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_avgr_u(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx);
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_dot_i16x8(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx);
|
||||
|
|
|
@ -64,18 +64,3 @@ aot_compile_simd_i16x8_saturate(AOTCompContext *comp_ctx,
|
|||
is_signed ? intrinsics[arith_op][0]
|
||||
: intrinsics[arith_op][1]);
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_saturate(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx,
|
||||
V128Arithmetic arith_op, bool is_signed)
|
||||
{
|
||||
char *intrinsics[][2] = {
|
||||
{ "llvm.sadd.sat.v4i32", "llvm.uadd.sat.v4i32" },
|
||||
{ "llvm.ssub.sat.v4i32", "llvm.usub.sat.v4i32" },
|
||||
};
|
||||
|
||||
return simd_sat_int_arith(comp_ctx, func_ctx, V128_i16x8_TYPE,
|
||||
is_signed ? intrinsics[arith_op][0]
|
||||
: intrinsics[arith_op][1]);
|
||||
}
|
||||
|
|
|
@ -22,10 +22,6 @@ aot_compile_simd_i16x8_saturate(AOTCompContext *comp_ctx,
|
|||
AOTFuncContext *func_ctx,
|
||||
V128Arithmetic arith_op, bool is_signed);
|
||||
|
||||
bool
|
||||
aot_compile_simd_i32x4_saturate(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx,
|
||||
V128Arithmetic arith_op, bool is_signed);
|
||||
#ifdef __cplusplus
|
||||
} /* end of extern "C" */
|
||||
#endif
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file aot_export.h
|
||||
*
|
||||
* @brief This file defines the exported AOT compilation APIs
|
||||
*/
|
||||
|
||||
#ifndef _AOT_EXPORT_H
|
||||
#define _AOT_EXPORT_H
|
||||
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file gc_export.h
|
||||
*
|
||||
* @brief This file defines the exported GC APIs
|
||||
*/
|
||||
|
||||
#ifndef _GC_EXPORT_H
|
||||
#define _GC_EXPORT_H
|
||||
|
||||
|
|
|
@ -3,6 +3,11 @@
|
|||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lib_export.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _LIB_EXPORT_H_
|
||||
#define _LIB_EXPORT_H_
|
||||
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
// WebAssembly C API
|
||||
|
||||
/**
|
||||
* @file wasm_c_api.h
|
||||
*
|
||||
* @brief This file defines the WebAssembly C APIs
|
||||
*/
|
||||
|
||||
#ifndef _WASM_C_API_H_
|
||||
#define _WASM_C_API_H_
|
||||
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file wasm_export.h
|
||||
*
|
||||
* @brief This file defines the exported common runtime APIs
|
||||
*/
|
||||
|
||||
#ifndef _WASM_EXPORT_H
|
||||
#define _WASM_EXPORT_H
|
||||
|
||||
|
@ -26,10 +32,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* clang-format off */
|
||||
|
||||
#define get_module_inst(exec_env) \
|
||||
wasm_runtime_get_module_inst(exec_env)
|
||||
#define get_module_inst(exec_env) wasm_runtime_get_module_inst(exec_env)
|
||||
|
||||
#define validate_app_addr(offset, size) \
|
||||
wasm_runtime_validate_app_addr(module_inst, offset, size)
|
||||
|
@ -46,8 +49,7 @@ extern "C" {
|
|||
#define module_malloc(size, p_native_addr) \
|
||||
wasm_runtime_module_malloc(module_inst, size, p_native_addr)
|
||||
|
||||
#define module_free(offset) \
|
||||
wasm_runtime_module_free(module_inst, offset)
|
||||
#define module_free(offset) wasm_runtime_module_free(module_inst, offset)
|
||||
|
||||
#define native_raw_return_type(type, args) type *raw_ret = (type *)(args)
|
||||
|
||||
|
@ -63,6 +65,25 @@ struct WASMModuleCommon;
|
|||
typedef struct WASMModuleCommon *wasm_module_t;
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
WASM_IMPORT_EXPORT_KIND_FUNC,
|
||||
WASM_IMPORT_EXPORT_KIND_TABLE,
|
||||
WASM_IMPORT_EXPORT_KIND_MEMORY,
|
||||
WASM_IMPORT_EXPORT_KIND_GLOBAL
|
||||
} wasm_import_export_kind_t;
|
||||
|
||||
typedef struct wasm_import_type {
|
||||
const char *module_name;
|
||||
const char *name;
|
||||
wasm_import_export_kind_t kind;
|
||||
bool linked;
|
||||
} wasm_import_type;
|
||||
|
||||
typedef struct wasm_export_type {
|
||||
const char *name;
|
||||
wasm_import_export_kind_t kind;
|
||||
} wasm_export_type;
|
||||
|
||||
/* Instantiated WASM module */
|
||||
struct WASMModuleInstanceCommon;
|
||||
typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
|
||||
|
@ -107,6 +128,8 @@ typedef enum {
|
|||
Alloc_With_System_Allocator,
|
||||
} mem_alloc_type_t;
|
||||
|
||||
typedef enum { Alloc_For_Runtime, Alloc_For_LinearMemory } mem_alloc_usage_t;
|
||||
|
||||
/* Memory allocator option */
|
||||
typedef union MemAllocOption {
|
||||
struct {
|
||||
|
@ -114,6 +137,9 @@ typedef union MemAllocOption {
|
|||
uint32_t heap_size;
|
||||
} pool;
|
||||
struct {
|
||||
/* the function signature is varied when
|
||||
WASM_MEM_ALLOC_WITH_USER_DATA and
|
||||
WASM_MEM_ALLOC_WITH_USAGE are defined */
|
||||
void *malloc_func;
|
||||
void *realloc_func;
|
||||
void *free_func;
|
||||
|
@ -359,8 +385,8 @@ wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
|
|||
* Callback to load a module file into a buffer in multi-module feature
|
||||
*/
|
||||
typedef bool (*module_reader)(package_type_t module_type,
|
||||
const char *module_name,
|
||||
uint8_t **p_buffer, uint32_t *p_size);
|
||||
const char *module_name, uint8_t **p_buffer,
|
||||
uint32_t *p_size);
|
||||
|
||||
/**
|
||||
* Callback to release the buffer loaded by module_reader callback
|
||||
|
@ -424,8 +450,8 @@ wasm_runtime_find_module_registered(const char *module_name);
|
|||
* @return return WASM module loaded, NULL if failed
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN wasm_module_t
|
||||
wasm_runtime_load(uint8_t *buf, uint32_t size,
|
||||
char *error_buf, uint32_t error_buf_size);
|
||||
wasm_runtime_load(uint8_t *buf, uint32_t size, char *error_buf,
|
||||
uint32_t error_buf_size);
|
||||
|
||||
/**
|
||||
* Load a WASM module with specified load argument.
|
||||
|
@ -501,12 +527,12 @@ wasm_runtime_get_module_hash(wasm_module_t module);
|
|||
* for STDERR is used.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_wasi_args_ex(wasm_module_t module,
|
||||
const char *dir_list[], uint32_t dir_count,
|
||||
const char *map_dir_list[], uint32_t map_dir_count,
|
||||
const char *env[], uint32_t env_count,
|
||||
char *argv[], int argc, int64_t stdinfd,
|
||||
int64_t stdoutfd, int64_t stderrfd);
|
||||
wasm_runtime_set_wasi_args_ex(wasm_module_t module, const char *dir_list[],
|
||||
uint32_t dir_count, const char *map_dir_list[],
|
||||
uint32_t map_dir_count, const char *env[],
|
||||
uint32_t env_count, char *argv[], int argc,
|
||||
int64_t stdinfd, int64_t stdoutfd,
|
||||
int64_t stderrfd);
|
||||
|
||||
/**
|
||||
* Set WASI parameters.
|
||||
|
@ -514,34 +540,34 @@ wasm_runtime_set_wasi_args_ex(wasm_module_t module,
|
|||
* Same as wasm_runtime_set_wasi_args_ex but with default stdio handles
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_wasi_args(wasm_module_t module,
|
||||
const char *dir_list[], uint32_t dir_count,
|
||||
const char *map_dir_list[], uint32_t map_dir_count,
|
||||
const char *env[], uint32_t env_count,
|
||||
char *argv[], int argc);
|
||||
wasm_runtime_set_wasi_args(wasm_module_t module, const char *dir_list[],
|
||||
uint32_t dir_count, const char *map_dir_list[],
|
||||
uint32_t map_dir_count, const char *env[],
|
||||
uint32_t env_count, char *argv[], int argc);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
|
||||
uint32_t addr_pool_size);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module, const char *ns_lookup_pool[],
|
||||
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
|
||||
const char *ns_lookup_pool[],
|
||||
uint32_t ns_lookup_pool_size);
|
||||
|
||||
/**
|
||||
* Instantiate a WASM module.
|
||||
*
|
||||
* @param module the WASM module to instantiate
|
||||
* @param default_stack_size the default stack size of the module instance when the
|
||||
* exec env's operation stack isn't created by user, e.g. API
|
||||
* @param default_stack_size the default stack size of the module instance when
|
||||
* the exec env's operation stack isn't created by user, e.g. API
|
||||
* wasm_application_execute_main() and wasm_application_execute_func()
|
||||
* create the operation stack internally with the stack size specified
|
||||
* here. And API wasm_runtime_create_exec_env() creates the operation
|
||||
* stack with stack size specified by its parameter, the stack size
|
||||
* specified here is ignored.
|
||||
* @param host_managed_heap_size the default heap size of the module instance, a heap will
|
||||
* be created besides the app memory space. Both wasm app and native
|
||||
* function can allocate memory from the heap.
|
||||
* @param host_managed_heap_size the default heap size of the module instance,
|
||||
* a heap will be created besides the app memory space. Both wasm app
|
||||
* and native function can allocate memory from the heap.
|
||||
* @param error_buf buffer to output the error info if failed
|
||||
* @param error_buf_size the size of the error buffer
|
||||
*
|
||||
|
@ -549,18 +575,20 @@ wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module, const char *ns_lookup
|
|||
*/
|
||||
WASM_RUNTIME_API_EXTERN wasm_module_inst_t
|
||||
wasm_runtime_instantiate(const wasm_module_t module,
|
||||
uint32_t default_stack_size, uint32_t host_managed_heap_size,
|
||||
char *error_buf, uint32_t error_buf_size);
|
||||
uint32_t default_stack_size,
|
||||
uint32_t host_managed_heap_size, char *error_buf,
|
||||
uint32_t error_buf_size);
|
||||
|
||||
/**
|
||||
* Instantiate a WASM module, with specified instantiation arguments
|
||||
*
|
||||
* Same as wasm_runtime_instantiate, but it also allows overwriting maximum memory
|
||||
* Same as wasm_runtime_instantiate, but it also allows overwriting maximum
|
||||
* memory
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN wasm_module_inst_t
|
||||
wasm_runtime_instantiate_ex(const wasm_module_t module,
|
||||
const InstantiationArgs *args,
|
||||
char *error_buf, uint32_t error_buf_size);
|
||||
const InstantiationArgs *args, char *error_buf,
|
||||
uint32_t error_buf_size);
|
||||
|
||||
/**
|
||||
* Set the running mode of a WASM module instance, override the
|
||||
|
@ -745,7 +773,8 @@ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
|
|||
* @return debug port if success, 0 otherwise.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN uint32_t
|
||||
wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
|
||||
wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env,
|
||||
int32_t port);
|
||||
|
||||
/**
|
||||
* Same as wasm_runtime_start_debug_instance_with_port(env, -1).
|
||||
|
@ -826,8 +855,7 @@ wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
|
|||
* info.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
|
||||
wasm_function_inst_t function,
|
||||
wasm_runtime_call_wasm(wasm_exec_env_t exec_env, wasm_function_inst_t function,
|
||||
uint32_t argc, uint32_t argv[]);
|
||||
|
||||
/**
|
||||
|
@ -848,9 +876,9 @@ wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
|
|||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
|
||||
wasm_function_inst_t function,
|
||||
uint32_t num_results, wasm_val_t results[],
|
||||
uint32_t num_args, wasm_val_t *args);
|
||||
wasm_function_inst_t function, uint32_t num_results,
|
||||
wasm_val_t results[], uint32_t num_args,
|
||||
wasm_val_t *args);
|
||||
|
||||
/**
|
||||
* Call the given WASM function of a WASM module instance with
|
||||
|
@ -870,9 +898,8 @@ wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
|
|||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
|
||||
wasm_function_inst_t function,
|
||||
uint32_t num_results, wasm_val_t results[],
|
||||
uint32_t num_args, ...);
|
||||
wasm_function_inst_t function, uint32_t num_results,
|
||||
wasm_val_t results[], uint32_t num_args, ...);
|
||||
|
||||
/**
|
||||
* Call a function reference of a given WASM runtime instance with
|
||||
|
@ -914,8 +941,8 @@ wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_index,
|
|||
* the exception info.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_application_execute_main(wasm_module_inst_t module_inst,
|
||||
int32_t argc, char *argv[]);
|
||||
wasm_application_execute_main(wasm_module_inst_t module_inst, int32_t argc,
|
||||
char *argv[]);
|
||||
|
||||
/**
|
||||
* Find the specified function in argv[0] from a WASM module instance
|
||||
|
@ -933,8 +960,8 @@ wasm_application_execute_main(wasm_module_inst_t module_inst,
|
|||
* to get the exception info.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_application_execute_func(wasm_module_inst_t module_inst,
|
||||
const char *name, int32_t argc, char *argv[]);
|
||||
wasm_application_execute_func(wasm_module_inst_t module_inst, const char *name,
|
||||
int32_t argc, char *argv[]);
|
||||
|
||||
/**
|
||||
* Get exception info of the WASM module instance.
|
||||
|
@ -993,8 +1020,7 @@ wasm_runtime_terminate(wasm_module_inst_t module_inst);
|
|||
* @param custom_data the custom data to be set
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
|
||||
void *custom_data);
|
||||
wasm_runtime_set_custom_data(wasm_module_inst_t module_inst, void *custom_data);
|
||||
|
||||
/**
|
||||
* Get the custom data within a WASM module instance.
|
||||
|
@ -1013,8 +1039,7 @@ wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
|
|||
* @param enable the flag to enable/disable the memory bounds checks
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst,
|
||||
bool enable);
|
||||
wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst, bool enable);
|
||||
|
||||
/**
|
||||
* Check if the memory bounds checks flag is enabled for a WASM module instance.
|
||||
|
@ -1023,8 +1048,7 @@ wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst,
|
|||
* @return true if the memory bounds checks flag is enabled, false otherwise
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_is_bounds_checks_enabled(
|
||||
wasm_module_inst_t module_inst);
|
||||
wasm_runtime_is_bounds_checks_enabled(wasm_module_inst_t module_inst);
|
||||
|
||||
/**
|
||||
* Allocate memory from the heap of WASM module instance
|
||||
|
@ -1072,8 +1096,8 @@ wasm_runtime_module_free(wasm_module_inst_t module_inst, uint64_t ptr);
|
|||
* Return non-zero if success, zero if failed.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN uint64_t
|
||||
wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
|
||||
const char *src, uint64_t size);
|
||||
wasm_runtime_module_dup_data(wasm_module_inst_t module_inst, const char *src,
|
||||
uint64_t size);
|
||||
|
||||
/**
|
||||
* Validate the app address, check whether it belongs to WASM module
|
||||
|
@ -1191,6 +1215,48 @@ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
|
|||
uint8_t **p_native_start_addr,
|
||||
uint8_t **p_native_end_addr);
|
||||
|
||||
/**
|
||||
* Get the number of import items for a WASM module
|
||||
*
|
||||
* @param module the WASM module
|
||||
*
|
||||
* @return the number of imports (zero for none), or -1 for failure
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN int32_t
|
||||
wasm_runtime_get_import_count(const wasm_module_t module);
|
||||
|
||||
/**
|
||||
* Get information about a specific WASM module import
|
||||
*
|
||||
* @param module the WASM module
|
||||
* @param import_index the desired import index
|
||||
* @param import_type the location to store information about the import
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_get_import_type(const wasm_module_t module, int32_t import_index,
|
||||
wasm_import_type *import_type);
|
||||
|
||||
/**
|
||||
* Get the number of export items for a WASM module
|
||||
*
|
||||
* @param module the WASM module
|
||||
*
|
||||
* @return the number of exports (zero for none), or -1 for failure
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN int32_t
|
||||
wasm_runtime_get_export_count(const wasm_module_t module);
|
||||
|
||||
/**
|
||||
* Get information about a specific WASM module export
|
||||
*
|
||||
* @param module the WASM module
|
||||
* @param export_index the desired export index
|
||||
* @param export_type the location to store information about the export
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index,
|
||||
wasm_export_type *export_type);
|
||||
|
||||
/**
|
||||
* Register native functions with same module name
|
||||
*
|
||||
|
@ -1210,7 +1276,8 @@ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
|
|||
* 'I': the parameter is i64 type
|
||||
* 'f': the parameter is f32 type
|
||||
* 'F': the parameter is f64 type
|
||||
* 'r': the parameter is externref type, it should be a uintptr_t in host
|
||||
* 'r': the parameter is externref type, it should be a uintptr_t
|
||||
* in host
|
||||
* '*': the parameter is a pointer (i32 in WASM), and runtime will
|
||||
* auto check its boundary before calling the native function.
|
||||
* If it is followed by '~', the checked length of the pointer
|
||||
|
@ -1244,7 +1311,6 @@ wasm_runtime_register_natives_raw(const char *module_name,
|
|||
NativeSymbol *native_symbols,
|
||||
uint32_t n_native_symbols);
|
||||
|
||||
|
||||
/**
|
||||
* Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
|
||||
*
|
||||
|
@ -1321,7 +1387,7 @@ wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst);
|
|||
|
||||
/**
|
||||
* Return execution time in ms of a given wasm funciton with
|
||||
* func_name. If the function is not found, return 0.
|
||||
* func_name. If the function is not found, return 0.
|
||||
*
|
||||
* @param module_inst the WASM module instance to profile
|
||||
* @param func_name could be an export name or a name in the
|
||||
|
@ -1400,8 +1466,8 @@ wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
|
|||
* @return true if success, false otherwise
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_externref_obj2ref(wasm_module_inst_t module_inst,
|
||||
void *extern_obj, uint32_t *p_externref_idx);
|
||||
wasm_externref_obj2ref(wasm_module_inst_t module_inst, void *extern_obj,
|
||||
uint32_t *p_externref_idx);
|
||||
|
||||
/**
|
||||
* Delete external object registered by `wasm_externref_obj2ref`.
|
||||
|
@ -1528,7 +1594,6 @@ WASM_RUNTIME_API_EXTERN const uint8_t *
|
|||
wasm_runtime_get_custom_section(wasm_module_t const module_comm,
|
||||
const char *name, uint32_t *len);
|
||||
|
||||
|
||||
/**
|
||||
* Get WAMR semantic version
|
||||
*/
|
||||
|
@ -1544,8 +1609,8 @@ wasm_runtime_is_import_func_linked(const char *module_name,
|
|||
const char *func_name);
|
||||
|
||||
/**
|
||||
* Check whether an import global `(import <module_name> <global_name> (global ...))`
|
||||
* is linked or not with runtime registered natvie globals
|
||||
* Check whether an import global `(import <module_name> <global_name>
|
||||
* (global ...))` is linked or not with runtime registered natvie globals
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_is_import_global_linked(const char *module_name,
|
||||
|
@ -1559,8 +1624,7 @@ typedef enum {
|
|||
typedef void (*enlarge_memory_error_callback_t)(
|
||||
uint32_t inc_page_count, uint64_t current_memory_size,
|
||||
uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
|
||||
wasm_module_inst_t instance, wasm_exec_env_t exec_env,
|
||||
void* user_data);
|
||||
wasm_module_inst_t instance, wasm_exec_env_t exec_env, void *user_data);
|
||||
|
||||
/**
|
||||
* Setup callback invoked when memory.grow fails
|
||||
|
@ -1622,8 +1686,8 @@ wasm_runtime_set_enlarge_mem_error_callback(
|
|||
*/
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void *
|
||||
wasm_runtime_create_context_key(
|
||||
void (*dtor)(wasm_module_inst_t inst, void *ctx));
|
||||
wasm_runtime_create_context_key(void (*dtor)(wasm_module_inst_t inst,
|
||||
void *ctx));
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_destroy_context_key(void *key);
|
||||
|
@ -1684,16 +1748,64 @@ wasm_runtime_begin_blocking_op(wasm_exec_env_t exec_env);
|
|||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_end_blocking_op(wasm_exec_env_t exec_env);
|
||||
|
||||
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_set_module_name(wasm_module_t module, const char *name,
|
||||
char *error_buf, uint32_t error_buf_size);
|
||||
|
||||
/* return the most recently set module name or "" if never set before */
|
||||
WASM_RUNTIME_API_EXTERN const char*
|
||||
WASM_RUNTIME_API_EXTERN const char *
|
||||
wasm_runtime_get_module_name(wasm_module_t module);
|
||||
|
||||
/* clang-format on */
|
||||
/*
|
||||
* wasm_runtime_detect_native_stack_overflow
|
||||
*
|
||||
* Detect native stack shortage.
|
||||
* Ensure that the calling thread still has a reasonable amount of
|
||||
* native stack (WASM_STACK_GUARD_SIZE bytes) available.
|
||||
*
|
||||
* If enough stack is left, this function returns true.
|
||||
* Otherwise, this function raises a "native stack overflow" trap and
|
||||
* returns false.
|
||||
*
|
||||
* Note: please do not expect a very strict detection. it's a good idea
|
||||
* to give some margins. wasm_runtime_detect_native_stack_overflow itself
|
||||
* requires a small amount of stack to run.
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_detect_native_stack_overflow(wasm_exec_env_t exec_env);
|
||||
|
||||
/*
|
||||
* wasm_runtime_detect_native_stack_overflow_size
|
||||
*
|
||||
* Similar to wasm_runtime_detect_native_stack_overflow,
|
||||
* but use the caller-specified size instead of WASM_STACK_GUARD_SIZE.
|
||||
*
|
||||
* An expected usage:
|
||||
* ```c
|
||||
* __attribute__((noinline)) // inlining can break the stack check
|
||||
* void stack_hog(void)
|
||||
* {
|
||||
* // consume a lot of stack here
|
||||
* }
|
||||
*
|
||||
* void
|
||||
* stack_hog_wrapper(exec_env) {
|
||||
* // the amount of stack stack_hog would consume,
|
||||
* // plus a small margin
|
||||
* uint32_t size = 10000000;
|
||||
*
|
||||
* if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, size)) {
|
||||
* // wasm_runtime_detect_native_stack_overflow_size has raised
|
||||
* // a trap.
|
||||
* return;
|
||||
* }
|
||||
* stack_hog();
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_runtime_detect_native_stack_overflow_size(wasm_exec_env_t exec_env,
|
||||
uint32_t required_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -274,7 +274,7 @@ typedef struct InitializerExpression {
|
|||
*/
|
||||
typedef struct RefHeapType_TypeIdx {
|
||||
/* ref_type is REF_TYPE_HT_NULLABLE or
|
||||
REF_TYPE_HT_NON_NULLABLE, (0x6C or 0x6B) */
|
||||
REF_TYPE_HT_NON_NULLABLE, (0x63 or 0x64) */
|
||||
uint8 ref_type;
|
||||
/* true if ref_type is REF_TYPE_HT_NULLABLE */
|
||||
bool nullable;
|
||||
|
@ -288,7 +288,7 @@ typedef struct RefHeapType_TypeIdx {
|
|||
*/
|
||||
typedef struct RefHeapType_Common {
|
||||
/* ref_type is REF_TYPE_HT_NULLABLE or
|
||||
REF_TYPE_HT_NON_NULLABLE (0x6C or 0x6B) */
|
||||
REF_TYPE_HT_NON_NULLABLE (0x63 or 0x64) */
|
||||
uint8 ref_type;
|
||||
/* true if ref_type is REF_TYPE_HT_NULLABLE */
|
||||
bool nullable;
|
||||
|
@ -338,18 +338,24 @@ typedef struct WASMType {
|
|||
uint16 type_flag;
|
||||
|
||||
bool is_sub_final;
|
||||
/* How many types are referring to this type */
|
||||
uint16 ref_count;
|
||||
/* The inheritance depth */
|
||||
uint32 inherit_depth;
|
||||
uint16 inherit_depth;
|
||||
/* The root type */
|
||||
struct WASMType *root_type;
|
||||
/* The parent type */
|
||||
struct WASMType *parent_type;
|
||||
uint32 parent_type_idx;
|
||||
|
||||
/* number of internal types in the current rec group, if the type is not in
|
||||
* a recursive group, rec_count = 0 */
|
||||
/* The number of internal types in the current rec group, and if
|
||||
the type is not in a recursive group, rec_count is 1 since a
|
||||
single type definition is reinterpreted as a short-hand for a
|
||||
recursive group containing just one type */
|
||||
uint16 rec_count;
|
||||
uint16 rec_idx;
|
||||
/* The index of the begin type of this group */
|
||||
uint32 rec_begin_type_idx;
|
||||
} WASMType, *WASMTypePtr;
|
||||
#endif /* end of WASM_ENABLE_GC */
|
||||
|
||||
|
@ -375,9 +381,6 @@ typedef struct WASMFuncType {
|
|||
uint16 ref_type_map_count;
|
||||
WASMRefTypeMap *ref_type_maps;
|
||||
WASMRefTypeMap *result_ref_type_maps;
|
||||
/* minimal type index of the type equal to this type,
|
||||
used in type equal check in call_indirect opcode */
|
||||
uint32 min_type_idx_normalized;
|
||||
#else
|
||||
uint16 ref_count;
|
||||
#endif
|
||||
|
|
|
@ -1159,6 +1159,10 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
|||
uint8 *frame_ref;
|
||||
#endif
|
||||
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
|
||||
all_cell_num = local_cell_num;
|
||||
#if WASM_ENABLE_GC != 0
|
||||
all_cell_num += (local_cell_num + 3) / 4;
|
||||
|
@ -1290,6 +1294,14 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
|
|||
uintptr_t aux_stack_origin_boundary = 0;
|
||||
uintptr_t aux_stack_origin_bottom = 0;
|
||||
|
||||
/*
|
||||
* perform stack overflow check before calling
|
||||
* wasm_interp_call_func_bytecode recursively.
|
||||
*/
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sub_func_inst) {
|
||||
snprintf(buf, sizeof(buf),
|
||||
"failed to call unlinked import function (%s, %s)",
|
||||
|
@ -2212,6 +2224,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
WASMFuncType *cur_type, *cur_func_type;
|
||||
WASMTableInstance *tbl_inst;
|
||||
uint32 tbl_idx;
|
||||
|
||||
#if WASM_ENABLE_TAIL_CALL != 0
|
||||
opcode = *(frame_ip - 1);
|
||||
#endif
|
||||
|
@ -2282,8 +2295,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
goto got_exception;
|
||||
}
|
||||
#else
|
||||
if (cur_type->min_type_idx_normalized
|
||||
!= cur_func_type->min_type_idx_normalized) {
|
||||
if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
|
||||
wasm_set_exception(module, "indirect call type mismatch");
|
||||
goto got_exception;
|
||||
}
|
||||
|
@ -4202,7 +4214,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
else
|
||||
#endif
|
||||
{
|
||||
*(uint32 *)global_addr = aux_stack_top;
|
||||
*(uint32 *)global_addr = (uint32)aux_stack_top;
|
||||
frame_sp--;
|
||||
}
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
|
@ -7108,12 +7120,13 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
}
|
||||
argc = function->param_cell_num;
|
||||
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&prev_frame);
|
||||
#if !(defined(OS_ENABLE_HW_BOUND_CHECK) \
|
||||
&& WASM_DISABLE_STACK_HW_BOUND_CHECK == 0)
|
||||
if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
|
||||
wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
|
||||
"native stack overflow");
|
||||
#if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
|
||||
/*
|
||||
* wasm_runtime_detect_native_stack_overflow is done by
|
||||
* call_wasm_with_hw_bound_check.
|
||||
*/
|
||||
#else
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1167,6 +1167,10 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
|||
all_cell_num += (local_cell_num + 3) / 4;
|
||||
#endif
|
||||
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(frame =
|
||||
ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
|
||||
prev_frame)))
|
||||
|
@ -1275,6 +1279,14 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
|
|||
uintptr_t aux_stack_origin_boundary = 0;
|
||||
uintptr_t aux_stack_origin_bottom = 0;
|
||||
|
||||
/*
|
||||
* perform stack overflow check before calling
|
||||
* wasm_interp_call_func_bytecode recursively.
|
||||
*/
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sub_func_inst) {
|
||||
snprintf(buf, sizeof(buf),
|
||||
"failed to call unlinked import function (%s, %s)",
|
||||
|
@ -1736,8 +1748,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
goto got_exception;
|
||||
}
|
||||
#else
|
||||
if (cur_type->min_type_idx_normalized
|
||||
!= cur_func_type->min_type_idx_normalized) {
|
||||
if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
|
||||
wasm_set_exception(module, "indirect call type mismatch");
|
||||
goto got_exception;
|
||||
}
|
||||
|
@ -6082,12 +6093,13 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
}
|
||||
argc = function->param_cell_num;
|
||||
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&prev_frame);
|
||||
#if !(defined(OS_ENABLE_HW_BOUND_CHECK) \
|
||||
&& WASM_DISABLE_STACK_HW_BOUND_CHECK == 0)
|
||||
if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
|
||||
wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
|
||||
"native stack overflow");
|
||||
#if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
|
||||
/*
|
||||
* wasm_runtime_detect_native_stack_overflow is done by
|
||||
* call_wasm_with_hw_bound_check.
|
||||
*/
|
||||
#else
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -36,6 +36,21 @@
|
|||
#define TEMPLATE_READ_VALUE(Type, p) \
|
||||
(p += sizeof(Type), *(Type *)(p - sizeof(Type)))
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
static bool
|
||||
has_module_memory64(WASMModule *module)
|
||||
{
|
||||
/* TODO: multi-memories for now assuming the memory idx type is consistent
|
||||
* across multi-memories */
|
||||
if (module->import_memory_count > 0)
|
||||
return !!(module->import_memories[0].u.memory.flags & MEMORY64_FLAG);
|
||||
else if (module->memory_count > 0)
|
||||
return !!(module->memories[0].flags & MEMORY64_FLAG);
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
|
||||
{
|
||||
|
@ -278,7 +293,10 @@ type2str(uint8 type)
|
|||
static bool
|
||||
is_32bit_type(uint8 type)
|
||||
{
|
||||
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
|
||||
if (type == VALUE_TYPE_I32
|
||||
|| type == VALUE_TYPE_F32
|
||||
/* the operand stack is in polymorphic state */
|
||||
|| type == VALUE_TYPE_ANY
|
||||
#if WASM_ENABLE_GC != 0
|
||||
|| (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
|
||||
#elif WASM_ENABLE_REF_TYPES != 0
|
||||
|
@ -394,10 +412,10 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
|
|||
|
||||
#if WASM_ENABLE_GC != 0
|
||||
static bool
|
||||
check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
if (type_index >= module->type_count) {
|
||||
if (type_index >= type_count) {
|
||||
set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
|
||||
type_index);
|
||||
return false;
|
||||
|
@ -409,7 +427,8 @@ static bool
|
|||
check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
{
|
||||
if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
|
||||
if (!check_type_index(module, module->type_count, type_index, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
|
||||
|
@ -775,8 +794,8 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
|||
if (!is_byte_a_type(type1)) {
|
||||
p--;
|
||||
read_leb_uint32(p, p_end, type_idx);
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
error_buf_size))
|
||||
if (!check_type_index(module, module->type_count, type_idx,
|
||||
error_buf, error_buf_size))
|
||||
goto fail;
|
||||
|
||||
wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
|
||||
|
@ -902,7 +921,8 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
|||
uint32 field_count;
|
||||
read_leb_uint32(p, p_end, type_idx);
|
||||
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
if (!check_type_index(module, module->type_count,
|
||||
type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -966,7 +986,8 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
|||
read_leb_uint32(p, p_end, cur_value.type_index);
|
||||
type_idx = cur_value.type_index;
|
||||
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
if (!check_type_index(module, module->type_count,
|
||||
type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -1001,7 +1022,8 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
|||
read_leb_uint32(p, p_end, cur_value.type_index);
|
||||
type_idx = cur_value.type_index;
|
||||
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
if (!check_type_index(module, module->type_count,
|
||||
type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -1275,6 +1297,13 @@ destroy_array_type(WASMArrayType *type)
|
|||
static void
|
||||
destroy_wasm_type(WASMType *type)
|
||||
{
|
||||
if (type->ref_count > 1) {
|
||||
/* The type is referenced by other types
|
||||
of current wasm module */
|
||||
type->ref_count--;
|
||||
return;
|
||||
}
|
||||
|
||||
if (type->type_flag == WASM_TYPE_FUNC)
|
||||
destroy_func_type((WASMFuncType *)type);
|
||||
else if (type->type_flag == WASM_TYPE_STRUCT)
|
||||
|
@ -1289,8 +1318,9 @@ destroy_wasm_type(WASMType *type)
|
|||
/* Resolve (ref null ht) or (ref ht) */
|
||||
static bool
|
||||
resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
|
||||
WASMModule *module, bool nullable, WASMRefType *ref_type,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
WASMModule *module, uint32 type_count, bool nullable,
|
||||
WASMRefType *ref_type, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
{
|
||||
const uint8 *p = *p_buf, *p_end = buf_end;
|
||||
|
||||
|
@ -1301,8 +1331,9 @@ resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
|
|||
|
||||
if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
|
||||
/* heap type is (type i), i : typeidx, >= 0 */
|
||||
if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
|
||||
error_buf, error_buf_size)) {
|
||||
if (!check_type_index(module, type_count,
|
||||
ref_type->ref_ht_typeidx.type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1320,9 +1351,10 @@ fail:
|
|||
|
||||
static bool
|
||||
resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
|
||||
WASMModule *module, bool *p_need_ref_type_map,
|
||||
WASMRefType *ref_type, bool allow_packed_type,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
WASMModule *module, uint32 type_count,
|
||||
bool *p_need_ref_type_map, WASMRefType *ref_type,
|
||||
bool allow_packed_type, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
{
|
||||
const uint8 *p = *p_buf, *p_end = buf_end;
|
||||
uint8 type;
|
||||
|
@ -1334,8 +1366,8 @@ resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
|
|||
|
||||
if (wasm_is_reftype_htref_nullable(type)) {
|
||||
/* (ref null ht) */
|
||||
if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
|
||||
error_buf_size))
|
||||
if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
|
||||
ref_type, error_buf, error_buf_size))
|
||||
return false;
|
||||
if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
|
||||
*p_need_ref_type_map = true;
|
||||
|
@ -1351,8 +1383,8 @@ resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
|
|||
}
|
||||
else if (wasm_is_reftype_htref_non_nullable(type)) {
|
||||
/* (ref ht) */
|
||||
if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
|
||||
error_buf, error_buf_size))
|
||||
if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
|
||||
ref_type, error_buf, error_buf_size))
|
||||
return false;
|
||||
*p_need_ref_type_map = true;
|
||||
#if WASM_ENABLE_STRINGREF != 0
|
||||
|
@ -1401,7 +1433,8 @@ reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
|
|||
|
||||
static bool
|
||||
resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
||||
uint32 type_idx, char *error_buf, uint32 error_buf_size)
|
||||
uint32 type_count, uint32 type_idx, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
{
|
||||
const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
|
||||
uint32 param_count, result_count, i, j = 0;
|
||||
|
@ -1417,8 +1450,9 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|||
read_leb_uint32(p, p_end, param_count);
|
||||
p_org = p;
|
||||
for (i = 0; i < param_count; i++) {
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (need_ref_type_map)
|
||||
|
@ -1427,8 +1461,9 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|||
|
||||
read_leb_uint32(p, p_end, result_count);
|
||||
for (i = 0; i < result_count; i++) {
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (need_ref_type_map) {
|
||||
|
@ -1468,8 +1503,9 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|||
}
|
||||
|
||||
for (i = 0; i < param_count; i++) {
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
type->types[i] = ref_type.ref_type;
|
||||
|
@ -1485,8 +1521,9 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|||
|
||||
read_leb_uint32(p, p_end, result_count);
|
||||
for (i = 0; i < result_count; i++) {
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
type->types[param_count + i] = ref_type.ref_type;
|
||||
|
@ -1527,18 +1564,6 @@ resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|||
}
|
||||
#endif
|
||||
|
||||
/* Calculate the minimal type index of the type equal to this type */
|
||||
type->min_type_idx_normalized = type_idx;
|
||||
for (i = 0; i < type_idx; i++) {
|
||||
WASMFuncType *func_type = (WASMFuncType *)module->types[i];
|
||||
if (func_type->base_type.type_flag == WASM_TYPE_FUNC
|
||||
&& wasm_func_type_equal(type, func_type, module->types,
|
||||
type_idx + 1)) {
|
||||
type->min_type_idx_normalized = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*p_buf = p;
|
||||
|
||||
module->types[type_idx] = (WASMType *)type;
|
||||
|
@ -1552,8 +1577,8 @@ fail:
|
|||
|
||||
static bool
|
||||
resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
|
||||
WASMModule *module, uint32 type_idx, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
WASMModule *module, uint32 type_count, uint32 type_idx,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
|
||||
uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
|
||||
|
@ -1569,8 +1594,9 @@ resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
|
|||
read_leb_uint32(p, p_end, field_count);
|
||||
p_org = p;
|
||||
for (i = 0; i < field_count; i++) {
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, true, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count,
|
||||
&need_ref_type_map, &ref_type, true, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (need_ref_type_map)
|
||||
|
@ -1617,8 +1643,9 @@ resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
|
|||
|
||||
offset = (uint32)offsetof(WASMStructObject, field_data);
|
||||
for (i = 0; i < field_count; i++) {
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, true, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count,
|
||||
&need_ref_type_map, &ref_type, true, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
type->fields[i].field_type = ref_type.ref_type;
|
||||
|
@ -1671,8 +1698,8 @@ fail:
|
|||
|
||||
static bool
|
||||
resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
|
||||
WASMModule *module, uint32 type_idx, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
WASMModule *module, uint32 type_count, uint32 type_idx,
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
const uint8 *p = *p_buf, *p_end = buf_end;
|
||||
uint8 mutable;
|
||||
|
@ -1680,8 +1707,8 @@ resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
|
|||
WASMRefType ref_type;
|
||||
WASMArrayType *type = NULL;
|
||||
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
|
||||
true, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
|
||||
&ref_type, true, error_buf, error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1730,7 +1757,8 @@ init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
|
|||
int32 heap_type, char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
if (heap_type >= 0) {
|
||||
if (!check_type_index(module, heap_type, error_buf, error_buf_size)) {
|
||||
if (!check_type_index(module, module->type_count, heap_type, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
|
||||
|
@ -2010,6 +2038,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
|
||||
if (flag == DEFINED_TYPE_FUNC) {
|
||||
if (!resolve_func_type(&p, buf_end, module,
|
||||
processed_type_count + rec_count,
|
||||
processed_type_count + j, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
|
@ -2017,6 +2046,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
}
|
||||
else if (flag == DEFINED_TYPE_STRUCT) {
|
||||
if (!resolve_struct_type(&p, buf_end, module,
|
||||
processed_type_count + rec_count,
|
||||
processed_type_count + j,
|
||||
error_buf, error_buf_size)) {
|
||||
return false;
|
||||
|
@ -2024,6 +2054,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
}
|
||||
else if (flag == DEFINED_TYPE_ARRAY) {
|
||||
if (!resolve_array_type(&p, buf_end, module,
|
||||
processed_type_count + rec_count,
|
||||
processed_type_count + j, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
|
@ -2037,13 +2068,13 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
|
||||
cur_type = module->types[processed_type_count + j];
|
||||
|
||||
cur_type->ref_count = 1;
|
||||
cur_type->parent_type_idx = parent_type_idx;
|
||||
cur_type->is_sub_final = is_sub_final;
|
||||
|
||||
if (rec_count > 1) {
|
||||
cur_type->rec_count = rec_count;
|
||||
cur_type->rec_idx = j;
|
||||
}
|
||||
cur_type->rec_count = rec_count;
|
||||
cur_type->rec_idx = j;
|
||||
cur_type->rec_begin_type_idx = processed_type_count;
|
||||
}
|
||||
|
||||
/* resolve subtyping relationship in current rec group */
|
||||
|
@ -2055,6 +2086,11 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
module->types[cur_type->parent_type_idx];
|
||||
cur_type->parent_type = parent_type;
|
||||
cur_type->root_type = parent_type->root_type;
|
||||
if (parent_type->inherit_depth == UINT16_MAX) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"parent type's inherit depth too large");
|
||||
return false;
|
||||
}
|
||||
cur_type->inherit_depth = parent_type->inherit_depth + 1;
|
||||
}
|
||||
else {
|
||||
|
@ -2080,6 +2116,49 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
}
|
||||
}
|
||||
|
||||
/* If there is already an equivalence type or a group of equivalence
|
||||
recursive types created, use it or them instead */
|
||||
for (j = 0; j < processed_type_count;) {
|
||||
WASMType *src_type = module->types[j];
|
||||
WASMType *cur_type = module->types[processed_type_count];
|
||||
uint32 k, src_rec_count;
|
||||
|
||||
src_rec_count = src_type->rec_count;
|
||||
if (src_rec_count != rec_count) {
|
||||
/* no type equivalence */
|
||||
j += src_rec_count;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (k = 0; k < rec_count; k++) {
|
||||
src_type = module->types[j + k];
|
||||
cur_type = module->types[processed_type_count + k];
|
||||
if (!wasm_type_equal(src_type, cur_type, module->types,
|
||||
module->type_count)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k < rec_count) {
|
||||
/* no type equivalence */
|
||||
j += src_rec_count;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* type equivalence */
|
||||
for (k = 0; k < rec_count; k++) {
|
||||
if (module->types[j + k]->ref_count == UINT16_MAX) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"wasm type's ref count too large");
|
||||
return false;
|
||||
}
|
||||
destroy_wasm_type(module->types[processed_type_count + k]);
|
||||
module->types[processed_type_count + k] =
|
||||
module->types[j + k];
|
||||
module->types[j + k]->ref_count++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (rec_count > 1) {
|
||||
LOG_VERBOSE("Finished processing rec group [%d-%d]",
|
||||
processed_type_count,
|
||||
|
@ -2511,8 +2590,9 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
|
|||
return false;
|
||||
}
|
||||
#else /* else of WASM_ENABLE_GC == 0 */
|
||||
if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
|
||||
|
@ -2947,8 +3027,9 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
|
|||
declare_type = read_uint8(p);
|
||||
declare_mutable = read_uint8(p);
|
||||
#else
|
||||
if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
declare_type = ref_type.ref_type;
|
||||
|
@ -3050,8 +3131,9 @@ load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
|
|||
return false;
|
||||
}
|
||||
#else /* else of WASM_ENABLE_GC == 0 */
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
|
||||
false, error_buf, error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, module->type_count,
|
||||
&need_ref_type_map, &ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
table->elem_type = ref_type.ref_type;
|
||||
|
@ -3536,7 +3618,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
|
|||
type_index_org = type_index;
|
||||
#endif
|
||||
|
||||
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|
||||
#if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
|
||||
&& WASM_ENABLE_GC == 0
|
||||
type_index = wasm_get_smallest_type_idx(
|
||||
module->types, module->type_count, type_index);
|
||||
#endif
|
||||
|
@ -3577,8 +3660,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
|
|||
#endif
|
||||
#else
|
||||
if (!resolve_value_type(&p_code, buf_code_end, module,
|
||||
&need_ref_type_map, &ref_type, false,
|
||||
error_buf, error_buf_size)) {
|
||||
module->type_count, &need_ref_type_map,
|
||||
&ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
local_count += sub_local_count;
|
||||
|
@ -3664,8 +3748,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
|
|||
}
|
||||
#else
|
||||
if (!resolve_value_type(&p_code, buf_code_end, module,
|
||||
&need_ref_type_map, &ref_type, false,
|
||||
error_buf, error_buf_size)) {
|
||||
module->type_count, &need_ref_type_map,
|
||||
&ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (need_ref_type_map) {
|
||||
|
@ -3923,9 +4008,9 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
|
|||
global->type = read_uint8(p);
|
||||
mutable = read_uint8(p);
|
||||
#else
|
||||
if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
|
||||
&ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
if (!resolve_value_type(&p, p_end, module, module->type_count,
|
||||
&need_ref_type_map, &ref_type, false,
|
||||
error_buf, error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
global->type = ref_type.ref_type;
|
||||
|
@ -4231,8 +4316,8 @@ load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
|
|||
#else
|
||||
p--;
|
||||
if (!resolve_value_type((const uint8 **)&p, p_end, module,
|
||||
&need_ref_type_map, &elem_ref_type, false,
|
||||
error_buf, error_buf_size)) {
|
||||
module->type_count, &need_ref_type_map,
|
||||
&elem_ref_type, false, error_buf, error_buf_size)) {
|
||||
return false;
|
||||
}
|
||||
if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
|
||||
|
@ -4546,7 +4631,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
|
|||
bool is_passive = false;
|
||||
uint32 mem_flag;
|
||||
#endif
|
||||
uint8 mem_offset_type;
|
||||
uint8 mem_offset_type = VALUE_TYPE_I32;
|
||||
|
||||
read_leb_uint32(p, p_end, data_seg_count);
|
||||
|
||||
|
@ -5592,6 +5677,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
uint32 aux_data_end_global_index = (uint32)-1;
|
||||
uint32 aux_heap_base_global_index = (uint32)-1;
|
||||
WASMFuncType *func_type;
|
||||
uint8 malloc_free_io_type = VALUE_TYPE_I32;
|
||||
|
||||
/* Find code and function sections if have */
|
||||
while (section) {
|
||||
|
@ -5820,6 +5906,10 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
module->retain_function = (uint32)-1;
|
||||
|
||||
/* Resolve malloc/free function exported by wasm module */
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (has_module_memory64(module))
|
||||
malloc_free_io_type = VALUE_TYPE_I64;
|
||||
#endif
|
||||
export = module->exports;
|
||||
for (i = 0; i < module->export_count; i++, export ++) {
|
||||
if (export->kind == EXPORT_KIND_FUNC) {
|
||||
|
@ -5828,8 +5918,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
func_index = export->index - module->import_function_count;
|
||||
func_type = module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 1 && func_type->result_count == 1
|
||||
&& func_type->types[0] == VALUE_TYPE_I32
|
||||
&& func_type->types[1] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[0] == malloc_free_io_type
|
||||
&& func_type->types[1] == malloc_free_io_type) {
|
||||
bh_assert(module->malloc_function == (uint32)-1);
|
||||
module->malloc_function = export->index;
|
||||
LOG_VERBOSE("Found malloc function, name: %s, index: %u",
|
||||
|
@ -5842,9 +5932,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
func_index = export->index - module->import_function_count;
|
||||
func_type = module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 2 && func_type->result_count == 1
|
||||
&& func_type->types[0] == VALUE_TYPE_I32
|
||||
&& func_type->types[0] == malloc_free_io_type
|
||||
&& func_type->types[1] == VALUE_TYPE_I32
|
||||
&& func_type->types[2] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[2] == malloc_free_io_type) {
|
||||
uint32 j;
|
||||
WASMExport *export_tmp;
|
||||
|
||||
|
@ -5868,8 +5958,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 1
|
||||
&& func_type->result_count == 1
|
||||
&& func_type->types[0] == VALUE_TYPE_I32
|
||||
&& func_type->types[1] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[0] == malloc_free_io_type
|
||||
&& func_type->types[1] == malloc_free_io_type) {
|
||||
bh_assert(module->retain_function
|
||||
== (uint32)-1);
|
||||
module->retain_function = export_tmp->index;
|
||||
|
@ -5895,7 +5985,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
func_index = export->index - module->import_function_count;
|
||||
func_type = module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 1 && func_type->result_count == 0
|
||||
&& func_type->types[0] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[0] == malloc_free_io_type) {
|
||||
bh_assert(module->free_function == (uint32)-1);
|
||||
module->free_function = export->index;
|
||||
LOG_VERBOSE("Found free function, name: %s, index: %u",
|
||||
|
@ -10670,14 +10760,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
|||
func->param_cell_num, func->local_cell_num, func->ret_cell_num);
|
||||
#endif
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
bool is_memory64 = false;
|
||||
/* TODO: multi-memories for now assuming the memory idx type is consistent
|
||||
* across multi-memories */
|
||||
if (module->import_memory_count > 0)
|
||||
is_memory64 = module->import_memories[0].u.memory.flags & MEMORY64_FLAG;
|
||||
else if (module->memory_count > 0)
|
||||
is_memory64 = module->memories[0].flags & MEMORY64_FLAG;
|
||||
|
||||
bool is_memory64 = has_module_memory64(module);
|
||||
mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
|
||||
#else
|
||||
mem_offset_type = VALUE_TYPE_I32;
|
||||
|
@ -10827,7 +10910,8 @@ re_scan:
|
|||
p_org = p;
|
||||
p--;
|
||||
if (!resolve_value_type((const uint8 **)&p, p_end,
|
||||
module, &need_ref_type_map,
|
||||
module, module->type_count,
|
||||
&need_ref_type_map,
|
||||
&wasm_ref_type, false,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
|
@ -11254,8 +11338,10 @@ re_scan:
|
|||
BlockType block_type;
|
||||
|
||||
if (loader_ctx->csp_num < 2
|
||||
|| (loader_ctx->frame_csp - 1)->label_type
|
||||
!= LABEL_TYPE_IF) {
|
||||
/* the matched if isn't found */
|
||||
|| (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
|
||||
/* duplicated else is found */
|
||||
|| (loader_ctx->frame_csp - 1)->else_addr) {
|
||||
set_error_buf(
|
||||
error_buf, error_buf_size,
|
||||
"opcode else found without matched opcode if");
|
||||
|
@ -11303,8 +11389,8 @@ re_scan:
|
|||
bh_memcpy_s(loader_ctx->frame_offset, size,
|
||||
block->param_frame_offsets, size);
|
||||
loader_ctx->frame_offset += (size / sizeof(int16));
|
||||
loader_ctx->dynamic_offset = block->start_dynamic_offset;
|
||||
}
|
||||
loader_ctx->dynamic_offset = block->start_dynamic_offset;
|
||||
#endif
|
||||
|
||||
break;
|
||||
|
@ -11450,16 +11536,17 @@ re_scan:
|
|||
#endif
|
||||
POP_I32();
|
||||
|
||||
/* Get the default depth and check it */
|
||||
/* Get each depth and check it */
|
||||
p_org = p;
|
||||
for (i = 0; i <= count; i++) {
|
||||
read_leb_uint32(p, p_end, depth);
|
||||
}
|
||||
if (loader_ctx->csp_num < depth + 1) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"unknown label, "
|
||||
"unexpected end of section or function");
|
||||
goto fail;
|
||||
bh_assert(loader_ctx->csp_num > 0);
|
||||
if (loader_ctx->csp_num - 1 < depth) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"unknown label, "
|
||||
"unexpected end of section or function");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
p = p_org;
|
||||
|
||||
|
@ -11475,12 +11562,6 @@ re_scan:
|
|||
for (i = 0; i <= count; i++) {
|
||||
p_org = p;
|
||||
read_leb_uint32(p, p_end, depth);
|
||||
if (loader_ctx->csp_num < depth + 1) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"unknown label, "
|
||||
"unexpected end of section or function");
|
||||
goto fail;
|
||||
}
|
||||
p = p_org;
|
||||
|
||||
/* Get the target block's arity and check it */
|
||||
|
@ -11598,6 +11679,15 @@ re_scan:
|
|||
if (opcode == WASM_OP_CALL_REF
|
||||
|| opcode == WASM_OP_RETURN_CALL_REF) {
|
||||
read_leb_uint32(p, p_end, type_idx1);
|
||||
if (!check_type_index(module, module->type_count, type_idx1,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"unkown function type");
|
||||
goto fail;
|
||||
}
|
||||
if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
|
||||
&type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
|
@ -11606,8 +11696,8 @@ re_scan:
|
|||
if (type == VALUE_TYPE_ANY) {
|
||||
type_idx = type_idx1;
|
||||
}
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
if (!check_type_index(module, module->type_count, type_idx,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
|
||||
|
@ -11615,7 +11705,9 @@ re_scan:
|
|||
"unkown function type");
|
||||
goto fail;
|
||||
}
|
||||
if (type_idx != type_idx1) {
|
||||
if (!wasm_func_type_is_super_of(
|
||||
(WASMFuncType *)module->types[type_idx1],
|
||||
(WASMFuncType *)module->types[type_idx])) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"function type mismatch");
|
||||
goto fail;
|
||||
|
@ -11871,8 +11963,7 @@ re_scan:
|
|||
loader_ctx->reftype_map_num--;
|
||||
}
|
||||
#endif
|
||||
if (is_32bit_type(*(loader_ctx->frame_ref - 1))
|
||||
|| *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
|
||||
if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
|
||||
loader_ctx->frame_ref--;
|
||||
loader_ctx->stack_cell_num--;
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
|
@ -12055,8 +12146,9 @@ re_scan:
|
|||
#else
|
||||
p_org = p + 1;
|
||||
if (!resolve_value_type((const uint8 **)&p, p_end, module,
|
||||
&need_ref_type_map, &wasm_ref_type,
|
||||
false, error_buf, error_buf_size)) {
|
||||
module->type_count, &need_ref_type_map,
|
||||
&wasm_ref_type, false, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
type = wasm_ref_type.ref_type;
|
||||
|
@ -12223,8 +12315,8 @@ re_scan:
|
|||
#else
|
||||
read_leb_int32(p, p_end, heap_type);
|
||||
if (heap_type >= 0) {
|
||||
if (!check_type_index(module, heap_type, error_buf,
|
||||
error_buf_size)) {
|
||||
if (!check_type_index(module, module->type_count, heap_type,
|
||||
error_buf, error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
|
||||
|
@ -12315,33 +12407,58 @@ re_scan:
|
|||
goto fail;
|
||||
}
|
||||
|
||||
/* Refer to a forward-declared function */
|
||||
if (func_idx >= cur_func_idx + module->import_function_count) {
|
||||
/* Refer to a forward-declared function:
|
||||
the function must be an import, exported, or present in
|
||||
a table elem segment or global initializer to be used as
|
||||
the operand to ref.func */
|
||||
if (func_idx >= module->import_function_count) {
|
||||
WASMTableSeg *table_seg = module->table_segments;
|
||||
bool func_declared = false;
|
||||
uint32 j;
|
||||
|
||||
/* Check whether the function is declared in table segs,
|
||||
note that it doesn't matter whether the table seg's mode
|
||||
is passive, active or declarative. */
|
||||
for (i = 0; i < module->table_seg_count; i++, table_seg++) {
|
||||
if (table_seg->elem_type == VALUE_TYPE_FUNCREF
|
||||
for (i = 0; i < module->global_count; i++) {
|
||||
if (module->globals[i].type == VALUE_TYPE_FUNCREF
|
||||
&& module->globals[i].init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
&& module->globals[i].init_expr.u.u32 == func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!func_declared) {
|
||||
/* Check whether the function is declared in table segs,
|
||||
note that it doesn't matter whether the table seg's
|
||||
mode is passive, active or declarative. */
|
||||
for (i = 0; i < module->table_seg_count;
|
||||
i++, table_seg++) {
|
||||
if (table_seg->elem_type == VALUE_TYPE_FUNCREF
|
||||
#if WASM_ENABLE_GC != 0
|
||||
|| (table_seg->elem_type == REF_TYPE_HT_NON_NULLABLE
|
||||
&& table_seg->elem_ref_type->ref_ht_common
|
||||
.heap_type
|
||||
== HEAP_TYPE_FUNC)
|
||||
/* elem type is (ref null? func) or
|
||||
(ref null? $t) */
|
||||
|| ((table_seg->elem_type
|
||||
== REF_TYPE_HT_NON_NULLABLE
|
||||
|| table_seg->elem_type
|
||||
== REF_TYPE_HT_NULLABLE)
|
||||
&& (table_seg->elem_ref_type->ref_ht_common
|
||||
.heap_type
|
||||
== HEAP_TYPE_FUNC
|
||||
|| table_seg->elem_ref_type
|
||||
->ref_ht_common.heap_type
|
||||
> 0))
|
||||
#endif
|
||||
) {
|
||||
for (j = 0; j < table_seg->value_count; j++) {
|
||||
if (table_seg->init_values[j].u.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
) {
|
||||
for (j = 0; j < table_seg->value_count; j++) {
|
||||
if (table_seg->init_values[j].u.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!func_declared) {
|
||||
/* Check whether the function is exported */
|
||||
for (i = 0; i < module->export_count; i++) {
|
||||
|
@ -13008,6 +13125,7 @@ re_scan:
|
|||
break;
|
||||
|
||||
case WASM_OP_F32_CONST:
|
||||
CHECK_BUF(p, p_end, sizeof(float32));
|
||||
p += sizeof(float32);
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
skip_label();
|
||||
|
@ -13026,6 +13144,7 @@ re_scan:
|
|||
break;
|
||||
|
||||
case WASM_OP_F64_CONST:
|
||||
CHECK_BUF(p, p_end, sizeof(float64));
|
||||
p += sizeof(float64);
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
skip_label();
|
||||
|
@ -13286,7 +13405,8 @@ re_scan:
|
|||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
emit_uint32(loader_ctx, type_idx);
|
||||
#endif
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
if (!check_type_index(module, module->type_count,
|
||||
type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -13372,7 +13492,8 @@ re_scan:
|
|||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
emit_uint32(loader_ctx, type_idx);
|
||||
#endif
|
||||
if (!check_type_index(module, type_idx, error_buf,
|
||||
if (!check_type_index(module, module->type_count,
|
||||
type_idx, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -13785,7 +13906,8 @@ re_scan:
|
|||
emit_uint32(loader_ctx, (uint32)heap_type);
|
||||
#endif
|
||||
if (heap_type >= 0) {
|
||||
if (!check_type_index(module, heap_type, error_buf,
|
||||
if (!check_type_index(module, module->type_count,
|
||||
heap_type, error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -14356,6 +14478,7 @@ re_scan:
|
|||
}
|
||||
case WASM_OP_MEMORY_COPY:
|
||||
{
|
||||
CHECK_BUF(p, p_end, sizeof(int16));
|
||||
/* both src and dst memory index should be 0 */
|
||||
if (*(int16 *)p != 0x0000)
|
||||
goto fail_zero_byte_expected;
|
||||
|
@ -15129,13 +15252,6 @@ re_scan:
|
|||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_narrow_i64x2_s:
|
||||
case SIMD_i32x4_narrow_i64x2_u:
|
||||
{
|
||||
POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMD_i32x4_extend_low_i16x8_s:
|
||||
case SIMD_i32x4_extend_high_i16x8_s:
|
||||
case SIMD_i32x4_extend_low_i16x8_u:
|
||||
|
@ -15162,7 +15278,6 @@ re_scan:
|
|||
case SIMD_i32x4_max_s:
|
||||
case SIMD_i32x4_max_u:
|
||||
case SIMD_i32x4_dot_i16x8_s:
|
||||
case SIMD_i32x4_avgr_u:
|
||||
case SIMD_i32x4_extmul_low_i16x8_s:
|
||||
case SIMD_i32x4_extmul_high_i16x8_s:
|
||||
case SIMD_i32x4_extmul_low_i16x8_u:
|
||||
|
@ -15226,7 +15341,6 @@ re_scan:
|
|||
/* f32x4 operation */
|
||||
case SIMD_f32x4_abs:
|
||||
case SIMD_f32x4_neg:
|
||||
case SIMD_f32x4_round:
|
||||
case SIMD_f32x4_sqrt:
|
||||
{
|
||||
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
||||
|
@ -15249,7 +15363,6 @@ re_scan:
|
|||
/* f64x2 operation */
|
||||
case SIMD_f64x2_abs:
|
||||
case SIMD_f64x2_neg:
|
||||
case SIMD_f64x2_round:
|
||||
case SIMD_f64x2_sqrt:
|
||||
{
|
||||
POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
|
||||
|
|
|
@ -25,6 +25,21 @@
|
|||
#define TEMPLATE_READ_VALUE(Type, p) \
|
||||
(p += sizeof(Type), *(Type *)(p - sizeof(Type)))
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
static bool
|
||||
has_module_memory64(WASMModule *module)
|
||||
{
|
||||
/* TODO: multi-memories for now assuming the memory idx type is consistent
|
||||
* across multi-memories */
|
||||
if (module->import_memory_count > 0)
|
||||
return !!(module->import_memories[0].u.memory.flags & MEMORY64_FLAG);
|
||||
else if (module->memory_count > 0)
|
||||
return !!(module->memories[0].flags & MEMORY64_FLAG);
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
|
||||
{
|
||||
|
@ -52,7 +67,10 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
|
|||
static bool
|
||||
is_32bit_type(uint8 type)
|
||||
{
|
||||
if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
|
||||
if (type == VALUE_TYPE_I32
|
||||
|| type == VALUE_TYPE_F32
|
||||
/* the operand stack is in polymorphic state */
|
||||
|| type == VALUE_TYPE_ANY
|
||||
#if WASM_ENABLE_REF_TYPES != 0
|
||||
|| type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
|
||||
#endif
|
||||
|
@ -1761,7 +1779,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
|
|||
bool is_passive = false;
|
||||
uint32 mem_flag;
|
||||
#endif
|
||||
uint8 mem_offset_type;
|
||||
uint8 mem_offset_type = VALUE_TYPE_I32;
|
||||
|
||||
read_leb_uint32(p, p_end, data_seg_count);
|
||||
|
||||
|
@ -2573,6 +2591,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
uint32 aux_data_end_global_index = (uint32)-1;
|
||||
uint32 aux_heap_base_global_index = (uint32)-1;
|
||||
WASMFuncType *func_type;
|
||||
uint8 malloc_free_io_type = VALUE_TYPE_I32;
|
||||
|
||||
/* Find code and function sections if have */
|
||||
while (section) {
|
||||
|
@ -2781,6 +2800,10 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
module->retain_function = (uint32)-1;
|
||||
|
||||
/* Resolve malloc/free function exported by wasm module */
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (has_module_memory64(module))
|
||||
malloc_free_io_type = VALUE_TYPE_I64;
|
||||
#endif
|
||||
export = module->exports;
|
||||
for (i = 0; i < module->export_count; i++, export ++) {
|
||||
if (export->kind == EXPORT_KIND_FUNC) {
|
||||
|
@ -2789,8 +2812,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
func_index = export->index - module->import_function_count;
|
||||
func_type = module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 1 && func_type->result_count == 1
|
||||
&& func_type->types[0] == VALUE_TYPE_I32
|
||||
&& func_type->types[1] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[0] == malloc_free_io_type
|
||||
&& func_type->types[1] == malloc_free_io_type) {
|
||||
bh_assert(module->malloc_function == (uint32)-1);
|
||||
module->malloc_function = export->index;
|
||||
LOG_VERBOSE("Found malloc function, name: %s, index: %u",
|
||||
|
@ -2803,9 +2826,9 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
func_index = export->index - module->import_function_count;
|
||||
func_type = module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 2 && func_type->result_count == 1
|
||||
&& func_type->types[0] == VALUE_TYPE_I32
|
||||
&& func_type->types[0] == malloc_free_io_type
|
||||
&& func_type->types[1] == VALUE_TYPE_I32
|
||||
&& func_type->types[2] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[2] == malloc_free_io_type) {
|
||||
uint32 j;
|
||||
WASMExport *export_tmp;
|
||||
|
||||
|
@ -2829,8 +2852,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 1
|
||||
&& func_type->result_count == 1
|
||||
&& func_type->types[0] == VALUE_TYPE_I32
|
||||
&& func_type->types[1] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[0] == malloc_free_io_type
|
||||
&& func_type->types[1] == malloc_free_io_type) {
|
||||
bh_assert(module->retain_function
|
||||
== (uint32)-1);
|
||||
module->retain_function = export_tmp->index;
|
||||
|
@ -2856,7 +2879,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
|
|||
func_index = export->index - module->import_function_count;
|
||||
func_type = module->functions[func_index]->func_type;
|
||||
if (func_type->param_count == 1 && func_type->result_count == 0
|
||||
&& func_type->types[0] == VALUE_TYPE_I32) {
|
||||
&& func_type->types[0] == malloc_free_io_type) {
|
||||
bh_assert(module->free_function == (uint32)-1);
|
||||
module->free_function = export->index;
|
||||
LOG_VERBOSE("Found free function, name: %s, index: %u",
|
||||
|
@ -4217,7 +4240,7 @@ wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
|
|||
ctx->frame_ref--;
|
||||
ctx->stack_cell_num--;
|
||||
|
||||
if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
|
||||
if (is_32bit_type(type))
|
||||
return true;
|
||||
|
||||
ctx->frame_ref--;
|
||||
|
@ -5159,10 +5182,21 @@ fail:
|
|||
goto fail; \
|
||||
} while (0)
|
||||
|
||||
#define PUSH_MEM_OFFSET() PUSH_OFFSET_TYPE(mem_offset_type)
|
||||
#define PUSH_MEM_OFFSET() \
|
||||
do { \
|
||||
if (!wasm_loader_push_frame_ref_offset(loader_ctx, mem_offset_type, \
|
||||
disable_emit, operand_offset, \
|
||||
error_buf, error_buf_size)) \
|
||||
goto fail; \
|
||||
} while (0)
|
||||
#define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
|
||||
|
||||
#define POP_MEM_OFFSET() POP_OFFSET_TYPE(mem_offset_type)
|
||||
#define POP_MEM_OFFSET() \
|
||||
do { \
|
||||
if (!wasm_loader_pop_frame_ref_offset(loader_ctx, mem_offset_type, \
|
||||
error_buf, error_buf_size)) \
|
||||
goto fail; \
|
||||
} while (0)
|
||||
|
||||
#define POP_AND_PUSH(type_pop, type_push) \
|
||||
do { \
|
||||
|
@ -5906,14 +5940,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
|
|||
func->param_cell_num, func->local_cell_num, func->ret_cell_num);
|
||||
#endif
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
bool is_memory64 = false;
|
||||
/* TODO: multi-memories for now assuming the memory idx type is consistent
|
||||
* across multi-memories */
|
||||
if (module->import_memory_count > 0)
|
||||
is_memory64 = module->import_memories[0].u.memory.flags & MEMORY64_FLAG;
|
||||
else if (module->memory_count > 0)
|
||||
is_memory64 = module->memories[0].flags & MEMORY64_FLAG;
|
||||
|
||||
bool is_memory64 = has_module_memory64(module);
|
||||
mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
|
||||
#else
|
||||
mem_offset_type = VALUE_TYPE_I32;
|
||||
|
@ -6190,8 +6217,11 @@ re_scan:
|
|||
BranchBlock *block = NULL;
|
||||
BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
|
||||
bh_assert(loader_ctx->csp_num >= 2
|
||||
/* the matched if is found */
|
||||
&& (loader_ctx->frame_csp - 1)->label_type
|
||||
== LABEL_TYPE_IF);
|
||||
== LABEL_TYPE_IF
|
||||
/* duplicated else isn't found */
|
||||
&& !(loader_ctx->frame_csp - 1)->else_addr);
|
||||
block = loader_ctx->frame_csp - 1;
|
||||
|
||||
/* check whether if branch's stack matches its result type */
|
||||
|
@ -6226,8 +6256,8 @@ re_scan:
|
|||
bh_memcpy_s(loader_ctx->frame_offset, size,
|
||||
block->param_frame_offsets, size);
|
||||
loader_ctx->frame_offset += (size / sizeof(int16));
|
||||
loader_ctx->dynamic_offset = block->start_dynamic_offset;
|
||||
}
|
||||
loader_ctx->dynamic_offset = block->start_dynamic_offset;
|
||||
#endif
|
||||
|
||||
break;
|
||||
|
@ -6324,13 +6354,11 @@ re_scan:
|
|||
case WASM_OP_BR_TABLE:
|
||||
{
|
||||
uint8 *ret_types = NULL;
|
||||
uint32 ret_count = 0;
|
||||
uint32 ret_count = 0, depth = 0;
|
||||
#if WASM_ENABLE_FAST_INTERP == 0
|
||||
uint8 *p_depth_begin, *p_depth;
|
||||
uint32 depth, j;
|
||||
BrTableCache *br_table_cache = NULL;
|
||||
|
||||
p_org = p - 1;
|
||||
uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
|
||||
uint32 j;
|
||||
#endif
|
||||
|
||||
read_leb_uint32(p, p_end, count);
|
||||
|
@ -6339,6 +6367,16 @@ re_scan:
|
|||
#endif
|
||||
POP_I32();
|
||||
|
||||
/* Get each depth and check it */
|
||||
p_org = p;
|
||||
for (i = 0; i <= count; i++) {
|
||||
read_leb_uint32(p, p_end, depth);
|
||||
bh_assert(loader_ctx->csp_num > 0);
|
||||
bh_assert(loader_ctx->csp_num - 1 >= depth);
|
||||
(void)depth;
|
||||
}
|
||||
p = p_org;
|
||||
|
||||
#if WASM_ENABLE_FAST_INTERP == 0
|
||||
p_depth_begin = p_depth = p;
|
||||
#endif
|
||||
|
@ -6364,8 +6402,8 @@ re_scan:
|
|||
error_buf, error_buf_size))) {
|
||||
goto fail;
|
||||
}
|
||||
*p_org = EXT_OP_BR_TABLE_CACHE;
|
||||
br_table_cache->br_table_op_addr = p_org;
|
||||
*p_opcode = EXT_OP_BR_TABLE_CACHE;
|
||||
br_table_cache->br_table_op_addr = p_opcode;
|
||||
br_table_cache->br_count = count;
|
||||
/* Copy previous depths which are one byte */
|
||||
for (j = 0; j < i; j++) {
|
||||
|
@ -6596,8 +6634,7 @@ re_scan:
|
|||
&& !cur_block->is_stack_polymorphic));
|
||||
|
||||
if (available_stack_cell > 0) {
|
||||
if (is_32bit_type(*(loader_ctx->frame_ref - 1))
|
||||
|| *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
|
||||
if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
|
||||
loader_ctx->frame_ref--;
|
||||
loader_ctx->stack_cell_num--;
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
|
@ -6903,26 +6940,43 @@ re_scan:
|
|||
goto fail;
|
||||
}
|
||||
|
||||
/* Refer to a forward-declared function */
|
||||
if (func_idx >= cur_func_idx + module->import_function_count) {
|
||||
/* Refer to a forward-declared function:
|
||||
the function must be an import, exported, or present in
|
||||
a table elem segment or global initializer to be used as
|
||||
the operand to ref.func */
|
||||
if (func_idx >= module->import_function_count) {
|
||||
WASMTableSeg *table_seg = module->table_segments;
|
||||
bool func_declared = false;
|
||||
uint32 j;
|
||||
|
||||
/* Check whether the function is declared in table segs,
|
||||
note that it doesn't matter whether the table seg's mode
|
||||
is passive, active or declarative. */
|
||||
for (i = 0; i < module->table_seg_count; i++, table_seg++) {
|
||||
if (table_seg->elem_type == VALUE_TYPE_FUNCREF) {
|
||||
for (j = 0; j < table_seg->value_count; j++) {
|
||||
if (table_seg->init_values[j].u.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
for (i = 0; i < module->global_count; i++) {
|
||||
if (module->globals[i].type == VALUE_TYPE_FUNCREF
|
||||
&& module->globals[i].init_expr.init_expr_type
|
||||
== INIT_EXPR_TYPE_FUNCREF_CONST
|
||||
&& module->globals[i].init_expr.u.u32 == func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!func_declared) {
|
||||
/* Check whether the function is declared in table segs,
|
||||
note that it doesn't matter whether the table seg's
|
||||
mode is passive, active or declarative. */
|
||||
for (i = 0; i < module->table_seg_count;
|
||||
i++, table_seg++) {
|
||||
if (table_seg->elem_type == VALUE_TYPE_FUNCREF) {
|
||||
for (j = 0; j < table_seg->value_count; j++) {
|
||||
if (table_seg->init_values[j].u.ref_index
|
||||
== func_idx) {
|
||||
func_declared = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!func_declared) {
|
||||
/* Check whether the function is exported */
|
||||
for (i = 0; i < module->export_count; i++) {
|
||||
|
@ -7351,6 +7405,7 @@ re_scan:
|
|||
break;
|
||||
|
||||
case WASM_OP_F32_CONST:
|
||||
CHECK_BUF(p, p_end, sizeof(float32));
|
||||
p += sizeof(float32);
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
skip_label();
|
||||
|
@ -7369,6 +7424,7 @@ re_scan:
|
|||
break;
|
||||
|
||||
case WASM_OP_F64_CONST:
|
||||
CHECK_BUF(p, p_end, sizeof(float64));
|
||||
p += sizeof(float64);
|
||||
#if WASM_ENABLE_FAST_INTERP != 0
|
||||
skip_label();
|
||||
|
@ -7676,6 +7732,7 @@ re_scan:
|
|||
}
|
||||
case WASM_OP_MEMORY_COPY:
|
||||
{
|
||||
CHECK_BUF(p, p_end, sizeof(int16));
|
||||
/* both src and dst memory index should be 0 */
|
||||
bh_assert(*(int16 *)p == 0x0000);
|
||||
p += 2;
|
||||
|
|
|
@ -593,8 +593,8 @@ typedef enum WASMSimdEXTOpcode {
|
|||
/* placeholder = 0xa2 */
|
||||
SIMD_i32x4_all_true = 0xa3,
|
||||
SIMD_i32x4_bitmask = 0xa4,
|
||||
SIMD_i32x4_narrow_i64x2_s = 0xa5,
|
||||
SIMD_i32x4_narrow_i64x2_u = 0xa6,
|
||||
/* placeholder = 0xa5 */
|
||||
/* placeholder = 0xa6 */
|
||||
SIMD_i32x4_extend_low_i16x8_s = 0xa7,
|
||||
SIMD_i32x4_extend_high_i16x8_s = 0xa8,
|
||||
SIMD_i32x4_extend_low_i16x8_u = 0xa9,
|
||||
|
@ -603,19 +603,19 @@ typedef enum WASMSimdEXTOpcode {
|
|||
SIMD_i32x4_shr_s = 0xac,
|
||||
SIMD_i32x4_shr_u = 0xad,
|
||||
SIMD_i32x4_add = 0xae,
|
||||
SIMD_i32x4_add_sat_s = 0xaf,
|
||||
SIMD_i32x4_add_sat_u = 0xb0,
|
||||
/* placeholder = 0xaf */
|
||||
/* placeholder = 0xb0 */
|
||||
SIMD_i32x4_sub = 0xb1,
|
||||
SIMD_i32x4_sub_sat_s = 0xb2,
|
||||
SIMD_i32x4_sub_sat_u = 0xb3,
|
||||
/* placeholder = 0xb4 */
|
||||
/* placeholder = 0xb2 */
|
||||
/* placeholder = 0xb3 */
|
||||
/* placeholder = 0xb4 */
|
||||
SIMD_i32x4_mul = 0xb5,
|
||||
SIMD_i32x4_min_s = 0xb6,
|
||||
SIMD_i32x4_min_u = 0xb7,
|
||||
SIMD_i32x4_max_s = 0xb8,
|
||||
SIMD_i32x4_max_u = 0xb9,
|
||||
SIMD_i32x4_dot_i16x8_s = 0xba,
|
||||
SIMD_i32x4_avgr_u = 0xbb,
|
||||
/* placeholder = 0xbb */
|
||||
SIMD_i32x4_extmul_low_i16x8_s = 0xbc,
|
||||
SIMD_i32x4_extmul_high_i16x8_s = 0xbd,
|
||||
SIMD_i32x4_extmul_low_i16x8_u = 0xbe,
|
||||
|
@ -658,7 +658,7 @@ typedef enum WASMSimdEXTOpcode {
|
|||
/* f32x4 operation */
|
||||
SIMD_f32x4_abs = 0xe0,
|
||||
SIMD_f32x4_neg = 0xe1,
|
||||
SIMD_f32x4_round = 0xe2,
|
||||
/* placeholder = 0xe2 */
|
||||
SIMD_f32x4_sqrt = 0xe3,
|
||||
SIMD_f32x4_add = 0xe4,
|
||||
SIMD_f32x4_sub = 0xe5,
|
||||
|
@ -672,7 +672,7 @@ typedef enum WASMSimdEXTOpcode {
|
|||
/* f64x2 operation */
|
||||
SIMD_f64x2_abs = 0xec,
|
||||
SIMD_f64x2_neg = 0xed,
|
||||
SIMD_f64x2_round = 0xee,
|
||||
/* placeholder = 0xee */
|
||||
SIMD_f64x2_sqrt = 0xef,
|
||||
SIMD_f64x2_add = 0xf0,
|
||||
SIMD_f64x2_sub = 0xf1,
|
||||
|
|
|
@ -1400,30 +1400,39 @@ fail:
|
|||
static bool
|
||||
execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
WASMFunctionInstance *malloc_func,
|
||||
WASMFunctionInstance *retain_func, uint32 size,
|
||||
uint32 *p_result)
|
||||
WASMFunctionInstance *retain_func, uint64 size,
|
||||
uint64 *p_result)
|
||||
{
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2], argc;
|
||||
uint32 argv[3], argc;
|
||||
bool ret;
|
||||
|
||||
argv[0] = size;
|
||||
argc = 1;
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
bool is_memory64 = module_inst->memories[0]->is_memory64;
|
||||
if (is_memory64) {
|
||||
argc = 2;
|
||||
PUT_I64_TO_ADDR(&argv[0], size);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
argc = 1;
|
||||
argv[0] = size;
|
||||
}
|
||||
|
||||
/* if __retain is exported, then this module is compiled by
|
||||
assemblyscript, the memory should be managed by as's runtime,
|
||||
in this case we need to call the retain function after malloc
|
||||
the memory */
|
||||
if (retain_func) {
|
||||
/* the malloc functino from assemblyscript is:
|
||||
/* the malloc function from assemblyscript is:
|
||||
function __new(size: usize, id: u32)
|
||||
id = 0 means this is an ArrayBuffer object */
|
||||
argv[1] = 0;
|
||||
argc = 2;
|
||||
argv[argc] = 0;
|
||||
argc++;
|
||||
}
|
||||
|
||||
if (exec_env) {
|
||||
|
@ -1475,24 +1484,42 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
|
||||
if (ret)
|
||||
*p_result = argv[0];
|
||||
if (ret) {
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (is_memory64)
|
||||
*p_result = GET_I64_FROM_ADDR(&argv[0]);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
*p_result = argv[0];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool
|
||||
execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
WASMFunctionInstance *free_func, uint32 offset)
|
||||
WASMFunctionInstance *free_func, uint64 offset)
|
||||
{
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2];
|
||||
uint32 argv[2], argc;
|
||||
bool ret;
|
||||
|
||||
argv[0] = offset;
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
if (module_inst->memories[0]->is_memory64) {
|
||||
PUT_I64_TO_ADDR(&argv[0], offset);
|
||||
argc = 2;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
argv[0] = (uint32)offset;
|
||||
argc = 1;
|
||||
}
|
||||
|
||||
if (exec_env) {
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
|
@ -1531,7 +1558,7 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
}
|
||||
}
|
||||
|
||||
ret = wasm_call_function(exec_env, free_func, 1, argv);
|
||||
ret = wasm_call_function(exec_env, free_func, argc, argv);
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
|
@ -3112,8 +3139,6 @@ call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
|
|||
{
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
|
||||
uint32 page_size = os_getpagesize();
|
||||
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
|
||||
WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
|
||||
uint8 *prev_top = exec_env->wasm_stack.top;
|
||||
#ifdef BH_PLATFORM_WINDOWS
|
||||
|
@ -3126,10 +3151,7 @@ call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
|
|||
/* Check native stack overflow firstly to ensure we have enough
|
||||
native stack to run the following codes before actually calling
|
||||
the aot function in invokeNative function. */
|
||||
RECORD_STACK_USAGE(exec_env, (uint8 *)&exec_env_tls);
|
||||
if ((uint8 *)&exec_env_tls < exec_env->native_stack_boundary
|
||||
+ page_size * (guard_page_count + 1)) {
|
||||
wasm_set_exception(module_inst, "native stack overflow");
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -3336,7 +3358,7 @@ wasm_module_malloc_internal(WASMModuleInstance *module_inst,
|
|||
{
|
||||
WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
|
||||
uint8 *addr = NULL;
|
||||
uint32 offset = 0;
|
||||
uint64 offset = 0;
|
||||
|
||||
/* TODO: Memory64 size check based on memory idx type */
|
||||
bh_assert(size <= UINT32_MAX);
|
||||
|
@ -3352,7 +3374,7 @@ wasm_module_malloc_internal(WASMModuleInstance *module_inst,
|
|||
else if (module_inst->e->malloc_function && module_inst->e->free_function) {
|
||||
if (!execute_malloc_function(
|
||||
module_inst, exec_env, module_inst->e->malloc_function,
|
||||
module_inst->e->retain_function, (uint32)size, &offset)) {
|
||||
module_inst->e->retain_function, size, &offset)) {
|
||||
return 0;
|
||||
}
|
||||
/* If we use app's malloc function,
|
||||
|
@ -3452,7 +3474,7 @@ wasm_module_free_internal(WASMModuleInstance *module_inst,
|
|||
&& module_inst->e->free_function && memory->memory_data <= addr
|
||||
&& addr < memory_data_end) {
|
||||
execute_free_function(module_inst, exec_env,
|
||||
module_inst->e->free_function, (uint32)ptr);
|
||||
module_inst->e->free_function, ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4353,11 +4375,21 @@ llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
|
|||
}
|
||||
|
||||
if (tbl_inst->cur_size > UINT32_MAX - inc_size) { /* integer overflow */
|
||||
#if WASM_ENABLE_SPEC_TEST == 0
|
||||
LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
|
||||
") failed because of integer overflow",
|
||||
tbl_inst->cur_size, inc_size);
|
||||
#endif
|
||||
return (uint32)-1;
|
||||
}
|
||||
|
||||
total_size = tbl_inst->cur_size + inc_size;
|
||||
if (total_size > tbl_inst->max_size) {
|
||||
#if WASM_ENABLE_SPEC_TEST == 0
|
||||
LOG_WARNING("table grow (%" PRIu32 "-> %" PRIu32
|
||||
") failed because of over max size",
|
||||
tbl_inst->cur_size, inc_size);
|
||||
#endif
|
||||
return (uint32)-1;
|
||||
}
|
||||
|
||||
|
@ -4394,6 +4426,22 @@ llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
|
|||
return wasm_obj_is_instance_of(gc_obj, type_index, types, type_count);
|
||||
}
|
||||
|
||||
bool
|
||||
llvm_jit_func_type_is_super_of(WASMModuleInstance *module_inst,
|
||||
uint32 type_idx1, uint32 type_idx2)
|
||||
{
|
||||
WASMModule *module = module_inst->module;
|
||||
WASMType **types = module->types;
|
||||
|
||||
if (type_idx1 == type_idx2)
|
||||
return true;
|
||||
|
||||
bh_assert(types[type_idx1]->type_flag == WASM_TYPE_FUNC);
|
||||
bh_assert(types[type_idx2]->type_flag == WASM_TYPE_FUNC);
|
||||
return wasm_func_type_is_super_of((WASMFuncType *)types[type_idx1],
|
||||
(WASMFuncType *)types[type_idx2]);
|
||||
}
|
||||
|
||||
WASMRttTypeRef
|
||||
llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index)
|
||||
{
|
||||
|
|
|
@ -811,6 +811,11 @@ bool
|
|||
llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
|
||||
WASMObjectRef gc_obj, uint32 type_index);
|
||||
|
||||
/* Whether func type1 is one of super types of func type2 */
|
||||
bool
|
||||
llvm_jit_func_type_is_super_of(WASMModuleInstance *module_inst,
|
||||
uint32 type_idx1, uint32 type_idx2);
|
||||
|
||||
WASMRttTypeRef
|
||||
llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index);
|
||||
|
||||
|
|
|
@ -823,7 +823,7 @@ os_fadvise(os_file_handle handle, __wasi_filesize_t offset,
|
|||
|
||||
int ret = posix_fadvise(handle, (off_t)offset, (off_t)length, nadvice);
|
||||
|
||||
if (ret < 0)
|
||||
if (ret != 0)
|
||||
return convert_errno(ret);
|
||||
|
||||
return __WASI_ESUCCESS;
|
||||
|
@ -920,7 +920,12 @@ os_readdir(os_dir_stream dir_stream, __wasi_dirent_t *entry,
|
|||
|
||||
if (dent == NULL) {
|
||||
*d_name = NULL;
|
||||
return convert_errno(errno);
|
||||
if (errno != 0) {
|
||||
return convert_errno(errno);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
long offset = (__wasi_dircookie_t)telldir(dir_stream);
|
||||
|
|
|
@ -445,9 +445,6 @@ os_thread_get_stack_boundary()
|
|||
pthread_attr_destroy(&attr);
|
||||
if (stack_size > max_stack_size)
|
||||
addr = addr + stack_size - max_stack_size;
|
||||
if (guard_size < (size_t)page_size)
|
||||
/* Reserved 1 guard page at least for safety */
|
||||
guard_size = (size_t)page_size;
|
||||
addr += guard_size;
|
||||
}
|
||||
(void)stack_size;
|
||||
|
@ -466,8 +463,6 @@ os_thread_get_stack_boundary()
|
|||
stack_size = max_stack_size;
|
||||
|
||||
addr -= stack_size;
|
||||
/* Reserved 1 guard page at least for safety */
|
||||
addr += page_size;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
88
core/shared/platform/esp-idf/espidf_clock.c
Normal file
88
core/shared/platform/esp-idf/espidf_clock.c
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright (C) 2023 Amazon Inc. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "libc_errno.h"
|
||||
#include "platform_api_extension.h"
|
||||
|
||||
#define NANOSECONDS_PER_SECOND 1000000000ULL
|
||||
|
||||
static __wasi_errno_t
|
||||
wasi_clockid_to_clockid(__wasi_clockid_t in, clockid_t *out)
|
||||
{
|
||||
switch (in) {
|
||||
case __WASI_CLOCK_MONOTONIC:
|
||||
*out = CLOCK_MONOTONIC;
|
||||
return __WASI_ESUCCESS;
|
||||
case __WASI_CLOCK_REALTIME:
|
||||
*out = CLOCK_REALTIME;
|
||||
return __WASI_ESUCCESS;
|
||||
case __WASI_CLOCK_PROCESS_CPUTIME_ID:
|
||||
#if defined(CLOCK_PROCESS_CPUTIME_ID)
|
||||
*out = CLOCK_PROCESS_CPUTIME_ID;
|
||||
return __WASI_ESUCCESS;
|
||||
#else
|
||||
return __WASI_ENOTSUP;
|
||||
#endif
|
||||
case __WASI_CLOCK_THREAD_CPUTIME_ID:
|
||||
#if defined(CLOCK_THREAD_CPUTIME_ID)
|
||||
*out = CLOCK_THREAD_CPUTIME_ID;
|
||||
return __WASI_ESUCCESS;
|
||||
#else
|
||||
return __WASI_ENOTSUP;
|
||||
#endif
|
||||
default:
|
||||
return __WASI_EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static __wasi_timestamp_t
|
||||
timespec_to_nanoseconds(const struct timespec *ts)
|
||||
{
|
||||
if (ts->tv_sec < 0)
|
||||
return 0;
|
||||
if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / NANOSECONDS_PER_SECOND)
|
||||
return UINT64_MAX;
|
||||
return (__wasi_timestamp_t)ts->tv_sec * NANOSECONDS_PER_SECOND
|
||||
+ (__wasi_timestamp_t)ts->tv_nsec;
|
||||
}
|
||||
|
||||
__wasi_errno_t
|
||||
os_clock_res_get(__wasi_clockid_t clock_id, __wasi_timestamp_t *resolution)
|
||||
{
|
||||
clockid_t nclock_id;
|
||||
__wasi_errno_t error = wasi_clockid_to_clockid(clock_id, &nclock_id);
|
||||
|
||||
if (error != __WASI_ESUCCESS)
|
||||
return error;
|
||||
|
||||
struct timespec ts;
|
||||
if (clock_getres(nclock_id, &ts) < 0)
|
||||
return convert_errno(errno);
|
||||
|
||||
*resolution = timespec_to_nanoseconds(&ts);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
__wasi_errno_t
|
||||
os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
|
||||
__wasi_timestamp_t *time)
|
||||
{
|
||||
clockid_t nclock_id;
|
||||
__wasi_errno_t error = wasi_clockid_to_clockid(clock_id, &nclock_id);
|
||||
|
||||
(void)precision;
|
||||
|
||||
if (error != __WASI_ESUCCESS)
|
||||
return error;
|
||||
|
||||
struct timespec ts;
|
||||
if (clock_gettime(nclock_id, &ts) < 0)
|
||||
return convert_errno(errno);
|
||||
|
||||
*time = timespec_to_nanoseconds(&ts);
|
||||
|
||||
return error;
|
||||
}
|
1014
core/shared/platform/esp-idf/espidf_file.c
Normal file
1014
core/shared/platform/esp-idf/espidf_file.c
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -9,11 +9,10 @@
|
|||
#include "soc/mmu.h"
|
||||
#include "rom/cache.h"
|
||||
|
||||
#define MEM_DUAL_BUS_OFFSET (IRAM0_CACHE_ADDRESS_LOW - DRAM0_CACHE_ADDRESS_LOW)
|
||||
#define MEM_DUAL_BUS_OFFSET (SOC_IROM_LOW - SOC_IROM_HIGH)
|
||||
|
||||
#define in_ibus_ext(addr) \
|
||||
(((uint32)addr >= IRAM0_CACHE_ADDRESS_LOW) \
|
||||
&& ((uint32)addr < IRAM0_CACHE_ADDRESS_HIGH))
|
||||
#define in_ibus_ext(addr) \
|
||||
(((uint32)addr >= SOC_IROM_LOW) && ((uint32)addr < SOC_IROM_HIGH))
|
||||
|
||||
static portMUX_TYPE s_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
||||
#endif
|
||||
|
@ -55,7 +54,24 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
|
|||
#else
|
||||
uint32_t mem_caps = MALLOC_CAP_8BIT;
|
||||
#endif
|
||||
return heap_caps_malloc(size, mem_caps);
|
||||
void *buf_origin =
|
||||
heap_caps_malloc(size + 4 + sizeof(uintptr_t), mem_caps);
|
||||
if (!buf_origin) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Memory allocation with MALLOC_CAP_SPIRAM or MALLOC_CAP_8BIT will
|
||||
// return 4-byte aligned Reserve extra 4 byte to fixup alignment and
|
||||
// size for the pointer to the originally allocated address
|
||||
void *buf_fixed = buf_origin + sizeof(void *);
|
||||
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
|
||||
buf_fixed = (void *)((uintptr_t)(buf_fixed + 4) & (~(uintptr_t)7));
|
||||
}
|
||||
|
||||
uintptr_t *addr_field = buf_fixed - sizeof(uintptr_t);
|
||||
*addr_field = (uintptr_t)buf_origin;
|
||||
|
||||
return buf_fixed;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,25 @@
|
|||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "platform_api_vmcore.h"
|
||||
#include "platform_api_extension.h"
|
||||
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)) \
|
||||
&& (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0))
|
||||
#define UTIMENSAT_TIMESPEC_POINTER 1
|
||||
#define FUTIMENS_TIMESPEC_POINTER 1
|
||||
#endif
|
||||
|
||||
#if CONFIG_LITTLEFS_OPEN_DIR && CONFIG_LITTLEFS_FCNTL_GET_PATH
|
||||
#define OPENAT_SUPPORT 1
|
||||
|
||||
#undef F_GETPATH
|
||||
#define F_GETPATH CONFIG_LITTLEFS_FCNTL_F_GETPATH_VALUE
|
||||
|
||||
#define DIR_PATH_LEN (CONFIG_LITTLEFS_OBJ_NAME_LEN + 1)
|
||||
#endif
|
||||
|
||||
int
|
||||
bh_platform_init()
|
||||
{
|
||||
|
@ -177,12 +193,40 @@ writev(int fildes, const struct iovec *iov, int iovcnt)
|
|||
return ntotal;
|
||||
}
|
||||
|
||||
#if OPENAT_SUPPORT
|
||||
int
|
||||
openat(int fd, const char *pathname, int flags, ...)
|
||||
{
|
||||
int new_fd;
|
||||
int ret;
|
||||
char dir_path[DIR_PATH_LEN];
|
||||
char *full_path;
|
||||
|
||||
ret = fcntl(fd, F_GETPATH, dir_path);
|
||||
if (ret != 0) {
|
||||
errno = -EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = asprintf(&full_path, "%s/%s", dir_path, pathname);
|
||||
if (ret < 0) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
new_fd = open(full_path, flags);
|
||||
free(full_path);
|
||||
|
||||
return new_fd;
|
||||
}
|
||||
#else
|
||||
int
|
||||
openat(int fd, const char *path, int oflags, ...)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
fstatat(int fd, const char *path, struct stat *buf, int flag)
|
||||
|
@ -234,7 +278,13 @@ unlinkat(int fd, const char *path, int flag)
|
|||
}
|
||||
|
||||
int
|
||||
utimensat(int fd, const char *path, const struct timespec ts[2], int flag)
|
||||
utimensat(int fd, const char *path,
|
||||
#if UTIMENSAT_TIMESPEC_POINTER
|
||||
const struct timespec *ts,
|
||||
#else
|
||||
const struct timespec ts[2],
|
||||
#endif
|
||||
int flag)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
|
@ -257,7 +307,13 @@ ftruncate(int fd, off_t length)
|
|||
#endif
|
||||
|
||||
int
|
||||
futimens(int fd, const struct timespec times[2])
|
||||
futimens(int fd,
|
||||
#if FUTIMENS_TIMESPEC_POINTER
|
||||
const struct timespec *times
|
||||
#else
|
||||
const struct timespec times[2]
|
||||
#endif
|
||||
)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
|
|
|
@ -8,19 +8,44 @@
|
|||
#include "libc_errno.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
static void
|
||||
textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr_in *out)
|
||||
static bool
|
||||
textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr *out,
|
||||
socklen_t *out_len)
|
||||
{
|
||||
struct sockaddr_in *v4;
|
||||
#ifdef IPPROTO_IPV6
|
||||
struct sockaddr_in6 *v6;
|
||||
#endif
|
||||
|
||||
assert(textual);
|
||||
|
||||
out->sin_family = AF_INET;
|
||||
out->sin_port = htons(port);
|
||||
out->sin_addr.s_addr = inet_addr(textual);
|
||||
v4 = (struct sockaddr_in *)out;
|
||||
if (inet_pton(AF_INET, textual, &v4->sin_addr.s_addr) == 1) {
|
||||
v4->sin_family = AF_INET;
|
||||
v4->sin_port = htons(port);
|
||||
*out_len = sizeof(struct sockaddr_in);
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef IPPROTO_IPV6
|
||||
v6 = (struct sockaddr_in6 *)out;
|
||||
if (inet_pton(AF_INET6, textual, &v6->sin6_addr.s6_addr) == 1) {
|
||||
v6->sin6_family = AF_INET6;
|
||||
v6->sin6_port = htons(port);
|
||||
*out_len = sizeof(struct sockaddr_in6);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static int
|
||||
sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr, socklen_t socklen,
|
||||
sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr,
|
||||
bh_sockaddr_t *bh_sockaddr)
|
||||
{
|
||||
switch (sockaddr->sa_family) {
|
||||
|
@ -28,31 +53,82 @@ sockaddr_to_bh_sockaddr(const struct sockaddr *sockaddr, socklen_t socklen,
|
|||
{
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
|
||||
|
||||
assert(socklen >= sizeof(struct sockaddr_in));
|
||||
|
||||
bh_sockaddr->port = ntohs(addr->sin_port);
|
||||
bh_sockaddr->addr_buffer.ipv4 = ntohl(addr->sin_addr.s_addr);
|
||||
bh_sockaddr->is_ipv4 = true;
|
||||
return BHT_OK;
|
||||
}
|
||||
#ifdef IPPROTO_IPV6
|
||||
case AF_INET6:
|
||||
{
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)sockaddr;
|
||||
size_t i;
|
||||
|
||||
bh_sockaddr->port = ntohs(addr->sin6_port);
|
||||
|
||||
for (i = 0; i < sizeof(bh_sockaddr->addr_buffer.ipv6)
|
||||
/ sizeof(bh_sockaddr->addr_buffer.ipv6[0]);
|
||||
i++) {
|
||||
uint16 part_addr = addr->sin6_addr.s6_addr[i * 2]
|
||||
| (addr->sin6_addr.s6_addr[i * 2 + 1] << 8);
|
||||
bh_sockaddr->addr_buffer.ipv6[i] = ntohs(part_addr);
|
||||
}
|
||||
|
||||
bh_sockaddr->is_ipv4 = false;
|
||||
return BHT_OK;
|
||||
}
|
||||
#endif
|
||||
default:
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bh_sockaddr_to_sockaddr(const bh_sockaddr_t *bh_sockaddr,
|
||||
struct sockaddr_storage *sockaddr, socklen_t *socklen)
|
||||
{
|
||||
if (bh_sockaddr->is_ipv4) {
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
|
||||
addr->sin_port = htons(bh_sockaddr->port);
|
||||
addr->sin_family = AF_INET;
|
||||
addr->sin_addr.s_addr = htonl(bh_sockaddr->addr_buffer.ipv4);
|
||||
*socklen = sizeof(*addr);
|
||||
}
|
||||
#ifdef IPPROTO_IPV6
|
||||
else {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)sockaddr;
|
||||
size_t i;
|
||||
addr->sin6_port = htons(bh_sockaddr->port);
|
||||
addr->sin6_family = AF_INET6;
|
||||
|
||||
for (i = 0; i < sizeof(bh_sockaddr->addr_buffer.ipv6)
|
||||
/ sizeof(bh_sockaddr->addr_buffer.ipv6[0]);
|
||||
i++) {
|
||||
uint16 part_addr = htons(bh_sockaddr->addr_buffer.ipv6[i]);
|
||||
addr->sin6_addr.s6_addr[i * 2] = 0xff & part_addr;
|
||||
addr->sin6_addr.s6_addr[i * 2 + 1] = (0xff00 & part_addr) >> 8;
|
||||
}
|
||||
|
||||
*socklen = sizeof(*addr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_create(bh_socket_t *sock, bool is_ipv4, bool is_tcp)
|
||||
{
|
||||
int af = is_ipv4 ? AF_INET : AF_INET6;
|
||||
|
||||
if (!sock) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
if (is_tcp) {
|
||||
*sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
*sock = socket(af, SOCK_STREAM, IPPROTO_TCP);
|
||||
}
|
||||
else {
|
||||
*sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
*sock = socket(af, SOCK_DGRAM, 0);
|
||||
}
|
||||
|
||||
return (*sock == -1) ? BHT_ERROR : BHT_OK;
|
||||
|
@ -61,28 +137,47 @@ os_socket_create(bh_socket_t *sock, bool is_ipv4, bool is_tcp)
|
|||
int
|
||||
os_socket_bind(bh_socket_t socket, const char *host, int *port)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
struct sockaddr_storage addr = { 0 };
|
||||
struct linger ling;
|
||||
socklen_t socklen;
|
||||
int ret;
|
||||
|
||||
assert(host);
|
||||
assert(port);
|
||||
|
||||
addr.sin_addr.s_addr = inet_addr(host);
|
||||
addr.sin_port = htons(*port);
|
||||
addr.sin_family = AF_INET;
|
||||
ling.l_onoff = 1;
|
||||
ling.l_linger = 0;
|
||||
|
||||
ret = bind(socket, (struct sockaddr *)&addr, sizeof(addr));
|
||||
if (!textual_addr_to_sockaddr(host, *port, (struct sockaddr *)&addr,
|
||||
&socklen)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = setsockopt(socket, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = bind(socket, (struct sockaddr *)&addr, socklen);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
socklen = sizeof(addr);
|
||||
if (getsockname(socket, (struct sockaddr *)&addr, &socklen) == -1) {
|
||||
if (getsockname(socket, (void *)&addr, &socklen) == -1) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
*port = ntohs(addr.sin_port);
|
||||
if (addr.ss_family == AF_INET) {
|
||||
*port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
|
||||
}
|
||||
else {
|
||||
#ifdef IPPROTO_IPV6
|
||||
*port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
|
||||
#else
|
||||
goto fail;
|
||||
#endif
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
|
||||
|
@ -120,10 +215,7 @@ int
|
|||
os_socket_accept(bh_socket_t server_sock, bh_socket_t *sock, void *addr,
|
||||
unsigned int *addrlen)
|
||||
{
|
||||
struct sockaddr addr_tmp;
|
||||
socklen_t len = sizeof(struct sockaddr);
|
||||
|
||||
*sock = accept(server_sock, (struct sockaddr *)&addr_tmp, &len);
|
||||
*sock = accept(server_sock, addr, (socklen_t *)addrlen);
|
||||
|
||||
if (*sock < 0) {
|
||||
return BHT_ERROR;
|
||||
|
@ -135,11 +227,14 @@ os_socket_accept(bh_socket_t server_sock, bh_socket_t *sock, void *addr,
|
|||
int
|
||||
os_socket_connect(bh_socket_t socket, const char *addr, int port)
|
||||
{
|
||||
struct sockaddr_in addr_in = { 0 };
|
||||
socklen_t addr_len = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_storage addr_in = { 0 };
|
||||
socklen_t addr_len;
|
||||
int ret = 0;
|
||||
|
||||
textual_addr_to_sockaddr(addr, port, &addr_in);
|
||||
if (!textual_addr_to_sockaddr(addr, port, (struct sockaddr *)&addr_in,
|
||||
&addr_len)) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
ret = connect(socket, (struct sockaddr *)&addr_in, addr_len);
|
||||
if (ret == -1) {
|
||||
|
@ -155,12 +250,53 @@ os_socket_recv(bh_socket_t socket, void *buf, unsigned int len)
|
|||
return recv(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags,
|
||||
bh_sockaddr_t *src_addr)
|
||||
{
|
||||
struct sockaddr_storage sock_addr = { 0 };
|
||||
socklen_t socklen = sizeof(sock_addr);
|
||||
int ret;
|
||||
|
||||
ret = recvfrom(socket, buf, len, flags, (struct sockaddr *)&sock_addr,
|
||||
&socklen);
|
||||
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (src_addr && socklen > 0) {
|
||||
if (sockaddr_to_bh_sockaddr((struct sockaddr *)&sock_addr, src_addr)
|
||||
== BHT_ERROR) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (src_addr) {
|
||||
memset(src_addr, 0, sizeof(*src_addr));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
|
||||
{
|
||||
return send(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len,
|
||||
int flags, const bh_sockaddr_t *dest_addr)
|
||||
{
|
||||
struct sockaddr_storage sock_addr = { 0 };
|
||||
socklen_t socklen = 0;
|
||||
|
||||
bh_sockaddr_to_sockaddr(dest_addr, &sock_addr, &socklen);
|
||||
|
||||
return sendto(socket, buf, len, flags, (const struct sockaddr *)&sock_addr,
|
||||
socklen);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_close(bh_socket_t socket)
|
||||
{
|
||||
|
@ -191,41 +327,701 @@ os_socket_inet_network(bool is_ipv4, const char *cp, bh_ip_addr_buffer_t *out)
|
|||
out->ipv4 = ntohl(out->ipv4);
|
||||
}
|
||||
else {
|
||||
#ifdef IPPROTO_IPV6
|
||||
if (inet_pton(AF_INET6, cp, out->ipv6) != 1) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
for (int i = 0; i < 8; i++) {
|
||||
out->ipv6[i] = ntohs(out->ipv6[i]);
|
||||
}
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
getaddrinfo_error_to_errno(int error)
|
||||
{
|
||||
switch (error) {
|
||||
case EAI_AGAIN:
|
||||
return EAGAIN;
|
||||
case EAI_FAIL:
|
||||
return EFAULT;
|
||||
case EAI_MEMORY:
|
||||
return ENOMEM;
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
is_addrinfo_supported(struct addrinfo *info)
|
||||
{
|
||||
return
|
||||
// Allow only IPv4 and IPv6
|
||||
(info->ai_family == AF_INET || info->ai_family == AF_INET6)
|
||||
// Allow only UDP and TCP
|
||||
&& (info->ai_socktype == SOCK_DGRAM || info->ai_socktype == SOCK_STREAM)
|
||||
&& (info->ai_protocol == IPPROTO_TCP
|
||||
|| info->ai_protocol == IPPROTO_UDP);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_resolve(const char *host, const char *service,
|
||||
uint8_t *hint_is_tcp, uint8_t *hint_is_ipv4,
|
||||
bh_addr_info_t *addr_info, size_t addr_info_size,
|
||||
size_t *max_info_size)
|
||||
{
|
||||
struct addrinfo hints = { 0 }, *res, *result;
|
||||
int hints_enabled = hint_is_tcp || hint_is_ipv4;
|
||||
int ret;
|
||||
size_t pos = 0;
|
||||
|
||||
if (hints_enabled) {
|
||||
if (hint_is_ipv4) {
|
||||
hints.ai_family = *hint_is_ipv4 ? AF_INET : AF_INET6;
|
||||
}
|
||||
if (hint_is_tcp) {
|
||||
hints.ai_socktype = *hint_is_tcp ? SOCK_STREAM : SOCK_DGRAM;
|
||||
}
|
||||
}
|
||||
|
||||
ret = getaddrinfo(host, strlen(service) == 0 ? NULL : service,
|
||||
hints_enabled ? &hints : NULL, &result);
|
||||
if (ret != BHT_OK) {
|
||||
errno = getaddrinfo_error_to_errno(ret);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
res = result;
|
||||
while (res) {
|
||||
if (addr_info_size > pos) {
|
||||
if (!is_addrinfo_supported(res)) {
|
||||
res = res->ai_next;
|
||||
continue;
|
||||
}
|
||||
|
||||
ret =
|
||||
sockaddr_to_bh_sockaddr(res->ai_addr, &addr_info[pos].sockaddr);
|
||||
|
||||
if (ret == BHT_ERROR) {
|
||||
freeaddrinfo(result);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
addr_info[pos].is_tcp = res->ai_socktype == SOCK_STREAM;
|
||||
}
|
||||
|
||||
pos++;
|
||||
res = res->ai_next;
|
||||
}
|
||||
|
||||
*max_info_size = pos;
|
||||
freeaddrinfo(result);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
os_socket_setbooloption(bh_socket_t socket, int level, int optname,
|
||||
bool is_enabled)
|
||||
{
|
||||
int option = (int)is_enabled;
|
||||
if (setsockopt(socket, level, optname, &option, sizeof(option)) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
os_socket_getbooloption(bh_socket_t socket, int level, int optname,
|
||||
bool *is_enabled)
|
||||
{
|
||||
assert(is_enabled);
|
||||
|
||||
int optval;
|
||||
socklen_t optval_size = sizeof(optval);
|
||||
if (getsockopt(socket, level, optname, &optval, &optval_size) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*is_enabled = (bool)optval;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_send_buf_size(bh_socket_t socket, size_t bufsiz)
|
||||
{
|
||||
int buf_size_int = (int)bufsiz;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_SNDBUF, &buf_size_int,
|
||||
sizeof(buf_size_int))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_send_buf_size(bh_socket_t socket, size_t *bufsiz)
|
||||
{
|
||||
assert(bufsiz);
|
||||
|
||||
int buf_size_int;
|
||||
socklen_t bufsiz_len = sizeof(buf_size_int);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_SNDBUF, &buf_size_int, &bufsiz_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*bufsiz = (size_t)buf_size_int;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_recv_buf_size(bh_socket_t socket, size_t bufsiz)
|
||||
{
|
||||
int buf_size_int = (int)bufsiz;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &buf_size_int,
|
||||
sizeof(buf_size_int))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_recv_buf_size(bh_socket_t socket, size_t *bufsiz)
|
||||
{
|
||||
assert(bufsiz);
|
||||
|
||||
int buf_size_int;
|
||||
socklen_t bufsiz_len = sizeof(buf_size_int);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_RCVBUF, &buf_size_int, &bufsiz_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*bufsiz = (size_t)buf_size_int;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_keep_alive(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
return os_socket_setbooloption(socket, SOL_SOCKET, SO_KEEPALIVE,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_keep_alive(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
return os_socket_getbooloption(socket, SOL_SOCKET, SO_KEEPALIVE,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_reuse_addr(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
return os_socket_setbooloption(socket, SOL_SOCKET, SO_REUSEADDR,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_reuse_addr(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
return os_socket_getbooloption(socket, SOL_SOCKET, SO_REUSEADDR,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_reuse_port(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
#if defined(SO_REUSEPORT) /* NuttX doesn't have SO_REUSEPORT */
|
||||
return os_socket_setbooloption(socket, SOL_SOCKET, SO_REUSEPORT,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = ENOTSUP;
|
||||
return BHT_ERROR;
|
||||
#endif /* defined(SO_REUSEPORT) */
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_reuse_port(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
#if defined(SO_REUSEPORT) /* NuttX doesn't have SO_REUSEPORT */
|
||||
return os_socket_getbooloption(socket, SOL_SOCKET, SO_REUSEPORT,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = ENOTSUP;
|
||||
return BHT_ERROR;
|
||||
#endif /* defined(SO_REUSEPORT) */
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_linger(bh_socket_t socket, bool is_enabled, int linger_s)
|
||||
{
|
||||
struct linger linger_opts = { .l_onoff = (int)is_enabled,
|
||||
.l_linger = linger_s };
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger_opts,
|
||||
sizeof(linger_opts))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_linger(bh_socket_t socket, bool *is_enabled, int *linger_s)
|
||||
{
|
||||
assert(is_enabled);
|
||||
assert(linger_s);
|
||||
|
||||
struct linger linger_opts;
|
||||
socklen_t linger_opts_len = sizeof(linger_opts);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_LINGER, &linger_opts,
|
||||
&linger_opts_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*linger_s = linger_opts.l_linger;
|
||||
*is_enabled = (bool)linger_opts.l_onoff;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_tcp_no_delay(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
return os_socket_setbooloption(socket, IPPROTO_TCP, TCP_NODELAY,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_tcp_no_delay(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
return os_socket_getbooloption(socket, IPPROTO_TCP, TCP_NODELAY,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_tcp_quick_ack(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
#ifdef TCP_QUICKACK
|
||||
return os_socket_setbooloption(socket, IPPROTO_TCP, TCP_QUICKACK,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_tcp_quick_ack(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
#ifdef TCP_QUICKACK
|
||||
return os_socket_getbooloption(socket, IPPROTO_TCP, TCP_QUICKACK,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_tcp_keep_idle(bh_socket_t socket, uint32 time_s)
|
||||
{
|
||||
int time_s_int = (int)time_s;
|
||||
#ifdef TCP_KEEPIDLE
|
||||
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &time_s_int,
|
||||
sizeof(time_s_int))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
return BHT_OK;
|
||||
#elif defined(TCP_KEEPALIVE)
|
||||
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPALIVE, &time_s_int,
|
||||
sizeof(time_s_int))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
return BHT_OK;
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_tcp_keep_idle(bh_socket_t socket, uint32 *time_s)
|
||||
{
|
||||
assert(time_s);
|
||||
int time_s_int;
|
||||
socklen_t time_s_len = sizeof(time_s_int);
|
||||
#ifdef TCP_KEEPIDLE
|
||||
if (getsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &time_s_int, &time_s_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*time_s = (uint32)time_s_int;
|
||||
return BHT_OK;
|
||||
#elif defined(TCP_KEEPALIVE)
|
||||
if (getsockopt(socket, IPPROTO_TCP, TCP_KEEPALIVE, &time_s_int, &time_s_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*time_s = (uint32)time_s_int;
|
||||
return BHT_OK;
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_tcp_keep_intvl(bh_socket_t socket, uint32 time_s)
|
||||
{
|
||||
int time_s_int = (int)time_s;
|
||||
#ifdef TCP_KEEPINTVL
|
||||
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPINTVL, &time_s_int,
|
||||
sizeof(time_s_int))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_tcp_keep_intvl(bh_socket_t socket, uint32 *time_s)
|
||||
{
|
||||
#ifdef TCP_KEEPINTVL
|
||||
assert(time_s);
|
||||
int time_s_int;
|
||||
socklen_t time_s_len = sizeof(time_s_int);
|
||||
if (getsockopt(socket, IPPROTO_TCP, TCP_KEEPINTVL, &time_s_int, &time_s_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*time_s = (uint32)time_s_int;
|
||||
return BHT_OK;
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_tcp_fastopen_connect(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
#ifdef TCP_FASTOPEN_CONNECT
|
||||
return os_socket_setbooloption(socket, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_tcp_fastopen_connect(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
#ifdef TCP_FASTOPEN_CONNECT
|
||||
return os_socket_getbooloption(socket, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_ip_multicast_loop(bh_socket_t socket, bool ipv6, bool is_enabled)
|
||||
{
|
||||
if (ipv6) {
|
||||
#ifdef IPPROTO_IPV6
|
||||
return os_socket_setbooloption(socket, IPPROTO_IPV6,
|
||||
IPV6_MULTICAST_LOOP, is_enabled);
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
return os_socket_setbooloption(socket, IPPROTO_IP, IP_MULTICAST_LOOP,
|
||||
is_enabled);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_ip_multicast_loop(bh_socket_t socket, bool ipv6, bool *is_enabled)
|
||||
{
|
||||
if (ipv6) {
|
||||
#ifdef IPPROTO_IPV6
|
||||
return os_socket_getbooloption(socket, IPPROTO_IPV6,
|
||||
IPV6_MULTICAST_LOOP, is_enabled);
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
return os_socket_getbooloption(socket, IPPROTO_IP, IP_MULTICAST_LOOP,
|
||||
is_enabled);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_ip_add_membership(bh_socket_t socket,
|
||||
bh_ip_addr_buffer_t *imr_multiaddr,
|
||||
uint32_t imr_interface, bool is_ipv6)
|
||||
{
|
||||
assert(imr_multiaddr);
|
||||
if (is_ipv6) {
|
||||
#if defined(IPPROTO_IPV6) && !defined(BH_PLATFORM_COSMOPOLITAN)
|
||||
struct ipv6_mreq mreq;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
((uint16_t *)mreq.ipv6mr_multiaddr.s6_addr)[i] =
|
||||
imr_multiaddr->ipv6[i];
|
||||
}
|
||||
mreq.ipv6mr_interface = imr_interface;
|
||||
if (setsockopt(socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
|
||||
sizeof(mreq))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
struct ip_mreq mreq;
|
||||
mreq.imr_multiaddr.s_addr = imr_multiaddr->ipv4;
|
||||
mreq.imr_interface.s_addr = imr_interface;
|
||||
if (setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
|
||||
sizeof(mreq))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
os_socket_set_ip_drop_membership(bh_socket_t socket,
|
||||
bh_ip_addr_buffer_t *imr_multiaddr,
|
||||
uint32_t imr_interface, bool is_ipv6)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
assert(imr_multiaddr);
|
||||
if (is_ipv6) {
|
||||
#if defined(IPPROTO_IPV6) && !defined(BH_PLATFORM_COSMOPOLITAN)
|
||||
struct ipv6_mreq mreq;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
((uint16_t *)mreq.ipv6mr_multiaddr.s6_addr)[i] =
|
||||
imr_multiaddr->ipv6[i];
|
||||
}
|
||||
mreq.ipv6mr_interface = imr_interface;
|
||||
if (setsockopt(socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
|
||||
sizeof(mreq))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
struct ip_mreq mreq;
|
||||
mreq.imr_multiaddr.s_addr = imr_multiaddr->ipv4;
|
||||
mreq.imr_interface.s_addr = imr_interface;
|
||||
if (setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
|
||||
sizeof(mreq))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (getpeername(socket, (struct sockaddr *)&addr, &addr_len) == -1) {
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_ip_ttl(bh_socket_t socket, uint8_t ttl_s)
|
||||
{
|
||||
if (setsockopt(socket, IPPROTO_IP, IP_TTL, &ttl_s, sizeof(ttl_s)) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return sockaddr_to_bh_sockaddr((struct sockaddr *)&addr, addr_len,
|
||||
sockaddr);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_ip_ttl(bh_socket_t socket, uint8_t *ttl_s)
|
||||
{
|
||||
socklen_t opt_len = sizeof(*ttl_s);
|
||||
if (getsockopt(socket, IPPROTO_IP, IP_TTL, ttl_s, &opt_len) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_ip_multicast_ttl(bh_socket_t socket, uint8_t ttl_s)
|
||||
{
|
||||
if (setsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, &ttl_s, sizeof(ttl_s))
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_ip_multicast_ttl(bh_socket_t socket, uint8_t *ttl_s)
|
||||
{
|
||||
socklen_t opt_len = sizeof(*ttl_s);
|
||||
if (getsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, ttl_s, &opt_len)
|
||||
!= 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_ipv6_only(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
#ifdef IPPROTO_IPV6
|
||||
return os_socket_setbooloption(socket, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_ipv6_only(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
#ifdef IPPROTO_IPV6
|
||||
return os_socket_getbooloption(socket, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
is_enabled);
|
||||
#else
|
||||
errno = EAFNOSUPPORT;
|
||||
return BHT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_broadcast(bh_socket_t socket, bool is_enabled)
|
||||
{
|
||||
return os_socket_setbooloption(socket, SOL_SOCKET, SO_BROADCAST,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled)
|
||||
{
|
||||
return os_socket_getbooloption(socket, SOL_SOCKET, SO_BROADCAST,
|
||||
is_enabled);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = timeout_us / 1000000UL;
|
||||
tv.tv_usec = timeout_us % 1000000UL;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
socklen_t tv_len = sizeof(tv);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, &tv_len) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = timeout_us / 1000000UL;
|
||||
tv.tv_usec = timeout_us % 1000000UL;
|
||||
if (setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
|
||||
{
|
||||
struct timeval tv;
|
||||
socklen_t tv_len = sizeof(tv);
|
||||
if (getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, &tv_len) != 0) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
*timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_local(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
struct sockaddr_storage addr_storage = { 0 };
|
||||
socklen_t addr_len = sizeof(addr_storage);
|
||||
int ret;
|
||||
|
||||
if (getsockname(socket, (struct sockaddr *)&addr, &addr_len) == -1) {
|
||||
ret = getsockname(socket, (struct sockaddr *)&addr_storage, &addr_len);
|
||||
|
||||
if (ret != BHT_OK) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return sockaddr_to_bh_sockaddr((struct sockaddr *)&addr, addr_len,
|
||||
sockaddr);
|
||||
return sockaddr_to_bh_sockaddr((struct sockaddr *)&addr_storage, sockaddr);
|
||||
}
|
||||
|
||||
int
|
||||
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr)
|
||||
{
|
||||
struct sockaddr_storage addr_storage = { 0 };
|
||||
socklen_t addr_len = sizeof(addr_storage);
|
||||
int ret;
|
||||
|
||||
ret = getpeername(socket, (struct sockaddr *)&addr_storage, &addr_len);
|
||||
|
||||
if (ret != BHT_OK) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
return sockaddr_to_bh_sockaddr((struct sockaddr *)&addr_storage, sockaddr);
|
||||
}
|
||||
|
|
|
@ -230,4 +230,59 @@ int
|
|||
os_cond_broadcast(korp_cond *cond)
|
||||
{
|
||||
return pthread_cond_broadcast(cond);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
os_rwlock_init(korp_rwlock *lock)
|
||||
{
|
||||
assert(lock);
|
||||
|
||||
if (pthread_rwlock_init(lock, NULL) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_rwlock_rdlock(korp_rwlock *lock)
|
||||
{
|
||||
assert(lock);
|
||||
|
||||
if (pthread_rwlock_rdlock(lock) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_rwlock_wrlock(korp_rwlock *lock)
|
||||
{
|
||||
assert(lock);
|
||||
|
||||
if (pthread_rwlock_wrlock(lock) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_rwlock_unlock(korp_rwlock *lock)
|
||||
{
|
||||
assert(lock);
|
||||
|
||||
if (pthread_rwlock_unlock(lock) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int
|
||||
os_rwlock_destroy(korp_rwlock *lock)
|
||||
{
|
||||
assert(lock);
|
||||
|
||||
if (pthread_rwlock_destroy(lock) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
|
|
@ -56,39 +56,29 @@ typedef unsigned int korp_sem;
|
|||
#define UTIME_OMIT ((1l << 30) - 2l)
|
||||
#endif
|
||||
|
||||
#ifdef DT_UNKNOWN
|
||||
#undef DT_UNKNOWN
|
||||
#endif
|
||||
|
||||
#ifdef DT_REG
|
||||
#undef DT_REG
|
||||
#endif
|
||||
|
||||
#ifdef DT_DIR
|
||||
#undef DT_DIR
|
||||
#endif
|
||||
|
||||
/* Below parts of d_type define are ported from Nuttx, under Apache License v2.0
|
||||
*/
|
||||
|
||||
/* File type code for the d_type field in dirent structure.
|
||||
* Note that because of the simplified filesystem organization of the NuttX,
|
||||
* top-level, pseudo-file system, an inode can be BOTH a file and a directory
|
||||
/* Following macros are defined in espressif GCC of esp-idf v5.3
|
||||
*/
|
||||
|
||||
#define DTYPE_UNKNOWN 0
|
||||
#define DTYPE_FIFO 1
|
||||
#define DTYPE_CHR 2
|
||||
#define DTYPE_SEM 3
|
||||
#define DTYPE_DIRECTORY 4
|
||||
#define DTYPE_MQ 5
|
||||
#define DTYPE_BLK 6
|
||||
#define DTYPE_SHM 7
|
||||
#define DTYPE_FILE 8
|
||||
#define DTYPE_MTD 9
|
||||
#define DTYPE_FILE 1
|
||||
#define DTYPE_DIRECTORY 2
|
||||
#define DTYPE_CHR 4
|
||||
#define DTYPE_BLK 5
|
||||
#define DTYPE_FIFO 8
|
||||
#define DTYPE_LINK 10
|
||||
#define DTYPE_SOCK 12
|
||||
|
||||
/* Following macros are not defined in espressif GCC of esp-idf v5.3
|
||||
*/
|
||||
|
||||
#define DTYPE_SEM 100
|
||||
#define DTYPE_MQ 101
|
||||
#define DTYPE_SHM 102
|
||||
#define DTYPE_MTD 103
|
||||
|
||||
/* The d_type field of the dirent structure is not specified by POSIX. It
|
||||
* is a non-standard, 4.5BSD extension that is implemented by most OSs. A
|
||||
* POSIX compliant OS may not implement the d_type field at all. Many OS's
|
||||
|
@ -96,18 +86,53 @@ typedef unsigned int korp_sem;
|
|||
* type names:
|
||||
*/
|
||||
|
||||
#ifndef DT_UNKNOWN
|
||||
#define DT_UNKNOWN DTYPE_UNKNOWN
|
||||
#endif
|
||||
|
||||
#ifndef DT_FIFO
|
||||
#define DT_FIFO DTYPE_FIFO
|
||||
#endif
|
||||
|
||||
#ifndef DT_CHR
|
||||
#define DT_CHR DTYPE_CHR
|
||||
#endif
|
||||
|
||||
#ifndef DT_SEM
|
||||
#define DT_SEM DTYPE_SEM
|
||||
#endif
|
||||
|
||||
#ifndef DT_DIR
|
||||
#define DT_DIR DTYPE_DIRECTORY
|
||||
#endif
|
||||
|
||||
#ifndef DT_MQ
|
||||
#define DT_MQ DTYPE_MQ
|
||||
#endif
|
||||
|
||||
#ifndef DT_BLK
|
||||
#define DT_BLK DTYPE_BLK
|
||||
#endif
|
||||
|
||||
#ifndef DT_SHM
|
||||
#define DT_SHM DTYPE_SHM
|
||||
#endif
|
||||
|
||||
#ifndef DT_REG
|
||||
#define DT_REG DTYPE_FILE
|
||||
#endif
|
||||
|
||||
#ifndef DT_MTD
|
||||
#define DT_MTD DTYPE_MTD
|
||||
#endif
|
||||
|
||||
#ifndef DT_LNK
|
||||
#define DT_LNK DTYPE_LINK
|
||||
#endif
|
||||
|
||||
#ifndef DT_SOCK
|
||||
#define DT_SOCK DTYPE_SOCK
|
||||
#endif
|
||||
|
||||
static inline int
|
||||
os_getpagesize()
|
||||
|
|
|
@ -350,7 +350,7 @@ ipfs_fopen(int fd, int flags)
|
|||
errno = __WASI_ECANCELED;
|
||||
sgx_fclose(sgx_file);
|
||||
os_printf("An error occurred while inserting the IPFS file pointer in "
|
||||
"the map.");
|
||||
"the map.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -529,4 +529,4 @@ ipfs_ftruncate(int fd, off_t len)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#endif /* end of WASM_ENABLE_SGX_IPFS */
|
||||
#endif /* end of WASM_ENABLE_SGX_IPFS */
|
||||
|
|
|
@ -154,8 +154,8 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
|
|||
|
||||
ret = sgx_alloc_rsrv_mem(aligned_size);
|
||||
if (ret == NULL) {
|
||||
os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.", size,
|
||||
aligned_size, prot);
|
||||
os_printf("os_mmap(size=%u, aligned size=%lu, prot=0x%x) failed.\n",
|
||||
size, aligned_size, prot);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
|
|||
|
||||
st = sgx_tprotect_rsrv_mem(ret, aligned_size, mprot);
|
||||
if (st != SGX_SUCCESS) {
|
||||
os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.", size,
|
||||
os_printf("os_mmap(size=%u, prot=0x%x) failed to set protect.\n", size,
|
||||
prot);
|
||||
sgx_free_rsrv_mem(ret, aligned_size);
|
||||
return NULL;
|
||||
|
@ -205,7 +205,8 @@ os_mprotect(void *addr, size_t size, int prot)
|
|||
mprot |= SGX_PROT_EXEC;
|
||||
st = sgx_tprotect_rsrv_mem(addr, aligned_size, mprot);
|
||||
if (st != SGX_SUCCESS)
|
||||
os_printf("os_mprotect(addr=0x%" PRIx64 ", size=%u, prot=0x%x) failed.",
|
||||
os_printf("os_mprotect(addr=0x%" PRIx64
|
||||
", size=%u, prot=0x%x) failed.\n",
|
||||
(uintptr_t)addr, size, prot);
|
||||
|
||||
return (st == SGX_SUCCESS ? 0 : -1);
|
||||
|
|
|
@ -577,4 +577,34 @@ os_thread_get_stack_boundary()
|
|||
|
||||
void
|
||||
os_thread_jit_write_protect_np(bool enabled)
|
||||
{}
|
||||
{}
|
||||
|
||||
int
|
||||
os_thread_detach(korp_tid thread)
|
||||
{
|
||||
(void)thread;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void
|
||||
os_thread_exit(void *retval)
|
||||
{
|
||||
(void)retval;
|
||||
os_thread_cleanup();
|
||||
k_thread_abort(k_current_get());
|
||||
}
|
||||
|
||||
int
|
||||
os_cond_broadcast(korp_cond *cond)
|
||||
{
|
||||
os_thread_wait_node *node;
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
node = cond->thread_wait_list;
|
||||
while (node) {
|
||||
os_thread_wait_node *next = node->next;
|
||||
k_sem_give(&node->sem);
|
||||
node = next;
|
||||
}
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#ifndef _WAMR_VERSION_H_
|
||||
#define _WAMR_VERSION_H_
|
||||
#define WAMR_VERSION_MAJOR 1
|
||||
#define WAMR_VERSION_MINOR 3
|
||||
#define WAMR_VERSION_PATCH 2
|
||||
#define WAMR_VERSION_MAJOR 2
|
||||
#define WAMR_VERSION_MINOR 0
|
||||
#define WAMR_VERSION_PATCH 0
|
||||
#endif
|
||||
|
|
|
@ -254,6 +254,10 @@ Currently we only profile the memory consumption of module, module_instance and
|
|||
|
||||
> See [Enable segue optimization for wamrc when generating the aot file](./perf_tune.md#3-enable-segue-optimization-for-wamrc-when-generating-the-aot-file) for more details.
|
||||
|
||||
#### **User defined linear memory allocator**
|
||||
- **WAMR_BUILD_ALLOC_WITH_USAGE**=1/0, default to disable if not set
|
||||
> Notes: by default, the linear memory is allocated by system. when it's set to 1 and Alloc_With_Allocator is selected, it will be allocated by customer.
|
||||
|
||||
#### **Enable running PGO(Profile-Guided Optimization) instrumented AOT file**
|
||||
- **WAMR_BUILD_STATIC_PGO**=1/0, default to disable if not set
|
||||
> Note: See [Use the AOT static PGO method](./perf_tune.md#5-use-the-aot-static-pgo-method) for more details.
|
||||
|
|
|
@ -89,7 +89,7 @@ Each letter in the "()" represents a parameter type, and the one following after
|
|||
- '**I**': i64
|
||||
- '**f**': f32
|
||||
- '**F**': f64
|
||||
- '**r**': externref (has to be the value of a `uintptr_t` variable)
|
||||
- '**r**': externref (has to be the value of a `uintptr_t` variable), or all kinds of GC reference types when GC feature is enabled
|
||||
- '**\***': the parameter is a buffer address in WASM application
|
||||
- '**~**': the parameter is the byte length of WASM buffer as referred by preceding argument "\*". It must follow after '*', otherwise, registration will fail
|
||||
- '**$**': the parameter is a string in WASM application
|
||||
|
|
|
@ -28,7 +28,7 @@ emcc -msimd128 -O3 -o <wasm_file> <c/c++ source files>
|
|||
- Reduce the footprint of JIT/AOT, the JIT/AOT code generated is smaller
|
||||
- Reduce the compilation time of JIT/AOT
|
||||
|
||||
Currently it is supported on linux x86-64, developer can use `--enable-segue=[<flags>]` for wamrc:
|
||||
Currently it is only supported on linux x86-64, developer can use `--enable-segue=[<flags>]` for wamrc:
|
||||
|
||||
```bash
|
||||
wamrc --enable-segue -o aot_file wasm_file
|
||||
|
@ -50,6 +50,8 @@ iwasm --enable-segue wasm_file (iwasm is built with llvm-jit enabled)
|
|||
iwasm --enable-segue=[<flags>] wasm_file
|
||||
```
|
||||
|
||||
> Note: Currently it is only supported on linux x86-64.
|
||||
|
||||
## 5. Use the AOT static PGO method
|
||||
|
||||
LLVM PGO (Profile-Guided Optimization) allows the compiler to better optimize code for how it actually runs. WAMR supports AOT static PGO, currently it is tested on Linux x86-64 and x86-32. The basic steps are:
|
||||
|
|
15
idf_component.yml
Normal file
15
idf_component.yml
Normal file
|
@ -0,0 +1,15 @@
|
|||
version: "2.0.0"
|
||||
description: WebAssembly Micro Runtime - A lightweight standalone WebAssembly (Wasm) runtime with small footprint, high performance and highly configurable features
|
||||
url: https://bytecodealliance.org/
|
||||
repository: https://github.com/bytecodealliance/wasm-micro-runtime.git
|
||||
documentation: https://wamr.gitbook.io/
|
||||
issues: https://github.com/bytecodealliance/wasm-micro-runtime/issues
|
||||
dependencies:
|
||||
idf: ">=4.4"
|
||||
targets:
|
||||
- esp32
|
||||
- esp32s3
|
||||
- esp32c3
|
||||
- esp32c6
|
||||
examples:
|
||||
- path: product-mini/platforms/esp-idf
|
|
@ -6,7 +6,4 @@ cmake_minimum_required(VERSION 3.5)
|
|||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
|
||||
set (COMPONENTS ${IDF_TARGET} main freertos esptool_py wamr)
|
||||
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{WAMR_PATH}/build-scripts/esp-idf")
|
||||
|
||||
project(wamr-simple)
|
|
@ -2,5 +2,4 @@
|
|||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES wamr)
|
||||
INCLUDE_DIRS ".")
|
||||
|
|
7
product-mini/platforms/esp-idf/main/idf_component.yml
Normal file
7
product-mini/platforms/esp-idf/main/idf_component.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
wasm-micro-runtime:
|
||||
version: "^2"
|
||||
override_path: "../../../.."
|
||||
idf:
|
||||
version: ">=4.4"
|
|
@ -12,11 +12,7 @@
|
|||
|
||||
#include "esp_log.h"
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32S3
|
||||
#define IWASM_MAIN_STACK_SIZE 5120
|
||||
#else
|
||||
#define IWASM_MAIN_STACK_SIZE 4096
|
||||
#endif
|
||||
|
||||
#define LOG_TAG "wamr"
|
||||
|
||||
|
|
|
@ -129,6 +129,10 @@ if(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL)
|
|||
set(WAMR_BUILD_GLOBAL_HEAP_SIZE ${_HEAP_SIZE_})
|
||||
endif()
|
||||
|
||||
if (CONFIG_INTERPRETERS_WAMR_MEM_ALLOC_WITH_USAGE)
|
||||
set(WAMR_BUILD_MEM_ALLOC_WITH_USAGE 1)
|
||||
endif()
|
||||
|
||||
if(CONFIG_INTERPRETERS_WAMR_ENABLE_SPEC_TEST)
|
||||
set(WAMR_BUILD_SPEC_TEST 1)
|
||||
endif()
|
||||
|
|
|
@ -373,6 +373,11 @@ CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=1
|
|||
CFLAGS += -DWASM_GLOBAL_HEAP_SIZE="$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE) * 1024"
|
||||
else
|
||||
CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=0
|
||||
ifeq ($(CONFIG_INTERPRETERS_WAMR_MEM_ALLOC_WITH_USAGE),y)
|
||||
CFLAGS += -DWASM_MEM_ALLOC_WITH_USAGE=1
|
||||
else
|
||||
CFLAGS += -DWASM_MEM_ALLOC_WITH_USAGE=0
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_INTERPRETERS_WAMR_ENABLE_SPEC_TEST),y)
|
||||
|
|
|
@ -445,6 +445,9 @@ static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
|
|||
#else
|
||||
static void *
|
||||
malloc_func(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
mem_alloc_usage_t usage,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
void *user_data,
|
||||
#endif
|
||||
|
@ -455,6 +458,9 @@ malloc_func(
|
|||
|
||||
static void *
|
||||
realloc_func(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
mem_alloc_usage_t usage, bool full_size_mmaped,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
void *user_data,
|
||||
#endif
|
||||
|
@ -465,6 +471,9 @@ realloc_func(
|
|||
|
||||
static void
|
||||
free_func(
|
||||
#if WASM_MEM_ALLOC_WITH_USAGE != 0
|
||||
mem_alloc_usage_t usage,
|
||||
#endif
|
||||
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
|
||||
void *user_data,
|
||||
#endif
|
||||
|
|
|
@ -7,6 +7,14 @@ include(CheckPIESupported)
|
|||
|
||||
project(debug_tools_sample)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
||||
find_package(WASISDK REQUIRED)
|
||||
|
||||
option(SOURCE_MAP_DEMO "Enable source map demo" OFF)
|
||||
if (SOURCE_MAP_DEMO)
|
||||
find_package(EMSCRIPTEN 3.1.50 REQUIRED)
|
||||
endif ()
|
||||
|
||||
################ runtime settings ################
|
||||
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
|
||||
if (APPLE)
|
||||
|
@ -61,7 +69,30 @@ include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
|||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
|
||||
|
||||
################ wasm application ################
|
||||
add_subdirectory(wasm-apps)
|
||||
include(ExternalProject)
|
||||
|
||||
# wasm32-wasi
|
||||
ExternalProject_Add(wasm33-wasi
|
||||
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps"
|
||||
CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build
|
||||
-DWASI_SDK_PREFIX=${WASISDK_HOME}
|
||||
-DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN}
|
||||
BUILD_COMMAND ${CMAKE_COMMAND} --build build
|
||||
INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
if (EMSCRIPTEN_FOUND)
|
||||
# wasm32-emscripten
|
||||
ExternalProject_Add(wasm32-emscripten
|
||||
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps"
|
||||
CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build
|
||||
-DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN_TOOLCHAIN}
|
||||
-DCMAKE_VERBOSE_MAKEFILE=On
|
||||
-DSOURCE_MAP_DEMO=On
|
||||
BUILD_COMMAND ${CMAKE_COMMAND} --build build
|
||||
INSTALL_COMMAND ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}/emscripten
|
||||
)
|
||||
endif ()
|
||||
|
||||
################ wamr runtime ################
|
||||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
||||
|
|
|
@ -80,6 +80,39 @@ $ python3 ../../../test-tools/addr2line/addr2line.py \
|
|||
call_stack.txt --no-addr
|
||||
```
|
||||
|
||||
#### sourcemap
|
||||
|
||||
This script also supports _sourcemap_ which is produced by [_emscripten_](https://emscripten.org/docs/tools_reference/emcc.html). The _sourcemap_ is used to map the wasm function to the original source file. To use it, add `-gsource-map` option to _emcc_ command line. The output should be a section named "sourceMappingURL" and a separated file named "_.map_.
|
||||
|
||||
If the wasm file is with _sourcemap_, the script will use it to get the source file and line info. It needs an extra command line option `--emsdk` to specify the path of _emsdk_. The script will use _emsymbolizer_ to query the source file and line info.
|
||||
|
||||
````bash
|
||||
$ python3 ../../../test-tools/addr2line/addr2line.py \
|
||||
--wasi-sdk /opt/wasi-sdk \
|
||||
--wabt /opt/wabt \
|
||||
--wasm-file emscripten/wasm-apps/trap.wasm \
|
||||
--emsdk /opt/emsdk \
|
||||
call_stack.from_wasm_w_sourcemap.txt
|
||||
|
||||
The output should be something like:
|
||||
|
||||
```text
|
||||
1: c
|
||||
at ../../../../../wasm-apps/trap.c:5:1
|
||||
2: b
|
||||
at ../../../../../wasm-apps/trap.c:11:12
|
||||
3: a
|
||||
at ../../../../../wasm-apps/trap.c:17:12
|
||||
4: main
|
||||
at ../../../../../wasm-apps/trap.c:24:5
|
||||
5: __main_void
|
||||
at ../../../../../../../../../emsdk/emscripten/system/lib/standalone/__main_void.c:53:10
|
||||
6: _start
|
||||
at ../../../../../../../../../emsdk/emscripten/system/lib/libc/crt1.c:27:3
|
||||
````
|
||||
|
||||
> The script assume the separated map file _.map_ is in the same directory as the wasm file.
|
||||
|
||||
### Another approach
|
||||
|
||||
If the wasm file is with "name" section, it is able to output function name in the stack trace. To achieve that, need to enable `WAMR_BUILD_LOAD_CUSTOM_SECTION` and `WAMR_BUILD_CUSTOM_NAME_SECTION`. If using .aot file, need to add `--emit-custom-sections=name` into wamrc command line options.
|
||||
|
@ -97,4 +130,4 @@ Then the output should be something like
|
|||
Exception: unreachable
|
||||
```
|
||||
|
||||
Also, it is able to use *addr2line.py* to add file and line info to the stack trace.
|
||||
Also, it is able to use _addr2line.py_ to add file and line info to the stack trace.
|
||||
|
|
45
samples/debug-tools/cmake/FindEMSCRIPTEN.cmake
Normal file
45
samples/debug-tools/cmake/FindEMSCRIPTEN.cmake
Normal file
|
@ -0,0 +1,45 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
find_path(EMSCRIPTEN_HOME
|
||||
NAMES upstream/emscripten
|
||||
PATHS /opt/emsdk
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_PATH
|
||||
NO_CMAKE_SYSTEM_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
REQUIRED
|
||||
)
|
||||
|
||||
find_file(EMSCRIPTEN_VERSION_FILE
|
||||
NAMES emscripten-version.txt
|
||||
PATHS ${EMSCRIPTEN_HOME}/upstream/emscripten
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_PATH
|
||||
NO_CMAKE_SYSTEM_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
REQUIRED
|
||||
)
|
||||
|
||||
file(READ ${EMSCRIPTEN_VERSION_FILE} EMSCRIPTEN_VERSION_FILE_CONTENT)
|
||||
|
||||
string(REGEX
|
||||
MATCH
|
||||
"[0-9]+\.[0-9]+(\.[0-9]+)*"
|
||||
EMSCRIPTEN_VERSION
|
||||
${EMSCRIPTEN_VERSION_FILE_CONTENT}
|
||||
)
|
||||
|
||||
find_package_handle_standard_args(EMSCRIPTEN
|
||||
REQUIRED_VARS EMSCRIPTEN_HOME
|
||||
VERSION_VAR EMSCRIPTEN_VERSION
|
||||
HANDLE_VERSION_RANGE
|
||||
)
|
||||
|
||||
if(EMSCRIPTEN_FOUND)
|
||||
set(EMSCRIPTEN_TOOLCHAIN ${EMSCRIPTEN_HOME}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake)
|
||||
set(EMCC ${EMSCRIPTEN_HOME}/upstream/emscripten/emcc)
|
||||
endif()
|
||||
mark_as_advanced(EMSCRIPTEN_TOOLCHAIN EMCC)
|
27
samples/debug-tools/cmake/FindWAMRC.cmake
Normal file
27
samples/debug-tools/cmake/FindWAMRC.cmake
Normal file
|
@ -0,0 +1,27 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
find_path(WAMRC_HOME
|
||||
wamr-compiler
|
||||
PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../..
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_PATH
|
||||
NO_CMAKE_SYSTEM_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
REQUIRED
|
||||
)
|
||||
|
||||
find_file(WAMRC_BIN
|
||||
wamrc
|
||||
HINTS ${WAMRC_HOME}/wamr-compiler/build
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_PATH
|
||||
NO_CMAKE_SYSTEM_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
REQUIRED
|
||||
)
|
||||
|
||||
find_package_handle_standard_args(WAMRC REQUIRED_VARS WAMRC_BIN)
|
||||
mark_as_advanced(WAMRC_BIN)
|
24
samples/debug-tools/cmake/FindWASISDK.cmake
Normal file
24
samples/debug-tools/cmake/FindWASISDK.cmake
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
file(GLOB WASISDK_SEARCH_PATH "/opt/wasi-sdk-*")
|
||||
find_path(WASISDK_HOME
|
||||
NAMES share/wasi-sysroot
|
||||
PATHS ${WASISDK_SEARCH_PATH}
|
||||
NO_DEFAULT_PATH
|
||||
REQUIRED
|
||||
)
|
||||
|
||||
string(REGEX MATCH [0-9]+\.[0-9]+\.*[0-9]* WASISDK_VERSION ${WASISDK_HOME})
|
||||
|
||||
find_package_handle_standard_args(WASISDK REQUIRED_VARS WASISDK_HOME VERSION_VAR WASISDK_VERSION)
|
||||
|
||||
if(WASISDK_FOUND)
|
||||
set(WASISDK_CC_COMMAND ${WASISDK_HOME}/bin/clang)
|
||||
set(WASISDK_CXX_COMMAND ${WASISDK_HOME}/bin/clang++)
|
||||
set(WASISDK_TOOLCHAIN ${WASISDK_HOME}/share/cmake/wasi-sdk.cmake)
|
||||
set(WASISDK_SYSROOT ${WASISDK_HOME}/share/wasi-sysroot)
|
||||
endif()
|
||||
mark_as_advanced(WASISDK_CC_COMMAND WASISDK_CXX_COMMAND WASISDK_TOOLCHAIN WASISDK_SYSROOT WASISDK_HOME)
|
|
@ -1,91 +1,58 @@
|
|||
# Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
if (APPLE)
|
||||
set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
|
||||
set (CMAKE_C_LINK_FLAGS "")
|
||||
set (CMAKE_CXX_LINK_FLAGS "")
|
||||
cmake_minimum_required (VERSION 3.14)
|
||||
|
||||
project (debut_tools_wasm)
|
||||
|
||||
set (CMAKE_BUILD_TYPE Debug) # Otherwise no debug symbols (addr2line)
|
||||
|
||||
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../cmake)
|
||||
find_package (WAMRC REQUIRED)
|
||||
|
||||
option(SOURCE_MAP_DEMO "Enable source map demo" OFF)
|
||||
if (SOURCE_MAP_DEMO)
|
||||
find_package(EMSCRIPTEN 3.1.50 REQUIRED)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WASI_SDK_DIR)
|
||||
set (WASI_SDK_DIR "/opt/wasi-sdk")
|
||||
endif ()
|
||||
|
||||
if (DEFINED WASI_SYSROOT)
|
||||
set (CMAKE_SYSROOT "${WASI_SYSROOT}")
|
||||
endif ()
|
||||
|
||||
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
|
||||
set (CMAKE_ASM_COMPILER "${WASI_SDK_DIR}/bin/clang")
|
||||
set (CMAKE_EXE_LINKER_FLAGS "-target wasm32-wasi")
|
||||
|
||||
################ wabt and wamrc dependencies ################
|
||||
message(CHECK_START "Detecting WABT")
|
||||
if(NOT (DEFINED WABT_DIR OR DEFINED CACHE{WABT_DIR}))
|
||||
find_path(WABT_DIR
|
||||
wabt
|
||||
PATHS /opt
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(DEFINED WABT_DIR)
|
||||
set(WABT_DIR ${WABT_DIR}/wabt)
|
||||
endif()
|
||||
endif()
|
||||
if(WABT_DIR)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
|
||||
message(CHECK_START "Detecting WASM_OBJDUMP at ${WABT_DIR}")
|
||||
find_program(WASM_OBJDUMP
|
||||
wasm-objdump
|
||||
PATHS "${WABT_DIR}/bin"
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(WASM_OBJDUMP)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
if((NOT EXISTS ${WASM_OBJDUMP}) )
|
||||
message(FATAL_ERROR "Please make sure to have wasm-objdump under the path=${WABT_DIR}/bin ")
|
||||
endif()
|
||||
|
||||
set(WAMR_COMPILER_DIR ${CMAKE_CURRENT_LIST_DIR}/../../wamr-compiler/build)
|
||||
message(CHECK_START "Detecting WAMR_COMPILER at ${WAMR_COMPILER_DIR}")
|
||||
find_file(WAMR_COMPILER
|
||||
wamrc
|
||||
PATHS "${CMAKE_CURRENT_LIST_DIR}/../../../wamr-compiler/build"
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(WAMR_COMPILER)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
if((NOT EXISTS ${WAMR_COMPILER}) )
|
||||
message(FATAL_ERROR "Please build wamrc under the path=${WAMR_ROOT_DIR}/wamr-compiler/")
|
||||
endif()
|
||||
|
||||
################ wasm and aot compilation ################
|
||||
function (compile_sample SOURCE_FILE)
|
||||
get_filename_component (FILE_NAME ${SOURCE_FILE} NAME_WLE)
|
||||
set (WASM_MODULE ${FILE_NAME}.wasm)
|
||||
add_executable (${WASM_MODULE} ${SOURCE_FILE})
|
||||
|
||||
add_custom_target(
|
||||
wasm_to_aot
|
||||
## wasm
|
||||
set (WASM_FILE ${FILE_NAME}.wasm)
|
||||
add_executable (${FILE_NAME} ${SOURCE_FILE})
|
||||
set_target_properties (${FILE_NAME} PROPERTIES SUFFIX .wasm)
|
||||
|
||||
## aot
|
||||
set (AOT_FILE ${FILE_NAME}.aot)
|
||||
add_custom_target (
|
||||
${FILE_NAME}_aot
|
||||
ALL
|
||||
DEPENDS ${WAMR_COMPILER} ${WASM_MODULE}
|
||||
DEPENDS ${WAMRC_BIN} ${WASM_FILE}
|
||||
# Use --enable-dump-call-stack to generate stack trace (addr2line)
|
||||
COMMAND ${WAMR_COMPILER} --size-level=0 --enable-dump-call-stack -o wasm-apps/trap.aot wasm-apps/trap.wasm
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
COMMAND ${WAMRC_BIN} --size-level=0 --enable-dump-call-stack -o ${AOT_FILE} ${WASM_FILE}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
## wasm + sourcemap
|
||||
if (DEFINED EMSCRIPTEN)
|
||||
add_custom_target(
|
||||
${FILE_NAME}_w_sourcemap
|
||||
ALL
|
||||
DEPENDS ${SOURCE_FILE}
|
||||
COMMAND ${EMCC} -O0 -gsource-map -o ${FILE_NAME}.sourcemap.wasm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
endif ()
|
||||
|
||||
## install both
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${WASM_FILE} DESTINATION wasm-apps)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${AOT_FILE} DESTINATION wasm-apps)
|
||||
if (DEFINED EMSCRIPTEN)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}.sourcemap.wasm DESTINATION wasm-apps)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}.sourcemap.wasm.map DESTINATION wasm-apps)
|
||||
endif ()
|
||||
endfunction ()
|
||||
|
||||
set(CMAKE_BUILD_TYPE Debug) # Otherwise no debug symbols (addr2line)
|
||||
compile_sample(trap.c)
|
||||
compile_sample(trap.c)
|
||||
|
|
1
samples/native-stack-overflow/.gitignore
vendored
Normal file
1
samples/native-stack-overflow/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/out/
|
99
samples/native-stack-overflow/CMakeLists.txt
Normal file
99
samples/native-stack-overflow/CMakeLists.txt
Normal file
|
@ -0,0 +1,99 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required (VERSION 3.14)
|
||||
|
||||
include(CheckPIESupported)
|
||||
|
||||
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
|
||||
project (native-stack-overflow)
|
||||
else()
|
||||
project (native-stack-overflow C ASM)
|
||||
endif()
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_EXTENSIONS YES)
|
||||
|
||||
################ runtime settings ################
|
||||
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
|
||||
if (APPLE)
|
||||
add_definitions(-DBH_PLATFORM_DARWIN)
|
||||
endif ()
|
||||
|
||||
# Reset default linker flags
|
||||
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
||||
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
||||
|
||||
# WAMR features switch
|
||||
|
||||
# Set WAMR_BUILD_TARGET, currently values supported:
|
||||
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
|
||||
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
|
||||
if (NOT DEFINED WAMR_BUILD_TARGET)
|
||||
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
|
||||
set (WAMR_BUILD_TARGET "AARCH64")
|
||||
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
|
||||
set (WAMR_BUILD_TARGET "RISCV64")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
# Build as X86_64 by default in 64-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_64")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
# Build as X86_32 by default in 32-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_32")
|
||||
else ()
|
||||
message(SEND_ERROR "Unsupported build target platform!")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set (CMAKE_BUILD_TYPE Release)
|
||||
endif ()
|
||||
|
||||
set (WAMR_BUILD_INTERP 1)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
set (WAMR_BUILD_JIT 0)
|
||||
set (WAMR_BUILD_LIBC_BUILTIN 0)
|
||||
set (WAMR_BUILD_LIBC_WASI 1)
|
||||
|
||||
if (NOT MSVC)
|
||||
# linker flags
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
|
||||
endif ()
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
|
||||
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
|
||||
endif ()
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 13.0.0)
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-usage")
|
||||
endif ()
|
||||
|
||||
# GCC doesn't have disable_tail_calls attribute
|
||||
if (CMAKE_C_COMPILER_ID MATCHES "GNU")
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-optimize-sibling-calls")
|
||||
endif ()
|
||||
|
||||
# build out vmlib
|
||||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
|
||||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
|
||||
|
||||
################ application related ################
|
||||
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
|
||||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
||||
|
||||
add_executable (native-stack-overflow src/main.c src/native_impl.c ${UNCOMMON_SHARED_SOURCE})
|
||||
|
||||
check_pie_supported()
|
||||
set_target_properties (native-stack-overflow PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
if (APPLE)
|
||||
target_link_libraries (native-stack-overflow vmlib -lm -ldl -lpthread)
|
||||
else ()
|
||||
target_link_libraries (native-stack-overflow vmlib -lm -ldl -lpthread -lrt)
|
||||
endif ()
|
4
samples/native-stack-overflow/README.md
Normal file
4
samples/native-stack-overflow/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
The "native-stack-overflow" sample project
|
||||
==========================================
|
||||
|
||||
This sample examines native stack overflow detection mechanisms.
|
75
samples/native-stack-overflow/build.sh
Executable file
75
samples/native-stack-overflow/build.sh
Executable file
|
@ -0,0 +1,75 @@
|
|||
#! /bin/sh
|
||||
|
||||
#
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#
|
||||
|
||||
CURR_DIR=$PWD
|
||||
WAMR_DIR=${PWD}/../..
|
||||
OUT_DIR=${PWD}/out
|
||||
|
||||
WASM_APPS=${PWD}/wasm-apps
|
||||
|
||||
|
||||
rm -rf ${OUT_DIR}
|
||||
mkdir ${OUT_DIR}
|
||||
mkdir ${OUT_DIR}/wasm-apps
|
||||
|
||||
|
||||
echo "##################### build (default)"
|
||||
cd ${CURR_DIR}
|
||||
mkdir -p cmake_build
|
||||
cd cmake_build
|
||||
cmake ..
|
||||
make -j 4
|
||||
if [ $? != 0 ];then
|
||||
echo "BUILD_FAIL native-stack-overflow exit as $?\n"
|
||||
exit 2
|
||||
fi
|
||||
cp -a native-stack-overflow ${OUT_DIR}
|
||||
|
||||
echo "##################### build (WAMR_DISABLE_HW_BOUND_CHECK=1)"
|
||||
cd ${CURR_DIR}
|
||||
mkdir -p cmake_build_disable_hw_bound
|
||||
cd cmake_build_disable_hw_bound
|
||||
cmake -D WAMR_DISABLE_HW_BOUND_CHECK=1 ..
|
||||
make -j 4
|
||||
if [ $? != 0 ];then
|
||||
echo "BUILD_FAIL native-stack-overflow exit as $?\n"
|
||||
exit 2
|
||||
fi
|
||||
cp -a native-stack-overflow ${OUT_DIR}/native-stack-overflow.WAMR_DISABLE_HW_BOUND_CHECK
|
||||
|
||||
echo
|
||||
|
||||
echo "##################### build wasm apps"
|
||||
|
||||
cd ${WASM_APPS}
|
||||
|
||||
for i in `ls *.c`
|
||||
do
|
||||
APP_SRC="$i"
|
||||
OUT_FILE=${i%.*}.wasm
|
||||
|
||||
# use WAMR SDK to build out the .wasm binary
|
||||
/opt/wasi-sdk/bin/clang \
|
||||
-mexec-model=reactor \
|
||||
-Os -z stack-size=4096 -Wl,--initial-memory=65536 \
|
||||
-Wl,--allow-undefined \
|
||||
-o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
|
||||
|
||||
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
|
||||
echo "build ${OUT_FILE} success"
|
||||
else
|
||||
echo "build ${OUT_FILE} fail"
|
||||
fi
|
||||
done
|
||||
echo "#################### build wasm apps done"
|
||||
|
||||
echo "#################### aot-compile"
|
||||
WAMRC=${WAMR_DIR}/wamr-compiler/build/wamrc
|
||||
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot --size-level=0 ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
|
||||
echo "#################### aot-compile (--bounds-checks=1)"
|
||||
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot.bounds-checks --size-level=0 --bounds-checks=1 ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
1
samples/native-stack-overflow/clean.sh
Executable file
1
samples/native-stack-overflow/clean.sh
Executable file
|
@ -0,0 +1 @@
|
|||
rm -rf cmake_build cmake_build_disable_hw_bound out
|
20
samples/native-stack-overflow/run.sh
Executable file
20
samples/native-stack-overflow/run.sh
Executable file
|
@ -0,0 +1,20 @@
|
|||
#! /bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
NAME=${1:-test1}
|
||||
|
||||
echo "====== Interpreter ${NAME}"
|
||||
out/native-stack-overflow out/wasm-apps/testapp.wasm ${NAME}
|
||||
|
||||
echo
|
||||
echo "====== Interpreter WAMR_DISABLE_HW_BOUND_CHECK=1 ${NAME}"
|
||||
out/native-stack-overflow.WAMR_DISABLE_HW_BOUND_CHECK out/wasm-apps/testapp.wasm ${NAME}
|
||||
|
||||
echo
|
||||
echo "====== AOT ${NAME}"
|
||||
out/native-stack-overflow out/wasm-apps/testapp.wasm.aot ${NAME}
|
||||
|
||||
echo
|
||||
echo "====== AOT WAMR_DISABLE_HW_BOUND_CHECK=1 ${NAME}"
|
||||
out/native-stack-overflow.WAMR_DISABLE_HW_BOUND_CHECK out/wasm-apps/testapp.wasm.aot.bounds-checks ${NAME}
|
213
samples/native-stack-overflow/src/main.c
Normal file
213
samples/native-stack-overflow/src/main.c
Normal file
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* Copyright (C) 2024 Midokura Japan KK. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "bh_read_file.h"
|
||||
|
||||
uint32_t
|
||||
host_consume_stack_and_call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx,
|
||||
uint32_t x, uint32_t stack);
|
||||
uint32_t
|
||||
host_consume_stack(wasm_exec_env_t exec_env, uint32_t stack);
|
||||
|
||||
extern unsigned int nest;
|
||||
|
||||
static NativeSymbol native_symbols[] = {
|
||||
{ "host_consume_stack_and_call_indirect",
|
||||
host_consume_stack_and_call_indirect, "(iii)i", NULL },
|
||||
{ "host_consume_stack", host_consume_stack, "(i)i", NULL },
|
||||
};
|
||||
|
||||
void *
|
||||
canary_addr()
|
||||
{
|
||||
uint8_t *p = os_thread_get_stack_boundary();
|
||||
#if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
|
||||
uint32_t page_size = os_getpagesize();
|
||||
uint32_t guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
|
||||
return p + page_size * guard_page_count;
|
||||
#else
|
||||
return p;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
canary_init(void)
|
||||
{
|
||||
uint32_t *canary = canary_addr();
|
||||
*canary = 0xaabbccdd;
|
||||
}
|
||||
|
||||
bool
|
||||
canary_check(void)
|
||||
{
|
||||
/* assume an overflow if the first uint32_t on the stack was modified */
|
||||
const uint32_t *canary = (void *)canary_addr();
|
||||
return *canary == 0xaabbccdd;
|
||||
}
|
||||
|
||||
struct record {
|
||||
bool failed;
|
||||
bool leaked;
|
||||
char exception[128]; /* EXCEPTION_BUF_LEN */
|
||||
};
|
||||
|
||||
void
|
||||
print_record(unsigned int start, unsigned int end, const struct record *rec)
|
||||
{
|
||||
printf("%5u - %5u | %6s | %6s | %s\n", start, end,
|
||||
rec->failed ? "failed" : "ok", rec->leaked ? "leaked" : "ok",
|
||||
rec->exception);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *buffer;
|
||||
char error_buf[128];
|
||||
|
||||
if (argc != 3) {
|
||||
return 2;
|
||||
}
|
||||
const char *module_path = argv[1];
|
||||
const char *funcname = argv[2];
|
||||
|
||||
wasm_module_t module = NULL;
|
||||
uint32 buf_size;
|
||||
uint32 stack_size = 4096;
|
||||
/*
|
||||
* disable app heap.
|
||||
* - we use wasi
|
||||
* - https://github.com/bytecodealliance/wasm-micro-runtime/issues/2275
|
||||
*/
|
||||
uint32 heap_size = 0;
|
||||
|
||||
RuntimeInitArgs init_args;
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
init_args.mem_alloc_type = Alloc_With_System_Allocator;
|
||||
init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
|
||||
init_args.native_module_name = "env";
|
||||
init_args.native_symbols = native_symbols;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
printf("wasm_runtime_full_init failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer = bh_read_file_to_buffer(module_path, &buf_size);
|
||||
if (!buffer) {
|
||||
printf("bh_read_file_to_buffer failed\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
if (!module) {
|
||||
printf("wasm_runtime_load failed: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* header */
|
||||
printf(" stack size | fail? | leak? | exception\n");
|
||||
printf("-------------------------------------------------------------------"
|
||||
"--------\n");
|
||||
|
||||
uint32_t page_size = os_getpagesize();
|
||||
unsigned int stack;
|
||||
unsigned int prevstack = 0; /* appease GCC -Wmaybe-uninitialized */
|
||||
unsigned int stack_range_start = 0;
|
||||
unsigned int stack_range_end = page_size * 6;
|
||||
unsigned int step = 16;
|
||||
struct record rec0;
|
||||
struct record rec1;
|
||||
struct record *rec = &rec0;
|
||||
struct record *prevrec = &rec1;
|
||||
bool have_prevrec = false;
|
||||
for (stack = stack_range_start; stack < stack_range_end; stack += step) {
|
||||
wasm_module_inst_t module_inst = NULL;
|
||||
wasm_exec_env_t exec_env = NULL;
|
||||
bool failed = true;
|
||||
const char *exception = NULL;
|
||||
nest = 0;
|
||||
|
||||
canary_init();
|
||||
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf));
|
||||
if (!module_inst) {
|
||||
printf("wasm_runtime_instantiate failed: %s\n", error_buf);
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
|
||||
if (!exec_env) {
|
||||
printf("wasm_runtime_create_exec_env failed\n");
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
wasm_function_inst_t func =
|
||||
wasm_runtime_lookup_function(module_inst, funcname);
|
||||
if (!func) {
|
||||
printf("wasm_runtime_lookup_function failed for %s\n", funcname);
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
/* note: the function type is (ii)i */
|
||||
uint32_t wasm_argv[] = {
|
||||
stack, /* native_stack */
|
||||
30, /* recurse_count */
|
||||
};
|
||||
uint32_t wasm_argc = 2;
|
||||
if (!wasm_runtime_call_wasm(exec_env, func, wasm_argc, wasm_argv)) {
|
||||
exception = wasm_runtime_get_exception(module_inst);
|
||||
goto fail2;
|
||||
}
|
||||
failed = false;
|
||||
fail2:
|
||||
if (!canary_check()) {
|
||||
printf("stack overurn detected for stack=%u\n", stack);
|
||||
abort();
|
||||
}
|
||||
|
||||
/*
|
||||
* note: non-zero "nest" here demonstrates resource leak on longjmp
|
||||
* from signal handler.
|
||||
* cf.
|
||||
* https://github.com/bytecodealliance/wasm-micro-runtime/issues/3320
|
||||
*/
|
||||
memset(rec, 0, sizeof(*rec));
|
||||
rec->failed = failed;
|
||||
rec->leaked = nest != 0;
|
||||
strncpy(rec->exception, exception ? exception : "",
|
||||
sizeof(rec->exception));
|
||||
if (have_prevrec && memcmp(prevrec, rec, sizeof(*rec))) {
|
||||
print_record(prevstack, stack, prevrec);
|
||||
have_prevrec = false;
|
||||
}
|
||||
if (!have_prevrec) {
|
||||
prevstack = stack;
|
||||
struct record *tmp = prevrec;
|
||||
prevrec = rec;
|
||||
rec = tmp;
|
||||
have_prevrec = true;
|
||||
}
|
||||
if (exec_env) {
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
}
|
||||
if (module_inst) {
|
||||
wasm_runtime_deinstantiate(module_inst);
|
||||
}
|
||||
}
|
||||
if (have_prevrec) {
|
||||
print_record(prevstack, stack, prevrec);
|
||||
}
|
||||
|
||||
fail:
|
||||
if (module) {
|
||||
wasm_runtime_unload(module);
|
||||
}
|
||||
if (buffer) {
|
||||
BH_FREE(buffer);
|
||||
}
|
||||
wasm_runtime_destroy();
|
||||
}
|
89
samples/native-stack-overflow/src/native_impl.c
Normal file
89
samples/native-stack-overflow/src/native_impl.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (C) 2024 Midokura Japan KK. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#define __STDC_WANT_LIB_EXT1__ 1
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
/*
|
||||
* this "nest" var has two purposes:
|
||||
* - prevent tail-call optimization
|
||||
* - detect possible resource leak
|
||||
*/
|
||||
unsigned int nest = 0;
|
||||
ptrdiff_t prev_diff = 0;
|
||||
|
||||
uint32_t
|
||||
call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx, uint32_t x)
|
||||
{
|
||||
uint32_t argv[1] = {
|
||||
x,
|
||||
};
|
||||
uint32_t argc = 1;
|
||||
if (!wasm_runtime_call_indirect(exec_env, funcidx, argc, argv)) {
|
||||
/* failed */
|
||||
return 0;
|
||||
}
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
uint32_t
|
||||
host_consume_stack_and_call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx,
|
||||
uint32_t x, uint32_t stack)
|
||||
{
|
||||
void *boundary = os_thread_get_stack_boundary();
|
||||
void *fp = __builtin_frame_address(0);
|
||||
ptrdiff_t diff = fp - boundary;
|
||||
/*
|
||||
* because this function performs recursive calls depending on
|
||||
* the user input, we don't have an apriori knowledge how much stack
|
||||
* we need. perform the overflow check on each iteration.
|
||||
*/
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return 0;
|
||||
}
|
||||
if (diff > stack) {
|
||||
prev_diff = diff;
|
||||
nest++;
|
||||
uint32_t ret =
|
||||
host_consume_stack_and_call_indirect(exec_env, funcidx, x, stack);
|
||||
nest--;
|
||||
return ret;
|
||||
}
|
||||
return call_indirect(exec_env, funcidx, x);
|
||||
}
|
||||
|
||||
__attribute__((noinline)) static uint32_t
|
||||
consume_stack1(wasm_exec_env_t exec_env, void *base, uint32_t stack)
|
||||
#if defined(__clang__)
|
||||
__attribute__((disable_tail_calls))
|
||||
#endif
|
||||
{
|
||||
void *fp = __builtin_frame_address(0);
|
||||
ptrdiff_t diff = (unsigned char *)base - (unsigned char *)fp;
|
||||
assert(diff > 0);
|
||||
if (diff > stack) {
|
||||
return diff;
|
||||
}
|
||||
return consume_stack1(exec_env, base, stack);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
host_consume_stack(wasm_exec_env_t exec_env, uint32_t stack)
|
||||
{
|
||||
/*
|
||||
* this function consumes a bit more than "stack" bytes.
|
||||
*/
|
||||
if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, 64 + stack)) {
|
||||
return 0;
|
||||
}
|
||||
void *base = __builtin_frame_address(0);
|
||||
return consume_stack1(exec_env, base, stack);
|
||||
}
|
74
samples/native-stack-overflow/wasm-apps/testapp.c
Normal file
74
samples/native-stack-overflow/wasm-apps/testapp.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (C) 2024 Midokura Japan KK. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t
|
||||
host_consume_stack_and_call_indirect(int (*)(int), uint32_t, uint32_t);
|
||||
uint32_t host_consume_stack(uint32_t);
|
||||
|
||||
int
|
||||
cb(int x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
int
|
||||
consume_stack_cb(int x) __attribute__((disable_tail_calls))
|
||||
{
|
||||
/*
|
||||
* intentions:
|
||||
*
|
||||
* - consume native stack by making recursive calls
|
||||
*
|
||||
* - avoid tail-call optimization (either by the C compiler or
|
||||
* aot-compiler)
|
||||
*/
|
||||
if (x == 0) {
|
||||
return 0;
|
||||
}
|
||||
return consume_stack_cb(x - 1) + 1;
|
||||
}
|
||||
|
||||
int
|
||||
host_consume_stack_cb(int x)
|
||||
{
|
||||
return host_consume_stack(x);
|
||||
}
|
||||
|
||||
__attribute__((export_name("test1"))) uint32_t
|
||||
test1(uint32_t native_stack, uint32_t recurse_count)
|
||||
{
|
||||
/*
|
||||
* ------ os_thread_get_stack_boundary
|
||||
* ^
|
||||
* |
|
||||
* | "native_stack" bytes of stack left by
|
||||
* | host_consume_stack_and_call_indirect
|
||||
* |
|
||||
* | ^
|
||||
* | |
|
||||
* | | consume_stack_cb (interpreter or aot)
|
||||
* v |
|
||||
* ^
|
||||
* |
|
||||
* | host_consume_stack_and_call_indirect
|
||||
* |
|
||||
*
|
||||
*
|
||||
*/
|
||||
uint32_t ret = host_consume_stack_and_call_indirect(
|
||||
consume_stack_cb, recurse_count, native_stack);
|
||||
return 42;
|
||||
}
|
||||
|
||||
__attribute__((export_name("test2"))) uint32_t
|
||||
test2(uint32_t native_stack, uint32_t recurse_count)
|
||||
{
|
||||
uint32_t ret = host_consume_stack_and_call_indirect(host_consume_stack_cb,
|
||||
6000, native_stack);
|
||||
return 42;
|
||||
}
|
|
@ -43,6 +43,28 @@ For example, there is a call-stack dump:
|
|||
"""
|
||||
|
||||
|
||||
def locate_sourceMappingURL_section(wasm_objdump: Path, wasm_file: Path) -> bool:
|
||||
"""
|
||||
Figure out if the wasm file has a sourceMappingURL section.
|
||||
"""
|
||||
cmd = f"{wasm_objdump} -h {wasm_file}"
|
||||
p = subprocess.run(
|
||||
shlex.split(cmd),
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
universal_newlines=True,
|
||||
)
|
||||
outputs = p.stdout.split(os.linesep)
|
||||
|
||||
for line in outputs:
|
||||
line = line.strip()
|
||||
if "sourceMappingURL" in line:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def get_code_section_start(wasm_objdump: Path, wasm_file: Path) -> int:
|
||||
"""
|
||||
Find the start offset of Code section in a wasm file.
|
||||
|
@ -62,15 +84,6 @@ def get_code_section_start(wasm_objdump: Path, wasm_file: Path) -> int:
|
|||
)
|
||||
outputs = p.stdout.split(os.linesep)
|
||||
|
||||
# if there is no .debug section, return -1
|
||||
for line in outputs:
|
||||
line = line.strip()
|
||||
if ".debug_info" in line:
|
||||
break
|
||||
else:
|
||||
print(f"No .debug_info section found {wasm_file}")
|
||||
return -1
|
||||
|
||||
for line in outputs:
|
||||
line = line.strip()
|
||||
if "Code" in line:
|
||||
|
@ -79,7 +92,7 @@ def get_code_section_start(wasm_objdump: Path, wasm_file: Path) -> int:
|
|||
return -1
|
||||
|
||||
|
||||
def get_line_info_from_function_addr(
|
||||
def get_line_info_from_function_addr_dwarf(
|
||||
dwarf_dump: Path, wasm_file: Path, offset: int
|
||||
) -> tuple[str, str, str, str]:
|
||||
"""
|
||||
|
@ -126,7 +139,7 @@ def get_dwarf_tag_value(tag: str, line: str) -> str:
|
|||
return m.groups()[0]
|
||||
|
||||
|
||||
def get_line_info_from_function_name(
|
||||
def get_line_info_from_function_name_dwarf(
|
||||
dwarf_dump: Path, wasm_file: Path, function_name: str
|
||||
) -> tuple[str, str, str]:
|
||||
"""
|
||||
|
@ -160,6 +173,51 @@ def get_line_info_from_function_name(
|
|||
return (function_name, function_file, function_line)
|
||||
|
||||
|
||||
def get_line_info_from_function_addr_sourcemapping(
|
||||
emsymbolizer: Path, wasm_file: Path, offset: int
|
||||
) -> tuple[str, str, str, str]:
|
||||
"""
|
||||
Find the location info of a given offset in a wasm file which is compiled with emcc.
|
||||
|
||||
{emsymbolizer} {wasm_file} {offset of file}
|
||||
|
||||
there usually are two lines:
|
||||
??
|
||||
relative path to source file:line:column
|
||||
"""
|
||||
debug_info_source = wasm_file.with_name(f"{wasm_file.name}.map")
|
||||
cmd = f"{emsymbolizer} -t code -f {debug_info_source} {wasm_file} {offset}"
|
||||
p = subprocess.run(
|
||||
shlex.split(cmd),
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
universal_newlines=True,
|
||||
cwd=Path.cwd(),
|
||||
)
|
||||
outputs = p.stdout.split(os.linesep)
|
||||
|
||||
function_name, function_file = "<unknown>", "unknown"
|
||||
function_line, function_column = "?", "?"
|
||||
|
||||
for line in outputs:
|
||||
line = line.strip()
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
||||
m = re.match("(.*):(\d+):(\d+)", line)
|
||||
if m:
|
||||
function_file, function_line, function_column = m.groups()
|
||||
continue
|
||||
else:
|
||||
# it's always ??, not sure about that
|
||||
if "??" != line:
|
||||
function_name = line
|
||||
|
||||
return (function_name, function_file, function_line, function_column)
|
||||
|
||||
|
||||
def parse_line_info(line_info: str) -> tuple[str, str, str]:
|
||||
"""
|
||||
line_info -> [file, line, column]
|
||||
|
@ -250,6 +308,7 @@ def main():
|
|||
action="store_true",
|
||||
help="use call stack without addresses or from fast interpreter mode",
|
||||
)
|
||||
parser.add_argument("--emsdk", type=Path, help="path to emsdk")
|
||||
args = parser.parse_args()
|
||||
|
||||
wasm_objdump = args.wabt.joinpath("bin/wasm-objdump")
|
||||
|
@ -261,6 +320,15 @@ def main():
|
|||
llvm_cxxfilt = args.wasi_sdk.joinpath("bin/llvm-cxxfilt")
|
||||
assert llvm_cxxfilt.exists()
|
||||
|
||||
emcc_production = locate_sourceMappingURL_section(wasm_objdump, args.wasm_file)
|
||||
if emcc_production:
|
||||
if args.emsdk is None:
|
||||
print("Please provide the path to emsdk via --emsdk")
|
||||
return -1
|
||||
|
||||
emsymbolizer = args.emsdk.joinpath("upstream/emscripten/emsymbolizer")
|
||||
assert emsymbolizer.exists()
|
||||
|
||||
code_section_start = get_code_section_start(wasm_objdump, args.wasm_file)
|
||||
if code_section_start == -1:
|
||||
return -1
|
||||
|
@ -281,6 +349,7 @@ def main():
|
|||
|
||||
_, offset, index = splitted
|
||||
if args.no_addr:
|
||||
# FIXME: w/ emcc production
|
||||
if not index.startswith("$f"): # E.g. _start or Text format
|
||||
print(f"{i}: {index}")
|
||||
continue
|
||||
|
@ -290,22 +359,40 @@ def main():
|
|||
print(f"{i}: {line}")
|
||||
continue
|
||||
|
||||
line_info = get_line_info_from_function_name(
|
||||
llvm_dwarf_dump, args.wasm_file, function_index_to_name[index]
|
||||
)
|
||||
if not emcc_production:
|
||||
_, function_file, function_line = (
|
||||
get_line_info_from_function_name_dwarf(
|
||||
llvm_dwarf_dump,
|
||||
args.wasm_file,
|
||||
function_index_to_name[index],
|
||||
)
|
||||
)
|
||||
else:
|
||||
_, function_file, function_line = _, "unknown", "?"
|
||||
|
||||
_, function_file, function_line = line_info
|
||||
function_name = demangle(llvm_cxxfilt, function_index_to_name[index])
|
||||
print(f"{i}: {function_name}")
|
||||
print(f"\tat {function_file}:{function_line}")
|
||||
else:
|
||||
offset = int(offset, 16)
|
||||
# match the algorithm in wasm_interp_create_call_stack()
|
||||
# either a *offset* to *code* section start
|
||||
# or a *offset* in a file
|
||||
assert offset > code_section_start
|
||||
offset = offset - code_section_start
|
||||
function_name, function_file, function_line, function_column = (
|
||||
get_line_info_from_function_addr(
|
||||
llvm_dwarf_dump, args.wasm_file, offset
|
||||
|
||||
if emcc_production:
|
||||
function_name, function_file, function_line, function_column = (
|
||||
get_line_info_from_function_addr_sourcemapping(
|
||||
emsymbolizer, args.wasm_file, offset
|
||||
)
|
||||
)
|
||||
else:
|
||||
function_name, function_file, function_line, function_column = (
|
||||
get_line_info_from_function_addr_dwarf(
|
||||
llvm_dwarf_dump, args.wasm_file, offset
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
# if can't parse function_name, use name section or <index>
|
||||
if function_name == "<unknown>":
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user