From 1103c16ff2e8411c8fd97e60ecdadd0ddf0e9a25 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 25 Sep 2021 00:10:22 +0800 Subject: [PATCH 1/6] zephyr: add module.yml and fix riscv compile error --- core/iwasm/aot/arch/aot_reloc_riscv.c | 2 +- core/iwasm/aot/arch/aot_reloc_thumb.c | 2 +- zephyr/module.yml | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 zephyr/module.yml diff --git a/core/iwasm/aot/arch/aot_reloc_riscv.c b/core/iwasm/aot/arch/aot_reloc_riscv.c index 244e61b41b..a5e0609293 100644 --- a/core/iwasm/aot/arch/aot_reloc_riscv.c +++ b/core/iwasm/aot/arch/aot_reloc_riscv.c @@ -77,7 +77,7 @@ rv_set_val(uint16 *addr, uint32 val) *addr = (val & 0xffff); *(addr + 1) = (val >> 16); - asm volatile("fence.i"); + __asm__ volatile("fence.i"); } /* Add a val to given address */ diff --git a/core/iwasm/aot/arch/aot_reloc_thumb.c b/core/iwasm/aot/arch/aot_reloc_thumb.c index 01fbe77ee0..19ed62c066 100644 --- a/core/iwasm/aot/arch/aot_reloc_thumb.c +++ b/core/iwasm/aot/arch/aot_reloc_thumb.c @@ -153,7 +153,7 @@ get_target_symbol_map(uint32 *sym_num) void get_current_target(char *target_buf, uint32 target_buf_size) { - const char * s = BUILD_TARGET; + const char *s = BUILD_TARGET; size_t s_size = sizeof(BUILD_TARGET); char *d = target_buf; diff --git a/zephyr/module.yml b/zephyr/module.yml new file mode 100644 index 0000000000..059c7368fa --- /dev/null +++ b/zephyr/module.yml @@ -0,0 +1,5 @@ +name: wasm-micro-runtime + +build: + cmake-ext: True + kconfig-ext: True From 3a5e2e9dbcf39572ace68268143d5eaf75f50a8c Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 29 Sep 2021 18:49:33 +0800 Subject: [PATCH 2/6] Fix klocwork issue, update document and update spec test script --- README.md | 2 +- core/iwasm/aot/arch/aot_reloc_riscv.c | 6 +++--- core/iwasm/common/wasm_c_api.c | 12 +++++++----- core/iwasm/compilation/simd/simd_load_store.c | 14 +++++++++++++- doc/build_wamr.md | 2 +- tests/wamr-test-suites/spec-test-script/runtest.py | 3 --- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a85e99fd62..0b8103b005 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Both wasm binary file and AoT file are supported by iwasm. The wamrc AoT compile cd wamr-compiler ./build_llvm.sh (or "./build_llvm_xtensa.sh" to support xtensa target) mkdir build && cd build -cmake .. (or "cmake .. -DWAMR_BUILD_TARGET=darwin" for MacOS) +cmake .. (or "cmake .. -DWAMR_BUILD_PLATFORM=darwin" for MacOS) make # wamrc is generated under current directory ``` diff --git a/core/iwasm/aot/arch/aot_reloc_riscv.c b/core/iwasm/aot/arch/aot_reloc_riscv.c index a5e0609293..cb09b810a6 100644 --- a/core/iwasm/aot/arch/aot_reloc_riscv.c +++ b/core/iwasm/aot/arch/aot_reloc_riscv.c @@ -267,7 +267,7 @@ apply_relocation(AOTModule *module, case R_RISCV_HI20: { - val = (int32)(intptr_t)(symbol_addr + reloc_addend); + val = (int32)(intptr_t)((uint8 *)symbol_addr + reloc_addend); CHECK_RELOC_OFFSET(sizeof(uint32)); if (val != (intptr_t)((uint8 *)symbol_addr + reloc_addend)) { @@ -284,7 +284,7 @@ apply_relocation(AOTModule *module, case R_RISCV_LO12_I: { - val = (int32)(intptr_t)(symbol_addr + reloc_addend); + val = (int32)(intptr_t)((uint8 *)symbol_addr + reloc_addend); CHECK_RELOC_OFFSET(sizeof(uint32)); if (val != (intptr_t)((uint8 *)symbol_addr + reloc_addend)) { @@ -301,7 +301,7 @@ apply_relocation(AOTModule *module, case R_RISCV_LO12_S: { - val = (int32)(intptr_t)(symbol_addr + reloc_addend); + val = (int32)(intptr_t)((uint8 *)symbol_addr + reloc_addend); CHECK_RELOC_OFFSET(sizeof(uint32)); if (val != (intptr_t)((uint8 *)symbol_addr + reloc_addend)) { diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index d79a75f4e2..6bf60e2e78 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -2804,9 +2804,10 @@ wasm_func_call(const wasm_func_t *func, /* copy parametes */ if (param_count - && !(argc = params_to_argv(func->inst_comm_rt, params->data, - wasm_functype_params(func->type), - param_count, argv))) { + && (!params + || !(argc = params_to_argv(func->inst_comm_rt, params->data, + wasm_functype_params(func->type), + param_count, argv)))) { goto failed; } @@ -2825,8 +2826,9 @@ wasm_func_call(const wasm_func_t *func, /* copy results */ if (result_count) { - if (!(argc = argv_to_results(argv, wasm_functype_results(func->type), - result_count, results->data))) { + if (!results + || !(argc = argv_to_results(argv, wasm_functype_results(func->type), + result_count, results->data))) { goto failed; } results->num_elems = result_count; diff --git a/core/iwasm/compilation/simd/simd_load_store.c b/core/iwasm/compilation/simd/simd_load_store.c index 097974b472..3719559c9c 100644 --- a/core/iwasm/compilation/simd/simd_load_store.c +++ b/core/iwasm/compilation/simd/simd_load_store.c @@ -82,7 +82,11 @@ aot_compile_simd_load_extend(AOTCompContext *comp_ctx, LLVMVectorType(INT16_TYPE, 4), LLVMVectorType(INT16_TYPE, 4), LLVMVectorType(I32_TYPE, 2), LLVMVectorType(I32_TYPE, 2), }; - LLVMTypeRef sub_vector_type = sub_vector_types[opcode_index]; + LLVMTypeRef sub_vector_type; + + bh_assert(opcode_index < 6); + + sub_vector_type = sub_vector_types[opcode_index]; /* to vector ptr type */ if (!sub_vector_type @@ -139,6 +143,8 @@ aot_compile_simd_load_splat(AOTCompContext *comp_ctx, LLVM_CONST(i32x2_zero), }; + bh_assert(opcode_index < 4); + if (!(element = simd_load(comp_ctx, func_ctx, align, offset, data_lengths[opcode_index], element_ptr_types[opcode_index]))) { @@ -179,6 +185,8 @@ aot_compile_simd_load_lane(AOTCompContext *comp_ctx, V128_i32x4_TYPE, V128_i64x2_TYPE }; LLVMValueRef lane = simd_lane_id_to_llvm_value(comp_ctx, lane_id); + bh_assert(opcode_index < 4); + if (!(vector = simd_pop_v128_and_bitcast( comp_ctx, func_ctx, vector_types[opcode_index], "src"))) { return false; @@ -225,6 +233,8 @@ aot_compile_simd_load_zero(AOTCompContext *comp_ctx, { LLVM_CONST(i32_zero), LLVM_CONST(i32_two) }, }; + bh_assert(opcode_index < 2); + if (!(element = simd_load(comp_ctx, func_ctx, align, offset, data_lengths[opcode_index], element_ptr_types[opcode_index]))) { @@ -320,6 +330,8 @@ aot_compile_simd_store_lane(AOTCompContext *comp_ctx, V128_i32x4_TYPE, V128_i64x2_TYPE }; LLVMValueRef lane = simd_lane_id_to_llvm_value(comp_ctx, lane_id); + bh_assert(opcode_index < 4); + if (!(vector = simd_pop_v128_and_bitcast( comp_ctx, func_ctx, vector_types[opcode_index], "src"))) { return false; diff --git a/doc/build_wamr.md b/doc/build_wamr.md index f11c9e6407..1203008f2e 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -19,7 +19,7 @@ The script `runtime_lib.cmake` defines a number of variables for configuring the - **WAMR_BUILD_PLATFORM**: set the target platform. It can be set to any platform name (folder name) under folder [core/shared/platform](../core/shared/platform). -- **WAMR_BUILD_TARGET**: set the target CPU architecture. Current supported targets are: X86_64, X86_32, AARCH64, ARM, THUMB, XTENSA, RISCV64 and MIPS. +- **WAMR_BUILD_TARGET**: set the target CPU architecture. Current supported targets are: X86_64, X86_32, AARCH64, ARM, THUMB, XTENSA, ARC, RISCV32, RISCV64 and MIPS. - For ARM and THUMB, the format is \\[\]\[_VFP], where \ is the ARM sub-architecture and the "_VFP" suffix means using VFP coprocessor registers s0-s15 (d0-d7) for passing arguments or returning results in standard procedure-call. Both \ and "_VFP" are optional, e.g. ARMV7, ARMV7_VFP, THUMBV7, THUMBV7_VFP and so on. - For AARCH64, the format is\[\], VFP is enabled by default. \ is optional, e.g. AARCH64, AARCH64V8, AARCH64V8.1 and so on. - For RISCV64, the format is \[_abi], where "_abi" is optional, currently the supported formats are RISCV64, RISCV64_LP64D and RISCV64_LP64: RISCV64 and RISCV64_LP64D are identical, using [LP64D](https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#-named-abis) as abi (LP64 with hardware floating-point calling convention for FLEN=64). And RISCV64_LP64 uses [LP64](https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#-named-abis) as abi (Integer calling-convention only, and hardware floating-point calling convention is not used). diff --git a/tests/wamr-test-suites/spec-test-script/runtest.py b/tests/wamr-test-suites/spec-test-script/runtest.py index 0209570c65..f795784830 100755 --- a/tests/wamr-test-suites/spec-test-script/runtest.py +++ b/tests/wamr-test-suites/spec-test-script/runtest.py @@ -902,9 +902,6 @@ def compile_wast_to_wasm(form, wast_tempfile, wasm_tempfile, opts): wast_tempfile, "-o", wasm_tempfile ] # optional arguments - if opts.simd: - cmd.append("--enable-simd") - if opts.ref_types: cmd.append("--enable-reference-types") cmd.append("--enable-bulk-memory") From 4d5f77b5e2847a00c7e67e610326cfbc3c630aa3 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 29 Sep 2021 23:03:47 +0800 Subject: [PATCH 3/6] Customize clang-format coding styles based on Mozilla template Customize clang-format coding styles based on Mozilla template. To check if source codes are well formatted: ``` bash $ cd ${wamr-root} $ clang-format --Werror --dry-run --style=file path/to/file ``` To format source codes in place ``` bash $ cd ${wamr_root} $ clang-format -i --style=file path/to/file ``` --- .clang-format | 145 +++----------------- .clang-tidy | 16 +++ ci/run_pre_commit_check.py | 263 +++++++++++++++++++++++++++++++++++++ 3 files changed, 300 insertions(+), 124 deletions(-) create mode 100644 .clang-tidy create mode 100644 ci/run_pre_commit_check.py diff --git a/.clang-format b/.clang-format index f0ecd63bf9..38fcef4ba3 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,6 @@ +# using [clang-formt-12 options](https://releases.llvm.org/12.0.0/tools/clang/docs/ClangFormatStyleOptions.html) RawStringFormats: - - Language: Cpp + - Language: Cpp Delimiters: - c - C @@ -13,35 +14,23 @@ RawStringFormats: - h - hpp CanonicalDelimiter: '' - BasedOnStyle: google - - Language: TextProto - Delimiters: - - pb - - PB - - proto - - PROTO - EnclosingFunctions: - - EqualsProto - - EquivToProto - - PARSE_PARTIAL_TEXT_PROTO - - PARSE_TEST_PROTO - - PARSE_TEXT_PROTO - - ParseTextOrDie - - ParseTextProtoOrDie - CanonicalDelimiter: '' - BasedOnStyle: google + BasedOnStyle: Mozilla Language: Cpp BasedOnStyle: Mozilla +# 6.1 IndentWidth: 4 +ContinuationIndentWidth: 4 +# 6.2 +TabWidth: 4 +UseTab: Never +# 6.3 +ColumnLimit: 80 +# 6.9 AlignAfterOpenBracket: Align -AllowAllArgumentsOnNextLine: false -AlignConsecutiveMacros: true -AllowShortBlocksOnASingleLine: true -AlwaysBreakAfterReturnType: All BinPackArguments: true -BinPackParameters: false -BreakBeforeBinaryOperators: NonAssignment +BinPackParameters: true +# 6.10 BreakBeforeBraces: Custom BraceWrapping: AfterCaseLabel: true @@ -60,104 +49,12 @@ BraceWrapping: SplitEmptyFunction: true SplitEmptyRecord: false SplitEmptyNamespace: true -ColumnLimit: 79 -ContinuationIndentWidth: 2 -DerivePointerAlignment: false -IncludeBlocks: Regroup -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 1 - - Regex: ".*" - Priority: 3 -IndentPPDirectives: None -KeepEmptyLinesAtTheStartOfBlocks: false -NamespaceIndentation: None -PointerAlignment: Right -ReflowComments: false -SortIncludes: false -Standard: Auto -StatementMacros: - - Q_UNUSED - - QT_REQUIRE_VERSION - -# AccessModifierOffset: -2 -# AlignConsecutiveAssignments: false -# AlignConsecutiveDeclarations: false -# AlignEscapedNewlines: Right -# AlignOperands: true -# AlignTrailingComments: true -# AllowAllConstructorInitializersOnNextLine: true -# AllowAllParametersOfDeclarationOnNextLine: false -# AllowShortCaseLabelsOnASingleLine: false -# AllowShortFunctionsOnASingleLine: Inline -# AllowShortLambdasOnASingleLine: All -# AllowShortIfStatementsOnASingleLine: Never -# AllowShortLoopsOnASingleLine: false -# AlwaysBreakAfterDefinitionReturnType: TopLevel -# AlwaysBreakAfterReturnType: TopLevel -# AlwaysBreakBeforeMultilineStrings: false -# AlwaysBreakTemplateDeclarations: Yes -# BreakBeforeInheritanceComma: false -# BreakInheritanceList: BeforeComma -# BreakBeforeTernaryOperators: true -# BreakConstructorInitializersBeforeComma: false -# BreakConstructorInitializers: BeforeComma -# BreakAfterJavaFieldAnnotations: false -# BreakStringLiterals: true -# CommentPragmas: '^ IWYU pragma:' -# CompactNamespaces: false -# ConstructorInitializerAllOnOneLineOrOnePerLine: false -# ConstructorInitializerIndentWidth: 2 -# Cpp11BracedListStyle: false -# DisableFormat: false -# ExperimentalAutoDetectBinPacking: false -# FixNamespaceComments: false -# ForEachMacros: -# - foreach -# - Q_FOREACH -# - BOOST_FOREACH -# IncludeIsMainRegex: '(Test)?$' -# IndentCaseLabels: true -# IndentWrappedFunctionNames: false -# JavaScriptQuotes: Leave -# JavaScriptWrapImports: true -# KeepEmptyLinesAtTheStartOfBlocks: true -# MacroBlockBegin: '' -# MacroBlockEnd: '' -# MaxEmptyLinesToKeep: 1 -# ObjCBinPackProtocolList: Auto -# ObjCBlockIndentWidth: 2 -# ObjCSpaceAfterProperty: true -# ObjCSpaceBeforeProtocolList: false -# PenaltyBreakAssignment: 2 -# PenaltyBreakBeforeFirstCallParameter: 19 -# PenaltyBreakComment: 300 -# PenaltyBreakFirstLessLess: 120 -# PenaltyBreakString: 1000 -# PenaltyBreakTemplateDeclaration: 10 -# PenaltyExcessCharacter: 1000000 -# PenaltyReturnTypeOnItsOwnLine: 200 -# SortIncludes: true -# SortUsingDeclarations: true -# SpaceAfterCStyleCast: false -# SpaceAfterLogicalNot: false -# SpaceAfterTemplateKeyword: false -# SpaceBeforeAssignmentOperators: true -# SpaceBeforeCpp11BracedList: false -# SpaceBeforeCtorInitializerColon: true -# SpaceBeforeInheritanceColon: true -# SpaceBeforeParens: ControlStatements -# SpaceBeforeRangeBasedForLoopColon: true -# SpaceInEmptyParentheses: false -# SpacesBeforeTrailingComments: 1 -# SpacesInAngles: false -# SpacesInContainerLiterals: true -# SpacesInCStyleCastParentheses: false -# SpacesInParentheses: false -# SpacesInSquareBrackets: false -# TabWidth: 4 -# UseTab: Never -# ... +# 6.27 +BreakBeforeBinaryOperators: NonAssignment +# additional +AlignEscapedNewlines: Left +AllowAllParametersOfDeclarationOnNextLine: false +AllowAllArgumentsOnNextLine: false +PointerAlignment: Right +SpaceAroundPointerQualifiers: After diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000000..67cd167fc4 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,16 @@ +# refer to https://clang.llvm.org/extra/clang-tidy/checks/list.html + +Checks: '-*, readability-identifier-naming, clang-analyzer-core.*,' +WarningsAsErrors: '-*' +HeaderFilterRegex: '' +FormatStyle: file +InheritParentConfig: false +AnalyzeTemporaryDtors: false +User: wamr +CheckOptions: + - key: readability-identifier-naming.VariableCase + value: lower_case + - key: readability-identifier-naming.ParameterCase + value: lower_case + - key: readability-identifier-naming.MacroDefinitionCase + value: UPPER_CASE diff --git a/ci/run_pre_commit_check.py b/ci/run_pre_commit_check.py new file mode 100644 index 0000000000..41f907bd82 --- /dev/null +++ b/ci/run_pre_commit_check.py @@ -0,0 +1,263 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +import json +import os +import pathlib +import queue +import re +import shlex +import shutil +import subprocess +import sys + +CLANG_CMD = "clang-13" +CLANG_CPP_CMD = "clang-cpp-13" +CLANG_FORMAT_CMD = "clang-format-13" +CLANG_TIDY_CMD = "clang-tidy-13" +CMAKE_CMD = "cmake" + + +# glob style patterns +EXCLUDE_PATHS = [ + "**/.git/", + "**/.github/", + "**/.vscode/", + "**/assembly-script/", + "**/build/", + "**/build-scripts/", + "**/ci/", + "**/core/deps/", + "**/doc/", + "**/samples/workload/", + "**/test-tools/", + "**/wamr-sdk/", + "**/wamr-dev/", + "**/wamr-dev-simd/", +] + +C_SUFFIXES = [".c", ".h"] + +VALID_DIR_NAME = r"([a-zA-Z0-9]+\-*)+[a-zA-Z0-9]*" +VALID_FILE_NAME = r"\.?([a-zA-Z0-9]+\_*)+[a-zA-Z0-9]*\.*\w*" + + +def locate_command(command): + if not shutil.which(command): + print(f"Command '{command}'' not found") + return False + + return True + + +def is_excluded(path): + for exclude_path in EXCLUDE_PATHS: + if path.match(exclude_path): + return True + return False + + +def pre_flight_check(root): + def check_clang_foramt(root): + if not locate_command(CLANG_FORMAT_CMD): + return False + + # Quick syntax check for .clang-format + try: + subprocess.check_call( + shlex.split(f"{CLANG_FORMAT_CMD} --dump-config"), cwd=root + ) + except subprocess.CalledProcessError: + print(f"Might have a typo in .clang-format") + return False + return True + + def check_clang_tidy(root): + if not locate_command(CLANG_TIDY_CMD): + return False + + if ( + not locate_command(CLANG_CMD) + or not locate_command(CLANG_CPP_CMD) + or not locate_command(CMAKE_CMD) + ): + return False + + # Quick syntax check for .clang-format + try: + subprocess.check_call( + shlex.split("{CLANG_TIDY_CMD} --dump-config"), cwd=root + ) + except subprocess.CalledProcessError: + print(f"Might have a typo in .clang-tidy") + return False + + # looking for compile command database + return True + + def check_aspell(root): + return True + + return check_clang_foramt(root) and check_clang_tidy(root) and check_aspell(root) + + +def run_clang_format(file_path, root): + try: + subprocess.check_call( + shlex.split( + f"{CLANG_FORMAT_CMD} --style=file --Werror --dry-run {file_path}" + ), + cwd=root, + ) + return True + except subprocess.CalledProcessError: + print(f"{file_path} failed the check of {CLANG_FORMAT_CMD}") + return False + + +def generate_compile_commands(compile_command_database, root): + CMD = f"{CMAKE_CMD} -DCMAKE_C_COMPILER={shutil.which(CLANG_CMD)} -DCMAKE_CXX_COMPILER={shutil.which(CLANG_CPP_CMD)} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .." + + try: + linux_mini_build = root.joinpath("product-mini/platforms/linux/build").resolve() + linux_mini_build.mkdir(exist_ok=True) + if subprocess.check_call(shlex.split(CMD), cwd=linux_mini_build): + return False + + wamrc_build = root.joinpath("wamr-compiler/build").resolve() + wamrc_build.mkdir(exist_ok=True) + + if subprocess.check_call(shlex.split(CMD), cwd=wamrc_build): + return False + + with open(linux_mini_build.joinpath("compile_commands.json"), "r") as f: + iwasm_compile_commands = json.load(f) + + with open(wamrc_build.joinpath("compile_commands.json"), "r") as f: + wamrc_compile_commands = json.load(f) + + all_compile_commands = iwasm_compile_commands + wamrc_compile_commands + # TODO: duplication items ? + with open(compile_command_database, "w") as f: + json.dump(all_compile_commands, f) + + return True + except subprocess.CalledProcessError: + return False + + +def run_clang_tidy(file_path, root): + # preparatoin + compile_command_database = pathlib.Path("/tmp/compile_commands.json") + if not compile_command_database.exists() and not generate_compile_commands( + compile_command_database, root + ): + return False + + try: + if subprocess.check_call( + shlex.split(f"{CLANG_TIDY_CMD} -p={compile_command_database} {file_path}"), + cwd=root, + ): + print(f"{file_path} failed the check of {CLANG_TIDY_CMD}") + except subprocess.CalledProcessError: + print(f"{file_path} failed the check of {CLANG_TIDY_CMD}") + return False + return True + + +def run_aspell(file_path, root): + return True + + +def check_dir_name(path, root): + # since we don't want to check the path beyond root. + # we hope "-" only will be used in a dir name as separators + return all( + [ + re.match(VALID_DIR_NAME, path_part) + for path_part in path.relative_to(root).parts + ] + ) + + +def check_file_name(path): + # since we don't want to check the path beyond root. + # we hope "_" only will be used in a file name as separators + return re.match(VALID_FILE_NAME, path.name) is not None + + +def run_pre_commit_check(path, root=None): + path = path.resolve() + if path.is_dir(): + if not check_dir_name(path, root): + print(f"{path} is not a valid directory name") + return False + else: + return True + + if path.is_file(): + if not check_file_name(path): + print(f"{path} is not a valid file name") + return False + + if not path.suffix in C_SUFFIXES: + return True + + return ( + run_clang_format(path, root) + and run_clang_tidy(path, root) + and run_aspell(path, root) + ) + + print(f"{path} neither a file nor a directory") + return False + + +def main(): + wamr_root = pathlib.Path(__file__).parent.joinpath("..").resolve() + + if not pre_flight_check(wamr_root): + return False + + invalid_file, invalid_directory = 0, 0 + + # in order to skip exclude directories ASAP, + # will not yield Path. + # since we will create every object + dirs = queue.Queue() + dirs.put(wamr_root) + while not dirs.empty(): + qsize = dirs.qsize() + while qsize: + current_dir = dirs.get() + + for path in current_dir.iterdir(): + path = path.resolve() + + if path.is_symlink(): + continue + + if path.is_dir() and not is_excluded(path): + invalid_directory += ( + 0 if run_pre_commit_check(path, wamr_root) else 1 + ) + dirs.put(path) + + if not path.is_file(): + continue + + invalid_file += 0 if run_pre_commit_check(path) else 1 + + else: + qsize -= 1 + + print(f"invalid_directory={invalid_directory}, invalid_file={invalid_file}") + return True + + +if __name__ == "__main__": + main() From 89c9e042789abae6f04fbd39a32dc482498b08bb Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 2 Oct 2021 16:56:48 +0800 Subject: [PATCH 4/6] Disable sort inclue files in .clang-format --- .clang-format | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-format b/.clang-format index 38fcef4ba3..0d945dd697 100644 --- a/.clang-format +++ b/.clang-format @@ -58,3 +58,4 @@ AllowAllParametersOfDeclarationOnNextLine: false AllowAllArgumentsOnNextLine: false PointerAlignment: Right SpaceAroundPointerQualifiers: After +SortIncludes: false From 5f588e8108cbafbef90206ad58e23f96f4f4d462 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 6 Oct 2021 17:54:16 +0800 Subject: [PATCH 5/6] Apply clang-format for interpreter source files --- core/iwasm/common/wasm_runtime_common.h | 1 - core/iwasm/interpreter/wasm.h | 25 +- core/iwasm/interpreter/wasm_interp.h | 62 +- core/iwasm/interpreter/wasm_interp_classic.c | 5690 +++++++++--------- core/iwasm/interpreter/wasm_interp_fast.c | 5580 +++++++++-------- core/iwasm/interpreter/wasm_loader.c | 2782 +++++---- core/iwasm/interpreter/wasm_loader.h | 20 +- core/iwasm/interpreter/wasm_mini_loader.c | 2463 ++++---- core/iwasm/interpreter/wasm_opcode.h | 1481 +++-- core/iwasm/interpreter/wasm_runtime.c | 699 +-- core/iwasm/interpreter/wasm_runtime.h | 83 +- doc/source_debugging.md | 18 +- 12 files changed, 9715 insertions(+), 9189 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index baaf3b9dab..cd13a2a09d 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -57,7 +57,6 @@ extern "C" { #define STORE_PTR(addr, ptr) do { \ *(void**)addr = (void*)ptr; \ } while (0) -#define LOAD_PTR(addr) (*(void**)(addr)) #else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */ diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 5bdf65c63d..4402b92e41 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -24,7 +24,7 @@ extern "C" { #define VALUE_TYPE_EXTERNREF 0x6F #define VALUE_TYPE_VOID 0x40 /* Used by AOT */ -#define VALUE_TYPE_I1 0x41 +#define VALUE_TYPE_I1 0x41 /* Used by loader to represent any type of i32/i64/f32/f64 */ #define VALUE_TYPE_ANY 0x42 @@ -66,8 +66,8 @@ extern "C" { #endif #define SUB_SECTION_TYPE_MODULE 0 -#define SUB_SECTION_TYPE_FUNC 1 -#define SUB_SECTION_TYPE_LOCAL 2 +#define SUB_SECTION_TYPE_FUNC 1 +#define SUB_SECTION_TYPE_LOCAL 2 #define IMPORT_KIND_FUNC 0 #define IMPORT_KIND_TABLE 1 @@ -449,7 +449,7 @@ typedef struct WASMBranchBlock { * @return the aligned value */ inline static unsigned -align_uint (unsigned v, unsigned b) +align_uint(unsigned v, unsigned b) { unsigned m = b - 1; return (v + m) & ~m; @@ -462,7 +462,7 @@ inline static uint32 wasm_string_hash(const char *str) { unsigned h = (unsigned)strlen(str); - const uint8 *p = (uint8*)str; + const uint8 *p = (uint8 *)str; const uint8 *end = p + h; while (p != end) @@ -547,9 +547,10 @@ wasm_type_equal(const WASMType *type1, const WASMType *type2) return (type1->param_count == type2->param_count && type1->result_count == type2->result_count && memcmp(type1->types, type2->types, - (uint32)(type1->param_count - + type1->result_count)) == 0) - ? true : false; + (uint32)(type1->param_count + type1->result_count)) + == 0) + ? true + : false; } inline static uint32 @@ -566,8 +567,7 @@ wasm_get_smallest_type_idx(WASMType **types, uint32 type_count, } static inline uint32 -block_type_get_param_types(BlockType *block_type, - uint8 **p_param_types) +block_type_get_param_types(BlockType *block_type, uint8 **p_param_types) { uint32 param_count = 0; if (!block_type->is_value_type) { @@ -577,15 +577,14 @@ block_type_get_param_types(BlockType *block_type, } else { *p_param_types = NULL; - param_count = 0; + param_count = 0; } return param_count; } static inline uint32 -block_type_get_result_types(BlockType *block_type, - uint8 **p_result_types) +block_type_get_result_types(BlockType *block_type, uint8 **p_result_types) { uint32 result_count = 0; if (block_type->is_value_type) { diff --git a/core/iwasm/interpreter/wasm_interp.h b/core/iwasm/interpreter/wasm_interp.h index a0d56f3287..4ac36edae6 100644 --- a/core/iwasm/interpreter/wasm_interp.h +++ b/core/iwasm/interpreter/wasm_interp.h @@ -17,43 +17,43 @@ struct WASMFunctionInstance; struct WASMExecEnv; typedef struct WASMInterpFrame { - /* The frame of the caller that are calling the current function. */ - struct WASMInterpFrame *prev_frame; + /* The frame of the caller that are calling the current function. */ + struct WASMInterpFrame *prev_frame; - /* The current WASM function. */ - struct WASMFunctionInstance *function; + /* The current WASM function. */ + struct WASMFunctionInstance *function; - /* Instruction pointer of the bytecode array. */ - uint8 *ip; + /* Instruction pointer of the bytecode array. */ + uint8 *ip; #if WASM_ENABLE_PERF_PROFILING != 0 - uint64 time_started; + uint64 time_started; #endif #if WASM_ENABLE_FAST_INTERP != 0 - /* return offset of the first return value of current frame. - the callee will put return values here continuously */ - uint32 ret_offset; - uint32 *lp; - uint32 operand[1]; + /* Return offset of the first return value of current frame, + the callee will put return values here continuously */ + uint32 ret_offset; + uint32 *lp; + uint32 operand[1]; #else - /* Operand stack top pointer of the current frame. The bottom of - the stack is the next cell after the last local variable. */ - uint32 *sp_bottom; - uint32 *sp_boundary; - uint32 *sp; + /* Operand stack top pointer of the current frame. The bottom of + the stack is the next cell after the last local variable. */ + uint32 *sp_bottom; + uint32 *sp_boundary; + uint32 *sp; - WASMBranchBlock *csp_bottom; - WASMBranchBlock *csp_boundary; - WASMBranchBlock *csp; + WASMBranchBlock *csp_bottom; + WASMBranchBlock *csp_boundary; + WASMBranchBlock *csp; - /* Frame data, the layout is: - lp: param_cell_count + local_cell_count - sp_bottom to sp_boundary: stack of data - csp_bottom to csp_boundary: stack of block - ref to frame end: data types of local vairables and stack data - */ - uint32 lp[1]; + /* Frame data, the layout is: + lp: param_cell_count + local_cell_count + sp_bottom to sp_boundary: stack of data + csp_bottom to csp_boundary: stack of block + ref to frame end: data types of local vairables and stack data + */ + uint32 lp[1]; #endif } WASMInterpFrame; @@ -68,15 +68,15 @@ typedef struct WASMInterpFrame { static inline unsigned wasm_interp_interp_frame_size(unsigned all_cell_num) { - return align_uint((uint32)offsetof(WASMInterpFrame, lp) - + all_cell_num * 5, 4); + return align_uint((uint32)offsetof(WASMInterpFrame, lp) + all_cell_num * 5, + 4); } void wasm_interp_call_wasm(struct WASMModuleInstance *module_inst, struct WASMExecEnv *exec_env, - struct WASMFunctionInstance *function, - uint32 argc, uint32 argv[]); + struct WASMFunctionInstance *function, uint32 argc, + uint32 argv[]); #ifdef __cplusplus } diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 1f9ddf3fb0..a4e48510af 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -23,29 +23,33 @@ typedef float64 CellType_F64; #define BR_TABLE_TMP_BUF_LEN 32 -#define CHECK_MEMORY_OVERFLOW(bytes) do { \ - uint64 offset1 = (uint64)offset + (uint64)addr; \ - if (offset1 + bytes <= (uint64)linear_mem_size) \ - /* If offset1 is in valid range, maddr must also be in valid range, \ - no need to check it again. */ \ - maddr = memory->memory_data + offset1; \ - else \ - goto out_of_bounds; \ - } while (0) - -#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) do { \ - uint64 offset1 = (uint32)(start); \ - if (offset1 + bytes <= (uint64)linear_mem_size) \ - /* App heap space is not valid space for bulk memory operation */ \ - maddr = memory->memory_data + offset1; \ - else \ - goto out_of_bounds; \ - } while (0) - -#define CHECK_ATOMIC_MEMORY_ACCESS() do { \ - if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0)\ - goto unaligned_atomic; \ - } while (0) +#define CHECK_MEMORY_OVERFLOW(bytes) \ + do { \ + uint64 offset1 = (uint64)offset + (uint64)addr; \ + if (offset1 + bytes <= (uint64)linear_mem_size) \ + /* If offset1 is in valid range, maddr must also \ + be in valid range, no need to check it again. */ \ + maddr = memory->memory_data + offset1; \ + else \ + goto out_of_bounds; \ + } while (0) + +#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \ + do { \ + uint64 offset1 = (uint32)(start); \ + if (offset1 + bytes <= (uint64)linear_mem_size) \ + /* App heap space is not valid space for \ + bulk memory operation */ \ + maddr = memory->memory_data + offset1; \ + else \ + goto out_of_bounds; \ + } while (0) + +#define CHECK_ATOMIC_MEMORY_ACCESS() \ + do { \ + if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0) \ + goto unaligned_atomic; \ + } while (0) static inline uint32 rotl32(uint32 n, uint32 c) @@ -87,7 +91,7 @@ static inline double wa_fmax(double a, double b) { double c = fmax(a, b); - if (c==0 && a==b) + if (c == 0 && a == b) return signbit(a) ? b : a; return c; } @@ -96,7 +100,7 @@ static inline double wa_fmin(double a, double b) { double c = fmin(a, b); - if (c==0 && a==b) + if (c == 0 && a == b) return signbit(a) ? a : b; return c; } @@ -200,141 +204,155 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) #define skip_leb(p) while (*p++ & 0x80) -#define PUSH_I32(value) do { \ - *(int32*)frame_sp++ = (int32)(value); \ - } while (0) +#define PUSH_I32(value) \ + do { \ + *(int32 *)frame_sp++ = (int32)(value); \ + } while (0) -#define PUSH_F32(value) do { \ - *(float32*)frame_sp++ = (float32)(value); \ - } while (0) +#define PUSH_F32(value) \ + do { \ + *(float32 *)frame_sp++ = (float32)(value); \ + } while (0) -#define PUSH_I64(value) do { \ - PUT_I64_TO_ADDR(frame_sp, value); \ - frame_sp += 2; \ - } while (0) +#define PUSH_I64(value) \ + do { \ + PUT_I64_TO_ADDR(frame_sp, value); \ + frame_sp += 2; \ + } while (0) -#define PUSH_F64(value) do { \ - PUT_F64_TO_ADDR(frame_sp, value); \ - frame_sp += 2; \ - } while (0) +#define PUSH_F64(value) \ + do { \ + PUT_F64_TO_ADDR(frame_sp, value); \ + frame_sp += 2; \ + } while (0) -#define PUSH_CSP(_label_type, cell_num, _target_addr) do { \ - bh_assert(frame_csp < frame->csp_boundary); \ - /* frame_csp->label_type = _label_type; */ \ - frame_csp->cell_num = cell_num; \ - frame_csp->begin_addr = frame_ip; \ - frame_csp->target_addr = _target_addr; \ - frame_csp->frame_sp = frame_sp; \ - frame_csp++; \ - } while (0) +#define PUSH_CSP(_label_type, cell_num, _target_addr) \ + do { \ + bh_assert(frame_csp < frame->csp_boundary); \ + /* frame_csp->label_type = _label_type; */ \ + frame_csp->cell_num = cell_num; \ + frame_csp->begin_addr = frame_ip; \ + frame_csp->target_addr = _target_addr; \ + frame_csp->frame_sp = frame_sp; \ + frame_csp++; \ + } while (0) -#define POP_I32() (--frame_sp, *(int32*)frame_sp) +#define POP_I32() (--frame_sp, *(int32 *)frame_sp) -#define POP_F32() (--frame_sp, *(float32*)frame_sp) +#define POP_F32() (--frame_sp, *(float32 *)frame_sp) #define POP_I64() (frame_sp -= 2, GET_I64_FROM_ADDR(frame_sp)) #define POP_F64() (frame_sp -= 2, GET_F64_FROM_ADDR(frame_sp)) -#define POP_CSP_CHECK_OVERFLOW(n) do { \ - bh_assert(frame_csp - n >= frame->csp_bottom); \ - } while (0) - -#define POP_CSP() do { \ - POP_CSP_CHECK_OVERFLOW(1); \ - --frame_csp; \ - } while (0) - -#define POP_CSP_N(n) do { \ - uint32 *frame_sp_old = frame_sp; \ - uint32 cell_num = 0; \ - POP_CSP_CHECK_OVERFLOW(n + 1); \ - frame_csp -= n; \ - frame_ip = (frame_csp - 1)->target_addr; \ - /* copy arity values of block */ \ - frame_sp = (frame_csp - 1)->frame_sp; \ - cell_num = (frame_csp - 1)->cell_num; \ - word_copy(frame_sp, frame_sp_old - cell_num, cell_num); \ - frame_sp += cell_num; \ - } while (0) +#define POP_CSP_CHECK_OVERFLOW(n) \ + do { \ + bh_assert(frame_csp - n >= frame->csp_bottom); \ + } while (0) + +#define POP_CSP() \ + do { \ + POP_CSP_CHECK_OVERFLOW(1); \ + --frame_csp; \ + } while (0) + +#define POP_CSP_N(n) \ + do { \ + uint32 *frame_sp_old = frame_sp; \ + uint32 cell_num = 0; \ + POP_CSP_CHECK_OVERFLOW(n + 1); \ + frame_csp -= n; \ + frame_ip = (frame_csp - 1)->target_addr; \ + /* copy arity values of block */ \ + frame_sp = (frame_csp - 1)->frame_sp; \ + cell_num = (frame_csp - 1)->cell_num; \ + word_copy(frame_sp, frame_sp_old - cell_num, cell_num); \ + frame_sp += cell_num; \ + } while (0) /* Pop the given number of elements from the given frame's stack. */ -#define POP(N) do { \ - int n = (N); \ - frame_sp -= n; \ - } while (0) - -#define SYNC_ALL_TO_FRAME() do { \ - frame->sp = frame_sp; \ - frame->ip = frame_ip; \ - frame->csp = frame_csp; \ - } while (0) - -#define UPDATE_ALL_FROM_FRAME() do { \ - frame_sp = frame->sp; \ - frame_ip = frame->ip; \ - frame_csp = frame->csp; \ - } while (0) - -#define read_leb_int64(p, p_end, res) do { \ - uint8 _val = *p; \ - if (!(_val & 0x80)) { \ - res = (int64)_val; \ - if (_val & 0x40) \ - /* sign extend */ \ - res |= 0xFFFFFFFFFFFFFF80LL; \ - p++; \ - break; \ - } \ - uint32 _off = 0; \ - res = (int64)read_leb(p, &_off, 64, true); \ - p += _off; \ -} while (0) - -#define read_leb_uint32(p, p_end, res) do { \ - uint8 _val = *p; \ - if (!(_val & 0x80)) { \ - res = _val; \ - p++; \ - break; \ - } \ - uint32 _off = 0; \ - res = (uint32)read_leb(p, &_off, 32, false); \ - p += _off; \ -} while (0) - -#define read_leb_int32(p, p_end, res) do { \ - uint8 _val = *p; \ - if (!(_val & 0x80)) { \ - res = (int32)_val; \ - if (_val & 0x40) \ - /* sign extend */ \ - res |= 0xFFFFFF80; \ - p++; \ - break; \ - } \ - uint32 _off = 0; \ - res = (int32)read_leb(p, &_off, 32, true); \ - p += _off; \ -} while (0) +#define POP(N) \ + do { \ + int n = (N); \ + frame_sp -= n; \ + } while (0) + +#define SYNC_ALL_TO_FRAME() \ + do { \ + frame->sp = frame_sp; \ + frame->ip = frame_ip; \ + frame->csp = frame_csp; \ + } while (0) + +#define UPDATE_ALL_FROM_FRAME() \ + do { \ + frame_sp = frame->sp; \ + frame_ip = frame->ip; \ + frame_csp = frame->csp; \ + } while (0) + +#define read_leb_int64(p, p_end, res) \ + do { \ + uint8 _val = *p; \ + if (!(_val & 0x80)) { \ + res = (int64)_val; \ + if (_val & 0x40) \ + /* sign extend */ \ + res |= 0xFFFFFFFFFFFFFF80LL; \ + p++; \ + break; \ + } \ + uint32 _off = 0; \ + res = (int64)read_leb(p, &_off, 64, true); \ + p += _off; \ + } while (0) + +#define read_leb_uint32(p, p_end, res) \ + do { \ + uint8 _val = *p; \ + if (!(_val & 0x80)) { \ + res = _val; \ + p++; \ + break; \ + } \ + uint32 _off = 0; \ + res = (uint32)read_leb(p, &_off, 32, false); \ + p += _off; \ + } while (0) + +#define read_leb_int32(p, p_end, res) \ + do { \ + uint8 _val = *p; \ + if (!(_val & 0x80)) { \ + res = (int32)_val; \ + if (_val & 0x40) \ + /* sign extend */ \ + res |= 0xFFFFFF80; \ + p++; \ + break; \ + } \ + uint32 _off = 0; \ + res = (int32)read_leb(p, &_off, 32, true); \ + p += _off; \ + } while (0) #if WASM_ENABLE_LABELS_AS_VALUES == 0 -#define RECOVER_FRAME_IP_END() \ - frame_ip_end = wasm_get_func_code_end(cur_func) +#define RECOVER_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func) #else #define RECOVER_FRAME_IP_END() (void)0 #endif -#define RECOVER_CONTEXT(new_frame) do { \ - frame = (new_frame); \ - cur_func = frame->function; \ - prev_frame = frame->prev_frame; \ - frame_ip = frame->ip; \ - RECOVER_FRAME_IP_END(); \ - frame_lp = frame->lp; \ - frame_sp = frame->sp; \ - frame_csp = frame->csp; \ - } while (0) +#define RECOVER_CONTEXT(new_frame) \ + do { \ + frame = (new_frame); \ + cur_func = frame->function; \ + prev_frame = frame->prev_frame; \ + frame_ip = frame->ip; \ + RECOVER_FRAME_IP_END(); \ + frame_lp = frame->lp; \ + frame_sp = frame->sp; \ + frame_csp = frame->csp; \ + } while (0) #if WASM_ENABLE_LABELS_AS_VALUES != 0 #define GET_OPCODE() opcode = *(frame_ip - 1); @@ -342,96 +360,105 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) #define GET_OPCODE() (void)0 #endif -#define DEF_OP_I_CONST(ctype, src_op_type) do { \ - ctype cval; \ - read_leb_##ctype(frame_ip, frame_ip_end, cval); \ - PUSH_##src_op_type(cval); \ - } while (0) - -#define DEF_OP_EQZ(src_op_type) do { \ - int32 val; \ - val = POP_##src_op_type() == 0; \ - PUSH_I32(val); \ - } while (0) - -#define DEF_OP_CMP(src_type, src_op_type, cond) do { \ - uint32 res; \ - src_type val1, val2; \ - val2 = (src_type)POP_##src_op_type(); \ - val1 = (src_type)POP_##src_op_type(); \ - res = val1 cond val2; \ - PUSH_I32(res); \ - } while (0) - -#define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) do { \ - src_type val1, val2; \ - val1 = (src_type)POP_##src_op_type(); \ - val2 = (src_type)operation(val1); \ - PUSH_##src_op_type(val2); \ - } while (0) - -#define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) do { \ - frame_sp -= sizeof(src_type2)/sizeof(uint32); \ - *(src_type1*)(frame_sp - sizeof(src_type1)/sizeof(uint32)) operation##= \ - *(src_type2*)(frame_sp); \ - } while (0) +#define DEF_OP_I_CONST(ctype, src_op_type) \ + do { \ + ctype cval; \ + read_leb_##ctype(frame_ip, frame_ip_end, cval); \ + PUSH_##src_op_type(cval); \ + } while (0) + +#define DEF_OP_EQZ(src_op_type) \ + do { \ + int32 val; \ + val = POP_##src_op_type() == 0; \ + PUSH_I32(val); \ + } while (0) + +#define DEF_OP_CMP(src_type, src_op_type, cond) \ + do { \ + uint32 res; \ + src_type val1, val2; \ + val2 = (src_type)POP_##src_op_type(); \ + val1 = (src_type)POP_##src_op_type(); \ + res = val1 cond val2; \ + PUSH_I32(res); \ + } while (0) + +#define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \ + do { \ + src_type val1, val2; \ + val1 = (src_type)POP_##src_op_type(); \ + val2 = (src_type)operation(val1); \ + PUSH_##src_op_type(val2); \ + } while (0) + +#define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \ + do { \ + frame_sp -= sizeof(src_type2) / sizeof(uint32); \ + *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \ + operation## = *(src_type2 *)(frame_sp); \ + } while (0) #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC #else -#define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) do {\ - src_type1 val1; \ - src_type2 val2; \ - frame_sp -= 2; \ - val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \ - val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \ - val1 operation##= val2; \ - PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \ - } while (0) +#define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) \ + do { \ + src_type1 val1; \ + src_type2 val2; \ + frame_sp -= 2; \ + val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \ + val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \ + val1 operation## = val2; \ + PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \ + } while (0) #endif -#define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) do { \ - frame_sp -= sizeof(src_type2)/sizeof(uint32); \ - *(src_type1*)(frame_sp - sizeof(src_type1)/sizeof(uint32)) operation##= \ - (*(src_type2*)(frame_sp) % 32); \ - } while (0) - -#define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) do { \ - src_type1 val1; \ - src_type2 val2; \ - frame_sp -= 2; \ - val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \ - val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \ - val1 operation##= (val2 % 64); \ - PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \ - } while (0) - -#define DEF_OP_MATH(src_type, src_op_type, method) do { \ - src_type val; \ - val = POP_##src_op_type(); \ - PUSH_##src_op_type(method(val)); \ - } while (0) +#define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \ + do { \ + frame_sp -= sizeof(src_type2) / sizeof(uint32); \ + *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \ + operation## = (*(src_type2 *)(frame_sp) % 32); \ + } while (0) + +#define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \ + do { \ + src_type1 val1; \ + src_type2 val2; \ + frame_sp -= 2; \ + val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \ + val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \ + val1 operation## = (val2 % 64); \ + PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \ + } while (0) + +#define DEF_OP_MATH(src_type, src_op_type, method) \ + do { \ + src_type val; \ + val = POP_##src_op_type(); \ + PUSH_##src_op_type(method(val)); \ + } while (0) #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \ -static dst_type \ -func_name(src_type src_value, src_type src_min, src_type src_max, \ - dst_type dst_min, dst_type dst_max, bool is_sign) \ -{ \ - dst_type dst_value = 0; \ - if (!isnan(src_value)) { \ - if (src_value <= src_min) \ - dst_value = dst_min; \ - else if (src_value >= src_max) \ - dst_value = dst_max; \ - else { \ - if (is_sign) \ - dst_value = (dst_type)(signed_type)src_value; \ - else \ - dst_value = (dst_type)src_value; \ - } \ - } \ - return dst_value; \ -} + static dst_type func_name(src_type src_value, src_type src_min, \ + src_type src_max, dst_type dst_min, \ + dst_type dst_max, bool is_sign) \ + { \ + dst_type dst_value = 0; \ + if (!isnan(src_value)) { \ + if (src_value <= src_min) \ + dst_value = dst_min; \ + else if (src_value >= src_max) \ + dst_value = dst_max; \ + else { \ + if (is_sign) \ + dst_value = (dst_type)(signed_type)src_value; \ + else \ + dst_value = (dst_type)src_value; \ + } \ + } \ + return dst_value; \ + } TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32) TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64) @@ -439,10 +466,8 @@ TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32) TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64) static bool -trunc_f32_to_int(WASMModuleInstance *module, - uint32 *frame_sp, - float32 src_min, float32 src_max, - bool saturating, bool is_i32, bool is_sign) +trunc_f32_to_int(WASMModuleInstance *module, uint32 *frame_sp, float32 src_min, + float32 src_max, bool saturating, bool is_i32, bool is_sign) { float32 src_value = POP_F32(); uint64 dst_value_i64; @@ -462,25 +487,23 @@ trunc_f32_to_int(WASMModuleInstance *module, if (is_i32) { uint32 dst_min = is_sign ? INT32_MIN : 0; uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX; - dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min, + dst_max, is_sign); PUSH_I32(dst_value_i32); } else { uint64 dst_min = is_sign ? INT64_MIN : 0; uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX; - dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min, + dst_max, is_sign); PUSH_I64(dst_value_i64); } return false; } static bool -trunc_f64_to_int(WASMModuleInstance *module, - uint32 *frame_sp, - float64 src_min, float64 src_max, - bool saturating, bool is_i32, bool is_sign) +trunc_f64_to_int(WASMModuleInstance *module, uint32 *frame_sp, float64 src_min, + float64 src_max, bool saturating, bool is_i32, bool is_sign) { float64 src_value = POP_F64(); uint64 dst_value_i64; @@ -500,150 +523,155 @@ trunc_f64_to_int(WASMModuleInstance *module, if (is_i32) { uint32 dst_min = is_sign ? INT32_MIN : 0; uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX; - dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min, + dst_max, is_sign); PUSH_I32(dst_value_i32); } else { uint64 dst_min = is_sign ? INT64_MIN : 0; uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX; - dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min, + dst_max, is_sign); PUSH_I64(dst_value_i64); } return false; } -#define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) do { \ - if (trunc_f32_to_int(module, frame_sp, min, max, \ - false, is_i32, is_sign)) \ - goto got_exception; \ - } while (0) - -#define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) do { \ - if (trunc_f64_to_int(module, frame_sp, min, max, \ - false, is_i32, is_sign)) \ - goto got_exception; \ - } while (0) - -#define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) do { \ - (void)trunc_f32_to_int(module, frame_sp, min, max, \ - true, is_i32, is_sign); \ - } while (0) - -#define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) do { \ - (void)trunc_f64_to_int(module, frame_sp, min, max, \ - true, is_i32, is_sign); \ - } while (0) - -#define DEF_OP_CONVERT(dst_type, dst_op_type, \ - src_type, src_op_type) do { \ - dst_type value = (dst_type)(src_type)POP_##src_op_type(); \ - PUSH_##dst_op_type(value); \ - } while (0) - -#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \ - uint32 param_count = cur_func->param_count; \ - read_leb_uint32(frame_ip, frame_ip_end, local_idx); \ - bh_assert(local_idx < param_count + cur_func->local_count); \ - local_offset = cur_func->local_offsets[local_idx]; \ - if (local_idx < param_count) \ - local_type = cur_func->param_types[local_idx]; \ - else \ - local_type = cur_func->local_types[local_idx - param_count]; \ - } while (0) - -#define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \ - case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \ - case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \ - case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \ - { \ - uint32 readv, sval; \ - \ - sval = POP_I32(); \ - addr = POP_I32(); \ - \ - if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint32)(*(uint8*)maddr); \ - *(uint8*)maddr = (uint8)(readv op sval); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint32)LOAD_U16(maddr); \ - STORE_U16(maddr, (uint16)(readv op sval)); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = LOAD_I32(maddr); \ - STORE_U32(maddr, readv op sval); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - PUSH_I32(readv); \ - break; \ - } \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \ - { \ - uint64 readv, sval; \ - \ - sval = (uint64)POP_I64(); \ - addr = POP_I32(); \ - \ - if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)(*(uint8*)maddr); \ - *(uint8*)maddr = (uint8)(readv op sval); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)LOAD_U16(maddr); \ - STORE_U16(maddr, (uint16)(readv op sval)); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)LOAD_U32(maddr); \ - STORE_U32(maddr, (uint32)(readv op sval)); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else { \ - uint64 op_result; \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)LOAD_I64(maddr); \ - op_result = readv op sval; \ - STORE_I64(maddr, op_result); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - PUSH_I64(readv); \ - break; \ - } +#define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \ + do { \ + if (trunc_f32_to_int(module, frame_sp, min, max, false, is_i32, \ + is_sign)) \ + goto got_exception; \ + } while (0) + +#define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \ + do { \ + if (trunc_f64_to_int(module, frame_sp, min, max, false, is_i32, \ + is_sign)) \ + goto got_exception; \ + } while (0) + +#define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \ + do { \ + (void)trunc_f32_to_int(module, frame_sp, min, max, true, is_i32, \ + is_sign); \ + } while (0) + +#define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \ + do { \ + (void)trunc_f64_to_int(module, frame_sp, min, max, true, is_i32, \ + is_sign); \ + } while (0) + +#define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \ + do { \ + dst_type value = (dst_type)(src_type)POP_##src_op_type(); \ + PUSH_##dst_op_type(value); \ + } while (0) + +#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \ + do { \ + uint32 param_count = cur_func->param_count; \ + read_leb_uint32(frame_ip, frame_ip_end, local_idx); \ + bh_assert(local_idx < param_count + cur_func->local_count); \ + local_offset = cur_func->local_offsets[local_idx]; \ + if (local_idx < param_count) \ + local_type = cur_func->param_types[local_idx]; \ + else \ + local_type = cur_func->local_types[local_idx - param_count]; \ + } while (0) + +#define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \ + case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \ + case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \ + case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \ + { \ + uint32 readv, sval; \ + \ + sval = POP_I32(); \ + addr = POP_I32(); \ + \ + if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint32)(*(uint8 *)maddr); \ + *(uint8 *)maddr = (uint8)(readv op sval); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint32)LOAD_U16(maddr); \ + STORE_U16(maddr, (uint16)(readv op sval)); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = LOAD_I32(maddr); \ + STORE_U32(maddr, readv op sval); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + PUSH_I32(readv); \ + break; \ + } \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \ + { \ + uint64 readv, sval; \ + \ + sval = (uint64)POP_I64(); \ + addr = POP_I32(); \ + \ + if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)(*(uint8 *)maddr); \ + *(uint8 *)maddr = (uint8)(readv op sval); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)LOAD_U16(maddr); \ + STORE_U16(maddr, (uint16)(readv op sval)); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)LOAD_U32(maddr); \ + STORE_U32(maddr, (uint32)(readv op sval)); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else { \ + uint64 op_result; \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)LOAD_I64(maddr); \ + op_result = readv op sval; \ + STORE_I64(maddr, op_result); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + PUSH_I64(readv); \ + break; \ + } static inline int32 sign_ext_8_32(int8 val) @@ -692,7 +720,7 @@ word_copy(uint32 *dest, uint32 *src, unsigned num) *dest++ = *src++; } -static inline WASMInterpFrame* +static inline WASMInterpFrame * ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) { WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size); @@ -704,7 +732,7 @@ ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) #endif } else { - wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, + wasm_set_exception((WASMModuleInstance *)exec_env->module_inst, "wasm operand stack overflow"); } @@ -716,8 +744,8 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame) { #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { - frame->function->total_exec_time += os_time_get_boot_microsecond() - - frame->time_started; + frame->function->total_exec_time += + os_time_get_boot_microsecond() - frame->time_started; frame->function->total_exec_cnt++; } #endif @@ -758,26 +786,26 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, if (func_import->call_conv_wasm_c_api) { ret = wasm_runtime_invoke_c_api_native( - (WASMModuleInstanceCommon *)module_inst, - func_import->func_ptr_linked, func_import->func_type, - cur_func->param_cell_num, frame->lp, - func_import->wasm_c_api_with_env, func_import->attachment); + (WASMModuleInstanceCommon *)module_inst, + func_import->func_ptr_linked, func_import->func_type, + cur_func->param_cell_num, frame->lp, + func_import->wasm_c_api_with_env, func_import->attachment); if (ret) { argv_ret[0] = frame->lp[0]; argv_ret[1] = frame->lp[1]; } } else if (!func_import->call_conv_raw) { - ret = wasm_runtime_invoke_native(exec_env, func_import->func_ptr_linked, - func_import->func_type, func_import->signature, - func_import->attachment, - frame->lp, cur_func->param_cell_num, argv_ret); + ret = wasm_runtime_invoke_native( + exec_env, func_import->func_ptr_linked, func_import->func_type, + func_import->signature, func_import->attachment, frame->lp, + cur_func->param_cell_num, argv_ret); } else { - ret = wasm_runtime_invoke_native_raw(exec_env, func_import->func_ptr_linked, - func_import->func_type, func_import->signature, - func_import->attachment, - frame->lp, cur_func->param_cell_num, argv_ret); + ret = wasm_runtime_invoke_native_raw( + exec_env, func_import->func_ptr_linked, func_import->func_type, + func_import->signature, func_import->attachment, frame->lp, + cur_func->param_cell_num, argv_ret); } if (!ret) @@ -833,8 +861,8 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst, exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst; /* call function of sub-module*/ - wasm_interp_call_func_bytecode(sub_module_inst, exec_env, - sub_func_inst, prev_frame); + wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst, + prev_frame); /* restore ip and module_inst */ prev_frame->ip = ip; @@ -852,71 +880,85 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst, #if WASM_ENABLE_THREAD_MGR != 0 #if WASM_ENABLE_DEBUG_INTERP != 0 -#define CHECK_SUSPEND_FLAGS() do { \ - if (IS_WAMR_TERM_SIG(exec_env->current_status->signal_flag)) { \ - return; \ - } \ - if (IS_WAMR_STOP_SIG(exec_env->current_status->signal_flag)) { \ - SYNC_ALL_TO_FRAME(); \ - wasm_cluster_thread_stopped(exec_env); \ - wasm_cluster_thread_waiting_run(exec_env); \ - } \ - } while (0) +#define CHECK_SUSPEND_FLAGS() \ + do { \ + if (IS_WAMR_TERM_SIG(exec_env->current_status->signal_flag)) { \ + return; \ + } \ + if (IS_WAMR_STOP_SIG(exec_env->current_status->signal_flag)) { \ + SYNC_ALL_TO_FRAME(); \ + wasm_cluster_thread_stopped(exec_env); \ + wasm_cluster_thread_waiting_run(exec_env); \ + } \ + } while (0) #else -#define CHECK_SUSPEND_FLAGS() do { \ - if (exec_env->suspend_flags.flags != 0) { \ - if (exec_env->suspend_flags.flags & 0x01) { \ - /* terminate current thread */ \ - return; \ - } \ - while (exec_env->suspend_flags.flags & 0x02){ \ - /* suspend current thread */ \ - os_cond_wait(&exec_env->wait_cond, \ - &exec_env->wait_lock); \ - } \ - } \ - } while (0) -#endif -#endif +#define CHECK_SUSPEND_FLAGS() \ + do { \ + if (exec_env->suspend_flags.flags != 0) { \ + if (exec_env->suspend_flags.flags & 0x01) { \ + /* terminate current thread */ \ + return; \ + } \ + while (exec_env->suspend_flags.flags & 0x02) { \ + /* suspend current thread */ \ + os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock); \ + } \ + } \ + } while (0) +#endif /* WASM_ENABLE_DEBUG_INTERP */ +#endif /* WASM_ENABLE_THREAD_MGR */ #if WASM_ENABLE_LABELS_AS_VALUES != 0 -#define HANDLE_OP(opcode) HANDLE_##opcode +#define HANDLE_OP(opcode) HANDLE_##opcode: #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++] #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0 -#define HANDLE_OP_END() \ - do { \ - while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \ - && exec_env->current_status->step_count++ == 1) { \ - exec_env->current_status->step_count = 0; \ - SYNC_ALL_TO_FRAME(); \ - wasm_cluster_thread_stopped(exec_env); \ - wasm_cluster_thread_waiting_run(exec_env); \ - } \ - goto *handle_table[*frame_ip++]; \ +#define HANDLE_OP_END() \ + do { \ + while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \ + && exec_env->current_status->step_count++ == 1) { \ + exec_env->current_status->step_count = 0; \ + SYNC_ALL_TO_FRAME(); \ + wasm_cluster_thread_stopped(exec_env); \ + wasm_cluster_thread_waiting_run(exec_env); \ + } \ + goto *handle_table[*frame_ip++]; \ } while (0) #else #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH() #endif -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ -#define HANDLE_OP(opcode) case opcode +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#define HANDLE_OP(opcode) case opcode: #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0 -#define HANDLE_OP_END() \ - if (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \ - && exec_env->current_status->step_count++ == 2) { \ - exec_env->current_status->step_count = 0; \ - SYNC_ALL_TO_FRAME(); \ - wasm_cluster_thread_stopped(exec_env); \ - wasm_cluster_thread_waiting_run(exec_env); \ - } \ +#define HANDLE_OP_END() \ + if (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \ + && exec_env->current_status->step_count++ == 2) { \ + exec_env->current_status->step_count = 0; \ + SYNC_ALL_TO_FRAME(); \ + wasm_cluster_thread_stopped(exec_env); \ + wasm_cluster_thread_waiting_run(exec_env); \ + } \ continue #else #define HANDLE_OP_END() continue #endif -#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ +#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ + +static inline uint8 * +get_global_addr(uint8 *global_data, WASMGlobalInstance *global) +{ +#if WASM_ENABLE_MULTI_MODULE == 0 + return global_data + global->data_offset; +#else + return global->import_global_inst + ? global->import_module_inst->global_data + + global->import_global_inst->data_offset + : global_data + global->data_offset; +#endif +} static void wasm_interp_call_func_bytecode(WASMModuleInstance *module, @@ -924,2542 +966,2764 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, WASMFunctionInstance *cur_func, WASMInterpFrame *prev_frame) { - WASMMemoryInstance *memory = module->default_memory; - uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0; - uint8 *global_data = module->global_data; - uint32 linear_mem_size = memory ? num_bytes_per_page * memory->cur_page_count : 0; - WASMType **wasm_types = module->module->types; - WASMGlobalInstance *globals = module->globals, *global; - uint8 opcode_IMPDEP = WASM_OP_IMPDEP; - WASMInterpFrame *frame = NULL; - /* Points to this special opcode so as to jump to the call_method_from_entry. */ - register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */ - register uint32 *frame_lp = NULL; /* cache of frame->lp */ - register uint32 *frame_sp = NULL; /* cache of frame->sp */ - WASMBranchBlock *frame_csp = NULL; - BlockAddr *cache_items; - uint8 *frame_ip_end = frame_ip + 1; - uint8 opcode; - uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0; - uint64 all_cell_num = 0; - int32 val; - uint8 *else_addr, *end_addr, *maddr = NULL; - uint32 local_idx, local_offset, global_idx; - uint8 local_type, *global_addr; - uint32 cache_index, type_index, cell_num; - uint8 value_type; + WASMMemoryInstance *memory = module->default_memory; + uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0; + uint8 *global_data = module->global_data; + uint32 linear_mem_size = + memory ? num_bytes_per_page * memory->cur_page_count : 0; + WASMType **wasm_types = module->module->types; + WASMGlobalInstance *globals = module->globals, *global; + uint8 opcode_IMPDEP = WASM_OP_IMPDEP; + WASMInterpFrame *frame = NULL; + /* Points to this special opcode so as to jump to the + * call_method_from_entry. */ + register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */ + register uint32 *frame_lp = NULL; /* cache of frame->lp */ + register uint32 *frame_sp = NULL; /* cache of frame->sp */ + WASMBranchBlock *frame_csp = NULL; + BlockAddr *cache_items; + uint8 *frame_ip_end = frame_ip + 1; + uint8 opcode; + uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0; + uint64 all_cell_num = 0; + int32 val; + uint8 *else_addr, *end_addr, *maddr = NULL; + uint32 local_idx, local_offset, global_idx; + uint8 local_type, *global_addr; + uint32 cache_index, type_index, cell_num; + uint8 value_type; #if WASM_ENABLE_LABELS_AS_VALUES != 0 - #define HANDLE_OPCODE(op) &&HANDLE_##op - DEFINE_GOTO_TABLE (const void *, handle_table); - #undef HANDLE_OPCODE +#define HANDLE_OPCODE(op) &&HANDLE_##op + DEFINE_GOTO_TABLE(const void *, handle_table); +#undef HANDLE_OPCODE #endif #if WASM_ENABLE_LABELS_AS_VALUES == 0 - while (frame_ip < frame_ip_end) { - opcode = *frame_ip++; - switch (opcode) { + while (frame_ip < frame_ip_end) { + opcode = *frame_ip++; + switch (opcode) { #else - FETCH_OPCODE_AND_DISPATCH (); + FETCH_OPCODE_AND_DISPATCH(); #endif - /* control instructions */ - HANDLE_OP (WASM_OP_UNREACHABLE): - wasm_set_exception(module, "unreachable"); - goto got_exception; + /* control instructions */ + HANDLE_OP(WASM_OP_UNREACHABLE) + { + wasm_set_exception(module, "unreachable"); + goto got_exception; + } - HANDLE_OP (WASM_OP_NOP): - HANDLE_OP_END (); - - HANDLE_OP (EXT_OP_BLOCK): - read_leb_uint32(frame_ip, frame_ip_end, type_index); - cell_num = wasm_types[type_index]->ret_cell_num; - goto handle_op_block; - - HANDLE_OP (WASM_OP_BLOCK): - value_type = *frame_ip++; - cell_num = wasm_value_type_cell_num(value_type); -handle_op_block: - cache_index = ((uintptr_t)frame_ip) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); - cache_items = exec_env->block_addr_cache[cache_index]; - if (cache_items[0].start_addr == frame_ip) { - end_addr = cache_items[0].end_addr; - } - else if (cache_items[1].start_addr == frame_ip) { - end_addr = cache_items[1].end_addr; - } + HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); } + + HANDLE_OP(EXT_OP_BLOCK) + { + read_leb_uint32(frame_ip, frame_ip_end, type_index); + cell_num = wasm_types[type_index]->ret_cell_num; + goto handle_op_block; + } + + HANDLE_OP(WASM_OP_BLOCK) + { + value_type = *frame_ip++; + cell_num = wasm_value_type_cell_num(value_type); + handle_op_block: + cache_index = ((uintptr_t)frame_ip) + & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); + cache_items = exec_env->block_addr_cache[cache_index]; + if (cache_items[0].start_addr == frame_ip) { + end_addr = cache_items[0].end_addr; + } + else if (cache_items[1].start_addr == frame_ip) { + end_addr = cache_items[1].end_addr; + } #if WASM_ENABLE_DEBUG_INTERP != 0 - else if (!wasm_loader_find_block_addr(exec_env, - (BlockAddr*)exec_env->block_addr_cache, - frame_ip, (uint8*)-1, - LABEL_TYPE_BLOCK, - &else_addr, &end_addr)) { - wasm_set_exception(module, "find block address failed"); - goto got_exception; - } + else if (!wasm_loader_find_block_addr( + exec_env, (BlockAddr *)exec_env->block_addr_cache, + frame_ip, (uint8 *)-1, LABEL_TYPE_BLOCK, + &else_addr, &end_addr)) { + wasm_set_exception(module, "find block address failed"); + goto got_exception; + } #endif - else { - end_addr = NULL; - } - PUSH_CSP(LABEL_TYPE_BLOCK, cell_num, end_addr); - HANDLE_OP_END (); - - HANDLE_OP (EXT_OP_LOOP): - read_leb_uint32(frame_ip, frame_ip_end, type_index); - cell_num = wasm_types[type_index]->param_cell_num; - goto handle_op_loop; - - HANDLE_OP (WASM_OP_LOOP): - value_type = *frame_ip++; - cell_num = wasm_value_type_cell_num(value_type); -handle_op_loop: - PUSH_CSP(LABEL_TYPE_LOOP, cell_num, frame_ip); - HANDLE_OP_END (); - - HANDLE_OP (EXT_OP_IF): - read_leb_uint32(frame_ip, frame_ip_end, type_index); - cell_num = wasm_types[type_index]->ret_cell_num; - goto handle_op_if; - - HANDLE_OP (WASM_OP_IF): - value_type = *frame_ip++; - cell_num = wasm_value_type_cell_num(value_type); -handle_op_if: - cache_index = ((uintptr_t)frame_ip) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); - cache_items = exec_env->block_addr_cache[cache_index]; - if (cache_items[0].start_addr == frame_ip) { - else_addr = cache_items[0].else_addr; - end_addr = cache_items[0].end_addr; - } - else if (cache_items[1].start_addr == frame_ip) { - else_addr = cache_items[1].else_addr; - end_addr = cache_items[1].end_addr; - } - else if (!wasm_loader_find_block_addr(exec_env, - (BlockAddr*)exec_env->block_addr_cache, - frame_ip, (uint8*)-1, - LABEL_TYPE_IF, - &else_addr, &end_addr)) { - wasm_set_exception(module, "find block address failed"); - goto got_exception; - } + else { + end_addr = NULL; + } + PUSH_CSP(LABEL_TYPE_BLOCK, cell_num, end_addr); + HANDLE_OP_END(); + } + + HANDLE_OP(EXT_OP_LOOP) + { + read_leb_uint32(frame_ip, frame_ip_end, type_index); + cell_num = wasm_types[type_index]->param_cell_num; + goto handle_op_loop; + } - cond = (uint32)POP_I32(); + HANDLE_OP(WASM_OP_LOOP) + { + value_type = *frame_ip++; + cell_num = wasm_value_type_cell_num(value_type); + handle_op_loop: + PUSH_CSP(LABEL_TYPE_LOOP, cell_num, frame_ip); + HANDLE_OP_END(); + } - if (cond) { /* if branch is met */ - PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr); - } - else { /* if branch is not met */ - /* if there is no else branch, go to the end addr */ - if (else_addr == NULL) { - frame_ip = end_addr + 1; - } - /* if there is an else branch, go to the else addr */ - else { - PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr); - frame_ip = else_addr + 1; - } - } - HANDLE_OP_END (); + HANDLE_OP(EXT_OP_IF) + { + read_leb_uint32(frame_ip, frame_ip_end, type_index); + cell_num = wasm_types[type_index]->ret_cell_num; + goto handle_op_if; + } - HANDLE_OP (WASM_OP_ELSE): - /* comes from the if branch in WASM_OP_IF */ - frame_ip = (frame_csp - 1)->target_addr; - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_IF) + { + value_type = *frame_ip++; + cell_num = wasm_value_type_cell_num(value_type); + handle_op_if: + cache_index = ((uintptr_t)frame_ip) + & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); + cache_items = exec_env->block_addr_cache[cache_index]; + if (cache_items[0].start_addr == frame_ip) { + else_addr = cache_items[0].else_addr; + end_addr = cache_items[0].end_addr; + } + else if (cache_items[1].start_addr == frame_ip) { + else_addr = cache_items[1].else_addr; + end_addr = cache_items[1].end_addr; + } + else if (!wasm_loader_find_block_addr( + exec_env, (BlockAddr *)exec_env->block_addr_cache, + frame_ip, (uint8 *)-1, LABEL_TYPE_IF, &else_addr, + &end_addr)) { + wasm_set_exception(module, "find block address failed"); + goto got_exception; + } + + cond = (uint32)POP_I32(); + + if (cond) { /* if branch is met */ + PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr); + } + else { /* if branch is not met */ + /* if there is no else branch, go to the end addr */ + if (else_addr == NULL) { + frame_ip = end_addr + 1; + } + /* if there is an else branch, go to the else addr */ + else { + PUSH_CSP(LABEL_TYPE_IF, cell_num, end_addr); + frame_ip = else_addr + 1; + } + } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_END): - if (frame_csp > frame->csp_bottom + 1) { - POP_CSP(); - } - else { /* end of function, treat as WASM_OP_RETURN */ - frame_sp -= cur_func->ret_cell_num; - for (i = 0; i < cur_func->ret_cell_num; i++) { - *prev_frame->sp++ = frame_sp[i]; - } - goto return_func; - } - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_ELSE) + { + /* comes from the if branch in WASM_OP_IF */ + frame_ip = (frame_csp - 1)->target_addr; + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_END) + { + if (frame_csp > frame->csp_bottom + 1) { + POP_CSP(); + } + else { /* end of function, treat as WASM_OP_RETURN */ + frame_sp -= cur_func->ret_cell_num; + for (i = 0; i < cur_func->ret_cell_num; i++) { + *prev_frame->sp++ = frame_sp[i]; + } + goto return_func; + } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_BR): + HANDLE_OP(WASM_OP_BR) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - read_leb_uint32(frame_ip, frame_ip_end, depth); -label_pop_csp_n: - POP_CSP_N(depth); - if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */ - if (!wasm_loader_find_block_addr(exec_env, - (BlockAddr*)exec_env->block_addr_cache, - (frame_csp - 1)->begin_addr, (uint8*)-1, - LABEL_TYPE_BLOCK, - &else_addr, &end_addr)) { - wasm_set_exception(module, "find block address failed"); - goto got_exception; - } - frame_ip = end_addr; - } - HANDLE_OP_END (); + read_leb_uint32(frame_ip, frame_ip_end, depth); + label_pop_csp_n: + POP_CSP_N(depth); + if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */ + if (!wasm_loader_find_block_addr( + exec_env, (BlockAddr *)exec_env->block_addr_cache, + (frame_csp - 1)->begin_addr, (uint8 *)-1, + LABEL_TYPE_BLOCK, &else_addr, &end_addr)) { + wasm_set_exception(module, "find block address failed"); + goto got_exception; + } + frame_ip = end_addr; + } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_BR_IF): + HANDLE_OP(WASM_OP_BR_IF) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - read_leb_uint32(frame_ip, frame_ip_end, depth); - cond = (uint32)POP_I32(); - if (cond) - goto label_pop_csp_n; - HANDLE_OP_END (); + read_leb_uint32(frame_ip, frame_ip_end, depth); + cond = (uint32)POP_I32(); + if (cond) + goto label_pop_csp_n; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_BR_TABLE): + HANDLE_OP(WASM_OP_BR_TABLE) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - read_leb_uint32(frame_ip, frame_ip_end, count); - lidx = POP_I32(); - if (lidx > count) - lidx = count; - for (i = 0; i < lidx; i++) - skip_leb(frame_ip); - read_leb_uint32(frame_ip, frame_ip_end, depth); - goto label_pop_csp_n; - - HANDLE_OP (WASM_OP_RETURN): - frame_sp -= cur_func->ret_cell_num; - for (i = 0; i < cur_func->ret_cell_num; i++) { - *prev_frame->sp++ = frame_sp[i]; - } - goto return_func; + read_leb_uint32(frame_ip, frame_ip_end, count); + lidx = POP_I32(); + if (lidx > count) + lidx = count; + for (i = 0; i < lidx; i++) + skip_leb(frame_ip); + read_leb_uint32(frame_ip, frame_ip_end, depth); + goto label_pop_csp_n; + } + + HANDLE_OP(WASM_OP_RETURN) + { + frame_sp -= cur_func->ret_cell_num; + for (i = 0; i < cur_func->ret_cell_num; i++) { + *prev_frame->sp++ = frame_sp[i]; + } + goto return_func; + } - HANDLE_OP (WASM_OP_CALL): + HANDLE_OP(WASM_OP_CALL) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - read_leb_uint32(frame_ip, frame_ip_end, fidx); + read_leb_uint32(frame_ip, frame_ip_end, fidx); #if WASM_ENABLE_MULTI_MODULE != 0 - if (fidx >= module->function_count) { - wasm_set_exception(module, "unknown function"); - goto got_exception; - } + if (fidx >= module->function_count) { + wasm_set_exception(module, "unknown function"); + goto got_exception; + } #endif - cur_func = module->functions + fidx; - goto call_func_from_interp; + cur_func = module->functions + fidx; + goto call_func_from_interp; + } #if WASM_ENABLE_TAIL_CALL != 0 - HANDLE_OP (WASM_OP_RETURN_CALL): + HANDLE_OP(WASM_OP_RETURN_CALL) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - read_leb_uint32(frame_ip, frame_ip_end, fidx); + read_leb_uint32(frame_ip, frame_ip_end, fidx); #if WASM_ENABLE_MULTI_MODULE != 0 - if (fidx >= module->function_count) { - wasm_set_exception(module, "unknown function"); - goto got_exception; - } + if (fidx >= module->function_count) { + wasm_set_exception(module, "unknown function"); + goto got_exception; + } #endif - cur_func = module->functions + fidx; + cur_func = module->functions + fidx; - goto call_func_from_return_call; + goto call_func_from_return_call; + } #endif /* WASM_ENABLE_TAIL_CALL */ - HANDLE_OP (WASM_OP_CALL_INDIRECT): + HANDLE_OP(WASM_OP_CALL_INDIRECT) #if WASM_ENABLE_TAIL_CALL != 0 - HANDLE_OP (WASM_OP_RETURN_CALL_INDIRECT): + HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT) #endif - { - WASMType *cur_type, *cur_func_type; - WASMTableInstance *tbl_inst; - uint32 tbl_idx; + { + WASMType *cur_type, *cur_func_type; + WASMTableInstance *tbl_inst; + uint32 tbl_idx; #if WASM_ENABLE_TAIL_CALL != 0 - opcode = *(frame_ip - 1); + opcode = *(frame_ip - 1); #endif #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - /** - * type check. compiler will make sure all like - * (call_indirect (type $x) (i32.const 1)) - * the function type has to be defined in the module also - * no matter it is used or not - */ - read_leb_uint32(frame_ip, frame_ip_end, tidx); - bh_assert(tidx < module->module->type_count); - cur_type = wasm_types[tidx]; - - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->table_count); - - tbl_inst = wasm_get_table_inst(module, tbl_idx); - - val = POP_I32(); - if (val < 0 || val >= (int32)tbl_inst->cur_size) { - wasm_set_exception(module, "undefined element"); - goto got_exception; - } - - fidx = ((uint32*)tbl_inst->base_addr)[val]; - if (fidx == (uint32)-1) { - wasm_set_exception(module, "uninitialized element"); - goto got_exception; - } - - /* - * we might be using a table injected by host or - * another module. In that case, we don't validate - * the elem value while loading - */ - if (fidx >= module->function_count) { - wasm_set_exception(module, "unknown function"); - goto got_exception; - } - - /* always call module own functions */ - cur_func = module->functions + fidx; - - if (cur_func->is_import_func) - cur_func_type = cur_func->u.func_import->func_type; - else - cur_func_type = cur_func->u.func->func_type; - if (!wasm_type_equal(cur_type, cur_func_type)) { - wasm_set_exception(module, "indirect call type mismatch"); - goto got_exception; - } + /** + * type check. compiler will make sure all like + * (call_indirect (type $x) (i32.const 1)) + * the function type has to be defined in the module also + * no matter it is used or not + */ + read_leb_uint32(frame_ip, frame_ip_end, tidx); + bh_assert(tidx < module->module->type_count); + cur_type = wasm_types[tidx]; + + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + val = POP_I32(); + if (val < 0 || val >= (int32)tbl_inst->cur_size) { + wasm_set_exception(module, "undefined element"); + goto got_exception; + } + + fidx = ((uint32 *)tbl_inst->base_addr)[val]; + if (fidx == (uint32)-1) { + wasm_set_exception(module, "uninitialized element"); + goto got_exception; + } + + /* + * we might be using a table injected by host or + * another module. In that case, we don't validate + * the elem value while loading + */ + if (fidx >= module->function_count) { + wasm_set_exception(module, "unknown function"); + goto got_exception; + } + + /* always call module own functions */ + cur_func = module->functions + fidx; + + if (cur_func->is_import_func) + cur_func_type = cur_func->u.func_import->func_type; + else + cur_func_type = cur_func->u.func->func_type; + if (!wasm_type_equal(cur_type, cur_func_type)) { + wasm_set_exception(module, "indirect call type mismatch"); + goto got_exception; + } #if WASM_ENABLE_TAIL_CALL != 0 - if (opcode == WASM_OP_RETURN_CALL_INDIRECT) - goto call_func_from_return_call; + if (opcode == WASM_OP_RETURN_CALL_INDIRECT) + goto call_func_from_return_call; #endif - goto call_func_from_interp; - } + goto call_func_from_interp; + } - /* parametric instructions */ - HANDLE_OP (WASM_OP_DROP): - { - frame_sp--; - HANDLE_OP_END (); - } + /* parametric instructions */ + HANDLE_OP(WASM_OP_DROP) + { + frame_sp--; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_DROP_64): - { - frame_sp -= 2; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_DROP_64) + { + frame_sp -= 2; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SELECT): - { - cond = (uint32)POP_I32(); - frame_sp--; - if (!cond) - *(frame_sp - 1) = *frame_sp; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_SELECT) + { + cond = (uint32)POP_I32(); + frame_sp--; + if (!cond) + *(frame_sp - 1) = *frame_sp; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SELECT_64): - { - cond = (uint32)POP_I32(); - frame_sp -= 2; - if (!cond) { - *(frame_sp - 2) = *frame_sp; - *(frame_sp - 1) = *(frame_sp + 1); - } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_SELECT_64) + { + cond = (uint32)POP_I32(); + frame_sp -= 2; + if (!cond) { + *(frame_sp - 2) = *frame_sp; + *(frame_sp - 1) = *(frame_sp + 1); + } + HANDLE_OP_END(); + } #if WASM_ENABLE_REF_TYPES != 0 - HANDLE_OP (WASM_OP_SELECT_T): - { - uint32 vec_len; - uint8 type; - - read_leb_uint32(frame_ip, frame_ip_end, vec_len); - type = *frame_ip++; - - cond = (uint32)POP_I32(); - if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) { - frame_sp -= 2; - if (!cond) { - *(frame_sp - 2) = *frame_sp; - *(frame_sp - 1) = *(frame_sp + 1); - } - } - else { - frame_sp--; - if (!cond) - *(frame_sp - 1) = *frame_sp; - } - - (void)vec_len; - HANDLE_OP_END (); - } - HANDLE_OP (WASM_OP_TABLE_GET): - { - uint32 tbl_idx, elem_idx; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_SELECT_T) + { + uint32 vec_len; + uint8 type; + + read_leb_uint32(frame_ip, frame_ip_end, vec_len); + type = *frame_ip++; + + cond = (uint32)POP_I32(); + if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) { + frame_sp -= 2; + if (!cond) { + *(frame_sp - 2) = *frame_sp; + *(frame_sp - 1) = *(frame_sp + 1); + } + } + else { + frame_sp--; + if (!cond) + *(frame_sp - 1) = *frame_sp; + } + + (void)vec_len; + HANDLE_OP_END(); + } + HANDLE_OP(WASM_OP_TABLE_GET) + { + uint32 tbl_idx, elem_idx; + WASMTableInstance *tbl_inst; - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->table_count); + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->table_count); - tbl_inst = wasm_get_table_inst(module, tbl_idx); + tbl_inst = wasm_get_table_inst(module, tbl_idx); - elem_idx = POP_I32(); - if (elem_idx >= tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } + elem_idx = POP_I32(); + if (elem_idx >= tbl_inst->cur_size) { + wasm_set_exception(module, "out of bounds table access"); + goto got_exception; + } - PUSH_I32(((uint32 *)tbl_inst->base_addr)[elem_idx]); - HANDLE_OP_END (); - } + PUSH_I32(((uint32 *)tbl_inst->base_addr)[elem_idx]); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_TABLE_SET): - { - uint32 tbl_idx, elem_idx, val; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_TABLE_SET) + { + uint32 tbl_idx, elem_idx, val; + WASMTableInstance *tbl_inst; - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->table_count); + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->table_count); - tbl_inst = wasm_get_table_inst(module, tbl_idx); + tbl_inst = wasm_get_table_inst(module, tbl_idx); - val = POP_I32(); - elem_idx = POP_I32(); - if (elem_idx >= tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } + val = POP_I32(); + elem_idx = POP_I32(); + if (elem_idx >= tbl_inst->cur_size) { + wasm_set_exception(module, "out of bounds table access"); + goto got_exception; + } - ((uint32 *)(tbl_inst->base_addr))[elem_idx] = val; - HANDLE_OP_END(); - } + ((uint32 *)(tbl_inst->base_addr))[elem_idx] = val; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_REF_NULL): - { - uint32 ref_type; - read_leb_uint32(frame_ip, frame_ip_end, ref_type); - PUSH_I32(NULL_REF); - (void)ref_type; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_REF_NULL) + { + uint32 ref_type; + read_leb_uint32(frame_ip, frame_ip_end, ref_type); + PUSH_I32(NULL_REF); + (void)ref_type; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_REF_IS_NULL): - { - uint32 val; - val = POP_I32(); - PUSH_I32(val == NULL_REF ? 1 : 0); - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_REF_IS_NULL) + { + uint32 val; + val = POP_I32(); + PUSH_I32(val == NULL_REF ? 1 : 0); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_REF_FUNC): - { - uint32 func_idx; - read_leb_uint32(frame_ip, frame_ip_end, func_idx); - PUSH_I32(func_idx); - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_REF_FUNC) + { + uint32 func_idx; + read_leb_uint32(frame_ip, frame_ip_end, func_idx); + PUSH_I32(func_idx); + HANDLE_OP_END(); + } #endif /* WASM_ENABLE_REF_TYPES */ - /* variable instructions */ - HANDLE_OP (WASM_OP_GET_LOCAL): - { - GET_LOCAL_INDEX_TYPE_AND_OFFSET(); + /* variable instructions */ + HANDLE_OP(WASM_OP_GET_LOCAL) + { + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); - switch (local_type) { - case VALUE_TYPE_I32: - case VALUE_TYPE_F32: + switch (local_type) { + case VALUE_TYPE_I32: + case VALUE_TYPE_F32: #if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - case VALUE_TYPE_EXTERNREF: + case VALUE_TYPE_FUNCREF: + case VALUE_TYPE_EXTERNREF: #endif - PUSH_I32(*(int32*)(frame_lp + local_offset)); - break; - case VALUE_TYPE_I64: - case VALUE_TYPE_F64: - PUSH_I64(GET_I64_FROM_ADDR(frame_lp + local_offset)); - break; - default: - wasm_set_exception(module, "invalid local type"); - goto got_exception; - } - - HANDLE_OP_END (); - } + PUSH_I32(*(int32 *)(frame_lp + local_offset)); + break; + case VALUE_TYPE_I64: + case VALUE_TYPE_F64: + PUSH_I64(GET_I64_FROM_ADDR(frame_lp + local_offset)); + break; + default: + wasm_set_exception(module, "invalid local type"); + goto got_exception; + } + + HANDLE_OP_END(); + } - HANDLE_OP (EXT_OP_GET_LOCAL_FAST): - { - local_offset = *frame_ip++; - if (local_offset & 0x80) - PUSH_I64(GET_I64_FROM_ADDR(frame_lp + (local_offset & 0x7F))); - else - PUSH_I32(*(int32*)(frame_lp + local_offset)); - HANDLE_OP_END (); - } + HANDLE_OP(EXT_OP_GET_LOCAL_FAST) + { + local_offset = *frame_ip++; + if (local_offset & 0x80) + PUSH_I64( + GET_I64_FROM_ADDR(frame_lp + (local_offset & 0x7F))); + else + PUSH_I32(*(int32 *)(frame_lp + local_offset)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_LOCAL): - { - GET_LOCAL_INDEX_TYPE_AND_OFFSET(); + HANDLE_OP(WASM_OP_SET_LOCAL) + { + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); - switch (local_type) { - case VALUE_TYPE_I32: - case VALUE_TYPE_F32: + switch (local_type) { + case VALUE_TYPE_I32: + case VALUE_TYPE_F32: #if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - case VALUE_TYPE_EXTERNREF: + case VALUE_TYPE_FUNCREF: + case VALUE_TYPE_EXTERNREF: #endif - *(int32*)(frame_lp + local_offset) = POP_I32(); - break; - case VALUE_TYPE_I64: - case VALUE_TYPE_F64: - PUT_I64_TO_ADDR((uint32*)(frame_lp + local_offset), POP_I64()); - break; - default: - wasm_set_exception(module, "invalid local type"); - goto got_exception; - } - - HANDLE_OP_END (); - } + *(int32 *)(frame_lp + local_offset) = POP_I32(); + break; + case VALUE_TYPE_I64: + case VALUE_TYPE_F64: + PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset), + POP_I64()); + break; + default: + wasm_set_exception(module, "invalid local type"); + goto got_exception; + } + + HANDLE_OP_END(); + } - HANDLE_OP (EXT_OP_SET_LOCAL_FAST): - { - local_offset = *frame_ip++; - if (local_offset & 0x80) - PUT_I64_TO_ADDR((uint32*)(frame_lp + (local_offset & 0x7F)), POP_I64()); - else - *(int32*)(frame_lp + local_offset) = POP_I32(); - HANDLE_OP_END (); - } + HANDLE_OP(EXT_OP_SET_LOCAL_FAST) + { + local_offset = *frame_ip++; + if (local_offset & 0x80) + PUT_I64_TO_ADDR( + (uint32 *)(frame_lp + (local_offset & 0x7F)), + POP_I64()); + else + *(int32 *)(frame_lp + local_offset) = POP_I32(); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_TEE_LOCAL): - { - GET_LOCAL_INDEX_TYPE_AND_OFFSET(); + HANDLE_OP(WASM_OP_TEE_LOCAL) + { + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); - switch (local_type) { - case VALUE_TYPE_I32: - case VALUE_TYPE_F32: + switch (local_type) { + case VALUE_TYPE_I32: + case VALUE_TYPE_F32: #if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_FUNCREF: - case VALUE_TYPE_EXTERNREF: + case VALUE_TYPE_FUNCREF: + case VALUE_TYPE_EXTERNREF: #endif - *(int32*)(frame_lp + local_offset) = *(int32*)(frame_sp - 1); - break; - case VALUE_TYPE_I64: - case VALUE_TYPE_F64: - PUT_I64_TO_ADDR((uint32*)(frame_lp + local_offset), - GET_I64_FROM_ADDR(frame_sp - 2)); - break; - default: - wasm_set_exception(module, "invalid local type"); - goto got_exception; - } - - HANDLE_OP_END (); - } - - HANDLE_OP (EXT_OP_TEE_LOCAL_FAST): - { - local_offset = *frame_ip++; - if (local_offset & 0x80) - PUT_I64_TO_ADDR((uint32*)(frame_lp + (local_offset & 0x7F)), - GET_I64_FROM_ADDR(frame_sp - 2)); - else - *(int32*)(frame_lp + local_offset) = *(int32*)(frame_sp - 1); - HANDLE_OP_END (); - } + *(int32 *)(frame_lp + local_offset) = + *(int32 *)(frame_sp - 1); + break; + case VALUE_TYPE_I64: + case VALUE_TYPE_F64: + PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset), + GET_I64_FROM_ADDR(frame_sp - 2)); + break; + default: + wasm_set_exception(module, "invalid local type"); + goto got_exception; + } + + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_GET_GLOBAL): - { - read_leb_uint32(frame_ip, frame_ip_end, global_idx); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - PUSH_I32(*(uint32*)global_addr); - HANDLE_OP_END (); - } + HANDLE_OP(EXT_OP_TEE_LOCAL_FAST) + { + local_offset = *frame_ip++; + if (local_offset & 0x80) + PUT_I64_TO_ADDR( + (uint32 *)(frame_lp + (local_offset & 0x7F)), + GET_I64_FROM_ADDR(frame_sp - 2)); + else + *(int32 *)(frame_lp + local_offset) = + *(int32 *)(frame_sp - 1); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_GET_GLOBAL_64): - { - read_leb_uint32(frame_ip, frame_ip_end, global_idx); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - PUSH_I64(GET_I64_FROM_ADDR((uint32*)global_addr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_GET_GLOBAL) + { + read_leb_uint32(frame_ip, frame_ip_end, global_idx); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + PUSH_I32(*(uint32 *)global_addr); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_GLOBAL): - { - read_leb_uint32(frame_ip, frame_ip_end, global_idx); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - *(int32*)global_addr = POP_I32(); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_GET_GLOBAL_64) + { + read_leb_uint32(frame_ip, frame_ip_end, global_idx); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + PUSH_I64(GET_I64_FROM_ADDR((uint32 *)global_addr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_GLOBAL_AUX_STACK): - { - uint32 aux_stack_top; + HANDLE_OP(WASM_OP_SET_GLOBAL) + { + read_leb_uint32(frame_ip, frame_ip_end, global_idx); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + *(int32 *)global_addr = POP_I32(); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, global_idx); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - aux_stack_top = *(uint32*)(frame_sp - 1); - if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) { - wasm_set_exception(module, "wasm auxiliary stack overflow"); - goto got_exception; - } - if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { - wasm_set_exception(module, "wasm auxiliary stack underflow"); - goto got_exception; - } - *(int32*)global_addr = aux_stack_top; - frame_sp--; + HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK) + { + uint32 aux_stack_top; + + read_leb_uint32(frame_ip, frame_ip_end, global_idx); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + aux_stack_top = *(uint32 *)(frame_sp - 1); + if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) { + wasm_set_exception(module, "wasm auxiliary stack overflow"); + goto got_exception; + } + if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { + wasm_set_exception(module, + "wasm auxiliary stack underflow"); + goto got_exception; + } + *(int32 *)global_addr = aux_stack_top; + frame_sp--; #if WASM_ENABLE_MEMORY_PROFILING != 0 - if (module->module->aux_stack_top_global_index != (uint32)-1) { - uint32 aux_stack_used = - module->module->aux_stack_bottom - *(uint32*)global_addr; - if (aux_stack_used > module->max_aux_stack_used) - module->max_aux_stack_used = aux_stack_used; - } + if (module->module->aux_stack_top_global_index != (uint32)-1) { + uint32 aux_stack_used = module->module->aux_stack_bottom + - *(uint32 *)global_addr; + if (aux_stack_used > module->max_aux_stack_used) + module->max_aux_stack_used = aux_stack_used; + } #endif - HANDLE_OP_END (); - } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_GLOBAL_64): - { - read_leb_uint32(frame_ip, frame_ip_end, global_idx); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - PUT_I64_TO_ADDR((uint32*)global_addr, POP_I64()); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_SET_GLOBAL_64) + { + read_leb_uint32(frame_ip, frame_ip_end, global_idx); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + PUT_I64_TO_ADDR((uint32 *)global_addr, POP_I64()); + HANDLE_OP_END(); + } - /* memory load instructions */ - HANDLE_OP (WASM_OP_I32_LOAD): - HANDLE_OP (WASM_OP_F32_LOAD): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(4); - PUSH_I32(LOAD_I32(maddr)); - (void)flags; - HANDLE_OP_END(); - } + /* memory load instructions */ + HANDLE_OP(WASM_OP_I32_LOAD) + HANDLE_OP(WASM_OP_F32_LOAD) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(4); + PUSH_I32(LOAD_I32(maddr)); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD): - HANDLE_OP (WASM_OP_F64_LOAD): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(8); - PUSH_I64(LOAD_I64(maddr)); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD) + HANDLE_OP(WASM_OP_F64_LOAD) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(8); + PUSH_I64(LOAD_I64(maddr)); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD8_S): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(1); - PUSH_I32(sign_ext_8_32(*(int8*)maddr)); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I32_LOAD8_S) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(1); + PUSH_I32(sign_ext_8_32(*(int8 *)maddr)); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD8_U): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(1); - PUSH_I32((uint32)(*(uint8*)maddr)); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I32_LOAD8_U) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(1); + PUSH_I32((uint32)(*(uint8 *)maddr)); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD16_S): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(2); - PUSH_I32(sign_ext_16_32(LOAD_I16(maddr))); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I32_LOAD16_S) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(2); + PUSH_I32(sign_ext_16_32(LOAD_I16(maddr))); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD16_U): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(2); - PUSH_I32((uint32)(LOAD_U16(maddr))); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I32_LOAD16_U) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(2); + PUSH_I32((uint32)(LOAD_U16(maddr))); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD8_S): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(1); - PUSH_I64(sign_ext_8_64(*(int8*)maddr)); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD8_S) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(1); + PUSH_I64(sign_ext_8_64(*(int8 *)maddr)); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD8_U): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(1); - PUSH_I64((uint64)(*(uint8*)maddr)); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD8_U) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(1); + PUSH_I64((uint64)(*(uint8 *)maddr)); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD16_S): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(2); - PUSH_I64(sign_ext_16_64(LOAD_I16(maddr))); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD16_S) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(2); + PUSH_I64(sign_ext_16_64(LOAD_I16(maddr))); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD16_U): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(2); - PUSH_I64((uint64)(LOAD_U16(maddr))); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD16_U) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(2); + PUSH_I64((uint64)(LOAD_U16(maddr))); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD32_S): - { - uint32 offset, flags, addr; - - opcode = *(frame_ip - 1); - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(4); - PUSH_I64(sign_ext_32_64(LOAD_I32(maddr))); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD32_S) + { + uint32 offset, flags, addr; + + opcode = *(frame_ip - 1); + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(4); + PUSH_I64(sign_ext_32_64(LOAD_I32(maddr))); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD32_U): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(4); - PUSH_I64((uint64)(LOAD_U32(maddr))); - (void)flags; - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_I64_LOAD32_U) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(4); + PUSH_I64((uint64)(LOAD_U32(maddr))); + (void)flags; + HANDLE_OP_END(); + } - /* memory store instructions */ - HANDLE_OP (WASM_OP_I32_STORE): - HANDLE_OP (WASM_OP_F32_STORE): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - frame_sp--; - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(4); - STORE_U32(maddr, frame_sp[1]); - (void)flags; - HANDLE_OP_END (); - } + /* memory store instructions */ + HANDLE_OP(WASM_OP_I32_STORE) + HANDLE_OP(WASM_OP_F32_STORE) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + frame_sp--; + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(4); + STORE_U32(maddr, frame_sp[1]); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_STORE): - HANDLE_OP (WASM_OP_F64_STORE): - { - uint32 offset, flags, addr; - - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - frame_sp -= 2; - addr = POP_I32(); - CHECK_MEMORY_OVERFLOW(8); - STORE_U32(maddr, frame_sp[1]); - STORE_U32(maddr + 4, frame_sp[2]); - (void)flags; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_STORE) + HANDLE_OP(WASM_OP_F64_STORE) + { + uint32 offset, flags, addr; + + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + frame_sp -= 2; + addr = POP_I32(); + CHECK_MEMORY_OVERFLOW(8); + STORE_U32(maddr, frame_sp[1]); + STORE_U32(maddr + 4, frame_sp[2]); + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_STORE8): - HANDLE_OP (WASM_OP_I32_STORE16): - { - uint32 offset, flags, addr; - uint32 sval; - - opcode = *(frame_ip - 1); - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - sval = (uint32)POP_I32(); - addr = POP_I32(); - - if (opcode == WASM_OP_I32_STORE8) { - CHECK_MEMORY_OVERFLOW(1); - *(uint8*)maddr = (uint8)sval; - } - else { - CHECK_MEMORY_OVERFLOW(2); - STORE_U16(maddr, (uint16)sval); - } - - (void)flags; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_STORE8) + HANDLE_OP(WASM_OP_I32_STORE16) + { + uint32 offset, flags, addr; + uint32 sval; + + opcode = *(frame_ip - 1); + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + sval = (uint32)POP_I32(); + addr = POP_I32(); + + if (opcode == WASM_OP_I32_STORE8) { + CHECK_MEMORY_OVERFLOW(1); + *(uint8 *)maddr = (uint8)sval; + } + else { + CHECK_MEMORY_OVERFLOW(2); + STORE_U16(maddr, (uint16)sval); + } + + (void)flags; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_STORE8): - HANDLE_OP (WASM_OP_I64_STORE16): - HANDLE_OP (WASM_OP_I64_STORE32): - { - uint32 offset, flags, addr; - uint64 sval; - - opcode = *(frame_ip - 1); - read_leb_uint32(frame_ip, frame_ip_end, flags); - read_leb_uint32(frame_ip, frame_ip_end, offset); - sval = (uint64)POP_I64(); - addr = POP_I32(); - - if (opcode == WASM_OP_I64_STORE8) { - CHECK_MEMORY_OVERFLOW(1); - *(uint8*)maddr = (uint8)sval; - } - else if(opcode == WASM_OP_I64_STORE16) { - CHECK_MEMORY_OVERFLOW(2); - STORE_U16(maddr, (uint16)sval); - } - else { - CHECK_MEMORY_OVERFLOW(4); - STORE_U32(maddr, (uint32)sval); - } - (void)flags; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_STORE8) + HANDLE_OP(WASM_OP_I64_STORE16) + HANDLE_OP(WASM_OP_I64_STORE32) + { + uint32 offset, flags, addr; + uint64 sval; + + opcode = *(frame_ip - 1); + read_leb_uint32(frame_ip, frame_ip_end, flags); + read_leb_uint32(frame_ip, frame_ip_end, offset); + sval = (uint64)POP_I64(); + addr = POP_I32(); + + if (opcode == WASM_OP_I64_STORE8) { + CHECK_MEMORY_OVERFLOW(1); + *(uint8 *)maddr = (uint8)sval; + } + else if (opcode == WASM_OP_I64_STORE16) { + CHECK_MEMORY_OVERFLOW(2); + STORE_U16(maddr, (uint16)sval); + } + else { + CHECK_MEMORY_OVERFLOW(4); + STORE_U32(maddr, (uint32)sval); + } + (void)flags; + HANDLE_OP_END(); + } - /* memory size and memory grow instructions */ - HANDLE_OP (WASM_OP_MEMORY_SIZE): - { - uint32 reserved; - read_leb_uint32(frame_ip, frame_ip_end, reserved); - PUSH_I32(memory->cur_page_count); - (void)reserved; - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_MEMORY_GROW): - { - uint32 reserved, delta, prev_page_count = memory->cur_page_count; - - read_leb_uint32(frame_ip, frame_ip_end, reserved); - delta = (uint32)POP_I32(); - - if (!wasm_enlarge_memory(module, delta)) { - /* failed to memory.grow, return -1 */ - PUSH_I32(-1); - } - else { - /* success, return previous page count */ - PUSH_I32(prev_page_count); - /* update memory instance ptr and memory size */ - memory = module->default_memory; - linear_mem_size = num_bytes_per_page * memory->cur_page_count; - } + /* memory size and memory grow instructions */ + HANDLE_OP(WASM_OP_MEMORY_SIZE) + { + uint32 reserved; + read_leb_uint32(frame_ip, frame_ip_end, reserved); + PUSH_I32(memory->cur_page_count); + (void)reserved; + HANDLE_OP_END(); + } - (void)reserved; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_MEMORY_GROW) + { + uint32 reserved, delta, + prev_page_count = memory->cur_page_count; + + read_leb_uint32(frame_ip, frame_ip_end, reserved); + delta = (uint32)POP_I32(); + + if (!wasm_enlarge_memory(module, delta)) { + /* failed to memory.grow, return -1 */ + PUSH_I32(-1); + } + else { + /* success, return previous page count */ + PUSH_I32(prev_page_count); + /* update memory instance ptr and memory size */ + memory = module->default_memory; + linear_mem_size = + num_bytes_per_page * memory->cur_page_count; + } + + (void)reserved; + HANDLE_OP_END(); + } - /* constant instructions */ - HANDLE_OP (WASM_OP_I32_CONST): - DEF_OP_I_CONST(int32, I32); - HANDLE_OP_END (); + /* constant instructions */ + HANDLE_OP(WASM_OP_I32_CONST) + DEF_OP_I_CONST(int32, I32); + HANDLE_OP_END(); + + HANDLE_OP(WASM_OP_I64_CONST) + DEF_OP_I_CONST(int64, I64); + HANDLE_OP_END(); + + HANDLE_OP(WASM_OP_F32_CONST) + { + uint8 *p_float = (uint8 *)frame_sp++; + for (i = 0; i < sizeof(float32); i++) + *p_float++ = *frame_ip++; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_CONST): - DEF_OP_I_CONST(int64, I64); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_F64_CONST) + { + uint8 *p_float = (uint8 *)frame_sp++; + frame_sp++; + for (i = 0; i < sizeof(float64); i++) + *p_float++ = *frame_ip++; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_F32_CONST): - { - uint8 *p_float = (uint8*)frame_sp++; - for (i = 0; i < sizeof(float32); i++) - *p_float++ = *frame_ip++; - HANDLE_OP_END (); - } + /* comparison instructions of i32 */ + HANDLE_OP(WASM_OP_I32_EQZ) + { + DEF_OP_EQZ(I32); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_F64_CONST): - { - uint8 *p_float = (uint8*)frame_sp++; - frame_sp++; - for (i = 0; i < sizeof(float64); i++) - *p_float++ = *frame_ip++; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_EQ) + { + DEF_OP_CMP(uint32, I32, ==); + HANDLE_OP_END(); + } - /* comparison instructions of i32 */ - HANDLE_OP (WASM_OP_I32_EQZ): - DEF_OP_EQZ(I32); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_NE) + { + DEF_OP_CMP(uint32, I32, !=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_EQ): - DEF_OP_CMP(uint32, I32, ==); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LT_S) + { + DEF_OP_CMP(int32, I32, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_NE): - DEF_OP_CMP(uint32, I32, !=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LT_U) + { + DEF_OP_CMP(uint32, I32, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LT_S): - DEF_OP_CMP(int32, I32, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GT_S) + { + DEF_OP_CMP(int32, I32, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LT_U): - DEF_OP_CMP(uint32, I32, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GT_U) + { + DEF_OP_CMP(uint32, I32, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GT_S): - DEF_OP_CMP(int32, I32, >); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LE_S) + { + DEF_OP_CMP(int32, I32, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GT_U): - DEF_OP_CMP(uint32, I32, >); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LE_U) + { + DEF_OP_CMP(uint32, I32, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LE_S): - DEF_OP_CMP(int32, I32, <=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GE_S) + { + DEF_OP_CMP(int32, I32, >=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LE_U): - DEF_OP_CMP(uint32, I32, <=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GE_U) + { + DEF_OP_CMP(uint32, I32, >=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GE_S): - DEF_OP_CMP(int32, I32, >=); - HANDLE_OP_END (); + /* comparison instructions of i64 */ + HANDLE_OP(WASM_OP_I64_EQZ) + { + DEF_OP_EQZ(I64); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GE_U): - DEF_OP_CMP(uint32, I32, >=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_EQ) + { + DEF_OP_CMP(uint64, I64, ==); + HANDLE_OP_END(); + } - /* comparison instructions of i64 */ - HANDLE_OP (WASM_OP_I64_EQZ): - DEF_OP_EQZ(I64); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_NE) + { + DEF_OP_CMP(uint64, I64, !=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_EQ): - DEF_OP_CMP(uint64, I64, ==); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_LT_S) + { + DEF_OP_CMP(int64, I64, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_NE): - DEF_OP_CMP(uint64, I64, !=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_LT_U) + { + DEF_OP_CMP(uint64, I64, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LT_S): - DEF_OP_CMP(int64, I64, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_GT_S) + { + DEF_OP_CMP(int64, I64, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LT_U): - DEF_OP_CMP(uint64, I64, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_GT_U) + { + DEF_OP_CMP(uint64, I64, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_GT_S): - DEF_OP_CMP(int64, I64, >); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_LE_S) + { + DEF_OP_CMP(int64, I64, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_GT_U): - DEF_OP_CMP(uint64, I64, >); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_LE_S): - DEF_OP_CMP(int64, I64, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_LE_U): - DEF_OP_CMP(uint64, I64, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_GE_S): - DEF_OP_CMP(int64, I64, >=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_GE_U): - DEF_OP_CMP(uint64, I64, >=); - HANDLE_OP_END (); - - /* comparison instructions of f32 */ - HANDLE_OP (WASM_OP_F32_EQ): - DEF_OP_CMP(float32, F32, ==); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_NE): - DEF_OP_CMP(float32, F32, !=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_LT): - DEF_OP_CMP(float32, F32, <); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_GT): - DEF_OP_CMP(float32, F32, >); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_LE): - DEF_OP_CMP(float32, F32, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_GE): - DEF_OP_CMP(float32, F32, >=); - HANDLE_OP_END (); - - /* comparison instructions of f64 */ - HANDLE_OP (WASM_OP_F64_EQ): - DEF_OP_CMP(float64, F64, ==); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_NE): - DEF_OP_CMP(float64, F64, !=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_LT): - DEF_OP_CMP(float64, F64, <); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_GT): - DEF_OP_CMP(float64, F64, >); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_LE): - DEF_OP_CMP(float64, F64, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_GE): - DEF_OP_CMP(float64, F64, >=); - HANDLE_OP_END (); - - /* numberic instructions of i32 */ - HANDLE_OP (WASM_OP_I32_CLZ): - DEF_OP_BIT_COUNT(uint32, I32, clz32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_CTZ): - DEF_OP_BIT_COUNT(uint32, I32, ctz32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_POPCNT): - DEF_OP_BIT_COUNT(uint32, I32, popcount32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_ADD): - DEF_OP_NUMERIC(uint32, uint32, I32, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_SUB): - DEF_OP_NUMERIC(uint32, uint32, I32, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_MUL): - DEF_OP_NUMERIC(uint32, uint32, I32, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_DIV_S): - { - int32 a, b; - - b = POP_I32(); - a = POP_I32(); - if (a == (int32)0x80000000 && b == -1) { - wasm_set_exception(module, "integer overflow"); - goto got_exception; - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I32(a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_DIV_U): - { - uint32 a, b; - - b = (uint32)POP_I32(); - a = (uint32)POP_I32(); - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I32(a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_REM_S): - { - int32 a, b; - - b = POP_I32(); - a = POP_I32(); - if (a == (int32)0x80000000 && b == -1) { - PUSH_I32(0); - HANDLE_OP_END (); - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I32(a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_REM_U): - { - uint32 a, b; - - b = (uint32)POP_I32(); - a = (uint32)POP_I32(); - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I32(a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_AND): - DEF_OP_NUMERIC(uint32, uint32, I32, &); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_OR): - DEF_OP_NUMERIC(uint32, uint32, I32, |); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_XOR): - DEF_OP_NUMERIC(uint32, uint32, I32, ^); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_SHL): - { - DEF_OP_NUMERIC2(uint32, uint32, I32, <<); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_SHR_S): - { - DEF_OP_NUMERIC2(int32, uint32, I32, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_SHR_U): - { - DEF_OP_NUMERIC2(uint32, uint32, I32, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_ROTL): - { - uint32 a, b; - - b = (uint32)POP_I32(); - a = (uint32)POP_I32(); - PUSH_I32(rotl32(a, b)); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_ROTR): - { - uint32 a, b; - - b = (uint32)POP_I32(); - a = (uint32)POP_I32(); - PUSH_I32(rotr32(a, b)); - HANDLE_OP_END (); - } - - /* numberic instructions of i64 */ - HANDLE_OP (WASM_OP_I64_CLZ): - DEF_OP_BIT_COUNT(uint64, I64, clz64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_CTZ): - DEF_OP_BIT_COUNT(uint64, I64, ctz64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_POPCNT): - DEF_OP_BIT_COUNT(uint64, I64, popcount64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_ADD): - DEF_OP_NUMERIC_64(uint64, uint64, I64, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_SUB): - DEF_OP_NUMERIC_64(uint64, uint64, I64, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_MUL): - DEF_OP_NUMERIC_64(uint64, uint64, I64, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_DIV_S): - { - int64 a, b; - - b = POP_I64(); - a = POP_I64(); - if (a == (int64)0x8000000000000000LL && b == -1) { - wasm_set_exception(module, "integer overflow"); - goto got_exception; - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I64(a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_DIV_U): - { - uint64 a, b; - - b = (uint64)POP_I64(); - a = (uint64)POP_I64(); - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I64(a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_REM_S): - { - int64 a, b; - - b = POP_I64(); - a = POP_I64(); - if (a == (int64)0x8000000000000000LL && b == -1) { - PUSH_I64(0); - HANDLE_OP_END (); - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I64(a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_REM_U): - { - uint64 a, b; - - b = (uint64)POP_I64(); - a = (uint64)POP_I64(); - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUSH_I64(a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_AND): - DEF_OP_NUMERIC_64(uint64, uint64, I64, &); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_OR): - DEF_OP_NUMERIC_64(uint64, uint64, I64, |); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_XOR): - DEF_OP_NUMERIC_64(uint64, uint64, I64, ^); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_SHL): - { - DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_SHR_S): - { - DEF_OP_NUMERIC2_64(int64, uint64, I64, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_SHR_U): - { - DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_ROTL): - { - uint64 a, b; - - b = (uint64)POP_I64(); - a = (uint64)POP_I64(); - PUSH_I64(rotl64(a, b)); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_ROTR): - { - uint64 a, b; - - b = (uint64)POP_I64(); - a = (uint64)POP_I64(); - PUSH_I64(rotr64(a, b)); - HANDLE_OP_END (); - } - - /* numberic instructions of f32 */ - HANDLE_OP (WASM_OP_F32_ABS): - DEF_OP_MATH(float32, F32, fabs); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_NEG): - { - uint32 u32 = frame_sp[-1]; - uint32 sign_bit = u32 & ((uint32)1 << 31); - if (sign_bit) - frame_sp[-1] = u32 & ~((uint32)1 << 31); - else - frame_sp[-1] = u32 | ((uint32)1 << 31); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F32_CEIL): - DEF_OP_MATH(float32, F32, ceil); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_FLOOR): - DEF_OP_MATH(float32, F32, floor); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_TRUNC): - DEF_OP_MATH(float32, F32, trunc); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_NEAREST): - DEF_OP_MATH(float32, F32, rint); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_SQRT): - DEF_OP_MATH(float32, F32, sqrt); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_ADD): - DEF_OP_NUMERIC(float32, float32, F32, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_SUB): - DEF_OP_NUMERIC(float32, float32, F32, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_MUL): - DEF_OP_NUMERIC(float32, float32, F32, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_DIV): - DEF_OP_NUMERIC(float32, float32, F32, /); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_MIN): - { - float32 a, b; - - b = POP_F32(); - a = POP_F32(); - - if (isnan(a)) - PUSH_F32(a); - else if (isnan(b)) - PUSH_F32(b); - else - PUSH_F32(wa_fmin(a, b)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LE_U) + { + DEF_OP_CMP(uint64, I64, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_F32_MAX): - { - float32 a, b; + HANDLE_OP(WASM_OP_I64_GE_S) + { + DEF_OP_CMP(int64, I64, >=); + HANDLE_OP_END(); + } - b = POP_F32(); - a = POP_F32(); + HANDLE_OP(WASM_OP_I64_GE_U) + { + DEF_OP_CMP(uint64, I64, >=); + HANDLE_OP_END(); + } - if (isnan(a)) - PUSH_F32(a); - else if (isnan(b)) - PUSH_F32(b); - else - PUSH_F32(wa_fmax(a, b)); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F32_COPYSIGN): - { - float32 a, b; - - b = POP_F32(); - a = POP_F32(); - PUSH_F32(signbit(b) ? -fabs(a) : fabs(a)); - HANDLE_OP_END (); - } - - /* numberic instructions of f64 */ - HANDLE_OP (WASM_OP_F64_ABS): - DEF_OP_MATH(float64, F64, fabs); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_NEG): - { - uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2); - uint64 sign_bit = u64 & (((uint64)1) << 63); - if (sign_bit) - PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63))); - else - PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63))); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F64_CEIL): - DEF_OP_MATH(float64, F64, ceil); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_FLOOR): - DEF_OP_MATH(float64, F64, floor); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_TRUNC): - DEF_OP_MATH(float64, F64, trunc); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_NEAREST): - DEF_OP_MATH(float64, F64, rint); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_SQRT): - DEF_OP_MATH(float64, F64, sqrt); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_ADD): - DEF_OP_NUMERIC_64(float64, float64, F64, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_SUB): - DEF_OP_NUMERIC_64(float64, float64, F64, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_MUL): - DEF_OP_NUMERIC_64(float64, float64, F64, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_DIV): - DEF_OP_NUMERIC_64(float64, float64, F64, /); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_MIN): - { - float64 a, b; - - b = POP_F64(); - a = POP_F64(); - - if (isnan(a)) - PUSH_F64(a); - else if (isnan(b)) - PUSH_F64(b); - else - PUSH_F64(wa_fmin(a, b)); - HANDLE_OP_END (); - } + /* comparison instructions of f32 */ + HANDLE_OP(WASM_OP_F32_EQ) + { + DEF_OP_CMP(float32, F32, ==); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_F64_MAX): - { - float64 a, b; + HANDLE_OP(WASM_OP_F32_NE) + { + DEF_OP_CMP(float32, F32, !=); + HANDLE_OP_END(); + } - b = POP_F64(); - a = POP_F64(); + HANDLE_OP(WASM_OP_F32_LT) + { + DEF_OP_CMP(float32, F32, <); + HANDLE_OP_END(); + } - if (isnan(a)) - PUSH_F64(a); - else if (isnan(b)) - PUSH_F64(b); - else - PUSH_F64(wa_fmax(a, b)); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F64_COPYSIGN): - { - float64 a, b; - - b = POP_F64(); - a = POP_F64(); - PUSH_F64(signbit(b) ? -fabs(a) : fabs(a)); - HANDLE_OP_END (); - } - - /* conversions of i32 */ - HANDLE_OP (WASM_OP_I32_WRAP_I64): - { - int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL); - PUSH_I32(value); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_F32_GT) + { + DEF_OP_CMP(float32, F32, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_TRUNC_S_F32): - /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX, - since float/double values of ieee754 cannot precisely represent - all int32/uint32/int64/uint64 values, e.g.: - UINT32_MAX is 4294967295, but (float32)4294967295 is 4294967296.0f, - but not 4294967295.0f. */ - DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_TRUNC_U_F32): - DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_TRUNC_S_F64): - DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true); - /* frame_sp can't be moved in trunc function, we need to manually adjust - it if src and dst op's cell num is different */ - frame_sp--; - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_TRUNC_U_F64): - DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false); - frame_sp--; - HANDLE_OP_END (); - - /* conversions of i64 */ - HANDLE_OP (WASM_OP_I64_EXTEND_S_I32): - DEF_OP_CONVERT(int64, I64, int32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_EXTEND_U_I32): - DEF_OP_CONVERT(int64, I64, uint32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_S_F32): - DEF_OP_TRUNC_F32(-9223373136366403584.0f, 9223372036854775808.0f, - false, true); - frame_sp++; - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_U_F32): - DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, - false, false); - frame_sp++; - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_S_F64): - DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0, - false, true); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_U_F64): - DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, - false, false); - HANDLE_OP_END (); - - /* conversions of f32 */ - HANDLE_OP (WASM_OP_F32_CONVERT_S_I32): - DEF_OP_CONVERT(float32, F32, int32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_CONVERT_U_I32): - DEF_OP_CONVERT(float32, F32, uint32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_CONVERT_S_I64): - DEF_OP_CONVERT(float32, F32, int64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_CONVERT_U_I64): - DEF_OP_CONVERT(float32, F32, uint64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_DEMOTE_F64): - DEF_OP_CONVERT(float32, F32, float64, F64); - HANDLE_OP_END (); - - /* conversions of f64 */ - HANDLE_OP (WASM_OP_F64_CONVERT_S_I32): - DEF_OP_CONVERT(float64, F64, int32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_CONVERT_U_I32): - DEF_OP_CONVERT(float64, F64, uint32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_CONVERT_S_I64): - DEF_OP_CONVERT(float64, F64, int64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_CONVERT_U_I64): - DEF_OP_CONVERT(float64, F64, uint64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_PROMOTE_F32): - DEF_OP_CONVERT(float64, F64, float32, F32); - HANDLE_OP_END (); - - /* reinterpretations */ - HANDLE_OP (WASM_OP_I32_REINTERPRET_F32): - HANDLE_OP (WASM_OP_I64_REINTERPRET_F64): - HANDLE_OP (WASM_OP_F32_REINTERPRET_I32): - HANDLE_OP (WASM_OP_F64_REINTERPRET_I64): - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_EXTEND8_S): - DEF_OP_CONVERT(int32, I32, int8, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_EXTEND16_S): - DEF_OP_CONVERT(int32, I32, int16, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_EXTEND8_S): - DEF_OP_CONVERT(int64, I64, int8, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_EXTEND16_S): - DEF_OP_CONVERT(int64, I64, int16, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_EXTEND32_S): - DEF_OP_CONVERT(int64, I64, int32, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_MISC_PREFIX): - { - uint32 opcode1; - - read_leb_uint32(frame_ip, frame_ip_end, opcode1); - opcode = (uint8)opcode1; + HANDLE_OP(WASM_OP_F32_LE) + { + DEF_OP_CMP(float32, F32, <=); + HANDLE_OP_END(); + } - switch (opcode) { - case WASM_OP_I32_TRUNC_SAT_S_F32: - DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f, - true, true); - break; - case WASM_OP_I32_TRUNC_SAT_U_F32: - DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, - true, false); - break; - case WASM_OP_I32_TRUNC_SAT_S_F64: - DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, - true, true); - frame_sp--; - break; - case WASM_OP_I32_TRUNC_SAT_U_F64: - DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, - true, false); - frame_sp--; - break; - case WASM_OP_I64_TRUNC_SAT_S_F32: - DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f, 9223372036854775808.0f, - false, true); - frame_sp++; - break; - case WASM_OP_I64_TRUNC_SAT_U_F32: - DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f, - false, false); - frame_sp++; - break; - case WASM_OP_I64_TRUNC_SAT_S_F64: - DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0, 9223372036854775808.0, - false, true); - break; - case WASM_OP_I64_TRUNC_SAT_U_F64: - DEF_OP_TRUNC_SAT_F64(-1.0f, 18446744073709551616.0, - false, false); - break; -#if WASM_ENABLE_BULK_MEMORY != 0 - case WASM_OP_MEMORY_INIT: - { - uint32 addr, segment; - uint64 bytes, offset, seg_len; - uint8* data; + HANDLE_OP(WASM_OP_F32_GE) + { + DEF_OP_CMP(float32, F32, >=); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, segment); - /* skip memory index */ - frame_ip++; + /* comparison instructions of f64 */ + HANDLE_OP(WASM_OP_F64_EQ) + { + DEF_OP_CMP(float64, F64, ==); + HANDLE_OP_END(); + } - bytes = (uint64)(uint32)POP_I32(); - offset = (uint64)(uint32)POP_I32(); - addr = (uint32)POP_I32(); + HANDLE_OP(WASM_OP_F64_NE) + { + DEF_OP_CMP(float64, F64, !=); + HANDLE_OP_END(); + } - CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr); + HANDLE_OP(WASM_OP_F64_LT) + { + DEF_OP_CMP(float64, F64, <); + HANDLE_OP_END(); + } - seg_len = (uint64)module->module->data_segments[segment]->data_length; - data = module->module->data_segments[segment]->data; - if (offset + bytes > seg_len) - goto out_of_bounds; + HANDLE_OP(WASM_OP_F64_GT) + { + DEF_OP_CMP(float64, F64, >); + HANDLE_OP_END(); + } - bh_memcpy_s(maddr, linear_mem_size - addr, - data + offset, (uint32)bytes); - break; - } - case WASM_OP_DATA_DROP: - { - uint32 segment; + HANDLE_OP(WASM_OP_F64_LE) + { + DEF_OP_CMP(float64, F64, <=); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, segment); - module->module->data_segments[segment]->data_length = 0; + HANDLE_OP(WASM_OP_F64_GE) + { + DEF_OP_CMP(float64, F64, >=); + HANDLE_OP_END(); + } - break; - } - case WASM_OP_MEMORY_COPY: - { - uint32 dst, src, len; - uint8 *mdst, *msrc; + /* numberic instructions of i32 */ + HANDLE_OP(WASM_OP_I32_CLZ) + { + DEF_OP_BIT_COUNT(uint32, I32, clz32); + HANDLE_OP_END(); + } - frame_ip += 2; + HANDLE_OP(WASM_OP_I32_CTZ) + { + DEF_OP_BIT_COUNT(uint32, I32, ctz32); + HANDLE_OP_END(); + } - len = POP_I32(); - src = POP_I32(); - dst = POP_I32(); + HANDLE_OP(WASM_OP_I32_POPCNT) + { + DEF_OP_BIT_COUNT(uint32, I32, popcount32); + HANDLE_OP_END(); + } - CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc); - CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + HANDLE_OP(WASM_OP_I32_ADD) + { + DEF_OP_NUMERIC(uint32, uint32, I32, +); + HANDLE_OP_END(); + } - /* allowing the destination and source to overlap */ - bh_memmove_s(mdst, linear_mem_size - dst, - msrc, len); + HANDLE_OP(WASM_OP_I32_SUB) + { + DEF_OP_NUMERIC(uint32, uint32, I32, -); + HANDLE_OP_END(); + } - break; - } - case WASM_OP_MEMORY_FILL: - { - uint32 dst, len; - uint8 val, *mdst; - frame_ip++; + HANDLE_OP(WASM_OP_I32_MUL) + { + DEF_OP_NUMERIC(uint32, uint32, I32, *); + HANDLE_OP_END(); + } - len = POP_I32(); - val = POP_I32(); - dst = POP_I32(); + HANDLE_OP(WASM_OP_I32_DIV_S) + { + int32 a, b; + + b = POP_I32(); + a = POP_I32(); + if (a == (int32)0x80000000 && b == -1) { + wasm_set_exception(module, "integer overflow"); + goto got_exception; + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I32(a / b); + HANDLE_OP_END(); + } - CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + HANDLE_OP(WASM_OP_I32_DIV_U) + { + uint32 a, b; + + b = (uint32)POP_I32(); + a = (uint32)POP_I32(); + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I32(a / b); + HANDLE_OP_END(); + } - memset(mdst, val, len); + HANDLE_OP(WASM_OP_I32_REM_S) + { + int32 a, b; + + b = POP_I32(); + a = POP_I32(); + if (a == (int32)0x80000000 && b == -1) { + PUSH_I32(0); + HANDLE_OP_END(); + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I32(a % b); + HANDLE_OP_END(); + } - break; - } -#endif /* WASM_ENABLE_BULK_MEMORY */ -#if WASM_ENABLE_REF_TYPES != 0 - case WASM_OP_TABLE_INIT: - { - uint32 tbl_idx, elem_idx; - uint64 n, s, d; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I32_REM_U) + { + uint32 a, b; + + b = (uint32)POP_I32(); + a = (uint32)POP_I32(); + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I32(a % b); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, elem_idx); - bh_assert(elem_idx < module->module->table_seg_count); + HANDLE_OP(WASM_OP_I32_AND) + { + DEF_OP_NUMERIC(uint32, uint32, I32, &); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->module->table_count); + HANDLE_OP(WASM_OP_I32_OR) + { + DEF_OP_NUMERIC(uint32, uint32, I32, |); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + HANDLE_OP(WASM_OP_I32_XOR) + { + DEF_OP_NUMERIC(uint32, uint32, I32, ^); + HANDLE_OP_END(); + } - n = (uint32)POP_I32(); - s = (uint32)POP_I32(); - d = (uint32)POP_I32(); + HANDLE_OP(WASM_OP_I32_SHL) + { + DEF_OP_NUMERIC2(uint32, uint32, I32, <<); + HANDLE_OP_END(); + } - /* TODO: what if the element is not passive? */ + HANDLE_OP(WASM_OP_I32_SHR_S) + { + DEF_OP_NUMERIC2(int32, uint32, I32, >>); + HANDLE_OP_END(); + } - if (!n) { - break; - } + HANDLE_OP(WASM_OP_I32_SHR_U) + { + DEF_OP_NUMERIC2(uint32, uint32, I32, >>); + HANDLE_OP_END(); + } - if (n + s > module->module->table_segments[elem_idx].function_count - || d + n > tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } + HANDLE_OP(WASM_OP_I32_ROTL) + { + uint32 a, b; - if (module->module->table_segments[elem_idx].is_dropped) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } - - if (!wasm_elem_is_passive( - module->module->table_segments[elem_idx].mode)) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } - - bh_memcpy_s( - (uint8 *)(tbl_inst) - + offsetof(WASMTableInstance, base_addr) + d * sizeof(uint32), - (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), - module->module->table_segments[elem_idx].func_indexes + s, - (uint32)(n * sizeof(uint32))); - - break; - } - case WASM_OP_ELEM_DROP: - { - uint32 elem_idx; - read_leb_uint32(frame_ip, frame_ip_end, elem_idx); - bh_assert(elem_idx < module->module->table_seg_count); + b = (uint32)POP_I32(); + a = (uint32)POP_I32(); + PUSH_I32(rotl32(a, b)); + HANDLE_OP_END(); + } - module->module->table_segments[elem_idx].is_dropped = true; - break; - } - case WASM_OP_TABLE_COPY: - { - uint32 src_tbl_idx, dst_tbl_idx; - uint64 n, s, d; - WASMTableInstance *src_tbl_inst, *dst_tbl_inst; - - read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx); - bh_assert(dst_tbl_idx < module->table_count); - - dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx); - - read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx); - bh_assert(src_tbl_idx < module->table_count); - - src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx); - - n = (uint32)POP_I32(); - s = (uint32)POP_I32(); - d = (uint32)POP_I32(); - - if (s + n > dst_tbl_inst->cur_size - || d + n > src_tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } - - /* if s >= d, copy from front to back */ - /* if s < d, copy from back to front */ - /* merge all together */ - bh_memmove_s( - (uint8 *)(dst_tbl_inst) + offsetof(WASMTableInstance, base_addr) - + d * sizeof(uint32), - (uint32)((dst_tbl_inst->cur_size - d) * sizeof(uint32)), - (uint8 *)(src_tbl_inst) + offsetof(WASMTableInstance, base_addr) - + s * sizeof(uint32), - (uint32)(n * sizeof(uint32))); - break; - } - case WASM_OP_TABLE_GROW: - { - uint32 tbl_idx, n, init_val, orig_tbl_sz; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I32_ROTR) + { + uint32 a, b; - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->table_count); + b = (uint32)POP_I32(); + a = (uint32)POP_I32(); + PUSH_I32(rotr32(a, b)); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + /* numberic instructions of i64 */ + HANDLE_OP(WASM_OP_I64_CLZ) + { + DEF_OP_BIT_COUNT(uint64, I64, clz64); + HANDLE_OP_END(); + } - orig_tbl_sz = tbl_inst->cur_size; + HANDLE_OP(WASM_OP_I64_CTZ) + { + DEF_OP_BIT_COUNT(uint64, I64, ctz64); + HANDLE_OP_END(); + } - n = POP_I32(); - init_val = POP_I32(); + HANDLE_OP(WASM_OP_I64_POPCNT) + { + DEF_OP_BIT_COUNT(uint64, I64, popcount64); + HANDLE_OP_END(); + } - if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) { - PUSH_I32(-1); - } - else { - PUSH_I32(orig_tbl_sz); - } - break; - } - case WASM_OP_TABLE_SIZE: - { - uint32 tbl_idx; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I64_ADD) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, +); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->table_count); + HANDLE_OP(WASM_OP_I64_SUB) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, -); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + HANDLE_OP(WASM_OP_I64_MUL) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, *); + HANDLE_OP_END(); + } - PUSH_I32(tbl_inst->cur_size); - break; - } - case WASM_OP_TABLE_FILL: - { - uint32 tbl_idx, n, val, i; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I64_DIV_S) + { + int64 a, b; + + b = POP_I64(); + a = POP_I64(); + if (a == (int64)0x8000000000000000LL && b == -1) { + wasm_set_exception(module, "integer overflow"); + goto got_exception; + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I64(a / b); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); - bh_assert(tbl_idx < module->table_count); + HANDLE_OP(WASM_OP_I64_DIV_U) + { + uint64 a, b; + + b = (uint64)POP_I64(); + a = (uint64)POP_I64(); + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I64(a / b); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + HANDLE_OP(WASM_OP_I64_REM_S) + { + int64 a, b; + + b = POP_I64(); + a = POP_I64(); + if (a == (int64)0x8000000000000000LL && b == -1) { + PUSH_I64(0); + HANDLE_OP_END(); + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I64(a % b); + HANDLE_OP_END(); + } - n = POP_I32(); - val = POP_I32(); - i = POP_I32(); + HANDLE_OP(WASM_OP_I64_REM_U) + { + uint64 a, b; + + b = (uint64)POP_I64(); + a = (uint64)POP_I64(); + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUSH_I64(a % b); + HANDLE_OP_END(); + } - /* TODO: what if the element is not passive? */ - /* TODO: what if the element is dropped? */ + HANDLE_OP(WASM_OP_I64_AND) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, &); + HANDLE_OP_END(); + } - if (i + n > tbl_inst->cur_size) { - /* TODO: verify warning content */ - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; - } + HANDLE_OP(WASM_OP_I64_OR) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, |); + HANDLE_OP_END(); + } - for (; n != 0; i++, n--) { - ((uint32 *)(tbl_inst->base_addr))[i] = val; - } + HANDLE_OP(WASM_OP_I64_XOR) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, ^); + HANDLE_OP_END(); + } - break; - } -#endif /* WASM_ENABLE_REF_TYPES */ - default: - wasm_set_exception(module, "unsupported opcode"); - goto got_exception; - } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_SHL) + { + DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<); + HANDLE_OP_END(); + } -#if WASM_ENABLE_SHARED_MEMORY != 0 - HANDLE_OP (WASM_OP_ATOMIC_PREFIX): - { - uint32 offset, align, addr; + HANDLE_OP(WASM_OP_I64_SHR_S) + { + DEF_OP_NUMERIC2_64(int64, uint64, I64, >>); + HANDLE_OP_END(); + } - opcode = *frame_ip++; + HANDLE_OP(WASM_OP_I64_SHR_U) + { + DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>); + HANDLE_OP_END(); + } - read_leb_uint32(frame_ip, frame_ip_end, align); - read_leb_uint32(frame_ip, frame_ip_end, offset); - switch (opcode) { - case WASM_OP_ATOMIC_NOTIFY: - { - uint32 count, ret; + HANDLE_OP(WASM_OP_I64_ROTL) + { + uint64 a, b; - count = POP_I32(); - addr = POP_I32(); - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); + b = (uint64)POP_I64(); + a = (uint64)POP_I64(); + PUSH_I64(rotl64(a, b)); + HANDLE_OP_END(); + } - ret = wasm_runtime_atomic_notify((WASMModuleInstanceCommon*)module, - maddr, count); - bh_assert((int32)ret >= 0); + HANDLE_OP(WASM_OP_I64_ROTR) + { + uint64 a, b; - PUSH_I32(ret); - break; - } - case WASM_OP_ATOMIC_WAIT32: - { - uint64 timeout; - uint32 expect, addr, ret; - - timeout = POP_I64(); - expect = POP_I32(); - addr = POP_I32(); - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - ret = wasm_runtime_atomic_wait((WASMModuleInstanceCommon*)module, maddr, - (uint64)expect, timeout, false); - if (ret == (uint32)-1) - goto got_exception; - - PUSH_I32(ret); - break; - } - case WASM_OP_ATOMIC_WAIT64: - { - uint64 timeout, expect; - uint32 ret; - - timeout = POP_I64(); - expect = POP_I64(); - addr = POP_I32(); - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - ret = wasm_runtime_atomic_wait((WASMModuleInstanceCommon*)module, - maddr, expect, timeout, true); - if (ret == (uint32)-1) - goto got_exception; - - PUSH_I32(ret); - break; - } - - case WASM_OP_ATOMIC_I32_LOAD: - case WASM_OP_ATOMIC_I32_LOAD8_U: - case WASM_OP_ATOMIC_I32_LOAD16_U: - { - uint32 readv; - - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = (uint32)(*(uint8*)maddr); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = (uint32)LOAD_U16(maddr); - os_mutex_unlock(&memory->mem_lock); - } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = LOAD_I32(maddr); - os_mutex_unlock(&memory->mem_lock); - } - - PUSH_I32(readv); - break; - } - - case WASM_OP_ATOMIC_I64_LOAD: - case WASM_OP_ATOMIC_I64_LOAD8_U: - case WASM_OP_ATOMIC_I64_LOAD16_U: - case WASM_OP_ATOMIC_I64_LOAD32_U: - { - uint64 readv; - - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)(*(uint8*)maddr); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U16(maddr); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U32(maddr); - os_mutex_unlock(&memory->mem_lock); - } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - readv = LOAD_I64(maddr); - os_mutex_unlock(&memory->mem_lock); - } - - PUSH_I64(readv); - break; - } - - case WASM_OP_ATOMIC_I32_STORE: - case WASM_OP_ATOMIC_I32_STORE8: - case WASM_OP_ATOMIC_I32_STORE16: - { - uint32 sval; - - sval = (uint32)POP_I32(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I32_STORE8) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - *(uint8*)maddr = (uint8)sval; - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I32_STORE16) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - STORE_U16(maddr, (uint16)sval); - os_mutex_unlock(&memory->mem_lock); - } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - STORE_U32(maddr, frame_sp[1]); - os_mutex_unlock(&memory->mem_lock); + b = (uint64)POP_I64(); + a = (uint64)POP_I64(); + PUSH_I64(rotr64(a, b)); + HANDLE_OP_END(); } - break; - } - - case WASM_OP_ATOMIC_I64_STORE: - case WASM_OP_ATOMIC_I64_STORE8: - case WASM_OP_ATOMIC_I64_STORE16: - case WASM_OP_ATOMIC_I64_STORE32: - { - uint64 sval; - - sval = (uint64)POP_I64(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I64_STORE8) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - *(uint8*)maddr = (uint8)sval; - os_mutex_unlock(&memory->mem_lock); - } - else if(opcode == WASM_OP_ATOMIC_I64_STORE16) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - STORE_U16(maddr, (uint16)sval); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I64_STORE32) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - STORE_U32(maddr, (uint32)sval); - os_mutex_unlock(&memory->mem_lock); - } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - os_mutex_lock(&memory->mem_lock); - STORE_U32(maddr, frame_sp[1]); - STORE_U32(maddr + 4, frame_sp[2]); - os_mutex_unlock(&memory->mem_lock); + + /* numberic instructions of f32 */ + HANDLE_OP(WASM_OP_F32_ABS) + { + DEF_OP_MATH(float32, F32, fabs); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_NEG) + { + uint32 u32 = frame_sp[-1]; + uint32 sign_bit = u32 & ((uint32)1 << 31); + if (sign_bit) + frame_sp[-1] = u32 & ~((uint32)1 << 31); + else + frame_sp[-1] = u32 | ((uint32)1 << 31); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CEIL) + { + DEF_OP_MATH(float32, F32, ceil); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_FLOOR) + { + DEF_OP_MATH(float32, F32, floor); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_TRUNC) + { + DEF_OP_MATH(float32, F32, trunc); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_NEAREST) + { + DEF_OP_MATH(float32, F32, rint); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_SQRT) + { + DEF_OP_MATH(float32, F32, sqrt); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_ADD) + { + DEF_OP_NUMERIC(float32, float32, F32, +); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_SUB) + { + DEF_OP_NUMERIC(float32, float32, F32, -); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_MUL) + { + DEF_OP_NUMERIC(float32, float32, F32, *); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_DIV) + { + DEF_OP_NUMERIC(float32, float32, F32, /); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_MIN) + { + float32 a, b; + + b = POP_F32(); + a = POP_F32(); + + if (isnan(a)) + PUSH_F32(a); + else if (isnan(b)) + PUSH_F32(b); + else + PUSH_F32(wa_fmin(a, b)); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_MAX) + { + float32 a, b; + + b = POP_F32(); + a = POP_F32(); + + if (isnan(a)) + PUSH_F32(a); + else if (isnan(b)) + PUSH_F32(b); + else + PUSH_F32(wa_fmax(a, b)); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_COPYSIGN) + { + float32 a, b; + + b = POP_F32(); + a = POP_F32(); + PUSH_F32(signbit(b) ? -fabs(a) : fabs(a)); + HANDLE_OP_END(); + } + + /* numberic instructions of f64 */ + HANDLE_OP(WASM_OP_F64_ABS) + { + DEF_OP_MATH(float64, F64, fabs); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_NEG) + { + uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2); + uint64 sign_bit = u64 & (((uint64)1) << 63); + if (sign_bit) + PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63))); + else + PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63))); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CEIL) + { + DEF_OP_MATH(float64, F64, ceil); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_FLOOR) + { + DEF_OP_MATH(float64, F64, floor); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_TRUNC) + { + DEF_OP_MATH(float64, F64, trunc); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_NEAREST) + { + DEF_OP_MATH(float64, F64, rint); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_SQRT) + { + DEF_OP_MATH(float64, F64, sqrt); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_ADD) + { + DEF_OP_NUMERIC_64(float64, float64, F64, +); + HANDLE_OP_END(); } - break; - } - - case WASM_OP_ATOMIC_RMW_I32_CMPXCHG: - case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U: - case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U: - { - uint32 readv, sval, expect; - - sval = POP_I32(); - expect = POP_I32(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = (uint32)(*(uint8*)maddr); - if (readv == expect) - *(uint8*)maddr = (uint8)(sval); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = (uint32)LOAD_U16(maddr); - if (readv == expect) - STORE_U16(maddr, (uint16)(sval)); - os_mutex_unlock(&memory->mem_lock); - } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = LOAD_I32(maddr); - if (readv == expect) - STORE_U32(maddr, sval); - os_mutex_unlock(&memory->mem_lock); - } - PUSH_I32(readv); - break; - } - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG: - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U: - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U: - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U: - { - uint64 readv, sval, expect; - - sval = (uint64)POP_I64(); - expect = (uint64)POP_I64(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)(*(uint8*)maddr); - if (readv == expect) - *(uint8*)maddr = (uint8)(sval); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U16(maddr); - if (readv == expect) - STORE_U16(maddr, (uint16)(sval)); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U32(maddr); - if (readv == expect) - STORE_U32(maddr, (uint32)(sval)); - os_mutex_unlock(&memory->mem_lock); - } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_I64(maddr); - if (readv == expect) { - STORE_I64(maddr, sval); - } - os_mutex_unlock(&memory->mem_lock); - } - PUSH_I64(readv); - break; - } - - DEF_ATOMIC_RMW_OPCODE(ADD, +); - DEF_ATOMIC_RMW_OPCODE(SUB, -); - DEF_ATOMIC_RMW_OPCODE(AND, &); - DEF_ATOMIC_RMW_OPCODE(OR, |); - DEF_ATOMIC_RMW_OPCODE(XOR, ^); - /* xchg, ignore the read value, and store the given value: - readv * 0 + sval */ - DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +); - } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_F64_SUB) + { + DEF_OP_NUMERIC_64(float64, float64, F64, -); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_MUL) + { + DEF_OP_NUMERIC_64(float64, float64, F64, *); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_DIV) + { + DEF_OP_NUMERIC_64(float64, float64, F64, /); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_MIN) + { + float64 a, b; + + b = POP_F64(); + a = POP_F64(); + + if (isnan(a)) + PUSH_F64(a); + else if (isnan(b)) + PUSH_F64(b); + else + PUSH_F64(wa_fmin(a, b)); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_MAX) + { + float64 a, b; + + b = POP_F64(); + a = POP_F64(); + + if (isnan(a)) + PUSH_F64(a); + else if (isnan(b)) + PUSH_F64(b); + else + PUSH_F64(wa_fmax(a, b)); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_COPYSIGN) + { + float64 a, b; + + b = POP_F64(); + a = POP_F64(); + PUSH_F64(signbit(b) ? -fabs(a) : fabs(a)); + HANDLE_OP_END(); + } + + /* conversions of i32 */ + HANDLE_OP(WASM_OP_I32_WRAP_I64) + { + int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL); + PUSH_I32(value); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_S_F32) + { + /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX, + since float/double values of ieee754 cannot precisely + represent all int32/uint32/int64/uint64 values, e.g. + UINT32_MAX is 4294967295, but (float32)4294967295 is + 4294967296.0f, but not 4294967295.0f. */ + DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_U_F32) + { + DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_S_F64) + { + DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true); + /* frame_sp can't be moved in trunc function, we need to + manually adjust it if src and dst op's cell num is + different */ + frame_sp--; + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_U_F64) + { + DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false); + frame_sp--; + HANDLE_OP_END(); + } + + /* conversions of i64 */ + HANDLE_OP(WASM_OP_I64_EXTEND_S_I32) + { + DEF_OP_CONVERT(int64, I64, int32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND_U_I32) + { + DEF_OP_CONVERT(int64, I64, uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_S_F32) + { + DEF_OP_TRUNC_F32(-9223373136366403584.0f, + 9223372036854775808.0f, false, true); + frame_sp++; + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_U_F32) + { + DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false); + frame_sp++; + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_S_F64) + { + DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0, + false, true); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_U_F64) + { + DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false); + HANDLE_OP_END(); + } + + /* conversions of f32 */ + HANDLE_OP(WASM_OP_F32_CONVERT_S_I32) + { + DEF_OP_CONVERT(float32, F32, int32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CONVERT_U_I32) + { + DEF_OP_CONVERT(float32, F32, uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CONVERT_S_I64) + { + DEF_OP_CONVERT(float32, F32, int64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CONVERT_U_I64) + { + DEF_OP_CONVERT(float32, F32, uint64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_DEMOTE_F64) + { + DEF_OP_CONVERT(float32, F32, float64, F64); + HANDLE_OP_END(); + } + + /* conversions of f64 */ + HANDLE_OP(WASM_OP_F64_CONVERT_S_I32) + { + DEF_OP_CONVERT(float64, F64, int32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CONVERT_U_I32) + { + DEF_OP_CONVERT(float64, F64, uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CONVERT_S_I64) + { + DEF_OP_CONVERT(float64, F64, int64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CONVERT_U_I64) + { + DEF_OP_CONVERT(float64, F64, uint64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_PROMOTE_F32) + { + DEF_OP_CONVERT(float64, F64, float32, F32); + HANDLE_OP_END(); + } + + /* reinterpretations */ + HANDLE_OP(WASM_OP_I32_REINTERPRET_F32) + HANDLE_OP(WASM_OP_I64_REINTERPRET_F64) + HANDLE_OP(WASM_OP_F32_REINTERPRET_I32) + HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); } + + HANDLE_OP(WASM_OP_I32_EXTEND8_S) + { + DEF_OP_CONVERT(int32, I32, int8, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_EXTEND16_S) + { + DEF_OP_CONVERT(int32, I32, int16, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND8_S) + { + DEF_OP_CONVERT(int64, I64, int8, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND16_S) + { + DEF_OP_CONVERT(int64, I64, int16, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND32_S) + { + DEF_OP_CONVERT(int64, I64, int32, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_MISC_PREFIX) + { + uint32 opcode1; + + read_leb_uint32(frame_ip, frame_ip_end, opcode1); + opcode = (uint8)opcode1; + + switch (opcode) { + case WASM_OP_I32_TRUNC_SAT_S_F32: + DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f, + true, true); + break; + case WASM_OP_I32_TRUNC_SAT_U_F32: + DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false); + break; + case WASM_OP_I32_TRUNC_SAT_S_F64: + DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true, + true); + frame_sp--; + break; + case WASM_OP_I32_TRUNC_SAT_U_F64: + DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false); + frame_sp--; + break; + case WASM_OP_I64_TRUNC_SAT_S_F32: + DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f, + 9223372036854775808.0f, false, + true); + frame_sp++; + break; + case WASM_OP_I64_TRUNC_SAT_U_F32: + DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f, + false, false); + frame_sp++; + break; + case WASM_OP_I64_TRUNC_SAT_S_F64: + DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0, + 9223372036854775808.0, false, + true); + break; + case WASM_OP_I64_TRUNC_SAT_U_F64: + DEF_OP_TRUNC_SAT_F64(-1.0f, 18446744073709551616.0, + false, false); + break; +#if WASM_ENABLE_BULK_MEMORY != 0 + case WASM_OP_MEMORY_INIT: + { + uint32 addr, segment; + uint64 bytes, offset, seg_len; + uint8 *data; + + read_leb_uint32(frame_ip, frame_ip_end, segment); + /* skip memory index */ + frame_ip++; + + bytes = (uint64)(uint32)POP_I32(); + offset = (uint64)(uint32)POP_I32(); + addr = (uint32)POP_I32(); + + CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr); + + seg_len = (uint64)module->module->data_segments[segment] + ->data_length; + data = module->module->data_segments[segment]->data; + if (offset + bytes > seg_len) + goto out_of_bounds; + + bh_memcpy_s(maddr, linear_mem_size - addr, + data + offset, (uint32)bytes); + break; + } + case WASM_OP_DATA_DROP: + { + uint32 segment; + + read_leb_uint32(frame_ip, frame_ip_end, segment); + module->module->data_segments[segment]->data_length = 0; + + break; + } + case WASM_OP_MEMORY_COPY: + { + uint32 dst, src, len; + uint8 *mdst, *msrc; + + frame_ip += 2; + + len = POP_I32(); + src = POP_I32(); + dst = POP_I32(); + + CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc); + CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + + /* allowing the destination and source to overlap */ + bh_memmove_s(mdst, linear_mem_size - dst, msrc, len); + + break; + } + case WASM_OP_MEMORY_FILL: + { + uint32 dst, len; + uint8 val, *mdst; + frame_ip++; + + len = POP_I32(); + val = POP_I32(); + dst = POP_I32(); + + CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + + memset(mdst, val, len); + + break; + } +#endif /* WASM_ENABLE_BULK_MEMORY */ +#if WASM_ENABLE_REF_TYPES != 0 + case WASM_OP_TABLE_INIT: + { + uint32 tbl_idx, elem_idx; + uint64 n, s, d; + WASMTableInstance *tbl_inst; + + read_leb_uint32(frame_ip, frame_ip_end, elem_idx); + bh_assert(elem_idx < module->module->table_seg_count); + + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + n = (uint32)POP_I32(); + s = (uint32)POP_I32(); + d = (uint32)POP_I32(); + + /* TODO: what if the element is not passive? */ + + if (!n) { + break; + } + + if (n + s > module->module->table_segments[elem_idx] + .function_count + || d + n > tbl_inst->cur_size) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + if (module->module->table_segments[elem_idx] + .is_dropped) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + if (!wasm_elem_is_passive( + module->module->table_segments[elem_idx] + .mode)) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + bh_memcpy_s( + (uint8 *)(tbl_inst) + + offsetof(WASMTableInstance, base_addr) + + d * sizeof(uint32), + (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), + module->module->table_segments[elem_idx] + .func_indexes + + s, + (uint32)(n * sizeof(uint32))); + + break; + } + case WASM_OP_ELEM_DROP: + { + uint32 elem_idx; + read_leb_uint32(frame_ip, frame_ip_end, elem_idx); + bh_assert(elem_idx < module->module->table_seg_count); + + module->module->table_segments[elem_idx].is_dropped = + true; + break; + } + case WASM_OP_TABLE_COPY: + { + uint32 src_tbl_idx, dst_tbl_idx; + uint64 n, s, d; + WASMTableInstance *src_tbl_inst, *dst_tbl_inst; + + read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx); + bh_assert(dst_tbl_idx < module->table_count); + + dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx); + + read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx); + bh_assert(src_tbl_idx < module->table_count); + + src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx); + + n = (uint32)POP_I32(); + s = (uint32)POP_I32(); + d = (uint32)POP_I32(); + + if (s + n > dst_tbl_inst->cur_size + || d + n > src_tbl_inst->cur_size) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + /* if s >= d, copy from front to back */ + /* if s < d, copy from back to front */ + /* merge all together */ + bh_memmove_s( + (uint8 *)(dst_tbl_inst) + + offsetof(WASMTableInstance, base_addr) + + d * sizeof(uint32), + (uint32)((dst_tbl_inst->cur_size - d) + * sizeof(uint32)), + (uint8 *)(src_tbl_inst) + + offsetof(WASMTableInstance, base_addr) + + s * sizeof(uint32), + (uint32)(n * sizeof(uint32))); + break; + } + case WASM_OP_TABLE_GROW: + { + uint32 tbl_idx, n, init_val, orig_tbl_sz; + WASMTableInstance *tbl_inst; + + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + orig_tbl_sz = tbl_inst->cur_size; + + n = POP_I32(); + init_val = POP_I32(); + + if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) { + PUSH_I32(-1); + } + else { + PUSH_I32(orig_tbl_sz); + } + break; + } + case WASM_OP_TABLE_SIZE: + { + uint32 tbl_idx; + WASMTableInstance *tbl_inst; + + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + PUSH_I32(tbl_inst->cur_size); + break; + } + case WASM_OP_TABLE_FILL: + { + uint32 tbl_idx, n, val, i; + WASMTableInstance *tbl_inst; + + read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + n = POP_I32(); + val = POP_I32(); + i = POP_I32(); + + /* TODO: what if the element is not passive? */ + /* TODO: what if the element is dropped? */ + + if (i + n > tbl_inst->cur_size) { + /* TODO: verify warning content */ + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + for (; n != 0; i++, n--) { + ((uint32 *)(tbl_inst->base_addr))[i] = val; + } + + break; + } +#endif /* WASM_ENABLE_REF_TYPES */ + default: + wasm_set_exception(module, "unsupported opcode"); + goto got_exception; + } + HANDLE_OP_END(); + } + +#if WASM_ENABLE_SHARED_MEMORY != 0 + HANDLE_OP(WASM_OP_ATOMIC_PREFIX) + { + uint32 offset, align, addr; + + opcode = *frame_ip++; + + read_leb_uint32(frame_ip, frame_ip_end, align); + read_leb_uint32(frame_ip, frame_ip_end, offset); + switch (opcode) { + case WASM_OP_ATOMIC_NOTIFY: + { + uint32 count, ret; + + count = POP_I32(); + addr = POP_I32(); + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + ret = wasm_runtime_atomic_notify( + (WASMModuleInstanceCommon *)module, maddr, count); + bh_assert((int32)ret >= 0); + + PUSH_I32(ret); + break; + } + case WASM_OP_ATOMIC_WAIT32: + { + uint64 timeout; + uint32 expect, addr, ret; + + timeout = POP_I64(); + expect = POP_I32(); + addr = POP_I32(); + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + ret = wasm_runtime_atomic_wait( + (WASMModuleInstanceCommon *)module, maddr, + (uint64)expect, timeout, false); + if (ret == (uint32)-1) + goto got_exception; + + PUSH_I32(ret); + break; + } + case WASM_OP_ATOMIC_WAIT64: + { + uint64 timeout, expect; + uint32 ret; + + timeout = POP_I64(); + expect = POP_I64(); + addr = POP_I32(); + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + ret = wasm_runtime_atomic_wait( + (WASMModuleInstanceCommon *)module, maddr, expect, + timeout, true); + if (ret == (uint32)-1) + goto got_exception; + + PUSH_I32(ret); + break; + } + + case WASM_OP_ATOMIC_I32_LOAD: + case WASM_OP_ATOMIC_I32_LOAD8_U: + case WASM_OP_ATOMIC_I32_LOAD16_U: + { + uint32 readv; + + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = (uint32)(*(uint8 *)maddr); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = (uint32)LOAD_U16(maddr); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = LOAD_I32(maddr); + os_mutex_unlock(&memory->mem_lock); + } + + PUSH_I32(readv); + break; + } + + case WASM_OP_ATOMIC_I64_LOAD: + case WASM_OP_ATOMIC_I64_LOAD8_U: + case WASM_OP_ATOMIC_I64_LOAD16_U: + case WASM_OP_ATOMIC_I64_LOAD32_U: + { + uint64 readv; + + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = (uint64)(*(uint8 *)maddr); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U16(maddr); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U32(maddr); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + readv = LOAD_I64(maddr); + os_mutex_unlock(&memory->mem_lock); + } + + PUSH_I64(readv); + break; + } + + case WASM_OP_ATOMIC_I32_STORE: + case WASM_OP_ATOMIC_I32_STORE8: + case WASM_OP_ATOMIC_I32_STORE16: + { + uint32 sval; + + sval = (uint32)POP_I32(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I32_STORE8) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + *(uint8 *)maddr = (uint8)sval; + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I32_STORE16) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + STORE_U16(maddr, (uint16)sval); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + STORE_U32(maddr, frame_sp[1]); + os_mutex_unlock(&memory->mem_lock); + } + break; + } + + case WASM_OP_ATOMIC_I64_STORE: + case WASM_OP_ATOMIC_I64_STORE8: + case WASM_OP_ATOMIC_I64_STORE16: + case WASM_OP_ATOMIC_I64_STORE32: + { + uint64 sval; + + sval = (uint64)POP_I64(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I64_STORE8) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + *(uint8 *)maddr = (uint8)sval; + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_STORE16) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + STORE_U16(maddr, (uint16)sval); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_STORE32) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + STORE_U32(maddr, (uint32)sval); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + os_mutex_lock(&memory->mem_lock); + STORE_U32(maddr, frame_sp[1]); + STORE_U32(maddr + 4, frame_sp[2]); + os_mutex_unlock(&memory->mem_lock); + } + break; + } + + case WASM_OP_ATOMIC_RMW_I32_CMPXCHG: + case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U: + case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U: + { + uint32 readv, sval, expect; + + sval = POP_I32(); + expect = POP_I32(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = (uint32)(*(uint8 *)maddr); + if (readv == expect) + *(uint8 *)maddr = (uint8)(sval); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = (uint32)LOAD_U16(maddr); + if (readv == expect) + STORE_U16(maddr, (uint16)(sval)); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = LOAD_I32(maddr); + if (readv == expect) + STORE_U32(maddr, sval); + os_mutex_unlock(&memory->mem_lock); + } + PUSH_I32(readv); + break; + } + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG: + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U: + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U: + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U: + { + uint64 readv, sval, expect; + + sval = (uint64)POP_I64(); + expect = (uint64)POP_I64(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)(*(uint8 *)maddr); + if (readv == expect) + *(uint8 *)maddr = (uint8)(sval); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U16(maddr); + if (readv == expect) + STORE_U16(maddr, (uint16)(sval)); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U32(maddr); + if (readv == expect) + STORE_U32(maddr, (uint32)(sval)); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_I64(maddr); + if (readv == expect) { + STORE_I64(maddr, sval); + } + os_mutex_unlock(&memory->mem_lock); + } + PUSH_I64(readv); + break; + } + + DEF_ATOMIC_RMW_OPCODE(ADD, +); + DEF_ATOMIC_RMW_OPCODE(SUB, -); + DEF_ATOMIC_RMW_OPCODE(AND, &); + DEF_ATOMIC_RMW_OPCODE(OR, |); + DEF_ATOMIC_RMW_OPCODE(XOR, ^); + /* xchg, ignore the read value, and store the given + value: readv * 0 + sval */ + DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +); + } + + HANDLE_OP_END(); + } #endif - HANDLE_OP (WASM_OP_IMPDEP): - frame = prev_frame; - frame_ip = frame->ip; - frame_sp = frame->sp; - frame_csp = frame->csp; - goto call_func_from_entry; + HANDLE_OP(WASM_OP_IMPDEP) + { + frame = prev_frame; + frame_ip = frame->ip; + frame_sp = frame->sp; + frame_csp = frame->csp; + goto call_func_from_entry; + } + #if WASM_ENABLE_DEBUG_INTERP != 0 - HANDLE_OP (DEBUG_OP_BREAK): - { - wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP); - exec_env->suspend_flags.flags |= 2; - frame_ip--; - SYNC_ALL_TO_FRAME(); - CHECK_SUSPEND_FLAGS(); - HANDLE_OP_END (); - } + HANDLE_OP(DEBUG_OP_BREAK) + { + wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP); + exec_env->suspend_flags.flags |= 2; + frame_ip--; + SYNC_ALL_TO_FRAME(); + CHECK_SUSPEND_FLAGS(); + HANDLE_OP_END(); + } #endif #if WASM_ENABLE_LABELS_AS_VALUES == 0 - default: - wasm_set_exception(module, "unsupported opcode"); - goto got_exception; - } + default: + wasm_set_exception(module, "unsupported opcode"); + goto got_exception; + } #endif #if WASM_ENABLE_LABELS_AS_VALUES != 0 - HANDLE_OP (WASM_OP_UNUSED_0x06): - HANDLE_OP (WASM_OP_UNUSED_0x07): - HANDLE_OP (WASM_OP_UNUSED_0x08): - HANDLE_OP (WASM_OP_UNUSED_0x09): - HANDLE_OP (WASM_OP_UNUSED_0x0a): + HANDLE_OP(WASM_OP_UNUSED_0x06) + HANDLE_OP(WASM_OP_UNUSED_0x07) + HANDLE_OP(WASM_OP_UNUSED_0x08) + HANDLE_OP(WASM_OP_UNUSED_0x09) + HANDLE_OP(WASM_OP_UNUSED_0x0a) #if WASM_ENABLE_TAIL_CALL == 0 - HANDLE_OP (WASM_OP_RETURN_CALL): - HANDLE_OP (WASM_OP_RETURN_CALL_INDIRECT): + HANDLE_OP(WASM_OP_RETURN_CALL) + HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT) #endif #if WASM_ENABLE_SHARED_MEMORY == 0 - HANDLE_OP (WASM_OP_ATOMIC_PREFIX): + HANDLE_OP(WASM_OP_ATOMIC_PREFIX) #endif #if WASM_ENABLE_REF_TYPES == 0 - HANDLE_OP (WASM_OP_SELECT_T): - HANDLE_OP (WASM_OP_TABLE_GET): - HANDLE_OP (WASM_OP_TABLE_SET): - HANDLE_OP (WASM_OP_REF_NULL): - HANDLE_OP (WASM_OP_REF_IS_NULL): - HANDLE_OP (WASM_OP_REF_FUNC): + HANDLE_OP(WASM_OP_SELECT_T) + HANDLE_OP(WASM_OP_TABLE_GET) + HANDLE_OP(WASM_OP_TABLE_SET) + HANDLE_OP(WASM_OP_REF_NULL) + HANDLE_OP(WASM_OP_REF_IS_NULL) + HANDLE_OP(WASM_OP_REF_FUNC) #endif - HANDLE_OP (WASM_OP_UNUSED_0x14): - HANDLE_OP (WASM_OP_UNUSED_0x15): - HANDLE_OP (WASM_OP_UNUSED_0x16): - HANDLE_OP (WASM_OP_UNUSED_0x17): - HANDLE_OP (WASM_OP_UNUSED_0x18): - HANDLE_OP (WASM_OP_UNUSED_0x19): - HANDLE_OP (WASM_OP_UNUSED_0x27): - /* Used by fast interpreter */ - HANDLE_OP (EXT_OP_SET_LOCAL_FAST_I64): - HANDLE_OP (EXT_OP_TEE_LOCAL_FAST_I64): - HANDLE_OP (EXT_OP_COPY_STACK_TOP): - HANDLE_OP (EXT_OP_COPY_STACK_TOP_I64): - HANDLE_OP (EXT_OP_COPY_STACK_VALUES): - { - wasm_set_exception(module, "unsupported opcode"); - goto got_exception; - } + HANDLE_OP(WASM_OP_UNUSED_0x14) + HANDLE_OP(WASM_OP_UNUSED_0x15) + HANDLE_OP(WASM_OP_UNUSED_0x16) + HANDLE_OP(WASM_OP_UNUSED_0x17) + HANDLE_OP(WASM_OP_UNUSED_0x18) + HANDLE_OP(WASM_OP_UNUSED_0x19) + HANDLE_OP(WASM_OP_UNUSED_0x27) + /* Used by fast interpreter */ + HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64) + HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64) + HANDLE_OP(EXT_OP_COPY_STACK_TOP) + HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64) + HANDLE_OP(EXT_OP_COPY_STACK_VALUES) + { + wasm_set_exception(module, "unsupported opcode"); + goto got_exception; + } #endif #if WASM_ENABLE_LABELS_AS_VALUES == 0 - continue; + continue; #else - FETCH_OPCODE_AND_DISPATCH (); + FETCH_OPCODE_AND_DISPATCH(); #endif #if WASM_ENABLE_TAIL_CALL != 0 - call_func_from_return_call: - POP(cur_func->param_cell_num); - word_copy(frame->lp, frame_sp, cur_func->param_cell_num); - FREE_FRAME(exec_env, frame); - wasm_exec_env_set_cur_frame(exec_env, - (WASMRuntimeFrame *)prev_frame); - goto call_func_from_entry; + call_func_from_return_call: + { + POP(cur_func->param_cell_num); + word_copy(frame->lp, frame_sp, cur_func->param_cell_num); + FREE_FRAME(exec_env, frame); + wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame); + goto call_func_from_entry; + } #endif - call_func_from_interp: - /* Only do the copy when it's called from interpreter. */ + call_func_from_interp: { - WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env); - POP(cur_func->param_cell_num); - SYNC_ALL_TO_FRAME(); - word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num); - prev_frame = frame; + /* Only do the copy when it's called from interpreter. */ + WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env); + POP(cur_func->param_cell_num); + SYNC_ALL_TO_FRAME(); + word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num); + prev_frame = frame; } - call_func_from_entry: + call_func_from_entry: { - if (cur_func->is_import_func) { + if (cur_func->is_import_func) { #if WASM_ENABLE_MULTI_MODULE != 0 - if (cur_func->import_func_inst) { - wasm_interp_call_func_import(module, exec_env, cur_func, - prev_frame); - } - else + if (cur_func->import_func_inst) { + wasm_interp_call_func_import(module, exec_env, cur_func, + prev_frame); + } + else #endif - { - wasm_interp_call_func_native(module, exec_env, cur_func, - prev_frame); - } - - prev_frame = frame->prev_frame; - cur_func = frame->function; - UPDATE_ALL_FROM_FRAME(); - - /* update memory instance ptr and memory size */ - memory = module->default_memory; - if (memory) - linear_mem_size = num_bytes_per_page * memory->cur_page_count; - if (wasm_get_exception(module)) - goto got_exception; - } - else { - WASMFunction *cur_wasm_func = cur_func->u.func; - WASMType *func_type; - - func_type = cur_wasm_func->func_type; - - all_cell_num = (uint64)cur_func->param_cell_num - + (uint64)cur_func->local_cell_num - + (uint64)cur_wasm_func->max_stack_cell_num - + ((uint64)cur_wasm_func->max_block_num) * sizeof(WASMBranchBlock) / 4; - if (all_cell_num >= UINT32_MAX) { - wasm_set_exception(module, "wasm operand stack overflow"); - goto got_exception; - } + { + wasm_interp_call_func_native(module, exec_env, cur_func, + prev_frame); + } + + prev_frame = frame->prev_frame; + cur_func = frame->function; + UPDATE_ALL_FROM_FRAME(); - frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num); - if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) { - frame = prev_frame; - goto got_exception; + /* update memory instance ptr and memory size */ + memory = module->default_memory; + if (memory) + linear_mem_size = num_bytes_per_page * memory->cur_page_count; + if (wasm_get_exception(module)) + goto got_exception; } + else { + WASMFunction *cur_wasm_func = cur_func->u.func; + WASMType *func_type; + + func_type = cur_wasm_func->func_type; + + all_cell_num = (uint64)cur_func->param_cell_num + + (uint64)cur_func->local_cell_num + + (uint64)cur_wasm_func->max_stack_cell_num + + ((uint64)cur_wasm_func->max_block_num) + * sizeof(WASMBranchBlock) / 4; + if (all_cell_num >= UINT32_MAX) { + wasm_set_exception(module, "wasm operand stack overflow"); + goto got_exception; + } - /* Initialize the interpreter context. */ - frame->function = cur_func; - frame_ip = wasm_get_func_code(cur_func); - frame_ip_end = wasm_get_func_code_end(cur_func); - frame_lp = frame->lp; + frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num); + if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) { + frame = prev_frame; + goto got_exception; + } + + /* Initialize the interpreter context. */ + frame->function = cur_func; + frame_ip = wasm_get_func_code(cur_func); + frame_ip_end = wasm_get_func_code_end(cur_func); + frame_lp = frame->lp; - frame_sp = frame->sp_bottom = frame_lp + cur_func->param_cell_num - + cur_func->local_cell_num; - frame->sp_boundary = frame->sp_bottom + cur_wasm_func->max_stack_cell_num; + frame_sp = frame->sp_bottom = + frame_lp + cur_func->param_cell_num + cur_func->local_cell_num; + frame->sp_boundary = + frame->sp_bottom + cur_wasm_func->max_stack_cell_num; - frame_csp = frame->csp_bottom = (WASMBranchBlock*)frame->sp_boundary; - frame->csp_boundary = frame->csp_bottom + cur_wasm_func->max_block_num; + frame_csp = frame->csp_bottom = + (WASMBranchBlock *)frame->sp_boundary; + frame->csp_boundary = + frame->csp_bottom + cur_wasm_func->max_block_num; - /* Initialize the local varialbes */ - memset(frame_lp + cur_func->param_cell_num, 0, - (uint32)(cur_func->local_cell_num * 4)); + /* Initialize the local varialbes */ + memset(frame_lp + cur_func->param_cell_num, 0, + (uint32)(cur_func->local_cell_num * 4)); - /* Push function block as first block */ - cell_num = func_type->ret_cell_num; - PUSH_CSP(LABEL_TYPE_FUNCTION, cell_num, frame_ip_end - 1); + /* Push function block as first block */ + cell_num = func_type->ret_cell_num; + PUSH_CSP(LABEL_TYPE_FUNCTION, cell_num, frame_ip_end - 1); - wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame*)frame); + wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame); #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - } - HANDLE_OP_END (); + } + HANDLE_OP_END(); } - return_func: + return_func: { - FREE_FRAME(exec_env, frame); - wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame*)prev_frame); + FREE_FRAME(exec_env, frame); + wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame); - if (!prev_frame->ip) - /* Called from native. */ - return; + if (!prev_frame->ip) + /* Called from native. */ + return; - RECOVER_CONTEXT(prev_frame); - HANDLE_OP_END (); + RECOVER_CONTEXT(prev_frame); + HANDLE_OP_END(); } #if WASM_ENABLE_SHARED_MEMORY != 0 - unaligned_atomic: - wasm_set_exception(module, "unaligned atomic"); - goto got_exception; + unaligned_atomic: + wasm_set_exception(module, "unaligned atomic"); + goto got_exception; #endif - out_of_bounds: - wasm_set_exception(module, "out of bounds memory access"); + out_of_bounds: + wasm_set_exception(module, "out of bounds memory access"); - got_exception: - SYNC_ALL_TO_FRAME(); - return; + got_exception: + SYNC_ALL_TO_FRAME(); + return; #if WASM_ENABLE_LABELS_AS_VALUES == 0 - } + } #else - FETCH_OPCODE_AND_DISPATCH (); + FETCH_OPCODE_AND_DISPATCH(); #endif } void -wasm_interp_call_wasm(WASMModuleInstance *module_inst, - WASMExecEnv *exec_env, - WASMFunctionInstance *function, - uint32 argc, uint32 argv[]) +wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env, + WASMFunctionInstance *function, uint32 argc, + uint32 argv[]) { WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env); WASMInterpFrame *frame, *outs_area; /* Allocate sufficient cells for all kinds of return values. */ - unsigned all_cell_num = function->ret_cell_num > 2 ? - function->ret_cell_num : 2, i; + unsigned all_cell_num = + function->ret_cell_num > 2 ? function->ret_cell_num : 2, + i; /* This frame won't be used by JITed code, so only allocate interp frame here. */ unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num); if (argc != function->param_cell_num) { char buf[128]; - snprintf(buf, sizeof(buf), - "invalid argument count %d, expected %d", + snprintf(buf, sizeof(buf), "invalid argument count %d, expected %d", argc, function->param_cell_num); wasm_set_exception(module_inst, buf); return; } - if ((uint8*)&prev_frame < exec_env->native_stack_boundary) { - wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, + if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) { + wasm_set_exception((WASMModuleInstance *)exec_env->module_inst, "native stack overflow"); return; } - if (!(frame = ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame*)prev_frame))) + if (!(frame = + ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame))) return; outs_area = wasm_exec_env_wasm_stack_top(exec_env); @@ -3476,15 +3740,15 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, if (function->is_import_func) { #if WASM_ENABLE_MULTI_MODULE != 0 if (function->import_module_inst) { - wasm_interp_call_func_import(module_inst, exec_env, - function, frame); + wasm_interp_call_func_import(module_inst, exec_env, function, + frame); } else #endif { /* it is a native function */ - wasm_interp_call_func_native(module_inst, exec_env, - function, frame); + wasm_interp_call_func_native(module_inst, exec_env, function, + frame); } } else { diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index ca32b8d369..b0e29e4564 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -18,29 +18,33 @@ typedef int64 CellType_I64; typedef float32 CellType_F32; typedef float64 CellType_F64; -#define CHECK_MEMORY_OVERFLOW(bytes) do { \ - uint64 offset1 = (uint64)offset + (uint64)addr; \ - if (offset1 + bytes <= (uint64)linear_mem_size) \ - /* If offset1 is in valid range, maddr must also be in valid range,\ - no need to check it again. */ \ - maddr = memory->memory_data + offset1; \ - else \ - goto out_of_bounds; \ - } while (0) - -#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) do { \ - uint64 offset1 = (uint32)(start); \ - if (offset1 + bytes <= linear_mem_size) \ - /* App heap space is not valid space for bulk memory operation */ \ - maddr = memory->memory_data + offset1; \ - else \ - goto out_of_bounds; \ - } while (0) - -#define CHECK_ATOMIC_MEMORY_ACCESS(align) do { \ - if (((uintptr_t)maddr & (align - 1)) != 0) \ - goto unaligned_atomic; \ - } while (0) +#define CHECK_MEMORY_OVERFLOW(bytes) \ + do { \ + uint64 offset1 = (uint64)offset + (uint64)addr; \ + if (offset1 + bytes <= (uint64)linear_mem_size) \ + /* If offset1 is in valid range, maddr must also \ + be in valid range, no need to check it again. */ \ + maddr = memory->memory_data + offset1; \ + else \ + goto out_of_bounds; \ + } while (0) + +#define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \ + do { \ + uint64 offset1 = (uint32)(start); \ + if (offset1 + bytes <= linear_mem_size) \ + /* App heap space is not valid space for \ + bulk memory operation */ \ + maddr = memory->memory_data + offset1; \ + else \ + goto out_of_bounds; \ + } while (0) + +#define CHECK_ATOMIC_MEMORY_ACCESS(align) \ + do { \ + if (((uintptr_t)maddr & (align - 1)) != 0) \ + goto unaligned_atomic; \ + } while (0) static inline uint32 rotl32(uint32 n, uint32 c) @@ -82,7 +86,7 @@ static inline double wa_fmax(double a, double b) { double c = fmax(a, b); - if (c==0 && a==b) + if (c == 0 && a == b) return signbit(a) ? b : a; return c; } @@ -91,7 +95,7 @@ static inline double wa_fmin(double a, double b) { double c = fmin(a, b); - if (c==0 && a==b) + if (c == 0 && a == b) return signbit(a) ? a : b; return c; } @@ -171,41 +175,48 @@ popcount64(uint64 u) } #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 -#define LOAD_U32_WITH_2U16S(addr) (*(uint32*)(addr)) -#define LOAD_PTR(addr) (*(void**)(addr)) +#define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr)) +#define LOAD_PTR(addr) (*(void **)(addr)) #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ static inline uint32 LOAD_U32_WITH_2U16S(void *addr) { - union { uint32 val; uint16 u16[2]; } u; + union { + uint32 val; + uint16 u16[2]; + } u; bh_assert(((uintptr_t)addr & 1) == 0); - u.u16[0] = ((uint16*)addr)[0]; - u.u16[1] = ((uint16*)addr)[1]; + u.u16[0] = ((uint16 *)addr)[0]; + u.u16[1] = ((uint16 *)addr)[1]; return u.val; } #if UINTPTR_MAX == UINT32_MAX -#define LOAD_PTR(addr) ((void*)LOAD_U32_WITH_2U16S(addr)) +#define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr)) #elif UINTPTR_MAX == UINT64_MAX static inline void * LOAD_PTR(void *addr) { uintptr_t addr1 = (uintptr_t)addr; - union { void *val; uint32 u32[2]; uint16 u16[4]; } u; + union { + void *val; + uint32 u32[2]; + uint16 u16[4]; + } u; bh_assert(((uintptr_t)addr & 1) == 0); if ((addr1 & (uintptr_t)7) == 0) - return *(void**)addr; + return *(void **)addr; if ((addr1 & (uintptr_t)3) == 0) { - u.u32[0] = ((uint32*)addr)[0]; - u.u32[1] = ((uint32*)addr)[1]; + u.u32[0] = ((uint32 *)addr)[0]; + u.u32[1] = ((uint32 *)addr)[1]; } else { - u.u16[0] = ((uint16*)addr)[0]; - u.u16[1] = ((uint16*)addr)[1]; - u.u16[2] = ((uint16*)addr)[2]; - u.u16[3] = ((uint16*)addr)[3]; + u.u16[0] = ((uint16 *)addr)[0]; + u.u16[1] = ((uint16 *)addr)[1]; + u.u16[2] = ((uint16 *)addr)[2]; + u.u16[3] = ((uint16 *)addr)[3]; } return u.val; } @@ -215,271 +226,289 @@ LOAD_PTR(void *addr) #define read_uint32(p) \ (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32))) -#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \ - uint32 param_count = cur_func->param_count; \ - local_idx = read_uint32(frame_ip); \ - bh_assert(local_idx < param_count + cur_func->local_count); \ - local_offset = cur_func->local_offsets[local_idx]; \ - if (local_idx < param_count) \ - local_type = cur_func->param_types[local_idx]; \ - else \ - local_type = cur_func->local_types[local_idx - param_count]; \ - } while (0) - -#define GET_OFFSET() (frame_ip += 2, *(int16*)(frame_ip - 2)) - -#define SET_OPERAND_I32(off, value) do { \ - *(uint32*)(frame_lp + *(int16*)(frame_ip + off)) = value; \ - } while (0) -#define SET_OPERAND_F32(off, value) do { \ - *(float32*)(frame_lp + *(int16*)(frame_ip + off)) = value; \ - } while (0) -#define SET_OPERAND_I64(off, value) do { \ - uint32 *addr_tmp = frame_lp + *(int16*)(frame_ip + off); \ - PUT_I64_TO_ADDR(addr_tmp, value); \ - } while (0) -#define SET_OPERAND_F64(off, value) do { \ - uint32 *addr_tmp = frame_lp + *(int16*)(frame_ip + off); \ - PUT_F64_TO_ADDR(addr_tmp, value); \ - } while (0) +#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \ + do { \ + uint32 param_count = cur_func->param_count; \ + local_idx = read_uint32(frame_ip); \ + bh_assert(local_idx < param_count + cur_func->local_count); \ + local_offset = cur_func->local_offsets[local_idx]; \ + if (local_idx < param_count) \ + local_type = cur_func->param_types[local_idx]; \ + else \ + local_type = cur_func->local_types[local_idx - param_count]; \ + } while (0) + +#define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2)) + +#define SET_OPERAND_I32(off, value) \ + do { \ + *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \ + } while (0) +#define SET_OPERAND_F32(off, value) \ + do { \ + *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \ + } while (0) +#define SET_OPERAND_I64(off, value) \ + do { \ + uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \ + PUT_I64_TO_ADDR(addr_tmp, value); \ + } while (0) +#define SET_OPERAND_F64(off, value) \ + do { \ + uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \ + PUT_F64_TO_ADDR(addr_tmp, value); \ + } while (0) #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value) -#define GET_OPERAND_I32(type, off) \ - *(type*)(frame_lp + *(int16*)(frame_ip + off)) -#define GET_OPERAND_F32(type, off) \ - *(type*)(frame_lp + *(int16*)(frame_ip + off)) -#define GET_OPERAND_I64(type, off) \ - (type)GET_I64_FROM_ADDR(frame_lp + *(int16*)(frame_ip + off)) -#define GET_OPERAND_F64(type, off) \ - (type)GET_F64_FROM_ADDR(frame_lp + *(int16*)(frame_ip + off)) +#define GET_OPERAND_I32(type, off) \ + *(type *)(frame_lp + *(int16 *)(frame_ip + off)) +#define GET_OPERAND_F32(type, off) \ + *(type *)(frame_lp + *(int16 *)(frame_ip + off)) +#define GET_OPERAND_I64(type, off) \ + (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off)) +#define GET_OPERAND_F64(type, off) \ + (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off)) #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off) -#define PUSH_I32(value) do { \ - *(int32*)(frame_lp + GET_OFFSET()) = value; \ - } while (0) +#define PUSH_I32(value) \ + do { \ + *(int32 *)(frame_lp + GET_OFFSET()) = value; \ + } while (0) -#define PUSH_F32(value) do { \ - *(float32*)(frame_lp + GET_OFFSET()) = value; \ - } while (0) +#define PUSH_F32(value) \ + do { \ + *(float32 *)(frame_lp + GET_OFFSET()) = value; \ + } while (0) -#define PUSH_I64(value) do { \ - uint32 *addr_tmp = frame_lp + GET_OFFSET(); \ - PUT_I64_TO_ADDR(addr_tmp, value); \ - } while (0) +#define PUSH_I64(value) \ + do { \ + uint32 *addr_tmp = frame_lp + GET_OFFSET(); \ + PUT_I64_TO_ADDR(addr_tmp, value); \ + } while (0) -#define PUSH_F64(value) do { \ - uint32 *addr_tmp = frame_lp + GET_OFFSET(); \ - PUT_F64_TO_ADDR(addr_tmp, value); \ - } while (0) +#define PUSH_F64(value) \ + do { \ + uint32 *addr_tmp = frame_lp + GET_OFFSET(); \ + PUT_F64_TO_ADDR(addr_tmp, value); \ + } while (0) -#define POP_I32() (*(int32*)(frame_lp + GET_OFFSET())) +#define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET())) -#define POP_F32() (*(float32*)(frame_lp + GET_OFFSET())) +#define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET())) #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET())) #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET())) -#define SYNC_ALL_TO_FRAME() do { \ - frame->ip = frame_ip; \ - } while (0) +#define SYNC_ALL_TO_FRAME() \ + do { \ + frame->ip = frame_ip; \ + } while (0) -#define UPDATE_ALL_FROM_FRAME() do { \ - frame_ip = frame->ip; \ - } while (0) +#define UPDATE_ALL_FROM_FRAME() \ + do { \ + frame_ip = frame->ip; \ + } while (0) #if WASM_ENABLE_LABELS_AS_VALUES != 0 #define UPDATE_FRAME_IP_END() (void)0 #else -#define UPDATE_FRAME_IP_END() \ - frame_ip_end = wasm_get_func_code_end(cur_func) +#define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func) #endif -#define RECOVER_CONTEXT(new_frame) do { \ - frame = (new_frame); \ - cur_func = frame->function; \ - prev_frame = frame->prev_frame; \ - frame_ip = frame->ip; \ - UPDATE_FRAME_IP_END(); \ - frame_lp = frame->lp; \ - } while (0) +#define RECOVER_CONTEXT(new_frame) \ + do { \ + frame = (new_frame); \ + cur_func = frame->function; \ + prev_frame = frame->prev_frame; \ + frame_ip = frame->ip; \ + UPDATE_FRAME_IP_END(); \ + frame_lp = frame->lp; \ + } while (0) #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 #define GET_OPCODE() opcode = *frame_ip++; #else -#define GET_OPCODE() opcode = *frame_ip; frame_ip += 2; +#define GET_OPCODE() \ + opcode = *frame_ip; \ + frame_ip += 2; #endif -#define DEF_OP_EQZ(ctype, src_op_type) do { \ - SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \ - frame_ip += 4; \ - } while (0) - -#define DEF_OP_CMP(src_type, src_op_type, cond) do { \ - SET_OPERAND(I32, 4, GET_OPERAND(src_type, src_op_type, 2) cond \ - GET_OPERAND(src_type, src_op_type, 0)); \ - frame_ip += 6; \ - } while (0) - -#define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) do { \ - SET_OPERAND(src_op_type, 2, (src_type)operation( \ - GET_OPERAND(src_type, src_op_type, 0))); \ - frame_ip += 4; \ - } while (0) - -#define DEF_OP_NUMERIC(src_type1, src_type2, \ - src_op_type, operation) do { \ - SET_OPERAND(src_op_type, 4, \ - GET_OPERAND(src_type1, src_op_type, 2) operation \ - GET_OPERAND(src_type2, src_op_type, 0)); \ - frame_ip += 6; \ - } while (0) - -#define DEF_OP_REINTERPRET(src_type, src_op_type) do { \ - SET_OPERAND(src_op_type, 2, \ - GET_OPERAND(src_type, src_op_type, 0)); \ - frame_ip += 4; \ - } while (0) +#define DEF_OP_EQZ(ctype, src_op_type) \ + do { \ + SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \ + frame_ip += 4; \ + } while (0) + +#define DEF_OP_CMP(src_type, src_op_type, cond) \ + do { \ + SET_OPERAND(I32, 4, \ + GET_OPERAND(src_type, src_op_type, 2) \ + cond GET_OPERAND(src_type, src_op_type, 0)); \ + frame_ip += 6; \ + } while (0) + +#define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \ + do { \ + SET_OPERAND( \ + src_op_type, 2, \ + (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \ + frame_ip += 4; \ + } while (0) + +#define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \ + do { \ + SET_OPERAND(src_op_type, 4, \ + GET_OPERAND(src_type1, src_op_type, 2) \ + operation GET_OPERAND(src_type2, src_op_type, 0)); \ + frame_ip += 6; \ + } while (0) + +#define DEF_OP_REINTERPRET(src_type, src_op_type) \ + do { \ + SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \ + frame_ip += 4; \ + } while (0) #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC -#define DEF_OP_NUMERIC2(src_type1, src_type2, \ - src_op_type, operation) do { \ - SET_OPERAND(src_op_type, 4, \ - GET_OPERAND(src_type1, src_op_type, 2) operation \ - (GET_OPERAND(src_type2, src_op_type, 0) % 32)); \ - frame_ip += 6; \ - } while (0) - -#define DEF_OP_NUMERIC2_64(src_type1, src_type2, \ - src_op_type, operation) do { \ - SET_OPERAND(src_op_type, 4, \ - GET_OPERAND(src_type1, src_op_type, 2) operation \ - (GET_OPERAND(src_type2, src_op_type, 0) % 64)); \ - frame_ip += 6; \ - } while (0) - -#define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \ - case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \ - case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \ - case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \ - { \ - uint32 readv, sval; \ - \ - sval = POP_I32(); \ - addr = POP_I32(); \ - \ - if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(1); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint32)(*(uint8*)maddr); \ - *(uint8*)maddr = (uint8)(readv op sval); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(2); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint32)LOAD_U16(maddr); \ - STORE_U16(maddr, (uint16)(readv op sval)); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(4); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = LOAD_I32(maddr); \ - STORE_U32(maddr, readv op sval); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - PUSH_I32(readv); \ - break; \ - } \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \ - case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \ - { \ - uint64 readv, sval; \ - \ - sval = (uint64)POP_I64(); \ - addr = POP_I32(); \ - \ - if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(1); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)(*(uint8*)maddr); \ - *(uint8*)maddr = (uint8)(readv op sval); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(2); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)LOAD_U16(maddr); \ - STORE_U16(maddr, (uint16)(readv op sval)); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(4); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)LOAD_U32(maddr); \ - STORE_U32(maddr, (uint32)(readv op sval)); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - else { \ - uint64 op_result; \ - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \ - CHECK_ATOMIC_MEMORY_ACCESS(8); \ - \ - os_mutex_lock(&memory->mem_lock); \ - readv = (uint64)LOAD_I64(maddr); \ - op_result = readv op sval; \ - STORE_I64(maddr, op_result); \ - os_mutex_unlock(&memory->mem_lock); \ - } \ - PUSH_I64(readv); \ - break; \ - } - -#define DEF_OP_MATH(src_type, src_op_type, method) do { \ - SET_OPERAND(src_op_type, 2, \ - (src_type)method(GET_OPERAND(src_type, \ - src_op_type, 0))); \ - frame_ip += 4; \ - } while (0) +#define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \ + do { \ + SET_OPERAND(src_op_type, 4, \ + GET_OPERAND(src_type1, src_op_type, 2) operation( \ + GET_OPERAND(src_type2, src_op_type, 0) % 32)); \ + frame_ip += 6; \ + } while (0) + +#define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \ + do { \ + SET_OPERAND(src_op_type, 4, \ + GET_OPERAND(src_type1, src_op_type, 2) operation( \ + GET_OPERAND(src_type2, src_op_type, 0) % 64)); \ + frame_ip += 6; \ + } while (0) + +#define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \ + case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \ + case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \ + case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \ + { \ + uint32 readv, sval; \ + \ + sval = POP_I32(); \ + addr = POP_I32(); \ + \ + if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(1); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint32)(*(uint8 *)maddr); \ + *(uint8 *)maddr = (uint8)(readv op sval); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(2); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint32)LOAD_U16(maddr); \ + STORE_U16(maddr, (uint16)(readv op sval)); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(4); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = LOAD_I32(maddr); \ + STORE_U32(maddr, readv op sval); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + PUSH_I32(readv); \ + break; \ + } \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \ + case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \ + { \ + uint64 readv, sval; \ + \ + sval = (uint64)POP_I64(); \ + addr = POP_I32(); \ + \ + if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(1); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)(*(uint8 *)maddr); \ + *(uint8 *)maddr = (uint8)(readv op sval); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(2); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)LOAD_U16(maddr); \ + STORE_U16(maddr, (uint16)(readv op sval)); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(4); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)LOAD_U32(maddr); \ + STORE_U32(maddr, (uint32)(readv op sval)); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + else { \ + uint64 op_result; \ + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \ + CHECK_ATOMIC_MEMORY_ACCESS(8); \ + \ + os_mutex_lock(&memory->mem_lock); \ + readv = (uint64)LOAD_I64(maddr); \ + op_result = readv op sval; \ + STORE_I64(maddr, op_result); \ + os_mutex_unlock(&memory->mem_lock); \ + } \ + PUSH_I64(readv); \ + break; \ + } + +#define DEF_OP_MATH(src_type, src_op_type, method) \ + do { \ + SET_OPERAND(src_op_type, 2, \ + (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \ + frame_ip += 4; \ + } while (0) #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \ -static dst_type \ -func_name(src_type src_value, src_type src_min, src_type src_max, \ - dst_type dst_min, dst_type dst_max, bool is_sign) \ -{ \ - dst_type dst_value = 0; \ - if (!isnan(src_value)) { \ - if (src_value <= src_min) \ - dst_value = dst_min; \ - else if (src_value >= src_max) \ - dst_value = dst_max; \ - else { \ - if (is_sign) \ - dst_value = (dst_type)(signed_type)src_value; \ - else \ - dst_value = (dst_type)src_value; \ - } \ - } \ - return dst_value; \ -} + static dst_type func_name(src_type src_value, src_type src_min, \ + src_type src_max, dst_type dst_min, \ + dst_type dst_max, bool is_sign) \ + { \ + dst_type dst_value = 0; \ + if (!isnan(src_value)) { \ + if (src_value <= src_min) \ + dst_value = dst_min; \ + else if (src_value >= src_max) \ + dst_value = dst_max; \ + else { \ + if (is_sign) \ + dst_value = (dst_type)(signed_type)src_value; \ + else \ + dst_value = (dst_type)src_value; \ + } \ + } \ + return dst_value; \ + } TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32) TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64) @@ -487,10 +516,9 @@ TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32) TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64) static bool -trunc_f32_to_int(WASMModuleInstance *module, - uint8 *frame_ip, uint32 *frame_lp, - float32 src_min, float32 src_max, - bool saturating, bool is_i32, bool is_sign) +trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp, + float32 src_min, float32 src_max, bool saturating, bool is_i32, + bool is_sign) { float32 src_value = GET_OPERAND(float32, F32, 0); uint64 dst_value_i64; @@ -510,25 +538,24 @@ trunc_f32_to_int(WASMModuleInstance *module, if (is_i32) { uint32 dst_min = is_sign ? INT32_MIN : 0; uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX; - dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min, + dst_max, is_sign); SET_OPERAND(I32, 2, dst_value_i32); } else { uint64 dst_min = is_sign ? INT64_MIN : 0; uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX; - dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min, + dst_max, is_sign); SET_OPERAND(I64, 2, dst_value_i64); } return false; } static bool -trunc_f64_to_int(WASMModuleInstance *module, - uint8 *frame_ip, uint32 *frame_lp, - float64 src_min, float64 src_max, - bool saturating, bool is_i32, bool is_sign) +trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp, + float64 src_min, float64 src_max, bool saturating, bool is_i32, + bool is_sign) { float64 src_value = GET_OPERAND(float64, F64, 0); uint64 dst_value_i64; @@ -548,51 +575,55 @@ trunc_f64_to_int(WASMModuleInstance *module, if (is_i32) { uint32 dst_min = is_sign ? INT32_MIN : 0; uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX; - dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min, + dst_max, is_sign); SET_OPERAND(I32, 2, dst_value_i32); } else { uint64 dst_min = is_sign ? INT64_MIN : 0; uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX; - dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, - dst_min, dst_max, is_sign); + dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min, + dst_max, is_sign); SET_OPERAND(I64, 2, dst_value_i64); } return false; } -#define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) do { \ - if (trunc_f32_to_int(module, frame_ip, frame_lp, min, max, \ - false, is_i32, is_sign)) \ - goto got_exception; \ - frame_ip += 4; \ - } while (0) - -#define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) do { \ - if (trunc_f64_to_int(module, frame_ip, frame_lp, min, max, \ - false, is_i32, is_sign)) \ - goto got_exception; \ - frame_ip += 4; \ - } while (0) - -#define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) do { \ - (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, \ - true, is_i32, is_sign); \ - frame_ip += 4; \ - } while (0) - -#define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) do { \ - (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, \ - true, is_i32, is_sign); \ - frame_ip += 4; \ - } while (0) - -#define DEF_OP_CONVERT(dst_type, dst_op_type, \ - src_type, src_op_type) do { \ - dst_type value = (dst_type)(src_type)POP_##src_op_type(); \ - PUSH_##dst_op_type(value); \ - } while (0) +#define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \ + do { \ + if (trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \ + is_i32, is_sign)) \ + goto got_exception; \ + frame_ip += 4; \ + } while (0) + +#define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \ + do { \ + if (trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \ + is_i32, is_sign)) \ + goto got_exception; \ + frame_ip += 4; \ + } while (0) + +#define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \ + do { \ + (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \ + is_i32, is_sign); \ + frame_ip += 4; \ + } while (0) + +#define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \ + do { \ + (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \ + is_i32, is_sign); \ + frame_ip += 4; \ + } while (0) + +#define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \ + do { \ + dst_type value = (dst_type)(src_type)POP_##src_op_type(); \ + PUSH_##dst_op_type(value); \ + } while (0) #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 #define CELL_SIZE sizeof(uint8) @@ -601,120 +632,117 @@ trunc_f64_to_int(WASMModuleInstance *module, #endif static bool -copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, - uint32 arity, uint32 total_cell_num, const uint8 *cells, +copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity, + uint32 total_cell_num, const uint8 *cells, const int16 *src_offsets, const uint16 *dst_offsets) { - /* To avoid the overlap issue between src offsets and dst offset, - * we use 2 steps to do the copy. First step, copy the src values - * to a tmp buf. Second step, copy the values from tmp buf to dst. - */ - uint32 buf[16] = {0}, i; - uint32 *tmp_buf = buf; - uint8 cell; - int16 src, buf_index = 0; - uint16 dst; - - /* Allocate memory if the buf is not large enough */ - if (total_cell_num > sizeof(buf)/sizeof(uint32)) { - uint64 total_size = sizeof(uint32) * (uint64)total_cell_num; - if (total_size >= UINT32_MAX - || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) { - wasm_set_exception(module, "allocate memory failed"); - return false; + /* To avoid the overlap issue between src offsets and dst offset, + * we use 2 steps to do the copy. First step, copy the src values + * to a tmp buf. Second step, copy the values from tmp buf to dst. + */ + uint32 buf[16] = { 0 }, i; + uint32 *tmp_buf = buf; + uint8 cell; + int16 src, buf_index = 0; + uint16 dst; + + /* Allocate memory if the buf is not large enough */ + if (total_cell_num > sizeof(buf) / sizeof(uint32)) { + uint64 total_size = sizeof(uint32) * (uint64)total_cell_num; + if (total_size >= UINT32_MAX + || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) { + wasm_set_exception(module, "allocate memory failed"); + return false; + } } - } - - /* 1) Copy values from src to tmp buf */ - for (i = 0; i < arity; i++) { - cell = cells[i * CELL_SIZE]; - src = src_offsets[i]; - if (cell == 1) - tmp_buf[buf_index] = frame_lp[src]; - else { - tmp_buf[buf_index] = frame_lp[src]; - tmp_buf[buf_index + 1] = frame_lp[src + 1]; + + /* 1) Copy values from src to tmp buf */ + for (i = 0; i < arity; i++) { + cell = cells[i * CELL_SIZE]; + src = src_offsets[i]; + if (cell == 1) + tmp_buf[buf_index] = frame_lp[src]; + else { + tmp_buf[buf_index] = frame_lp[src]; + tmp_buf[buf_index + 1] = frame_lp[src + 1]; + } + buf_index += cell; } - buf_index += cell; - } - - /* 2) Copy values from tmp buf to dest */ - buf_index = 0; - for (i = 0; i < arity; i++) { - cell = cells[i * CELL_SIZE]; - dst = dst_offsets[i]; - if (cell == 1) - frame_lp[dst] = tmp_buf[buf_index]; - else { - frame_lp[dst] = tmp_buf[buf_index]; - frame_lp[dst + 1] = tmp_buf[buf_index + 1]; + + /* 2) Copy values from tmp buf to dest */ + buf_index = 0; + for (i = 0; i < arity; i++) { + cell = cells[i * CELL_SIZE]; + dst = dst_offsets[i]; + if (cell == 1) + frame_lp[dst] = tmp_buf[buf_index]; + else { + frame_lp[dst] = tmp_buf[buf_index]; + frame_lp[dst + 1] = tmp_buf[buf_index + 1]; + } + buf_index += cell; } - buf_index += cell; - } - if (tmp_buf != buf) { - wasm_runtime_free(tmp_buf); - } + if (tmp_buf != buf) { + wasm_runtime_free(tmp_buf); + } - return true; + return true; } -#define RECOVER_BR_INFO() do { \ - uint32 arity; \ - /* read arity */ \ - arity = read_uint32(frame_ip); \ - if (arity) { \ - uint32 total_cell; \ - uint16 *dst_offsets = NULL; \ - uint8 *cells; \ - int16 *src_offsets = NULL; \ - /* read total cell num */ \ - total_cell = read_uint32(frame_ip); \ - /* cells */ \ - cells = (uint8 *)frame_ip; \ - frame_ip += arity * CELL_SIZE; \ - /* src offsets */ \ - src_offsets = (int16 *)frame_ip; \ - frame_ip += arity * sizeof(int16); \ - /* dst offsets */ \ - dst_offsets = (uint16*)frame_ip; \ - frame_ip += arity * sizeof(uint16); \ - if (arity == 1) { \ - if (cells[0] == 1) \ - frame_lp[dst_offsets[0]] = \ - frame_lp[src_offsets[0]]; \ - else if (cells[0] == 2) { \ - frame_lp[dst_offsets[0]] = \ - frame_lp[src_offsets[0]]; \ - frame_lp[dst_offsets[0] + 1] = \ - frame_lp[src_offsets[0] + 1]; \ - } \ - } \ - else { \ - if (!copy_stack_values(module, frame_lp, \ - arity, total_cell, \ - cells, src_offsets, \ - dst_offsets)) \ - goto got_exception; \ - } \ - } \ - frame_ip = (uint8*)LOAD_PTR(frame_ip); \ - } while (0) - -#define SKIP_BR_INFO() do { \ - uint32 arity; \ - /* read and skip arity */ \ - arity = read_uint32(frame_ip); \ - if (arity) { \ - /* skip total cell num */ \ - frame_ip += sizeof(uint32); \ - /* skip cells, src offsets and dst offsets */ \ - frame_ip += (CELL_SIZE + sizeof(int16) \ - + sizeof(uint16)) * arity; \ - } \ - /* skip target address */ \ - frame_ip += sizeof(uint8*); \ - } while (0) +#define RECOVER_BR_INFO() \ + do { \ + uint32 arity; \ + /* read arity */ \ + arity = read_uint32(frame_ip); \ + if (arity) { \ + uint32 total_cell; \ + uint16 *dst_offsets = NULL; \ + uint8 *cells; \ + int16 *src_offsets = NULL; \ + /* read total cell num */ \ + total_cell = read_uint32(frame_ip); \ + /* cells */ \ + cells = (uint8 *)frame_ip; \ + frame_ip += arity * CELL_SIZE; \ + /* src offsets */ \ + src_offsets = (int16 *)frame_ip; \ + frame_ip += arity * sizeof(int16); \ + /* dst offsets */ \ + dst_offsets = (uint16 *)frame_ip; \ + frame_ip += arity * sizeof(uint16); \ + if (arity == 1) { \ + if (cells[0] == 1) \ + frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \ + else if (cells[0] == 2) { \ + frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \ + frame_lp[dst_offsets[0] + 1] = \ + frame_lp[src_offsets[0] + 1]; \ + } \ + } \ + else { \ + if (!copy_stack_values(module, frame_lp, arity, total_cell, \ + cells, src_offsets, dst_offsets)) \ + goto got_exception; \ + } \ + } \ + frame_ip = (uint8 *)LOAD_PTR(frame_ip); \ + } while (0) + +#define SKIP_BR_INFO() \ + do { \ + uint32 arity; \ + /* read and skip arity */ \ + arity = read_uint32(frame_ip); \ + if (arity) { \ + /* skip total cell num */ \ + frame_ip += sizeof(uint32); \ + /* skip cells, src offsets and dst offsets */ \ + frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \ + } \ + /* skip target address */ \ + frame_ip += sizeof(uint8 *); \ + } while (0) static inline int32 sign_ext_8_32(int8 val) @@ -763,7 +791,7 @@ word_copy(uint32 *dest, uint32 *src, unsigned num) *dest++ = *src++; } -static inline WASMInterpFrame* +static inline WASMInterpFrame * ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) { WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size); @@ -775,7 +803,7 @@ ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) #endif } else { - wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, + wasm_set_exception((WASMModuleInstance *)exec_env->module_inst, "wasm operand stack overflow"); } @@ -787,8 +815,8 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame) { #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { - frame->function->total_exec_time += os_time_get_boot_microsecond() - - frame->time_started; + frame->function->total_exec_time += + os_time_get_boot_microsecond() - frame->time_started; frame->function->total_exec_cnt++; } #endif @@ -823,32 +851,32 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, snprintf(buf, sizeof(buf), "failed to call unlinked import function (%s, %s)", func_import->module_name, func_import->field_name); - wasm_set_exception((WASMModuleInstance*)module_inst, buf); + wasm_set_exception((WASMModuleInstance *)module_inst, buf); return; } if (func_import->call_conv_wasm_c_api) { ret = wasm_runtime_invoke_c_api_native( - (WASMModuleInstanceCommon *)module_inst, - func_import->func_ptr_linked, func_import->func_type, - cur_func->param_cell_num, frame->lp, - func_import->wasm_c_api_with_env, func_import->attachment); + (WASMModuleInstanceCommon *)module_inst, + func_import->func_ptr_linked, func_import->func_type, + cur_func->param_cell_num, frame->lp, + func_import->wasm_c_api_with_env, func_import->attachment); if (ret) { argv_ret[0] = frame->lp[0]; argv_ret[1] = frame->lp[1]; } } else if (!func_import->call_conv_raw) { - ret = wasm_runtime_invoke_native(exec_env, func_import->func_ptr_linked, - func_import->func_type, func_import->signature, - func_import->attachment, - frame->lp, cur_func->param_cell_num, argv_ret); + ret = wasm_runtime_invoke_native( + exec_env, func_import->func_ptr_linked, func_import->func_type, + func_import->signature, func_import->attachment, frame->lp, + cur_func->param_cell_num, argv_ret); } else { - ret = wasm_runtime_invoke_native_raw(exec_env, func_import->func_ptr_linked, - func_import->func_type, func_import->signature, - func_import->attachment, - frame->lp, cur_func->param_cell_num, argv_ret); + ret = wasm_runtime_invoke_native_raw( + exec_env, func_import->func_ptr_linked, func_import->func_type, + func_import->signature, func_import->attachment, frame->lp, + cur_func->param_cell_num, argv_ret); } if (!ret) @@ -902,8 +930,8 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst, exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst; /* call function of sub-module*/ - wasm_interp_call_func_bytecode(sub_module_inst, exec_env, - sub_func_inst, prev_frame); + wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst, + prev_frame); /* restore ip and module_inst */ prev_frame->ip = ip; @@ -920,15 +948,16 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst, #endif #if WASM_ENABLE_THREAD_MGR != 0 -#define CHECK_SUSPEND_FLAGS() do { \ - if (exec_env->suspend_flags.flags != 0) { \ - if (exec_env->suspend_flags.flags & 0x01) { \ - /* terminate current thread */ \ - return; \ +#define CHECK_SUSPEND_FLAGS() \ + do { \ + if (exec_env->suspend_flags.flags != 0) { \ + if (exec_env->suspend_flags.flags & 0x01) { \ + /* terminate current thread */ \ + return; \ + } \ + /* TODO: support suspend and breakpoint */ \ } \ - /* TODO: support suspend and breakpoint */ \ - } \ - } while (0) + } while (0) #endif #if WASM_ENABLE_OPCODE_COUNTER != 0 @@ -937,9 +966,14 @@ typedef struct OpcodeInfo { uint64 count; } OpcodeInfo; -#define HANDLE_OPCODE(op) { #op, 0 } -DEFINE_GOTO_TABLE (OpcodeInfo, opcode_table); +/* clang-format off */ +#define HANDLE_OPCODE(op) \ + { \ + #op, 0 \ + } +DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table); #undef HANDLE_OPCODE +/* clang-format on */ static void wasm_interp_dump_op_count() @@ -952,2481 +986,2719 @@ wasm_interp_dump_op_count() printf("total opcode count: %ld\n", total_count); for (i = 0; i < WASM_OP_IMPDEP; i++) if (opcode_table[i].count > 0) - printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", - opcode_table[i].name, opcode_table[i].count, + printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name, + opcode_table[i].count, opcode_table[i].count * 100.0f / total_count); } #endif #if WASM_ENABLE_LABELS_AS_VALUES != 0 -/* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n");h_##opcode */ +/* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */ #if WASM_ENABLE_OPCODE_COUNTER != 0 -#define HANDLE_OP(opcode) HANDLE_##opcode:opcode_table[opcode].count++;h_##opcode +#define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++; #else -#define HANDLE_OP(opcode) HANDLE_##opcode +#define HANDLE_OP(opcode) HANDLE_##opcode: #endif #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 -#define FETCH_OPCODE_AND_DISPATCH() do { \ - const void *p_label_addr = *(void**)frame_ip; \ - frame_ip += sizeof(void*); \ - goto *p_label_addr; \ - } while (0) +#define FETCH_OPCODE_AND_DISPATCH() \ + do { \ + const void *p_label_addr = *(void **)frame_ip; \ + frame_ip += sizeof(void *); \ + goto *p_label_addr; \ + } while (0) #else -#define FETCH_OPCODE_AND_DISPATCH() do { \ - const void *p_label_addr = label_base \ - + *(int16*)frame_ip; \ - frame_ip += sizeof(int16); \ - goto *p_label_addr; \ - } while (0) +#define FETCH_OPCODE_AND_DISPATCH() \ + do { \ + const void *p_label_addr = label_base + *(int16 *)frame_ip; \ + frame_ip += sizeof(int16); \ + goto *p_label_addr; \ + } while (0) #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH() -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ -#define HANDLE_OP(opcode) case opcode +#define HANDLE_OP(opcode) case opcode: #define HANDLE_OP_END() continue -#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ +#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ #if WASM_ENABLE_LABELS_AS_VALUES != 0 static void **global_handle_table; #endif +static inline uint8 * +get_global_addr(uint8 *global_data, WASMGlobalInstance *global) +{ +#if WASM_ENABLE_MULTI_MODULE == 0 + return global_data + global->data_offset; +#else + return global->import_global_inst + ? global->import_module_inst->global_data + + global->import_global_inst->data_offset + : global_data + global->data_offset; +#endif +} + static void wasm_interp_call_func_bytecode(WASMModuleInstance *module, WASMExecEnv *exec_env, WASMFunctionInstance *cur_func, WASMInterpFrame *prev_frame) { - WASMMemoryInstance *memory = module->default_memory; - uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0; - uint8 *global_data = module->global_data; - uint32 linear_mem_size = memory ? num_bytes_per_page * memory->cur_page_count : 0; - WASMGlobalInstance *globals = module->globals, *global; - uint8 opcode_IMPDEP = WASM_OP_IMPDEP; - WASMInterpFrame *frame = NULL; - /* Points to this special opcode so as to jump to the call_method_from_entry. */ - register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */ - register uint32 *frame_lp = NULL; /* cache of frame->lp */ + WASMMemoryInstance *memory = module->default_memory; + uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0; + uint8 *global_data = module->global_data; + uint32 linear_mem_size = + memory ? num_bytes_per_page * memory->cur_page_count : 0; + WASMGlobalInstance *globals = module->globals, *global; + uint8 opcode_IMPDEP = WASM_OP_IMPDEP; + WASMInterpFrame *frame = NULL; + /* Points to this special opcode so as to jump to the + * call_method_from_entry. */ + register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */ + register uint32 *frame_lp = NULL; /* cache of frame->lp */ #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 - /* cache of label base addr */ - register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE; + /* cache of label base addr */ + register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE; #endif #endif - uint8 *frame_ip_end = frame_ip + 1; - uint32 cond, count, fidx, tidx, frame_size = 0; - uint64 all_cell_num = 0; - int16 addr1, addr2, addr_ret = 0; - int32 didx, val; - uint8 *maddr = NULL; - uint32 local_idx, local_offset, global_idx; - uint8 opcode, local_type, *global_addr; + uint8 *frame_ip_end = frame_ip + 1; + uint32 cond, count, fidx, tidx, frame_size = 0; + uint64 all_cell_num = 0; + int16 addr1, addr2, addr_ret = 0; + int32 didx, val; + uint8 *maddr = NULL; + uint32 local_idx, local_offset, global_idx; + uint8 opcode, local_type, *global_addr; #if WASM_ENABLE_LABELS_AS_VALUES != 0 - #define HANDLE_OPCODE(op) &&HANDLE_##op - DEFINE_GOTO_TABLE (const void*, handle_table); - #undef HANDLE_OPCODE - if (exec_env == NULL) { - global_handle_table = (void **)handle_table; - return; - } +#define HANDLE_OPCODE(op) &&HANDLE_##op + DEFINE_GOTO_TABLE(const void *, handle_table); +#undef HANDLE_OPCODE + if (exec_env == NULL) { + global_handle_table = (void **)handle_table; + return; + } #endif #if WASM_ENABLE_LABELS_AS_VALUES == 0 - while (frame_ip < frame_ip_end) { - opcode = *frame_ip++; + while (frame_ip < frame_ip_end) { + opcode = *frame_ip++; #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 - frame_ip++; + frame_ip++; #endif - switch (opcode) { + switch (opcode) { #else - goto *handle_table[WASM_OP_IMPDEP]; + goto *handle_table[WASM_OP_IMPDEP]; #endif - /* control instructions */ - HANDLE_OP (WASM_OP_UNREACHABLE): - wasm_set_exception(module, "unreachable"); - goto got_exception; + /* control instructions */ + HANDLE_OP(WASM_OP_UNREACHABLE) + { + wasm_set_exception(module, "unreachable"); + goto got_exception; + } - HANDLE_OP (WASM_OP_IF): - cond = (uint32)POP_I32(); - - if (cond == 0) { - uint8 *else_addr = (uint8*)LOAD_PTR(frame_ip); - if (else_addr == NULL) { - frame_ip = (uint8*)LOAD_PTR(frame_ip + sizeof(uint8*)); - } - else { - frame_ip = else_addr; - } - } - else { - frame_ip += sizeof(uint8*) * 2; - } - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_IF) + { + cond = (uint32)POP_I32(); + + if (cond == 0) { + uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip); + if (else_addr == NULL) { + frame_ip = + (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *)); + } + else { + frame_ip = else_addr; + } + } + else { + frame_ip += sizeof(uint8 *) * 2; + } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_ELSE): - frame_ip = (uint8*)LOAD_PTR(frame_ip); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_ELSE) + { + frame_ip = (uint8 *)LOAD_PTR(frame_ip); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_BR): + HANDLE_OP(WASM_OP_BR) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif -recover_br_info: - RECOVER_BR_INFO(); - HANDLE_OP_END (); + recover_br_info: + RECOVER_BR_INFO(); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_BR_IF): + HANDLE_OP(WASM_OP_BR_IF) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - cond = frame_lp[GET_OFFSET()]; + cond = frame_lp[GET_OFFSET()]; - if (cond) - goto recover_br_info; - else - SKIP_BR_INFO(); + if (cond) + goto recover_br_info; + else + SKIP_BR_INFO(); - HANDLE_OP_END (); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_BR_TABLE): - { - uint32 arity, br_item_size; + HANDLE_OP(WASM_OP_BR_TABLE) + { + uint32 arity, br_item_size; #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - count = read_uint32(frame_ip); - didx = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - - if (!(didx >= 0 && (uint32)didx < count)) - didx = count; - - /* all br items must have the same arity and item size, - so we only calculate the first item size */ - arity = LOAD_U32_WITH_2U16S(frame_ip); - br_item_size = sizeof(uint32); /* arity */ - if (arity) { - /* total cell num */ - br_item_size += sizeof(uint32); - /* cells, src offsets and dst offsets */ - br_item_size += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) - * arity; - } - /* target address */ - br_item_size += sizeof(uint8*); - - frame_ip += br_item_size * didx; - goto recover_br_info; - } - - HANDLE_OP (WASM_OP_RETURN): - { - uint32 ret_idx; - WASMType *func_type; - uint32 off, ret_offset; - uint8 *ret_types; - if (cur_func->is_import_func) - func_type = cur_func->u.func_import->func_type; - else - func_type = cur_func->u.func->func_type; - - /* types of each return value */ - ret_types = func_type->types + func_type->param_count; - ret_offset = prev_frame->ret_offset; - - for (ret_idx = 0, off = sizeof(int16) * (func_type->result_count - 1); - ret_idx < func_type->result_count; - ret_idx++, off -= sizeof(int16)) { - if (ret_types[ret_idx] == VALUE_TYPE_I64 - || ret_types[ret_idx] == VALUE_TYPE_F64) { - PUT_I64_TO_ADDR(prev_frame->lp + ret_offset, - GET_OPERAND(uint64, I64, off)); - ret_offset += 2; + count = read_uint32(frame_ip); + didx = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + + if (!(didx >= 0 && (uint32)didx < count)) + didx = count; + + /* all br items must have the same arity and item size, + so we only calculate the first item size */ + arity = LOAD_U32_WITH_2U16S(frame_ip); + br_item_size = sizeof(uint32); /* arity */ + if (arity) { + /* total cell num */ + br_item_size += sizeof(uint32); + /* cells, src offsets and dst offsets */ + br_item_size += + (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; + } + /* target address */ + br_item_size += sizeof(uint8 *); + + frame_ip += br_item_size * didx; + goto recover_br_info; } - else { - prev_frame->lp[ret_offset] = GET_OPERAND(uint32, I32, off); - ret_offset++; + + HANDLE_OP(WASM_OP_RETURN) + { + uint32 ret_idx; + WASMType *func_type; + uint32 off, ret_offset; + uint8 *ret_types; + if (cur_func->is_import_func) + func_type = cur_func->u.func_import->func_type; + else + func_type = cur_func->u.func->func_type; + + /* types of each return value */ + ret_types = func_type->types + func_type->param_count; + ret_offset = prev_frame->ret_offset; + + for (ret_idx = 0, + off = sizeof(int16) * (func_type->result_count - 1); + ret_idx < func_type->result_count; + ret_idx++, off -= sizeof(int16)) { + if (ret_types[ret_idx] == VALUE_TYPE_I64 + || ret_types[ret_idx] == VALUE_TYPE_F64) { + PUT_I64_TO_ADDR(prev_frame->lp + ret_offset, + GET_OPERAND(uint64, I64, off)); + ret_offset += 2; + } + else { + prev_frame->lp[ret_offset] = + GET_OPERAND(uint32, I32, off); + ret_offset++; + } + } + goto return_func; } - } - } - goto return_func; - HANDLE_OP (WASM_OP_CALL_INDIRECT): + HANDLE_OP(WASM_OP_CALL_INDIRECT) #if WASM_ENABLE_TAIL_CALL != 0 - HANDLE_OP (WASM_OP_RETURN_CALL_INDIRECT): + HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT) #endif - { - WASMType *cur_type, *cur_func_type; - WASMTableInstance *tbl_inst; - uint32 tbl_idx; + { + WASMType *cur_type, *cur_func_type; + WASMTableInstance *tbl_inst; + uint32 tbl_idx; #if WASM_ENABLE_TAIL_CALL != 0 - GET_OPCODE(); + GET_OPCODE(); #endif #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - tidx = read_uint32(frame_ip); - cur_type = module->module->types[tidx]; - - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->table_count); - - tbl_inst = wasm_get_table_inst(module, tbl_idx); - - val = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - - if (val < 0 || val >= (int32)tbl_inst->cur_size) { - wasm_set_exception(module, "undefined element"); - goto got_exception; - } - - fidx = ((uint32*)tbl_inst->base_addr)[val]; - if (fidx == (uint32)-1) { - wasm_set_exception(module, "uninitialized element"); - goto got_exception; - } - - /* - * we might be using a table injected by host or - * another module. in that case, we don't validate - * the elem value while loading - */ - if (fidx >= module->function_count) { - wasm_set_exception(module, "unknown function"); - goto got_exception; - } - - /* always call module own functions */ - cur_func = module->functions + fidx; - - if (cur_func->is_import_func) - cur_func_type = cur_func->u.func_import->func_type; - else - cur_func_type = cur_func->u.func->func_type; - if (!wasm_type_equal(cur_type, cur_func_type)) { - wasm_set_exception(module, "indirect call type mismatch"); - goto got_exception; - } + tidx = read_uint32(frame_ip); + cur_type = module->module->types[tidx]; + + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + val = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + + if (val < 0 || val >= (int32)tbl_inst->cur_size) { + wasm_set_exception(module, "undefined element"); + goto got_exception; + } + + fidx = ((uint32 *)tbl_inst->base_addr)[val]; + if (fidx == (uint32)-1) { + wasm_set_exception(module, "uninitialized element"); + goto got_exception; + } + + /* + * we might be using a table injected by host or + * another module. in that case, we don't validate + * the elem value while loading + */ + if (fidx >= module->function_count) { + wasm_set_exception(module, "unknown function"); + goto got_exception; + } + + /* always call module own functions */ + cur_func = module->functions + fidx; + + if (cur_func->is_import_func) + cur_func_type = cur_func->u.func_import->func_type; + else + cur_func_type = cur_func->u.func->func_type; + if (!wasm_type_equal(cur_type, cur_func_type)) { + wasm_set_exception(module, "indirect call type mismatch"); + goto got_exception; + } #if WASM_ENABLE_TAIL_CALL != 0 - if (opcode == WASM_OP_RETURN_CALL_INDIRECT) - goto call_func_from_return_call; + if (opcode == WASM_OP_RETURN_CALL_INDIRECT) + goto call_func_from_return_call; #endif - goto call_func_from_interp; - } + goto call_func_from_interp; + } - /* parametric instructions */ - HANDLE_OP (WASM_OP_SELECT): - { - cond = frame_lp[GET_OFFSET()]; - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - addr_ret = GET_OFFSET(); - - if (!cond) { - if (addr_ret != addr1) - frame_lp[addr_ret] = frame_lp[addr1]; - } - else { - if (addr_ret != addr2) - frame_lp[addr_ret] = frame_lp[addr2]; - } - HANDLE_OP_END (); - } + /* parametric instructions */ + HANDLE_OP(WASM_OP_SELECT) + { + cond = frame_lp[GET_OFFSET()]; + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + addr_ret = GET_OFFSET(); + + if (!cond) { + if (addr_ret != addr1) + frame_lp[addr_ret] = frame_lp[addr1]; + } + else { + if (addr_ret != addr2) + frame_lp[addr_ret] = frame_lp[addr2]; + } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SELECT_64): - { - cond = frame_lp[GET_OFFSET()]; - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - addr_ret = GET_OFFSET(); - - if (!cond) { - if (addr_ret != addr1) - PUT_I64_TO_ADDR(frame_lp + addr_ret, - GET_I64_FROM_ADDR(frame_lp + addr1)); - } - else { - if (addr_ret != addr2) - PUT_I64_TO_ADDR(frame_lp + addr_ret, - GET_I64_FROM_ADDR(frame_lp + addr2)); - } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_SELECT_64) + { + cond = frame_lp[GET_OFFSET()]; + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + addr_ret = GET_OFFSET(); + + if (!cond) { + if (addr_ret != addr1) + PUT_I64_TO_ADDR(frame_lp + addr_ret, + GET_I64_FROM_ADDR(frame_lp + addr1)); + } + else { + if (addr_ret != addr2) + PUT_I64_TO_ADDR(frame_lp + addr_ret, + GET_I64_FROM_ADDR(frame_lp + addr2)); + } + HANDLE_OP_END(); + } #if WASM_ENABLE_REF_TYPES != 0 - HANDLE_OP (WASM_OP_TABLE_GET): - { - uint32 tbl_idx, elem_idx; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_TABLE_GET) + { + uint32 tbl_idx, elem_idx; + WASMTableInstance *tbl_inst; - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->table_count); + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->table_count); - tbl_inst = wasm_get_table_inst(module, tbl_idx); + tbl_inst = wasm_get_table_inst(module, tbl_idx); - elem_idx = POP_I32(); - if (elem_idx >= tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + elem_idx = POP_I32(); + if (elem_idx >= tbl_inst->cur_size) { + wasm_set_exception(module, "out of bounds table access"); + goto got_exception; + } + + PUSH_I32(((uint32 *)tbl_inst->base_addr)[elem_idx]); + HANDLE_OP_END(); } - PUSH_I32(((uint32 *)tbl_inst->base_addr)[elem_idx]); - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_TABLE_SET) + { + uint32 tbl_idx, elem_idx, val; + WASMTableInstance *tbl_inst; - HANDLE_OP (WASM_OP_TABLE_SET): - { - uint32 tbl_idx, elem_idx, val; - WASMTableInstance *tbl_inst; + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->table_count); - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->table_count); + tbl_inst = wasm_get_table_inst(module, tbl_idx); - tbl_inst = wasm_get_table_inst(module, tbl_idx); + val = POP_I32(); + elem_idx = POP_I32(); + if (elem_idx >= tbl_inst->cur_size) { + wasm_set_exception(module, "out of bounds table access"); + goto got_exception; + } - val = POP_I32(); - elem_idx = POP_I32(); - if (elem_idx >= tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + ((uint32 *)tbl_inst->base_addr)[elem_idx] = val; + HANDLE_OP_END(); } - ((uint32 *)tbl_inst->base_addr)[elem_idx] = val; - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_REF_NULL): - { - PUSH_I32(NULL_REF); - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_REF_NULL) + { + PUSH_I32(NULL_REF); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_REF_IS_NULL): - { - uint32 val; - val = POP_I32(); - PUSH_I32(val == NULL_REF ? 1 : 0); - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_REF_IS_NULL) + { + uint32 val; + val = POP_I32(); + PUSH_I32(val == NULL_REF ? 1 : 0); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_REF_FUNC): - { - uint32 func_idx = read_uint32(frame_ip); - PUSH_I32(func_idx); - HANDLE_OP_END(); - } + HANDLE_OP(WASM_OP_REF_FUNC) + { + uint32 func_idx = read_uint32(frame_ip); + PUSH_I32(func_idx); + HANDLE_OP_END(); + } #endif /* WASM_ENABLE_REF_TYPES */ - /* variable instructions */ - HANDLE_OP (EXT_OP_SET_LOCAL_FAST): - HANDLE_OP (EXT_OP_TEE_LOCAL_FAST): - { + /* variable instructions */ + HANDLE_OP(EXT_OP_SET_LOCAL_FAST) + HANDLE_OP(EXT_OP_TEE_LOCAL_FAST) + { #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 - local_offset = *frame_ip++; + local_offset = *frame_ip++; #else - local_offset = *frame_ip; - frame_ip += 2; + /* clang-format off */ + local_offset = *frame_ip; + frame_ip += 2; + /* clang-format on */ #endif - *(uint32*)(frame_lp + local_offset) = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - HANDLE_OP_END (); - } + *(uint32 *)(frame_lp + local_offset) = + GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + HANDLE_OP_END(); + } - HANDLE_OP (EXT_OP_SET_LOCAL_FAST_I64): - HANDLE_OP (EXT_OP_TEE_LOCAL_FAST_I64): - { + HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64) + HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64) + { #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 - local_offset = *frame_ip++; + local_offset = *frame_ip++; #else - local_offset = *frame_ip; - frame_ip += 2; + /* clang-format off */ + local_offset = *frame_ip; + frame_ip += 2; + /* clang-format on */ #endif - PUT_I64_TO_ADDR((uint32*)(frame_lp + local_offset), - GET_OPERAND(uint64, I64, 0)); - frame_ip += 2; - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_GET_GLOBAL): - { - global_idx = read_uint32(frame_ip); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - addr_ret = GET_OFFSET(); - frame_lp[addr_ret] = *(uint32*)global_addr; - HANDLE_OP_END (); - } + PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset), + GET_OPERAND(uint64, I64, 0)); + frame_ip += 2; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_GET_GLOBAL_64): - { - global_idx = read_uint32(frame_ip); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - addr_ret = GET_OFFSET(); - PUT_I64_TO_ADDR(frame_lp + addr_ret, - GET_I64_FROM_ADDR((uint32*)global_addr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_GET_GLOBAL) + { + global_idx = read_uint32(frame_ip); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + addr_ret = GET_OFFSET(); + frame_lp[addr_ret] = *(uint32 *)global_addr; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_GLOBAL): - { - global_idx = read_uint32(frame_ip); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - addr1 = GET_OFFSET(); - *(int32*)global_addr = frame_lp[addr1]; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_GET_GLOBAL_64) + { + global_idx = read_uint32(frame_ip); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + addr_ret = GET_OFFSET(); + PUT_I64_TO_ADDR(frame_lp + addr_ret, + GET_I64_FROM_ADDR((uint32 *)global_addr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_GLOBAL_AUX_STACK): - { - uint32 aux_stack_top; + HANDLE_OP(WASM_OP_SET_GLOBAL) + { + global_idx = read_uint32(frame_ip); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + addr1 = GET_OFFSET(); + *(int32 *)global_addr = frame_lp[addr1]; + HANDLE_OP_END(); + } - global_idx = read_uint32(frame_ip); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - aux_stack_top = frame_lp[GET_OFFSET()]; - if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) { - wasm_set_exception(module, "wasm auxiliary stack overflow"); - goto got_exception; - } - if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { - wasm_set_exception(module, "wasm auxiliary stack underflow"); - goto got_exception; - } - *(int32*)global_addr = aux_stack_top; + HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK) + { + uint32 aux_stack_top; + + global_idx = read_uint32(frame_ip); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + aux_stack_top = frame_lp[GET_OFFSET()]; + if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) { + wasm_set_exception(module, "wasm auxiliary stack overflow"); + goto got_exception; + } + if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { + wasm_set_exception(module, + "wasm auxiliary stack underflow"); + goto got_exception; + } + *(int32 *)global_addr = aux_stack_top; #if WASM_ENABLE_MEMORY_PROFILING != 0 - if (module->module->aux_stack_top_global_index != (uint32)-1) { - uint32 aux_stack_used = - module->module->aux_stack_bottom - *(uint32*)global_addr; - if (aux_stack_used > module->max_aux_stack_used) - module->max_aux_stack_used = aux_stack_used; - } + if (module->module->aux_stack_top_global_index != (uint32)-1) { + uint32 aux_stack_used = module->module->aux_stack_bottom + - *(uint32 *)global_addr; + if (aux_stack_used > module->max_aux_stack_used) + module->max_aux_stack_used = aux_stack_used; + } #endif - HANDLE_OP_END (); - } + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_SET_GLOBAL_64): - { - global_idx = read_uint32(frame_ip); - bh_assert(global_idx < module->global_count); - global = globals + global_idx; -#if WASM_ENABLE_MULTI_MODULE == 0 - global_addr = global_data + global->data_offset; -#else - global_addr = global->import_global_inst - ? global->import_module_inst->global_data - + global->import_global_inst->data_offset - : global_data + global->data_offset; -#endif - addr1 = GET_OFFSET(); - PUT_I64_TO_ADDR((uint32*)global_addr, - GET_I64_FROM_ADDR(frame_lp + addr1)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_SET_GLOBAL_64) + { + global_idx = read_uint32(frame_ip); + bh_assert(global_idx < module->global_count); + global = globals + global_idx; + global_addr = get_global_addr(global_data, global); + addr1 = GET_OFFSET(); + PUT_I64_TO_ADDR((uint32 *)global_addr, + GET_I64_FROM_ADDR(frame_lp + addr1)); + HANDLE_OP_END(); + } - /* memory load instructions */ - HANDLE_OP (WASM_OP_I32_LOAD): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(4); - frame_lp[addr_ret] = LOAD_I32(maddr); - HANDLE_OP_END (); - } + /* memory load instructions */ + HANDLE_OP(WASM_OP_I32_LOAD) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(4); + frame_lp[addr_ret] = LOAD_I32(maddr); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(8); - PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(8); + PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD8_S): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(1); - frame_lp[addr_ret] = sign_ext_8_32(*(int8*)maddr); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_LOAD8_S) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(1); + frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD8_U): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(1); - frame_lp[addr_ret] = (uint32)(*(uint8*)maddr); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_LOAD8_U) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(1); + frame_lp[addr_ret] = (uint32)(*(uint8 *)maddr); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD16_S): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(2); - frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_LOAD16_S) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(2); + frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LOAD16_U): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(2); - frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_LOAD16_U) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(2); + frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD8_S): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(1); - PUT_I64_TO_ADDR(frame_lp + addr_ret, sign_ext_8_64(*(int8*)maddr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD8_S) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(1); + PUT_I64_TO_ADDR(frame_lp + addr_ret, + sign_ext_8_64(*(int8 *)maddr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD8_U): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(1); - PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8*)maddr)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD8_U) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(1); + PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr)); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD16_S): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(2); - PUT_I64_TO_ADDR(frame_lp + addr_ret, sign_ext_16_64(LOAD_I16(maddr))); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD16_S) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(2); + PUT_I64_TO_ADDR(frame_lp + addr_ret, + sign_ext_16_64(LOAD_I16(maddr))); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD16_U): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(2); - PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr))); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD16_U) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(2); + PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr))); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD32_S): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(4); - PUT_I64_TO_ADDR(frame_lp + addr_ret, sign_ext_32_64(LOAD_I32(maddr))); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD32_S) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(4); + PUT_I64_TO_ADDR(frame_lp + addr_ret, + sign_ext_32_64(LOAD_I32(maddr))); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LOAD32_U): - { - uint32 offset, addr; - offset = read_uint32(frame_ip); - addr = GET_OPERAND(uint32, I32, 0); - frame_ip += 2; - addr_ret = GET_OFFSET(); - CHECK_MEMORY_OVERFLOW(4); - PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr))); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_LOAD32_U) + { + uint32 offset, addr; + offset = read_uint32(frame_ip); + addr = GET_OPERAND(uint32, I32, 0); + frame_ip += 2; + addr_ret = GET_OFFSET(); + CHECK_MEMORY_OVERFLOW(4); + PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr))); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_STORE): - { - uint32 offset, addr; - uint32 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint32, I32, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(4); - STORE_U32(maddr, sval); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_STORE) + { + uint32 offset, addr; + uint32 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint32, I32, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(4); + STORE_U32(maddr, sval); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_STORE8): - { - uint32 offset, addr; - uint32 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint32, I32, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(1); - *(uint8*)maddr = (uint8)sval; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_STORE8) + { + uint32 offset, addr; + uint32 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint32, I32, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(1); + *(uint8 *)maddr = (uint8)sval; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_STORE16): - { - uint32 offset, addr; - uint32 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint32, I32, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(2); - STORE_U16(maddr, (uint16)sval); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_STORE16) + { + uint32 offset, addr; + uint32 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint32, I32, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(2); + STORE_U16(maddr, (uint16)sval); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_STORE): - { - uint32 offset, addr; - uint64 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint64, I64, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(8); - STORE_I64(maddr, sval); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_STORE) + { + uint32 offset, addr; + uint64 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint64, I64, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(8); + STORE_I64(maddr, sval); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_STORE8): - { - uint32 offset, addr; - uint64 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint64, I64, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(1); - *(uint8*)maddr = (uint8)sval; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_STORE8) + { + uint32 offset, addr; + uint64 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint64, I64, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(1); + *(uint8 *)maddr = (uint8)sval; + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_STORE16): - { - uint32 offset, addr; - uint64 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint64, I64, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(2); - STORE_U16(maddr, (uint16)sval); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_STORE16) + { + uint32 offset, addr; + uint64 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint64, I64, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(2); + STORE_U16(maddr, (uint16)sval); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_STORE32): - { - uint32 offset, addr; - uint64 sval; - offset = read_uint32(frame_ip); - sval = GET_OPERAND(uint64, I64, 0); - addr = GET_OPERAND(uint32, I32, 2); - frame_ip += 4; - CHECK_MEMORY_OVERFLOW(4); - STORE_U32(maddr, (uint32)sval); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_STORE32) + { + uint32 offset, addr; + uint64 sval; + offset = read_uint32(frame_ip); + sval = GET_OPERAND(uint64, I64, 0); + addr = GET_OPERAND(uint32, I32, 2); + frame_ip += 4; + CHECK_MEMORY_OVERFLOW(4); + STORE_U32(maddr, (uint32)sval); + HANDLE_OP_END(); + } - /* memory size and memory grow instructions */ - HANDLE_OP (WASM_OP_MEMORY_SIZE): - { - uint32 reserved; - addr_ret = GET_OFFSET(); - frame_lp[addr_ret] = memory->cur_page_count; - (void)reserved; - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_MEMORY_GROW): - { - uint32 reserved, delta, prev_page_count = memory->cur_page_count; - - addr1 = GET_OFFSET(); - addr_ret = GET_OFFSET(); - delta = (uint32)frame_lp[addr1]; - - if (!wasm_enlarge_memory(module, delta)) { - /* failed to memory.grow, return -1 */ - frame_lp[addr_ret] = -1; - } - else { - /* success, return previous page count */ - frame_lp[addr_ret] = prev_page_count; - /* update memory instance ptr and memory size */ - memory = module->default_memory; - linear_mem_size = num_bytes_per_page * memory->cur_page_count; - } + /* memory size and memory grow instructions */ + HANDLE_OP(WASM_OP_MEMORY_SIZE) + { + uint32 reserved; + addr_ret = GET_OFFSET(); + frame_lp[addr_ret] = memory->cur_page_count; + (void)reserved; + HANDLE_OP_END(); + } - (void)reserved; - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_MEMORY_GROW) + { + uint32 reserved, delta, + prev_page_count = memory->cur_page_count; + + addr1 = GET_OFFSET(); + addr_ret = GET_OFFSET(); + delta = (uint32)frame_lp[addr1]; + + if (!wasm_enlarge_memory(module, delta)) { + /* failed to memory.grow, return -1 */ + frame_lp[addr_ret] = -1; + } + else { + /* success, return previous page count */ + frame_lp[addr_ret] = prev_page_count; + /* update memory instance ptr and memory size */ + memory = module->default_memory; + linear_mem_size = + num_bytes_per_page * memory->cur_page_count; + } + + (void)reserved; + HANDLE_OP_END(); + } - /* comparison instructions of i32 */ - HANDLE_OP (WASM_OP_I32_EQZ): - DEF_OP_EQZ(int32, I32); - HANDLE_OP_END (); + /* comparison instructions of i32 */ + HANDLE_OP(WASM_OP_I32_EQZ) + { + DEF_OP_EQZ(int32, I32); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_EQ): - DEF_OP_CMP(uint32, I32, ==); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_EQ) + { + DEF_OP_CMP(uint32, I32, ==); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_NE): - DEF_OP_CMP(uint32, I32, !=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_NE) + { + DEF_OP_CMP(uint32, I32, !=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LT_S): - DEF_OP_CMP(int32, I32, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LT_S) + { + DEF_OP_CMP(int32, I32, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LT_U): - DEF_OP_CMP(uint32, I32, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LT_U) + { + DEF_OP_CMP(uint32, I32, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GT_S): - DEF_OP_CMP(int32, I32, >); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GT_S) + { + DEF_OP_CMP(int32, I32, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GT_U): - DEF_OP_CMP(uint32, I32, >); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GT_U) + { + DEF_OP_CMP(uint32, I32, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LE_S): - DEF_OP_CMP(int32, I32, <=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LE_S) + { + DEF_OP_CMP(int32, I32, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_LE_U): - DEF_OP_CMP(uint32, I32, <=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_LE_U) + { + DEF_OP_CMP(uint32, I32, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GE_S): - DEF_OP_CMP(int32, I32, >=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GE_S) + { + DEF_OP_CMP(int32, I32, >=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_GE_U): - DEF_OP_CMP(uint32, I32, >=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I32_GE_U) + { + DEF_OP_CMP(uint32, I32, >=); + HANDLE_OP_END(); + } - /* comparison instructions of i64 */ - HANDLE_OP (WASM_OP_I64_EQZ): - DEF_OP_EQZ(int64, I64); - HANDLE_OP_END (); + /* comparison instructions of i64 */ + HANDLE_OP(WASM_OP_I64_EQZ) + { + DEF_OP_EQZ(int64, I64); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_EQ): - DEF_OP_CMP(uint64, I64, ==); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_EQ) + { + DEF_OP_CMP(uint64, I64, ==); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_NE): - DEF_OP_CMP(uint64, I64, !=); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_NE) + { + DEF_OP_CMP(uint64, I64, !=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LT_S): - DEF_OP_CMP(int64, I64, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_LT_S) + { + DEF_OP_CMP(int64, I64, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_LT_U): - DEF_OP_CMP(uint64, I64, <); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_LT_U) + { + DEF_OP_CMP(uint64, I64, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_GT_S): - DEF_OP_CMP(int64, I64, >); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_I64_GT_S) + { + DEF_OP_CMP(int64, I64, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_GT_U): - DEF_OP_CMP(uint64, I64, >); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_LE_S): - DEF_OP_CMP(int64, I64, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_LE_U): - DEF_OP_CMP(uint64, I64, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_GE_S): - DEF_OP_CMP(int64, I64, >=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_GE_U): - DEF_OP_CMP(uint64, I64, >=); - HANDLE_OP_END (); - - /* comparison instructions of f32 */ - HANDLE_OP (WASM_OP_F32_EQ): - DEF_OP_CMP(float32, F32, ==); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_NE): - DEF_OP_CMP(float32, F32, !=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_LT): - DEF_OP_CMP(float32, F32, <); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_GT): - DEF_OP_CMP(float32, F32, >); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_LE): - DEF_OP_CMP(float32, F32, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_GE): - DEF_OP_CMP(float32, F32, >=); - HANDLE_OP_END (); - - /* comparison instructions of f64 */ - HANDLE_OP (WASM_OP_F64_EQ): - DEF_OP_CMP(float64, F64, ==); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_NE): - DEF_OP_CMP(float64, F64, !=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_LT): - DEF_OP_CMP(float64, F64, <); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_GT): - DEF_OP_CMP(float64, F64, >); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_LE): - DEF_OP_CMP(float64, F64, <=); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_GE): - DEF_OP_CMP(float64, F64, >=); - HANDLE_OP_END (); - - /* numberic instructions of i32 */ - HANDLE_OP (WASM_OP_I32_CLZ): - DEF_OP_BIT_COUNT(uint32, I32, clz32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_CTZ): - DEF_OP_BIT_COUNT(uint32, I32, ctz32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_POPCNT): - DEF_OP_BIT_COUNT(uint32, I32, popcount32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_ADD): - DEF_OP_NUMERIC(uint32, uint32, I32, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_SUB): - DEF_OP_NUMERIC(uint32, uint32, I32, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_MUL): - DEF_OP_NUMERIC(uint32, uint32, I32, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_DIV_S): - { - int32 a, b; - - b = frame_lp[GET_OFFSET()]; - a = frame_lp[GET_OFFSET()]; - addr_ret = GET_OFFSET(); - if (a == (int32)0x80000000 && b == -1) { - wasm_set_exception(module, "integer overflow"); - goto got_exception; - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - frame_lp[addr_ret] = (a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_DIV_U): - { - uint32 a, b; - - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - addr_ret = GET_OFFSET(); - - b = (uint32)frame_lp[addr1]; - a = (uint32)frame_lp[addr2]; - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - frame_lp[addr_ret] = (a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_REM_S): - { - int32 a, b; - - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - addr_ret = GET_OFFSET(); - - b = frame_lp[addr1]; - a = frame_lp[addr2]; - if (a == (int32)0x80000000 && b == -1) { - frame_lp[addr_ret] = 0; - HANDLE_OP_END (); - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - frame_lp[addr_ret] = (a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_REM_U): - { - uint32 a, b; - - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - addr_ret = GET_OFFSET(); - - b = (uint32)frame_lp[addr1]; - a = (uint32)frame_lp[addr2]; - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - frame_lp[addr_ret] = (a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_AND): - DEF_OP_NUMERIC(uint32, uint32, I32, &); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_OR): - DEF_OP_NUMERIC(uint32, uint32, I32, |); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_XOR): - DEF_OP_NUMERIC(uint32, uint32, I32, ^); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_SHL): - { - DEF_OP_NUMERIC2(uint32, uint32, I32, <<); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_SHR_S): - { - DEF_OP_NUMERIC2(int32, uint32, I32, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_SHR_U): - { - DEF_OP_NUMERIC2(uint32, uint32, I32, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_ROTL): - { - uint32 a, b; - - b = (uint32)frame_lp[GET_OFFSET()]; - a = (uint32)frame_lp[GET_OFFSET()]; - frame_lp[GET_OFFSET()] = rotl32(a, b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I32_ROTR): - { - uint32 a, b; - - b = (uint32)frame_lp[GET_OFFSET()]; - a = (uint32)frame_lp[GET_OFFSET()]; - frame_lp[GET_OFFSET()] = rotr32(a, b); - HANDLE_OP_END (); - } - - /* numberic instructions of i64 */ - HANDLE_OP (WASM_OP_I64_CLZ): - DEF_OP_BIT_COUNT(uint64, I64, clz64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_CTZ): - DEF_OP_BIT_COUNT(uint64, I64, ctz64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_POPCNT): - DEF_OP_BIT_COUNT(uint64, I64, popcount64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_ADD): - DEF_OP_NUMERIC_64(uint64, uint64, I64, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_SUB): - DEF_OP_NUMERIC_64(uint64, uint64, I64, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_MUL): - DEF_OP_NUMERIC_64(uint64, uint64, I64, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_DIV_S): - { - int64 a, b; - - b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - if (a == (int64)0x8000000000000000LL && b == -1) { - wasm_set_exception(module, "integer overflow"); - goto got_exception; - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_DIV_U): - { - uint64 a, b; - - b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_REM_S): - { - int64 a, b; - - b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - if (a == (int64)0x8000000000000000LL && b == -1) { - *(int64*)(frame_lp + GET_OFFSET()) = 0; - HANDLE_OP_END (); - } - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_REM_U): - { - uint64 a, b; - - b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - if (b == 0) { - wasm_set_exception(module, "integer divide by zero"); - goto got_exception; - } - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_AND): - DEF_OP_NUMERIC_64(uint64, uint64, I64, &); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_OR): - DEF_OP_NUMERIC_64(uint64, uint64, I64, |); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_XOR): - DEF_OP_NUMERIC_64(uint64, uint64, I64, ^); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_SHL): - { - DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_SHR_S): - { - DEF_OP_NUMERIC2_64(int64, uint64, I64, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_SHR_U): - { - DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_ROTL): - { - uint64 a, b; - - b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b)); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_I64_ROTR): - { - uint64 a, b; - - b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b)); - HANDLE_OP_END (); - } - - /* numberic instructions of f32 */ - HANDLE_OP (WASM_OP_F32_ABS): - DEF_OP_MATH(float32, F32, fabs); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_NEG): - { - uint32 u32 = frame_lp[GET_OFFSET()]; - uint32 sign_bit = u32 & ((uint32)1 << 31); - addr_ret = GET_OFFSET(); - if (sign_bit) - frame_lp[addr_ret] = u32 & ~((uint32)1 << 31); - else - frame_lp[addr_ret] = u32 | ((uint32)1 << 31); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F32_CEIL): - DEF_OP_MATH(float32, F32, ceil); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_FLOOR): - DEF_OP_MATH(float32, F32, floor); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_TRUNC): - DEF_OP_MATH(float32, F32, trunc); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_NEAREST): - DEF_OP_MATH(float32, F32, rint); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_SQRT): - DEF_OP_MATH(float32, F32, sqrt); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_ADD): - DEF_OP_NUMERIC(float32, float32, F32, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_SUB): - DEF_OP_NUMERIC(float32, float32, F32, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_MUL): - DEF_OP_NUMERIC(float32, float32, F32, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_DIV): - DEF_OP_NUMERIC(float32, float32, F32, /); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_MIN): - { - float32 a, b; - - b = *(float32*)(frame_lp + GET_OFFSET()); - a = *(float32*)(frame_lp + GET_OFFSET()); - - if (isnan(a)) - *(float32*)(frame_lp + GET_OFFSET()) = a; - else if (isnan(b)) - *(float32*)(frame_lp + GET_OFFSET()) = b; - else - *(float32*)(frame_lp + GET_OFFSET()) = (float32)wa_fmin(a, b); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_GT_U) + { + DEF_OP_CMP(uint64, I64, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_F32_MAX): - { - float32 a, b; + HANDLE_OP(WASM_OP_I64_LE_S) + { + DEF_OP_CMP(int64, I64, <=); + HANDLE_OP_END(); + } - b = *(float32*)(frame_lp + GET_OFFSET()); - a = *(float32*)(frame_lp + GET_OFFSET()); + HANDLE_OP(WASM_OP_I64_LE_U) + { + DEF_OP_CMP(uint64, I64, <=); + HANDLE_OP_END(); + } - if (isnan(a)) - *(float32*)(frame_lp + GET_OFFSET()) = a; - else if (isnan(b)) - *(float32*)(frame_lp + GET_OFFSET()) = b; - else - *(float32*)(frame_lp + GET_OFFSET()) = (float32)wa_fmax(a, b); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F32_COPYSIGN): - { - float32 a, b; - - b = *(float32*)(frame_lp + GET_OFFSET()); - a = *(float32*)(frame_lp + GET_OFFSET()); - *(float32*)(frame_lp + GET_OFFSET()) = - (float32)(signbit(b) ? -fabs(a) : fabs(a)); - HANDLE_OP_END (); - } - - /* numberic instructions of f64 */ - HANDLE_OP (WASM_OP_F64_ABS): - DEF_OP_MATH(float64, F64, fabs); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_NEG): - { - uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); - uint64 sign_bit = u64 & (((uint64)1) << 63); - if (sign_bit) - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), - (u64 & ~(((uint64)1) << 63))); - else - PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), - (u64 | (((uint64)1) << 63))); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F64_CEIL): - DEF_OP_MATH(float64, F64, ceil); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_FLOOR): - DEF_OP_MATH(float64, F64, floor); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_TRUNC): - DEF_OP_MATH(float64, F64, trunc); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_NEAREST): - DEF_OP_MATH(float64, F64, rint); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_SQRT): - DEF_OP_MATH(float64, F64, sqrt); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_ADD): - DEF_OP_NUMERIC_64(float64, float64, F64, +); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_SUB): - DEF_OP_NUMERIC_64(float64, float64, F64, -); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_MUL): - DEF_OP_NUMERIC_64(float64, float64, F64, *); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_DIV): - DEF_OP_NUMERIC_64(float64, float64, F64, /); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_MIN): - { - float64 a, b; - - b = POP_F64(); - a = POP_F64(); - - if (isnan(a)) - PUSH_F64(a); - else if (isnan(b)) - PUSH_F64(b); - else - PUSH_F64(wa_fmin(a, b)); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I64_GE_S) + { + DEF_OP_CMP(int64, I64, >=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_F64_MAX): - { - float64 a, b; + HANDLE_OP(WASM_OP_I64_GE_U) + { + DEF_OP_CMP(uint64, I64, >=); + HANDLE_OP_END(); + } - b = POP_F64(); - a = POP_F64(); + /* comparison instructions of f32 */ + HANDLE_OP(WASM_OP_F32_EQ) + { + DEF_OP_CMP(float32, F32, ==); + HANDLE_OP_END(); + } - if (isnan(a)) - PUSH_F64(a); - else if (isnan(b)) - PUSH_F64(b); - else - PUSH_F64(wa_fmax(a, b)); - HANDLE_OP_END (); - } - - HANDLE_OP (WASM_OP_F64_COPYSIGN): - { - float64 a, b; - - b = POP_F64(); - a = POP_F64(); - PUSH_F64(signbit(b) ? -fabs(a) : fabs(a)); - HANDLE_OP_END (); - } - - /* conversions of i32 */ - HANDLE_OP (WASM_OP_I32_WRAP_I64): - { - int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL); - PUSH_I32(value); - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_F32_NE) + { + DEF_OP_CMP(float32, F32, !=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_TRUNC_S_F32): - /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX, - since float/double values of ieee754 cannot precisely represent - all int32/uint32/int64/uint64 values, e.g.: - UINT32_MAX is 4294967295, but (float32)4294967295 is 4294967296.0f, - but not 4294967295.0f. */ - DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_TRUNC_U_F32): - DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_TRUNC_S_F64): - DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I32_TRUNC_U_F64): - DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false); - HANDLE_OP_END (); - - /* conversions of i64 */ - HANDLE_OP (WASM_OP_I64_EXTEND_S_I32): - DEF_OP_CONVERT(int64, I64, int32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_EXTEND_U_I32): - DEF_OP_CONVERT(int64, I64, uint32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_S_F32): - DEF_OP_TRUNC_F32(-9223373136366403584.0f, - 9223372036854775808.0f, false, true); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_U_F32): - DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, - false, false); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_S_F64): - DEF_OP_TRUNC_F64(-9223372036854777856.0, - 9223372036854775808.0, false, true); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_I64_TRUNC_U_F64): - DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, - false, false); - HANDLE_OP_END (); - - /* conversions of f32 */ - HANDLE_OP (WASM_OP_F32_CONVERT_S_I32): - DEF_OP_CONVERT(float32, F32, int32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_CONVERT_U_I32): - DEF_OP_CONVERT(float32, F32, uint32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_CONVERT_S_I64): - DEF_OP_CONVERT(float32, F32, int64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_CONVERT_U_I64): - DEF_OP_CONVERT(float32, F32, uint64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F32_DEMOTE_F64): - DEF_OP_CONVERT(float32, F32, float64, F64); - HANDLE_OP_END (); - - /* conversions of f64 */ - HANDLE_OP (WASM_OP_F64_CONVERT_S_I32): - DEF_OP_CONVERT(float64, F64, int32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_CONVERT_U_I32): - DEF_OP_CONVERT(float64, F64, uint32, I32); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_CONVERT_S_I64): - DEF_OP_CONVERT(float64, F64, int64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_CONVERT_U_I64): - DEF_OP_CONVERT(float64, F64, uint64, I64); - HANDLE_OP_END (); - - HANDLE_OP (WASM_OP_F64_PROMOTE_F32): - DEF_OP_CONVERT(float64, F64, float32, F32); - HANDLE_OP_END (); - - /* reinterpretations */ - HANDLE_OP (WASM_OP_I32_REINTERPRET_F32): - HANDLE_OP (WASM_OP_F32_REINTERPRET_I32): - DEF_OP_REINTERPRET(uint32, I32); - HANDLE_OP_END (); - HANDLE_OP (WASM_OP_I64_REINTERPRET_F64): - HANDLE_OP (WASM_OP_F64_REINTERPRET_I64): - DEF_OP_REINTERPRET(int64, I64); - HANDLE_OP_END (); - - HANDLE_OP (EXT_OP_COPY_STACK_TOP): - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - frame_lp[addr2] = frame_lp[addr1]; - HANDLE_OP_END (); - - HANDLE_OP (EXT_OP_COPY_STACK_TOP_I64): - addr1 = GET_OFFSET(); - addr2 = GET_OFFSET(); - frame_lp[addr2] = frame_lp[addr1]; - frame_lp[addr2 + 1] = frame_lp[addr1 + 1]; - HANDLE_OP_END (); - - HANDLE_OP (EXT_OP_COPY_STACK_VALUES): - { - uint32 values_count, total_cell; - uint8 *cells; - int16 *src_offsets = NULL; - uint16 *dst_offsets = NULL; - - /* read values_count */ - values_count = read_uint32(frame_ip); - /* read total cell num */ - total_cell = read_uint32(frame_ip); - /* cells */ - cells = (uint8 *)frame_ip; - frame_ip += values_count * CELL_SIZE; - /* src offsets */ - src_offsets = (int16 *)frame_ip; - frame_ip += values_count * sizeof(int16); - /* dst offsets */ - dst_offsets = (uint16*)frame_ip; - frame_ip += values_count * sizeof(uint16); - - if (!copy_stack_values(module, frame_lp, values_count, - total_cell, cells, - src_offsets, dst_offsets)) - goto got_exception; - - HANDLE_OP_END (); - } - HANDLE_OP (WASM_OP_SET_LOCAL): - HANDLE_OP (WASM_OP_TEE_LOCAL): - { - GET_LOCAL_INDEX_TYPE_AND_OFFSET(); - addr1 = GET_OFFSET(); - - if (local_type == VALUE_TYPE_I32 - || local_type == VALUE_TYPE_F32) { - *(int32*)(frame_lp + local_offset) = frame_lp[addr1]; - } - else if (local_type == VALUE_TYPE_I64 - || local_type == VALUE_TYPE_F64) { - PUT_I64_TO_ADDR((uint32*)(frame_lp + local_offset), - GET_I64_FROM_ADDR(frame_lp + addr1)); - } - else { - wasm_set_exception(module, "invalid local type"); - goto got_exception; - } + HANDLE_OP(WASM_OP_F32_LT) + { + DEF_OP_CMP(float32, F32, <); + HANDLE_OP_END(); + } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_F32_GT) + { + DEF_OP_CMP(float32, F32, >); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_LE) + { + DEF_OP_CMP(float32, F32, <=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_EXTEND8_S): - DEF_OP_CONVERT(int32, I32, int8, I32); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_F32_GE) + { + DEF_OP_CMP(float32, F32, >=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I32_EXTEND16_S): - DEF_OP_CONVERT(int32, I32, int16, I32); - HANDLE_OP_END (); + /* comparison instructions of f64 */ + HANDLE_OP(WASM_OP_F64_EQ) + { + DEF_OP_CMP(float64, F64, ==); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_EXTEND8_S): - DEF_OP_CONVERT(int64, I64, int8, I64); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_F64_NE) + { + DEF_OP_CMP(float64, F64, !=); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_EXTEND16_S): - DEF_OP_CONVERT(int64, I64, int16, I64); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_F64_LT) + { + DEF_OP_CMP(float64, F64, <); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_I64_EXTEND32_S): - DEF_OP_CONVERT(int64, I64, int32, I64); - HANDLE_OP_END (); + HANDLE_OP(WASM_OP_F64_GT) + { + DEF_OP_CMP(float64, F64, >); + HANDLE_OP_END(); + } - HANDLE_OP (WASM_OP_MISC_PREFIX): - { - GET_OPCODE(); - switch (opcode) - { - case WASM_OP_I32_TRUNC_SAT_S_F32: - DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f, - true, true); - break; - case WASM_OP_I32_TRUNC_SAT_U_F32: - DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, - true, false); - break; - case WASM_OP_I32_TRUNC_SAT_S_F64: - DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, - true, true); - break; - case WASM_OP_I32_TRUNC_SAT_U_F64: - DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, - true, false); - break; - case WASM_OP_I64_TRUNC_SAT_S_F32: - DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f, 9223372036854775808.0f, - false, true); - break; - case WASM_OP_I64_TRUNC_SAT_U_F32: - DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f, - false, false); - break; - case WASM_OP_I64_TRUNC_SAT_S_F64: - DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0, 9223372036854775808.0, - false, true); - break; - case WASM_OP_I64_TRUNC_SAT_U_F64: - DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0, - false, false); - break; -#if WASM_ENABLE_BULK_MEMORY != 0 - case WASM_OP_MEMORY_INIT: - { - uint32 addr, segment; - uint64 bytes, offset, seg_len; - uint8* data; + HANDLE_OP(WASM_OP_F64_LE) + { + DEF_OP_CMP(float64, F64, <=); + HANDLE_OP_END(); + } - segment = read_uint32(frame_ip); + HANDLE_OP(WASM_OP_F64_GE) + { + DEF_OP_CMP(float64, F64, >=); + HANDLE_OP_END(); + } - bytes = (uint64)POP_I32(); - offset = (uint64)POP_I32(); - addr = POP_I32(); + /* numberic instructions of i32 */ + HANDLE_OP(WASM_OP_I32_CLZ) + { + DEF_OP_BIT_COUNT(uint32, I32, clz32); + HANDLE_OP_END(); + } - CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr); + HANDLE_OP(WASM_OP_I32_CTZ) + { + DEF_OP_BIT_COUNT(uint32, I32, ctz32); + HANDLE_OP_END(); + } - seg_len = (uint64)module->module->data_segments[segment]->data_length; - data = module->module->data_segments[segment]->data; - if (offset + bytes > seg_len) - goto out_of_bounds; + HANDLE_OP(WASM_OP_I32_POPCNT) + { + DEF_OP_BIT_COUNT(uint32, I32, popcount32); + HANDLE_OP_END(); + } - bh_memcpy_s(maddr, linear_mem_size - addr, - data + offset, (uint32)bytes); - break; - } - case WASM_OP_DATA_DROP: - { - uint32 segment; + HANDLE_OP(WASM_OP_I32_ADD) + { + DEF_OP_NUMERIC(uint32, uint32, I32, +); + HANDLE_OP_END(); + } - segment = read_uint32(frame_ip); + HANDLE_OP(WASM_OP_I32_SUB) + { + DEF_OP_NUMERIC(uint32, uint32, I32, -); + HANDLE_OP_END(); + } - module->module->data_segments[segment]->data_length = 0; + HANDLE_OP(WASM_OP_I32_MUL) + { + DEF_OP_NUMERIC(uint32, uint32, I32, *); + HANDLE_OP_END(); + } - break; - } - case WASM_OP_MEMORY_COPY: - { - uint32 dst, src, len; - uint8 *mdst, *msrc; + HANDLE_OP(WASM_OP_I32_DIV_S) + { + int32 a, b; + + b = frame_lp[GET_OFFSET()]; + a = frame_lp[GET_OFFSET()]; + addr_ret = GET_OFFSET(); + if (a == (int32)0x80000000 && b == -1) { + wasm_set_exception(module, "integer overflow"); + goto got_exception; + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + frame_lp[addr_ret] = (a / b); + HANDLE_OP_END(); + } - len = POP_I32(); - src = POP_I32(); - dst = POP_I32(); + HANDLE_OP(WASM_OP_I32_DIV_U) + { + uint32 a, b; + + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + addr_ret = GET_OFFSET(); + + b = (uint32)frame_lp[addr1]; + a = (uint32)frame_lp[addr2]; + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + frame_lp[addr_ret] = (a / b); + HANDLE_OP_END(); + } - CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc); - CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + HANDLE_OP(WASM_OP_I32_REM_S) + { + int32 a, b; + + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + addr_ret = GET_OFFSET(); + + b = frame_lp[addr1]; + a = frame_lp[addr2]; + if (a == (int32)0x80000000 && b == -1) { + frame_lp[addr_ret] = 0; + HANDLE_OP_END(); + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + frame_lp[addr_ret] = (a % b); + HANDLE_OP_END(); + } - /* allowing the destination and source to overlap */ - bh_memmove_s(mdst, linear_mem_size - dst, msrc, len); + HANDLE_OP(WASM_OP_I32_REM_U) + { + uint32 a, b; + + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + addr_ret = GET_OFFSET(); + + b = (uint32)frame_lp[addr1]; + a = (uint32)frame_lp[addr2]; + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + frame_lp[addr_ret] = (a % b); + HANDLE_OP_END(); + } - break; - } - case WASM_OP_MEMORY_FILL: - { - uint32 dst, len; - uint8 val, *mdst; + HANDLE_OP(WASM_OP_I32_AND) + { + DEF_OP_NUMERIC(uint32, uint32, I32, &); + HANDLE_OP_END(); + } - len = POP_I32(); - val = POP_I32(); - dst = POP_I32(); + HANDLE_OP(WASM_OP_I32_OR) + { + DEF_OP_NUMERIC(uint32, uint32, I32, |); + HANDLE_OP_END(); + } - CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + HANDLE_OP(WASM_OP_I32_XOR) + { + DEF_OP_NUMERIC(uint32, uint32, I32, ^); + HANDLE_OP_END(); + } - memset(mdst, val, len); + HANDLE_OP(WASM_OP_I32_SHL) + { + DEF_OP_NUMERIC2(uint32, uint32, I32, <<); + HANDLE_OP_END(); + } - break; - } -#endif /* WASM_ENABLE_BULK_MEMORY */ -#if WASM_ENABLE_REF_TYPES != 0 - case WASM_OP_TABLE_INIT: - { - uint32 tbl_idx, elem_idx; - uint64 n, s, d; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I32_SHR_S) + { + DEF_OP_NUMERIC2(int32, uint32, I32, >>); + HANDLE_OP_END(); + } - elem_idx = read_uint32(frame_ip); - bh_assert(elem_idx < module->module->table_seg_count); + HANDLE_OP(WASM_OP_I32_SHR_U) + { + DEF_OP_NUMERIC2(uint32, uint32, I32, >>); + HANDLE_OP_END(); + } - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->module->table_count); + HANDLE_OP(WASM_OP_I32_ROTL) + { + uint32 a, b; - tbl_inst = wasm_get_table_inst(module, tbl_idx); + b = (uint32)frame_lp[GET_OFFSET()]; + a = (uint32)frame_lp[GET_OFFSET()]; + frame_lp[GET_OFFSET()] = rotl32(a, b); + HANDLE_OP_END(); + } - n = (uint32)POP_I32(); - s = (uint32)POP_I32(); - d = (uint32)POP_I32(); + HANDLE_OP(WASM_OP_I32_ROTR) + { + uint32 a, b; - if (!n) { - break; + b = (uint32)frame_lp[GET_OFFSET()]; + a = (uint32)frame_lp[GET_OFFSET()]; + frame_lp[GET_OFFSET()] = rotr32(a, b); + HANDLE_OP_END(); } - if (n + s > module->module->table_segments[elem_idx].function_count - || d + n > tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + /* numberic instructions of i64 */ + HANDLE_OP(WASM_OP_I64_CLZ) + { + DEF_OP_BIT_COUNT(uint64, I64, clz64); + HANDLE_OP_END(); } - if (module->module->table_segments[elem_idx].is_dropped) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + HANDLE_OP(WASM_OP_I64_CTZ) + { + DEF_OP_BIT_COUNT(uint64, I64, ctz64); + HANDLE_OP_END(); } - if (!wasm_elem_is_passive( - module->module->table_segments[elem_idx].mode)) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + HANDLE_OP(WASM_OP_I64_POPCNT) + { + DEF_OP_BIT_COUNT(uint64, I64, popcount64); + HANDLE_OP_END(); } - bh_memcpy_s( - (uint8 *)tbl_inst + offsetof(WASMTableInstance, base_addr) - + d * sizeof(uint32), - (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), - module->module->table_segments[elem_idx].func_indexes + s, - (uint32)(n * sizeof(uint32))); - break; - } - case WASM_OP_ELEM_DROP: - { - uint32 elem_idx = read_uint32(frame_ip); - bh_assert(elem_idx < module->module->table_seg_count); + HANDLE_OP(WASM_OP_I64_ADD) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, +); + HANDLE_OP_END(); + } - module->module->table_segments[elem_idx].is_dropped = true; - break; - } - case WASM_OP_TABLE_COPY: - { - uint32 src_tbl_idx, dst_tbl_idx; - uint64 n, s, d; - WASMTableInstance *src_tbl_inst, *dst_tbl_inst; + HANDLE_OP(WASM_OP_I64_SUB) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, -); + HANDLE_OP_END(); + } - dst_tbl_idx = read_uint32(frame_ip); - bh_assert(dst_tbl_idx < module->table_count); + HANDLE_OP(WASM_OP_I64_MUL) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, *); + HANDLE_OP_END(); + } - dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx); + HANDLE_OP(WASM_OP_I64_DIV_S) + { + int64 a, b; + + b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + if (a == (int64)0x8000000000000000LL && b == -1) { + wasm_set_exception(module, "integer overflow"); + goto got_exception; + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b); + HANDLE_OP_END(); + } - src_tbl_idx = read_uint32(frame_ip); - bh_assert(src_tbl_idx < module->table_count); + HANDLE_OP(WASM_OP_I64_DIV_U) + { + uint64 a, b; + + b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b); + HANDLE_OP_END(); + } - src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx); + HANDLE_OP(WASM_OP_I64_REM_S) + { + int64 a, b; + + b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + if (a == (int64)0x8000000000000000LL && b == -1) { + *(int64 *)(frame_lp + GET_OFFSET()) = 0; + HANDLE_OP_END(); + } + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b); + HANDLE_OP_END(); + } - n = (uint32)POP_I32(); - s = (uint32)POP_I32(); - d = (uint32)POP_I32(); + HANDLE_OP(WASM_OP_I64_REM_U) + { + uint64 a, b; + + b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + if (b == 0) { + wasm_set_exception(module, "integer divide by zero"); + goto got_exception; + } + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b); + HANDLE_OP_END(); + } - if (s + n > dst_tbl_inst->cur_size - || d + n > src_tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + HANDLE_OP(WASM_OP_I64_AND) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, &); + HANDLE_OP_END(); } - /* if s >= d, copy from front to back */ - /* if s < d, copy from back to front */ - /* merge all together */ - bh_memmove_s( - (uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, base_addr) - + d * sizeof(uint32), - (uint32)((dst_tbl_inst->cur_size - d) * sizeof(uint32)), - (uint8 *)src_tbl_inst - + offsetof(WASMTableInstance, base_addr) + s * sizeof(uint32), - (uint32)(n * sizeof(uint32))); - break; - } - case WASM_OP_TABLE_GROW: - { - uint32 tbl_idx, n, init_val, orig_tbl_sz; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I64_OR) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, |); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_XOR) + { + DEF_OP_NUMERIC_64(uint64, uint64, I64, ^); + HANDLE_OP_END(); + } - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->table_count); + HANDLE_OP(WASM_OP_I64_SHL) + { + DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + HANDLE_OP(WASM_OP_I64_SHR_S) + { + DEF_OP_NUMERIC2_64(int64, uint64, I64, >>); + HANDLE_OP_END(); + } - orig_tbl_sz = tbl_inst->cur_size; + HANDLE_OP(WASM_OP_I64_SHR_U) + { + DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>); + HANDLE_OP_END(); + } - n = POP_I32(); - init_val = POP_I32(); + HANDLE_OP(WASM_OP_I64_ROTL) + { + uint64 a, b; - if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) { - PUSH_I32(-1); - } else { - PUSH_I32(orig_tbl_sz); + b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b)); + HANDLE_OP_END(); } - break; - } - case WASM_OP_TABLE_SIZE: - { - uint32 tbl_idx; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_I64_ROTR) + { + uint64 a, b; - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->table_count); + b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b)); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + /* numberic instructions of f32 */ + HANDLE_OP(WASM_OP_F32_ABS) + { + DEF_OP_MATH(float32, F32, fabs); + HANDLE_OP_END(); + } - PUSH_I32(tbl_inst->cur_size); - break; - } - case WASM_OP_TABLE_FILL: - { - uint32 tbl_idx, n, val, i; - WASMTableInstance *tbl_inst; + HANDLE_OP(WASM_OP_F32_NEG) + { + uint32 u32 = frame_lp[GET_OFFSET()]; + uint32 sign_bit = u32 & ((uint32)1 << 31); + addr_ret = GET_OFFSET(); + if (sign_bit) + frame_lp[addr_ret] = u32 & ~((uint32)1 << 31); + else + frame_lp[addr_ret] = u32 | ((uint32)1 << 31); + HANDLE_OP_END(); + } - tbl_idx = read_uint32(frame_ip); - bh_assert(tbl_idx < module->table_count); + HANDLE_OP(WASM_OP_F32_CEIL) + { + DEF_OP_MATH(float32, F32, ceil); + HANDLE_OP_END(); + } - tbl_inst = wasm_get_table_inst(module, tbl_idx); + HANDLE_OP(WASM_OP_F32_FLOOR) + { + DEF_OP_MATH(float32, F32, floor); + HANDLE_OP_END(); + } - n = POP_I32(); - val = POP_I32(); - i = POP_I32(); + HANDLE_OP(WASM_OP_F32_TRUNC) + { + DEF_OP_MATH(float32, F32, trunc); + HANDLE_OP_END(); + } - if (i + n > tbl_inst->cur_size) { - wasm_set_exception(module, "out of bounds table access"); - goto got_exception; + HANDLE_OP(WASM_OP_F32_NEAREST) + { + DEF_OP_MATH(float32, F32, rint); + HANDLE_OP_END(); } - for (; n != 0; i++, n--) { - ((uint32 *)(tbl_inst->base_addr))[i] = val; + HANDLE_OP(WASM_OP_F32_SQRT) + { + DEF_OP_MATH(float32, F32, sqrt); + HANDLE_OP_END(); } - break; - } -#endif /* WASM_ENABLE_REF_TYPES */ - default: - wasm_set_exception(module, "unsupported opcode"); - goto got_exception; - } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_F32_ADD) + { + DEF_OP_NUMERIC(float32, float32, F32, +); + HANDLE_OP_END(); + } -#if WASM_ENABLE_SHARED_MEMORY != 0 - HANDLE_OP (WASM_OP_ATOMIC_PREFIX): - { - uint32 offset, addr; + HANDLE_OP(WASM_OP_F32_SUB) + { + DEF_OP_NUMERIC(float32, float32, F32, -); + HANDLE_OP_END(); + } - GET_OPCODE(); + HANDLE_OP(WASM_OP_F32_MUL) + { + DEF_OP_NUMERIC(float32, float32, F32, *); + HANDLE_OP_END(); + } - offset = read_uint32(frame_ip); + HANDLE_OP(WASM_OP_F32_DIV) + { + DEF_OP_NUMERIC(float32, float32, F32, /); + HANDLE_OP_END(); + } - switch (opcode) { - case WASM_OP_ATOMIC_NOTIFY: - { - uint32 count, ret; - - count = POP_I32(); - addr = POP_I32(); - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - - ret = wasm_runtime_atomic_notify((WASMModuleInstanceCommon*)module, - maddr, count); - bh_assert((int32)ret >= 0); - - PUSH_I32(ret); - break; - } - case WASM_OP_ATOMIC_WAIT32: - { - uint64 timeout; - uint32 expect, addr, ret; - - timeout = POP_I64(); - expect = POP_I32(); - addr = POP_I32(); - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - - ret = wasm_runtime_atomic_wait((WASMModuleInstanceCommon*)module, maddr, - (uint64)expect, timeout, false); - if (ret == (uint32)-1) - goto got_exception; - - PUSH_I32(ret); - break; - } - case WASM_OP_ATOMIC_WAIT64: - { - uint64 timeout, expect; - uint32 ret; - - timeout = POP_I64(); - expect = POP_I64(); - addr = POP_I32(); - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(8); - - ret = wasm_runtime_atomic_wait((WASMModuleInstanceCommon*)module, - maddr, expect, timeout, true); - if (ret == (uint32)-1) - goto got_exception; - - PUSH_I32(ret); - break; - } - - case WASM_OP_ATOMIC_I32_LOAD: - case WASM_OP_ATOMIC_I32_LOAD8_U: - case WASM_OP_ATOMIC_I32_LOAD16_U: - { - uint32 readv; - - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(1); - os_mutex_lock(&memory->mem_lock); - readv = (uint32)(*(uint8*)maddr); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(2); - os_mutex_lock(&memory->mem_lock); - readv = (uint32)LOAD_U16(maddr); - os_mutex_unlock(&memory->mem_lock); + HANDLE_OP(WASM_OP_F32_MIN) + { + float32 a, b; + + b = *(float32 *)(frame_lp + GET_OFFSET()); + a = *(float32 *)(frame_lp + GET_OFFSET()); + + if (isnan(a)) + *(float32 *)(frame_lp + GET_OFFSET()) = a; + else if (isnan(b)) + *(float32 *)(frame_lp + GET_OFFSET()) = b; + else + *(float32 *)(frame_lp + GET_OFFSET()) = + (float32)wa_fmin(a, b); + HANDLE_OP_END(); } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - os_mutex_lock(&memory->mem_lock); - readv = LOAD_I32(maddr); - os_mutex_unlock(&memory->mem_lock); - } - - PUSH_I32(readv); - break; - } - - case WASM_OP_ATOMIC_I64_LOAD: - case WASM_OP_ATOMIC_I64_LOAD8_U: - case WASM_OP_ATOMIC_I64_LOAD16_U: - case WASM_OP_ATOMIC_I64_LOAD32_U: - { - uint64 readv; - - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(1); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)(*(uint8*)maddr); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(2); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U16(maddr); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U32(maddr); - os_mutex_unlock(&memory->mem_lock); + + HANDLE_OP(WASM_OP_F32_MAX) + { + float32 a, b; + + b = *(float32 *)(frame_lp + GET_OFFSET()); + a = *(float32 *)(frame_lp + GET_OFFSET()); + + if (isnan(a)) + *(float32 *)(frame_lp + GET_OFFSET()) = a; + else if (isnan(b)) + *(float32 *)(frame_lp + GET_OFFSET()) = b; + else + *(float32 *)(frame_lp + GET_OFFSET()) = + (float32)wa_fmax(a, b); + HANDLE_OP_END(); } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(8); - os_mutex_lock(&memory->mem_lock); - readv = LOAD_I64(maddr); - os_mutex_unlock(&memory->mem_lock); - } - - PUSH_I64(readv); - break; - } - case WASM_OP_ATOMIC_I32_STORE: - case WASM_OP_ATOMIC_I32_STORE8: - case WASM_OP_ATOMIC_I32_STORE16: - { - uint32 sval; - - sval = (uint32)POP_I32(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I32_STORE8) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(1); - os_mutex_lock(&memory->mem_lock); - *(uint8*)maddr = (uint8)sval; - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I32_STORE16) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(2); - os_mutex_lock(&memory->mem_lock); - STORE_U16(maddr, (uint16)sval); - os_mutex_unlock(&memory->mem_lock); + + HANDLE_OP(WASM_OP_F32_COPYSIGN) + { + float32 a, b; + + b = *(float32 *)(frame_lp + GET_OFFSET()); + a = *(float32 *)(frame_lp + GET_OFFSET()); + *(float32 *)(frame_lp + GET_OFFSET()) = + (float32)(signbit(b) ? -fabs(a) : fabs(a)); + HANDLE_OP_END(); } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - os_mutex_lock(&memory->mem_lock); - STORE_U32(maddr, sval); - os_mutex_unlock(&memory->mem_lock); - } - break; - } - - case WASM_OP_ATOMIC_I64_STORE: - case WASM_OP_ATOMIC_I64_STORE8: - case WASM_OP_ATOMIC_I64_STORE16: - case WASM_OP_ATOMIC_I64_STORE32: - { - uint64 sval; - - sval = (uint64)POP_I64(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_I64_STORE8) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(1); - os_mutex_lock(&memory->mem_lock); - *(uint8*)maddr = (uint8)sval; - os_mutex_unlock(&memory->mem_lock); - } - else if(opcode == WASM_OP_ATOMIC_I64_STORE16) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(2); - os_mutex_lock(&memory->mem_lock); - STORE_U16(maddr, (uint16)sval); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_I64_STORE32) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - os_mutex_lock(&memory->mem_lock); - STORE_U32(maddr, (uint32)sval); - os_mutex_unlock(&memory->mem_lock); + + /* numberic instructions of f64 */ + HANDLE_OP(WASM_OP_F64_ABS) + { + DEF_OP_MATH(float64, F64, fabs); + HANDLE_OP_END(); } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(8); - os_mutex_lock(&memory->mem_lock); - STORE_I64(maddr, sval); - os_mutex_unlock(&memory->mem_lock); - } - break; - } - - case WASM_OP_ATOMIC_RMW_I32_CMPXCHG: - case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U: - case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U: - { - uint32 readv, sval, expect; - - sval = POP_I32(); - expect = POP_I32(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(1); - - os_mutex_lock(&memory->mem_lock); - readv = (uint32)(*(uint8*)maddr); - if (readv == expect) - *(uint8*)maddr = (uint8)(sval); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(2); - - os_mutex_lock(&memory->mem_lock); - readv = (uint32)LOAD_U16(maddr); - if (readv == expect) - STORE_U16(maddr, (uint16)(sval)); - os_mutex_unlock(&memory->mem_lock); + + HANDLE_OP(WASM_OP_F64_NEG) + { + uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()); + uint64 sign_bit = u64 & (((uint64)1) << 63); + if (sign_bit) + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), + (u64 & ~(((uint64)1) << 63))); + else + PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), + (u64 | (((uint64)1) << 63))); + HANDLE_OP_END(); } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - os_mutex_lock(&memory->mem_lock); - readv = LOAD_I32(maddr); - if (readv == expect) - STORE_U32(maddr, sval); - os_mutex_unlock(&memory->mem_lock); - } - PUSH_I32(readv); - break; - } - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG: - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U: - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U: - case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U: - { - uint64 readv, sval, expect; - - sval = (uint64)POP_I64(); - expect = (uint64)POP_I64(); - addr = POP_I32(); - - if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(1); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)(*(uint8*)maddr); - if (readv == expect) - *(uint8*)maddr = (uint8)(sval); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(2); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U16(maddr); - if (readv == expect) - STORE_U16(maddr, (uint16)(sval)); - os_mutex_unlock(&memory->mem_lock); - } - else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(4); - - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_U32(maddr); - if (readv == expect) - STORE_U32(maddr, (uint32)(sval)); - os_mutex_unlock(&memory->mem_lock); + HANDLE_OP(WASM_OP_F64_CEIL) + { + DEF_OP_MATH(float64, F64, ceil); + HANDLE_OP_END(); } - else { - CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); - CHECK_ATOMIC_MEMORY_ACCESS(8); - os_mutex_lock(&memory->mem_lock); - readv = (uint64)LOAD_I64(maddr); - if (readv == expect) { - STORE_I64(maddr, sval); - } - os_mutex_unlock(&memory->mem_lock); - } - PUSH_I64(readv); - break; - } - - DEF_ATOMIC_RMW_OPCODE(ADD, +); - DEF_ATOMIC_RMW_OPCODE(SUB, -); - DEF_ATOMIC_RMW_OPCODE(AND, &); - DEF_ATOMIC_RMW_OPCODE(OR, |); - DEF_ATOMIC_RMW_OPCODE(XOR, ^); - /* xchg, ignore the read value, and store the given value: - readv * 0 + sval */ - DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +); - } + HANDLE_OP(WASM_OP_F64_FLOOR) + { + DEF_OP_MATH(float64, F64, floor); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_TRUNC) + { + DEF_OP_MATH(float64, F64, trunc); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_NEAREST) + { + DEF_OP_MATH(float64, F64, rint); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_SQRT) + { + DEF_OP_MATH(float64, F64, sqrt); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_ADD) + { + DEF_OP_NUMERIC_64(float64, float64, F64, +); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_SUB) + { + DEF_OP_NUMERIC_64(float64, float64, F64, -); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_MUL) + { + DEF_OP_NUMERIC_64(float64, float64, F64, *); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_DIV) + { + DEF_OP_NUMERIC_64(float64, float64, F64, /); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_MIN) + { + float64 a, b; + + b = POP_F64(); + a = POP_F64(); + + if (isnan(a)) + PUSH_F64(a); + else if (isnan(b)) + PUSH_F64(b); + else + PUSH_F64(wa_fmin(a, b)); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_MAX) + { + float64 a, b; + + b = POP_F64(); + a = POP_F64(); + + if (isnan(a)) + PUSH_F64(a); + else if (isnan(b)) + PUSH_F64(b); + else + PUSH_F64(wa_fmax(a, b)); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_COPYSIGN) + { + float64 a, b; + + b = POP_F64(); + a = POP_F64(); + PUSH_F64(signbit(b) ? -fabs(a) : fabs(a)); + HANDLE_OP_END(); + } + + /* conversions of i32 */ + HANDLE_OP(WASM_OP_I32_WRAP_I64) + { + int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL); + PUSH_I32(value); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_S_F32) + { + /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX, + since float/double values of ieee754 cannot precisely + represent all int32/uint32/int64/uint64 values, e.g.: + UINT32_MAX is 4294967295, but (float32)4294967295 is + 4294967296.0f, but not 4294967295.0f. */ + DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true); + HANDLE_OP_END(); + } - HANDLE_OP_END (); - } + HANDLE_OP(WASM_OP_I32_TRUNC_U_F32) + { + DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_S_F64) + { + DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_TRUNC_U_F64) + { + DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false); + HANDLE_OP_END(); + } + + /* conversions of i64 */ + HANDLE_OP(WASM_OP_I64_EXTEND_S_I32) + { + DEF_OP_CONVERT(int64, I64, int32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND_U_I32) + { + DEF_OP_CONVERT(int64, I64, uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_S_F32) + { + DEF_OP_TRUNC_F32(-9223373136366403584.0f, + 9223372036854775808.0f, false, true); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_U_F32) + { + DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_S_F64) + { + DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0, + false, true); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_TRUNC_U_F64) + { + DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false); + HANDLE_OP_END(); + } + + /* conversions of f32 */ + HANDLE_OP(WASM_OP_F32_CONVERT_S_I32) + { + DEF_OP_CONVERT(float32, F32, int32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CONVERT_U_I32) + { + DEF_OP_CONVERT(float32, F32, uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CONVERT_S_I64) + { + DEF_OP_CONVERT(float32, F32, int64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_CONVERT_U_I64) + { + DEF_OP_CONVERT(float32, F32, uint64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F32_DEMOTE_F64) + { + DEF_OP_CONVERT(float32, F32, float64, F64); + HANDLE_OP_END(); + } + + /* conversions of f64 */ + HANDLE_OP(WASM_OP_F64_CONVERT_S_I32) + { + DEF_OP_CONVERT(float64, F64, int32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CONVERT_U_I32) + { + DEF_OP_CONVERT(float64, F64, uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CONVERT_S_I64) + { + DEF_OP_CONVERT(float64, F64, int64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_CONVERT_U_I64) + { + DEF_OP_CONVERT(float64, F64, uint64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_F64_PROMOTE_F32) + { + DEF_OP_CONVERT(float64, F64, float32, F32); + HANDLE_OP_END(); + } + + /* reinterpretations */ + HANDLE_OP(WASM_OP_I32_REINTERPRET_F32) + HANDLE_OP(WASM_OP_F32_REINTERPRET_I32) + { + DEF_OP_REINTERPRET(uint32, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_REINTERPRET_F64) + HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) + { + DEF_OP_REINTERPRET(int64, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(EXT_OP_COPY_STACK_TOP) + { + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + frame_lp[addr2] = frame_lp[addr1]; + HANDLE_OP_END(); + } + + HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64) + { + addr1 = GET_OFFSET(); + addr2 = GET_OFFSET(); + frame_lp[addr2] = frame_lp[addr1]; + frame_lp[addr2 + 1] = frame_lp[addr1 + 1]; + HANDLE_OP_END(); + } + + HANDLE_OP(EXT_OP_COPY_STACK_VALUES) + { + uint32 values_count, total_cell; + uint8 *cells; + int16 *src_offsets = NULL; + uint16 *dst_offsets = NULL; + + /* read values_count */ + values_count = read_uint32(frame_ip); + /* read total cell num */ + total_cell = read_uint32(frame_ip); + /* cells */ + cells = (uint8 *)frame_ip; + frame_ip += values_count * CELL_SIZE; + /* src offsets */ + src_offsets = (int16 *)frame_ip; + frame_ip += values_count * sizeof(int16); + /* dst offsets */ + dst_offsets = (uint16 *)frame_ip; + frame_ip += values_count * sizeof(uint16); + + if (!copy_stack_values(module, frame_lp, values_count, + total_cell, cells, src_offsets, + dst_offsets)) + goto got_exception; + + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_SET_LOCAL) + HANDLE_OP(WASM_OP_TEE_LOCAL) + { + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); + addr1 = GET_OFFSET(); + + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) { + *(int32 *)(frame_lp + local_offset) = frame_lp[addr1]; + } + else if (local_type == VALUE_TYPE_I64 + || local_type == VALUE_TYPE_F64) { + PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset), + GET_I64_FROM_ADDR(frame_lp + addr1)); + } + else { + wasm_set_exception(module, "invalid local type"); + goto got_exception; + } + + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_EXTEND8_S) + { + DEF_OP_CONVERT(int32, I32, int8, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I32_EXTEND16_S) + { + DEF_OP_CONVERT(int32, I32, int16, I32); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND8_S) + { + DEF_OP_CONVERT(int64, I64, int8, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND16_S) + { + DEF_OP_CONVERT(int64, I64, int16, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_I64_EXTEND32_S) + { + DEF_OP_CONVERT(int64, I64, int32, I64); + HANDLE_OP_END(); + } + + HANDLE_OP(WASM_OP_MISC_PREFIX) + { + GET_OPCODE(); + switch (opcode) { + case WASM_OP_I32_TRUNC_SAT_S_F32: + DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f, + true, true); + break; + case WASM_OP_I32_TRUNC_SAT_U_F32: + DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false); + break; + case WASM_OP_I32_TRUNC_SAT_S_F64: + DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true, + true); + break; + case WASM_OP_I32_TRUNC_SAT_U_F64: + DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false); + break; + case WASM_OP_I64_TRUNC_SAT_S_F32: + DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f, + 9223372036854775808.0f, false, + true); + break; + case WASM_OP_I64_TRUNC_SAT_U_F32: + DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f, + false, false); + break; + case WASM_OP_I64_TRUNC_SAT_S_F64: + DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0, + 9223372036854775808.0, false, + true); + break; + case WASM_OP_I64_TRUNC_SAT_U_F64: + DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0, + false, false); + break; +#if WASM_ENABLE_BULK_MEMORY != 0 + case WASM_OP_MEMORY_INIT: + { + uint32 addr, segment; + uint64 bytes, offset, seg_len; + uint8 *data; + + segment = read_uint32(frame_ip); + + bytes = (uint64)POP_I32(); + offset = (uint64)POP_I32(); + addr = POP_I32(); + + CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr); + + seg_len = (uint64)module->module->data_segments[segment] + ->data_length; + data = module->module->data_segments[segment]->data; + if (offset + bytes > seg_len) + goto out_of_bounds; + + bh_memcpy_s(maddr, linear_mem_size - addr, + data + offset, (uint32)bytes); + break; + } + case WASM_OP_DATA_DROP: + { + uint32 segment; + + segment = read_uint32(frame_ip); + + module->module->data_segments[segment]->data_length = 0; + + break; + } + case WASM_OP_MEMORY_COPY: + { + uint32 dst, src, len; + uint8 *mdst, *msrc; + + len = POP_I32(); + src = POP_I32(); + dst = POP_I32(); + + CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc); + CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + + /* allowing the destination and source to overlap */ + bh_memmove_s(mdst, linear_mem_size - dst, msrc, len); + + break; + } + case WASM_OP_MEMORY_FILL: + { + uint32 dst, len; + uint8 val, *mdst; + + len = POP_I32(); + val = POP_I32(); + dst = POP_I32(); + + CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); + + memset(mdst, val, len); + + break; + } +#endif /* WASM_ENABLE_BULK_MEMORY */ +#if WASM_ENABLE_REF_TYPES != 0 + case WASM_OP_TABLE_INIT: + { + uint32 tbl_idx, elem_idx; + uint64 n, s, d; + WASMTableInstance *tbl_inst; + + elem_idx = read_uint32(frame_ip); + bh_assert(elem_idx < module->module->table_seg_count); + + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + n = (uint32)POP_I32(); + s = (uint32)POP_I32(); + d = (uint32)POP_I32(); + + if (!n) { + break; + } + + if (n + s > module->module->table_segments[elem_idx] + .function_count + || d + n > tbl_inst->cur_size) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + if (module->module->table_segments[elem_idx] + .is_dropped) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + if (!wasm_elem_is_passive( + module->module->table_segments[elem_idx] + .mode)) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + bh_memcpy_s( + (uint8 *)tbl_inst + + offsetof(WASMTableInstance, base_addr) + + d * sizeof(uint32), + (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), + module->module->table_segments[elem_idx] + .func_indexes + + s, + (uint32)(n * sizeof(uint32))); + break; + } + case WASM_OP_ELEM_DROP: + { + uint32 elem_idx = read_uint32(frame_ip); + bh_assert(elem_idx < module->module->table_seg_count); + + module->module->table_segments[elem_idx].is_dropped = + true; + break; + } + case WASM_OP_TABLE_COPY: + { + uint32 src_tbl_idx, dst_tbl_idx; + uint64 n, s, d; + WASMTableInstance *src_tbl_inst, *dst_tbl_inst; + + dst_tbl_idx = read_uint32(frame_ip); + bh_assert(dst_tbl_idx < module->table_count); + + dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx); + + src_tbl_idx = read_uint32(frame_ip); + bh_assert(src_tbl_idx < module->table_count); + + src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx); + + n = (uint32)POP_I32(); + s = (uint32)POP_I32(); + d = (uint32)POP_I32(); + + if (s + n > dst_tbl_inst->cur_size + || d + n > src_tbl_inst->cur_size) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + /* if s >= d, copy from front to back */ + /* if s < d, copy from back to front */ + /* merge all together */ + bh_memmove_s( + (uint8 *)dst_tbl_inst + + offsetof(WASMTableInstance, base_addr) + + d * sizeof(uint32), + (uint32)((dst_tbl_inst->cur_size - d) + * sizeof(uint32)), + (uint8 *)src_tbl_inst + + offsetof(WASMTableInstance, base_addr) + + s * sizeof(uint32), + (uint32)(n * sizeof(uint32))); + break; + } + case WASM_OP_TABLE_GROW: + { + uint32 tbl_idx, n, init_val, orig_tbl_sz; + WASMTableInstance *tbl_inst; + + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + orig_tbl_sz = tbl_inst->cur_size; + + n = POP_I32(); + init_val = POP_I32(); + + if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) { + PUSH_I32(-1); + } + else { + PUSH_I32(orig_tbl_sz); + } + + break; + } + case WASM_OP_TABLE_SIZE: + { + uint32 tbl_idx; + WASMTableInstance *tbl_inst; + + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + PUSH_I32(tbl_inst->cur_size); + break; + } + case WASM_OP_TABLE_FILL: + { + uint32 tbl_idx, n, val, i; + WASMTableInstance *tbl_inst; + + tbl_idx = read_uint32(frame_ip); + bh_assert(tbl_idx < module->table_count); + + tbl_inst = wasm_get_table_inst(module, tbl_idx); + + n = POP_I32(); + val = POP_I32(); + i = POP_I32(); + + if (i + n > tbl_inst->cur_size) { + wasm_set_exception(module, + "out of bounds table access"); + goto got_exception; + } + + for (; n != 0; i++, n--) { + ((uint32 *)(tbl_inst->base_addr))[i] = val; + } + + break; + } +#endif /* WASM_ENABLE_REF_TYPES */ + default: + wasm_set_exception(module, "unsupported opcode"); + goto got_exception; + } + HANDLE_OP_END(); + } + +#if WASM_ENABLE_SHARED_MEMORY != 0 + HANDLE_OP(WASM_OP_ATOMIC_PREFIX) + { + uint32 offset, addr; + + GET_OPCODE(); + + offset = read_uint32(frame_ip); + + switch (opcode) { + case WASM_OP_ATOMIC_NOTIFY: + { + uint32 count, ret; + + count = POP_I32(); + addr = POP_I32(); + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + + ret = wasm_runtime_atomic_notify( + (WASMModuleInstanceCommon *)module, maddr, count); + bh_assert((int32)ret >= 0); + + PUSH_I32(ret); + break; + } + case WASM_OP_ATOMIC_WAIT32: + { + uint64 timeout; + uint32 expect, addr, ret; + + timeout = POP_I64(); + expect = POP_I32(); + addr = POP_I32(); + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + + ret = wasm_runtime_atomic_wait( + (WASMModuleInstanceCommon *)module, maddr, + (uint64)expect, timeout, false); + if (ret == (uint32)-1) + goto got_exception; + + PUSH_I32(ret); + break; + } + case WASM_OP_ATOMIC_WAIT64: + { + uint64 timeout, expect; + uint32 ret; + + timeout = POP_I64(); + expect = POP_I64(); + addr = POP_I32(); + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(8); + + ret = wasm_runtime_atomic_wait( + (WASMModuleInstanceCommon *)module, maddr, expect, + timeout, true); + if (ret == (uint32)-1) + goto got_exception; + + PUSH_I32(ret); + break; + } + + case WASM_OP_ATOMIC_I32_LOAD: + case WASM_OP_ATOMIC_I32_LOAD8_U: + case WASM_OP_ATOMIC_I32_LOAD16_U: + { + uint32 readv; + + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(1); + os_mutex_lock(&memory->mem_lock); + readv = (uint32)(*(uint8 *)maddr); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(2); + os_mutex_lock(&memory->mem_lock); + readv = (uint32)LOAD_U16(maddr); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + os_mutex_lock(&memory->mem_lock); + readv = LOAD_I32(maddr); + os_mutex_unlock(&memory->mem_lock); + } + + PUSH_I32(readv); + break; + } + + case WASM_OP_ATOMIC_I64_LOAD: + case WASM_OP_ATOMIC_I64_LOAD8_U: + case WASM_OP_ATOMIC_I64_LOAD16_U: + case WASM_OP_ATOMIC_I64_LOAD32_U: + { + uint64 readv; + + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(1); + os_mutex_lock(&memory->mem_lock); + readv = (uint64)(*(uint8 *)maddr); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(2); + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U16(maddr); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U32(maddr); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(8); + os_mutex_lock(&memory->mem_lock); + readv = LOAD_I64(maddr); + os_mutex_unlock(&memory->mem_lock); + } + + PUSH_I64(readv); + break; + } + case WASM_OP_ATOMIC_I32_STORE: + case WASM_OP_ATOMIC_I32_STORE8: + case WASM_OP_ATOMIC_I32_STORE16: + { + uint32 sval; + + sval = (uint32)POP_I32(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I32_STORE8) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(1); + os_mutex_lock(&memory->mem_lock); + *(uint8 *)maddr = (uint8)sval; + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I32_STORE16) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(2); + os_mutex_lock(&memory->mem_lock); + STORE_U16(maddr, (uint16)sval); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + os_mutex_lock(&memory->mem_lock); + STORE_U32(maddr, sval); + os_mutex_unlock(&memory->mem_lock); + } + break; + } + + case WASM_OP_ATOMIC_I64_STORE: + case WASM_OP_ATOMIC_I64_STORE8: + case WASM_OP_ATOMIC_I64_STORE16: + case WASM_OP_ATOMIC_I64_STORE32: + { + uint64 sval; + + sval = (uint64)POP_I64(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_I64_STORE8) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(1); + os_mutex_lock(&memory->mem_lock); + *(uint8 *)maddr = (uint8)sval; + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_STORE16) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(2); + os_mutex_lock(&memory->mem_lock); + STORE_U16(maddr, (uint16)sval); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_I64_STORE32) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + os_mutex_lock(&memory->mem_lock); + STORE_U32(maddr, (uint32)sval); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(8); + os_mutex_lock(&memory->mem_lock); + STORE_I64(maddr, sval); + os_mutex_unlock(&memory->mem_lock); + } + break; + } + + case WASM_OP_ATOMIC_RMW_I32_CMPXCHG: + case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U: + case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U: + { + uint32 readv, sval, expect; + + sval = POP_I32(); + expect = POP_I32(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(1); + + os_mutex_lock(&memory->mem_lock); + readv = (uint32)(*(uint8 *)maddr); + if (readv == expect) + *(uint8 *)maddr = (uint8)(sval); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(2); + + os_mutex_lock(&memory->mem_lock); + readv = (uint32)LOAD_U16(maddr); + if (readv == expect) + STORE_U16(maddr, (uint16)(sval)); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + + os_mutex_lock(&memory->mem_lock); + readv = LOAD_I32(maddr); + if (readv == expect) + STORE_U32(maddr, sval); + os_mutex_unlock(&memory->mem_lock); + } + PUSH_I32(readv); + break; + } + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG: + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U: + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U: + case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U: + { + uint64 readv, sval, expect; + + sval = (uint64)POP_I64(); + expect = (uint64)POP_I64(); + addr = POP_I32(); + + if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(1); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)(*(uint8 *)maddr); + if (readv == expect) + *(uint8 *)maddr = (uint8)(sval); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(2); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U16(maddr); + if (readv == expect) + STORE_U16(maddr, (uint16)(sval)); + os_mutex_unlock(&memory->mem_lock); + } + else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(4); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_U32(maddr); + if (readv == expect) + STORE_U32(maddr, (uint32)(sval)); + os_mutex_unlock(&memory->mem_lock); + } + else { + CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); + CHECK_ATOMIC_MEMORY_ACCESS(8); + + os_mutex_lock(&memory->mem_lock); + readv = (uint64)LOAD_I64(maddr); + if (readv == expect) { + STORE_I64(maddr, sval); + } + os_mutex_unlock(&memory->mem_lock); + } + PUSH_I64(readv); + break; + } + + DEF_ATOMIC_RMW_OPCODE(ADD, +); + DEF_ATOMIC_RMW_OPCODE(SUB, -); + DEF_ATOMIC_RMW_OPCODE(AND, &); + DEF_ATOMIC_RMW_OPCODE(OR, |); + DEF_ATOMIC_RMW_OPCODE(XOR, ^); + /* xchg, ignore the read value, and store the given + value: readv * 0 + sval */ + DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +); + } + + HANDLE_OP_END(); + } #endif - HANDLE_OP (WASM_OP_IMPDEP): - frame = prev_frame; - frame_ip = frame->ip; - goto call_func_from_entry; + HANDLE_OP(WASM_OP_IMPDEP) + { + frame = prev_frame; + frame_ip = frame->ip; + goto call_func_from_entry; + } - HANDLE_OP (WASM_OP_CALL): + HANDLE_OP(WASM_OP_CALL) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - fidx = read_uint32(frame_ip); + fidx = read_uint32(frame_ip); #if WASM_ENABLE_MULTI_MODULE != 0 - if (fidx >= module->function_count) { - wasm_set_exception(module, "unknown function"); - goto got_exception; - } + if (fidx >= module->function_count) { + wasm_set_exception(module, "unknown function"); + goto got_exception; + } #endif - cur_func = module->functions + fidx; - goto call_func_from_interp; + cur_func = module->functions + fidx; + goto call_func_from_interp; + } #if WASM_ENABLE_TAIL_CALL != 0 - HANDLE_OP (WASM_OP_RETURN_CALL): + HANDLE_OP(WASM_OP_RETURN_CALL) + { #if WASM_ENABLE_THREAD_MGR != 0 - CHECK_SUSPEND_FLAGS(); + CHECK_SUSPEND_FLAGS(); #endif - fidx = read_uint32(frame_ip); + fidx = read_uint32(frame_ip); #if WASM_ENABLE_MULTI_MODULE != 0 - if (fidx >= module->function_count) { - wasm_set_exception(module, "unknown function"); - goto got_exception; - } + if (fidx >= module->function_count) { + wasm_set_exception(module, "unknown function"); + goto got_exception; + } #endif - cur_func = module->functions + fidx; - goto call_func_from_return_call; + cur_func = module->functions + fidx; + goto call_func_from_return_call; + } #endif /* WASM_ENABLE_TAIL_CALL */ #if WASM_ENABLE_LABELS_AS_VALUES == 0 - default: - wasm_set_exception(module, "unsupported opcode"); - goto got_exception; - } + default: + wasm_set_exception(module, "unsupported opcode"); + goto got_exception; + } #endif #if WASM_ENABLE_LABELS_AS_VALUES != 0 - HANDLE_OP (WASM_OP_UNUSED_0x06): - HANDLE_OP (WASM_OP_UNUSED_0x07): - HANDLE_OP (WASM_OP_UNUSED_0x08): - HANDLE_OP (WASM_OP_UNUSED_0x09): - HANDLE_OP (WASM_OP_UNUSED_0x0a): + HANDLE_OP(WASM_OP_UNUSED_0x06) + HANDLE_OP(WASM_OP_UNUSED_0x07) + HANDLE_OP(WASM_OP_UNUSED_0x08) + HANDLE_OP(WASM_OP_UNUSED_0x09) + HANDLE_OP(WASM_OP_UNUSED_0x0a) #if WASM_ENABLE_TAIL_CALL == 0 - HANDLE_OP (WASM_OP_RETURN_CALL): - HANDLE_OP (WASM_OP_RETURN_CALL_INDIRECT): + HANDLE_OP(WASM_OP_RETURN_CALL) + HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT) #endif #if WASM_ENABLE_SHARED_MEMORY == 0 - HANDLE_OP (WASM_OP_ATOMIC_PREFIX): + HANDLE_OP(WASM_OP_ATOMIC_PREFIX) #endif #if WASM_ENABLE_REF_TYPES == 0 - HANDLE_OP (WASM_OP_TABLE_GET): - HANDLE_OP (WASM_OP_TABLE_SET): - HANDLE_OP (WASM_OP_REF_NULL): - HANDLE_OP (WASM_OP_REF_IS_NULL): - HANDLE_OP (WASM_OP_REF_FUNC): + HANDLE_OP(WASM_OP_TABLE_GET) + HANDLE_OP(WASM_OP_TABLE_SET) + HANDLE_OP(WASM_OP_REF_NULL) + HANDLE_OP(WASM_OP_REF_IS_NULL) + HANDLE_OP(WASM_OP_REF_FUNC) #endif - /* SELECT_T is converted to SELECT or SELECT_64 */ - HANDLE_OP (WASM_OP_SELECT_T): - HANDLE_OP (WASM_OP_UNUSED_0x14): - HANDLE_OP (WASM_OP_UNUSED_0x15): - HANDLE_OP (WASM_OP_UNUSED_0x16): - HANDLE_OP (WASM_OP_UNUSED_0x17): - HANDLE_OP (WASM_OP_UNUSED_0x18): - HANDLE_OP (WASM_OP_UNUSED_0x19): - HANDLE_OP (WASM_OP_UNUSED_0x27): - /* optimized op code */ - HANDLE_OP (WASM_OP_F32_STORE): - HANDLE_OP (WASM_OP_F64_STORE): - HANDLE_OP (WASM_OP_F32_LOAD): - HANDLE_OP (WASM_OP_F64_LOAD): - HANDLE_OP (EXT_OP_GET_LOCAL_FAST): - HANDLE_OP (WASM_OP_GET_LOCAL): - HANDLE_OP (WASM_OP_F64_CONST): - HANDLE_OP (WASM_OP_I64_CONST): - HANDLE_OP (WASM_OP_F32_CONST): - HANDLE_OP (WASM_OP_I32_CONST): - HANDLE_OP (WASM_OP_DROP): - HANDLE_OP (WASM_OP_DROP_64): - HANDLE_OP (WASM_OP_BLOCK): - HANDLE_OP (WASM_OP_LOOP): - HANDLE_OP (WASM_OP_END): - HANDLE_OP (WASM_OP_NOP): - HANDLE_OP (EXT_OP_BLOCK): - HANDLE_OP (EXT_OP_LOOP): - HANDLE_OP (EXT_OP_IF): - { - wasm_set_exception(module, "unsupported opcode"); - goto got_exception; - } + /* SELECT_T is converted to SELECT or SELECT_64 */ + HANDLE_OP(WASM_OP_SELECT_T) + HANDLE_OP(WASM_OP_UNUSED_0x14) + HANDLE_OP(WASM_OP_UNUSED_0x15) + HANDLE_OP(WASM_OP_UNUSED_0x16) + HANDLE_OP(WASM_OP_UNUSED_0x17) + HANDLE_OP(WASM_OP_UNUSED_0x18) + HANDLE_OP(WASM_OP_UNUSED_0x19) + HANDLE_OP(WASM_OP_UNUSED_0x27) + /* optimized op code */ + HANDLE_OP(WASM_OP_F32_STORE) + HANDLE_OP(WASM_OP_F64_STORE) + HANDLE_OP(WASM_OP_F32_LOAD) + HANDLE_OP(WASM_OP_F64_LOAD) + HANDLE_OP(EXT_OP_GET_LOCAL_FAST) + HANDLE_OP(WASM_OP_GET_LOCAL) + HANDLE_OP(WASM_OP_F64_CONST) + HANDLE_OP(WASM_OP_I64_CONST) + HANDLE_OP(WASM_OP_F32_CONST) + HANDLE_OP(WASM_OP_I32_CONST) + HANDLE_OP(WASM_OP_DROP) + HANDLE_OP(WASM_OP_DROP_64) + HANDLE_OP(WASM_OP_BLOCK) + HANDLE_OP(WASM_OP_LOOP) + HANDLE_OP(WASM_OP_END) + HANDLE_OP(WASM_OP_NOP) + HANDLE_OP(EXT_OP_BLOCK) + HANDLE_OP(EXT_OP_LOOP) + HANDLE_OP(EXT_OP_IF) + { + wasm_set_exception(module, "unsupported opcode"); + goto got_exception; + } #endif #if WASM_ENABLE_LABELS_AS_VALUES == 0 - continue; + continue; #else - FETCH_OPCODE_AND_DISPATCH (); + FETCH_OPCODE_AND_DISPATCH(); #endif -#if WASM_ENABLE_TAIL_CALL !=0 - call_func_from_return_call: - { - uint32 *lp_base; - uint32 *lp; - int i; - - if (!(lp_base = lp = - wasm_runtime_malloc(cur_func->param_cell_num * sizeof(uint32)))) { - wasm_set_exception(module, "allocate memory failed"); - goto got_exception; - } - for (i = 0; i < cur_func->param_count; i++) { - if (cur_func->param_types[i] == VALUE_TYPE_I64 - || cur_func->param_types[i] == VALUE_TYPE_F64) { - PUT_I64_TO_ADDR(lp, GET_OPERAND(uint64, I64, - 2 * (cur_func->param_count - i - 1))); - lp += 2; - } - else { - *lp = GET_OPERAND(uint32, I32, (2 * (cur_func->param_count - i - 1))); - lp ++; - } - } - frame->lp = frame->operand + cur_func->const_cell_num; - word_copy(frame->lp, lp_base, lp - lp_base); - wasm_runtime_free(lp_base); - FREE_FRAME(exec_env, frame); - frame_ip += cur_func->param_count * sizeof(int16); - wasm_exec_env_set_cur_frame(exec_env, - (WASMRuntimeFrame *)prev_frame); - goto call_func_from_entry; - } +#if WASM_ENABLE_TAIL_CALL != 0 + call_func_from_return_call: + { + uint32 *lp_base; + uint32 *lp; + int i; + + if (!(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num + * sizeof(uint32)))) { + wasm_set_exception(module, "allocate memory failed"); + goto got_exception; + } + for (i = 0; i < cur_func->param_count; i++) { + if (cur_func->param_types[i] == VALUE_TYPE_I64 + || cur_func->param_types[i] == VALUE_TYPE_F64) { + PUT_I64_TO_ADDR( + lp, GET_OPERAND(uint64, I64, + 2 * (cur_func->param_count - i - 1))); + lp += 2; + } + else { + *lp = GET_OPERAND(uint32, I32, + (2 * (cur_func->param_count - i - 1))); + lp++; + } + } + frame->lp = frame->operand + cur_func->const_cell_num; + word_copy(frame->lp, lp_base, lp - lp_base); + wasm_runtime_free(lp_base); + FREE_FRAME(exec_env, frame); + frame_ip += cur_func->param_count * sizeof(int16); + wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame); + goto call_func_from_entry; + } #endif /* WASM_ENABLE_TAIL_CALL */ - call_func_from_interp: - /* Only do the copy when it's called from interpreter. */ + + call_func_from_interp: { - WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env); - int i; + /* Only do the copy when it's called from interpreter. */ + WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env); + int i; #if WASM_ENABLE_MULTI_MODULE != 0 - if (cur_func->is_import_func) { - outs_area->lp = outs_area->operand - + (cur_func->import_func_inst - ? cur_func->import_func_inst->const_cell_num - : 0); - } - else -#endif - { - outs_area->lp = outs_area->operand + cur_func->const_cell_num; - } - for (i = 0; i < cur_func->param_count; i++) { - if (cur_func->param_types[i] == VALUE_TYPE_I64 - || cur_func->param_types[i] == VALUE_TYPE_F64) { - PUT_I64_TO_ADDR(outs_area->lp, - GET_OPERAND(uint64, I64, - 2 * (cur_func->param_count - i - 1))); - outs_area->lp += 2; + if (cur_func->is_import_func) { + outs_area->lp = outs_area->operand + + (cur_func->import_func_inst + ? cur_func->import_func_inst->const_cell_num + : 0); } - else { - *outs_area->lp = GET_OPERAND(uint32, I32, - (2 * (cur_func->param_count - i - 1))); - outs_area->lp ++; - } - } - frame_ip += cur_func->param_count * sizeof(int16); - if (cur_func->ret_cell_num != 0) { - /* Get the first return value's offset. Since loader emit all return - * values' offset so we must skip remain return values' offsets. - */ - WASMType *func_type; - if (cur_func->is_import_func) - func_type = cur_func->u.func_import->func_type; else - func_type = cur_func->u.func->func_type; - frame->ret_offset = GET_OFFSET(); - frame_ip += 2 * (func_type->result_count - 1); - } - SYNC_ALL_TO_FRAME(); - prev_frame = frame; +#endif + { + outs_area->lp = outs_area->operand + cur_func->const_cell_num; + } + for (i = 0; i < cur_func->param_count; i++) { + if (cur_func->param_types[i] == VALUE_TYPE_I64 + || cur_func->param_types[i] == VALUE_TYPE_F64) { + PUT_I64_TO_ADDR( + outs_area->lp, + GET_OPERAND(uint64, I64, + 2 * (cur_func->param_count - i - 1))); + outs_area->lp += 2; + } + else { + *outs_area->lp = GET_OPERAND( + uint32, I32, (2 * (cur_func->param_count - i - 1))); + outs_area->lp++; + } + } + frame_ip += cur_func->param_count * sizeof(int16); + if (cur_func->ret_cell_num != 0) { + /* Get the first return value's offset. Since loader emit + * all return values' offset so we must skip remain return + * values' offsets. + */ + WASMType *func_type; + if (cur_func->is_import_func) + func_type = cur_func->u.func_import->func_type; + else + func_type = cur_func->u.func->func_type; + frame->ret_offset = GET_OFFSET(); + frame_ip += 2 * (func_type->result_count - 1); + } + SYNC_ALL_TO_FRAME(); + prev_frame = frame; } - call_func_from_entry: + call_func_from_entry: { - if (cur_func->is_import_func) { + if (cur_func->is_import_func) { #if WASM_ENABLE_MULTI_MODULE != 0 - if (cur_func->import_func_inst) { - wasm_interp_call_func_import(module, exec_env, cur_func, - prev_frame); - } - else + if (cur_func->import_func_inst) { + wasm_interp_call_func_import(module, exec_env, cur_func, + prev_frame); + } + else #endif - { - wasm_interp_call_func_native(module, exec_env, cur_func, - prev_frame); - } - - prev_frame = frame->prev_frame; - cur_func = frame->function; - UPDATE_ALL_FROM_FRAME(); - - /* update memory instance ptr and memory size */ - memory = module->default_memory; - if (memory) - linear_mem_size = num_bytes_per_page * memory->cur_page_count; - if (wasm_get_exception(module)) - goto got_exception; - } - else { - WASMFunction *cur_wasm_func = cur_func->u.func; - - all_cell_num = (uint64)cur_func->param_cell_num - + (uint64)cur_func->local_cell_num - + (uint64)cur_func->const_cell_num - + (uint64)cur_wasm_func->max_stack_cell_num; - if (all_cell_num >= UINT32_MAX) { - wasm_set_exception(module, "wasm operand stack overflow"); - goto got_exception; - } + { + wasm_interp_call_func_native(module, exec_env, cur_func, + prev_frame); + } + + prev_frame = frame->prev_frame; + cur_func = frame->function; + UPDATE_ALL_FROM_FRAME(); - frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num); - if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) { - frame = prev_frame; - goto got_exception; + /* update memory instance ptr and memory size */ + memory = module->default_memory; + if (memory) + linear_mem_size = num_bytes_per_page * memory->cur_page_count; + if (wasm_get_exception(module)) + goto got_exception; } + else { + WASMFunction *cur_wasm_func = cur_func->u.func; + + all_cell_num = (uint64)cur_func->param_cell_num + + (uint64)cur_func->local_cell_num + + (uint64)cur_func->const_cell_num + + (uint64)cur_wasm_func->max_stack_cell_num; + if (all_cell_num >= UINT32_MAX) { + wasm_set_exception(module, "wasm operand stack overflow"); + goto got_exception; + } - /* Initialize the interpreter context. */ - frame->function = cur_func; - frame_ip = wasm_get_func_code(cur_func); - frame_ip_end = wasm_get_func_code_end(cur_func); + frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num); + if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) { + frame = prev_frame; + goto got_exception; + } - frame_lp = frame->lp = frame->operand + cur_wasm_func->const_cell_num; + /* Initialize the interpreter context. */ + frame->function = cur_func; + frame_ip = wasm_get_func_code(cur_func); + frame_ip_end = wasm_get_func_code_end(cur_func); - /* Initialize the consts */ - word_copy(frame->operand, (uint32*)cur_wasm_func->consts, - cur_wasm_func->const_cell_num); + frame_lp = frame->lp = + frame->operand + cur_wasm_func->const_cell_num; - /* Initialize the local varialbes */ - memset(frame_lp + cur_func->param_cell_num, 0, - (uint32)(cur_func->local_cell_num * 4)); + /* Initialize the consts */ + word_copy(frame->operand, (uint32 *)cur_wasm_func->consts, + cur_wasm_func->const_cell_num); - wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame*)frame); - } - HANDLE_OP_END (); + /* Initialize the local varialbes */ + memset(frame_lp + cur_func->param_cell_num, 0, + (uint32)(cur_func->local_cell_num * 4)); + + wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame); + } + HANDLE_OP_END(); } - return_func: + return_func: { - FREE_FRAME(exec_env, frame); - wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame*)prev_frame); + FREE_FRAME(exec_env, frame); + wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame); - if (!prev_frame->ip) - /* Called from native. */ - return; + if (!prev_frame->ip) + /* Called from native. */ + return; - RECOVER_CONTEXT(prev_frame); - HANDLE_OP_END (); + RECOVER_CONTEXT(prev_frame); + HANDLE_OP_END(); } - (void)frame_ip_end; + (void)frame_ip_end; #if WASM_ENABLE_SHARED_MEMORY != 0 - unaligned_atomic: - wasm_set_exception(module, "unaligned atomic"); - goto got_exception; + unaligned_atomic: + wasm_set_exception(module, "unaligned atomic"); + goto got_exception; #endif - out_of_bounds: - wasm_set_exception(module, "out of bounds memory access"); + out_of_bounds: + wasm_set_exception(module, "out of bounds memory access"); - got_exception: - SYNC_ALL_TO_FRAME(); - return; + got_exception: + SYNC_ALL_TO_FRAME(); + return; #if WASM_ENABLE_LABELS_AS_VALUES == 0 - } + } #else - FETCH_OPCODE_AND_DISPATCH (); + FETCH_OPCODE_AND_DISPATCH(); #endif } @@ -3442,37 +3714,37 @@ wasm_interp_get_handle_table() #endif void -wasm_interp_call_wasm(WASMModuleInstance *module_inst, - WASMExecEnv *exec_env, - WASMFunctionInstance *function, - uint32 argc, uint32 argv[]) +wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env, + WASMFunctionInstance *function, uint32 argc, + uint32 argv[]) { WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env); WASMInterpFrame *frame, *outs_area; /* Allocate sufficient cells for all kinds of return values. */ - unsigned all_cell_num = function->ret_cell_num > 2 ? - function->ret_cell_num : 2, i; + unsigned all_cell_num = + function->ret_cell_num > 2 ? function->ret_cell_num : 2, + i; /* This frame won't be used by JITed code, so only allocate interp frame here. */ unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num); if (argc != function->param_cell_num) { char buf[128]; - snprintf(buf, sizeof(buf), - "invalid argument count %d, expected %d", + snprintf(buf, sizeof(buf), "invalid argument count %d, expected %d", argc, function->param_cell_num); wasm_set_exception(module_inst, buf); return; } - if ((uint8*)&prev_frame < exec_env->native_stack_boundary) { - wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, + if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) { + wasm_set_exception((WASMModuleInstance *)exec_env->module_inst, "native stack overflow"); return; } - if (!(frame = ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame*)prev_frame))) + if (!(frame = + ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame))) return; outs_area = wasm_exec_env_wasm_stack_top(exec_env); @@ -3491,15 +3763,15 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, #if WASM_ENABLE_MULTI_MODULE != 0 if (function->import_module_inst) { LOG_DEBUG("it is a function of a sub module"); - wasm_interp_call_func_import(module_inst, exec_env, - function, frame); + wasm_interp_call_func_import(module_inst, exec_env, function, + frame); } else #endif { LOG_DEBUG("it is an native function"); - wasm_interp_call_func_native(module_inst, exec_env, - function, frame); + wasm_interp_call_func_native(module_inst, exec_env, function, + frame); } } else { diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index ea0d9cfe08..979ab0be26 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -17,21 +17,20 @@ /* Read a value of given type from the address pointed to by the given pointer and increase the pointer to the position just after the value being read. */ -#define TEMPLATE_READ_VALUE(Type, p) \ +#define TEMPLATE_READ_VALUE(Type, p) \ (p += sizeof(Type), *(Type *)(p - sizeof(Type))) static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { if (error_buf != NULL) { - snprintf(error_buf, error_buf_size, - "WASM module load failed: %s", string); + snprintf(error_buf, error_buf_size, "WASM module load failed: %s", + string); } } static void -set_error_buf_v(char *error_buf, uint32 error_buf_size, - const char *format, ...) +set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...) { va_list args; char buf[128]; @@ -40,8 +39,7 @@ set_error_buf_v(char *error_buf, uint32 error_buf_size, va_start(args, format); vsnprintf(buf, sizeof(buf), format, args); va_end(args); - snprintf(error_buf, error_buf_size, - "WASM module load failed: %s", buf); + snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf); } } @@ -68,19 +66,19 @@ check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length, return true; } -#define CHECK_BUF(buf, buf_end, length) do { \ - if (!check_buf(buf, buf_end, length, \ - error_buf, error_buf_size)) { \ - goto fail; \ - } \ -} while (0) +#define CHECK_BUF(buf, buf_end, length) \ + do { \ + if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \ + goto fail; \ + } \ + } while (0) -#define CHECK_BUF1(buf, buf_end, length) do { \ - if (!check_buf1(buf, buf_end, length, \ - error_buf, error_buf_size)) { \ - goto fail; \ - } \ -} while (0) +#define CHECK_BUF1(buf, buf_end, length) \ + do { \ + if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \ + goto fail; \ + } \ + } while (0) #define skip_leb(p) while (*p++ & 0x80) #define skip_leb_int64(p, p_end) skip_leb(p) @@ -88,9 +86,8 @@ check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length, #define skip_leb_int32(p, p_end) skip_leb(p) static bool -read_leb(uint8 **p_buf, const uint8 *buf_end, - uint32 maxbits, bool sign, uint64 *p_result, - char* error_buf, uint32 error_buf_size) +read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign, + uint64 *p_result, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; uint64 result = 0; @@ -164,33 +161,36 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, return false; } -#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p) +#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p) #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p) -#define read_bool(p) TEMPLATE_READ_VALUE(bool, p) - -#define read_leb_int64(p, p_end, res) do { \ - uint64 res64; \ - if (!read_leb((uint8**)&p, p_end, 64, true, &res64,\ - error_buf, error_buf_size)) \ - goto fail; \ - res = (int64)res64; \ -} while (0) - -#define read_leb_uint32(p, p_end, res) do { \ - uint64 res64; \ - if (!read_leb((uint8**)&p, p_end, 32, false, &res64,\ - error_buf, error_buf_size)) \ - goto fail; \ - res = (uint32)res64; \ -} while (0) - -#define read_leb_int32(p, p_end, res) do { \ - uint64 res64; \ - if (!read_leb((uint8**)&p, p_end, 32, true, &res64,\ - error_buf, error_buf_size)) \ - goto fail; \ - res = (int32)res64; \ -} while (0) +#define read_bool(p) TEMPLATE_READ_VALUE(bool, p) + +#define read_leb_int64(p, p_end, res) \ + do { \ + uint64 res64; \ + if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \ + error_buf_size)) \ + goto fail; \ + res = (int64)res64; \ + } while (0) + +#define read_leb_uint32(p, p_end, res) \ + do { \ + uint64 res64; \ + if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \ + error_buf_size)) \ + goto fail; \ + res = (uint32)res64; \ + } while (0) + +#define read_leb_int32(p, p_end, res) \ + do { \ + uint64 res64; \ + if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \ + error_buf_size)) \ + goto fail; \ + res = (int32)res64; \ + } while (0) static char * type2str(uint8 type) @@ -242,7 +242,7 @@ is_value_type(uint8 type) || type == VALUE_TYPE_V128 #endif #endif - ) + ) return true; return false; } @@ -256,7 +256,7 @@ is_byte_a_type(uint8 type) #if WASM_ENABLE_SIMD != 0 #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) static V128 -read_i8x16(uint8 *p_buf, char* error_buf, uint32 error_buf_size) +read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size) { V128 result; uint8 i; @@ -275,10 +275,8 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); return NULL; } @@ -287,7 +285,7 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) } static bool -check_utf8_str(const uint8* str, uint32 len) +check_utf8_str(const uint8 *str, uint32 len) { /* The valid ranges are taken from page 125, below link https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */ @@ -307,20 +305,17 @@ check_utf8_str(const uint8* str, uint32 len) } else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) { if (chr == 0xE0) { - if (p[1] < 0xA0 || p[1] > 0xBF - || p[2] < 0x80 || p[2] > 0xBF) { + if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) { return false; } } else if (chr == 0xED) { - if (p[1] < 0x80 || p[1] > 0x9F - || p[2] < 0x80 || p[2] > 0xBF) { + if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) { return false; } } else if (chr >= 0xE1 && chr <= 0xEF) { - if (p[1] < 0x80 || p[1] > 0xBF - || p[2] < 0x80 || p[2] > 0xBF) { + if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) { return false; } } @@ -328,22 +323,19 @@ check_utf8_str(const uint8* str, uint32 len) } else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) { if (chr == 0xF0) { - if (p[1] < 0x90 || p[1] > 0xBF - || p[2] < 0x80 || p[2] > 0xBF + if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF || p[3] < 0x80 || p[3] > 0xBF) { return false; } } else if (chr >= 0xF1 && chr <= 0xF3) { - if (p[1] < 0x80 || p[1] > 0xBF - || p[2] < 0x80 || p[2] > 0xBF + if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF || p[3] < 0x80 || p[3] > 0xBF) { return false; } } else if (chr == 0xF4) { - if (p[1] < 0x80 || p[1] > 0x8F - || p[2] < 0x80 || p[2] > 0xBF + if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF || p[3] < 0x80 || p[3] > 0xBF) { return false; } @@ -357,15 +349,14 @@ check_utf8_str(const uint8* str, uint32 len) return (p == p_end); } -static char* +static char * const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, - char* error_buf, uint32 error_buf_size) + char *error_buf, uint32 error_buf_size) { StringNode *node, *node_next; if (!check_utf8_str(str, len)) { - set_error_buf(error_buf, error_buf_size, - "invalid UTF-8 encoding"); + set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding"); return NULL; } @@ -373,8 +364,7 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, node = module->const_str_list; while (node) { node_next = node->next; - if (strlen(node->str) == len - && !memcmp(node->str, str, len)) + if (strlen(node->str) == len && !memcmp(node->str, str, len)) break; node = node_next; } @@ -383,12 +373,12 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, return node->str; } - if (!(node = loader_malloc(sizeof(StringNode) + len + 1, - error_buf, error_buf_size))) { + if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf, + error_buf_size))) { return NULL; } - node->str = ((char*)node) + sizeof(StringNode); + node->str = ((char *)node) + sizeof(StringNode); bh_memcpy_s(node->str, len + 1, str, len); node->str[len] = '\0'; @@ -408,8 +398,8 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, static bool load_init_expr(const uint8 **p_buf, const uint8 *buf_end, - InitializerExpression *init_expr, uint8 type, - char *error_buf, uint32 error_buf_size) + InitializerExpression *init_expr, uint8 type, char *error_buf, + uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint8 flag, end_byte, *p_float; @@ -437,7 +427,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, if (type != VALUE_TYPE_F32) goto fail_type_mismatch; CHECK_BUF(p, p_end, 4); - p_float = (uint8*)&init_expr->u.f32; + p_float = (uint8 *)&init_expr->u.f32; for (i = 0; i < sizeof(float32); i++) *p_float++ = *p++; break; @@ -446,7 +436,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, if (type != VALUE_TYPE_F64) goto fail_type_mismatch; CHECK_BUF(p, p_end, 8); - p_float = (uint8*)&init_expr->u.f64; + p_float = (uint8 *)&init_expr->u.f64; for (i = 0; i < sizeof(float64); i++) *p_float++ = *p++; break; @@ -464,7 +454,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, (void)flag; CHECK_BUF(p, p_end, 16); - wasm_runtime_read_v128(p, &high, &low); + wasm_runtime_read_v128(p, &high, &low); p += 16; init_expr->u.v128.i64x2[0] = high; @@ -506,9 +496,10 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, case INIT_EXPR_TYPE_GET_GLOBAL: read_leb_uint32(p, p_end, init_expr->u.global_index); break; - default: { + default: + { #if WASM_ENABLE_REF_TYPES != 0 -illegal_opcode: + illegal_opcode: #endif set_error_buf(error_buf, error_buf_size, "illegal opcode " @@ -546,9 +537,9 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (type_count) { module->type_count = type_count; - total_size = sizeof(WASMType*) * (uint64)type_count; - if (!(module->types = loader_malloc - (total_size, error_buf, error_buf_size))) { + total_size = sizeof(WASMType *) * (uint64)type_count; + if (!(module->types = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -556,8 +547,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, CHECK_BUF(p, p_end, 1); flag = read_uint8(p); if (flag != 0x60) { - set_error_buf(error_buf, error_buf_size, - "invalid type flag"); + set_error_buf(error_buf, error_buf_size, "invalid type flag"); return false; } @@ -577,10 +567,10 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, return false; } - total_size = offsetof(WASMType, types) + - sizeof(uint8) * (uint64)(param_count + result_count); + total_size = offsetof(WASMType, types) + + sizeof(uint8) * (uint64)(param_count + result_count); if (!(type = module->types[i] = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -598,8 +588,8 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } param_cell_num = wasm_get_cell_num(type->types, param_count); - ret_cell_num = wasm_get_cell_num(type->types + param_count, - result_count); + ret_cell_num = + wasm_get_cell_num(type->types + param_count, result_count); if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) { set_error_buf(error_buf, error_buf_size, "param count or result count too large"); @@ -625,7 +615,7 @@ static void adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size) { uint32 default_max_size = - init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE; + init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE; if (max_size_flag) { /* module defines the table limitation */ @@ -633,7 +623,7 @@ adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size) if (init_size < *max_size) { *max_size = - *max_size < default_max_size ? *max_size : default_max_size; + *max_size < default_max_size ? *max_size : default_max_size; } } else { @@ -648,12 +638,10 @@ adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size) * module name, field name and export kind */ static WASMExport * -wasm_loader_find_export(const WASMModule *module, - const char *module_name, - const char *field_name, - uint8 export_kind, - uint32 export_index_boundary, - char *error_buf, uint32 error_buf_size) +wasm_loader_find_export(const WASMModule *module, const char *module_name, + const char *field_name, uint8 export_kind, + uint32 export_index_boundary, char *error_buf, + uint32 error_buf_size) { WASMExport *export; uint32 i; @@ -680,9 +668,8 @@ wasm_loader_find_export(const WASMModule *module, } if (export->index >= export_index_boundary) { - LOG_DEBUG("%s in the module %s is out of index (%d >= %d )", - field_name, module_name, - export->index, export_index_boundary); + LOG_DEBUG("%s in the module %s is out of index (%d >= %d )", field_name, + module_name, export->index, export_index_boundary); set_error_buf(error_buf, error_buf_size, "incompatible import type"); return NULL; } @@ -691,8 +678,7 @@ wasm_loader_find_export(const WASMModule *module, } static WASMFunction * -wasm_loader_resolve_function(const char *module_name, - const char *function_name, +wasm_loader_resolve_function(const char *module_name, const char *function_name, const WASMType *expected_function_type, char *error_buf, uint32 error_buf_size) { @@ -703,20 +689,18 @@ wasm_loader_resolve_function(const char *module_name, WASMType *target_function_type = NULL; module_reg = wasm_runtime_find_module_registered(module_name); - if (!module_reg - || module_reg->module_type != Wasm_Module_Bytecode) { - LOG_DEBUG("can not find a module named %s for function %s", - module_name, function_name); + if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { + LOG_DEBUG("can not find a module named %s for function %s", module_name, + function_name); set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } module = (WASMModule *)module_reg; - export = wasm_loader_find_export(module, module_name, function_name, - EXPORT_KIND_FUNC, - module->import_function_count - + module->function_count, - error_buf, error_buf_size); + export = wasm_loader_find_export( + module, module_name, function_name, EXPORT_KIND_FUNC, + module->import_function_count + module->function_count, error_buf, + error_buf_size); if (!export) { return NULL; } @@ -724,16 +708,16 @@ wasm_loader_resolve_function(const char *module_name, /* resolve function type and function */ if (export->index < module->import_function_count) { target_function_type = - module->import_functions[export->index].u.function.func_type; + module->import_functions[export->index].u.function.func_type; function = module->import_functions[export->index] - .u.function.import_func_linked; + .u.function.import_func_linked; } else { target_function_type = - module->functions[export->index - module->import_function_count] - ->func_type; + module->functions[export->index - module->import_function_count] + ->func_type; function = - module->functions[export->index - module->import_function_count]; + module->functions[export->index - module->import_function_count]; } /* check function type */ @@ -748,8 +732,8 @@ wasm_loader_resolve_function(const char *module_name, static WASMTable * wasm_loader_resolve_table(const char *module_name, const char *table_name, - uint32 init_size, uint32 max_size, - char *error_buf, uint32 error_buf_size) + uint32 init_size, uint32 max_size, char *error_buf, + uint32 error_buf_size) { WASMModuleCommon *module_reg; WASMTable *table = NULL; @@ -757,19 +741,17 @@ wasm_loader_resolve_table(const char *module_name, const char *table_name, WASMModule *module = NULL; module_reg = wasm_runtime_find_module_registered(module_name); - if (!module_reg - || module_reg->module_type != Wasm_Module_Bytecode) { + if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for table", module_name); set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } module = (WASMModule *)module_reg; - export = wasm_loader_find_export(module, module_name, table_name, - EXPORT_KIND_TABLE, - module->table_count - + module->import_table_count, - error_buf, error_buf_size); + export = wasm_loader_find_export( + module, module_name, table_name, EXPORT_KIND_TABLE, + module->table_count + module->import_table_count, error_buf, + error_buf_size); if (!export) { return NULL; } @@ -777,7 +759,7 @@ wasm_loader_resolve_table(const char *module_name, const char *table_name, /* resolve table and check the init/max size */ if (export->index < module->import_table_count) { table = - module->import_tables[export->index].u.table.import_table_linked; + module->import_tables[export->index].u.table.import_table_linked; } else { table = &(module->tables[export->index - module->import_table_count]); @@ -804,35 +786,32 @@ wasm_loader_resolve_memory(const char *module_name, const char *memory_name, WASMModule *module = NULL; module_reg = wasm_runtime_find_module_registered(module_name); - if (!module_reg - || module_reg->module_type != Wasm_Module_Bytecode) { + if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for memory", module_name); set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } module = (WASMModule *)module_reg; - export = wasm_loader_find_export(module, module_name, memory_name, - EXPORT_KIND_MEMORY, - module->import_memory_count - + module->memory_count, - error_buf, error_buf_size); + export = wasm_loader_find_export( + module, module_name, memory_name, EXPORT_KIND_MEMORY, + module->import_memory_count + module->memory_count, error_buf, + error_buf_size); if (!export) { return NULL; } - /* resolve memory and check the init/max page count */ if (export->index < module->import_memory_count) { - memory = - module->import_memories[export->index].u.memory.import_memory_linked; + memory = module->import_memories[export->index] + .u.memory.import_memory_linked; } else { memory = - &(module->memories[export->index - module->import_memory_count]); + &(module->memories[export->index - module->import_memory_count]); } - if (memory->init_page_count < init_page_count || - memory->max_page_count > max_page_count) { + if (memory->init_page_count < init_page_count + || memory->max_page_count > max_page_count) { LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)", module_name, memory_name, memory->init_page_count, memory->max_page_count, init_page_count, max_page_count); @@ -843,10 +822,9 @@ wasm_loader_resolve_memory(const char *module_name, const char *memory_name, } static WASMGlobal * -wasm_loader_resolve_global(const char *module_name, - const char *global_name, - uint8 type, bool is_mutable, - char *error_buf, uint32 error_buf_size) +wasm_loader_resolve_global(const char *module_name, const char *global_name, + uint8 type, bool is_mutable, char *error_buf, + uint32 error_buf_size) { WASMModuleCommon *module_reg; WASMGlobal *global = NULL; @@ -854,19 +832,17 @@ wasm_loader_resolve_global(const char *module_name, WASMModule *module = NULL; module_reg = wasm_runtime_find_module_registered(module_name); - if (!module_reg - || module_reg->module_type != Wasm_Module_Bytecode) { + if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for global", module_name); set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } module = (WASMModule *)module_reg; - export = wasm_loader_find_export(module, module_name, global_name, - EXPORT_KIND_GLOBAL, - module->import_global_count - + module->global_count, - error_buf, error_buf_size); + export = wasm_loader_find_export( + module, module_name, global_name, EXPORT_KIND_GLOBAL, + module->import_global_count + module->global_count, error_buf, + error_buf_size); if (!export) { return NULL; } @@ -874,11 +850,11 @@ wasm_loader_resolve_global(const char *module_name, /* resolve and check the global */ if (export->index < module->import_global_count) { global = - module->import_globals[export->index].u.global.import_global_linked; + module->import_globals[export->index].u.global.import_global_linked; } else { global = - &(module->globals[export->index - module->import_global_count]); + &(module->globals[export->index - module->import_global_count]); } if (global->type != type || global->is_mutable != is_mutable) { LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)", @@ -894,11 +870,11 @@ static WASMModule * search_sub_module(const WASMModule *parent_module, const char *sub_module_name) { WASMRegisteredModule *node = - bh_list_first_elem(parent_module->import_module_list); + bh_list_first_elem(parent_module->import_module_list); while (node && strcmp(sub_module_name, node->module_name)) { node = bh_list_elem_next(node); } - return node ? (WASMModule*)node->module : NULL; + return node ? (WASMModule *)node->module : NULL; } static bool @@ -920,7 +896,7 @@ register_sub_module(const WASMModule *parent_module, } node->module_name = sub_module_name; - node->module = (WASMModuleCommon*)sub_module; + node->module = (WASMModuleCommon *)sub_module; ret = bh_list_insert(parent_module->import_module_list, node); bh_assert(BH_LIST_SUCCESS == ret); (void)ret; @@ -948,7 +924,7 @@ load_depended_module(const WASMModule *parent_module, /* check the global registered module list */ sub_module = - (WASMModule *)wasm_runtime_find_module_registered(sub_module_name); + (WASMModule *)wasm_runtime_find_module_registered(sub_module_name); if (sub_module) { LOG_DEBUG("%s has been loaded", sub_module_name); goto register_sub_module; @@ -958,8 +934,7 @@ load_depended_module(const WASMModule *parent_module, if (!reader) { set_error_buf_v(error_buf, error_buf_size, - "no sub module reader to load %s", - sub_module_name); + "no sub module reader to load %s", sub_module_name); return NULL; } @@ -967,16 +942,14 @@ load_depended_module(const WASMModule *parent_module, ret = wasm_runtime_is_loading_module(sub_module_name); if (ret) { set_error_buf_v(error_buf, error_buf_size, - "found circular dependency on %s", - sub_module_name); + "found circular dependency on %s", sub_module_name); return NULL; } ret = wasm_runtime_add_loading_module(sub_module_name, error_buf, error_buf_size); if (!ret) { - LOG_DEBUG("can not add %s into loading module list\n", - sub_module_name); + LOG_DEBUG("can not add %s into loading module list\n", sub_module_name); return NULL; } @@ -989,7 +962,7 @@ load_depended_module(const WASMModule *parent_module, } sub_module = - wasm_loader_load(buffer, buffer_size, error_buf, error_buf_size); + wasm_loader_load(buffer, buffer_size, error_buf, error_buf_size); if (!sub_module) { LOG_DEBUG("error: can not load the sub_module %s", sub_module_name); /* others will be destroyed in runtime_destroy() */ @@ -999,12 +972,12 @@ load_depended_module(const WASMModule *parent_module, wasm_runtime_delete_loading_module(sub_module_name); /* register on a global list */ - ret = wasm_runtime_register_module_internal(sub_module_name, - (WASMModuleCommon*)sub_module, - buffer, buffer_size, error_buf, - error_buf_size); + ret = wasm_runtime_register_module_internal( + sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size, + error_buf, error_buf_size); if (!ret) { - LOG_DEBUG("error: can not register module %s globally\n", sub_module_name); + LOG_DEBUG("error: can not register module %s globally\n", + sub_module_name); /* others will be unloaded in runtime_destroy() */ goto unload_module; } @@ -1014,8 +987,7 @@ load_depended_module(const WASMModule *parent_module, ret = register_sub_module(parent_module, sub_module_name, sub_module); if (!ret) { set_error_buf_v(error_buf, error_buf_size, - "failed to register sub module %s", - sub_module_name); + "failed to register sub module %s", sub_module_name); /* since it is in the global module list, no need to * unload the module. the runtime_destroy() will do it */ @@ -1045,10 +1017,9 @@ load_depended_module(const WASMModule *parent_module, static bool load_function_import(const uint8 **p_buf, const uint8 *buf_end, const WASMModule *parent_module, - const char *sub_module_name, - const char *function_name, - WASMFunctionImport *function, - char *error_buf, uint32 error_buf_size) + const char *sub_module_name, const char *function_name, + WASMFunctionImport *function, char *error_buf, + uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint32 declare_type_index = 0; @@ -1062,7 +1033,6 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, bool linked_call_conv_raw = false; bool is_native_symbol = false; - CHECK_BUF(p, p_end, 1); read_leb_uint32(p, p_end, declare_type_index); *p_buf = p; @@ -1074,19 +1044,15 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) declare_type_index = wasm_get_smallest_type_idx( - parent_module->types, parent_module->type_count, - declare_type_index); + parent_module->types, parent_module->type_count, declare_type_index); #endif declare_func_type = parent_module->types[declare_type_index]; /* lookup registered native symbols first */ - linked_func = wasm_native_resolve_symbol(sub_module_name, - function_name, - declare_func_type, - &linked_signature, - &linked_attachment, - &linked_call_conv_raw); + linked_func = wasm_native_resolve_symbol( + sub_module_name, function_name, declare_func_type, &linked_signature, + &linked_attachment, &linked_call_conv_raw); if (linked_func) { is_native_symbol = true; } @@ -1099,11 +1065,9 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, return false; } } - linked_func = wasm_loader_resolve_function(sub_module_name, - function_name, - declare_func_type, - error_buf, - error_buf_size); + linked_func = wasm_loader_resolve_function( + sub_module_name, function_name, declare_func_type, error_buf, + error_buf_size); } #endif @@ -1125,8 +1089,8 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, } static bool -check_table_max_size(uint32 init_size, uint32 max_size, - char *error_buf, uint32 error_buf_size) +check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf, + uint32 error_buf_size) { if (max_size < init_size) { set_error_buf(error_buf, error_buf_size, @@ -1138,10 +1102,8 @@ check_table_max_size(uint32 init_size, uint32 max_size, static bool load_table_import(const uint8 **p_buf, const uint8 *buf_end, - WASMModule *parent_module, - const char *sub_module_name, - const char *table_name, - WASMTableImport *table, + WASMModule *parent_module, const char *sub_module_name, + const char *table_name, WASMTableImport *table, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; @@ -1194,9 +1156,8 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end, } linked_table = wasm_loader_resolve_table( - sub_module_name, table_name, - declare_init_size, declare_max_size, - error_buf, error_buf_size); + sub_module_name, table_name, declare_init_size, declare_max_size, + error_buf, error_buf_size); if (!linked_table) { return false; } @@ -1248,8 +1209,7 @@ unsigned wasm_runtime_memory_pool_size(); static bool -check_memory_init_size(uint32 init_size, - char *error_buf, uint32 error_buf_size) +check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size) { if (init_size > 65536) { set_error_buf(error_buf, error_buf_size, @@ -1260,8 +1220,8 @@ check_memory_init_size(uint32 init_size, } static bool -check_memory_max_size(uint32 init_size, uint32 max_size, - char *error_buf, uint32 error_buf_size) +check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf, + uint32 error_buf_size) { if (max_size < init_size) { set_error_buf(error_buf, error_buf_size, @@ -1279,10 +1239,8 @@ check_memory_max_size(uint32 init_size, uint32 max_size, static bool load_memory_import(const uint8 **p_buf, const uint8 *buf_end, - WASMModule *parent_module, - const char *sub_module_name, - const char *memory_name, - WASMMemoryImport *memory, + WASMModule *parent_module, const char *sub_module_name, + const char *memory_name, WASMMemoryImport *memory, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; @@ -1333,9 +1291,8 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, } linked_memory = wasm_loader_resolve_memory( - sub_module_name, memory_name, - declare_init_page_count, declare_max_page_count, - error_buf, error_buf_size); + sub_module_name, memory_name, declare_init_page_count, + declare_max_page_count, error_buf, error_buf_size); if (!linked_memory) { return false; } @@ -1386,10 +1343,9 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, static bool load_global_import(const uint8 **p_buf, const uint8 *buf_end, - const WASMModule *parent_module, - char *sub_module_name, char *global_name, - WASMGlobalImport *global, - char *error_buf, uint32 error_buf_size) + const WASMModule *parent_module, char *sub_module_name, + char *global_name, WASMGlobalImport *global, char *error_buf, + uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint8 declare_type = 0; @@ -1411,7 +1367,7 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end, #if WASM_ENABLE_LIBC_BUILTIN != 0 global->is_linked = wasm_native_lookup_libc_builtin_global( - sub_module_name, global_name, global); + sub_module_name, global_name, global); if (global->is_linked) { if (global->type != declare_type || global->is_mutable != declare_mutable) { @@ -1431,10 +1387,9 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end, } /* check sub modules */ - linked_global = - wasm_loader_resolve_global(sub_module_name, global_name, - declare_type, declare_mutable, - error_buf, error_buf_size); + linked_global = wasm_loader_resolve_global( + sub_module_name, global_name, declare_type, declare_mutable, + error_buf, error_buf_size); if (linked_global) { global->import_module = sub_module; global->import_global_linked = linked_global; @@ -1550,22 +1505,23 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory, return false; } else if (memory->flags == 2) { - set_error_buf(error_buf, error_buf_size, "shared memory must have maximum"); + set_error_buf(error_buf, error_buf_size, + "shared memory must have maximum"); return false; } #endif read_leb_uint32(p, p_end, memory->init_page_count); - if (!check_memory_init_size(memory->init_page_count, - error_buf, error_buf_size)) + if (!check_memory_init_size(memory->init_page_count, error_buf, + error_buf_size)) return false; if (memory->flags & 1) { read_leb_uint32(p, p_end, memory->max_page_count); if (!check_memory_max_size(memory->init_page_count, - memory->max_page_count, - error_buf, error_buf_size)) - return false; + memory->max_page_count, error_buf, + error_buf_size)) + return false; if (memory->max_page_count > max_page_count) memory->max_page_count = max_page_count; } @@ -1596,10 +1552,11 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, uint8 u8, kind; /* insert builtin module names into const str list */ - if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size) - || !const_str_list_insert((uint8*)"wasi_unstable", 13, module, + if (!const_str_list_insert((uint8 *)"env", 3, module, error_buf, + error_buf_size) + || !const_str_list_insert((uint8 *)"wasi_unstable", 13, module, error_buf, error_buf_size) - || !const_str_list_insert((uint8*)"wasi_snapshot_preview1", 22, module, + || !const_str_list_insert((uint8 *)"wasi_snapshot_preview1", 22, module, error_buf, error_buf_size)) { return false; } @@ -1609,8 +1566,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (import_count) { module->import_count = import_count; total_size = sizeof(WASMImport) * (uint64)import_count; - if (!(module->imports = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->imports = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1650,10 +1607,10 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if ( #if WASM_ENABLE_REF_TYPES != 0 - !wasm_get_ref_types_flag() && + !wasm_get_ref_types_flag() && #endif - module->import_table_count > 1) { + module->import_table_count > 1) { set_error_buf(error_buf, error_buf_size, "multiple tables"); return false; @@ -1693,11 +1650,12 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->imports + module->import_function_count; if (module->import_memory_count) import_memories = module->import_memories = - module->imports + module->import_function_count + module->import_table_count; + module->imports + module->import_function_count + + module->import_table_count; if (module->import_global_count) import_globals = module->import_globals = - module->imports + module->import_function_count + module->import_table_count - + module->import_memory_count; + module->imports + module->import_function_count + + module->import_table_count + module->import_memory_count; p = p_old; @@ -1707,7 +1665,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(sub_module_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, error_buf, error_buf_size))) { return false; } p += name_len; @@ -1716,7 +1674,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(field_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, error_buf, error_buf_size))) { return false; } p += name_len; @@ -1729,10 +1687,9 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_FUNC: /* import function */ bh_assert(import_functions); import = import_functions++; - if (!load_function_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.function, - error_buf, error_buf_size)) { + if (!load_function_import( + &p, p_end, module, sub_module_name, field_name, + &import->u.function, error_buf, error_buf_size)) { return false; } break; @@ -1740,9 +1697,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_TABLE: /* import table */ bh_assert(import_tables); import = import_tables++; - if (!load_table_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.table, + if (!load_table_import(&p, p_end, module, sub_module_name, + field_name, &import->u.table, error_buf, error_buf_size)) { LOG_DEBUG("can not import such a table (%s,%s)", sub_module_name, field_name); @@ -1753,9 +1709,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_MEMORY: /* import memory */ bh_assert(import_memories); import = import_memories++; - if (!load_memory_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.memory, + if (!load_memory_import(&p, p_end, module, sub_module_name, + field_name, &import->u.memory, error_buf, error_buf_size)) { return false; } @@ -1764,9 +1719,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_GLOBAL: /* import global */ bh_assert(import_globals); import = import_globals++; - if (!load_global_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.global, + if (!load_global_import(&p, p_end, module, sub_module_name, + field_name, &import->u.global, error_buf, error_buf_size)) { return false; } @@ -1786,7 +1740,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, import = module->import_functions; for (i = 0; i < module->import_function_count; i++, import++) { if (!strcmp(import->u.names.module_name, "wasi_unstable") - || !strcmp(import->u.names.module_name, "wasi_snapshot_preview1")) { + || !strcmp(import->u.names.module_name, + "wasi_snapshot_preview1")) { module->is_wasi_module = true; break; } @@ -1809,8 +1764,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } static bool -init_function_local_offsets(WASMFunction *func, - char *error_buf, uint32 error_buf_size) +init_function_local_offsets(WASMFunction *func, char *error_buf, + uint32 error_buf_size) { WASMType *param_type = func->func_type; uint32 param_count = param_type->param_count; @@ -1827,7 +1782,7 @@ init_function_local_offsets(WASMFunction *func, */ if (total_size > 0 && !(func->local_offsets = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1848,8 +1803,8 @@ init_function_local_offsets(WASMFunction *func, static bool load_function_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code, const uint8 *buf_code_end, - WASMModule *module, - char *error_buf, uint32 error_buf_size) + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; const uint8 *p_code = buf_code, *p_code_end, *p_code_save; @@ -1874,9 +1829,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (func_count) { module->function_count = func_count; - total_size = sizeof(WASMFunction*) * (uint64)func_count; + total_size = sizeof(WASMFunction *) * (uint64)func_count; if (!(module->functions = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1890,12 +1845,11 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) type_index = wasm_get_smallest_type_idx( - module->types, module->type_count, type_index); + module->types, module->type_count, type_index); #endif read_leb_uint32(p_code, buf_code_end, code_size); - if (code_size == 0 - || p_code + code_size > buf_code_end) { + if (code_size == 0 || p_code + code_size > buf_code_end) { set_error_buf(error_buf, error_buf_size, "invalid function code size"); return false; @@ -1911,8 +1865,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, for (j = 0; j < local_set_count; j++) { read_leb_uint32(p_code, buf_code_end, sub_local_count); if (sub_local_count > UINT32_MAX - local_count) { - set_error_buf(error_buf, error_buf_size, - "too many locals"); + set_error_buf(error_buf, error_buf_size, "too many locals"); return false; } CHECK_BUF(p_code, buf_code_end, 1); @@ -1926,7 +1879,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, total_size = sizeof(WASMFunction) + (uint64)local_count; if (!(func = module->functions[i] = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1934,7 +1887,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, func->func_type = module->types[type_index]; func->local_count = local_count; if (local_count > 0) - func->local_types = (uint8*)func + sizeof(WASMFunction); + func->local_types = (uint8 *)func + sizeof(WASMFunction); func->code_size = code_size; /* * we shall make a copy of code body [p_code, p_code + code_size] @@ -1946,7 +1899,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, * memcpy(code_body_cp, p_code, code_size); * func->code = code_body_cp; */ - func->code = (uint8*)p_code; + func->code = (uint8 *)p_code; /* Load each local type */ p_code = p_code_save; @@ -2016,10 +1969,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, } static bool -check_function_index(const WASMModule *module, - uint32 function_index, - char *error_buf, - uint32 error_buf_size) +check_function_index(const WASMModule *module, uint32 function_index, + char *error_buf, uint32 error_buf_size) { if (function_index >= module->import_function_count + module->function_count) { @@ -2042,9 +1993,9 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, table_count); if ( #if WASM_ENABLE_REF_TYPES != 0 - !wasm_get_ref_types_flag() && + !wasm_get_ref_types_flag() && #endif - module->import_table_count + table_count > 1) { + module->import_table_count + table_count > 1) { /* a total of one table is allowed */ set_error_buf(error_buf, error_buf_size, "multiple tables"); return false; @@ -2053,8 +2004,8 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (table_count) { module->table_count = table_count; total_size = sizeof(WASMTable) * (uint64)table_count; - if (!(module->tables = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->tables = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -2095,8 +2046,8 @@ load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (memory_count) { module->memory_count = memory_count; total_size = sizeof(WASMMemory) * (uint64)memory_count; - if (!(module->memories = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->memories = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -2133,14 +2084,14 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (global_count) { module->global_count = global_count; total_size = sizeof(WASMGlobal) * (uint64)global_count; - if (!(module->globals = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->globals = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } global = module->globals; - for(i = 0; i < global_count; i++, global++) { + for (i = 0; i < global_count; i++, global++) { CHECK_BUF(p, p_end, 2); global->type = read_uint8(p); mutable = read_uint8(p); @@ -2151,8 +2102,8 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, global->is_mutable = mutable ? true : false; /* initialize expression */ - if (!load_init_expr(&p, p_end, &(global->init_expr), - global->type, error_buf, error_buf_size)) + if (!load_init_expr(&p, p_end, &(global->init_expr), global->type, + error_buf, error_buf_size)) return false; if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) { @@ -2168,9 +2119,9 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, return false; } } - else if (INIT_EXPR_TYPE_FUNCREF_CONST == global->init_expr.init_expr_type) { - if (!check_function_index(module, - global->init_expr.u.ref_index, + else if (INIT_EXPR_TYPE_FUNCREF_CONST + == global->init_expr.init_expr_type) { + if (!check_function_index(module, global->init_expr.u.ref_index, error_buf, error_buf_size)) { return false; } @@ -2205,28 +2156,27 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (export_count) { module->export_count = export_count; total_size = sizeof(WASMExport) * (uint64)export_count; - if (!(module->exports = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->exports = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } export = module->exports; - for (i = 0; i < export_count; i++, export++) { + for (i = 0; i < export_count; i++, export ++) { read_leb_uint32(p, p_end, str_len); CHECK_BUF(p, p_end, str_len); for (j = 0; j < i; j++) { name = module->exports[j].name; - if (strlen(name) == str_len - && memcmp(name, p, str_len) == 0) { - set_error_buf(error_buf, error_buf_size, - "duplicate export name"); - return false; + if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) { + set_error_buf(error_buf, error_buf_size, + "duplicate export name"); + return false; } } - if (!(export->name = const_str_list_insert(p, str_len, module, - error_buf, error_buf_size))) { + if (!(export->name = const_str_list_insert( + p, str_len, module, error_buf, error_buf_size))) { return false; } @@ -2236,11 +2186,11 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, index); export->index = index; - switch(export->kind) { + switch (export->kind) { /* function index */ case EXPORT_KIND_FUNC: if (index >= module->function_count - + module->import_function_count) { + + module->import_function_count) { set_error_buf(error_buf, error_buf_size, "unknown function"); return false; @@ -2254,8 +2204,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, break; /* table index */ case EXPORT_KIND_TABLE: - if (index >= module->table_count - + module->import_table_count) { + if (index + >= module->table_count + module->import_table_count) { set_error_buf(error_buf, error_buf_size, "unknown table"); return false; @@ -2263,8 +2213,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, break; /* memory index */ case EXPORT_KIND_MEMORY: - if (index >= module->memory_count - + module->import_memory_count) { + if (index + >= module->memory_count + module->import_memory_count) { set_error_buf(error_buf, error_buf_size, "unknown memory"); return false; @@ -2272,8 +2222,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, break; /* global index */ case EXPORT_KIND_GLOBAL: - if (index >= module->global_count - + module->import_global_count) { + if (index + >= module->global_count + module->import_global_count) { set_error_buf(error_buf, error_buf_size, "unknown global"); return false; @@ -2288,8 +2238,7 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -2300,14 +2249,14 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } static bool -check_table_index(const WASMModule *module, uint32 table_index, - char *error_buf, uint32 error_buf_size) +check_table_index(const WASMModule *module, uint32 table_index, char *error_buf, + uint32 error_buf_size) { if ( #if WASM_ENABLE_REF_TYPES != 0 - !wasm_get_ref_types_flag() && + !wasm_get_ref_types_flag() && #endif - table_index != 0) { + table_index != 0) { set_error_buf(error_buf, error_buf_size, "zero byte expected"); return false; } @@ -2321,9 +2270,8 @@ check_table_index(const WASMModule *module, uint32 table_index, } static bool -load_table_index(const uint8 **p_buf, const uint8 *buf_end, - WASMModule *module, uint32 *p_table_index, - char *error_buf, uint32 error_buf_size) +load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module, + uint32 *p_table_index, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint32 table_index; @@ -2342,9 +2290,8 @@ load_table_index(const uint8 **p_buf, const uint8 *buf_end, #if WASM_ENABLE_REF_TYPES != 0 static bool -load_elem_type(const uint8 **p_buf, const uint8 *buf_end, - uint32 *p_elem_type, bool elemkind_zero, - char *error_buf, uint32 error_buf_size) +load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type, + bool elemkind_zero, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint8 elem_type; @@ -2372,8 +2319,7 @@ load_elem_type(const uint8 **p_buf, const uint8 *buf_end, static bool load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module, WASMTableSeg *table_segment, - bool use_init_expr, - char *error_buf, uint32 error_buf_size) + bool use_init_expr, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint32 function_count, function_index = 0, i; @@ -2383,8 +2329,8 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, table_segment->function_count = function_count; total_size = sizeof(uint32) * (uint64)function_count; if (total_size > 0 - && !(table_segment->func_indexes = (uint32 *) - loader_malloc(total_size, error_buf, error_buf_size))) { + && !(table_segment->func_indexes = (uint32 *)loader_malloc( + total_size, error_buf, error_buf_size))) { return false; } @@ -2428,8 +2374,9 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) +load_table_segment_section(const uint8 *buf, const uint8 *buf_end, + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 table_segment_count, i; @@ -2441,8 +2388,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m if (table_segment_count) { module->table_seg_count = table_segment_count; total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count; - if (!(module->table_segments = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->table_segments = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -2472,13 +2419,13 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m error_buf, error_buf_size)) return false; if (!load_init_expr( - &p, p_end, &table_segment->base_offset, - VALUE_TYPE_I32, error_buf, error_buf_size)) + &p, p_end, &table_segment->base_offset, + VALUE_TYPE_I32, error_buf, error_buf_size)) return false; if (!load_func_index_vec( - &p, p_end, module, table_segment, - table_segment->mode == 0 ? false : true, - error_buf, error_buf_size)) + &p, p_end, module, table_segment, + table_segment->mode == 0 ? false : true, + error_buf, error_buf_size)) return false; break; /* elemkind + passive/declarative */ @@ -2501,18 +2448,18 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m error_buf, error_buf_size)) return false; if (!load_init_expr( - &p, p_end, &table_segment->base_offset, - VALUE_TYPE_I32, error_buf, error_buf_size)) + &p, p_end, &table_segment->base_offset, + VALUE_TYPE_I32, error_buf, error_buf_size)) return false; if (!load_elem_type( - &p, p_end, &table_segment->elem_type, - table_segment->mode == 2 ? true : false, - error_buf, error_buf_size)) + &p, p_end, &table_segment->elem_type, + table_segment->mode == 2 ? true : false, + error_buf, error_buf_size)) return false; if (!load_func_index_vec( - &p, p_end, module, table_segment, - table_segment->mode == 2 ? false : true, - error_buf, error_buf_size)) + &p, p_end, module, table_segment, + table_segment->mode == 2 ? false : true, + error_buf, error_buf_size)) return false; break; case 5: @@ -2522,8 +2469,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m error_buf, error_buf_size)) return false; if (!load_func_index_vec(&p, p_end, module, - table_segment, true, - error_buf, error_buf_size)) + table_segment, true, error_buf, + error_buf_size)) return false; break; default: @@ -2566,8 +2513,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m static bool load_data_segment_section(const uint8 *buf, const uint8 *buf_end, - WASMModule *module, - char *error_buf, uint32 error_buf_size) + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 data_seg_count, i, mem_index, data_seg_len; @@ -2592,9 +2539,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, if (data_seg_count) { module->data_seg_count = data_seg_count; - total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count; - if (!(module->data_segments = loader_malloc - (total_size, error_buf, error_buf_size))) { + total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count; + if (!(module->data_segments = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -2614,7 +2561,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, case 0x02: /* read following memory index */ read_leb_uint32(p, p_end, mem_index); -check_mem_index: + check_mem_index: if (mem_index >= module->import_memory_count + module->memory_count) { set_error_buf_v(error_buf, error_buf_size, @@ -2625,7 +2572,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, case 0x03: default: set_error_buf(error_buf, error_buf_size, "unknown memory"); - return false; + return false; break; } #else @@ -2646,8 +2593,8 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, read_leb_uint32(p, p_end, data_seg_len); - if (!(dataseg = module->data_segments[i] = loader_malloc - (sizeof(WASMDataSeg), error_buf, error_buf_size))) { + if (!(dataseg = module->data_segments[i] = loader_malloc( + sizeof(WASMDataSeg), error_buf, error_buf_size))) { return false; } @@ -2656,15 +2603,16 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, if (!is_passive) #endif { - bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression), - &init_expr, sizeof(InitializerExpression)); + bh_memcpy_s(&dataseg->base_offset, + sizeof(InitializerExpression), &init_expr, + sizeof(InitializerExpression)); dataseg->memory_index = mem_index; } dataseg->data_length = data_seg_len; CHECK_BUF(p, p_end, data_seg_len); - dataseg->data = (uint8*)p; + dataseg->data = (uint8 *)p; p += data_seg_len; } } @@ -2682,8 +2630,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, #if WASM_ENABLE_BULK_MEMORY != 0 static bool -load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) +load_datacount_section(const uint8 *buf, const uint8 *buf_end, + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 data_seg_count1 = 0; @@ -2704,10 +2653,8 @@ load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *modul #endif static bool -load_code_section(const uint8 *buf, const uint8 *buf_end, - const uint8 *buf_func, - const uint8 *buf_func_end, - WASMModule *module, +load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func, + const uint8 *buf_func_end, WASMModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -2752,9 +2699,8 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (start_function < module->import_function_count) type = module->import_functions[start_function].u.function.func_type; else - type = - module->functions[start_function - module->import_function_count] - ->func_type; + type = module->functions[start_function - module->import_function_count] + ->func_type; if (type->param_count != 0 || type->result_count != 0) { set_error_buf(error_buf, error_buf_size, "invalid start function"); return false; @@ -2775,8 +2721,7 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 static bool -handle_name_section(const uint8 *buf, const uint8 *buf_end, - WASMModule *module, +handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -2841,9 +2786,9 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, return false; } if (!(module->functions[func_index]->field_name = - const_str_list_insert(p, func_name_len, - module, error_buf, - error_buf_size))) { + const_str_list_insert(p, func_name_len, + module, error_buf, + error_buf_size))) { return false; } } @@ -2851,8 +2796,9 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, } } break; - case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection */ - case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */ + case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection + */ + case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */ default: p = p + subsection_size; break; @@ -2880,15 +2826,13 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); - if (name_len == 0 - || p + name_len > p_end) { + if (name_len == 0 || p + name_len > p_end) { set_error_buf(error_buf, error_buf_size, "unexpected end"); return false; } if (!check_utf8_str(p, name_len)) { - set_error_buf(error_buf, error_buf_size, - "invalid UTF-8 encoding"); + set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding"); return false; } @@ -2905,9 +2849,9 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } static bool -wasm_loader_prepare_bytecode(WASMModule *module, - WASMFunction *func, uint32 cur_func_idx, - char *error_buf, uint32 error_buf_size); +wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, + uint32 cur_func_idx, char *error_buf, + uint32 error_buf_size); #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0 void ** @@ -2917,13 +2861,13 @@ static void **handle_table; #endif static bool -load_from_sections(WASMModule *module, WASMSection *sections, - char *error_buf, uint32 error_buf_size) +load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, + uint32 error_buf_size) { WASMExport *export; WASMSection *section = sections; const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL, - *buf_func = NULL, *buf_func_end = NULL; + *buf_func = NULL, *buf_func_end = NULL; WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL; WASMGlobal *aux_stack_top_global = NULL, *global; uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1; @@ -2956,18 +2900,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, switch (section->section_type) { case SECTION_TYPE_USER: /* unsupported user section, ignore it. */ - if (!load_user_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_user_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_TYPE: - if (!load_type_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_type_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_IMPORT: - if (!load_import_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_import_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_FUNC: @@ -2976,33 +2920,33 @@ load_from_sections(WASMModule *module, WASMSection *sections, return false; break; case SECTION_TYPE_TABLE: - if (!load_table_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_table_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_MEMORY: - if (!load_memory_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_memory_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_GLOBAL: - if (!load_global_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_global_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_export_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_START: - if (!load_start_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_start_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_ELEM: - if (!load_table_segment_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_table_segment_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_CODE: @@ -3011,20 +2955,19 @@ load_from_sections(WASMModule *module, WASMSection *sections, return false; break; case SECTION_TYPE_DATA: - if (!load_data_segment_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_data_segment_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; #if WASM_ENABLE_BULK_MEMORY != 0 case SECTION_TYPE_DATACOUNT: - if (!load_datacount_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_datacount_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; #endif default: - set_error_buf(error_buf, error_buf_size, - "invalid section id"); + set_error_buf(error_buf, error_buf_size, "invalid section id"); return false; } @@ -3037,15 +2980,14 @@ load_from_sections(WASMModule *module, WASMSection *sections, /* Resolve auxiliary data/stack/heap info and reset memory info */ export = module->exports; - for (i = 0; i < module->export_count; i++, export++) { + for (i = 0; i < module->export_count; i++, export ++) { if (export->kind == EXPORT_KIND_GLOBAL) { if (!strcmp(export->name, "__heap_base")) { global_index = export->index - module->import_global_count; global = module->globals + global_index; - if (global->type == VALUE_TYPE_I32 - && !global->is_mutable - && global->init_expr.init_expr_type == - INIT_EXPR_TYPE_I32_CONST) { + if (global->type == VALUE_TYPE_I32 && !global->is_mutable + && global->init_expr.init_expr_type + == INIT_EXPR_TYPE_I32_CONST) { aux_heap_base_global = global; aux_heap_base = global->init_expr.u.i32; aux_heap_base_global_index = export->index; @@ -3056,10 +2998,9 @@ load_from_sections(WASMModule *module, WASMSection *sections, else if (!strcmp(export->name, "__data_end")) { global_index = export->index - module->import_global_count; global = module->globals + global_index; - if (global->type == VALUE_TYPE_I32 - && !global->is_mutable - && global->init_expr.init_expr_type == - INIT_EXPR_TYPE_I32_CONST) { + if (global->type == VALUE_TYPE_I32 && !global->is_mutable + && global->init_expr.init_expr_type + == INIT_EXPR_TYPE_I32_CONST) { aux_data_end_global = global; aux_data_end = global->init_expr.u.i32; aux_data_end_global_index = export->index; @@ -3100,17 +3041,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, if (global->is_mutable /* heap_base and data_end is not mutable */ && global->type == VALUE_TYPE_I32 - && global->init_expr.init_expr_type == - INIT_EXPR_TYPE_I32_CONST + && global->init_expr.init_expr_type + == INIT_EXPR_TYPE_I32_CONST && (uint32)global->init_expr.u.i32 <= aux_heap_base) { aux_stack_top_global = global; aux_stack_top = (uint32)global->init_expr.u.i32; module->aux_stack_top_global_index = - module->import_global_count + global_index; + module->import_global_count + global_index; module->aux_stack_bottom = aux_stack_top; - module->aux_stack_size = aux_stack_top > aux_data_end - ? aux_stack_top - aux_data_end - : aux_stack_top; + module->aux_stack_size = + aux_stack_top > aux_data_end + ? aux_stack_top - aux_data_end + : aux_stack_top; LOG_VERBOSE("Found aux stack top global, value: %d, " "global index: %d, stack size: %d", aux_stack_top, global_index, @@ -3129,14 +3071,13 @@ load_from_sections(WASMModule *module, WASMSection *sections, /* Resolve malloc/free function exported by wasm module */ export = module->exports; - for (i = 0; i < module->export_count; i++, export++) { + for (i = 0; i < module->export_count; i++, export ++) { if (export->kind == EXPORT_KIND_FUNC) { if (!strcmp(export->name, "malloc") && export->index >= module->import_function_count) { 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 + 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) { bh_assert(module->malloc_function == (uint32)-1); @@ -3150,8 +3091,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, /* __new && __pin for AssemblyScript */ 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 + if (func_type->param_count == 2 && func_type->result_count == 1 && func_type->types[0] == VALUE_TYPE_I32 && func_type->types[1] == VALUE_TYPE_I32 && func_type->types[2] == VALUE_TYPE_I32) { @@ -3173,19 +3113,20 @@ load_from_sections(WASMModule *module, WASMSection *sections, && (export_tmp->index >= module->import_function_count)) { func_index = export_tmp->index - - module->import_function_count; + - module->import_function_count; func_type = - module->functions[func_index]->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) { - bh_assert( - module->retain_function == (uint32)-1); + bh_assert(module->retain_function + == (uint32)-1); module->retain_function = export_tmp->index; - LOG_VERBOSE( - "Found retain function, name: %s, index: %u", - export_tmp->name, export_tmp->index); + LOG_VERBOSE("Found retain function, name: %s, " + "index: %u", + export_tmp->name, + export_tmp->index); break; } } @@ -3203,8 +3144,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, && export->index >= module->import_function_count) { 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 + if (func_type->param_count == 1 && func_type->result_count == 0 && func_type->types[0] == VALUE_TYPE_I32) { bh_assert(module->free_function == (uint32)-1); module->free_function = export->index; @@ -3221,8 +3161,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, for (i = 0; i < module->function_count; i++) { WASMFunction *func = module->functions[i]; - if (!wasm_loader_prepare_bytecode(module, func, i, - error_buf, error_buf_size)) { + if (!wasm_loader_prepare_bytecode(module, func, i, error_buf, + error_buf_size)) { return false; } } @@ -3231,16 +3171,15 @@ load_from_sections(WASMModule *module, WASMSection *sections, WASMMemoryImport *memory_import; WASMMemory *memory; - if (aux_data_end_global - && aux_heap_base_global + if (aux_data_end_global && aux_heap_base_global && aux_stack_top_global) { uint64 init_memory_size; uint32 shrunk_memory_size = align_uint(aux_heap_base, 8); if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - init_memory_size = (uint64)memory_import->num_bytes_per_page * - memory_import->init_page_count; + init_memory_size = (uint64)memory_import->num_bytes_per_page + * memory_import->init_page_count; if (shrunk_memory_size <= init_memory_size) { /* Reset memory info to decrease memory usage */ memory_import->num_bytes_per_page = shrunk_memory_size; @@ -3251,8 +3190,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, } if (module->memory_count) { memory = &module->memories[0]; - init_memory_size = (uint64)memory->num_bytes_per_page * - memory->init_page_count; + init_memory_size = (uint64)memory->num_bytes_per_page + * memory->init_page_count; if (shrunk_memory_size <= init_memory_size) { /* Reset memory info to decrease memory usage */ memory->num_bytes_per_page = shrunk_memory_size; @@ -3281,16 +3220,16 @@ load_from_sections(WASMModule *module, WASMSection *sections, } #if WASM_ENABLE_MEMORY_TRACING != 0 - wasm_runtime_dump_module_mem_consumption((WASMModuleCommon*)module); + wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module); #endif return true; } -static WASMModule* +static WASMModule * create_module(char *error_buf, uint32 error_buf_size) { - WASMModule *module = loader_malloc(sizeof(WASMModule), - error_buf, error_buf_size); + WASMModule *module = + loader_malloc(sizeof(WASMModule), error_buf, error_buf_size); if (!module) { return NULL; @@ -3312,9 +3251,10 @@ create_module(char *error_buf, uint32 error_buf_size) #if WASM_ENABLE_DEBUG_INTERP != 0 static void -record_fast_op(WASMModule *module, uint8 * pos, uint8 orig_op) +record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op) { - WASMFastOPCodeNode *fast_op = wasm_runtime_malloc(sizeof(WASMFastOPCodeNode)); + WASMFastOPCodeNode *fast_op = + wasm_runtime_malloc(sizeof(WASMFastOPCodeNode)); if (fast_op) { fast_op->offset = pos - module->load_addr; fast_op->orig_op = orig_op; @@ -3324,8 +3264,8 @@ record_fast_op(WASMModule *module, uint8 * pos, uint8 orig_op) #endif WASMModule * -wasm_loader_load_from_sections(WASMSection *section_list, - char *error_buf, uint32 error_buf_size) +wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf, + uint32 error_buf_size) { WASMModule *module = create_module(error_buf, error_buf_size); if (!module) @@ -3351,6 +3291,7 @@ destroy_sections(WASMSection *section_list) } } +/* clang-format off */ static uint8 section_ids[] = { SECTION_TYPE_USER, SECTION_TYPE_TYPE, @@ -3368,6 +3309,7 @@ static uint8 section_ids[] = { SECTION_TYPE_CODE, SECTION_TYPE_DATA }; +/* clang-format on */ static uint8 get_section_index(uint8 section_type) @@ -3383,12 +3325,11 @@ get_section_index(uint8 section_type) } static bool -create_sections(const uint8 *buf, uint32 size, - WASMSection **p_section_list, +create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list, char *error_buf, uint32 error_buf_size) { WASMSection *section_list_end = NULL, *section; - const uint8 *p = buf, *p_end = buf + size/*, *section_body*/; + const uint8 *p = buf, *p_end = buf + size /*, *section_body*/; uint8 section_type, section_index, last_section_index = (uint8)-1; uint32 section_size; @@ -3416,13 +3357,13 @@ create_sections(const uint8 *buf, uint32 size, read_leb_uint32(p, p_end, section_size); CHECK_BUF1(p, p_end, section_size); - if (!(section = loader_malloc(sizeof(WASMSection), - error_buf, error_buf_size))) { + if (!(section = loader_malloc(sizeof(WASMSection), error_buf, + error_buf_size))) { return false; } section->section_type = section_type; - section->section_body = (uint8*)p; + section->section_body = (uint8 *)p; section->section_body_size = section_size; if (!section_list_end) @@ -3435,8 +3376,7 @@ create_sections(const uint8 *buf, uint32 size, p += section_size; } else { - set_error_buf(error_buf, error_buf_size, - "invalid section id"); + set_error_buf(error_buf, error_buf_size, "invalid section id"); return false; } } @@ -3447,7 +3387,7 @@ create_sections(const uint8 *buf, uint32 size, } static void -exchange32(uint8* p_data) +exchange32(uint8 *p_data) { uint8 value = *p_data; *p_data = *(p_data + 3); @@ -3466,8 +3406,8 @@ static union { #define is_little_endian() (__ue.b == 1) static bool -load(const uint8 *buf, uint32 size, WASMModule *module, - char *error_buf, uint32 error_buf_size) +load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf_end = buf + size; const uint8 *p = buf, *p_end = buf_end; @@ -3477,27 +3417,26 @@ load(const uint8 *buf, uint32 size, WASMModule *module, CHECK_BUF1(p, p_end, sizeof(uint32)); magic_number = read_uint32(p); if (!is_little_endian()) - exchange32((uint8*)&magic_number); + exchange32((uint8 *)&magic_number); if (magic_number != WASM_MAGIC_NUMBER) { - set_error_buf(error_buf, error_buf_size, - "magic header not detected"); + set_error_buf(error_buf, error_buf_size, "magic header not detected"); return false; } CHECK_BUF1(p, p_end, sizeof(uint32)); version = read_uint32(p); if (!is_little_endian()) - exchange32((uint8*)&version); + exchange32((uint8 *)&version); if (version != WASM_CURRENT_VERSION) { - set_error_buf(error_buf, error_buf_size, - "unknown binary version"); + set_error_buf(error_buf, error_buf_size, "unknown binary version"); return false; } if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size) - || !load_from_sections(module, section_list, error_buf, error_buf_size)) { + || !load_from_sections(module, section_list, error_buf, + error_buf_size)) { destroy_sections(section_list); return false; } @@ -3508,8 +3447,9 @@ load(const uint8 *buf, uint32 size, WASMModule *module, return false; } -WASMModule* -wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size) +WASMModule * +wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size) { WASMModule *module = create_module(error_buf, error_buf_size); if (!module) { @@ -3610,7 +3550,7 @@ wasm_loader_unload(WASMModule *module) /* just release the sub module list */ if (module->import_module_list) { WASMRegisteredModule *node = - bh_list_first_elem(module->import_module_list); + bh_list_first_elem(module->import_module_list); while (node) { WASMRegisteredModule *next = bh_list_elem_next(node); bh_list_remove(module->import_module_list, node); @@ -3631,8 +3571,8 @@ wasm_loader_unload(WASMModule *module) #if WASM_ENABLE_DEBUG_INTERP != 0 WASMFastOPCodeNode *fast_opcode = bh_list_first_elem(&module->fast_opcode_list); - while(fast_opcode) { - WASMFastOPCodeNode * next = bh_list_elem_next(fast_opcode); + while (fast_opcode) { + WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode); wasm_runtime_free(fast_opcode); fast_opcode = next; } @@ -3641,12 +3581,9 @@ wasm_loader_unload(WASMModule *module) } bool -wasm_loader_find_block_addr(WASMExecEnv *exec_env, - BlockAddr *block_addr_cache, - const uint8 *start_addr, - const uint8 *code_end_addr, - uint8 label_type, - uint8 **p_else_addr, +wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache, + const uint8 *start_addr, const uint8 *code_end_addr, + uint8 label_type, uint8 **p_else_addr, uint8 **p_end_addr) { const uint8 *p = start_addr, *p_end = code_end_addr; @@ -3655,7 +3592,7 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, uint32 block_nested_depth = 1, count, i, j, t; uint32 error_buf_size = sizeof(error_buf); uint8 opcode, u8; - BlockAddr block_stack[16] = {{0}}, *block; + BlockAddr block_stack[16] = { { 0 } }, *block; i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; @@ -3675,7 +3612,7 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, while (p < code_end_addr) { opcode = *p++; #if WASM_ENABLE_DEBUG_INTERP != 0 -op_break_retry: + op_break_retry: #endif switch (opcode) { case WASM_OP_UNREACHABLE: @@ -3687,7 +3624,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, case WASM_OP_IF: /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */ u8 = read_uint8(p); - if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) { + if (block_nested_depth + < sizeof(block_stack) / sizeof(BlockAddr)) { block_stack[block_nested_depth].start_addr = p; block_stack[block_nested_depth].else_addr = NULL; } @@ -3699,7 +3637,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, case EXT_OP_IF: /* block type */ skip_leb_uint32(p, p_end); - if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) { + if (block_nested_depth + < sizeof(block_stack) / sizeof(BlockAddr)) { block_stack[block_nested_depth].start_addr = p; block_stack[block_nested_depth].else_addr = NULL; } @@ -3708,33 +3647,37 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, case WASM_OP_ELSE: if (label_type == LABEL_TYPE_IF && block_nested_depth == 1) - else_addr = (uint8*)(p - 1); - if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr)) - block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1); + else_addr = (uint8 *)(p - 1); + if (block_nested_depth - 1 + < sizeof(block_stack) / sizeof(BlockAddr)) + block_stack[block_nested_depth - 1].else_addr = + (uint8 *)(p - 1); break; case WASM_OP_END: if (block_nested_depth == 1) { if (label_type == LABEL_TYPE_IF) *p_else_addr = else_addr; - *p_end_addr = (uint8*)(p - 1); + *p_end_addr = (uint8 *)(p - 1); - block_stack[0].end_addr = (uint8*)(p - 1); - for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) { + block_stack[0].end_addr = (uint8 *)(p - 1); + for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr); + t++) { start_addr = block_stack[t].start_addr; if (start_addr) { i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); - block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; + block = + block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) if (!block[j].start_addr) break; if (j == BLOCK_ADDR_CONFLICT_SIZE) { - memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) * - sizeof(BlockAddr)); + memmove(block + 1, block, + (BLOCK_ADDR_CONFLICT_SIZE - 1) + * sizeof(BlockAddr)); j = 0; - } block[j].start_addr = block_stack[t].start_addr; block[j].else_addr = block_stack[t].else_addr; @@ -3747,8 +3690,10 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, } else { block_nested_depth--; - if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) - block_stack[block_nested_depth].end_addr = (uint8*)(p - 1); + if (block_nested_depth + < sizeof(block_stack) / sizeof(BlockAddr)) + block_stack[block_nested_depth].end_addr = + (uint8 *)(p - 1); } break; @@ -3759,7 +3704,7 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, case WASM_OP_BR_TABLE: read_leb_uint32(p, p_end, count); /* lable num */ - for (i = 0; i <= count; i++) /* lableidxs */ + for (i = 0; i <= count; i++) /* lableidxs */ skip_leb_uint32(p, p_end); break; @@ -4091,9 +4036,11 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) case WASM_OP_SIMD_PREFIX: { - /* TODO: shall we ceate a table to be friendly to branch prediction */ + /* TODO: shall we ceate a table to be friendly to branch + * prediction */ opcode = read_uint8(p); - /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h */ + /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h + */ switch (opcode) { case SIMD_v128_load: case SIMD_v128_load8x8_s: @@ -4167,8 +4114,8 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, default: /* * since latest SIMD specific used almost every value - * from 0x00 to 0xff, the default branch will present all - * opcodes without imm + * from 0x00 to 0xff, the default branch will present + * all opcodes without imm * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md */ break; @@ -4195,19 +4142,20 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, } #endif #if WASM_ENABLE_DEBUG_INTERP != 0 - case DEBUG_OP_BREAK: { + case DEBUG_OP_BREAK: + { WASMDebugInstance *debug_instance = - wasm_exec_env_get_instance(exec_env); + wasm_exec_env_get_instance(exec_env); char orignal_opcode[1]; uint64 size = 1; WASMModuleInstance *module_inst = - (WASMModuleInstance *)exec_env->module_inst; + (WASMModuleInstance *)exec_env->module_inst; uint64 offset = (p - 1) >= module_inst->module->load_addr - ? (p - 1) - module_inst->module->load_addr - : ~0; + ? (p - 1) - module_inst->module->load_addr + : ~0; if (debug_instance) { - if (wasm_debug_instance_get_obj_mem( - debug_instance, offset, orignal_opcode, &size) + if (wasm_debug_instance_get_obj_mem(debug_instance, offset, + orignal_opcode, &size) && size == 1) { LOG_VERBOSE("WASM loader find OP_BREAK , recover it " "with %02x: ", @@ -4231,9 +4179,9 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, return false; } -#define REF_ANY VALUE_TYPE_ANY -#define REF_I32 VALUE_TYPE_I32 -#define REF_F32 VALUE_TYPE_F32 +#define REF_ANY VALUE_TYPE_ANY +#define REF_I32 VALUE_TYPE_I32 +#define REF_F32 VALUE_TYPE_F32 #define REF_I64_1 VALUE_TYPE_I64 #define REF_I64_2 VALUE_TYPE_I64 #define REF_F64_1 VALUE_TYPE_F64 @@ -4242,19 +4190,19 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, #define REF_V128_2 VALUE_TYPE_V128 #define REF_V128_3 VALUE_TYPE_V128 #define REF_V128_4 VALUE_TYPE_V128 -#define REF_FUNCREF VALUE_TYPE_FUNCREF +#define REF_FUNCREF VALUE_TYPE_FUNCREF #define REF_EXTERNREF VALUE_TYPE_EXTERNREF #if WASM_ENABLE_FAST_INTERP != 0 #if WASM_DEBUG_PREPROCESSOR != 0 -#define LOG_OP(...) os_printf(__VA_ARGS__) +#define LOG_OP(...) os_printf(__VA_ARGS__) #else -#define LOG_OP(...) (void)0 +#define LOG_OP(...) (void)0 #endif #define PATCH_ELSE 0 -#define PATCH_END 1 +#define PATCH_END 1 typedef struct BranchBlockPatch { struct BranchBlockPatch *next; uint8 patch_type; @@ -4335,14 +4283,13 @@ typedef struct Const { uint8 value_type; } Const; -static void* -memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, - char *error_buf, uint32 error_buf_size) +static void * +memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf, + uint32 error_buf_size) { uint8 *mem_new; bh_assert(size_new > size_old); - if ((mem_new = loader_malloc - (size_new, error_buf, error_buf_size))) { + if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) { bh_memcpy_s(mem_new, size_new, mem_old, size_old); memset(mem_new + size_old, 0, size_new - size_old); wasm_runtime_free(mem_old); @@ -4350,47 +4297,51 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, return mem_new; } -#define MEM_REALLOC(mem, size_old, size_new) do { \ - void *mem_new = memory_realloc(mem, size_old, size_new, \ - error_buf, error_buf_size); \ - if (!mem_new) \ - goto fail; \ - mem = mem_new; \ - } while (0) - -#define CHECK_CSP_PUSH() do { \ - if (ctx->frame_csp >= ctx->frame_csp_boundary) { \ - MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \ - (uint32)(ctx->frame_csp_size \ - + 8 * sizeof(BranchBlock))); \ - ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \ - ctx->frame_csp_boundary = ctx->frame_csp_bottom + \ - ctx->frame_csp_size / sizeof(BranchBlock); \ - ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \ - } \ - } while (0) - -#define CHECK_CSP_POP() do { \ - if (ctx->csp_num < 1) { \ - set_error_buf(error_buf, error_buf_size, \ - "type mismatch: " \ - "expect data but block stack was empty"); \ - goto fail; \ - } \ - } while (0) +#define MEM_REALLOC(mem, size_old, size_new) \ + do { \ + void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \ + error_buf_size); \ + if (!mem_new) \ + goto fail; \ + mem = mem_new; \ + } while (0) + +#define CHECK_CSP_PUSH() \ + do { \ + if (ctx->frame_csp >= ctx->frame_csp_boundary) { \ + MEM_REALLOC( \ + ctx->frame_csp_bottom, ctx->frame_csp_size, \ + (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \ + ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \ + ctx->frame_csp_boundary = \ + ctx->frame_csp_bottom \ + + ctx->frame_csp_size / sizeof(BranchBlock); \ + ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \ + } \ + } while (0) + +#define CHECK_CSP_POP() \ + do { \ + if (ctx->csp_num < 1) { \ + set_error_buf(error_buf, error_buf_size, \ + "type mismatch: " \ + "expect data but block stack was empty"); \ + goto fail; \ + } \ + } while (0) #if WASM_ENABLE_FAST_INTERP != 0 static bool -check_offset_push(WASMLoaderContext *ctx, - char *error_buf, uint32 error_buf_size) +check_offset_push(WASMLoaderContext *ctx, char *error_buf, + uint32 error_buf_size) { uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom); if (ctx->frame_offset >= ctx->frame_offset_boundary) { MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size, ctx->frame_offset_size + 16); ctx->frame_offset_size += 16; - ctx->frame_offset_boundary = ctx->frame_offset_bottom + - ctx->frame_offset_size / sizeof(int16); + ctx->frame_offset_boundary = + ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16); ctx->frame_offset = ctx->frame_offset_bottom + cell_num; } return true; @@ -4426,15 +4377,14 @@ free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num) for (uint32 i = 0; i < csp_num; i++) { free_label_patch_list(tmp_csp); - tmp_csp ++; + tmp_csp++; } } #endif /* end of WASM_ENABLE_FAST_INTERP */ static bool -check_stack_push(WASMLoaderContext *ctx, - char *error_buf, uint32 error_buf_size) +check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size) { if (ctx->frame_ref >= ctx->frame_ref_boundary) { MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size, @@ -4448,7 +4398,6 @@ check_stack_push(WASMLoaderContext *ctx, return false; } - static bool check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, char *error_buf, uint32 error_buf_size) @@ -4460,7 +4409,7 @@ check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, || (type == VALUE_TYPE_V128 && stack_cell_num < 4) #endif #endif - ) { + ) { set_error_buf(error_buf, error_buf_size, "type mismatch: expect data but stack was empty"); return false; @@ -4472,16 +4421,14 @@ check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, #if WASM_ENABLE_SIMD != 0 #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || (type == VALUE_TYPE_V128 - && (*(frame_ref - 4) != REF_V128_1 - || *(frame_ref - 3) != REF_V128_2 + && (*(frame_ref - 4) != REF_V128_1 || *(frame_ref - 3) != REF_V128_2 || *(frame_ref - 2) != REF_V128_3 || *(frame_ref - 1) != REF_V128_4)) #endif #endif - ) { + ) { set_error_buf_v(error_buf, error_buf_size, "%s%s%s", - "type mismatch: expect ", - type2str(type), + "type mismatch: expect ", type2str(type), " but got other"); return false; } @@ -4490,20 +4437,19 @@ check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, } static bool -check_stack_pop(WASMLoaderContext *ctx, uint8 type, - char *error_buf, uint32 error_buf_size) +check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf, + uint32 error_buf_size) { - int32 block_stack_cell_num = (int32) - (ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num); + int32 block_stack_cell_num = + (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num); - if (block_stack_cell_num > 0 - && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) { + if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) { /* the stack top is a value of any type, return success */ return true; } - if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, - type, error_buf, error_buf_size)) + if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type, + error_buf, error_buf_size)) return false; return true; @@ -4531,7 +4477,7 @@ wasm_loader_ctx_destroy(WASMLoaderContext *ctx) } } -static WASMLoaderContext* +static WASMLoaderContext * wasm_loader_ctx_init(WASMFunction *func) { WASMLoaderContext *loader_ctx = @@ -4542,15 +4488,15 @@ wasm_loader_ctx_init(WASMFunction *func) loader_ctx->frame_ref_size = 32; if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = - wasm_runtime_malloc(loader_ctx->frame_ref_size))) + wasm_runtime_malloc(loader_ctx->frame_ref_size))) goto fail; memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size); - loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + - loader_ctx->frame_ref_size; + loader_ctx->frame_ref_boundary = + loader_ctx->frame_ref_bottom + loader_ctx->frame_ref_size; loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8; if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = - wasm_runtime_malloc(loader_ctx->frame_csp_size))) + wasm_runtime_malloc(loader_ctx->frame_csp_size))) goto fail; memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size); loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8; @@ -4558,21 +4504,21 @@ wasm_loader_ctx_init(WASMFunction *func) #if WASM_ENABLE_FAST_INTERP != 0 loader_ctx->frame_offset_size = sizeof(int16) * 32; if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset = - wasm_runtime_malloc(loader_ctx->frame_offset_size))) + wasm_runtime_malloc(loader_ctx->frame_offset_size))) goto fail; - memset(loader_ctx->frame_offset_bottom, 0, - loader_ctx->frame_offset_size); + memset(loader_ctx->frame_offset_bottom, 0, loader_ctx->frame_offset_size); loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32; loader_ctx->num_const = 0; loader_ctx->const_buf_size = sizeof(Const) * 8; - if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size))) + if (!(loader_ctx->const_buf = + wasm_runtime_malloc(loader_ctx->const_buf_size))) goto fail; memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size); loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset = - loader_ctx->max_dynamic_offset = func->param_cell_num + - func->local_cell_num; + loader_ctx->max_dynamic_offset = + func->param_cell_num + func->local_cell_num; #endif return loader_ctx; @@ -4582,8 +4528,8 @@ wasm_loader_ctx_init(WASMFunction *func) } static bool -wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, - char *error_buf, uint32 error_buf_size) +wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf, + uint32 error_buf_size) { if (type == VALUE_TYPE_VOID) return true; @@ -4624,12 +4570,12 @@ wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, } static bool -wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, - char *error_buf, uint32 error_buf_size) +wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf, + uint32 error_buf_size) { BranchBlock *cur_block = ctx->frame_csp - 1; - int32 available_stack_cell = (int32) - (ctx->stack_cell_num - cur_block->stack_cell_num); + int32 available_stack_cell = + (int32)(ctx->stack_cell_num - cur_block->stack_cell_num); /* Directly return success if current block is in stack * polymorphic state while stack is empty. */ @@ -4664,11 +4610,12 @@ wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, static bool wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt, - uint8 type_push, uint8 type_pop, - char *error_buf, uint32 error_buf_size) + uint8 type_push, uint8 type_pop, char *error_buf, + uint32 error_buf_size) { for (int i = 0; i < pop_cnt; i++) { - if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, error_buf_size)) + if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, + error_buf_size)) return false; } if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size)) @@ -4678,7 +4625,7 @@ wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt, static bool wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type, - BlockType block_type, uint8* start_addr, + BlockType block_type, uint8 *start_addr, char *error_buf, uint32 error_buf_size) { CHECK_CSP_PUSH(); @@ -4701,8 +4648,8 @@ wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type, } static bool -wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, - char *error_buf, uint32 error_buf_size) +wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf, + uint32 error_buf_size) { CHECK_CSP_POP(); #if WASM_ENABLE_FAST_INTERP != 0 @@ -4721,124 +4668,132 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 -#define emit_label(opcode) do { \ - wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \ - LOG_OP("\nemit_op [%02x]\t", opcode); \ - } while (0) -#define skip_label() do { \ - wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \ - LOG_OP("\ndelete last op\n"); \ - } while (0) +#define emit_label(opcode) \ + do { \ + wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) +#define skip_label() \ + do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#define emit_label(opcode) do { \ - int32 offset = (int32)((uint8*)handle_table[opcode] \ - - (uint8*)handle_table[0]); \ - if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \ - set_error_buf(error_buf, error_buf_size, \ - "pre-compiled label offset out of range"); \ - goto fail; \ - } \ - wasm_loader_emit_int16(loader_ctx, offset); \ - LOG_OP("\nemit_op [%02x]\t", opcode); \ - } while (0) -#define skip_label() do { \ - wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \ - LOG_OP("\ndelete last op\n"); \ - } while (0) +#define emit_label(opcode) \ + do { \ + int32 offset = \ + (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \ + if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \ + set_error_buf(error_buf, error_buf_size, \ + "pre-compiled label offset out of range"); \ + goto fail; \ + } \ + wasm_loader_emit_int16(loader_ctx, offset); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) +#define skip_label() \ + do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ -#define emit_label(opcode) do { \ - wasm_loader_emit_uint8(loader_ctx, opcode); \ - LOG_OP("\nemit_op [%02x]\t", opcode); \ - } while (0) -#define skip_label() do { \ - wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \ - LOG_OP("\ndelete last op\n"); \ - } while (0) +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#define emit_label(opcode) \ + do { \ + wasm_loader_emit_uint8(loader_ctx, opcode); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) +#define skip_label() \ + do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ -#define emit_empty_label_addr_and_frame_ip(type) do { \ - if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \ - loader_ctx->p_code_compiled, \ - error_buf, error_buf_size)) \ - goto fail; \ - /* label address, to be patched */ \ - wasm_loader_emit_ptr(loader_ctx, NULL); \ - } while (0) - -#define emit_br_info(frame_csp) do { \ - if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \ - && last_op <= WASM_OP_I32_ROTR) \ - || (last_op == WASM_OP_I32_LOAD \ - || last_op == WASM_OP_F32_LOAD) \ - || (last_op >= WASM_OP_I32_LOAD8_S \ - && last_op <= WASM_OP_I32_LOAD16_U) \ - || (last_op >= WASM_OP_F32_ABS \ - && last_op <= WASM_OP_F32_COPYSIGN) \ - || (last_op >= WASM_OP_I32_WRAP_I64 \ - && last_op <= WASM_OP_I32_TRUNC_U_F64) \ - || (last_op >= WASM_OP_F32_CONVERT_S_I32 \ - && last_op <= WASM_OP_F32_DEMOTE_F64) \ - || (last_op == WASM_OP_I32_REINTERPRET_F32) \ - || (last_op == WASM_OP_F32_REINTERPRET_I32) \ - || (last_op == EXT_OP_COPY_STACK_TOP) - -#define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \ - && last_op <= WASM_OP_I64_ROTR) \ - || (last_op >= WASM_OP_F64_ABS \ - && last_op <= WASM_OP_F64_COPYSIGN) \ - || (last_op == WASM_OP_I64_LOAD \ - || last_op == WASM_OP_F64_LOAD) \ - || (last_op >= WASM_OP_I64_LOAD8_S \ - && last_op <= WASM_OP_I64_LOAD32_U) \ - || (last_op >= WASM_OP_I64_EXTEND_S_I32 \ - && last_op <= WASM_OP_I64_TRUNC_U_F64) \ - || (last_op >= WASM_OP_F64_CONVERT_S_I32 \ - && last_op <= WASM_OP_F64_PROMOTE_F32) \ - || (last_op == WASM_OP_I64_REINTERPRET_F64) \ - || (last_op == WASM_OP_F64_REINTERPRET_I64) \ - || (last_op == EXT_OP_COPY_STACK_TOP_I64) - -#define GET_CONST_OFFSET(type, val) do { \ - if (!(wasm_loader_get_const_offset(loader_ctx, type, \ - &val, &operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define GET_CONST_F32_OFFSET(type, fval) do { \ - if (!(wasm_loader_get_const_offset(loader_ctx, type, \ - &fval, &operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define GET_CONST_F64_OFFSET(type, fval) do { \ - if (!(wasm_loader_get_const_offset(loader_ctx, type, \ - &fval, &operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define emit_operand(ctx, offset) do { \ - wasm_loader_emit_int16(ctx, offset); \ - LOG_OP("%d\t", offset); \ - } while (0) - -#define emit_byte(ctx, byte) do { \ - wasm_loader_emit_uint8(ctx, byte); \ - LOG_OP("%d\t", byte); \ - } while (0) - -#define emit_uint32(ctx, value) do { \ - wasm_loader_emit_uint32(ctx, value); \ - LOG_OP("%d\t", value); \ - } while (0) +#define emit_empty_label_addr_and_frame_ip(type) \ + do { \ + if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \ + loader_ctx->p_code_compiled, error_buf, \ + error_buf_size)) \ + goto fail; \ + /* label address, to be patched */ \ + wasm_loader_emit_ptr(loader_ctx, NULL); \ + } while (0) + +#define emit_br_info(frame_csp) \ + do { \ + if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define LAST_OP_OUTPUT_I32() \ + (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \ + || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \ + || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \ + || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \ + || (last_op >= WASM_OP_I32_WRAP_I64 \ + && last_op <= WASM_OP_I32_TRUNC_U_F64) \ + || (last_op >= WASM_OP_F32_CONVERT_S_I32 \ + && last_op <= WASM_OP_F32_DEMOTE_F64) \ + || (last_op == WASM_OP_I32_REINTERPRET_F32) \ + || (last_op == WASM_OP_F32_REINTERPRET_I32) \ + || (last_op == EXT_OP_COPY_STACK_TOP) + +#define LAST_OP_OUTPUT_I64() \ + (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \ + || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \ + || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \ + || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \ + || (last_op >= WASM_OP_I64_EXTEND_S_I32 \ + && last_op <= WASM_OP_I64_TRUNC_U_F64) \ + || (last_op >= WASM_OP_F64_CONVERT_S_I32 \ + && last_op <= WASM_OP_F64_PROMOTE_F32) \ + || (last_op == WASM_OP_I64_REINTERPRET_F64) \ + || (last_op == WASM_OP_F64_REINTERPRET_I64) \ + || (last_op == EXT_OP_COPY_STACK_TOP_I64) + +#define GET_CONST_OFFSET(type, val) \ + do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \ + &operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define GET_CONST_F32_OFFSET(type, fval) \ + do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \ + &operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define GET_CONST_F64_OFFSET(type, fval) \ + do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \ + &operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define emit_operand(ctx, offset) \ + do { \ + wasm_loader_emit_int16(ctx, offset); \ + LOG_OP("%d\t", offset); \ + } while (0) + +#define emit_byte(ctx, byte) \ + do { \ + wasm_loader_emit_uint8(ctx, byte); \ + LOG_OP("%d\t", byte); \ + } while (0) + +#define emit_uint32(ctx, value) \ + do { \ + wasm_loader_emit_uint32(ctx, value); \ + LOG_OP("%d\t", value); \ + } while (0) static bool wasm_loader_ctx_reinit(WASMLoaderContext *ctx) @@ -4846,8 +4801,7 @@ wasm_loader_ctx_reinit(WASMLoaderContext *ctx) if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size))) return false; memset(ctx->p_code_compiled, 0, ctx->code_compiled_size); - ctx->p_code_compiled_end = ctx->p_code_compiled + - ctx->code_compiled_size; + ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size; /* clean up frame ref */ memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size); @@ -4971,8 +4925,9 @@ wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size) static bool preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode, - uint32 local_index, uint32 local_type, bool *preserved, - char *error_buf, uint32 error_buf_size) + uint32 local_index, uint32 local_type, + bool *preserved, char *error_buf, + uint32 error_buf_size) { uint32 i = 0; int16 preserved_offset = (int16)local_index; @@ -4981,7 +4936,8 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode, while (i < loader_ctx->stack_cell_num) { uint8 cur_type = loader_ctx->frame_ref_bottom[i]; - /* move previous local into dynamic space before a set/tee_local opcode */ + /* move previous local into dynamic space before a set/tee_local opcode + */ if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) { if (!(*preserved)) { *preserved = true; @@ -5056,12 +5012,12 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode, } static bool -add_label_patch_to_list(BranchBlock *frame_csp, - uint8 patch_type, uint8 *p_code_compiled, - char *error_buf, uint32 error_buf_size) +add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type, + uint8 *p_code_compiled, char *error_buf, + uint32 error_buf_size) { - BranchBlockPatch *patch = loader_malloc - (sizeof(BranchBlockPatch), error_buf, error_buf_size); + BranchBlockPatch *patch = + loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size); if (!patch) { return false; } @@ -5079,8 +5035,7 @@ add_label_patch_to_list(BranchBlock *frame_csp, } static void -apply_label_patch(WASMLoaderContext *ctx, uint8 depth, - uint8 patch_type) +apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type) { BranchBlock *frame_csp = ctx->frame_csp - depth; BranchBlockPatch *node = frame_csp->patch_list; @@ -5153,11 +5108,11 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, for (i = (int32)arity - 1; i >= 0; i--) { cell = (uint8)wasm_value_type_cell_num(types[i]); frame_offset -= cell; - emit_operand(ctx, *(int16*)(frame_offset)); + emit_operand(ctx, *(int16 *)(frame_offset)); } /* Part e */ - dynamic_offset = frame_csp->dynamic_offset - + wasm_get_cell_num(types, arity); + dynamic_offset = + frame_csp->dynamic_offset + wasm_get_cell_num(types, arity); for (i = (int32)arity - 1; i >= 0; i--) { cell = (uint8)wasm_value_type_cell_num(types[i]); dynamic_offset -= cell; @@ -5170,8 +5125,7 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, wasm_loader_emit_ptr(ctx, frame_csp->code_compiled); } else { - if (!add_label_patch_to_list(frame_csp, PATCH_END, - ctx->p_code_compiled, + if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled, error_buf, error_buf_size)) return false; /* label address, to be patched */ @@ -5233,8 +5187,8 @@ wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type, then current block is the function block */ uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0; BranchBlock *cur_block = ctx->frame_csp - depth; - int32 available_stack_cell = (int32) - (ctx->stack_cell_num - cur_block->stack_cell_num); + int32 available_stack_cell = + (int32)(ctx->stack_cell_num - cur_block->stack_cell_num); /* Directly return success if current block is in stack * polymorphic state while stack is empty. */ @@ -5279,12 +5233,13 @@ wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt, uint8 i; for (i = 0; i < pop_cnt; i++) { - if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, error_buf_size)) + if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, + error_buf_size)) return false; } - if (!wasm_loader_push_frame_offset(ctx, type_push, - disable_emit, operand_offset, - error_buf, error_buf_size)) + if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit, + operand_offset, error_buf, + error_buf_size)) return false; return true; @@ -5335,26 +5290,29 @@ wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt, } static bool -wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, - void *value, int16 *offset, - char *error_buf, uint32 error_buf_size) +wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value, + int16 *offset, char *error_buf, + uint32 error_buf_size) { int16 operand_offset = 0; Const *c; for (c = (Const *)ctx->const_buf; - (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) { + (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) { /* TODO: handle v128 type? */ if ((type == c->value_type) - && ((type == VALUE_TYPE_I64 && *(int64*)value == c->value.i64) - || (type == VALUE_TYPE_I32 && *(int32*)value == c->value.i32) + && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64) + || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32) #if WASM_ENABLE_REF_TYPES != 0 - || (type == VALUE_TYPE_FUNCREF && *(int32*)value == c->value.i32) - || (type == VALUE_TYPE_EXTERNREF && *(int32*)value == c->value.i32) + || (type == VALUE_TYPE_FUNCREF + && *(int32 *)value == c->value.i32) + || (type == VALUE_TYPE_EXTERNREF + && *(int32 *)value == c->value.i32) #endif || (type == VALUE_TYPE_F64 && (0 == memcmp(value, &(c->value.f64), sizeof(float64)))) || (type == VALUE_TYPE_F32 - && (0 == memcmp(value, &(c->value.f32), sizeof(float32)))))) { + && (0 + == memcmp(value, &(c->value.f32), sizeof(float32)))))) { operand_offset = c->slot_index; break; } @@ -5365,48 +5323,49 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, } if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) { if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) { - MEM_REALLOC(ctx->const_buf, - ctx->const_buf_size, + MEM_REALLOC(ctx->const_buf, ctx->const_buf_size, ctx->const_buf_size + 4 * sizeof(Const)); ctx->const_buf_size += 4 * sizeof(Const); c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const)); } c->value_type = type; switch (type) { - case VALUE_TYPE_F64: - bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, sizeof(float64)); - ctx->const_cell_num += 2; - /* The const buf will be reversed, we use the second cell */ - /* of the i64/f64 const so the finnal offset is corrent */ - operand_offset ++; - break; - case VALUE_TYPE_I64: - c->value.i64 = *(int64*)value; - ctx->const_cell_num += 2; - operand_offset ++; - break; - case VALUE_TYPE_F32: - bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, sizeof(float32)); - ctx->const_cell_num ++; - break; - case VALUE_TYPE_I32: - c->value.i32 = *(int32*)value; - ctx->const_cell_num ++; - break; + case VALUE_TYPE_F64: + bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, + sizeof(float64)); + ctx->const_cell_num += 2; + /* The const buf will be reversed, we use the second cell */ + /* of the i64/f64 const so the finnal offset is corrent */ + operand_offset++; + break; + case VALUE_TYPE_I64: + c->value.i64 = *(int64 *)value; + ctx->const_cell_num += 2; + operand_offset++; + break; + case VALUE_TYPE_F32: + bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, + sizeof(float32)); + ctx->const_cell_num++; + break; + case VALUE_TYPE_I32: + c->value.i32 = *(int32 *)value; + ctx->const_cell_num++; + break; #if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_EXTERNREF: - case VALUE_TYPE_FUNCREF: - c->value.i32 = *(int32*)value; - ctx->const_cell_num ++; - break; + case VALUE_TYPE_EXTERNREF: + case VALUE_TYPE_FUNCREF: + c->value.i32 = *(int32 *)value; + ctx->const_cell_num++; + break; #endif - default: - break; + default: + break; } c->slot_index = operand_offset; - ctx->num_const ++; - LOG_OP("#### new const [%d]: %ld\n", - ctx->num_const, (int64)c->value.i64); + ctx->num_const++; + LOG_OP("#### new const [%d]: %ld\n", ctx->num_const, + (int64)c->value.i64); } /* use negetive index for const */ operand_offset = -(operand_offset + 1); @@ -5433,77 +5392,85 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, only push the frame_offset stack, no emit */ -#define TEMPLATE_PUSH(Type) do { \ - if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type,\ - disable_emit, operand_offset,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define TEMPLATE_POP(Type) do { \ - if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_OFFSET_TYPE(type) do { \ - if (!(wasm_loader_push_frame_offset(loader_ctx, type, \ - disable_emit, operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_OFFSET_TYPE(type) do { \ - if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 1, \ - type_push, type_pop, \ - disable_emit, operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define TEMPLATE_PUSH(Type) \ + do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define TEMPLATE_POP(Type) \ + do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_OFFSET_TYPE(type) \ + do { \ + if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \ + operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_OFFSET_TYPE(type) \ + do { \ + if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref_offset( \ + loader_ctx, 1, type_push, type_pop, disable_emit, \ + operand_offset, error_buf, error_buf_size))) \ + goto fail; \ + } while (0) /* type of POPs should be the same */ -#define POP2_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 2, \ - type_push, type_pop, \ - disable_emit, operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define POP2_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref_offset( \ + loader_ctx, 2, type_push, type_pop, disable_emit, \ + operand_offset, error_buf, error_buf_size))) \ + goto fail; \ + } while (0) #else /* WASM_ENABLE_FAST_INTERP */ -#define TEMPLATE_PUSH(Type) do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define TEMPLATE_POP(Type) do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, \ - type_push, type_pop, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define TEMPLATE_PUSH(Type) \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define TEMPLATE_POP(Type) \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \ + type_pop, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) /* type of POPs should be the same */ -#define POP2_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, \ - type_push, type_pop, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define POP2_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \ + type_pop, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) #endif /* WASM_ENABLE_FAST_INTERP */ #define PUSH_I32() TEMPLATE_PUSH(I32) @@ -5525,19 +5492,18 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, #if WASM_ENABLE_FAST_INTERP != 0 static bool -reserve_block_ret(WASMLoaderContext *loader_ctx, - uint8 opcode, bool disable_emit, - char *error_buf, uint32 error_buf_size) +reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, + bool disable_emit, char *error_buf, uint32 error_buf_size) { int16 operand_offset = 0; - BranchBlock *block = (opcode == WASM_OP_ELSE) ? - loader_ctx->frame_csp - 1 : loader_ctx->frame_csp; + BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1 + : loader_ctx->frame_csp; BlockType *block_type = &block->block_type; uint8 *return_types = NULL; uint32 return_count = 0, value_count = 0, total_cel_num = 0; int32 i = 0; - int16 dynamic_offset, dynamic_offset_org, - *frame_offset = NULL, *frame_offset_org = NULL; + int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL, + *frame_offset_org = NULL; return_count = block_type_get_result_types(block_type, &return_types); @@ -5549,7 +5515,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, /* insert op_copy before else opcode */ if (opcode == WASM_OP_ELSE) skip_label(); - emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP : EXT_OP_COPY_STACK_TOP_I64); + emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP + : EXT_OP_COPY_STACK_TOP_I64); emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell)); emit_operand(loader_ctx, block->dynamic_offset); @@ -5578,8 +5545,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, */ frame_offset = frame_offset_org = loader_ctx->frame_offset; dynamic_offset = dynamic_offset_org = - block->dynamic_offset - + wasm_get_cell_num(return_types, return_count); + block->dynamic_offset + wasm_get_cell_num(return_types, return_count); /* First traversal to get the count of values needed to be copied. */ for (i = (int32)return_count - 1; i >= 0; i--) { @@ -5598,9 +5564,9 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 *emit_data = NULL, *cells = NULL; int16 *src_offsets = NULL; uint16 *dst_offsets = NULL; - uint64 size = (uint64)value_count * (sizeof(*cells) - + sizeof(*src_offsets) - + sizeof(*dst_offsets)); + uint64 size = + (uint64)value_count + * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets)); /* Allocate memory for the emit data */ if (!(emit_data = loader_malloc(size, error_buf, error_buf_size))) @@ -5619,7 +5585,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, /* Part b) */ emit_uint32(loader_ctx, total_cel_num); - /* Second traversal to get each value's cell num, src offset and dst offset. */ + /* Second traversal to get each value's cell num, src offset and dst + * offset. */ frame_offset = frame_offset_org; dynamic_offset = dynamic_offset_org; for (i = (int32)return_count - 1, j = 0; i >= 0; i--) { @@ -5671,83 +5638,86 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, } #endif /* WASM_ENABLE_FAST_INTERP */ -#define RESERVE_BLOCK_RET() do { \ - if (!reserve_block_ret(loader_ctx, opcode, disable_emit, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_TYPE(type) do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_TYPE(type) do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define PUSH_CSP(label_type, block_type, _start_addr) do { \ - if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \ - _start_addr, error_buf, \ - error_buf_size)) \ - goto fail; \ - } while (0) - -#define POP_CSP() do { \ - if (!wasm_loader_pop_frame_csp(loader_ctx, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \ - read_leb_uint32(p, p_end, local_idx); \ - if (local_idx >= param_count + local_count) { \ - set_error_buf(error_buf, error_buf_size, \ - "unknown local"); \ - goto fail; \ - } \ - local_type = local_idx < param_count \ - ? param_types[local_idx] \ - : local_types[local_idx - param_count]; \ - local_offset = local_offsets[local_idx]; \ - } while (0) - -#define CHECK_BR(depth) do { \ - if (!wasm_loader_check_br(loader_ctx, depth, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) +#define RESERVE_BLOCK_RET() \ + do { \ + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_TYPE(type) \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_TYPE(type) \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_CSP(label_type, block_type, _start_addr) \ + do { \ + if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \ + _start_addr, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_CSP() \ + do { \ + if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \ + do { \ + read_leb_uint32(p, p_end, local_idx); \ + if (local_idx >= param_count + local_count) { \ + set_error_buf(error_buf, error_buf_size, "unknown local"); \ + goto fail; \ + } \ + local_type = local_idx < param_count \ + ? param_types[local_idx] \ + : local_types[local_idx - param_count]; \ + local_offset = local_offsets[local_idx]; \ + } while (0) + +#define CHECK_BR(depth) \ + do { \ + if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) static bool -check_memory(WASMModule *module, - char *error_buf, uint32 error_buf_size) +check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size) { - if (module->memory_count == 0 - && module->import_memory_count == 0) { + if (module->memory_count == 0 && module->import_memory_count == 0) { set_error_buf(error_buf, error_buf_size, "unknown memory"); return false; } return true; } -#define CHECK_MEMORY() do { \ - if (!check_memory(module, error_buf, error_buf_size)) \ - goto fail; \ - } while (0) +#define CHECK_MEMORY() \ + do { \ + if (!check_memory(module, error_buf, error_buf_size)) \ + goto fail; \ + } while (0) static bool -check_memory_access_align(uint8 opcode, uint32 align, - char *error_buf, uint32 error_buf_size) +check_memory_access_align(uint8 opcode, uint32 align, char *error_buf, + uint32 error_buf_size) { uint8 mem_access_aligns[] = { 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */ 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */ }; - bh_assert(opcode >= WASM_OP_I32_LOAD - && opcode <= WASM_OP_I64_STORE32); + bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32); if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) { set_error_buf(error_buf, error_buf_size, "alignment must not be larger than natural"); @@ -5759,20 +5729,20 @@ check_memory_access_align(uint8 opcode, uint32 align, #if WASM_ENABLE_SIMD != 0 #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) static bool -check_simd_memory_access_align(uint8 opcode, uint32 align, - char *error_buf, uint32 error_buf_size) +check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf, + uint32 error_buf_size) { uint8 mem_access_aligns[] = { - 4, /* load */ - 3, 3, 3, 3, 3, 3, /* load and extend */ - 0, 1, 2, 3, /* load and splat */ - 4, /* store */ + 4, /* load */ + 3, 3, 3, 3, 3, 3, /* load and extend */ + 0, 1, 2, 3, /* load and splat */ + 4, /* store */ }; uint8 mem_access_aligns_load_lane[] = { 0, 1, 2, 3, /* load lane */ 0, 1, 2, 3, /* store lane */ - 2, 3 /* store zero */ + 2, 3 /* store zero */ }; if (!((opcode <= SIMD_v128_store) @@ -5797,8 +5767,8 @@ check_simd_memory_access_align(uint8 opcode, uint32 align, } static bool -check_simd_access_lane(uint8 opcode, uint8 lane, - char *error_buf, uint32 error_buf_size) +check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf, + uint32 error_buf_size) { switch (opcode) { case SIMD_i8x16_extract_lane_s: @@ -5860,9 +5830,7 @@ check_simd_access_lane(uint8 opcode, uint8 lane, } static bool -check_simd_shuffle_mask(V128 mask, - char *error_buf, - uint32 error_buf_size) +check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size) { uint8 i; for (i = 0; i != 16; ++i) { @@ -5878,10 +5846,10 @@ check_simd_shuffle_mask(V128 mask, #if WASM_ENABLE_SHARED_MEMORY != 0 static bool -check_memory_align_equal(uint8 opcode, uint32 align, - char *error_buf, uint32 error_buf_size) +check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf, + uint32 error_buf_size) { - uint8 wait_notify_aligns[] = {2, 2, 3}; + uint8 wait_notify_aligns[] = { 2, 2, 3 }; uint8 mem_access_aligns[] = { 2, 3, 0, 1, 0, 1, 2, }; @@ -5918,10 +5886,10 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint16 cell_num; if (loader_ctx->csp_num < depth + 1) { - set_error_buf(error_buf, error_buf_size, - "unknown label, " - "unexpected end of section or function"); - return false; + set_error_buf(error_buf, error_buf_size, + "unknown label, " + "unexpected end of section or function"); + return false; } cur_block = loader_ctx->frame_csp - 1; @@ -5941,7 +5909,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, * and then re-push the values to make the stack top values * match block type. */ if (cur_block->is_stack_polymorphic) { - for (i = (int32)arity -1; i >= 0; i--) { + for (i = (int32)arity - 1; i >= 0; i--) { #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(types[i]); #endif @@ -5958,13 +5926,12 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, return true; } - available_stack_cell = (int32) - (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + available_stack_cell = + (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num); /* Check stack top values match target block type */ - for (i = (int32)arity -1; i >= 0; i--) { - if (!check_stack_top_values(frame_ref, available_stack_cell, - types[i], + for (i = (int32)arity - 1; i >= 0; i--) { + if (!check_stack_top_values(frame_ref, available_stack_cell, types[i], error_buf, error_buf_size)) return false; cell_num = wasm_value_type_cell_num(types[i]); @@ -5979,8 +5946,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, } static BranchBlock * -check_branch_block(WASMLoaderContext *loader_ctx, - uint8 **p_buf, uint8 *buf_end, +check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end, char *error_buf, uint32 error_buf_size) { uint8 *p = *p_buf, *p_end = buf_end; @@ -6010,19 +5976,18 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, int32 available_stack_cell, return_cell_num, i; uint8 *frame_ref = NULL; - available_stack_cell = (int32) - (loader_ctx->stack_cell_num - - block->stack_cell_num); + available_stack_cell = + (int32)(loader_ctx->stack_cell_num - block->stack_cell_num); return_count = block_type_get_result_types(block_type, &return_types); - return_cell_num = return_count > 0 ? - wasm_get_cell_num(return_types, return_count) : 0; + return_cell_num = + return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0; /* If the stack is in polymorphic state, just clear the stack * and then re-push the values to make the stack top values * match block type. */ if (block->is_stack_polymorphic) { - for (i = (int32)return_count -1; i >= 0; i--) { + for (i = (int32)return_count - 1; i >= 0; i--) { #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(return_types[i]); #endif @@ -6031,8 +5996,9 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, /* Check stack is empty */ if (loader_ctx->stack_cell_num != block->stack_cell_num) { - set_error_buf(error_buf, error_buf_size, - "type mismatch: stack size does not match block type"); + set_error_buf( + error_buf, error_buf_size, + "type mismatch: stack size does not match block type"); goto fail; } @@ -6056,10 +6022,9 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, /* Check stack values match return types */ frame_ref = loader_ctx->frame_ref; - for (i = (int32)return_count -1; i >= 0; i--) { + for (i = (int32)return_count - 1; i >= 0; i--) { if (!check_stack_top_values(frame_ref, available_stack_cell, - return_types[i], - error_buf, error_buf_size)) + return_types[i], error_buf, error_buf_size)) return false; frame_ref -= wasm_value_type_cell_num(return_types[i]); available_stack_cell -= wasm_value_type_cell_num(return_types[i]); @@ -6084,7 +6049,7 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, */ static bool copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, - char* error_buf, uint32 error_buf_size) + char *error_buf, uint32 error_buf_size) { int16 *frame_offset = NULL; uint8 *cells = NULL, cell; @@ -6099,8 +6064,7 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, bool disable_emit = false; int16 operand_offset = 0; - uint64 size = (uint64)param_count * (sizeof(*cells) - + sizeof(*src_offsets)); + uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets)); /* For if block, we also need copy the condition operand offset. */ if (is_if_block) @@ -6136,9 +6100,8 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, /* Part a) */ emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count); /* Part b) */ - emit_uint32(loader_ctx, is_if_block ? - wasm_type->param_cell_num + 1 : - wasm_type->param_cell_num); + emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1 + : wasm_type->param_cell_num); /* Part c) */ for (i = 0; i < param_count; i++) emit_byte(loader_ctx, cells[i]); @@ -6170,44 +6133,47 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, /* reset the stack to the state of before entering the last block */ #if WASM_ENABLE_FAST_INTERP != 0 -#define RESET_STACK() do { \ - loader_ctx->stack_cell_num = \ - (loader_ctx->frame_csp - 1)->stack_cell_num; \ - loader_ctx->frame_ref = \ - loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ - loader_ctx->frame_offset = \ - loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \ -} while (0) +#define RESET_STACK() \ + do { \ + loader_ctx->stack_cell_num = \ + (loader_ctx->frame_csp - 1)->stack_cell_num; \ + loader_ctx->frame_ref = \ + loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ + loader_ctx->frame_offset = \ + loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \ + } while (0) #else -#define RESET_STACK() do { \ - loader_ctx->stack_cell_num = \ - (loader_ctx->frame_csp - 1)->stack_cell_num; \ - loader_ctx->frame_ref = \ - loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ -} while (0) +#define RESET_STACK() \ + do { \ + loader_ctx->stack_cell_num = \ + (loader_ctx->frame_csp - 1)->stack_cell_num; \ + loader_ctx->frame_ref = \ + loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ + } while (0) #endif /* set current block's stack polymorphic state */ -#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) do { \ - BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ - cur_block->is_stack_polymorphic = flag; \ -} while (0) +#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \ + do { \ + BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ + cur_block->is_stack_polymorphic = flag; \ + } while (0) #define BLOCK_HAS_PARAM(block_type) \ (!block_type.is_value_type && block_type.u.type->param_count > 0) -#define PRESERVE_LOCAL_FOR_BLOCK() do { \ - if (!(preserve_local_for_block(loader_ctx, opcode, \ - error_buf, error_buf_size))) { \ - goto fail; \ - } \ -} while (0) +#define PRESERVE_LOCAL_FOR_BLOCK() \ + do { \ + if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \ + error_buf_size))) { \ + goto fail; \ + } \ + } while (0) #if WASM_ENABLE_REF_TYPES != 0 static bool -get_table_elem_type(const WASMModule *module, - uint32 table_idx, uint8 *p_elem_type, - char *error_buf, uint32 error_buf_size) +get_table_elem_type(const WASMModule *module, uint32 table_idx, + uint8 *p_elem_type, char *error_buf, uint32 error_buf_size) { if (!check_table_index(module, table_idx, error_buf, error_buf_size)) { return false; @@ -6217,16 +6183,17 @@ get_table_elem_type(const WASMModule *module, if (table_idx < module->import_table_count) *p_elem_type = module->import_tables[table_idx].u.table.elem_type; else - *p_elem_type = module->tables[module->import_table_count - + table_idx].elem_type; + *p_elem_type = + module->tables[module->import_table_count + table_idx] + .elem_type; } return true; } static bool -get_table_seg_elem_type(const WASMModule *module, - uint32 table_seg_idx, uint8 *p_elem_type, - char *error_buf, uint32 error_buf_size) +get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx, + uint8 *p_elem_type, char *error_buf, + uint32 error_buf_size) { if (table_seg_idx >= module->table_seg_count) { #if WASM_ENABLE_REF_TYPES != 0 @@ -6238,8 +6205,7 @@ get_table_seg_elem_type(const WASMModule *module, "unknown elem segment %u", table_seg_idx); } #else - set_error_buf(error_buf, error_buf_size, - "unknown table segment"); + set_error_buf(error_buf, error_buf_size, "unknown table segment"); #endif return false; } @@ -6252,9 +6218,9 @@ get_table_seg_elem_type(const WASMModule *module, #endif static bool -wasm_loader_prepare_bytecode(WASMModule *module, - WASMFunction *func, uint32 cur_func_idx, - char *error_buf, uint32 error_buf_size) +wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, + uint32 cur_func_idx, char *error_buf, + uint32 error_buf_size) { uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org; uint32 param_count, local_count, global_count; @@ -6278,9 +6244,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, float64 f64; LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n", - func->param_cell_num, - func->local_cell_num, - func->ret_cell_num); + func->param_cell_num, func->local_cell_num, func->ret_cell_num); #endif global_count = module->import_global_count + module->global_count; @@ -6296,8 +6260,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, local_offsets = func->local_offsets; if (!(loader_ctx = wasm_loader_ctx_init(func))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); goto fail; } @@ -6305,8 +6268,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, re_scan: if (loader_ctx->code_compiled_size > 0) { if (!wasm_loader_ctx_reinit(loader_ctx)) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); goto fail; } p = func->code; @@ -6348,7 +6310,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 PRESERVE_LOCAL_FOR_BLOCK(); #endif -handle_op_block_and_loop: + handle_op_block_and_loop: { uint8 value_type; BlockType block_type; @@ -6373,12 +6335,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, } block_type.is_value_type = false; block_type.u.type = module->types[type_index]; -#if WASM_ENABLE_FAST_INTERP == 0 \ - && WASM_ENABLE_WAMR_COMPILER == 0 \ +#if WASM_ENABLE_FAST_INTERP == 0 && WASM_ENABLE_WAMR_COMPILER == 0 \ && WASM_ENABLE_JIT == 0 /* If block use type index as block type, change the opcode - * to new extended opcode so that interpreter can resolve the - * block quickly. + * to new extended opcode so that interpreter can resolve + * the block quickly. */ #if WASM_ENABLE_DEBUG_INTERP != 0 record_fast_op(module, p - 2, *(p - 2)); @@ -6391,10 +6352,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, if (BLOCK_HAS_PARAM(block_type)) { WASMType *wasm_type = block_type.u.type; for (i = 0; i < block_type.u.type->param_count; i++) - POP_TYPE(wasm_type->types[wasm_type->param_count - i - 1]); + POP_TYPE( + wasm_type->types[wasm_type->param_count - i - 1]); } - PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), block_type, p); + PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), + block_type, p); /* Pass parameters to block */ if (BLOCK_HAS_PARAM(block_type)) { @@ -6410,14 +6373,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, skip_label(); if (BLOCK_HAS_PARAM(block_type)) { /* Make sure params are in dynamic space */ - if (!copy_params_to_dynamic_space(loader_ctx, - false, - error_buf, - error_buf_size)) + if (!copy_params_to_dynamic_space( + loader_ctx, false, error_buf, error_buf_size)) goto fail; } (loader_ctx->frame_csp - 1)->code_compiled = - loader_ctx->p_code_compiled; + loader_ctx->p_code_compiled; } else if (opcode == WASM_OP_IF) { /* If block has parameters, we should make sure they are in @@ -6427,8 +6388,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, * (func (export "params-id") (param i32) (result i32) * (i32.const 1) * (i32.const 2) - * (if (param i32 i32) (result i32 i32) (local.get 0) (then)) - * (i32.add) + * (if (param i32 i32) (result i32 i32) (local.get 0) + * (then)) (i32.add) * ) * * So we should emit a copy instruction before the if. @@ -6446,10 +6407,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, /* skip the if label */ skip_label(); /* Emit a copy instruction */ - if (!copy_params_to_dynamic_space(loader_ctx, - true, - error_buf, - error_buf_size)) + if (!copy_params_to_dynamic_space( + loader_ctx, true, error_buf, error_buf_size)) goto fail; /* Emit the if instruction */ @@ -6457,16 +6416,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, /* Emit the new condition operand offset */ POP_OFFSET_TYPE(VALUE_TYPE_I32); - /* Save top param_count values of frame_offset stack, so that - * we can recover it before executing else branch */ - size = sizeof(int16) * - (uint64)block_type.u.type->param_cell_num; - if (!(block->param_frame_offsets = - loader_malloc(size, error_buf, error_buf_size))) + /* Save top param_count values of frame_offset stack, so + * that we can recover it before executing else branch + */ + size = sizeof(int16) + * (uint64)block_type.u.type->param_cell_num; + if (!(block->param_frame_offsets = loader_malloc( + size, error_buf, error_buf_size))) goto fail; - bh_memcpy_s(block->param_frame_offsets, - (uint32)size, - loader_ctx->frame_offset - size/sizeof(int16), + bh_memcpy_s(block->param_frame_offsets, (uint32)size, + loader_ctx->frame_offset + - size / sizeof(int16), (uint32)size); } @@ -6482,9 +6442,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, BlockType block_type = (loader_ctx->frame_csp - 1)->block_type; if (loader_ctx->csp_num < 2 - || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF) { - set_error_buf(error_buf, error_buf_size, - "opcode else found without matched opcode if"); + || (loader_ctx->frame_csp - 1)->label_type + != LABEL_TYPE_IF) { + set_error_buf( + error_buf, error_buf_size, + "opcode else found without matched opcode if"); goto fail; } @@ -6496,7 +6458,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, (loader_ctx->frame_csp - 1)->else_addr = p - 1; #if WASM_ENABLE_FAST_INTERP != 0 - /* if the result of if branch is in local or const area, add a copy op */ + /* if the result of if branch is in local or const area, add a + * copy op */ RESERVE_BLOCK_RET(); emit_empty_label_addr_and_frame_ip(PATCH_END); @@ -6516,11 +6479,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, if (BLOCK_HAS_PARAM((block_type))) { uint32 size; BranchBlock *block = loader_ctx->frame_csp - 1; - size = sizeof(int16) * - block_type.u.type->param_cell_num; + size = sizeof(int16) * block_type.u.type->param_cell_num; bh_memcpy_s(loader_ctx->frame_offset, size, block->param_frame_offsets, size); - loader_ctx->frame_offset += (size/sizeof(int16)); + loader_ctx->frame_offset += (size / sizeof(int16)); } #endif @@ -6532,11 +6494,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, BranchBlock *cur_block = loader_ctx->frame_csp - 1; /* check whether block stack matches its result type */ - if (!check_block_stack(loader_ctx, cur_block, - error_buf, error_buf_size)) + if (!check_block_stack(loader_ctx, cur_block, error_buf, + error_buf_size)) goto fail; - /* if no else branch, and return types do not match param types, fail */ + /* if no else branch, and return types do not match param types, + * fail */ if (cur_block->label_type == LABEL_TYPE_IF && !cur_block->else_addr) { uint32 param_count = 0, ret_count = 0; @@ -6555,7 +6518,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, ret_types = block_type->u.type->types + param_count; } if (param_count != ret_count - || (param_count && memcmp(param_types, ret_types, param_count))) { + || (param_count + && memcmp(param_types, ret_types, param_count))) { set_error_buf(error_buf, error_buf_size, "type mismatch: else branch missing"); goto fail; @@ -6601,8 +6565,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, case WASM_OP_BR: { - if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, - error_buf, error_buf_size))) + if (!(frame_csp_tmp = check_branch_block( + loader_ctx, &p, p_end, error_buf, error_buf_size))) goto fail; RESET_STACK(); @@ -6614,8 +6578,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, { POP_I32(); - if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, - error_buf, error_buf_size))) + if (!(frame_csp_tmp = check_branch_block( + loader_ctx, &p, p_end, error_buf, error_buf_size))) goto fail; break; @@ -6634,32 +6598,34 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i <= count; i++) { if (!(frame_csp_tmp = - check_branch_block(loader_ctx, &p, p_end, - error_buf, error_buf_size))) + check_branch_block(loader_ctx, &p, p_end, + error_buf, error_buf_size))) goto fail; if (i == 0) { if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP) - ret_count = - block_type_get_result_types(&frame_csp_tmp->block_type, - &ret_types); + ret_count = block_type_get_result_types( + &frame_csp_tmp->block_type, &ret_types); } else { uint8 *tmp_ret_types = NULL; uint32 tmp_ret_count = 0; - /* Check whether all table items have the same return type */ + /* Check whether all table items have the same return + * type */ if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP) - tmp_ret_count = - block_type_get_result_types(&frame_csp_tmp->block_type, - &tmp_ret_types); + tmp_ret_count = block_type_get_result_types( + &frame_csp_tmp->block_type, &tmp_ret_types); if (ret_count != tmp_ret_count || (ret_count - && 0 != memcmp(ret_types, tmp_ret_types, ret_count))) { - set_error_buf(error_buf, error_buf_size, - "type mismatch: br_table targets must " - "all use same result type"); + && 0 + != memcmp(ret_types, tmp_ret_types, + ret_count))) { + set_error_buf( + error_buf, error_buf_size, + "type mismatch: br_table targets must " + "all use same result type"); goto fail; } } @@ -6674,7 +6640,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, { int32 idx; uint8 ret_type; - for (idx = (int32)func->func_type->result_count - 1; idx >= 0; idx--) { + for (idx = (int32)func->func_type->result_count - 1; idx >= 0; + idx--) { ret_type = *(func->func_type->types + func->func_type->param_count + idx); POP_TYPE(ret_type); @@ -6710,13 +6677,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, } if (func_idx < module->import_function_count) - func_type = module->import_functions[func_idx].u.function.func_type; - else func_type = - module->functions[func_idx - module->import_function_count]->func_type; + module->import_functions[func_idx].u.function.func_type; + else + func_type = module + ->functions[func_idx + - module->import_function_count] + ->func_type; if (func_type->param_count > 0) { - for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) { + for (idx = (int32)(func_type->param_count - 1); idx >= 0; + idx--) { POP_TYPE(func_type->types[idx]); #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(func_type->types[idx]); @@ -6730,31 +6701,34 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i < func_type->result_count; i++) { PUSH_TYPE(func_type->types[func_type->param_count + i]); #if WASM_ENABLE_FAST_INTERP != 0 - /* Here we emit each return value's dynamic_offset. But in fact - * these offsets are continuous, so interpreter only need to get - * the first return value's offset. + /* Here we emit each return value's dynamic_offset. But + * in fact these offsets are continuous, so interpreter + * only need to get the first return value's offset. */ - PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]); + PUSH_OFFSET_TYPE( + func_type->types[func_type->param_count + i]); #endif } #if WASM_ENABLE_TAIL_CALL != 0 } else { uint8 type; - if (func_type->result_count != func->func_type->result_count) { - set_error_buf_v(error_buf, error_buf_size, - "%s%u%s", "type mismatch: expect ", + if (func_type->result_count + != func->func_type->result_count) { + set_error_buf_v(error_buf, error_buf_size, "%s%u%s", + "type mismatch: expect ", func->func_type->result_count, " return values but got other"); goto fail; } for (i = 0; i < func_type->result_count; i++) { - type = func->func_type->types[func->func_type->param_count + i]; - if (func_type->types[func_type->param_count + i] != type) { - set_error_buf_v(error_buf, error_buf_size, - "%s%s%s", "type mismatch: expect ", - type2str(type), - " but got other"); + type = func->func_type + ->types[func->func_type->param_count + i]; + if (func_type->types[func_type->param_count + i] + != type) { + set_error_buf_v(error_buf, error_buf_size, "%s%s%s", + "type mismatch: expect ", + type2str(type), " but got other"); goto fail; } } @@ -6810,15 +6784,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_I32(); if (type_idx >= module->type_count) { - set_error_buf(error_buf, error_buf_size, - "unknown type"); + set_error_buf(error_buf, error_buf_size, "unknown type"); goto fail; } func_type = module->types[type_idx]; if (func_type->param_count > 0) { - for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) { + for (idx = (int32)(func_type->param_count - 1); idx >= 0; + idx--) { POP_TYPE(func_type->types[idx]); #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(func_type->types[idx]); @@ -6832,27 +6806,30 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i < func_type->result_count; i++) { PUSH_TYPE(func_type->types[func_type->param_count + i]); #if WASM_ENABLE_FAST_INTERP != 0 - PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]); + PUSH_OFFSET_TYPE( + func_type->types[func_type->param_count + i]); #endif } #if WASM_ENABLE_TAIL_CALL != 0 } else { uint8 type; - if (func_type->result_count != func->func_type->result_count) { - set_error_buf_v(error_buf, error_buf_size, - "%s%u%s", "type mismatch: expect ", + if (func_type->result_count + != func->func_type->result_count) { + set_error_buf_v(error_buf, error_buf_size, "%s%u%s", + "type mismatch: expect ", func->func_type->result_count, " return values but got other"); goto fail; } for (i = 0; i < func_type->result_count; i++) { - type = func->func_type->types[func->func_type->param_count + i]; - if (func_type->types[func_type->param_count + i] != type) { - set_error_buf_v(error_buf, error_buf_size, - "%s%s%s", "type mismatch: expect ", - type2str(type), - " but got other"); + type = func->func_type + ->types[func->func_type->param_count + i]; + if (func_type->types[func_type->param_count + i] + != type) { + set_error_buf_v(error_buf, error_buf_size, "%s%s%s", + "type mismatch: expect ", + type2str(type), " but got other"); goto fail; } } @@ -6868,8 +6845,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, case WASM_OP_DROP_64: { BranchBlock *cur_block = loader_ctx->frame_csp - 1; - int32 available_stack_cell = (int32) - (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + int32 available_stack_cell = + (int32)(loader_ctx->stack_cell_num + - cur_block->stack_cell_num); if (available_stack_cell <= 0 && !cur_block->is_stack_polymorphic) { @@ -6886,15 +6864,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, || *(loader_ctx->frame_ref - 1) == REF_FUNCREF || *(loader_ctx->frame_ref - 1) == REF_EXTERNREF #endif - ) { + ) { loader_ctx->frame_ref--; loader_ctx->stack_cell_num--; #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); loader_ctx->frame_offset--; - if (*(loader_ctx->frame_offset) > - loader_ctx->start_dynamic_offset) - loader_ctx->dynamic_offset --; + if (*(loader_ctx->frame_offset) + > loader_ctx->start_dynamic_offset) + loader_ctx->dynamic_offset--; #endif } else if (*(loader_ctx->frame_ref - 1) == REF_I64_1 @@ -6907,8 +6885,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); loader_ctx->frame_offset -= 2; - if (*(loader_ctx->frame_offset) > - loader_ctx->start_dynamic_offset) + if (*(loader_ctx->frame_offset) + > loader_ctx->start_dynamic_offset) loader_ctx->dynamic_offset -= 2; #endif } @@ -6943,8 +6921,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_I32(); - available_stack_cell = (int32) - (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + available_stack_cell = (int32)(loader_ctx->stack_cell_num + - cur_block->stack_cell_num); if (available_stack_cell <= 0 && !cur_block->is_stack_polymorphic) { @@ -6972,21 +6950,24 @@ wasm_loader_prepare_bytecode(WASMModule *module, loader_ctx->p_code_compiled - 2; #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 - *(void**)(p_code_compiled_tmp - sizeof(void*)) = - handle_table[opcode_tmp]; + *(void **)(p_code_compiled_tmp + - sizeof(void *)) = + handle_table[opcode_tmp]; #else - int32 offset = (int32) - ((uint8*)handle_table[opcode_tmp] - - (uint8*)handle_table[0]); - if (!(offset >= INT16_MIN && offset < INT16_MAX)) { + int32 offset = + (int32)((uint8 *)handle_table[opcode_tmp] + - (uint8 *)handle_table[0]); + if (!(offset >= INT16_MIN + && offset < INT16_MAX)) { set_error_buf(error_buf, error_buf_size, - "pre-compiled label offset out of range"); + "pre-compiled label offset " + "out of range"); goto fail; } - *(int16*)(p_code_compiled_tmp - sizeof(int16)) = - (int16)offset; + *(int16 *)(p_code_compiled_tmp + - sizeof(int16)) = (int16)offset; #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 *(p_code_compiled_tmp - 1) = opcode_tmp; #else @@ -7002,7 +6983,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, break; #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */ #endif /* WASM_ENABLE_SIMD != 0 */ - default: { + default: + { set_error_buf(error_buf, error_buf_size, "type mismatch"); goto fail; @@ -7072,25 +7054,25 @@ wasm_loader_prepare_bytecode(WASMModule *module, } else { if (ref_type == VALUE_TYPE_F64 - || ref_type == VALUE_TYPE_I64) + || ref_type == VALUE_TYPE_I64) opcode_tmp = WASM_OP_SELECT_64; #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 - *(void**)(p_code_compiled_tmp - sizeof(void*)) = - handle_table[opcode_tmp]; + *(void **)(p_code_compiled_tmp - sizeof(void *)) = + handle_table[opcode_tmp]; #else - int32 offset = (int32) - ((uint8*)handle_table[opcode_tmp] - - (uint8*)handle_table[0]); + int32 offset = (int32)((uint8 *)handle_table[opcode_tmp] + - (uint8 *)handle_table[0]); if (!(offset >= INT16_MIN && offset < INT16_MAX)) { - set_error_buf(error_buf, error_buf_size, - "pre-compiled label offset out of range"); + set_error_buf( + error_buf, error_buf_size, + "pre-compiled label offset out of range"); goto fail; } - *(int16*)(p_code_compiled_tmp - sizeof(int16)) = - (int16)offset; + *(int16 *)(p_code_compiled_tmp - sizeof(int16)) = + (int16)offset; #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 *(p_code_compiled_tmp - 1) = opcode_tmp; #else @@ -7181,21 +7163,20 @@ wasm_loader_prepare_bytecode(WASMModule *module, } #if WASM_ENABLE_FAST_INTERP != 0 - if (!wasm_loader_pop_frame_ref_offset( - loader_ctx, VALUE_TYPE_FUNCREF, - error_buf, error_buf_size) + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, + VALUE_TYPE_FUNCREF, + error_buf, error_buf_size) && !wasm_loader_pop_frame_ref_offset( - loader_ctx, VALUE_TYPE_EXTERNREF, - error_buf, error_buf_size)) { + loader_ctx, VALUE_TYPE_EXTERNREF, error_buf, + error_buf_size)) { goto fail; } #else - if (!wasm_loader_pop_frame_ref( - loader_ctx, VALUE_TYPE_FUNCREF, - error_buf, error_buf_size) - && !wasm_loader_pop_frame_ref( - loader_ctx, VALUE_TYPE_EXTERNREF, - error_buf, error_buf_size)) { + if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, + error_buf, error_buf_size) + && !wasm_loader_pop_frame_ref(loader_ctx, + VALUE_TYPE_EXTERNREF, + error_buf, error_buf_size)) { goto fail; } #endif @@ -7224,8 +7205,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i < module->table_seg_count; i++, table_seg++) { if (table_seg->elem_type == VALUE_TYPE_FUNCREF && wasm_elem_is_declarative(table_seg->mode)) { - for (j =0; j < table_seg->function_count; j++) { - if (table_seg->func_indexes[j] == cur_func_idx) { + for (j = 0; j < table_seg->function_count; j++) { + if (table_seg->func_indexes[j] + == cur_func_idx) { func_declared = true; break; } @@ -7297,22 +7279,24 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_TYPE(local_type); #if WASM_ENABLE_FAST_INTERP != 0 - if (!(preserve_referenced_local(loader_ctx, opcode, local_offset, - local_type, &preserve_local, - error_buf, error_buf_size))) + if (!(preserve_referenced_local( + loader_ctx, opcode, local_offset, local_type, + &preserve_local, error_buf, error_buf_size))) goto fail; if (local_offset < 256) { skip_label(); if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) { if (loader_ctx->p_code_compiled) - STORE_U16(loader_ctx->p_code_compiled - 2, local_offset); - loader_ctx->frame_offset --; - loader_ctx->dynamic_offset --; + STORE_U16(loader_ctx->p_code_compiled - 2, + local_offset); + loader_ctx->frame_offset--; + loader_ctx->dynamic_offset--; } else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) { if (loader_ctx->p_code_compiled) - STORE_U16(loader_ctx->p_code_compiled - 2, local_offset); + STORE_U16(loader_ctx->p_code_compiled - 2, + local_offset); loader_ctx->frame_offset -= 2; loader_ctx->dynamic_offset -= 2; } @@ -7328,7 +7312,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_OFFSET_TYPE(local_type); } } - else { /* local index larger than 255, reserve leb */ + else { /* local index larger than 255, reserve leb */ emit_uint32(loader_ctx, local_idx); POP_OFFSET_TYPE(local_type); } @@ -7369,8 +7353,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, GET_LOCAL_INDEX_TYPE_AND_OFFSET(); #if WASM_ENABLE_FAST_INTERP != 0 /* If the stack is in polymorphic state, do fake pop and push on - offset stack to keep the depth of offset stack to be the same - with ref stack */ + offset stack to keep the depth of offset stack to be the + same with ref stack */ BranchBlock *cur_block = loader_ctx->frame_csp - 1; if (cur_block->is_stack_polymorphic) { POP_OFFSET_TYPE(local_type); @@ -7381,9 +7365,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, PUSH_TYPE(local_type); #if WASM_ENABLE_FAST_INTERP != 0 - if (!(preserve_referenced_local(loader_ctx, opcode, local_offset, - local_type, &preserve_local, - error_buf, error_buf_size))) + if (!(preserve_referenced_local( + loader_ctx, opcode, local_offset, local_type, + &preserve_local, error_buf, error_buf_size))) goto fail; if (local_offset < 256) { @@ -7397,11 +7381,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, emit_byte(loader_ctx, (uint8)local_offset); } } - else { /* local index larger than 255, reserve leb */ + else { /* local index larger than 255, reserve leb */ emit_uint32(loader_ctx, local_idx); } - emit_operand(loader_ctx, *(loader_ctx->frame_offset - - wasm_value_type_cell_num(local_type))); + emit_operand(loader_ctx, + *(loader_ctx->frame_offset + - wasm_value_type_cell_num(local_type))); #else #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) if (local_offset < 0x80) { @@ -7438,16 +7423,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, p_org = p - 1; read_leb_uint32(p, p_end, global_idx); if (global_idx >= global_count) { - set_error_buf(error_buf, error_buf_size, - "unknown global"); + set_error_buf(error_buf, error_buf_size, "unknown global"); goto fail; } global_type = - global_idx < module->import_global_count - ? module->import_globals[global_idx].u.global.type - : module->globals[global_idx - module->import_global_count] - .type; + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.type + : module + ->globals[global_idx + - module->import_global_count] + .type; PUSH_TYPE(global_type); @@ -7461,7 +7447,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, *p_org = WASM_OP_GET_GLOBAL_64; } #endif -#else /* else of WASM_ENABLE_FAST_INTERP */ +#else /* else of WASM_ENABLE_FAST_INTERP */ if (global_type == VALUE_TYPE_I64 || global_type == VALUE_TYPE_F64) { skip_label(); @@ -7480,16 +7466,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, p_org = p - 1; read_leb_uint32(p, p_end, global_idx); if (global_idx >= global_count) { - set_error_buf(error_buf, error_buf_size, - "unknown global"); + set_error_buf(error_buf, error_buf_size, "unknown global"); goto fail; } is_mutable = - global_idx < module->import_global_count - ? module->import_globals[global_idx].u.global.is_mutable - : module->globals[global_idx - module->import_global_count] - .is_mutable; + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.is_mutable + : module + ->globals[global_idx + - module->import_global_count] + .is_mutable; if (!is_mutable) { set_error_buf(error_buf, error_buf_size, "global is immutable"); @@ -7497,10 +7484,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, } global_type = - global_idx < module->import_global_count - ? module->import_globals[global_idx].u.global.type - : module->globals[global_idx - module->import_global_count] - .type; + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.type + : module + ->globals[global_idx + - module->import_global_count] + .type; POP_TYPE(global_type); @@ -7519,7 +7508,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, #endif *p_org = WASM_OP_SET_GLOBAL_AUX_STACK; } -#else /* else of WASM_ENABLE_FAST_INTERP */ +#else /* else of WASM_ENABLE_FAST_INTERP */ if (global_type == VALUE_TYPE_I64 || global_type == VALUE_TYPE_F64) { skip_label(); @@ -7582,17 +7571,16 @@ wasm_loader_prepare_bytecode(WASMModule *module, } #endif CHECK_MEMORY(); - read_leb_uint32(p, p_end, align); /* align */ + read_leb_uint32(p, p_end, align); /* align */ read_leb_uint32(p, p_end, mem_offset); /* offset */ - if (!check_memory_access_align(opcode, align, - error_buf, error_buf_size)) { + if (!check_memory_access_align(opcode, align, error_buf, + error_buf_size)) { goto fail; } #if WASM_ENABLE_FAST_INTERP != 0 emit_uint32(loader_ctx, mem_offset); #endif - switch (opcode) - { + switch (opcode) { /* load */ case WASM_OP_I32_LOAD: case WASM_OP_I32_LOAD8_S: @@ -7698,7 +7686,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); disable_emit = true; - bh_memcpy_s((uint8*)&f32, sizeof(float32), p_org, sizeof(float32)); + bh_memcpy_s((uint8 *)&f32, sizeof(float32), p_org, + sizeof(float32)); GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32); #endif PUSH_F32(); @@ -7710,7 +7699,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, skip_label(); disable_emit = true; /* Some MCU may require 8-byte align */ - bh_memcpy_s((uint8*)&f64, sizeof(float64), p_org, sizeof(float64)); + bh_memcpy_s((uint8 *)&f64, sizeof(float64), p_org, + sizeof(float64)); GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64); #endif PUSH_F64(); @@ -7948,263 +7938,272 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 emit_byte(loader_ctx, ((uint8)opcode1)); #endif - switch (opcode1) - { - case WASM_OP_I32_TRUNC_SAT_S_F32: - case WASM_OP_I32_TRUNC_SAT_U_F32: - POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); - break; - case WASM_OP_I32_TRUNC_SAT_S_F64: - case WASM_OP_I32_TRUNC_SAT_U_F64: - POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); - break; - case WASM_OP_I64_TRUNC_SAT_S_F32: - case WASM_OP_I64_TRUNC_SAT_U_F32: - POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64); - break; - case WASM_OP_I64_TRUNC_SAT_S_F64: - case WASM_OP_I64_TRUNC_SAT_U_F64: - POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); - break; + switch (opcode1) { + case WASM_OP_I32_TRUNC_SAT_S_F32: + case WASM_OP_I32_TRUNC_SAT_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); + break; + case WASM_OP_I32_TRUNC_SAT_S_F64: + case WASM_OP_I32_TRUNC_SAT_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); + break; + case WASM_OP_I64_TRUNC_SAT_S_F32: + case WASM_OP_I64_TRUNC_SAT_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64); + break; + case WASM_OP_I64_TRUNC_SAT_S_F64: + case WASM_OP_I64_TRUNC_SAT_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); + break; #if WASM_ENABLE_BULK_MEMORY != 0 - case WASM_OP_MEMORY_INIT: - { - read_leb_uint32(p, p_end, data_seg_idx); + case WASM_OP_MEMORY_INIT: + { + read_leb_uint32(p, p_end, data_seg_idx); #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, data_seg_idx); + emit_uint32(loader_ctx, data_seg_idx); #endif - if (module->import_memory_count == 0 && module->memory_count == 0) - goto fail_unknown_memory; + if (module->import_memory_count == 0 + && module->memory_count == 0) + goto fail_unknown_memory; - if (*p++ != 0x00) - goto fail_zero_byte_expected; + if (*p++ != 0x00) + goto fail_zero_byte_expected; - if (data_seg_idx >= module->data_seg_count) { - set_error_buf_v(error_buf, error_buf_size, - "unknown data segment %d", data_seg_idx); - goto fail; - } + if (data_seg_idx >= module->data_seg_count) { + set_error_buf_v(error_buf, error_buf_size, + "unknown data segment %d", + data_seg_idx); + goto fail; + } - if (module->data_seg_count1 == 0) - goto fail_data_cnt_sec_require; + if (module->data_seg_count1 == 0) + goto fail_data_cnt_sec_require; - POP_I32(); - POP_I32(); - POP_I32(); - break; - } - case WASM_OP_DATA_DROP: - { - read_leb_uint32(p, p_end, data_seg_idx); + POP_I32(); + POP_I32(); + POP_I32(); + break; + } + case WASM_OP_DATA_DROP: + { + read_leb_uint32(p, p_end, data_seg_idx); #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, data_seg_idx); + emit_uint32(loader_ctx, data_seg_idx); #endif - if (data_seg_idx >= module->data_seg_count) { - set_error_buf(error_buf, error_buf_size, - "unknown data segment"); - goto fail; - } + if (data_seg_idx >= module->data_seg_count) { + set_error_buf(error_buf, error_buf_size, + "unknown data segment"); + goto fail; + } - if (module->data_seg_count1 == 0) - goto fail_data_cnt_sec_require; + if (module->data_seg_count1 == 0) + goto fail_data_cnt_sec_require; - break; - } - case WASM_OP_MEMORY_COPY: - { - /* both src and dst memory index should be 0 */ - if (*(int16*)p != 0x0000) - goto fail_zero_byte_expected; - p += 2; + break; + } + case WASM_OP_MEMORY_COPY: + { + /* both src and dst memory index should be 0 */ + if (*(int16 *)p != 0x0000) + goto fail_zero_byte_expected; + p += 2; - if (module->import_memory_count == 0 && module->memory_count == 0) - goto fail_unknown_memory; + if (module->import_memory_count == 0 + && module->memory_count == 0) + goto fail_unknown_memory; - POP_I32(); - POP_I32(); - POP_I32(); - break; - } - case WASM_OP_MEMORY_FILL: - { - if (*p++ != 0x00) { - goto fail_zero_byte_expected; - } - if (module->import_memory_count == 0 && module->memory_count == 0) { - goto fail_unknown_memory; + POP_I32(); + POP_I32(); + POP_I32(); + break; } + case WASM_OP_MEMORY_FILL: + { + if (*p++ != 0x00) { + goto fail_zero_byte_expected; + } + if (module->import_memory_count == 0 + && module->memory_count == 0) { + goto fail_unknown_memory; + } - POP_I32(); - POP_I32(); - POP_I32(); - break; -fail_zero_byte_expected: - set_error_buf(error_buf, error_buf_size, - "zero byte expected"); - goto fail; + POP_I32(); + POP_I32(); + POP_I32(); + break; + fail_zero_byte_expected: + set_error_buf(error_buf, error_buf_size, + "zero byte expected"); + goto fail; -fail_unknown_memory: - set_error_buf(error_buf, error_buf_size, - "unknown memory 0"); - goto fail; -fail_data_cnt_sec_require: - set_error_buf(error_buf, error_buf_size, - "data count section required"); - goto fail; - } + fail_unknown_memory: + set_error_buf(error_buf, error_buf_size, + "unknown memory 0"); + goto fail; + fail_data_cnt_sec_require: + set_error_buf(error_buf, error_buf_size, + "data count section required"); + goto fail; + } #endif /* WASM_ENABLE_BULK_MEMORY */ #if WASM_ENABLE_REF_TYPES != 0 - case WASM_OP_TABLE_INIT: - { - uint8 seg_ref_type = 0, tbl_ref_type = 0; - - if (!wasm_get_ref_types_flag()) { - goto unsupported_opcode; - } + case WASM_OP_TABLE_INIT: + { + uint8 seg_ref_type = 0, tbl_ref_type = 0; - read_leb_uint32(p, p_end, table_seg_idx); - read_leb_uint32(p, p_end, table_idx); + if (!wasm_get_ref_types_flag()) { + goto unsupported_opcode; + } - if (!get_table_elem_type(module, table_idx, &tbl_ref_type, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, table_seg_idx); + read_leb_uint32(p, p_end, table_idx); - if (!get_table_seg_elem_type(module, table_seg_idx, - &seg_ref_type, error_buf, + if (!get_table_elem_type(module, table_idx, + &tbl_ref_type, error_buf, error_buf_size)) - goto fail; + goto fail; - if (seg_ref_type != tbl_ref_type) { - set_error_buf(error_buf, error_buf_size, - "type mismatch"); - goto fail; - } + if (!get_table_seg_elem_type(module, table_seg_idx, + &seg_ref_type, error_buf, + error_buf_size)) + goto fail; + + if (seg_ref_type != tbl_ref_type) { + set_error_buf(error_buf, error_buf_size, + "type mismatch"); + goto fail; + } #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_seg_idx); - emit_uint32(loader_ctx, table_idx); + emit_uint32(loader_ctx, table_seg_idx); + emit_uint32(loader_ctx, table_idx); #endif - POP_I32(); - POP_I32(); - POP_I32(); - break; - } - case WASM_OP_ELEM_DROP: - { - if (!wasm_get_ref_types_flag()) { - goto unsupported_opcode; + POP_I32(); + POP_I32(); + POP_I32(); + break; } + case WASM_OP_ELEM_DROP: + { + if (!wasm_get_ref_types_flag()) { + goto unsupported_opcode; + } - read_leb_uint32(p, p_end, table_seg_idx); - if (!get_table_seg_elem_type(module, table_seg_idx, NULL, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, table_seg_idx); + if (!get_table_seg_elem_type(module, table_seg_idx, + NULL, error_buf, + error_buf_size)) + goto fail; #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_seg_idx); + emit_uint32(loader_ctx, table_seg_idx); #endif - break; - } - case WASM_OP_TABLE_COPY: - { - uint8 src_ref_type, dst_ref_type; - uint32 src_tbl_idx, dst_tbl_idx; - - if (!wasm_get_ref_types_flag()) { - goto unsupported_opcode; + break; } + case WASM_OP_TABLE_COPY: + { + uint8 src_ref_type, dst_ref_type; + uint32 src_tbl_idx, dst_tbl_idx; - read_leb_uint32(p, p_end, src_tbl_idx); - if (!get_table_elem_type(module, src_tbl_idx, &src_ref_type, - error_buf, error_buf_size)) - goto fail; + if (!wasm_get_ref_types_flag()) { + goto unsupported_opcode; + } - read_leb_uint32(p, p_end, dst_tbl_idx); - if (!get_table_elem_type(module, dst_tbl_idx, &dst_ref_type, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, src_tbl_idx); + if (!get_table_elem_type(module, src_tbl_idx, + &src_ref_type, error_buf, + error_buf_size)) + goto fail; - if (src_ref_type != dst_ref_type) { - set_error_buf(error_buf, error_buf_size, - "type mismatch"); - goto fail; - } + read_leb_uint32(p, p_end, dst_tbl_idx); + if (!get_table_elem_type(module, dst_tbl_idx, + &dst_ref_type, error_buf, + error_buf_size)) + goto fail; + + if (src_ref_type != dst_ref_type) { + set_error_buf(error_buf, error_buf_size, + "type mismatch"); + goto fail; + } #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, src_tbl_idx); - emit_uint32(loader_ctx, dst_tbl_idx); + emit_uint32(loader_ctx, src_tbl_idx); + emit_uint32(loader_ctx, dst_tbl_idx); #endif - POP_I32(); - POP_I32(); - POP_I32(); - break; - } - case WASM_OP_TABLE_SIZE: - { - if (!wasm_get_ref_types_flag()) { - goto unsupported_opcode; + POP_I32(); + POP_I32(); + POP_I32(); + break; } + case WASM_OP_TABLE_SIZE: + { + if (!wasm_get_ref_types_flag()) { + goto unsupported_opcode; + } - read_leb_uint32(p, p_end, table_idx); - /* TODO: shall we create a new function to check - table idx instead of using below function? */ - if (!get_table_elem_type(module, table_idx, NULL, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, table_idx); + /* TODO: shall we create a new function to check + table idx instead of using below function? */ + if (!get_table_elem_type(module, table_idx, NULL, + error_buf, error_buf_size)) + goto fail; #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_idx); + emit_uint32(loader_ctx, table_idx); #endif - PUSH_I32(); - break; - } - case WASM_OP_TABLE_GROW: - case WASM_OP_TABLE_FILL: - { - uint8 decl_ref_type; - - if (!wasm_get_ref_types_flag()) { - goto unsupported_opcode; + PUSH_I32(); + break; } + case WASM_OP_TABLE_GROW: + case WASM_OP_TABLE_FILL: + { + uint8 decl_ref_type; - read_leb_uint32(p, p_end, table_idx); - if (!get_table_elem_type(module, table_idx, - &decl_ref_type, error_buf, - error_buf_size)) - goto fail; - - if (opcode1 == WASM_OP_TABLE_GROW) { - if (table_idx < module->import_table_count) { - module->import_tables[table_idx] - .u.table.possible_grow = true; + if (!wasm_get_ref_types_flag()) { + goto unsupported_opcode; } - else { - module->tables[table_idx - module->import_table_count] - .possible_grow = true; + + read_leb_uint32(p, p_end, table_idx); + if (!get_table_elem_type(module, table_idx, + &decl_ref_type, error_buf, + error_buf_size)) + goto fail; + + if (opcode1 == WASM_OP_TABLE_GROW) { + if (table_idx < module->import_table_count) { + module->import_tables[table_idx] + .u.table.possible_grow = true; + } + else { + module + ->tables[table_idx + - module->import_table_count] + .possible_grow = true; + } } - } #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_idx); + emit_uint32(loader_ctx, table_idx); #endif - POP_I32(); + POP_I32(); #if WASM_ENABLE_FAST_INTERP != 0 - POP_OFFSET_TYPE(decl_ref_type); + POP_OFFSET_TYPE(decl_ref_type); #endif - POP_TYPE(decl_ref_type); - if (opcode1 == WASM_OP_TABLE_GROW) - PUSH_I32(); - else - POP_I32(); - break; - } + POP_TYPE(decl_ref_type); + if (opcode1 == WASM_OP_TABLE_GROW) + PUSH_I32(); + else + POP_I32(); + break; + } #endif /* WASM_ENABLE_REF_TYPES */ - default: - set_error_buf_v(error_buf, error_buf_size, - "%s %02x %02x", - "unsupported opcode", 0xfc, opcode1); - goto fail; + default: + set_error_buf_v(error_buf, error_buf_size, + "%s %02x %02x", "unsupported opcode", + 0xfc, opcode1); + goto fail; } break; } @@ -8214,7 +8213,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, case WASM_OP_SIMD_PREFIX: { opcode = read_uint8(p); - /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h */ + /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h + */ switch (opcode) { /* memory instruction */ case SIMD_v128_load: @@ -8233,7 +8233,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, read_leb_uint32(p, p_end, align); /* align */ if (!check_simd_memory_access_align( - opcode, align, error_buf, error_buf_size)) { + opcode, align, error_buf, error_buf_size)) { goto fail; } @@ -8249,7 +8249,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, read_leb_uint32(p, p_end, align); /* align */ if (!check_simd_memory_access_align( - opcode, align, error_buf, error_buf_size)) { + opcode, align, error_buf, error_buf_size)) { goto fail; } @@ -8354,15 +8354,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, if (replace[opcode - SIMD_i8x16_extract_lane_s]) { if (!(wasm_loader_pop_frame_ref( - loader_ctx, - replace[opcode - SIMD_i8x16_extract_lane_s], - error_buf, error_buf_size))) + loader_ctx, + replace[opcode - SIMD_i8x16_extract_lane_s], + error_buf, error_buf_size))) goto fail; } POP_AND_PUSH( - VALUE_TYPE_V128, - push_type[opcode - SIMD_i8x16_extract_lane_s]); + VALUE_TYPE_V128, + push_type[opcode - SIMD_i8x16_extract_lane_s]); break; } @@ -8463,7 +8463,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, read_leb_uint32(p, p_end, align); /* align */ if (!check_simd_memory_access_align( - opcode, align, error_buf, error_buf_size)) { + opcode, align, error_buf, error_buf_size)) { goto fail; } @@ -8491,7 +8491,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, read_leb_uint32(p, p_end, align); /* align */ if (!check_simd_memory_access_align( - opcode, align, error_buf, error_buf_size)) { + opcode, align, error_buf, error_buf_size)) { goto fail; } @@ -8864,10 +8864,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, #endif if (opcode != WASM_OP_ATOMIC_FENCE) { CHECK_MEMORY(); - read_leb_uint32(p, p_end, align); /* align */ + read_leb_uint32(p, p_end, align); /* align */ read_leb_uint32(p, p_end, mem_offset); /* offset */ - if (!check_memory_align_equal(opcode, align, - error_buf, + if (!check_memory_align_equal(opcode, align, error_buf, error_buf_size)) { goto fail; } @@ -8990,8 +8989,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, break; default: set_error_buf_v(error_buf, error_buf_size, - "%s %02x %02x", - "unsupported opcode", 0xfe, opcode); + "%s %02x %02x", "unsupported opcode", + 0xfe, opcode); goto fail; } break; @@ -9000,10 +8999,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, default: #if WASM_ENABLE_REF_TYPES != 0 -unsupported_opcode: + unsupported_opcode: #endif - set_error_buf_v(error_buf, error_buf_size, - "%s %02x", + set_error_buf_v(error_buf, error_buf_size, "%s %02x", "unsupported opcode", opcode); goto fail; } @@ -9025,15 +9023,14 @@ wasm_loader_prepare_bytecode(WASMModule *module, func->const_cell_num = loader_ctx->const_cell_num; if (func->const_cell_num > 0) { - if (!(func->consts = func_const = - loader_malloc(func->const_cell_num * 4, - error_buf, error_buf_size))) + if (!(func->consts = func_const = loader_malloc( + func->const_cell_num * 4, error_buf, error_buf_size))) goto fail; func_const_end = func->consts + func->const_cell_num * 4; /* reverse the const buf */ for (int i = loader_ctx->num_const - 1; i >= 0; i--) { - Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const)); + Const *c = (Const *)(loader_ctx->const_buf + i * sizeof(Const)); if (c->value_type == VALUE_TYPE_F64 || c->value_type == VALUE_TYPE_I64) { bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), @@ -9048,8 +9045,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, } } - func->max_stack_cell_num = loader_ctx->preserved_local_offset - - loader_ctx->start_dynamic_offset + 1; + func->max_stack_cell_num = loader_ctx->preserved_local_offset + - loader_ctx->start_dynamic_offset + 1; #else func->max_stack_cell_num = loader_ctx->max_stack_cell_num; #endif @@ -9085,4 +9082,3 @@ wasm_get_ref_types_flag() return ref_types_flag; } #endif - diff --git a/core/iwasm/interpreter/wasm_loader.h b/core/iwasm/interpreter/wasm_loader.h index 03b3b8bd53..403df27f87 100644 --- a/core/iwasm/interpreter/wasm_loader.h +++ b/core/iwasm/interpreter/wasm_loader.h @@ -23,8 +23,9 @@ extern "C" { * * @return return module loaded, NULL if failed */ -WASMModule* -wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size); +WASMModule * +wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size); /** * Load a WASM module from a specified WASM section list. @@ -35,9 +36,9 @@ wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_bu * * @return return WASM module loaded, NULL if failed */ -WASMModule* -wasm_loader_load_from_sections(WASMSection *section_list, - char *error_buf, uint32 error_buf_size); +WASMModule * +wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf, + uint32 error_buf_size); /** * Unload a WASM module. @@ -64,12 +65,9 @@ wasm_loader_unload(WASMModule *module); */ bool -wasm_loader_find_block_addr(WASMExecEnv *exec_env, - BlockAddr *block_addr_cache, - const uint8 *start_addr, - const uint8 *code_end_addr, - uint8 block_type, - uint8 **p_else_addr, +wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache, + const uint8 *start_addr, const uint8 *code_end_addr, + uint8 block_type, uint8 **p_else_addr, uint8 **p_end_addr); #if WASM_ENABLE_REF_TYPES != 0 diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index c4bcb505d4..ac299f14f7 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -14,24 +14,26 @@ /* Read a value of given type from the address pointed to by the given pointer and increase the pointer to the position just after the value being read. */ -#define TEMPLATE_READ_VALUE(Type, p) \ +#define TEMPLATE_READ_VALUE(Type, p) \ (p += sizeof(Type), *(Type *)(p - sizeof(Type))) static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "WASM module load failed: %s", string); + snprintf(error_buf, error_buf_size, "WASM module load failed: %s", + string); } -#define CHECK_BUF(buf, buf_end, length) do { \ - bh_assert(buf + length <= buf_end); \ - } while (0) +#define CHECK_BUF(buf, buf_end, length) \ + do { \ + bh_assert(buf + length <= buf_end); \ + } while (0) -#define CHECK_BUF1(buf, buf_end, length) do { \ - bh_assert(buf + length <= buf_end); \ - } while (0) +#define CHECK_BUF1(buf, buf_end, length) \ + do { \ + bh_assert(buf + length <= buf_end); \ + } while (0) #define skip_leb(p) while (*p++ & 0x80) #define skip_leb_int64(p, p_end) skip_leb(p) @@ -60,9 +62,8 @@ is_64bit_type(uint8 type) } static void -read_leb(uint8 **p_buf, const uint8 *buf_end, - uint32 maxbits, bool sign, uint64 *p_result, - char* error_buf, uint32 error_buf_size) +read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign, + uint64 *p_result, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; uint64 result = 0; @@ -125,40 +126,41 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, *p_result = result; } -#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p) +#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p) #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p) -#define read_bool(p) TEMPLATE_READ_VALUE(bool, p) - -#define read_leb_int64(p, p_end, res) do { \ - uint64 res64; \ - read_leb((uint8**)&p, p_end, 64, true, &res64, \ - error_buf, error_buf_size); \ - res = (int64)res64; \ -} while (0) - -#define read_leb_uint32(p, p_end, res) do { \ - uint64 res64; \ - read_leb((uint8**)&p, p_end, 32, false, &res64, \ - error_buf, error_buf_size); \ - res = (uint32)res64; \ -} while (0) - -#define read_leb_int32(p, p_end, res) do { \ - uint64 res64; \ - read_leb((uint8**)&p, p_end, 32, true, &res64, \ - error_buf, error_buf_size); \ - res = (int32)res64; \ -} while (0) +#define read_bool(p) TEMPLATE_READ_VALUE(bool, p) + +#define read_leb_int64(p, p_end, res) \ + do { \ + uint64 res64; \ + read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \ + error_buf_size); \ + res = (int64)res64; \ + } while (0) + +#define read_leb_uint32(p, p_end, res) \ + do { \ + uint64 res64; \ + read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \ + error_buf_size); \ + res = (uint32)res64; \ + } while (0) + +#define read_leb_int32(p, p_end, res) \ + do { \ + uint64 res64; \ + read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \ + error_buf_size); \ + res = (int32)res64; \ + } while (0) static void * loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); return NULL; } @@ -168,7 +170,7 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) static char * const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, - char *error_buf, uint32 error_buf_size) + char *error_buf, uint32 error_buf_size) { StringNode *node, *node_next; @@ -176,8 +178,7 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, node = module->const_str_list; while (node) { node_next = node->next; - if (strlen(node->str) == len - && !memcmp(node->str, str, len)) + if (strlen(node->str) == len && !memcmp(node->str, str, len)) break; node = node_next; } @@ -187,12 +188,12 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, return node->str; } - if (!(node = loader_malloc(sizeof(StringNode) + len + 1, - error_buf, error_buf_size))) { + if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf, + error_buf_size))) { return NULL; } - node->str = ((char*)node) + sizeof(StringNode); + node->str = ((char *)node) + sizeof(StringNode); bh_memcpy_s(node->str, len + 1, str, len); node->str[len] = '\0'; @@ -212,8 +213,8 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, static bool load_init_expr(const uint8 **p_buf, const uint8 *buf_end, - InitializerExpression *init_expr, uint8 type, - char *error_buf, uint32 error_buf_size) + InitializerExpression *init_expr, uint8 type, char *error_buf, + uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint8 flag, end_byte, *p_float; @@ -238,7 +239,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, case INIT_EXPR_TYPE_F32_CONST: bh_assert(type == VALUE_TYPE_F32); CHECK_BUF(p, p_end, 4); - p_float = (uint8*)&init_expr->u.f32; + p_float = (uint8 *)&init_expr->u.f32; for (i = 0; i < sizeof(float32); i++) *p_float++ = *p++; break; @@ -246,7 +247,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, case INIT_EXPR_TYPE_F64_CONST: bh_assert(type == VALUE_TYPE_F64); CHECK_BUF(p, p_end, 8); - p_float = (uint8*)&init_expr->u.f64; + p_float = (uint8 *)&init_expr->u.f64; for (i = 0; i < sizeof(float64); i++) *p_float++ = *p++; break; @@ -311,9 +312,9 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (type_count) { module->type_count = type_count; - total_size = sizeof(WASMType*) * (uint64)type_count; - if (!(module->types = loader_malloc - (total_size, error_buf, error_buf_size))) { + total_size = sizeof(WASMType *) * (uint64)type_count; + if (!(module->types = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -334,10 +335,10 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, bh_assert(param_count <= UINT16_MAX && result_count <= UINT16_MAX); - total_size = offsetof(WASMType, types) + - sizeof(uint8) * (uint64)(param_count + result_count); + total_size = offsetof(WASMType, types) + + sizeof(uint8) * (uint64)(param_count + result_count); if (!(type = module->types[i] = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -355,9 +356,10 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } param_cell_num = wasm_get_cell_num(type->types, param_count); - ret_cell_num = wasm_get_cell_num(type->types + param_count, - result_count); - bh_assert(param_cell_num <= UINT16_MAX && ret_cell_num <= UINT16_MAX); + ret_cell_num = + wasm_get_cell_num(type->types + param_count, result_count); + bh_assert(param_cell_num <= UINT16_MAX + && ret_cell_num <= UINT16_MAX); type->param_cell_num = (uint16)param_cell_num; type->ret_cell_num = (uint16)ret_cell_num; } @@ -373,7 +375,7 @@ static void adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size) { uint32 default_max_size = - init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE; + init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE; if (max_size_flag) { /* module defines the table limitation */ @@ -381,7 +383,7 @@ adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size) if (init_size < *max_size) { *max_size = - *max_size < default_max_size ? *max_size : default_max_size; + *max_size < default_max_size ? *max_size : default_max_size; } } else { @@ -393,10 +395,9 @@ adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size) static bool load_function_import(const uint8 **p_buf, const uint8 *buf_end, const WASMModule *parent_module, - const char *sub_module_name, - const char *function_name, - WASMFunctionImport *function, - char *error_buf, uint32 error_buf_size) + const char *sub_module_name, const char *function_name, + WASMFunctionImport *function, char *error_buf, + uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint32 declare_type_index = 0; @@ -415,12 +416,9 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, declare_func_type = parent_module->types[declare_type_index]; /* check built-in modules */ - linked_func = wasm_native_resolve_symbol(sub_module_name, - function_name, - declare_func_type, - &linked_signature, - &linked_attachment, - &linked_call_conv_raw); + linked_func = wasm_native_resolve_symbol( + sub_module_name, function_name, declare_func_type, &linked_signature, + &linked_attachment, &linked_call_conv_raw); function->module_name = (char *)sub_module_name; function->field_name = (char *)function_name; @@ -434,10 +432,8 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, static bool load_table_import(const uint8 **p_buf, const uint8 *buf_end, - WASMModule *parent_module, - const char *sub_module_name, - const char *table_name, - WASMTableImport *table, + WASMModule *parent_module, const char *sub_module_name, + const char *table_name, WASMTableImport *table, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; @@ -449,8 +445,8 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end, declare_elem_type = read_uint8(p); bh_assert(VALUE_TYPE_FUNCREF == declare_elem_type #if WASM_ENABLE_REF_TYPES != 0 - || (wasm_get_ref_types_flag() && - VALUE_TYPE_EXTERNREF == declare_elem_type) + || (wasm_get_ref_types_flag() + && VALUE_TYPE_EXTERNREF == declare_elem_type) #endif ); @@ -465,8 +461,8 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end, &declare_max_size); *p_buf = p; - bh_assert(!((declare_max_size_flag & 1) - && declare_init_size > declare_max_size)); + bh_assert( + !((declare_max_size_flag & 1) && declare_init_size > declare_max_size)); /* now we believe all declaration are ok */ table->elem_type = declare_elem_type; @@ -481,10 +477,8 @@ wasm_runtime_memory_pool_size(); static bool load_memory_import(const uint8 **p_buf, const uint8 *buf_end, - WASMModule *parent_module, - const char *sub_module_name, - const char *memory_name, - WASMMemoryImport *memory, + WASMModule *parent_module, const char *sub_module_name, + const char *memory_name, WASMMemoryImport *memory, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; @@ -528,10 +522,9 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, static bool load_global_import(const uint8 **p_buf, const uint8 *buf_end, - const WASMModule *parent_module, - char *sub_module_name, char *global_name, - WASMGlobalImport *global, - char *error_buf, uint32 error_buf_size) + const WASMModule *parent_module, char *sub_module_name, + char *global_name, WASMGlobalImport *global, char *error_buf, + uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint8 declare_type = 0; @@ -550,8 +543,8 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end, #if WASM_ENABLE_LIBC_BUILTIN != 0 /* check built-in modules */ - ret = wasm_native_lookup_libc_builtin_global(sub_module_name, - global_name, global); + ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name, + global); if (ret) { bh_assert(global->type == declare_type && global->is_mutable != declare_mutable); @@ -578,8 +571,8 @@ load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table, table->elem_type = read_uint8(p); bh_assert((VALUE_TYPE_FUNCREF == table->elem_type) #if WASM_ENABLE_REF_TYPES != 0 - || (wasm_get_ref_types_flag() && - VALUE_TYPE_EXTERNREF == table->elem_type) + || (wasm_get_ref_types_flag() + && VALUE_TYPE_EXTERNREF == table->elem_type) #endif ); @@ -659,10 +652,11 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, uint8 u8, kind; /* insert builtin module names into const str list */ - if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size) - || !const_str_list_insert((uint8*)"wasi_unstable", 13, module, + if (!const_str_list_insert((uint8 *)"env", 3, module, error_buf, + error_buf_size) + || !const_str_list_insert((uint8 *)"wasi_unstable", 13, module, error_buf, error_buf_size) - || !const_str_list_insert((uint8*)"wasi_snapshot_preview1", 22, module, + || !const_str_list_insert((uint8 *)"wasi_snapshot_preview1", 22, module, error_buf, error_buf_size)) { return false; } @@ -672,8 +666,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (import_count) { module->import_count = import_count; total_size = sizeof(WASMImport) * (uint64)import_count; - if (!(module->imports = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->imports = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -712,9 +706,9 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->import_table_count++; bh_assert( #if WASM_ENABLE_REF_TYPES != 0 - wasm_get_ref_types_flag() || + wasm_get_ref_types_flag() || #endif - module->import_table_count <= 1); + module->import_table_count <= 1); break; @@ -746,11 +740,12 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->imports + module->import_function_count; if (module->import_memory_count) import_memories = module->import_memories = - module->imports + module->import_function_count + module->import_table_count; + module->imports + module->import_function_count + + module->import_table_count; if (module->import_global_count) import_globals = module->import_globals = - module->imports + module->import_function_count + module->import_table_count - + module->import_memory_count; + module->imports + module->import_function_count + + module->import_table_count + module->import_memory_count; p = p_old; @@ -762,7 +757,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(sub_module_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, error_buf, error_buf_size))) { return false; } p += name_len; @@ -771,7 +766,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); CHECK_BUF(p, p_end, name_len); if (!(field_name = const_str_list_insert( - p, name_len, module, error_buf, error_buf_size))) { + p, name_len, module, error_buf, error_buf_size))) { return false; } p += name_len; @@ -780,16 +775,15 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, /* 0x00/0x01/0x02/0x03 */ kind = read_uint8(p); - LOG_DEBUG("import #%d: (%s, %s), kind: %d", - i, sub_module_name, field_name, kind); + LOG_DEBUG("import #%d: (%s, %s), kind: %d", i, sub_module_name, + field_name, kind); switch (kind) { case IMPORT_KIND_FUNC: /* import function */ bh_assert(import_functions); import = import_functions++; - if (!load_function_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.function, - error_buf, error_buf_size)) { + if (!load_function_import( + &p, p_end, module, sub_module_name, field_name, + &import->u.function, error_buf, error_buf_size)) { return false; } break; @@ -797,9 +791,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_TABLE: /* import table */ bh_assert(import_tables); import = import_tables++; - if (!load_table_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.table, + if (!load_table_import(&p, p_end, module, sub_module_name, + field_name, &import->u.table, error_buf, error_buf_size)) { LOG_DEBUG("can not import such a table (%s,%s)", sub_module_name, field_name); @@ -810,9 +803,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_MEMORY: /* import memory */ bh_assert(import_memories); import = import_memories++; - if (!load_memory_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.memory, + if (!load_memory_import(&p, p_end, module, sub_module_name, + field_name, &import->u.memory, error_buf, error_buf_size)) { return false; } @@ -821,9 +813,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, case IMPORT_KIND_GLOBAL: /* import global */ bh_assert(import_globals); import = import_globals++; - if (!load_global_import(&p, p_end, module, - sub_module_name, field_name, - &import->u.global, + if (!load_global_import(&p, p_end, module, sub_module_name, + field_name, &import->u.global, error_buf, error_buf_size)) { return false; } @@ -844,7 +835,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, import = module->import_functions; for (i = 0; i < module->import_function_count; i++, import++) { if (!strcmp(import->u.names.module_name, "wasi_unstable") - || !strcmp(import->u.names.module_name, "wasi_snapshot_preview1")) { + || !strcmp(import->u.names.module_name, + "wasi_snapshot_preview1")) { module->is_wasi_module = true; break; } @@ -862,8 +854,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } static bool -init_function_local_offsets(WASMFunction *func, - char *error_buf, uint32 error_buf_size) +init_function_local_offsets(WASMFunction *func, char *error_buf, + uint32 error_buf_size) { WASMType *param_type = func->func_type; uint32 param_count = param_type->param_count; @@ -875,7 +867,7 @@ init_function_local_offsets(WASMFunction *func, if (total_size > 0 && !(func->local_offsets = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -896,8 +888,8 @@ init_function_local_offsets(WASMFunction *func, static bool load_function_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code, const uint8 *buf_code_end, - WASMModule *module, - char *error_buf, uint32 error_buf_size) + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; const uint8 *p_code = buf_code, *p_code_end, *p_code_save; @@ -917,9 +909,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (func_count) { module->function_count = func_count; - total_size = sizeof(WASMFunction*) * (uint64)func_count; + total_size = sizeof(WASMFunction *) * (uint64)func_count; if (!(module->functions = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -929,8 +921,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, bh_assert(type_index < module->type_count); read_leb_uint32(p_code, buf_code_end, code_size); - bh_assert(code_size > 0 - && p_code + code_size <= buf_code_end); + bh_assert(code_size > 0 && p_code + code_size <= buf_code_end); /* Resolve local set count */ p_code_end = p_code + code_size; @@ -954,7 +945,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, total_size = sizeof(WASMFunction) + (uint64)local_count; if (!(func = module->functions[i] = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -962,7 +953,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, func->func_type = module->types[type_index]; func->local_count = local_count; if (local_count > 0) - func->local_types = (uint8*)func + sizeof(WASMFunction); + func->local_types = (uint8 *)func + sizeof(WASMFunction); func->code_size = code_size; /* * we shall make a copy of code body [p_code, p_code + code_size] @@ -974,7 +965,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, * memcpy(code_body_cp, p_code, code_size); * func->code = code_body_cp; */ - func->code = (uint8*)p_code; + func->code = (uint8 *)p_code; /* Load each local type */ p_code = p_code_save; @@ -983,8 +974,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, read_leb_uint32(p_code, buf_code_end, sub_local_count); bh_assert(sub_local_count && local_type_index <= UINT32_MAX - sub_local_count - && local_type_index + sub_local_count - <= local_count); + && local_type_index + sub_local_count <= local_count); CHECK_BUF(p_code, buf_code_end, 1); /* 0x7F/0x7E/0x7D/0x7C */ @@ -1020,10 +1010,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, } static bool -check_function_index(const WASMModule *module, - uint32 function_index, - char *error_buf, - uint32 error_buf_size) +check_function_index(const WASMModule *module, uint32 function_index, + char *error_buf, uint32 error_buf_size) { return (function_index < module->import_function_count + module->function_count); @@ -1041,15 +1029,15 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, table_count); bh_assert( #if WASM_ENABLE_REF_TYPES != 0 - wasm_get_ref_types_flag() || + wasm_get_ref_types_flag() || #endif - module->import_table_count + table_count <= 1); + module->import_table_count + table_count <= 1); if (table_count) { module->table_count = table_count; total_size = sizeof(WASMTable) * (uint64)table_count; - if (!(module->tables = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->tables = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1080,8 +1068,8 @@ load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (memory_count) { module->memory_count = memory_count; total_size = sizeof(WASMMemory) * (uint64)memory_count; - if (!(module->memories = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->memories = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1112,14 +1100,14 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (global_count) { module->global_count = global_count; total_size = sizeof(WASMGlobal) * (uint64)global_count; - if (!(module->globals = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->globals = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } global = module->globals; - for(i = 0; i < global_count; i++, global++) { + for (i = 0; i < global_count; i++, global++) { CHECK_BUF(p, p_end, 2); global->type = read_uint8(p); mutable = read_uint8(p); @@ -1127,8 +1115,8 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, global->is_mutable = mutable ? true : false; /* initialize expression */ - if (!load_init_expr(&p, p_end, &(global->init_expr), - global->type, error_buf, error_buf_size)) + if (!load_init_expr(&p, p_end, &(global->init_expr), global->type, + error_buf, error_buf_size)) return false; if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) { @@ -1146,7 +1134,7 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, == global->init_expr.init_expr_type) { bh_assert(global->init_expr.u.ref_index < module->import_function_count - + module->function_count); + + module->function_count); } } } @@ -1172,13 +1160,13 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (export_count) { module->export_count = export_count; total_size = sizeof(WASMExport) * (uint64)export_count; - if (!(module->exports = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->exports = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } export = module->exports; - for (i = 0; i < export_count; i++, export++) { + for (i = 0; i < export_count; i++, export ++) { read_leb_uint32(p, p_end, str_len); CHECK_BUF(p, p_end, str_len); @@ -1188,8 +1176,8 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, && memcmp(name, p, str_len) == 0)); } - if (!(export->name = const_str_list_insert(p, str_len, module, - error_buf, error_buf_size))) { + if (!(export->name = const_str_list_insert( + p, str_len, module, error_buf, error_buf_size))) { return false; } @@ -1199,26 +1187,26 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, index); export->index = index; - switch(export->kind) { + switch (export->kind) { /* function index */ case EXPORT_KIND_FUNC: bh_assert(index < module->function_count - + module->import_function_count); + + module->import_function_count); break; /* table index */ case EXPORT_KIND_TABLE: bh_assert(index < module->table_count - + module->import_table_count); + + module->import_table_count); break; /* memory index */ case EXPORT_KIND_MEMORY: bh_assert(index < module->memory_count - + module->import_memory_count); + + module->import_memory_count); break; /* global index */ case EXPORT_KIND_GLOBAL: bh_assert(index < module->global_count - + module->import_global_count); + + module->import_global_count); break; default: bh_assert(0); @@ -1234,14 +1222,14 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } static bool -check_table_index(const WASMModule *module, uint32 table_index, - char *error_buf, uint32 error_buf_size) +check_table_index(const WASMModule *module, uint32 table_index, char *error_buf, + uint32 error_buf_size) { if ( #if WASM_ENABLE_REF_TYPES != 0 - !wasm_get_ref_types_flag() && + !wasm_get_ref_types_flag() && #endif - table_index != 0) { + table_index != 0) { return false; } @@ -1253,9 +1241,8 @@ check_table_index(const WASMModule *module, uint32 table_index, #if WASM_ENABLE_REF_TYPES != 0 static bool -load_table_index(const uint8 **p_buf, const uint8 *buf_end, - WASMModule *module, uint32 *p_table_index, - char *error_buf, uint32 error_buf_size) +load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module, + uint32 *p_table_index, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint32 table_index; @@ -1271,9 +1258,8 @@ load_table_index(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_elem_type(const uint8 **p_buf, const uint8 *buf_end, - uint32 *p_elem_type, bool elemkind_zero, - char *error_buf, uint32 error_buf_size) +load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type, + bool elemkind_zero, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint8 elem_type; @@ -1301,8 +1287,7 @@ load_elem_type(const uint8 **p_buf, const uint8 *buf_end, static bool load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module, WASMTableSeg *table_segment, - bool use_init_expr, - char *error_buf, uint32 error_buf_size) + bool use_init_expr, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; uint32 function_count, function_index = 0, i; @@ -1312,8 +1297,8 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, table_segment->function_count = function_count; total_size = sizeof(uint32) * (uint64)function_count; if (total_size > 0 - && !(table_segment->func_indexes = (uint32 *) - loader_malloc(total_size, error_buf, error_buf_size))) { + && !(table_segment->func_indexes = (uint32 *)loader_malloc( + total_size, error_buf, error_buf_size))) { return false; } @@ -1355,8 +1340,9 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) +load_table_segment_section(const uint8 *buf, const uint8 *buf_end, + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 table_segment_count, i, table_index, function_count; @@ -1368,8 +1354,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m if (table_segment_count) { module->table_seg_count = table_segment_count; total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count; - if (!(module->table_segments = loader_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module->table_segments = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1395,14 +1381,14 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m return false; if (!load_init_expr( - &p, p_end, &table_segment->base_offset, - VALUE_TYPE_I32, error_buf, error_buf_size)) + &p, p_end, &table_segment->base_offset, + VALUE_TYPE_I32, error_buf, error_buf_size)) return false; if (!load_func_index_vec( - &p, p_end, module, table_segment, - table_segment->mode == 0 ? false : true, - error_buf, error_buf_size)) + &p, p_end, module, table_segment, + table_segment->mode == 0 ? false : true, + error_buf, error_buf_size)) return false; break; /* elemkind + passive/declarative */ @@ -1425,18 +1411,18 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m error_buf, error_buf_size)) return false; if (!load_init_expr( - &p, p_end, &table_segment->base_offset, - VALUE_TYPE_I32, error_buf, error_buf_size)) + &p, p_end, &table_segment->base_offset, + VALUE_TYPE_I32, error_buf, error_buf_size)) return false; if (!load_elem_type( - &p, p_end, &table_segment->elem_type, - table_segment->mode == 2 ? true : false, - error_buf, error_buf_size)) + &p, p_end, &table_segment->elem_type, + table_segment->mode == 2 ? true : false, + error_buf, error_buf_size)) return false; if (!load_func_index_vec( - &p, p_end, module, table_segment, - table_segment->mode == 2 ? false : true, - error_buf, error_buf_size)) + &p, p_end, module, table_segment, + table_segment->mode == 2 ? false : true, + error_buf, error_buf_size)) return false; break; case 5: @@ -1446,8 +1432,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m error_buf, error_buf_size)) return false; if (!load_func_index_vec(&p, p_end, module, - table_segment, true, - error_buf, error_buf_size)) + table_segment, true, error_buf, + error_buf_size)) return false; break; default: @@ -1473,7 +1459,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m total_size = sizeof(uint32) * (uint64)function_count; if (total_size > 0 && !(table_segment->func_indexes = (uint32 *)loader_malloc( - total_size, error_buf, error_buf_size))) { + total_size, error_buf, error_buf_size))) { return false; } @@ -1493,8 +1479,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m static bool load_data_segment_section(const uint8 *buf, const uint8 *buf_end, - WASMModule *module, - char *error_buf, uint32 error_buf_size) + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 data_seg_count, i, mem_index, data_seg_len; @@ -1515,9 +1501,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, if (data_seg_count) { module->data_seg_count = data_seg_count; - total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count; - if (!(module->data_segments = loader_malloc - (total_size, error_buf, error_buf_size))) { + total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count; + if (!(module->data_segments = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1537,9 +1523,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, case 0x02: /* read following memory index */ read_leb_uint32(p, p_end, mem_index); -check_mem_index: + check_mem_index: bh_assert(mem_index < module->import_memory_count - + module->memory_count); + + module->memory_count); break; case 0x03: default: @@ -1547,8 +1533,8 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, break; } #else - bh_assert(mem_index < module->import_memory_count - + module->memory_count); + bh_assert(mem_index + < module->import_memory_count + module->memory_count); #endif /* WASM_ENABLE_BULK_MEMORY */ #if WASM_ENABLE_BULK_MEMORY != 0 @@ -1560,8 +1546,8 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, read_leb_uint32(p, p_end, data_seg_len); - if (!(dataseg = module->data_segments[i] = loader_malloc - (sizeof(WASMDataSeg), error_buf, error_buf_size))) { + if (!(dataseg = module->data_segments[i] = loader_malloc( + sizeof(WASMDataSeg), error_buf, error_buf_size))) { return false; } @@ -1570,15 +1556,16 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, if (!is_passive) #endif { - bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression), - &init_expr, sizeof(InitializerExpression)); + bh_memcpy_s(&dataseg->base_offset, + sizeof(InitializerExpression), &init_expr, + sizeof(InitializerExpression)); dataseg->memory_index = mem_index; } dataseg->data_length = data_seg_len; CHECK_BUF(p, p_end, data_seg_len); - dataseg->data = (uint8*)p; + dataseg->data = (uint8 *)p; p += data_seg_len; } } @@ -1590,8 +1577,9 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, #if WASM_ENABLE_BULK_MEMORY != 0 static bool -load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, - char *error_buf, uint32 error_buf_size) +load_datacount_section(const uint8 *buf, const uint8 *buf_end, + WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 data_seg_count1 = 0; @@ -1606,10 +1594,8 @@ load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *modul #endif static bool -load_code_section(const uint8 *buf, const uint8 *buf_end, - const uint8 *buf_func, - const uint8 *buf_func_end, - WASMModule *module, +load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func, + const uint8 *buf_func_end, WASMModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -1640,14 +1626,14 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, start_function); - bh_assert(start_function < module->function_count - + module->import_function_count); + bh_assert(start_function + < module->function_count + module->import_function_count); if (start_function < module->import_function_count) type = module->import_functions[start_function].u.function.func_type; else type = module->functions[start_function - module->import_function_count] - ->func_type; + ->func_type; bh_assert(type->param_count == 0 && type->result_count == 0); @@ -1661,8 +1647,7 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 static bool -handle_name_section(const uint8 *buf, const uint8 *buf_end, - WASMModule *module, +handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -1701,9 +1686,9 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, func_index -= module->import_count; bh_assert(func_index < module->function_count); if (!(module->functions[func_index]->field_name = - const_str_list_insert(p, func_name_len, - module, error_buf, - error_buf_size))) { + const_str_list_insert(p, func_name_len, + module, error_buf, + error_buf_size))) { return false; } } @@ -1711,8 +1696,9 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end, } } break; - case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection */ - case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */ + case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection + */ + case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */ default: p = p + subsection_size; break; @@ -1737,8 +1723,7 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, read_leb_uint32(p, p_end, name_len); - bh_assert(name_len > 0 - && p + name_len <= p_end); + bh_assert(name_len > 0 && p + name_len <= p_end); #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 if (memcmp(p, "name", 4) == 0) { @@ -1753,9 +1738,8 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, #if WASM_ENABLE_REF_TYPES != 0 static bool -get_table_elem_type(const WASMModule *module, - uint32 table_idx, uint8 *p_elem_type, - char *error_buf, uint32 error_buf_size) +get_table_elem_type(const WASMModule *module, uint32 table_idx, + uint8 *p_elem_type, char *error_buf, uint32 error_buf_size) { if (!check_table_index(module, table_idx, error_buf, error_buf_size)) { return false; @@ -1765,16 +1749,17 @@ get_table_elem_type(const WASMModule *module, if (table_idx < module->import_table_count) *p_elem_type = module->import_tables[table_idx].u.table.elem_type; else - *p_elem_type = module->tables[module->import_table_count - + table_idx].elem_type; + *p_elem_type = + module->tables[module->import_table_count + table_idx] + .elem_type; } return true; } static bool -get_table_seg_elem_type(const WASMModule *module, - uint32 table_seg_idx, uint8 *p_elem_type, - char *error_buf, uint32 error_buf_size) +get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx, + uint8 *p_elem_type, char *error_buf, + uint32 error_buf_size) { if (table_seg_idx >= module->table_seg_count) { return false; @@ -1788,10 +1773,8 @@ get_table_seg_elem_type(const WASMModule *module, #endif static bool -wasm_loader_prepare_bytecode(WASMModule *module, - WASMFunction *func, - uint32 cur_func_idx, - char *error_buf, +wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, + uint32 cur_func_idx, char *error_buf, uint32 error_buf_size); #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0 @@ -1802,13 +1785,13 @@ static void **handle_table; #endif static bool -load_from_sections(WASMModule *module, WASMSection *sections, - char *error_buf, uint32 error_buf_size) +load_from_sections(WASMModule *module, WASMSection *sections, char *error_buf, + uint32 error_buf_size) { WASMExport *export; WASMSection *section = sections; const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL, - *buf_func = NULL, *buf_func_end = NULL; + *buf_func = NULL, *buf_func_end = NULL; WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL; WASMGlobal *aux_stack_top_global = NULL, *global; uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1; @@ -1838,44 +1821,53 @@ load_from_sections(WASMModule *module, WASMSection *sections, switch (section->section_type) { case SECTION_TYPE_USER: /* unsupported user section, ignore it. */ - if (!load_user_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_user_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_TYPE: - if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_type_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_IMPORT: - if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_import_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_FUNC: if (!load_function_section(buf, buf_end, buf_code, buf_code_end, - module, error_buf, error_buf_size)) + module, error_buf, error_buf_size)) return false; break; case SECTION_TYPE_TABLE: - if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_table_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_MEMORY: - if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_memory_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_GLOBAL: - if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_global_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_export_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_START: - if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_start_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_ELEM: - if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_table_segment_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case SECTION_TYPE_CODE: @@ -1884,18 +1876,19 @@ load_from_sections(WASMModule *module, WASMSection *sections, return false; break; case SECTION_TYPE_DATA: - if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_data_segment_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; #if WASM_ENABLE_BULK_MEMORY != 0 case SECTION_TYPE_DATACOUNT: - if (!load_datacount_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_datacount_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; #endif default: - set_error_buf(error_buf, error_buf_size, - "invalid section id"); + set_error_buf(error_buf, error_buf_size, "invalid section id"); return false; } @@ -1908,15 +1901,14 @@ load_from_sections(WASMModule *module, WASMSection *sections, /* Resolve auxiliary data/stack/heap info and reset memory info */ export = module->exports; - for (i = 0; i < module->export_count; i++, export++) { + for (i = 0; i < module->export_count; i++, export ++) { if (export->kind == EXPORT_KIND_GLOBAL) { if (!strcmp(export->name, "__heap_base")) { global_index = export->index - module->import_global_count; global = module->globals + global_index; - if (global->type == VALUE_TYPE_I32 - && !global->is_mutable - && global->init_expr.init_expr_type == - INIT_EXPR_TYPE_I32_CONST) { + if (global->type == VALUE_TYPE_I32 && !global->is_mutable + && global->init_expr.init_expr_type + == INIT_EXPR_TYPE_I32_CONST) { aux_heap_base_global = global; aux_heap_base = global->init_expr.u.i32; aux_heap_base_global_index = export->index; @@ -1927,10 +1919,9 @@ load_from_sections(WASMModule *module, WASMSection *sections, else if (!strcmp(export->name, "__data_end")) { global_index = export->index - module->import_global_count; global = module->globals + global_index; - if (global->type == VALUE_TYPE_I32 - && !global->is_mutable - && global->init_expr.init_expr_type == - INIT_EXPR_TYPE_I32_CONST) { + if (global->type == VALUE_TYPE_I32 && !global->is_mutable + && global->init_expr.init_expr_type + == INIT_EXPR_TYPE_I32_CONST) { aux_data_end_global = global; aux_data_end = global->init_expr.u.i32; aux_data_end_global_index = export->index; @@ -1971,17 +1962,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, if (global->is_mutable /* heap_base and data_end is not mutable */ && global->type == VALUE_TYPE_I32 - && global->init_expr.init_expr_type == - INIT_EXPR_TYPE_I32_CONST + && global->init_expr.init_expr_type + == INIT_EXPR_TYPE_I32_CONST && (uint32)global->init_expr.u.i32 <= aux_heap_base) { aux_stack_top_global = global; aux_stack_top = (uint32)global->init_expr.u.i32; module->aux_stack_top_global_index = - module->import_global_count + global_index; + module->import_global_count + global_index; module->aux_stack_bottom = aux_stack_top; - module->aux_stack_size = aux_stack_top > aux_data_end - ? aux_stack_top - aux_data_end - : aux_stack_top; + module->aux_stack_size = + aux_stack_top > aux_data_end + ? aux_stack_top - aux_data_end + : aux_stack_top; LOG_VERBOSE("Found aux stack top global, value: %d, " "global index: %d, stack size: %d", aux_stack_top, global_index, @@ -1999,14 +1991,13 @@ load_from_sections(WASMModule *module, WASMSection *sections, /* Resolve malloc/free function exported by wasm module */ export = module->exports; - for (i = 0; i < module->export_count; i++, export++) { + for (i = 0; i < module->export_count; i++, export ++) { if (export->kind == EXPORT_KIND_FUNC) { if (!strcmp(export->name, "malloc") && export->index >= module->import_function_count) { 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 + 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) { bh_assert(module->malloc_function == (uint32)-1); @@ -2020,8 +2011,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, /* __new && __pin for AssemblyScript */ 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 + if (func_type->param_count == 2 && func_type->result_count == 1 && func_type->types[0] == VALUE_TYPE_I32 && func_type->types[1] == VALUE_TYPE_I32 && func_type->types[2] == VALUE_TYPE_I32) { @@ -2043,19 +2033,20 @@ load_from_sections(WASMModule *module, WASMSection *sections, && (export_tmp->index >= module->import_function_count)) { func_index = export_tmp->index - - module->import_function_count; + - module->import_function_count; func_type = - module->functions[func_index]->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) { - bh_assert( - module->retain_function == (uint32)-1); + bh_assert(module->retain_function + == (uint32)-1); module->retain_function = export_tmp->index; - LOG_VERBOSE( - "Found retain function, name: %s, index: %u", - export_tmp->name, export_tmp->index); + LOG_VERBOSE("Found retain function, name: %s, " + "index: %u", + export_tmp->name, + export_tmp->index); break; } } @@ -2073,8 +2064,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, && export->index >= module->import_function_count) { 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 + if (func_type->param_count == 1 && func_type->result_count == 0 && func_type->types[0] == VALUE_TYPE_I32) { bh_assert(module->free_function == (uint32)-1); module->free_function = export->index; @@ -2091,8 +2081,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, for (i = 0; i < module->function_count; i++) { WASMFunction *func = module->functions[i]; - if (!wasm_loader_prepare_bytecode(module, func, i, - error_buf, error_buf_size)) { + if (!wasm_loader_prepare_bytecode(module, func, i, error_buf, + error_buf_size)) { return false; } } @@ -2101,16 +2091,15 @@ load_from_sections(WASMModule *module, WASMSection *sections, WASMMemoryImport *memory_import; WASMMemory *memory; - if (aux_data_end_global - && aux_heap_base_global + if (aux_data_end_global && aux_heap_base_global && aux_stack_top_global) { uint64 init_memory_size; uint32 shrunk_memory_size = align_uint(aux_heap_base, 8); if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - init_memory_size = (uint64)memory_import->num_bytes_per_page * - memory_import->init_page_count; + init_memory_size = (uint64)memory_import->num_bytes_per_page + * memory_import->init_page_count; if (shrunk_memory_size <= init_memory_size) { /* Reset memory info to decrease memory usage */ memory_import->num_bytes_per_page = shrunk_memory_size; @@ -2121,8 +2110,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, } if (module->memory_count) { memory = &module->memories[0]; - init_memory_size = (uint64)memory->num_bytes_per_page * - memory->init_page_count; + init_memory_size = (uint64)memory->num_bytes_per_page + * memory->init_page_count; if (shrunk_memory_size <= init_memory_size) { /* Reset memory info to decrease memory usage */ memory->num_bytes_per_page = shrunk_memory_size; @@ -2156,11 +2145,11 @@ load_from_sections(WASMModule *module, WASMSection *sections, return true; } -static WASMModule* +static WASMModule * create_module(char *error_buf, uint32 error_buf_size) { - WASMModule *module = loader_malloc(sizeof(WASMModule), - error_buf, error_buf_size); + WASMModule *module = + loader_malloc(sizeof(WASMModule), error_buf, error_buf_size); if (!module) { return NULL; @@ -2178,8 +2167,8 @@ create_module(char *error_buf, uint32 error_buf_size) } WASMModule * -wasm_loader_load_from_sections(WASMSection *section_list, - char *error_buf, uint32 error_buf_size) +wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf, + uint32 error_buf_size) { WASMModule *module = create_module(error_buf, error_buf_size); if (!module) @@ -2205,6 +2194,7 @@ destroy_sections(WASMSection *section_list) } } +/* clang-format off */ static uint8 section_ids[] = { SECTION_TYPE_USER, SECTION_TYPE_TYPE, @@ -2222,6 +2212,7 @@ static uint8 section_ids[] = { SECTION_TYPE_CODE, SECTION_TYPE_DATA }; +/* clang-format on */ static uint8 get_section_index(uint8 section_type) @@ -2237,12 +2228,11 @@ get_section_index(uint8 section_type) } static bool -create_sections(const uint8 *buf, uint32 size, - WASMSection **p_section_list, +create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list, char *error_buf, uint32 error_buf_size) { WASMSection *section_list_end = NULL, *section; - const uint8 *p = buf, *p_end = buf + size/*, *section_body*/; + const uint8 *p = buf, *p_end = buf + size /*, *section_body*/; uint8 section_type, section_index, last_section_index = (uint8)-1; uint32 section_size; @@ -2266,13 +2256,13 @@ create_sections(const uint8 *buf, uint32 size, read_leb_uint32(p, p_end, section_size); CHECK_BUF1(p, p_end, section_size); - if (!(section = loader_malloc(sizeof(WASMSection), - error_buf, error_buf_size))) { + if (!(section = loader_malloc(sizeof(WASMSection), error_buf, + error_buf_size))) { return false; } section->section_type = section_type; - section->section_body = (uint8*)p; + section->section_body = (uint8 *)p; section->section_body_size = section_size; if (!*p_section_list) @@ -2294,7 +2284,7 @@ create_sections(const uint8 *buf, uint32 size, } static void -exchange32(uint8* p_data) +exchange32(uint8 *p_data) { uint8 value = *p_data; *p_data = *(p_data + 3); @@ -2313,8 +2303,8 @@ static union { #define is_little_endian() (__ue.b == 1) static bool -load(const uint8 *buf, uint32 size, WASMModule *module, - char *error_buf, uint32 error_buf_size) +load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf_end = buf + size; const uint8 *p = buf, *p_end = buf_end; @@ -2324,14 +2314,14 @@ load(const uint8 *buf, uint32 size, WASMModule *module, CHECK_BUF1(p, p_end, sizeof(uint32)); magic_number = read_uint32(p); if (!is_little_endian()) - exchange32((uint8*)&magic_number); + exchange32((uint8 *)&magic_number); bh_assert(magic_number == WASM_MAGIC_NUMBER); CHECK_BUF1(p, p_end, sizeof(uint32)); version = read_uint32(p); if (!is_little_endian()) - exchange32((uint8*)&version); + exchange32((uint8 *)&version); if (version != WASM_CURRENT_VERSION) { set_error_buf(error_buf, error_buf_size, "unknown binary version"); @@ -2339,7 +2329,8 @@ load(const uint8 *buf, uint32 size, WASMModule *module, } if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size) - || !load_from_sections(module, section_list, error_buf, error_buf_size)) { + || !load_from_sections(module, section_list, error_buf, + error_buf_size)) { destroy_sections(section_list); return false; } @@ -2349,8 +2340,9 @@ load(const uint8 *buf, uint32 size, WASMModule *module, return true; } -WASMModule* -wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size) +WASMModule * +wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size) { WASMModule *module = create_module(error_buf, error_buf_size); if (!module) { @@ -2447,10 +2439,8 @@ wasm_loader_unload(WASMModule *module) bool wasm_loader_find_block_addr(BlockAddr *block_addr_cache, - const uint8 *start_addr, - const uint8 *code_end_addr, - uint8 label_type, - uint8 **p_else_addr, + const uint8 *start_addr, const uint8 *code_end_addr, + uint8 label_type, uint8 **p_else_addr, uint8 **p_end_addr) { const uint8 *p = start_addr, *p_end = code_end_addr; @@ -2489,7 +2479,8 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, case WASM_OP_IF: /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */ u8 = read_uint8(p); - if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) { + if (block_nested_depth + < sizeof(block_stack) / sizeof(BlockAddr)) { block_stack[block_nested_depth].start_addr = p; block_stack[block_nested_depth].else_addr = NULL; } @@ -2501,7 +2492,8 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, case EXT_OP_IF: /* block type */ skip_leb_uint32(p, p_end); - if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) { + if (block_nested_depth + < sizeof(block_stack) / sizeof(BlockAddr)) { block_stack[block_nested_depth].start_addr = p; block_stack[block_nested_depth].else_addr = NULL; } @@ -2510,33 +2502,37 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, case WASM_OP_ELSE: if (label_type == LABEL_TYPE_IF && block_nested_depth == 1) - else_addr = (uint8*)(p - 1); - if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr)) - block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1); + else_addr = (uint8 *)(p - 1); + if (block_nested_depth - 1 + < sizeof(block_stack) / sizeof(BlockAddr)) + block_stack[block_nested_depth - 1].else_addr = + (uint8 *)(p - 1); break; case WASM_OP_END: if (block_nested_depth == 1) { if (label_type == LABEL_TYPE_IF) *p_else_addr = else_addr; - *p_end_addr = (uint8*)(p - 1); + *p_end_addr = (uint8 *)(p - 1); - block_stack[0].end_addr = (uint8*)(p - 1); - for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) { + block_stack[0].end_addr = (uint8 *)(p - 1); + for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr); + t++) { start_addr = block_stack[t].start_addr; if (start_addr) { i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1); - block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; + block = + block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) if (!block[j].start_addr) break; if (j == BLOCK_ADDR_CONFLICT_SIZE) { - memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) * - sizeof(BlockAddr)); + memmove(block + 1, block, + (BLOCK_ADDR_CONFLICT_SIZE - 1) + * sizeof(BlockAddr)); j = 0; - } block[j].start_addr = block_stack[t].start_addr; block[j].else_addr = block_stack[t].else_addr; @@ -2549,8 +2545,10 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, } else { block_nested_depth--; - if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) - block_stack[block_nested_depth].end_addr = (uint8*)(p - 1); + if (block_nested_depth + < sizeof(block_stack) / sizeof(BlockAddr)) + block_stack[block_nested_depth].end_addr = + (uint8 *)(p - 1); } break; @@ -2561,7 +2559,7 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, case WASM_OP_BR_TABLE: read_leb_uint32(p, p_end, count); /* lable num */ - for (i = 0; i <= count; i++) /* lableidxs */ + for (i = 0; i <= count; i++) /* lableidxs */ skip_leb_uint32(p, p_end); break; @@ -2916,24 +2914,24 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, return false; } -#define REF_I32 VALUE_TYPE_I32 -#define REF_F32 VALUE_TYPE_F32 +#define REF_I32 VALUE_TYPE_I32 +#define REF_F32 VALUE_TYPE_F32 #define REF_I64_1 VALUE_TYPE_I64 #define REF_I64_2 VALUE_TYPE_I64 #define REF_F64_1 VALUE_TYPE_F64 #define REF_F64_2 VALUE_TYPE_F64 -#define REF_ANY VALUE_TYPE_ANY +#define REF_ANY VALUE_TYPE_ANY #if WASM_ENABLE_FAST_INTERP != 0 #if WASM_DEBUG_PREPROCESSOR != 0 -#define LOG_OP(...) os_printf(__VA_ARGS__) +#define LOG_OP(...) os_printf(__VA_ARGS__) #else -#define LOG_OP(...) (void)0 +#define LOG_OP(...) (void)0 #endif #define PATCH_ELSE 0 -#define PATCH_END 1 +#define PATCH_END 1 typedef struct BranchBlockPatch { struct BranchBlockPatch *next; uint8 patch_type; @@ -3014,14 +3012,13 @@ typedef struct Const { uint8 value_type; } Const; -static void* -memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, - char *error_buf, uint32 error_buf_size) +static void * +memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf, + uint32 error_buf_size) { uint8 *mem_new; bh_assert(size_new > size_old); - if ((mem_new = loader_malloc - (size_new, error_buf, error_buf_size))) { + if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) { bh_memcpy_s(mem_new, size_new, mem_old, size_old); memset(mem_new + size_old, 0, size_new - size_old); wasm_runtime_free(mem_old); @@ -3029,42 +3026,46 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, return mem_new; } -#define MEM_REALLOC(mem, size_old, size_new) do { \ - void *mem_new = memory_realloc(mem, size_old, size_new, \ - error_buf, error_buf_size); \ - if (!mem_new) \ - goto fail; \ - mem = mem_new; \ - } while (0) - -#define CHECK_CSP_PUSH() do { \ - if (ctx->frame_csp >= ctx->frame_csp_boundary) { \ - MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \ - (uint32)(ctx->frame_csp_size \ - + 8 * sizeof(BranchBlock))); \ - ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \ - ctx->frame_csp_boundary = ctx->frame_csp_bottom + \ - ctx->frame_csp_size / sizeof(BranchBlock); \ - ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \ - } \ - } while (0) - -#define CHECK_CSP_POP() do { \ - bh_assert(ctx->csp_num >= 1); \ - } while (0) +#define MEM_REALLOC(mem, size_old, size_new) \ + do { \ + void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \ + error_buf_size); \ + if (!mem_new) \ + goto fail; \ + mem = mem_new; \ + } while (0) + +#define CHECK_CSP_PUSH() \ + do { \ + if (ctx->frame_csp >= ctx->frame_csp_boundary) { \ + MEM_REALLOC( \ + ctx->frame_csp_bottom, ctx->frame_csp_size, \ + (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \ + ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \ + ctx->frame_csp_boundary = \ + ctx->frame_csp_bottom \ + + ctx->frame_csp_size / sizeof(BranchBlock); \ + ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \ + } \ + } while (0) + +#define CHECK_CSP_POP() \ + do { \ + bh_assert(ctx->csp_num >= 1); \ + } while (0) #if WASM_ENABLE_FAST_INTERP != 0 static bool -check_offset_push(WASMLoaderContext *ctx, - char *error_buf, uint32 error_buf_size) +check_offset_push(WASMLoaderContext *ctx, char *error_buf, + uint32 error_buf_size) { uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom); if (ctx->frame_offset >= ctx->frame_offset_boundary) { MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size, ctx->frame_offset_size + 16); ctx->frame_offset_size += 16; - ctx->frame_offset_boundary = ctx->frame_offset_bottom + - ctx->frame_offset_size / sizeof(int16); + ctx->frame_offset_boundary = + ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16); ctx->frame_offset = ctx->frame_offset_bottom + cell_num; } return true; @@ -3100,15 +3101,14 @@ free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num) for (uint32 i = 0; i < csp_num; i++) { free_label_patch_list(tmp_csp); - tmp_csp ++; + tmp_csp++; } } #endif static bool -check_stack_push(WASMLoaderContext *ctx, - char *error_buf, uint32 error_buf_size) +check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size) { if (ctx->frame_ref >= ctx->frame_ref_boundary) { MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size, @@ -3122,7 +3122,6 @@ check_stack_push(WASMLoaderContext *ctx, return false; } - static bool check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, char *error_buf, uint32 error_buf_size) @@ -3130,32 +3129,31 @@ check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, bh_assert(!((is_32bit_type(type) && stack_cell_num < 1) || (is_64bit_type(type) && stack_cell_num < 2))); - bh_assert(!((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32) - || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32) - || (type == VALUE_TYPE_I64 - && (*(frame_ref - 2) != REF_I64_1 - || *(frame_ref - 1) != REF_I64_2)) - || (type == VALUE_TYPE_F64 - && (*(frame_ref - 2) != REF_F64_1 - || *(frame_ref - 1) != REF_F64_2)))); + bh_assert(!( + (type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32) + || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32) + || (type == VALUE_TYPE_I64 + && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2)) + || (type == VALUE_TYPE_F64 + && (*(frame_ref - 2) != REF_F64_1 + || *(frame_ref - 1) != REF_F64_2)))); return true; } static bool -check_stack_pop(WASMLoaderContext *ctx, uint8 type, - char *error_buf, uint32 error_buf_size) +check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf, + uint32 error_buf_size) { - int32 block_stack_cell_num = (int32) - (ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num); + int32 block_stack_cell_num = + (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num); - if (block_stack_cell_num > 0 - && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) { + if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) { /* the stack top is a value of any type, return success */ return true; } - if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, - type, error_buf, error_buf_size)) + if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type, + error_buf, error_buf_size)) return false; return true; @@ -3183,7 +3181,7 @@ wasm_loader_ctx_destroy(WASMLoaderContext *ctx) } } -static WASMLoaderContext* +static WASMLoaderContext * wasm_loader_ctx_init(WASMFunction *func) { WASMLoaderContext *loader_ctx = @@ -3194,15 +3192,15 @@ wasm_loader_ctx_init(WASMFunction *func) loader_ctx->frame_ref_size = 32; if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = - wasm_runtime_malloc(loader_ctx->frame_ref_size))) + wasm_runtime_malloc(loader_ctx->frame_ref_size))) goto fail; memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size); - loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + - loader_ctx->frame_ref_size; + loader_ctx->frame_ref_boundary = + loader_ctx->frame_ref_bottom + loader_ctx->frame_ref_size; loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8; if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = - wasm_runtime_malloc(loader_ctx->frame_csp_size))) + wasm_runtime_malloc(loader_ctx->frame_csp_size))) goto fail; memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size); loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8; @@ -3210,21 +3208,21 @@ wasm_loader_ctx_init(WASMFunction *func) #if WASM_ENABLE_FAST_INTERP != 0 loader_ctx->frame_offset_size = sizeof(int16) * 32; if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset = - wasm_runtime_malloc(loader_ctx->frame_offset_size))) + wasm_runtime_malloc(loader_ctx->frame_offset_size))) goto fail; - memset(loader_ctx->frame_offset_bottom, 0, - loader_ctx->frame_offset_size); + memset(loader_ctx->frame_offset_bottom, 0, loader_ctx->frame_offset_size); loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32; loader_ctx->num_const = 0; loader_ctx->const_buf_size = sizeof(Const) * 8; - if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size))) + if (!(loader_ctx->const_buf = + wasm_runtime_malloc(loader_ctx->const_buf_size))) goto fail; memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size); loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset = - loader_ctx->max_dynamic_offset = func->param_cell_num + - func->local_cell_num; + loader_ctx->max_dynamic_offset = + func->param_cell_num + func->local_cell_num; #endif return loader_ctx; @@ -3234,8 +3232,8 @@ wasm_loader_ctx_init(WASMFunction *func) } static bool -wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, - char *error_buf, uint32 error_buf_size) +wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf, + uint32 error_buf_size) { if (type == VALUE_TYPE_VOID) return true; @@ -3261,12 +3259,12 @@ wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, } static bool -wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, - char *error_buf, uint32 error_buf_size) +wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf, + uint32 error_buf_size) { BranchBlock *cur_block = ctx->frame_csp - 1; - int32 available_stack_cell = (int32) - (ctx->stack_cell_num - cur_block->stack_cell_num); + int32 available_stack_cell = + (int32)(ctx->stack_cell_num - cur_block->stack_cell_num); /* Directly return success if current block is in stack * polymorphic state while stack is empty. */ @@ -3292,11 +3290,12 @@ wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, static bool wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt, - uint8 type_push, uint8 type_pop, - char *error_buf, uint32 error_buf_size) + uint8 type_push, uint8 type_pop, char *error_buf, + uint32 error_buf_size) { for (int i = 0; i < pop_cnt; i++) { - if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, error_buf_size)) + if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, + error_buf_size)) return false; } if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size)) @@ -3306,7 +3305,7 @@ wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt, static bool wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type, - BlockType block_type, uint8* start_addr, + BlockType block_type, uint8 *start_addr, char *error_buf, uint32 error_buf_size) { CHECK_CSP_PUSH(); @@ -3329,8 +3328,8 @@ wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type, } static bool -wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, - char *error_buf, uint32 error_buf_size) +wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf, + uint32 error_buf_size) { CHECK_CSP_POP(); #if WASM_ENABLE_FAST_INTERP != 0 @@ -3346,124 +3345,132 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 -#define emit_label(opcode) do { \ - wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \ - LOG_OP("\nemit_op [%02x]\t", opcode); \ - } while (0) -#define skip_label() do { \ - wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \ - LOG_OP("\ndelete last op\n"); \ - } while (0) +#define emit_label(opcode) \ + do { \ + wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) +#define skip_label() \ + do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#define emit_label(opcode) do { \ - int32 offset = (int32)((uint8*)handle_table[opcode] \ - - (uint8*)handle_table[0]); \ - if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \ - set_error_buf(error_buf, error_buf_size, \ - "pre-compiled label offset out of range"); \ - goto fail; \ - } \ - wasm_loader_emit_int16(loader_ctx, offset); \ - LOG_OP("\nemit_op [%02x]\t", opcode); \ - } while (0) -#define skip_label() do { \ - wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \ - LOG_OP("\ndelete last op\n"); \ - } while (0) +#define emit_label(opcode) \ + do { \ + int32 offset = \ + (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \ + if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \ + set_error_buf(error_buf, error_buf_size, \ + "pre-compiled label offset out of range"); \ + goto fail; \ + } \ + wasm_loader_emit_int16(loader_ctx, offset); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) +#define skip_label() \ + do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ -#define emit_label(opcode) do { \ - wasm_loader_emit_uint8(loader_ctx, opcode); \ - LOG_OP("\nemit_op [%02x]\t", opcode); \ - } while (0) -#define skip_label() do { \ - wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \ - LOG_OP("\ndelete last op\n"); \ - } while (0) +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#define emit_label(opcode) \ + do { \ + wasm_loader_emit_uint8(loader_ctx, opcode); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) +#define skip_label() \ + do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */ -#define emit_empty_label_addr_and_frame_ip(type) do { \ - if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \ - loader_ctx->p_code_compiled, \ - error_buf, error_buf_size)) \ - goto fail; \ - /* label address, to be patched */ \ - wasm_loader_emit_ptr(loader_ctx, NULL); \ - } while (0) - -#define emit_br_info(frame_csp) do { \ - if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \ - && last_op <= WASM_OP_I32_ROTR) \ - || (last_op == WASM_OP_I32_LOAD \ - || last_op == WASM_OP_F32_LOAD) \ - || (last_op >= WASM_OP_I32_LOAD8_S \ - && last_op <= WASM_OP_I32_LOAD16_U) \ - || (last_op >= WASM_OP_F32_ABS \ - && last_op <= WASM_OP_F32_COPYSIGN) \ - || (last_op >= WASM_OP_I32_WRAP_I64 \ - && last_op <= WASM_OP_I32_TRUNC_U_F64) \ - || (last_op >= WASM_OP_F32_CONVERT_S_I32 \ - && last_op <= WASM_OP_F32_DEMOTE_F64) \ - || (last_op == WASM_OP_I32_REINTERPRET_F32) \ - || (last_op == WASM_OP_F32_REINTERPRET_I32) \ - || (last_op == EXT_OP_COPY_STACK_TOP) - -#define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \ - && last_op <= WASM_OP_I64_ROTR) \ - || (last_op >= WASM_OP_F64_ABS \ - && last_op <= WASM_OP_F64_COPYSIGN) \ - || (last_op == WASM_OP_I64_LOAD \ - || last_op == WASM_OP_F64_LOAD) \ - || (last_op >= WASM_OP_I64_LOAD8_S \ - && last_op <= WASM_OP_I64_LOAD32_U) \ - || (last_op >= WASM_OP_I64_EXTEND_S_I32 \ - && last_op <= WASM_OP_I64_TRUNC_U_F64) \ - || (last_op >= WASM_OP_F64_CONVERT_S_I32 \ - && last_op <= WASM_OP_F64_PROMOTE_F32) \ - || (last_op == WASM_OP_I64_REINTERPRET_F64) \ - || (last_op == WASM_OP_F64_REINTERPRET_I64) \ - || (last_op == EXT_OP_COPY_STACK_TOP_I64) - -#define GET_CONST_OFFSET(type, val) do { \ - if (!(wasm_loader_get_const_offset(loader_ctx, type, \ - &val, &operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define GET_CONST_F32_OFFSET(type, fval) do { \ - if (!(wasm_loader_get_const_offset(loader_ctx, type, \ - &fval, &operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define GET_CONST_F64_OFFSET(type, fval) do { \ - if (!(wasm_loader_get_const_offset(loader_ctx, type, \ - &fval, &operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define emit_operand(ctx, offset) do { \ - wasm_loader_emit_int16(ctx, offset); \ - LOG_OP("%d\t", offset); \ - } while (0) - -#define emit_byte(ctx, byte) do { \ - wasm_loader_emit_uint8(ctx, byte); \ - LOG_OP("%d\t", byte); \ - } while (0) - -#define emit_uint32(ctx, value) do { \ - wasm_loader_emit_uint32(ctx, value); \ - LOG_OP("%d\t", value); \ - } while (0) +#define emit_empty_label_addr_and_frame_ip(type) \ + do { \ + if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \ + loader_ctx->p_code_compiled, error_buf, \ + error_buf_size)) \ + goto fail; \ + /* label address, to be patched */ \ + wasm_loader_emit_ptr(loader_ctx, NULL); \ + } while (0) + +#define emit_br_info(frame_csp) \ + do { \ + if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define LAST_OP_OUTPUT_I32() \ + (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \ + || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \ + || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \ + || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \ + || (last_op >= WASM_OP_I32_WRAP_I64 \ + && last_op <= WASM_OP_I32_TRUNC_U_F64) \ + || (last_op >= WASM_OP_F32_CONVERT_S_I32 \ + && last_op <= WASM_OP_F32_DEMOTE_F64) \ + || (last_op == WASM_OP_I32_REINTERPRET_F32) \ + || (last_op == WASM_OP_F32_REINTERPRET_I32) \ + || (last_op == EXT_OP_COPY_STACK_TOP) + +#define LAST_OP_OUTPUT_I64() \ + (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \ + || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \ + || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \ + || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \ + || (last_op >= WASM_OP_I64_EXTEND_S_I32 \ + && last_op <= WASM_OP_I64_TRUNC_U_F64) \ + || (last_op >= WASM_OP_F64_CONVERT_S_I32 \ + && last_op <= WASM_OP_F64_PROMOTE_F32) \ + || (last_op == WASM_OP_I64_REINTERPRET_F64) \ + || (last_op == WASM_OP_F64_REINTERPRET_I64) \ + || (last_op == EXT_OP_COPY_STACK_TOP_I64) + +#define GET_CONST_OFFSET(type, val) \ + do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \ + &operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define GET_CONST_F32_OFFSET(type, fval) \ + do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \ + &operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define GET_CONST_F64_OFFSET(type, fval) \ + do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \ + &operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define emit_operand(ctx, offset) \ + do { \ + wasm_loader_emit_int16(ctx, offset); \ + LOG_OP("%d\t", offset); \ + } while (0) + +#define emit_byte(ctx, byte) \ + do { \ + wasm_loader_emit_uint8(ctx, byte); \ + LOG_OP("%d\t", byte); \ + } while (0) + +#define emit_uint32(ctx, value) \ + do { \ + wasm_loader_emit_uint32(ctx, value); \ + LOG_OP("%d\t", value); \ + } while (0) static bool wasm_loader_ctx_reinit(WASMLoaderContext *ctx) @@ -3471,8 +3478,7 @@ wasm_loader_ctx_reinit(WASMLoaderContext *ctx) if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size))) return false; memset(ctx->p_code_compiled, 0, ctx->code_compiled_size); - ctx->p_code_compiled_end = ctx->p_code_compiled + - ctx->code_compiled_size; + ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size; /* clean up frame ref */ memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size); @@ -3596,8 +3602,9 @@ wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size) static bool preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode, - uint32 local_index, uint32 local_type, bool *preserved, - char *error_buf, uint32 error_buf_size) + uint32 local_index, uint32 local_type, + bool *preserved, char *error_buf, + uint32 error_buf_size) { uint32 i = 0; int16 preserved_offset = (int16)local_index; @@ -3606,7 +3613,8 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode, while (i < loader_ctx->stack_cell_num) { uint8 cur_type = loader_ctx->frame_ref_bottom[i]; - /* move previous local into dynamic space before a set/tee_local opcode */ + /* move previous local into dynamic space before a set/tee_local opcode + */ if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) { if (!(*preserved)) { *preserved = true; @@ -3682,12 +3690,12 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode, } static bool -add_label_patch_to_list(BranchBlock *frame_csp, - uint8 patch_type, uint8 *p_code_compiled, - char *error_buf, uint32 error_buf_size) +add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type, + uint8 *p_code_compiled, char *error_buf, + uint32 error_buf_size) { - BranchBlockPatch *patch = loader_malloc - (sizeof(BranchBlockPatch), error_buf, error_buf_size); + BranchBlockPatch *patch = + loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size); if (!patch) { return false; } @@ -3705,8 +3713,7 @@ add_label_patch_to_list(BranchBlock *frame_csp, } static void -apply_label_patch(WASMLoaderContext *ctx, uint8 depth, - uint8 patch_type) +apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type) { BranchBlock *frame_csp = ctx->frame_csp - depth; BranchBlockPatch *node = frame_csp->patch_list; @@ -3780,11 +3787,11 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, for (i = (int32)arity - 1; i >= 0; i--) { cell = (uint8)wasm_value_type_cell_num(types[i]); frame_offset -= cell; - emit_operand(ctx, *(int16*)(frame_offset)); + emit_operand(ctx, *(int16 *)(frame_offset)); } /* Part e */ - dynamic_offset = frame_csp->dynamic_offset - + wasm_get_cell_num(types, arity); + dynamic_offset = + frame_csp->dynamic_offset + wasm_get_cell_num(types, arity); for (i = (int32)arity - 1; i >= 0; i--) { cell = (uint8)wasm_value_type_cell_num(types[i]); dynamic_offset -= cell; @@ -3797,8 +3804,7 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, wasm_loader_emit_ptr(ctx, frame_csp->code_compiled); } else { - if (!add_label_patch_to_list(frame_csp, PATCH_END, - ctx->p_code_compiled, + if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled, error_buf, error_buf_size)) return false; /* label address, to be patched */ @@ -3860,8 +3866,8 @@ wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type, then current block is the function block */ uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0; BranchBlock *cur_block = ctx->frame_csp - depth; - int32 available_stack_cell = (int32) - (ctx->stack_cell_num - cur_block->stack_cell_num); + int32 available_stack_cell = + (int32)(ctx->stack_cell_num - cur_block->stack_cell_num); /* Directly return success if current block is in stack * polymorphic state while stack is empty. */ @@ -3904,12 +3910,13 @@ wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt, char *error_buf, uint32 error_buf_size) { for (int i = 0; i < pop_cnt; i++) { - if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, error_buf_size)) + if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, + error_buf_size)) return false; } - if (!wasm_loader_push_frame_offset(ctx, type_push, - disable_emit, operand_offset, - error_buf, error_buf_size)) + if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit, + operand_offset, error_buf, + error_buf_size)) return false; return true; @@ -3960,78 +3967,81 @@ wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt, } static bool -wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, - void *value, int16 *offset, - char *error_buf, uint32 error_buf_size) +wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value, + int16 *offset, char *error_buf, + uint32 error_buf_size) { int16 operand_offset = 0; Const *c; for (c = (Const *)ctx->const_buf; - (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) { + (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) { if ((type == c->value_type) - && ((type == VALUE_TYPE_I64 && *(int64*)value == c->value.i64) - || (type == VALUE_TYPE_I32 && *(int32*)value == c->value.i32) + && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64) + || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32) #if WASM_ENABLE_REF_TYPES != 0 - || (type == VALUE_TYPE_FUNCREF && *(int32*)value == c->value.i32) - || (type == VALUE_TYPE_EXTERNREF && *(int32*)value == c->value.i32) + || (type == VALUE_TYPE_FUNCREF + && *(int32 *)value == c->value.i32) + || (type == VALUE_TYPE_EXTERNREF + && *(int32 *)value == c->value.i32) #endif - || (type == VALUE_TYPE_F64 - && (0 == memcmp(value, &(c->value.f64), sizeof(float64)))) - || (type == VALUE_TYPE_F32 - && (0 == memcmp(value, &(c->value.f32), sizeof(float32)))))) { + || (type == VALUE_TYPE_F64 + && (0 == memcmp(value, &(c->value.f64), sizeof(float64)))) + || (type == VALUE_TYPE_F32 + && (0 + == memcmp(value, &(c->value.f32), sizeof(float32)))))) { operand_offset = c->slot_index; break; } - if (c->value_type == VALUE_TYPE_I64 - || c->value_type == VALUE_TYPE_F64) + if (c->value_type == VALUE_TYPE_I64 || c->value_type == VALUE_TYPE_F64) operand_offset += 2; else operand_offset += 1; } if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) { if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) { - MEM_REALLOC(ctx->const_buf, - ctx->const_buf_size, + MEM_REALLOC(ctx->const_buf, ctx->const_buf_size, ctx->const_buf_size + 4 * sizeof(Const)); ctx->const_buf_size += 4 * sizeof(Const); c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const)); } c->value_type = type; switch (type) { - case VALUE_TYPE_F64: - bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, sizeof(float64)); - ctx->const_cell_num += 2; - /* The const buf will be reversed, we use the second cell */ - /* of the i64/f64 const so the finnal offset is corrent */ - operand_offset ++; - break; - case VALUE_TYPE_I64: - c->value.i64 = *(int64*)value; - ctx->const_cell_num += 2; - operand_offset ++; - break; - case VALUE_TYPE_F32: - bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, sizeof(float32)); - ctx->const_cell_num ++; - break; - case VALUE_TYPE_I32: - c->value.i32 = *(int32*)value; - ctx->const_cell_num ++; - break; + case VALUE_TYPE_F64: + bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, + sizeof(float64)); + ctx->const_cell_num += 2; + /* The const buf will be reversed, we use the second cell */ + /* of the i64/f64 const so the finnal offset is corrent */ + operand_offset++; + break; + case VALUE_TYPE_I64: + c->value.i64 = *(int64 *)value; + ctx->const_cell_num += 2; + operand_offset++; + break; + case VALUE_TYPE_F32: + bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, + sizeof(float32)); + ctx->const_cell_num++; + break; + case VALUE_TYPE_I32: + c->value.i32 = *(int32 *)value; + ctx->const_cell_num++; + break; #if WASM_ENABLE_REF_TYPES != 0 - case VALUE_TYPE_EXTERNREF: - case VALUE_TYPE_FUNCREF: - c->value.i32 = *(int32*)value; - ctx->const_cell_num ++; - break; + case VALUE_TYPE_EXTERNREF: + case VALUE_TYPE_FUNCREF: + c->value.i32 = *(int32 *)value; + ctx->const_cell_num++; + break; #endif - default: - break; + default: + break; } c->slot_index = operand_offset; - ctx->num_const ++; - LOG_OP("#### new const [%d]: %ld\n", - ctx->num_const, (int64)c->value.i64); + ctx->num_const++; + LOG_OP("#### new const [%d]: %ld\n", ctx->num_const, + (int64)c->value.i64); } /* use negetive index for const */ operand_offset = -(operand_offset + 1); @@ -4057,189 +4067,211 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, PUSH_XXX(); only push the frame_offset stack, no emit */ -#define PUSH_I32() do { \ - if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \ - disable_emit, operand_offset,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_F32() do { \ - if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \ - disable_emit, operand_offset,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_I64() do { \ - if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \ - disable_emit, operand_offset,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_F64() do { \ - if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \ - disable_emit, operand_offset,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_FUNCREF() do { \ - if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF,\ - disable_emit, operand_offset,\ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define POP_I32() do { \ - if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define POP_F32() do { \ - if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define POP_I64() do { \ - if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define POP_F64() do { \ - if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_OFFSET_TYPE(type) do { \ - if (!(wasm_loader_push_frame_offset(loader_ctx, type, \ - disable_emit, operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_OFFSET_TYPE(type) do { \ - if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 1, \ - type_push, type_pop, \ - disable_emit, operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define PUSH_I32() \ + do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_F32() \ + do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_I64() \ + do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_F64() \ + do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_FUNCREF() \ + do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_I32() \ + do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_F32() \ + do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_I64() \ + do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_F64() \ + do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_OFFSET_TYPE(type) \ + do { \ + if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \ + operand_offset, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_OFFSET_TYPE(type) \ + do { \ + if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref_offset( \ + loader_ctx, 1, type_push, type_pop, disable_emit, \ + operand_offset, error_buf, error_buf_size))) \ + goto fail; \ + } while (0) /* type of POPs should be the same */ -#define POP2_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 2, \ - type_push, type_pop, \ - disable_emit, operand_offset, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define POP2_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref_offset( \ + loader_ctx, 2, type_push, type_pop, disable_emit, \ + operand_offset, error_buf, error_buf_size))) \ + goto fail; \ + } while (0) #else /* WASM_ENABLE_FAST_INTERP */ -#define PUSH_I32() do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define PUSH_F32() do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define PUSH_I64() do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define PUSH_F64() do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define PUSH_FUNCREF() do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,\ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_I32() do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_F32() do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_I64() do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_F64() do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_FUNCREF() do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, \ - type_push, type_pop, \ +#define PUSH_I32() \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_F32() \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_I64() \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_F64() \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_FUNCREF() \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \ error_buf, error_buf_size))) \ - goto fail; \ - } while (0) + goto fail; \ + } while (0) + +#define POP_I32() \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_F32() \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_I64() \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_F64() \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_FUNCREF() \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \ + type_pop, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) /* type of POPs should be the same */ -#define POP2_AND_PUSH(type_pop, type_push) do { \ - if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, \ - type_push, type_pop, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) +#define POP2_AND_PUSH(type_pop, type_push) \ + do { \ + if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \ + type_pop, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) #endif /* WASM_ENABLE_FAST_INTERP */ #if WASM_ENABLE_FAST_INTERP != 0 static bool -reserve_block_ret(WASMLoaderContext *loader_ctx, - uint8 opcode, bool disable_emit, - char *error_buf, uint32 error_buf_size) +reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, + bool disable_emit, char *error_buf, uint32 error_buf_size) { int16 operand_offset = 0; - BranchBlock *block = (opcode == WASM_OP_ELSE) ? - loader_ctx->frame_csp - 1 : loader_ctx->frame_csp; + BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1 + : loader_ctx->frame_csp; BlockType *block_type = &block->block_type; uint8 *return_types = NULL; uint32 return_count = 0, value_count = 0, total_cel_num = 0; int32 i = 0; - int16 dynamic_offset, dynamic_offset_org, - *frame_offset = NULL, *frame_offset_org = NULL; + int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL, + *frame_offset_org = NULL; return_count = block_type_get_result_types(block_type, &return_types); @@ -4251,7 +4283,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, /* insert op_copy before else opcode */ if (opcode == WASM_OP_ELSE) skip_label(); - emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP : EXT_OP_COPY_STACK_TOP_I64); + emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP + : EXT_OP_COPY_STACK_TOP_I64); emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell)); emit_operand(loader_ctx, block->dynamic_offset); @@ -4280,8 +4313,7 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, */ frame_offset = frame_offset_org = loader_ctx->frame_offset; dynamic_offset = dynamic_offset_org = - block->dynamic_offset - + wasm_get_cell_num(return_types, return_count); + block->dynamic_offset + wasm_get_cell_num(return_types, return_count); /* First traversal to get the count of values needed to be copied. */ for (i = (int32)return_count - 1; i >= 0; i--) { @@ -4300,9 +4332,9 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 *emit_data = NULL, *cells = NULL; int16 *src_offsets = NULL; uint16 *dst_offsets = NULL; - uint64 size = (uint64)value_count * (sizeof(*cells) - + sizeof(*src_offsets) - + sizeof(*dst_offsets)); + uint64 size = + (uint64)value_count + * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets)); /* Allocate memory for the emit data */ if (!(emit_data = loader_malloc(size, error_buf, error_buf_size))) @@ -4321,7 +4353,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, /* Part b) */ emit_uint32(loader_ctx, total_cel_num); - /* Second traversal to get each value's cell num, src offset and dst offset. */ + /* Second traversal to get each value's cell num, src offset and dst + * offset. */ frame_offset = frame_offset_org; dynamic_offset = dynamic_offset_org; for (i = (int32)return_count - 1, j = 0; i >= 0; i--) { @@ -4374,56 +4407,62 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, #endif /* WASM_ENABLE_FAST_INTERP */ -#define RESERVE_BLOCK_RET() do { \ - if (!reserve_block_ret(loader_ctx, opcode, disable_emit, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define PUSH_TYPE(type) do { \ - if (!(wasm_loader_push_frame_ref(loader_ctx, type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define POP_TYPE(type) do { \ - if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \ - error_buf, error_buf_size))) \ - goto fail; \ - } while (0) - -#define PUSH_CSP(label_type, block_type, _start_addr) do { \ - if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \ - _start_addr, error_buf, \ - error_buf_size)) \ - goto fail; \ - } while (0) - -#define POP_CSP() do { \ - if (!wasm_loader_pop_frame_csp(loader_ctx, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \ - read_leb_uint32(p, p_end, local_idx); \ - bh_assert(local_idx < param_count + local_count);\ - local_type = local_idx < param_count \ - ? param_types[local_idx] \ - : local_types[local_idx - param_count]; \ - local_offset = local_offsets[local_idx]; \ - } while (0) - -#define CHECK_BR(depth) do { \ - if (!wasm_loader_check_br(loader_ctx, depth, \ - error_buf, error_buf_size)) \ - goto fail; \ - } while (0) - -#define CHECK_MEMORY() do { \ - bh_assert(module->import_memory_count \ - + module->memory_count > 0); \ - } while (0) +#define RESERVE_BLOCK_RET() \ + do { \ + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_TYPE(type) \ + do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_TYPE(type) \ + do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \ + error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_CSP(label_type, block_type, _start_addr) \ + do { \ + if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \ + _start_addr, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_CSP() \ + do { \ + if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \ + do { \ + read_leb_uint32(p, p_end, local_idx); \ + bh_assert(local_idx < param_count + local_count); \ + local_type = local_idx < param_count \ + ? param_types[local_idx] \ + : local_types[local_idx - param_count]; \ + local_offset = local_offsets[local_idx]; \ + } while (0) + +#define CHECK_BR(depth) \ + do { \ + if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define CHECK_MEMORY() \ + do { \ + bh_assert(module->import_memory_count + module->memory_count > 0); \ + } while (0) static bool is_value_type(uint8 type) @@ -4445,7 +4484,6 @@ is_byte_a_type(uint8 type) return is_value_type(type) || (type == VALUE_TYPE_VOID); } - static bool wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, char *error_buf, uint32 error_buf_size) @@ -4458,10 +4496,10 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint16 cell_num; if (loader_ctx->csp_num < depth + 1) { - set_error_buf(error_buf, error_buf_size, - "unknown label, " - "unexpected end of section or function"); - return false; + set_error_buf(error_buf, error_buf_size, + "unknown label, " + "unexpected end of section or function"); + return false; } cur_block = loader_ctx->frame_csp - 1; @@ -4481,7 +4519,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, * and then re-push the values to make the stack top values * match block type. */ if (cur_block->is_stack_polymorphic) { - for (i = (int32)arity -1; i >= 0; i--) { + for (i = (int32)arity - 1; i >= 0; i--) { #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(types[i]); #endif @@ -4498,13 +4536,12 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, return true; } - available_stack_cell = (int32) - (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + available_stack_cell = + (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num); /* Check stack top values match target block type */ - for (i = (int32)arity -1; i >= 0; i--) { - if (!check_stack_top_values(frame_ref, available_stack_cell, - types[i], + for (i = (int32)arity - 1; i >= 0; i--) { + if (!check_stack_top_values(frame_ref, available_stack_cell, types[i], error_buf, error_buf_size)) return false; cell_num = wasm_value_type_cell_num(types[i]); @@ -4519,8 +4556,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, } static BranchBlock * -check_branch_block(WASMLoaderContext *loader_ctx, - uint8 **p_buf, uint8 *buf_end, +check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end, char *error_buf, uint32 error_buf_size) { uint8 *p = *p_buf, *p_end = buf_end; @@ -4550,19 +4586,18 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, int32 available_stack_cell, return_cell_num, i; uint8 *frame_ref = NULL; - available_stack_cell = (int32) - (loader_ctx->stack_cell_num - - block->stack_cell_num); + available_stack_cell = + (int32)(loader_ctx->stack_cell_num - block->stack_cell_num); return_count = block_type_get_result_types(block_type, &return_types); - return_cell_num = return_count > 0 ? - wasm_get_cell_num(return_types, return_count) : 0; + return_cell_num = + return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0; /* If the stack is in polymorphic state, just clear the stack * and then re-push the values to make the stack top values * match block type. */ if (block->is_stack_polymorphic) { - for (i = (int32)return_count -1; i >= 0; i--) { + for (i = (int32)return_count - 1; i >= 0; i--) { #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(return_types[i]); #endif @@ -4588,10 +4623,9 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, /* Check stack values match return types */ frame_ref = loader_ctx->frame_ref; - for (i = (int32)return_count -1; i >= 0; i--) { + for (i = (int32)return_count - 1; i >= 0; i--) { if (!check_stack_top_values(frame_ref, available_stack_cell, - return_types[i], - error_buf, error_buf_size)) + return_types[i], error_buf, error_buf_size)) return false; frame_ref -= wasm_value_type_cell_num(return_types[i]); available_stack_cell -= wasm_value_type_cell_num(return_types[i]); @@ -4617,7 +4651,7 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, */ static bool copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, - char* error_buf, uint32 error_buf_size) + char *error_buf, uint32 error_buf_size) { int16 *frame_offset = NULL; uint8 *cells = NULL, cell; @@ -4632,8 +4666,7 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, bool disable_emit = false; int16 operand_offset = 0; - uint64 size = (uint64)param_count * (sizeof(*cells) - + sizeof(*src_offsets)); + uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets)); /* For if block, we also need copy the condition operand offset. */ if (is_if_block) @@ -4669,9 +4702,8 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, /* Part a) */ emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count); /* Part b) */ - emit_uint32(loader_ctx, is_if_block ? - wasm_type->param_cell_num + 1 : - wasm_type->param_cell_num); + emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1 + : wasm_type->param_cell_num); /* Part c) */ for (i = 0; i < param_count; i++) emit_byte(loader_ctx, cells[i]); @@ -4703,44 +4735,46 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, /* reset the stack to the state of before entering the last block */ #if WASM_ENABLE_FAST_INTERP != 0 -#define RESET_STACK() do { \ - loader_ctx->stack_cell_num = \ - (loader_ctx->frame_csp - 1)->stack_cell_num; \ - loader_ctx->frame_ref = \ - loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ - loader_ctx->frame_offset = \ - loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \ -} while (0) +#define RESET_STACK() \ + do { \ + loader_ctx->stack_cell_num = \ + (loader_ctx->frame_csp - 1)->stack_cell_num; \ + loader_ctx->frame_ref = \ + loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ + loader_ctx->frame_offset = \ + loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \ + } while (0) #else -#define RESET_STACK() do { \ - loader_ctx->stack_cell_num = \ - (loader_ctx->frame_csp - 1)->stack_cell_num; \ - loader_ctx->frame_ref = \ - loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ -} while (0) +#define RESET_STACK() \ + do { \ + loader_ctx->stack_cell_num = \ + (loader_ctx->frame_csp - 1)->stack_cell_num; \ + loader_ctx->frame_ref = \ + loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ + } while (0) #endif /* set current block's stack polymorphic state */ -#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) do { \ - BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ - cur_block->is_stack_polymorphic = flag; \ -} while (0) +#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \ + do { \ + BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ + cur_block->is_stack_polymorphic = flag; \ + } while (0) #define BLOCK_HAS_PARAM(block_type) \ (!block_type.is_value_type && block_type.u.type->param_count > 0) -#define PRESERVE_LOCAL_FOR_BLOCK() do { \ - if (!(preserve_local_for_block(loader_ctx, opcode, \ - error_buf, error_buf_size))) { \ - goto fail; \ - } \ -} while (0) +#define PRESERVE_LOCAL_FOR_BLOCK() \ + do { \ + if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \ + error_buf_size))) { \ + goto fail; \ + } \ + } while (0) static bool -wasm_loader_prepare_bytecode(WASMModule *module, - WASMFunction *func, - uint32 cur_func_idx, - char *error_buf, +wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, + uint32 cur_func_idx, char *error_buf, uint32 error_buf_size) { uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org; @@ -4767,9 +4801,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, float64 f64; LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n", - func->param_cell_num, - func->local_cell_num, - func->ret_cell_num); + func->param_cell_num, func->local_cell_num, func->ret_cell_num); #endif global_count = module->import_global_count + module->global_count; @@ -4785,8 +4817,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, local_offsets = func->local_offsets; if (!(loader_ctx = wasm_loader_ctx_init(func))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); goto fail; } @@ -4794,8 +4825,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, re_scan: if (loader_ctx->code_compiled_size > 0) { if (!wasm_loader_ctx_reinit(loader_ctx)) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); goto fail; } p = func->code; @@ -4837,7 +4867,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 PRESERVE_LOCAL_FOR_BLOCK(); #endif -handle_op_block_and_loop: + handle_op_block_and_loop: { uint8 value_type; BlockType block_type; @@ -4858,12 +4888,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, bh_assert(type_index < module->type_count); block_type.is_value_type = false; block_type.u.type = module->types[type_index]; -#if WASM_ENABLE_FAST_INTERP == 0 \ - && WASM_ENABLE_WAMR_COMPILER == 0 \ +#if WASM_ENABLE_FAST_INTERP == 0 && WASM_ENABLE_WAMR_COMPILER == 0 \ && WASM_ENABLE_JIT == 0 /* If block use type index as block type, change the opcode - * to new extended opcode so that interpreter can resolve the - * block quickly. + * to new extended opcode so that interpreter can resolve + * the block quickly. */ *(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK); #endif @@ -4873,10 +4902,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, if (BLOCK_HAS_PARAM(block_type)) { WASMType *wasm_type = block_type.u.type; for (i = 0; i < block_type.u.type->param_count; i++) - POP_TYPE(wasm_type->types[wasm_type->param_count - i - 1]); + POP_TYPE( + wasm_type->types[wasm_type->param_count - i - 1]); } - PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), block_type, p); + PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), + block_type, p); /* Pass parameters to block */ if (BLOCK_HAS_PARAM(block_type)) { @@ -4892,14 +4923,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, skip_label(); if (BLOCK_HAS_PARAM(block_type)) { /* Make sure params are in dynamic space */ - if (!copy_params_to_dynamic_space(loader_ctx, - false, - error_buf, - error_buf_size)) + if (!copy_params_to_dynamic_space( + loader_ctx, false, error_buf, error_buf_size)) goto fail; } (loader_ctx->frame_csp - 1)->code_compiled = - loader_ctx->p_code_compiled; + loader_ctx->p_code_compiled; } else if (opcode == WASM_OP_IF) { /* If block has parameters, we should make sure they are in @@ -4909,8 +4938,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, * (func (export "params-id") (param i32) (result i32) * (i32.const 1) * (i32.const 2) - * (if (param i32 i32) (result i32 i32) (local.get 0) (then)) - * (i32.add) + * (if (param i32 i32) (result i32 i32) (local.get 0) + * (then)) (i32.add) * ) * * So we should emit a copy instruction before the if. @@ -4928,10 +4957,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, /* skip the if label */ skip_label(); /* Emit a copy instruction */ - if (!copy_params_to_dynamic_space(loader_ctx, - true, - error_buf, - error_buf_size)) + if (!copy_params_to_dynamic_space( + loader_ctx, true, error_buf, error_buf_size)) goto fail; /* Emit the if instruction */ @@ -4939,16 +4966,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, /* Emit the new condition operand offset */ POP_OFFSET_TYPE(VALUE_TYPE_I32); - /* Save top param_count values of frame_offset stack, so that - * we can recover it before executing else branch */ - size = sizeof(int16) * - (uint64)block_type.u.type->param_cell_num; - if (!(block->param_frame_offsets = - loader_malloc(size, error_buf, error_buf_size))) + /* Save top param_count values of frame_offset stack, so + * that we can recover it before executing else branch + */ + size = sizeof(int16) + * (uint64)block_type.u.type->param_cell_num; + if (!(block->param_frame_offsets = loader_malloc( + size, error_buf, error_buf_size))) goto fail; - bh_memcpy_s(block->param_frame_offsets, - (uint32)size, - loader_ctx->frame_offset - size/sizeof(int16), + bh_memcpy_s(block->param_frame_offsets, (uint32)size, + loader_ctx->frame_offset + - size / sizeof(int16), (uint32)size); } @@ -4964,7 +4992,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, BlockType block_type = (loader_ctx->frame_csp - 1)->block_type; bh_assert(loader_ctx->csp_num >= 2 && (loader_ctx->frame_csp - 1)->label_type - == LABEL_TYPE_IF); + == LABEL_TYPE_IF); /* check whether if branch's stack matches its result type */ if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1, @@ -4974,7 +5002,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, (loader_ctx->frame_csp - 1)->else_addr = p - 1; #if WASM_ENABLE_FAST_INTERP != 0 - /* if the result of if branch is in local or const area, add a copy op */ + /* if the result of if branch is in local or const area, add a + * copy op */ RESERVE_BLOCK_RET(); emit_empty_label_addr_and_frame_ip(PATCH_END); @@ -4994,11 +5023,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, if (BLOCK_HAS_PARAM((block_type))) { uint32 size; BranchBlock *block = loader_ctx->frame_csp - 1; - size = sizeof(int16) * - block_type.u.type->param_cell_num; + size = sizeof(int16) * block_type.u.type->param_cell_num; bh_memcpy_s(loader_ctx->frame_offset, size, block->param_frame_offsets, size); - loader_ctx->frame_offset += (size/sizeof(int16)); + loader_ctx->frame_offset += (size / sizeof(int16)); } #endif @@ -5010,11 +5038,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, BranchBlock *cur_block = loader_ctx->frame_csp - 1; /* check whether block stack matches its result type */ - if (!check_block_stack(loader_ctx, cur_block, - error_buf, error_buf_size)) + if (!check_block_stack(loader_ctx, cur_block, error_buf, + error_buf_size)) goto fail; - /* if no else branch, and return types do not match param types, fail */ + /* if no else branch, and return types do not match param types, + * fail */ if (cur_block->label_type == LABEL_TYPE_IF && !cur_block->else_addr) { uint32 param_count = 0, ret_count = 0; @@ -5032,9 +5061,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, param_types = block_type->u.type->types; ret_types = block_type->u.type->types + param_count; } - bh_assert(param_count == ret_count - && (!param_count - || !memcmp(param_types, ret_types, param_count))); + bh_assert( + param_count == ret_count + && (!param_count + || !memcmp(param_types, ret_types, param_count))); (void)ret_types; (void)ret_count; (void)param_types; @@ -5079,8 +5109,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, case WASM_OP_BR: { - if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, - error_buf, error_buf_size))) + if (!(frame_csp_tmp = check_branch_block( + loader_ctx, &p, p_end, error_buf, error_buf_size))) goto fail; RESET_STACK(); @@ -5092,8 +5122,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, { POP_I32(); - if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, - error_buf, error_buf_size))) + if (!(frame_csp_tmp = check_branch_block( + loader_ctx, &p, p_end, error_buf, error_buf_size))) goto fail; break; @@ -5112,8 +5142,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i <= count; i++) { if (!(frame_csp_tmp = - check_branch_block(loader_ctx, &p, p_end, - error_buf, error_buf_size))) + check_branch_block(loader_ctx, &p, p_end, + error_buf, error_buf_size))) goto fail; } @@ -5129,7 +5159,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, { int32 idx; uint8 ret_type; - for (idx = (int32)func->func_type->result_count - 1; idx >= 0; idx--) { + for (idx = (int32)func->func_type->result_count - 1; idx >= 0; + idx--) { ret_type = *(func->func_type->types + func->func_type->param_count + idx); POP_TYPE(ret_type); @@ -5161,16 +5192,20 @@ wasm_loader_prepare_bytecode(WASMModule *module, #endif bh_assert(func_idx < module->import_function_count - + module->function_count); + + module->function_count); if (func_idx < module->import_function_count) - func_type = module->import_functions[func_idx].u.function.func_type; - else func_type = - module->functions[func_idx - module->import_function_count]->func_type; + module->import_functions[func_idx].u.function.func_type; + else + func_type = module + ->functions[func_idx + - module->import_function_count] + ->func_type; if (func_type->param_count > 0) { - for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) { + for (idx = (int32)(func_type->param_count - 1); idx >= 0; + idx--) { POP_TYPE(func_type->types[idx]); #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(func_type->types[idx]); @@ -5184,20 +5219,24 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i < func_type->result_count; i++) { PUSH_TYPE(func_type->types[func_type->param_count + i]); #if WASM_ENABLE_FAST_INTERP != 0 - /* Here we emit each return value's dynamic_offset. But in fact - * these offsets are continuous, so interpreter only need to get - * the first return value's offset. + /* Here we emit each return value's dynamic_offset. But + * in fact these offsets are continuous, so interpreter + * only need to get the first return value's offset. */ - PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]); + PUSH_OFFSET_TYPE( + func_type->types[func_type->param_count + i]); #endif } #if WASM_ENABLE_TAIL_CALL != 0 } else { - bh_assert(func_type->result_count == func->func_type->result_count); + bh_assert(func_type->result_count + == func->func_type->result_count); for (i = 0; i < func_type->result_count; i++) { - bh_assert(func_type->types[func_type->param_count + i] == - func->func_type->types[func->func_type->param_count + i]); + bh_assert( + func_type->types[func_type->param_count + i] + == func->func_type + ->types[func->func_type->param_count + i]); } } #endif @@ -5214,8 +5253,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMType *func_type; uint32 type_idx, table_idx; - bh_assert(module->import_table_count - + module->table_count > 0); + bh_assert(module->import_table_count + module->table_count > 0); read_leb_uint32(p, p_end, type_idx); @@ -5237,7 +5275,6 @@ wasm_loader_prepare_bytecode(WASMModule *module, goto fail; } - #if WASM_ENABLE_FAST_INTERP != 0 /* we need to emit before arguments */ emit_uint32(loader_ctx, type_idx); @@ -5252,7 +5289,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, func_type = module->types[type_idx]; if (func_type->param_count > 0) { - for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) { + for (idx = (int32)(func_type->param_count - 1); idx >= 0; + idx--) { POP_TYPE(func_type->types[idx]); #if WASM_ENABLE_FAST_INTERP != 0 POP_OFFSET_TYPE(func_type->types[idx]); @@ -5266,16 +5304,20 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i < func_type->result_count; i++) { PUSH_TYPE(func_type->types[func_type->param_count + i]); #if WASM_ENABLE_FAST_INTERP != 0 - PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]); + PUSH_OFFSET_TYPE( + func_type->types[func_type->param_count + i]); #endif } #if WASM_ENABLE_TAIL_CALL != 0 } else { - bh_assert(func_type->result_count == func->func_type->result_count); + bh_assert(func_type->result_count + == func->func_type->result_count); for (i = 0; i < func_type->result_count; i++) { - bh_assert(func_type->types[func_type->param_count + i] == - func->func_type->types[func->func_type->param_count + i]); + bh_assert( + func_type->types[func_type->param_count + i] + == func->func_type + ->types[func->func_type->param_count + i]); } } #endif @@ -5288,8 +5330,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, case WASM_OP_DROP_64: { BranchBlock *cur_block = loader_ctx->frame_csp - 1; - int32 available_stack_cell = (int32) - (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + int32 available_stack_cell = + (int32)(loader_ctx->stack_cell_num + - cur_block->stack_cell_num); bh_assert(!(available_stack_cell <= 0 && !cur_block->is_stack_polymorphic)); @@ -5302,9 +5345,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); loader_ctx->frame_offset--; - if (*(loader_ctx->frame_offset) > - loader_ctx->start_dynamic_offset) - loader_ctx->dynamic_offset --; + if (*(loader_ctx->frame_offset) + > loader_ctx->start_dynamic_offset) + loader_ctx->dynamic_offset--; #endif } else { @@ -5316,8 +5359,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); loader_ctx->frame_offset -= 2; - if (*(loader_ctx->frame_offset) > - loader_ctx->start_dynamic_offset) + if (*(loader_ctx->frame_offset) + > loader_ctx->start_dynamic_offset) loader_ctx->dynamic_offset -= 2; #endif } @@ -5339,8 +5382,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_I32(); - available_stack_cell = (int32) - (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + available_stack_cell = (int32)(loader_ctx->stack_cell_num + - cur_block->stack_cell_num); bh_assert(!(available_stack_cell <= 0 && !cur_block->is_stack_polymorphic)); @@ -5362,21 +5405,24 @@ wasm_loader_prepare_bytecode(WASMModule *module, loader_ctx->p_code_compiled - 2; #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 - *(void**)(p_code_compiled_tmp - sizeof(void*)) = - handle_table[opcode_tmp]; + *(void **)(p_code_compiled_tmp + - sizeof(void *)) = + handle_table[opcode_tmp]; #else - int32 offset = (int32) - ((uint8*)handle_table[opcode_tmp] - - (uint8*)handle_table[0]); - if (!(offset >= INT16_MIN && offset < INT16_MAX)) { + int32 offset = + (int32)((uint8 *)handle_table[opcode_tmp] + - (uint8 *)handle_table[0]); + if (!(offset >= INT16_MIN + && offset < INT16_MAX)) { set_error_buf(error_buf, error_buf_size, - "pre-compiled label offset out of range"); + "pre-compiled label offset " + "out of range"); goto fail; } - *(int16*)(p_code_compiled_tmp - sizeof(int16)) = - (int16)offset; + *(int16 *)(p_code_compiled_tmp + - sizeof(int16)) = (int16)offset; #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 *(p_code_compiled_tmp - 1) = opcode_tmp; #else @@ -5449,21 +5495,20 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_LABELS_AS_VALUES != 0 #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 - *(void**)(p_code_compiled_tmp - sizeof(void*)) = - handle_table[opcode_tmp]; + *(void **)(p_code_compiled_tmp - sizeof(void *)) = + handle_table[opcode_tmp]; #else - int32 offset = (int32) - ((uint8*)handle_table[opcode_tmp] - - (uint8*)handle_table[0]); + int32 offset = (int32)((uint8 *)handle_table[opcode_tmp] + - (uint8 *)handle_table[0]); if (!(offset >= INT16_MIN && offset < INT16_MAX)) { set_error_buf(error_buf, error_buf_size, "pre-compiled label offset out of range"); goto fail; } - *(int16*)(p_code_compiled_tmp - sizeof(int16)) = - (int16)offset; + *(int16 *)(p_code_compiled_tmp - sizeof(int16)) = + (int16)offset; #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ -#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ +#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 *(p_code_compiled_tmp - 1) = opcode_tmp; #else @@ -5497,7 +5542,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, uint32 table_idx; if (!wasm_get_ref_types_flag()) { - goto fail;; + goto fail; + ; } read_leb_uint32(p, p_end, table_idx); @@ -5530,7 +5576,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, uint8 ref_type; if (!wasm_get_ref_types_flag()) { - goto fail;; + goto fail; + ; } CHECK_BUF(p, p_end, 1); @@ -5550,25 +5597,25 @@ wasm_loader_prepare_bytecode(WASMModule *module, case WASM_OP_REF_IS_NULL: { if (!wasm_get_ref_types_flag()) { - goto fail;; + goto fail; + ; } #if WASM_ENABLE_FAST_INTERP != 0 - if (!wasm_loader_pop_frame_ref_offset( - loader_ctx, VALUE_TYPE_FUNCREF, - error_buf, error_buf_size) + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, + VALUE_TYPE_FUNCREF, + error_buf, error_buf_size) && !wasm_loader_pop_frame_ref_offset( - loader_ctx, VALUE_TYPE_EXTERNREF, - error_buf, error_buf_size)) { + loader_ctx, VALUE_TYPE_EXTERNREF, error_buf, + error_buf_size)) { goto fail; } #else - if (!wasm_loader_pop_frame_ref( - loader_ctx, VALUE_TYPE_FUNCREF, - error_buf, error_buf_size) - && !wasm_loader_pop_frame_ref( - loader_ctx, VALUE_TYPE_EXTERNREF, - error_buf, error_buf_size)) { + if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, + error_buf, error_buf_size) + && !wasm_loader_pop_frame_ref(loader_ctx, + VALUE_TYPE_EXTERNREF, + error_buf, error_buf_size)) { goto fail; } #endif @@ -5579,7 +5626,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, { uint32 func_idx = 0; if (!wasm_get_ref_types_flag()) { - goto fail;; + goto fail; + ; } read_leb_uint32(p, p_end, func_idx); @@ -5598,8 +5646,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, for (i = 0; i < module->table_seg_count; i++, table_seg++) { if (table_seg->elem_type == VALUE_TYPE_FUNCREF && wasm_elem_is_declarative(table_seg->mode)) { - for (j =0; j < table_seg->function_count; j++) { - if (table_seg->func_indexes[j] == cur_func_idx) { + for (j = 0; j < table_seg->function_count; j++) { + if (table_seg->func_indexes[j] + == cur_func_idx) { func_declared = true; break; } @@ -5656,22 +5705,24 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_TYPE(local_type); #if WASM_ENABLE_FAST_INTERP != 0 - if (!(preserve_referenced_local(loader_ctx, opcode, local_offset, - local_type, &preserve_local, - error_buf, error_buf_size))) + if (!(preserve_referenced_local( + loader_ctx, opcode, local_offset, local_type, + &preserve_local, error_buf, error_buf_size))) goto fail; if (local_offset < 256) { skip_label(); if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) { if (loader_ctx->p_code_compiled) - STORE_U16(loader_ctx->p_code_compiled - 2, local_offset); - loader_ctx->frame_offset --; - loader_ctx->dynamic_offset --; + STORE_U16(loader_ctx->p_code_compiled - 2, + local_offset); + loader_ctx->frame_offset--; + loader_ctx->dynamic_offset--; } else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) { if (loader_ctx->p_code_compiled) - STORE_U16(loader_ctx->p_code_compiled - 2, local_offset); + STORE_U16(loader_ctx->p_code_compiled - 2, + local_offset); loader_ctx->frame_offset -= 2; loader_ctx->dynamic_offset -= 2; } @@ -5687,7 +5738,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, POP_OFFSET_TYPE(local_type); } } - else { /* local index larger than 255, reserve leb */ + else { /* local index larger than 255, reserve leb */ emit_uint32(loader_ctx, local_idx); POP_OFFSET_TYPE(local_type); } @@ -5713,8 +5764,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, GET_LOCAL_INDEX_TYPE_AND_OFFSET(); #if WASM_ENABLE_FAST_INTERP != 0 /* If the stack is in polymorphic state, do fake pop and push on - offset stack to keep the depth of offset stack to be the same - with ref stack */ + offset stack to keep the depth of offset stack to be the + same with ref stack */ BranchBlock *cur_block = loader_ctx->frame_csp - 1; if (cur_block->is_stack_polymorphic) { POP_OFFSET_TYPE(local_type); @@ -5725,9 +5776,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, PUSH_TYPE(local_type); #if WASM_ENABLE_FAST_INTERP != 0 - if (!(preserve_referenced_local(loader_ctx, opcode, local_offset, - local_type, &preserve_local, - error_buf, error_buf_size))) + if (!(preserve_referenced_local( + loader_ctx, opcode, local_offset, local_type, + &preserve_local, error_buf, error_buf_size))) goto fail; if (local_offset < 256) { @@ -5741,11 +5792,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, emit_byte(loader_ctx, (uint8)local_offset); } } - else { /* local index larger than 255, reserve leb */ + else { /* local index larger than 255, reserve leb */ emit_uint32(loader_ctx, local_idx); } - emit_operand(loader_ctx, *(loader_ctx->frame_offset - - wasm_value_type_cell_num(local_type))); + emit_operand(loader_ctx, + *(loader_ctx->frame_offset + - wasm_value_type_cell_num(local_type))); #else #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) if (local_offset < 0x80) { @@ -5769,10 +5821,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, bh_assert(global_idx < global_count); global_type = - global_idx < module->import_global_count - ? module->import_globals[global_idx].u.global.type - : module->globals[global_idx - module->import_global_count] - .type; + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.type + : module + ->globals[global_idx + - module->import_global_count] + .type; PUSH_TYPE(global_type); @@ -5783,7 +5837,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, *p_org = WASM_OP_GET_GLOBAL_64; } #endif -#else /* else of WASM_ENABLE_FAST_INTERP */ +#else /* else of WASM_ENABLE_FAST_INTERP */ if (is_64bit_type(global_type)) { skip_label(); emit_label(WASM_OP_GET_GLOBAL_64); @@ -5803,17 +5857,21 @@ wasm_loader_prepare_bytecode(WASMModule *module, bh_assert(global_idx < global_count); is_mutable = - global_idx < module->import_global_count - ? module->import_globals[global_idx].u.global.is_mutable - : module->globals[global_idx - module->import_global_count] - .is_mutable; + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.is_mutable + : module + ->globals[global_idx + - module->import_global_count] + .is_mutable; bh_assert(is_mutable); global_type = - global_idx < module->import_global_count - ? module->import_globals[global_idx].u.global.type - : module->globals[global_idx - module->import_global_count] - .type; + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.type + : module + ->globals[global_idx + - module->import_global_count] + .type; POP_TYPE(global_type); @@ -5827,7 +5885,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, *p_org = WASM_OP_SET_GLOBAL_AUX_STACK; } #endif -#else /* else of WASM_ENABLE_FAST_INTERP */ +#else /* else of WASM_ENABLE_FAST_INTERP */ if (is_64bit_type(global_type)) { skip_label(); emit_label(WASM_OP_SET_GLOBAL_64); @@ -5891,13 +5949,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, } #endif CHECK_MEMORY(); - read_leb_uint32(p, p_end, align); /* align */ + read_leb_uint32(p, p_end, align); /* align */ read_leb_uint32(p, p_end, mem_offset); /* offset */ #if WASM_ENABLE_FAST_INTERP != 0 emit_uint32(loader_ctx, mem_offset); #endif - switch (opcode) - { + switch (opcode) { /* load */ case WASM_OP_I32_LOAD: case WASM_OP_I32_LOAD8_S: @@ -5997,7 +6054,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); disable_emit = true; - bh_memcpy_s((uint8*)&f32, sizeof(float32), p_org, sizeof(float32)); + bh_memcpy_s((uint8 *)&f32, sizeof(float32), p_org, + sizeof(float32)); GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32); #endif PUSH_F32(); @@ -6009,7 +6067,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, skip_label(); disable_emit = true; /* Some MCU may require 8-byte align */ - bh_memcpy_s((uint8*)&f64, sizeof(float64), p_org, sizeof(float64)); + bh_memcpy_s((uint8 *)&f64, sizeof(float64), p_org, + sizeof(float64)); GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64); #endif PUSH_F64(); @@ -6248,227 +6307,236 @@ wasm_loader_prepare_bytecode(WASMModule *module, emit_byte(loader_ctx, ((uint8)opcode1)); #endif switch (opcode1) { - case WASM_OP_I32_TRUNC_SAT_S_F32: - case WASM_OP_I32_TRUNC_SAT_U_F32: - POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); - break; - case WASM_OP_I32_TRUNC_SAT_S_F64: - case WASM_OP_I32_TRUNC_SAT_U_F64: - POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); - break; - case WASM_OP_I64_TRUNC_SAT_S_F32: - case WASM_OP_I64_TRUNC_SAT_U_F32: - POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64); - break; - case WASM_OP_I64_TRUNC_SAT_S_F64: - case WASM_OP_I64_TRUNC_SAT_U_F64: - POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); - break; + case WASM_OP_I32_TRUNC_SAT_S_F32: + case WASM_OP_I32_TRUNC_SAT_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); + break; + case WASM_OP_I32_TRUNC_SAT_S_F64: + case WASM_OP_I32_TRUNC_SAT_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); + break; + case WASM_OP_I64_TRUNC_SAT_S_F32: + case WASM_OP_I64_TRUNC_SAT_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64); + break; + case WASM_OP_I64_TRUNC_SAT_S_F64: + case WASM_OP_I64_TRUNC_SAT_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); + break; #if WASM_ENABLE_BULK_MEMORY != 0 - case WASM_OP_MEMORY_INIT: - read_leb_uint32(p, p_end, segment_index); + case WASM_OP_MEMORY_INIT: + read_leb_uint32(p, p_end, segment_index); #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, segment_index); + emit_uint32(loader_ctx, segment_index); #endif - bh_assert(module->import_memory_count - + module->memory_count > 0); + bh_assert(module->import_memory_count + + module->memory_count + > 0); - bh_assert(*p == 0x00); - p++; + bh_assert(*p == 0x00); + p++; - bh_assert(segment_index < module->data_seg_count); - bh_assert(module->data_seg_count1 > 0); + bh_assert(segment_index < module->data_seg_count); + bh_assert(module->data_seg_count1 > 0); - POP_I32(); - POP_I32(); - POP_I32(); - break; - case WASM_OP_DATA_DROP: - read_leb_uint32(p, p_end, segment_index); + POP_I32(); + POP_I32(); + POP_I32(); + break; + case WASM_OP_DATA_DROP: + read_leb_uint32(p, p_end, segment_index); #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, segment_index); + emit_uint32(loader_ctx, segment_index); #endif - bh_assert(segment_index < module->data_seg_count); - bh_assert(module->data_seg_count1 > 0); - break; - case WASM_OP_MEMORY_COPY: - /* both src and dst memory index should be 0 */ - bh_assert(*(int16*)p != 0x0000); - p += 2; + bh_assert(segment_index < module->data_seg_count); + bh_assert(module->data_seg_count1 > 0); + break; + case WASM_OP_MEMORY_COPY: + /* both src and dst memory index should be 0 */ + bh_assert(*(int16 *)p != 0x0000); + p += 2; - bh_assert(module->import_memory_count - + module->memory_count > 0); + bh_assert(module->import_memory_count + + module->memory_count + > 0); - POP_I32(); - POP_I32(); - POP_I32(); - break; - case WASM_OP_MEMORY_FILL: - bh_assert(*p == 0); - p++; + POP_I32(); + POP_I32(); + POP_I32(); + break; + case WASM_OP_MEMORY_FILL: + bh_assert(*p == 0); + p++; - bh_assert(module->import_memory_count - + module->memory_count > 0); + bh_assert(module->import_memory_count + + module->memory_count + > 0); - POP_I32(); - POP_I32(); - POP_I32(); - break; + POP_I32(); + POP_I32(); + POP_I32(); + break; #endif /* WASM_ENABLE_BULK_MEMORY */ #if WASM_ENABLE_REF_TYPES != 0 - case WASM_OP_TABLE_INIT: - { - uint8 seg_ref_type, tbl_ref_type; - uint32 table_seg_idx, table_idx; - - if (!wasm_get_ref_types_flag()) { - goto fail; - } + case WASM_OP_TABLE_INIT: + { + uint8 seg_ref_type, tbl_ref_type; + uint32 table_seg_idx, table_idx; - read_leb_uint32(p, p_end, table_seg_idx); - read_leb_uint32(p, p_end, table_idx); + if (!wasm_get_ref_types_flag()) { + goto fail; + } - if (!get_table_elem_type(module, table_idx, &tbl_ref_type, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, table_seg_idx); + read_leb_uint32(p, p_end, table_idx); - if (!get_table_seg_elem_type(module, table_seg_idx, - &seg_ref_type, error_buf, + if (!get_table_elem_type(module, table_idx, + &tbl_ref_type, error_buf, error_buf_size)) - goto fail; + goto fail; - if (seg_ref_type != tbl_ref_type) { - set_error_buf(error_buf, error_buf_size, - "type mismatch"); - goto fail; - } + if (!get_table_seg_elem_type(module, table_seg_idx, + &seg_ref_type, error_buf, + error_buf_size)) + goto fail; + + if (seg_ref_type != tbl_ref_type) { + set_error_buf(error_buf, error_buf_size, + "type mismatch"); + goto fail; + } #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_seg_idx); - emit_uint32(loader_ctx, table_idx); + emit_uint32(loader_ctx, table_seg_idx); + emit_uint32(loader_ctx, table_idx); #endif - POP_I32(); - POP_I32(); - POP_I32(); - break; - } - case WASM_OP_ELEM_DROP: - { - uint32 table_seg_idx; - if (!wasm_get_ref_types_flag()) { - goto fail; + POP_I32(); + POP_I32(); + POP_I32(); + break; } + case WASM_OP_ELEM_DROP: + { + uint32 table_seg_idx; + if (!wasm_get_ref_types_flag()) { + goto fail; + } - read_leb_uint32(p, p_end, table_seg_idx); - if (!get_table_seg_elem_type(module, table_seg_idx, NULL, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, table_seg_idx); + if (!get_table_seg_elem_type(module, table_seg_idx, + NULL, error_buf, + error_buf_size)) + goto fail; #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_seg_idx); + emit_uint32(loader_ctx, table_seg_idx); #endif - break; - } - case WASM_OP_TABLE_COPY: - { - uint8 src_ref_type, dst_ref_type; - uint32 src_tbl_idx, dst_tbl_idx; - - if (!wasm_get_ref_types_flag()) { - goto fail; + break; } + case WASM_OP_TABLE_COPY: + { + uint8 src_ref_type, dst_ref_type; + uint32 src_tbl_idx, dst_tbl_idx; - read_leb_uint32(p, p_end, src_tbl_idx); - if (!get_table_elem_type(module, src_tbl_idx, &src_ref_type, - error_buf, error_buf_size)) - goto fail; + if (!wasm_get_ref_types_flag()) { + goto fail; + } - read_leb_uint32(p, p_end, dst_tbl_idx); - if (!get_table_elem_type(module, dst_tbl_idx, &dst_ref_type, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, src_tbl_idx); + if (!get_table_elem_type(module, src_tbl_idx, + &src_ref_type, error_buf, + error_buf_size)) + goto fail; - if (src_ref_type != dst_ref_type) { - set_error_buf(error_buf, error_buf_size, - "type mismatch"); - goto fail; - } + read_leb_uint32(p, p_end, dst_tbl_idx); + if (!get_table_elem_type(module, dst_tbl_idx, + &dst_ref_type, error_buf, + error_buf_size)) + goto fail; + + if (src_ref_type != dst_ref_type) { + set_error_buf(error_buf, error_buf_size, + "type mismatch"); + goto fail; + } #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, src_tbl_idx); - emit_uint32(loader_ctx, dst_tbl_idx); + emit_uint32(loader_ctx, src_tbl_idx); + emit_uint32(loader_ctx, dst_tbl_idx); #endif - POP_I32(); - POP_I32(); - POP_I32(); - break; - } - case WASM_OP_TABLE_SIZE: - { - uint32 table_idx; - if (!wasm_get_ref_types_flag()) { - goto fail; + POP_I32(); + POP_I32(); + POP_I32(); + break; } + case WASM_OP_TABLE_SIZE: + { + uint32 table_idx; + if (!wasm_get_ref_types_flag()) { + goto fail; + } - read_leb_uint32(p, p_end, table_idx); - /* TODO: shall we create a new function to check - table idx instead of using below function? */ - if (!get_table_elem_type(module, table_idx, NULL, - error_buf, error_buf_size)) - goto fail; + read_leb_uint32(p, p_end, table_idx); + /* TODO: shall we create a new function to check + table idx instead of using below function? */ + if (!get_table_elem_type(module, table_idx, NULL, + error_buf, error_buf_size)) + goto fail; #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_idx); + emit_uint32(loader_ctx, table_idx); #endif - PUSH_I32(); - break; - } - case WASM_OP_TABLE_GROW: - case WASM_OP_TABLE_FILL: - { - uint8 decl_ref_type; - uint32 table_idx; - - if (!wasm_get_ref_types_flag()) { - goto fail; + PUSH_I32(); + break; } + case WASM_OP_TABLE_GROW: + case WASM_OP_TABLE_FILL: + { + uint8 decl_ref_type; + uint32 table_idx; - read_leb_uint32(p, p_end, table_idx); - if (!get_table_elem_type(module, table_idx, - &decl_ref_type, error_buf, - error_buf_size)) - goto fail; - - if (opcode1 == WASM_OP_TABLE_GROW) { - if (table_idx < module->import_table_count) { - module->import_tables[table_idx] - .u.table.possible_grow = true; + if (!wasm_get_ref_types_flag()) { + goto fail; } - else { - module->tables[table_idx - module->import_table_count] - .possible_grow = true; + + read_leb_uint32(p, p_end, table_idx); + if (!get_table_elem_type(module, table_idx, + &decl_ref_type, error_buf, + error_buf_size)) + goto fail; + + if (opcode1 == WASM_OP_TABLE_GROW) { + if (table_idx < module->import_table_count) { + module->import_tables[table_idx] + .u.table.possible_grow = true; + } + else { + module + ->tables[table_idx + - module->import_table_count] + .possible_grow = true; + } } - } #if WASM_ENABLE_FAST_INTERP != 0 - emit_uint32(loader_ctx, table_idx); + emit_uint32(loader_ctx, table_idx); #endif - POP_I32(); + POP_I32(); #if WASM_ENABLE_FAST_INTERP != 0 - POP_OFFSET_TYPE(decl_ref_type); + POP_OFFSET_TYPE(decl_ref_type); #endif - POP_TYPE(decl_ref_type); - if (opcode1 == WASM_OP_TABLE_GROW) - PUSH_I32(); - else - POP_I32(); - break; - } + POP_TYPE(decl_ref_type); + if (opcode1 == WASM_OP_TABLE_GROW) + PUSH_I32(); + else + POP_I32(); + break; + } #endif /* WASM_ENABLE_REF_TYPES */ - default: - bh_assert(0); - break; + default: + bh_assert(0); + break; } break; } @@ -6482,7 +6550,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, #endif if (opcode != WASM_OP_ATOMIC_FENCE) { CHECK_MEMORY(); - read_leb_uint32(p, p_end, align); /* align */ + read_leb_uint32(p, p_end, align); /* align */ read_leb_uint32(p, p_end, mem_offset); /* offset */ #if WASM_ENABLE_FAST_INTERP != 0 emit_uint32(loader_ctx, mem_offset); @@ -6628,15 +6696,14 @@ wasm_loader_prepare_bytecode(WASMModule *module, func->const_cell_num = loader_ctx->const_cell_num; if (func->const_cell_num > 0 - && !(func->consts = func_const = - loader_malloc(func->const_cell_num * 4, - error_buf, error_buf_size))) { + && !(func->consts = func_const = loader_malloc( + func->const_cell_num * 4, error_buf, error_buf_size))) { goto fail; } func_const_end = func->consts + func->const_cell_num * 4; /* reverse the const buf */ for (int i = loader_ctx->num_const - 1; i >= 0; i--) { - Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const)); + Const *c = (Const *)(loader_ctx->const_buf + i * sizeof(Const)); if (c->value_type == VALUE_TYPE_F64 || c->value_type == VALUE_TYPE_I64) { bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), @@ -6650,8 +6717,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, } } - func->max_stack_cell_num = loader_ctx->preserved_local_offset - - loader_ctx->start_dynamic_offset + 1; + func->max_stack_cell_num = loader_ctx->preserved_local_offset + - loader_ctx->start_dynamic_offset + 1; #else func->max_stack_cell_num = loader_ctx->max_stack_cell_num; #endif diff --git a/core/iwasm/interpreter/wasm_opcode.h b/core/iwasm/interpreter/wasm_opcode.h index 294a87a824..f31d326268 100644 --- a/core/iwasm/interpreter/wasm_opcode.h +++ b/core/iwasm/interpreter/wasm_opcode.h @@ -14,673 +14,668 @@ extern "C" { typedef enum WASMOpcode { /* control instructions */ - WASM_OP_UNREACHABLE = 0x00, /* unreachable */ - WASM_OP_NOP = 0x01, /* nop */ - WASM_OP_BLOCK = 0x02, /* block */ - WASM_OP_LOOP = 0x03, /* loop */ - WASM_OP_IF = 0x04, /* if */ - WASM_OP_ELSE = 0x05, /* else */ - - WASM_OP_UNUSED_0x06 = 0x06, - WASM_OP_UNUSED_0x07 = 0x07, - WASM_OP_UNUSED_0x08 = 0x08, - WASM_OP_UNUSED_0x09 = 0x09, - WASM_OP_UNUSED_0x0a = 0x0a, - - WASM_OP_END = 0x0b, /* end */ - WASM_OP_BR = 0x0c, /* br */ - WASM_OP_BR_IF = 0x0d, /* br if */ - WASM_OP_BR_TABLE = 0x0e, /* br table */ - WASM_OP_RETURN = 0x0f, /* return */ - WASM_OP_CALL = 0x10, /* call */ - WASM_OP_CALL_INDIRECT = 0x11, /* call_indirect */ - WASM_OP_RETURN_CALL = 0x12, /* return_call */ + WASM_OP_UNREACHABLE = 0x00, /* unreachable */ + WASM_OP_NOP = 0x01, /* nop */ + WASM_OP_BLOCK = 0x02, /* block */ + WASM_OP_LOOP = 0x03, /* loop */ + WASM_OP_IF = 0x04, /* if */ + WASM_OP_ELSE = 0x05, /* else */ + + WASM_OP_UNUSED_0x06 = 0x06, + WASM_OP_UNUSED_0x07 = 0x07, + WASM_OP_UNUSED_0x08 = 0x08, + WASM_OP_UNUSED_0x09 = 0x09, + WASM_OP_UNUSED_0x0a = 0x0a, + + WASM_OP_END = 0x0b, /* end */ + WASM_OP_BR = 0x0c, /* br */ + WASM_OP_BR_IF = 0x0d, /* br if */ + WASM_OP_BR_TABLE = 0x0e, /* br table */ + WASM_OP_RETURN = 0x0f, /* return */ + WASM_OP_CALL = 0x10, /* call */ + WASM_OP_CALL_INDIRECT = 0x11, /* call_indirect */ + WASM_OP_RETURN_CALL = 0x12, /* return_call */ WASM_OP_RETURN_CALL_INDIRECT = 0x13, /* return_call_indirect */ - WASM_OP_UNUSED_0x14 = 0x14, - WASM_OP_UNUSED_0x15 = 0x15, - WASM_OP_UNUSED_0x16 = 0x16, - WASM_OP_UNUSED_0x17 = 0x17, - WASM_OP_UNUSED_0x18 = 0x18, - WASM_OP_UNUSED_0x19 = 0x19, + WASM_OP_UNUSED_0x14 = 0x14, + WASM_OP_UNUSED_0x15 = 0x15, + WASM_OP_UNUSED_0x16 = 0x16, + WASM_OP_UNUSED_0x17 = 0x17, + WASM_OP_UNUSED_0x18 = 0x18, + WASM_OP_UNUSED_0x19 = 0x19, /* parametric instructions */ - WASM_OP_DROP = 0x1a, /* drop */ - WASM_OP_SELECT = 0x1b, /* select */ - WASM_OP_SELECT_T = 0x1c, /* select t */ + WASM_OP_DROP = 0x1a, /* drop */ + WASM_OP_SELECT = 0x1b, /* select */ + WASM_OP_SELECT_T = 0x1c, /* select t */ WASM_OP_GET_GLOBAL_64 = 0x1d, WASM_OP_SET_GLOBAL_64 = 0x1e, WASM_OP_SET_GLOBAL_AUX_STACK = 0x1f, /* variable instructions */ - WASM_OP_GET_LOCAL = 0x20, /* get_local */ - WASM_OP_SET_LOCAL = 0x21, /* set_local */ - WASM_OP_TEE_LOCAL = 0x22, /* tee_local */ - WASM_OP_GET_GLOBAL = 0x23, /* get_global */ - WASM_OP_SET_GLOBAL = 0x24, /* set_global */ + WASM_OP_GET_LOCAL = 0x20, /* get_local */ + WASM_OP_SET_LOCAL = 0x21, /* set_local */ + WASM_OP_TEE_LOCAL = 0x22, /* tee_local */ + WASM_OP_GET_GLOBAL = 0x23, /* get_global */ + WASM_OP_SET_GLOBAL = 0x24, /* set_global */ - WASM_OP_TABLE_GET = 0x25, /* table.get */ - WASM_OP_TABLE_SET = 0x26, /* table.set */ - WASM_OP_UNUSED_0x27 = 0x27, + WASM_OP_TABLE_GET = 0x25, /* table.get */ + WASM_OP_TABLE_SET = 0x26, /* table.set */ + WASM_OP_UNUSED_0x27 = 0x27, /* memory instructions */ - WASM_OP_I32_LOAD = 0x28, /* i32.load */ - WASM_OP_I64_LOAD = 0x29, /* i64.load */ - WASM_OP_F32_LOAD = 0x2a, /* f32.load */ - WASM_OP_F64_LOAD = 0x2b, /* f64.load */ - WASM_OP_I32_LOAD8_S = 0x2c, /* i32.load8_s */ - WASM_OP_I32_LOAD8_U = 0x2d, /* i32.load8_u */ - WASM_OP_I32_LOAD16_S = 0x2e, /* i32.load16_s */ - WASM_OP_I32_LOAD16_U = 0x2f, /* i32.load16_u */ - WASM_OP_I64_LOAD8_S = 0x30, /* i64.load8_s */ - WASM_OP_I64_LOAD8_U = 0x31, /* i64.load8_u */ - WASM_OP_I64_LOAD16_S = 0x32, /* i64.load16_s */ - WASM_OP_I64_LOAD16_U = 0x33, /* i64.load16_u */ - WASM_OP_I64_LOAD32_S = 0x34, /* i32.load32_s */ - WASM_OP_I64_LOAD32_U = 0x35, /* i32.load32_u */ - WASM_OP_I32_STORE = 0x36, /* i32.store */ - WASM_OP_I64_STORE = 0x37, /* i64.store */ - WASM_OP_F32_STORE = 0x38, /* f32.store */ - WASM_OP_F64_STORE = 0x39, /* f64.store */ - WASM_OP_I32_STORE8 = 0x3a, /* i32.store8 */ - WASM_OP_I32_STORE16 = 0x3b, /* i32.store16 */ - WASM_OP_I64_STORE8 = 0x3c, /* i64.store8 */ - WASM_OP_I64_STORE16 = 0x3d, /* i64.sotre16 */ - WASM_OP_I64_STORE32 = 0x3e, /* i64.store32 */ - WASM_OP_MEMORY_SIZE = 0x3f, /* memory.size */ - WASM_OP_MEMORY_GROW = 0x40, /* memory.grow */ + WASM_OP_I32_LOAD = 0x28, /* i32.load */ + WASM_OP_I64_LOAD = 0x29, /* i64.load */ + WASM_OP_F32_LOAD = 0x2a, /* f32.load */ + WASM_OP_F64_LOAD = 0x2b, /* f64.load */ + WASM_OP_I32_LOAD8_S = 0x2c, /* i32.load8_s */ + WASM_OP_I32_LOAD8_U = 0x2d, /* i32.load8_u */ + WASM_OP_I32_LOAD16_S = 0x2e, /* i32.load16_s */ + WASM_OP_I32_LOAD16_U = 0x2f, /* i32.load16_u */ + WASM_OP_I64_LOAD8_S = 0x30, /* i64.load8_s */ + WASM_OP_I64_LOAD8_U = 0x31, /* i64.load8_u */ + WASM_OP_I64_LOAD16_S = 0x32, /* i64.load16_s */ + WASM_OP_I64_LOAD16_U = 0x33, /* i64.load16_u */ + WASM_OP_I64_LOAD32_S = 0x34, /* i32.load32_s */ + WASM_OP_I64_LOAD32_U = 0x35, /* i32.load32_u */ + WASM_OP_I32_STORE = 0x36, /* i32.store */ + WASM_OP_I64_STORE = 0x37, /* i64.store */ + WASM_OP_F32_STORE = 0x38, /* f32.store */ + WASM_OP_F64_STORE = 0x39, /* f64.store */ + WASM_OP_I32_STORE8 = 0x3a, /* i32.store8 */ + WASM_OP_I32_STORE16 = 0x3b, /* i32.store16 */ + WASM_OP_I64_STORE8 = 0x3c, /* i64.store8 */ + WASM_OP_I64_STORE16 = 0x3d, /* i64.sotre16 */ + WASM_OP_I64_STORE32 = 0x3e, /* i64.store32 */ + WASM_OP_MEMORY_SIZE = 0x3f, /* memory.size */ + WASM_OP_MEMORY_GROW = 0x40, /* memory.grow */ /* constant instructions */ - WASM_OP_I32_CONST = 0x41, /* i32.const */ - WASM_OP_I64_CONST = 0x42, /* i64.const */ - WASM_OP_F32_CONST = 0x43, /* f32.const */ - WASM_OP_F64_CONST = 0x44, /* f64.const */ + WASM_OP_I32_CONST = 0x41, /* i32.const */ + WASM_OP_I64_CONST = 0x42, /* i64.const */ + WASM_OP_F32_CONST = 0x43, /* f32.const */ + WASM_OP_F64_CONST = 0x44, /* f64.const */ /* comparison instructions */ - WASM_OP_I32_EQZ = 0x45, /* i32.eqz */ - WASM_OP_I32_EQ = 0x46, /* i32.eq */ - WASM_OP_I32_NE = 0x47, /* i32.ne */ - WASM_OP_I32_LT_S = 0x48, /* i32.lt_s */ - WASM_OP_I32_LT_U = 0x49, /* i32.lt_u */ - WASM_OP_I32_GT_S = 0x4a, /* i32.gt_s */ - WASM_OP_I32_GT_U = 0x4b, /* i32.gt_u */ - WASM_OP_I32_LE_S = 0x4c, /* i32.le_s */ - WASM_OP_I32_LE_U = 0x4d, /* i32.le_u */ - WASM_OP_I32_GE_S = 0x4e, /* i32.ge_s */ - WASM_OP_I32_GE_U = 0x4f, /* i32.ge_u */ - - WASM_OP_I64_EQZ = 0x50, /* i64.eqz */ - WASM_OP_I64_EQ = 0x51, /* i64.eq */ - WASM_OP_I64_NE = 0x52, /* i64.ne */ - WASM_OP_I64_LT_S = 0x53, /* i64.lt_s */ - WASM_OP_I64_LT_U = 0x54, /* i64.lt_u */ - WASM_OP_I64_GT_S = 0x55, /* i64.gt_s */ - WASM_OP_I64_GT_U = 0x56, /* i64.gt_u */ - WASM_OP_I64_LE_S = 0x57, /* i64.le_s */ - WASM_OP_I64_LE_U = 0x58, /* i64.le_u */ - WASM_OP_I64_GE_S = 0x59, /* i64.ge_s */ - WASM_OP_I64_GE_U = 0x5a, /* i64.ge_u */ - - WASM_OP_F32_EQ = 0x5b, /* f32.eq */ - WASM_OP_F32_NE = 0x5c, /* f32.ne */ - WASM_OP_F32_LT = 0x5d, /* f32.lt */ - WASM_OP_F32_GT = 0x5e, /* f32.gt */ - WASM_OP_F32_LE = 0x5f, /* f32.le */ - WASM_OP_F32_GE = 0x60, /* f32.ge */ - - WASM_OP_F64_EQ = 0x61, /* f64.eq */ - WASM_OP_F64_NE = 0x62, /* f64.ne */ - WASM_OP_F64_LT = 0x63, /* f64.lt */ - WASM_OP_F64_GT = 0x64, /* f64.gt */ - WASM_OP_F64_LE = 0x65, /* f64.le */ - WASM_OP_F64_GE = 0x66, /* f64.ge */ + WASM_OP_I32_EQZ = 0x45, /* i32.eqz */ + WASM_OP_I32_EQ = 0x46, /* i32.eq */ + WASM_OP_I32_NE = 0x47, /* i32.ne */ + WASM_OP_I32_LT_S = 0x48, /* i32.lt_s */ + WASM_OP_I32_LT_U = 0x49, /* i32.lt_u */ + WASM_OP_I32_GT_S = 0x4a, /* i32.gt_s */ + WASM_OP_I32_GT_U = 0x4b, /* i32.gt_u */ + WASM_OP_I32_LE_S = 0x4c, /* i32.le_s */ + WASM_OP_I32_LE_U = 0x4d, /* i32.le_u */ + WASM_OP_I32_GE_S = 0x4e, /* i32.ge_s */ + WASM_OP_I32_GE_U = 0x4f, /* i32.ge_u */ + + WASM_OP_I64_EQZ = 0x50, /* i64.eqz */ + WASM_OP_I64_EQ = 0x51, /* i64.eq */ + WASM_OP_I64_NE = 0x52, /* i64.ne */ + WASM_OP_I64_LT_S = 0x53, /* i64.lt_s */ + WASM_OP_I64_LT_U = 0x54, /* i64.lt_u */ + WASM_OP_I64_GT_S = 0x55, /* i64.gt_s */ + WASM_OP_I64_GT_U = 0x56, /* i64.gt_u */ + WASM_OP_I64_LE_S = 0x57, /* i64.le_s */ + WASM_OP_I64_LE_U = 0x58, /* i64.le_u */ + WASM_OP_I64_GE_S = 0x59, /* i64.ge_s */ + WASM_OP_I64_GE_U = 0x5a, /* i64.ge_u */ + + WASM_OP_F32_EQ = 0x5b, /* f32.eq */ + WASM_OP_F32_NE = 0x5c, /* f32.ne */ + WASM_OP_F32_LT = 0x5d, /* f32.lt */ + WASM_OP_F32_GT = 0x5e, /* f32.gt */ + WASM_OP_F32_LE = 0x5f, /* f32.le */ + WASM_OP_F32_GE = 0x60, /* f32.ge */ + + WASM_OP_F64_EQ = 0x61, /* f64.eq */ + WASM_OP_F64_NE = 0x62, /* f64.ne */ + WASM_OP_F64_LT = 0x63, /* f64.lt */ + WASM_OP_F64_GT = 0x64, /* f64.gt */ + WASM_OP_F64_LE = 0x65, /* f64.le */ + WASM_OP_F64_GE = 0x66, /* f64.ge */ /* numeric operators */ - WASM_OP_I32_CLZ = 0x67, /* i32.clz */ - WASM_OP_I32_CTZ = 0x68, /* i32.ctz */ - WASM_OP_I32_POPCNT = 0x69, /* i32.popcnt */ - WASM_OP_I32_ADD = 0x6a, /* i32.add */ - WASM_OP_I32_SUB = 0x6b, /* i32.sub */ - WASM_OP_I32_MUL = 0x6c, /* i32.mul */ - WASM_OP_I32_DIV_S = 0x6d, /* i32.div_s */ - WASM_OP_I32_DIV_U = 0x6e, /* i32.div_u */ - WASM_OP_I32_REM_S = 0x6f, /* i32.rem_s */ - WASM_OP_I32_REM_U = 0x70, /* i32.rem_u */ - WASM_OP_I32_AND = 0x71, /* i32.and */ - WASM_OP_I32_OR = 0x72, /* i32.or */ - WASM_OP_I32_XOR = 0x73, /* i32.xor */ - WASM_OP_I32_SHL = 0x74, /* i32.shl */ - WASM_OP_I32_SHR_S = 0x75, /* i32.shr_s */ - WASM_OP_I32_SHR_U = 0x76, /* i32.shr_u */ - WASM_OP_I32_ROTL = 0x77, /* i32.rotl */ - WASM_OP_I32_ROTR = 0x78, /* i32.rotr */ - - WASM_OP_I64_CLZ = 0x79, /* i64.clz */ - WASM_OP_I64_CTZ = 0x7a, /* i64.ctz */ - WASM_OP_I64_POPCNT = 0x7b, /* i64.popcnt */ - WASM_OP_I64_ADD = 0x7c, /* i64.add */ - WASM_OP_I64_SUB = 0x7d, /* i64.sub */ - WASM_OP_I64_MUL = 0x7e, /* i64.mul */ - WASM_OP_I64_DIV_S = 0x7f, /* i64.div_s */ - WASM_OP_I64_DIV_U = 0x80, /* i64.div_u */ - WASM_OP_I64_REM_S = 0x81, /* i64.rem_s */ - WASM_OP_I64_REM_U = 0x82, /* i64.rem_u */ - WASM_OP_I64_AND = 0x83, /* i64.and */ - WASM_OP_I64_OR = 0x84, /* i64.or */ - WASM_OP_I64_XOR = 0x85, /* i64.xor */ - WASM_OP_I64_SHL = 0x86, /* i64.shl */ - WASM_OP_I64_SHR_S = 0x87, /* i64.shr_s */ - WASM_OP_I64_SHR_U = 0x88, /* i64.shr_u */ - WASM_OP_I64_ROTL = 0x89, /* i64.rotl */ - WASM_OP_I64_ROTR = 0x8a, /* i64.rotr */ - - WASM_OP_F32_ABS = 0x8b, /* f32.abs */ - WASM_OP_F32_NEG = 0x8c, /* f32.neg */ - WASM_OP_F32_CEIL = 0x8d, /* f32.ceil */ - WASM_OP_F32_FLOOR = 0x8e, /* f32.floor */ - WASM_OP_F32_TRUNC = 0x8f, /* f32.trunc */ - WASM_OP_F32_NEAREST = 0x90, /* f32.nearest */ - WASM_OP_F32_SQRT = 0x91, /* f32.sqrt */ - WASM_OP_F32_ADD = 0x92, /* f32.add */ - WASM_OP_F32_SUB = 0x93, /* f32.sub */ - WASM_OP_F32_MUL = 0x94, /* f32.mul */ - WASM_OP_F32_DIV = 0x95, /* f32.div */ - WASM_OP_F32_MIN = 0x96, /* f32.min */ - WASM_OP_F32_MAX = 0x97, /* f32.max */ - WASM_OP_F32_COPYSIGN = 0x98, /* f32.copysign */ - - WASM_OP_F64_ABS = 0x99, /* f64.abs */ - WASM_OP_F64_NEG = 0x9a, /* f64.neg */ - WASM_OP_F64_CEIL = 0x9b, /* f64.ceil */ - WASM_OP_F64_FLOOR = 0x9c, /* f64.floor */ - WASM_OP_F64_TRUNC = 0x9d, /* f64.trunc */ - WASM_OP_F64_NEAREST = 0x9e, /* f64.nearest */ - WASM_OP_F64_SQRT = 0x9f, /* f64.sqrt */ - WASM_OP_F64_ADD = 0xa0, /* f64.add */ - WASM_OP_F64_SUB = 0xa1, /* f64.sub */ - WASM_OP_F64_MUL = 0xa2, /* f64.mul */ - WASM_OP_F64_DIV = 0xa3, /* f64.div */ - WASM_OP_F64_MIN = 0xa4, /* f64.min */ - WASM_OP_F64_MAX = 0xa5, /* f64.max */ - WASM_OP_F64_COPYSIGN = 0xa6, /* f64.copysign */ + WASM_OP_I32_CLZ = 0x67, /* i32.clz */ + WASM_OP_I32_CTZ = 0x68, /* i32.ctz */ + WASM_OP_I32_POPCNT = 0x69, /* i32.popcnt */ + WASM_OP_I32_ADD = 0x6a, /* i32.add */ + WASM_OP_I32_SUB = 0x6b, /* i32.sub */ + WASM_OP_I32_MUL = 0x6c, /* i32.mul */ + WASM_OP_I32_DIV_S = 0x6d, /* i32.div_s */ + WASM_OP_I32_DIV_U = 0x6e, /* i32.div_u */ + WASM_OP_I32_REM_S = 0x6f, /* i32.rem_s */ + WASM_OP_I32_REM_U = 0x70, /* i32.rem_u */ + WASM_OP_I32_AND = 0x71, /* i32.and */ + WASM_OP_I32_OR = 0x72, /* i32.or */ + WASM_OP_I32_XOR = 0x73, /* i32.xor */ + WASM_OP_I32_SHL = 0x74, /* i32.shl */ + WASM_OP_I32_SHR_S = 0x75, /* i32.shr_s */ + WASM_OP_I32_SHR_U = 0x76, /* i32.shr_u */ + WASM_OP_I32_ROTL = 0x77, /* i32.rotl */ + WASM_OP_I32_ROTR = 0x78, /* i32.rotr */ + + WASM_OP_I64_CLZ = 0x79, /* i64.clz */ + WASM_OP_I64_CTZ = 0x7a, /* i64.ctz */ + WASM_OP_I64_POPCNT = 0x7b, /* i64.popcnt */ + WASM_OP_I64_ADD = 0x7c, /* i64.add */ + WASM_OP_I64_SUB = 0x7d, /* i64.sub */ + WASM_OP_I64_MUL = 0x7e, /* i64.mul */ + WASM_OP_I64_DIV_S = 0x7f, /* i64.div_s */ + WASM_OP_I64_DIV_U = 0x80, /* i64.div_u */ + WASM_OP_I64_REM_S = 0x81, /* i64.rem_s */ + WASM_OP_I64_REM_U = 0x82, /* i64.rem_u */ + WASM_OP_I64_AND = 0x83, /* i64.and */ + WASM_OP_I64_OR = 0x84, /* i64.or */ + WASM_OP_I64_XOR = 0x85, /* i64.xor */ + WASM_OP_I64_SHL = 0x86, /* i64.shl */ + WASM_OP_I64_SHR_S = 0x87, /* i64.shr_s */ + WASM_OP_I64_SHR_U = 0x88, /* i64.shr_u */ + WASM_OP_I64_ROTL = 0x89, /* i64.rotl */ + WASM_OP_I64_ROTR = 0x8a, /* i64.rotr */ + + WASM_OP_F32_ABS = 0x8b, /* f32.abs */ + WASM_OP_F32_NEG = 0x8c, /* f32.neg */ + WASM_OP_F32_CEIL = 0x8d, /* f32.ceil */ + WASM_OP_F32_FLOOR = 0x8e, /* f32.floor */ + WASM_OP_F32_TRUNC = 0x8f, /* f32.trunc */ + WASM_OP_F32_NEAREST = 0x90, /* f32.nearest */ + WASM_OP_F32_SQRT = 0x91, /* f32.sqrt */ + WASM_OP_F32_ADD = 0x92, /* f32.add */ + WASM_OP_F32_SUB = 0x93, /* f32.sub */ + WASM_OP_F32_MUL = 0x94, /* f32.mul */ + WASM_OP_F32_DIV = 0x95, /* f32.div */ + WASM_OP_F32_MIN = 0x96, /* f32.min */ + WASM_OP_F32_MAX = 0x97, /* f32.max */ + WASM_OP_F32_COPYSIGN = 0x98, /* f32.copysign */ + + WASM_OP_F64_ABS = 0x99, /* f64.abs */ + WASM_OP_F64_NEG = 0x9a, /* f64.neg */ + WASM_OP_F64_CEIL = 0x9b, /* f64.ceil */ + WASM_OP_F64_FLOOR = 0x9c, /* f64.floor */ + WASM_OP_F64_TRUNC = 0x9d, /* f64.trunc */ + WASM_OP_F64_NEAREST = 0x9e, /* f64.nearest */ + WASM_OP_F64_SQRT = 0x9f, /* f64.sqrt */ + WASM_OP_F64_ADD = 0xa0, /* f64.add */ + WASM_OP_F64_SUB = 0xa1, /* f64.sub */ + WASM_OP_F64_MUL = 0xa2, /* f64.mul */ + WASM_OP_F64_DIV = 0xa3, /* f64.div */ + WASM_OP_F64_MIN = 0xa4, /* f64.min */ + WASM_OP_F64_MAX = 0xa5, /* f64.max */ + WASM_OP_F64_COPYSIGN = 0xa6, /* f64.copysign */ /* conversions */ - WASM_OP_I32_WRAP_I64 = 0xa7, /* i32.wrap/i64 */ - WASM_OP_I32_TRUNC_S_F32 = 0xa8, /* i32.trunc_s/f32 */ - WASM_OP_I32_TRUNC_U_F32 = 0xa9, /* i32.trunc_u/f32 */ - WASM_OP_I32_TRUNC_S_F64 = 0xaa, /* i32.trunc_s/f64 */ - WASM_OP_I32_TRUNC_U_F64 = 0xab, /* i32.trunc_u/f64 */ - - WASM_OP_I64_EXTEND_S_I32 = 0xac, /* i64.extend_s/i32 */ - WASM_OP_I64_EXTEND_U_I32 = 0xad, /* i64.extend_u/i32 */ - WASM_OP_I64_TRUNC_S_F32 = 0xae, /* i64.trunc_s/f32 */ - WASM_OP_I64_TRUNC_U_F32 = 0xaf, /* i64.trunc_u/f32 */ - WASM_OP_I64_TRUNC_S_F64 = 0xb0, /* i64.trunc_s/f64 */ - WASM_OP_I64_TRUNC_U_F64 = 0xb1, /* i64.trunc_u/f64 */ + WASM_OP_I32_WRAP_I64 = 0xa7, /* i32.wrap/i64 */ + WASM_OP_I32_TRUNC_S_F32 = 0xa8, /* i32.trunc_s/f32 */ + WASM_OP_I32_TRUNC_U_F32 = 0xa9, /* i32.trunc_u/f32 */ + WASM_OP_I32_TRUNC_S_F64 = 0xaa, /* i32.trunc_s/f64 */ + WASM_OP_I32_TRUNC_U_F64 = 0xab, /* i32.trunc_u/f64 */ + + WASM_OP_I64_EXTEND_S_I32 = 0xac, /* i64.extend_s/i32 */ + WASM_OP_I64_EXTEND_U_I32 = 0xad, /* i64.extend_u/i32 */ + WASM_OP_I64_TRUNC_S_F32 = 0xae, /* i64.trunc_s/f32 */ + WASM_OP_I64_TRUNC_U_F32 = 0xaf, /* i64.trunc_u/f32 */ + WASM_OP_I64_TRUNC_S_F64 = 0xb0, /* i64.trunc_s/f64 */ + WASM_OP_I64_TRUNC_U_F64 = 0xb1, /* i64.trunc_u/f64 */ WASM_OP_F32_CONVERT_S_I32 = 0xb2, /* f32.convert_s/i32 */ WASM_OP_F32_CONVERT_U_I32 = 0xb3, /* f32.convert_u/i32 */ WASM_OP_F32_CONVERT_S_I64 = 0xb4, /* f32.convert_s/i64 */ WASM_OP_F32_CONVERT_U_I64 = 0xb5, /* f32.convert_u/i64 */ - WASM_OP_F32_DEMOTE_F64 = 0xb6, /* f32.demote/f64 */ + WASM_OP_F32_DEMOTE_F64 = 0xb6, /* f32.demote/f64 */ WASM_OP_F64_CONVERT_S_I32 = 0xb7, /* f64.convert_s/i32 */ WASM_OP_F64_CONVERT_U_I32 = 0xb8, /* f64.convert_u/i32 */ WASM_OP_F64_CONVERT_S_I64 = 0xb9, /* f64.convert_s/i64 */ WASM_OP_F64_CONVERT_U_I64 = 0xba, /* f64.convert_u/i64 */ - WASM_OP_F64_PROMOTE_F32 = 0xbb, /* f64.promote/f32 */ + WASM_OP_F64_PROMOTE_F32 = 0xbb, /* f64.promote/f32 */ /* reinterpretations */ - WASM_OP_I32_REINTERPRET_F32 = 0xbc, /* i32.reinterpret/f32 */ - WASM_OP_I64_REINTERPRET_F64 = 0xbd, /* i64.reinterpret/f64 */ - WASM_OP_F32_REINTERPRET_I32 = 0xbe, /* f32.reinterpret/i32 */ - WASM_OP_F64_REINTERPRET_I64 = 0xbf, /* f64.reinterpret/i64 */ + WASM_OP_I32_REINTERPRET_F32 = 0xbc, /* i32.reinterpret/f32 */ + WASM_OP_I64_REINTERPRET_F64 = 0xbd, /* i64.reinterpret/f64 */ + WASM_OP_F32_REINTERPRET_I32 = 0xbe, /* f32.reinterpret/i32 */ + WASM_OP_F64_REINTERPRET_I64 = 0xbf, /* f64.reinterpret/i64 */ - WASM_OP_I32_EXTEND8_S = 0xc0, /* i32.extend8_s */ - WASM_OP_I32_EXTEND16_S = 0xc1, /* i32.extend16_s */ - WASM_OP_I64_EXTEND8_S = 0xc2, /* i64.extend8_s */ - WASM_OP_I64_EXTEND16_S = 0xc3, /* i64.extend16_s */ - WASM_OP_I64_EXTEND32_S = 0xc4, /* i64.extend32_s */ + WASM_OP_I32_EXTEND8_S = 0xc0, /* i32.extend8_s */ + WASM_OP_I32_EXTEND16_S = 0xc1, /* i32.extend16_s */ + WASM_OP_I64_EXTEND8_S = 0xc2, /* i64.extend8_s */ + WASM_OP_I64_EXTEND16_S = 0xc3, /* i64.extend16_s */ + WASM_OP_I64_EXTEND32_S = 0xc4, /* i64.extend32_s */ /* drop/select specified types*/ - WASM_OP_DROP_64 = 0xc5, - WASM_OP_SELECT_64 = 0xc6, + WASM_OP_DROP_64 = 0xc5, + WASM_OP_SELECT_64 = 0xc6, /* extend op code */ - EXT_OP_GET_LOCAL_FAST = 0xc7, - EXT_OP_SET_LOCAL_FAST_I64 = 0xc8, - EXT_OP_SET_LOCAL_FAST = 0xc9, - EXT_OP_TEE_LOCAL_FAST = 0xca, - EXT_OP_TEE_LOCAL_FAST_I64 = 0xcb, - EXT_OP_COPY_STACK_TOP = 0xcc, - EXT_OP_COPY_STACK_TOP_I64 = 0xcd, - EXT_OP_COPY_STACK_VALUES = 0xce, + EXT_OP_GET_LOCAL_FAST = 0xc7, + EXT_OP_SET_LOCAL_FAST_I64 = 0xc8, + EXT_OP_SET_LOCAL_FAST = 0xc9, + EXT_OP_TEE_LOCAL_FAST = 0xca, + EXT_OP_TEE_LOCAL_FAST_I64 = 0xcb, + EXT_OP_COPY_STACK_TOP = 0xcc, + EXT_OP_COPY_STACK_TOP_I64 = 0xcd, + EXT_OP_COPY_STACK_VALUES = 0xce, - WASM_OP_IMPDEP = 0xcf, + WASM_OP_IMPDEP = 0xcf, - WASM_OP_REF_NULL = 0xd0, /* ref.null */ - WASM_OP_REF_IS_NULL = 0xd1, /* ref.is_null */ - WASM_OP_REF_FUNC = 0xd2, /* ref.func */ + WASM_OP_REF_NULL = 0xd0, /* ref.null */ + WASM_OP_REF_IS_NULL = 0xd1, /* ref.is_null */ + WASM_OP_REF_FUNC = 0xd2, /* ref.func */ - EXT_OP_BLOCK = 0xd3, /* block with blocktype */ - EXT_OP_LOOP = 0xd4, /* loop with blocktype */ - EXT_OP_IF = 0xd5, /* if with blocktype */ + EXT_OP_BLOCK = 0xd3, /* block with blocktype */ + EXT_OP_LOOP = 0xd4, /* loop with blocktype */ + EXT_OP_IF = 0xd5, /* if with blocktype */ #if WASM_ENABLE_DEBUG_INTERP != 0 - DEBUG_OP_BREAK = 0xd6, /* debug break point */ + DEBUG_OP_BREAK = 0xd6, /* debug break point */ #endif /* Post-MVP extend op prefix */ - WASM_OP_MISC_PREFIX = 0xfc, - WASM_OP_SIMD_PREFIX = 0xfd, - WASM_OP_ATOMIC_PREFIX = 0xfe, + WASM_OP_MISC_PREFIX = 0xfc, + WASM_OP_SIMD_PREFIX = 0xfd, + WASM_OP_ATOMIC_PREFIX = 0xfe, } WASMOpcode; typedef enum WASMMiscEXTOpcode { - WASM_OP_I32_TRUNC_SAT_S_F32 = 0x00, - WASM_OP_I32_TRUNC_SAT_U_F32 = 0x01, - WASM_OP_I32_TRUNC_SAT_S_F64 = 0x02, - WASM_OP_I32_TRUNC_SAT_U_F64 = 0x03, - WASM_OP_I64_TRUNC_SAT_S_F32 = 0x04, - WASM_OP_I64_TRUNC_SAT_U_F32 = 0x05, - WASM_OP_I64_TRUNC_SAT_S_F64 = 0x06, - WASM_OP_I64_TRUNC_SAT_U_F64 = 0x07, - WASM_OP_MEMORY_INIT = 0x08, - WASM_OP_DATA_DROP = 0x09, - WASM_OP_MEMORY_COPY = 0x0a, - WASM_OP_MEMORY_FILL = 0x0b, - WASM_OP_TABLE_INIT = 0x0c, - WASM_OP_ELEM_DROP = 0x0d, - WASM_OP_TABLE_COPY = 0x0e, - WASM_OP_TABLE_GROW = 0x0f, - WASM_OP_TABLE_SIZE = 0x10, - WASM_OP_TABLE_FILL = 0x11, + WASM_OP_I32_TRUNC_SAT_S_F32 = 0x00, + WASM_OP_I32_TRUNC_SAT_U_F32 = 0x01, + WASM_OP_I32_TRUNC_SAT_S_F64 = 0x02, + WASM_OP_I32_TRUNC_SAT_U_F64 = 0x03, + WASM_OP_I64_TRUNC_SAT_S_F32 = 0x04, + WASM_OP_I64_TRUNC_SAT_U_F32 = 0x05, + WASM_OP_I64_TRUNC_SAT_S_F64 = 0x06, + WASM_OP_I64_TRUNC_SAT_U_F64 = 0x07, + WASM_OP_MEMORY_INIT = 0x08, + WASM_OP_DATA_DROP = 0x09, + WASM_OP_MEMORY_COPY = 0x0a, + WASM_OP_MEMORY_FILL = 0x0b, + WASM_OP_TABLE_INIT = 0x0c, + WASM_OP_ELEM_DROP = 0x0d, + WASM_OP_TABLE_COPY = 0x0e, + WASM_OP_TABLE_GROW = 0x0f, + WASM_OP_TABLE_SIZE = 0x10, + WASM_OP_TABLE_FILL = 0x11, } WASMMiscEXTOpcode; typedef enum WASMSimdEXTOpcode { /* memory instruction */ - SIMD_v128_load = 0x00, - SIMD_v128_load8x8_s = 0x01, - SIMD_v128_load8x8_u = 0x02, - SIMD_v128_load16x4_s = 0x03, - SIMD_v128_load16x4_u = 0x04, - SIMD_v128_load32x2_s = 0x05, - SIMD_v128_load32x2_u = 0x06, - SIMD_v128_load8_splat = 0x07, + SIMD_v128_load = 0x00, + SIMD_v128_load8x8_s = 0x01, + SIMD_v128_load8x8_u = 0x02, + SIMD_v128_load16x4_s = 0x03, + SIMD_v128_load16x4_u = 0x04, + SIMD_v128_load32x2_s = 0x05, + SIMD_v128_load32x2_u = 0x06, + SIMD_v128_load8_splat = 0x07, SIMD_v128_load16_splat = 0x08, SIMD_v128_load32_splat = 0x09, SIMD_v128_load64_splat = 0x0a, - SIMD_v128_store = 0x0b, + SIMD_v128_store = 0x0b, /* basic operation */ - SIMD_v128_const = 0x0c, - SIMD_v8x16_shuffle = 0x0d, - SIMD_v8x16_swizzle = 0x0e, + SIMD_v128_const = 0x0c, + SIMD_v8x16_shuffle = 0x0d, + SIMD_v8x16_swizzle = 0x0e, /* splat operation */ - SIMD_i8x16_splat = 0x0f, - SIMD_i16x8_splat = 0x10, - SIMD_i32x4_splat = 0x11, - SIMD_i64x2_splat = 0x12, - SIMD_f32x4_splat = 0x13, - SIMD_f64x2_splat = 0x14, + SIMD_i8x16_splat = 0x0f, + SIMD_i16x8_splat = 0x10, + SIMD_i32x4_splat = 0x11, + SIMD_i64x2_splat = 0x12, + SIMD_f32x4_splat = 0x13, + SIMD_f64x2_splat = 0x14, /* lane operation */ SIMD_i8x16_extract_lane_s = 0x15, SIMD_i8x16_extract_lane_u = 0x16, - SIMD_i8x16_replace_lane = 0x17, + SIMD_i8x16_replace_lane = 0x17, SIMD_i16x8_extract_lane_s = 0x18, SIMD_i16x8_extract_lane_u = 0x19, - SIMD_i16x8_replace_lane = 0x1a, - SIMD_i32x4_extract_lane = 0x1b, - SIMD_i32x4_replace_lane = 0x1c, - SIMD_i64x2_extract_lane = 0x1d, - SIMD_i64x2_replace_lane = 0x1e, - SIMD_f32x4_extract_lane = 0x1f, - SIMD_f32x4_replace_lane = 0x20, - SIMD_f64x2_extract_lane = 0x21, - SIMD_f64x2_replace_lane = 0x22, + SIMD_i16x8_replace_lane = 0x1a, + SIMD_i32x4_extract_lane = 0x1b, + SIMD_i32x4_replace_lane = 0x1c, + SIMD_i64x2_extract_lane = 0x1d, + SIMD_i64x2_replace_lane = 0x1e, + SIMD_f32x4_extract_lane = 0x1f, + SIMD_f32x4_replace_lane = 0x20, + SIMD_f64x2_extract_lane = 0x21, + SIMD_f64x2_replace_lane = 0x22, /* i8x16 compare operation */ - SIMD_i8x16_eq = 0x23, - SIMD_i8x16_ne = 0x24, - SIMD_i8x16_lt_s = 0x25, - SIMD_i8x16_lt_u = 0x26, - SIMD_i8x16_gt_s = 0x27, - SIMD_i8x16_gt_u = 0x28, - SIMD_i8x16_le_s = 0x29, - SIMD_i8x16_le_u = 0x2a, - SIMD_i8x16_ge_s = 0x2b, - SIMD_i8x16_ge_u = 0x2c, + SIMD_i8x16_eq = 0x23, + SIMD_i8x16_ne = 0x24, + SIMD_i8x16_lt_s = 0x25, + SIMD_i8x16_lt_u = 0x26, + SIMD_i8x16_gt_s = 0x27, + SIMD_i8x16_gt_u = 0x28, + SIMD_i8x16_le_s = 0x29, + SIMD_i8x16_le_u = 0x2a, + SIMD_i8x16_ge_s = 0x2b, + SIMD_i8x16_ge_u = 0x2c, /* i16x8 compare operation */ - SIMD_i16x8_eq = 0x2d, - SIMD_i16x8_ne = 0x2e, - SIMD_i16x8_lt_s = 0x2f, - SIMD_i16x8_lt_u = 0x30, - SIMD_i16x8_gt_s = 0x31, - SIMD_i16x8_gt_u = 0x32, - SIMD_i16x8_le_s = 0x33, - SIMD_i16x8_le_u = 0x34, - SIMD_i16x8_ge_s = 0x35, - SIMD_i16x8_ge_u = 0x36, + SIMD_i16x8_eq = 0x2d, + SIMD_i16x8_ne = 0x2e, + SIMD_i16x8_lt_s = 0x2f, + SIMD_i16x8_lt_u = 0x30, + SIMD_i16x8_gt_s = 0x31, + SIMD_i16x8_gt_u = 0x32, + SIMD_i16x8_le_s = 0x33, + SIMD_i16x8_le_u = 0x34, + SIMD_i16x8_ge_s = 0x35, + SIMD_i16x8_ge_u = 0x36, /* i32x4 compare operation */ - SIMD_i32x4_eq = 0x37, - SIMD_i32x4_ne = 0x38, - SIMD_i32x4_lt_s = 0x39, - SIMD_i32x4_lt_u = 0x3a, - SIMD_i32x4_gt_s = 0x3b, - SIMD_i32x4_gt_u = 0x3c, - SIMD_i32x4_le_s = 0x3d, - SIMD_i32x4_le_u = 0x3e, - SIMD_i32x4_ge_s = 0x3f, - SIMD_i32x4_ge_u = 0x40, + SIMD_i32x4_eq = 0x37, + SIMD_i32x4_ne = 0x38, + SIMD_i32x4_lt_s = 0x39, + SIMD_i32x4_lt_u = 0x3a, + SIMD_i32x4_gt_s = 0x3b, + SIMD_i32x4_gt_u = 0x3c, + SIMD_i32x4_le_s = 0x3d, + SIMD_i32x4_le_u = 0x3e, + SIMD_i32x4_ge_s = 0x3f, + SIMD_i32x4_ge_u = 0x40, /* f32x4 compare operation */ - SIMD_f32x4_eq = 0x41, - SIMD_f32x4_ne = 0x42, - SIMD_f32x4_lt = 0x43, - SIMD_f32x4_gt = 0x44, - SIMD_f32x4_le = 0x45, - SIMD_f32x4_ge = 0x46, + SIMD_f32x4_eq = 0x41, + SIMD_f32x4_ne = 0x42, + SIMD_f32x4_lt = 0x43, + SIMD_f32x4_gt = 0x44, + SIMD_f32x4_le = 0x45, + SIMD_f32x4_ge = 0x46, /* f64x2 compare operation */ - SIMD_f64x2_eq = 0x47, - SIMD_f64x2_ne = 0x48, - SIMD_f64x2_lt = 0x49, - SIMD_f64x2_gt = 0x4a, - SIMD_f64x2_le = 0x4b, - SIMD_f64x2_ge = 0x4c, + SIMD_f64x2_eq = 0x47, + SIMD_f64x2_ne = 0x48, + SIMD_f64x2_lt = 0x49, + SIMD_f64x2_gt = 0x4a, + SIMD_f64x2_le = 0x4b, + SIMD_f64x2_ge = 0x4c, /* v128 operation */ - SIMD_v128_not = 0x4d, - SIMD_v128_and = 0x4e, - SIMD_v128_andnot = 0x4f, - SIMD_v128_or = 0x50, - SIMD_v128_xor = 0x51, + SIMD_v128_not = 0x4d, + SIMD_v128_and = 0x4e, + SIMD_v128_andnot = 0x4f, + SIMD_v128_or = 0x50, + SIMD_v128_xor = 0x51, SIMD_v128_bitselect = 0x52, - SIMD_v128_any_true = 0x53, + SIMD_v128_any_true = 0x53, /* Load Lane Operation */ - SIMD_v128_load8_lane = 0x54, - SIMD_v128_load16_lane = 0x55, - SIMD_v128_load32_lane = 0x56, - SIMD_v128_load64_lane = 0x57, - SIMD_v128_store8_lane = 0x58, + SIMD_v128_load8_lane = 0x54, + SIMD_v128_load16_lane = 0x55, + SIMD_v128_load32_lane = 0x56, + SIMD_v128_load64_lane = 0x57, + SIMD_v128_store8_lane = 0x58, SIMD_v128_store16_lane = 0x59, SIMD_v128_store32_lane = 0x5a, SIMD_v128_store64_lane = 0x5b, - SIMD_v128_load32_zero = 0x5c, - SIMD_v128_load64_zero = 0x5d, + SIMD_v128_load32_zero = 0x5c, + SIMD_v128_load64_zero = 0x5d, /* Float conversion */ - SIMD_f32x4_demote_f64x2_zero = 0x5e, + SIMD_f32x4_demote_f64x2_zero = 0x5e, SIMD_f64x2_promote_low_f32x4_zero = 0x5f, /* i8x16 Operation */ - SIMD_i8x16_abs = 0x60, - SIMD_i8x16_neg = 0x61, - SIMD_i8x16_popcnt = 0x62, - SIMD_i8x16_all_true = 0x63, - SIMD_i8x16_bitmask = 0x64, + SIMD_i8x16_abs = 0x60, + SIMD_i8x16_neg = 0x61, + SIMD_i8x16_popcnt = 0x62, + SIMD_i8x16_all_true = 0x63, + SIMD_i8x16_bitmask = 0x64, SIMD_i8x16_narrow_i16x8_s = 0x65, SIMD_i8x16_narrow_i16x8_u = 0x66, - SIMD_f32x4_ceil = 0x67, - SIMD_f32x4_floor = 0x68, - SIMD_f32x4_trunc = 0x69, - SIMD_f32x4_nearest = 0x6a, - SIMD_i8x16_shl = 0x6b, - SIMD_i8x16_shr_s = 0x6c, - SIMD_i8x16_shr_u = 0x6d, - SIMD_i8x16_add = 0x6e, - SIMD_i8x16_add_sat_s = 0x6f, - SIMD_i8x16_add_sat_u = 0x70, - SIMD_i8x16_sub = 0x71, - SIMD_i8x16_sub_sat_s = 0x72, - SIMD_i8x16_sub_sat_u = 0x73, - SIMD_f64x2_ceil = 0x74, - SIMD_f64x2_floor = 0x75, - SIMD_i8x16_min_s = 0x76, - SIMD_i8x16_min_u = 0x77, - SIMD_i8x16_max_s = 0x78, - SIMD_i8x16_max_u = 0x79, - SIMD_f64x2_trunc = 0x7a, - SIMD_i8x16_avgr_u = 0x7b, + SIMD_f32x4_ceil = 0x67, + SIMD_f32x4_floor = 0x68, + SIMD_f32x4_trunc = 0x69, + SIMD_f32x4_nearest = 0x6a, + SIMD_i8x16_shl = 0x6b, + SIMD_i8x16_shr_s = 0x6c, + SIMD_i8x16_shr_u = 0x6d, + SIMD_i8x16_add = 0x6e, + SIMD_i8x16_add_sat_s = 0x6f, + SIMD_i8x16_add_sat_u = 0x70, + SIMD_i8x16_sub = 0x71, + SIMD_i8x16_sub_sat_s = 0x72, + SIMD_i8x16_sub_sat_u = 0x73, + SIMD_f64x2_ceil = 0x74, + SIMD_f64x2_floor = 0x75, + SIMD_i8x16_min_s = 0x76, + SIMD_i8x16_min_u = 0x77, + SIMD_i8x16_max_s = 0x78, + SIMD_i8x16_max_u = 0x79, + SIMD_f64x2_trunc = 0x7a, + SIMD_i8x16_avgr_u = 0x7b, SIMD_i16x8_extadd_pairwise_i8x16_s = 0x7c, SIMD_i16x8_extadd_pairwise_i8x16_u = 0x7d, SIMD_i32x4_extadd_pairwise_i16x8_s = 0x7e, SIMD_i32x4_extadd_pairwise_i16x8_u = 0x7f, /* i16x8 operation */ - SIMD_i16x8_abs = 0x80, - SIMD_i16x8_neg = 0x81, - SIMD_i16x8_q15mulr_sat_s = 0x82, - SIMD_i16x8_all_true = 0x83, - SIMD_i16x8_bitmask = 0x84, + SIMD_i16x8_abs = 0x80, + SIMD_i16x8_neg = 0x81, + SIMD_i16x8_q15mulr_sat_s = 0x82, + SIMD_i16x8_all_true = 0x83, + SIMD_i16x8_bitmask = 0x84, SIMD_i16x8_narrow_i32x4_s = 0x85, SIMD_i16x8_narrow_i32x4_u = 0x86, - SIMD_i16x8_extend_low_i8x16_s = 0x87, + SIMD_i16x8_extend_low_i8x16_s = 0x87, SIMD_i16x8_extend_high_i8x16_s = 0x88, - SIMD_i16x8_extend_low_i8x16_u = 0x89, + SIMD_i16x8_extend_low_i8x16_u = 0x89, SIMD_i16x8_extend_high_i8x16_u = 0x8a, - SIMD_i16x8_shl = 0x8b, - SIMD_i16x8_shr_s = 0x8c, - SIMD_i16x8_shr_u = 0x8d, - SIMD_i16x8_add = 0x8e, - SIMD_i16x8_add_sat_s = 0x8f, - SIMD_i16x8_add_sat_u = 0x90, - SIMD_i16x8_sub = 0x91, - SIMD_i16x8_sub_sat_s = 0x92, - SIMD_i16x8_sub_sat_u = 0x93, - SIMD_f64x2_nearest = 0x94, - SIMD_i16x8_mul = 0x95, - SIMD_i16x8_min_s = 0x96, - SIMD_i16x8_min_u = 0x97, - SIMD_i16x8_max_s = 0x98, - SIMD_i16x8_max_u = 0x99, + SIMD_i16x8_shl = 0x8b, + SIMD_i16x8_shr_s = 0x8c, + SIMD_i16x8_shr_u = 0x8d, + SIMD_i16x8_add = 0x8e, + SIMD_i16x8_add_sat_s = 0x8f, + SIMD_i16x8_add_sat_u = 0x90, + SIMD_i16x8_sub = 0x91, + SIMD_i16x8_sub_sat_s = 0x92, + SIMD_i16x8_sub_sat_u = 0x93, + SIMD_f64x2_nearest = 0x94, + SIMD_i16x8_mul = 0x95, + SIMD_i16x8_min_s = 0x96, + SIMD_i16x8_min_u = 0x97, + SIMD_i16x8_max_s = 0x98, + SIMD_i16x8_max_u = 0x99, /* placeholder = 0x9a */ - SIMD_i16x8_avgr_u = 0x9b, - SIMD_i16x8_extmul_low_i8x16_s = 0x9c, + SIMD_i16x8_avgr_u = 0x9b, + SIMD_i16x8_extmul_low_i8x16_s = 0x9c, SIMD_i16x8_extmul_high_i8x16_s = 0x9d, - SIMD_i16x8_extmul_low_i8x16_u = 0x9e, + SIMD_i16x8_extmul_low_i8x16_u = 0x9e, SIMD_i16x8_extmul_high_i8x16_u = 0x9f, /* i32x4 operation */ - SIMD_i32x4_abs = 0xa0, - SIMD_i32x4_neg = 0xa1, + SIMD_i32x4_abs = 0xa0, + SIMD_i32x4_neg = 0xa1, /* placeholder = 0xa2 */ - SIMD_i32x4_all_true = 0xa3, - SIMD_i32x4_bitmask = 0xa4, + SIMD_i32x4_all_true = 0xa3, + SIMD_i32x4_bitmask = 0xa4, SIMD_i32x4_narrow_i64x2_s = 0xa5, SIMD_i32x4_narrow_i64x2_u = 0xa6, - SIMD_i32x4_extend_low_i16x8_s = 0xa7, + SIMD_i32x4_extend_low_i16x8_s = 0xa7, SIMD_i32x4_extend_high_i16x8_s = 0xa8, - SIMD_i32x4_extend_low_i16x8_u = 0xa9, + SIMD_i32x4_extend_low_i16x8_u = 0xa9, SIMD_i32x4_extend_high_i16x8_u = 0xaa, - SIMD_i32x4_shl = 0xab, - 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, - SIMD_i32x4_sub = 0xb1, - SIMD_i32x4_sub_sat_s = 0xb2, - SIMD_i32x4_sub_sat_u = 0xb3, + SIMD_i32x4_shl = 0xab, + 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, + SIMD_i32x4_sub = 0xb1, + SIMD_i32x4_sub_sat_s = 0xb2, + SIMD_i32x4_sub_sat_u = 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, - SIMD_i32x4_extmul_low_i16x8_s = 0xbc, + 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, + SIMD_i32x4_extmul_low_i16x8_s = 0xbc, SIMD_i32x4_extmul_high_i16x8_s = 0xbd, - SIMD_i32x4_extmul_low_i16x8_u = 0xbe, + SIMD_i32x4_extmul_low_i16x8_u = 0xbe, SIMD_i32x4_extmul_high_i16x8_u = 0xbf, /* i64x2 operation */ - SIMD_i64x2_abs = 0xc0, - SIMD_i64x2_neg = 0xc1, + SIMD_i64x2_abs = 0xc0, + SIMD_i64x2_neg = 0xc1, /* placeholder = 0xc2 */ - SIMD_i64x2_all_true = 0xc3, - SIMD_i64x2_bitmask = 0xc4, + SIMD_i64x2_all_true = 0xc3, + SIMD_i64x2_bitmask = 0xc4, /* placeholder = 0xc5 */ /* placeholder = 0xc6 */ - SIMD_i64x2_extend_low_i32x4_s = 0xc7, + SIMD_i64x2_extend_low_i32x4_s = 0xc7, SIMD_i64x2_extend_high_i32x4_s = 0xc8, - SIMD_i64x2_extend_low_i32x4_u = 0xc9, + SIMD_i64x2_extend_low_i32x4_u = 0xc9, SIMD_i64x2_extend_high_i32x4_u = 0xca, - SIMD_i64x2_shl = 0xcb, - SIMD_i64x2_shr_s = 0xcc, - SIMD_i64x2_shr_u = 0xcd, - SIMD_i64x2_add = 0xce, + SIMD_i64x2_shl = 0xcb, + SIMD_i64x2_shr_s = 0xcc, + SIMD_i64x2_shr_u = 0xcd, + SIMD_i64x2_add = 0xce, /* placeholder = 0xcf */ /* placeholder = 0xd0 */ - SIMD_i64x2_sub = 0xd1, + SIMD_i64x2_sub = 0xd1, /* placeholder = 0xd2 */ /* placeholder = 0xd3 */ /* placeholder = 0xd4 */ - SIMD_i64x2_mul = 0xd5, - SIMD_i64x2_eq = 0xd6, - SIMD_i64x2_ne = 0xd7, - SIMD_i64x2_lt_s = 0xd8, - SIMD_i64x2_gt_s = 0xd9, - SIMD_i64x2_le_s = 0xda, - SIMD_i64x2_ge_s = 0xdb, - SIMD_i64x2_extmul_low_i32x4_s = 0xdc, + SIMD_i64x2_mul = 0xd5, + SIMD_i64x2_eq = 0xd6, + SIMD_i64x2_ne = 0xd7, + SIMD_i64x2_lt_s = 0xd8, + SIMD_i64x2_gt_s = 0xd9, + SIMD_i64x2_le_s = 0xda, + SIMD_i64x2_ge_s = 0xdb, + SIMD_i64x2_extmul_low_i32x4_s = 0xdc, SIMD_i64x2_extmul_high_i32x4_s = 0xdd, - SIMD_i64x2_extmul_low_i32x4_u = 0xde, + SIMD_i64x2_extmul_low_i32x4_u = 0xde, SIMD_i64x2_extmul_high_i32x4_u = 0xdf, /* f32x4 operation */ - SIMD_f32x4_abs = 0xe0, - SIMD_f32x4_neg = 0xe1, - SIMD_f32x4_round = 0xe2, - SIMD_f32x4_sqrt = 0xe3, - SIMD_f32x4_add = 0xe4, - SIMD_f32x4_sub = 0xe5, - SIMD_f32x4_mul = 0xe6, - SIMD_f32x4_div = 0xe7, - SIMD_f32x4_min = 0xe8, - SIMD_f32x4_max = 0xe9, - SIMD_f32x4_pmin = 0xea, - SIMD_f32x4_pmax = 0xeb, + SIMD_f32x4_abs = 0xe0, + SIMD_f32x4_neg = 0xe1, + SIMD_f32x4_round = 0xe2, + SIMD_f32x4_sqrt = 0xe3, + SIMD_f32x4_add = 0xe4, + SIMD_f32x4_sub = 0xe5, + SIMD_f32x4_mul = 0xe6, + SIMD_f32x4_div = 0xe7, + SIMD_f32x4_min = 0xe8, + SIMD_f32x4_max = 0xe9, + SIMD_f32x4_pmin = 0xea, + SIMD_f32x4_pmax = 0xeb, /* f64x2 operation */ - SIMD_f64x2_abs = 0xec, - SIMD_f64x2_neg = 0xed, - SIMD_f64x2_round = 0xee, - SIMD_f64x2_sqrt = 0xef, - SIMD_f64x2_add = 0xf0, - SIMD_f64x2_sub = 0xf1, - SIMD_f64x2_mul = 0xf2, - SIMD_f64x2_div = 0xf3, - SIMD_f64x2_min = 0xf4, - SIMD_f64x2_max = 0xf5, - SIMD_f64x2_pmin = 0xf6, - SIMD_f64x2_pmax = 0xf7, + SIMD_f64x2_abs = 0xec, + SIMD_f64x2_neg = 0xed, + SIMD_f64x2_round = 0xee, + SIMD_f64x2_sqrt = 0xef, + SIMD_f64x2_add = 0xf0, + SIMD_f64x2_sub = 0xf1, + SIMD_f64x2_mul = 0xf2, + SIMD_f64x2_div = 0xf3, + SIMD_f64x2_min = 0xf4, + SIMD_f64x2_max = 0xf5, + SIMD_f64x2_pmin = 0xf6, + SIMD_f64x2_pmax = 0xf7, /* conversion operation */ - SIMD_i32x4_trunc_sat_f32x4_s = 0xf8, - SIMD_i32x4_trunc_sat_f32x4_u = 0xf9, - SIMD_f32x4_convert_i32x4_s = 0xfa, - SIMD_f32x4_convert_i32x4_u = 0xfb, + SIMD_i32x4_trunc_sat_f32x4_s = 0xf8, + SIMD_i32x4_trunc_sat_f32x4_u = 0xf9, + SIMD_f32x4_convert_i32x4_s = 0xfa, + SIMD_f32x4_convert_i32x4_u = 0xfb, SIMD_i32x4_trunc_sat_f64x2_s_zero = 0xfc, SIMD_i32x4_trunc_sat_f64x2_u_zero = 0xfd, - SIMD_f64x2_convert_low_i32x4_s = 0xfe, - SIMD_f64x2_convert_low_i32x4_u = 0xff, + SIMD_f64x2_convert_low_i32x4_s = 0xfe, + SIMD_f64x2_convert_low_i32x4_u = 0xff, } WASMSimdEXTOpcode; typedef enum WASMAtomicEXTOpcode { /* atomic wait and notify */ - WASM_OP_ATOMIC_NOTIFY = 0x00, - WASM_OP_ATOMIC_WAIT32 = 0x01, - WASM_OP_ATOMIC_WAIT64 = 0x02, - WASM_OP_ATOMIC_FENCE = 0x03, + WASM_OP_ATOMIC_NOTIFY = 0x00, + WASM_OP_ATOMIC_WAIT32 = 0x01, + WASM_OP_ATOMIC_WAIT64 = 0x02, + WASM_OP_ATOMIC_FENCE = 0x03, /* atomic load and store */ - WASM_OP_ATOMIC_I32_LOAD = 0x10, - WASM_OP_ATOMIC_I64_LOAD = 0x11, - WASM_OP_ATOMIC_I32_LOAD8_U = 0x12, - WASM_OP_ATOMIC_I32_LOAD16_U = 0x13, - WASM_OP_ATOMIC_I64_LOAD8_U = 0x14, - WASM_OP_ATOMIC_I64_LOAD16_U = 0x15, - WASM_OP_ATOMIC_I64_LOAD32_U = 0x16, - WASM_OP_ATOMIC_I32_STORE = 0x17, - WASM_OP_ATOMIC_I64_STORE = 0x18, - WASM_OP_ATOMIC_I32_STORE8 = 0x19, - WASM_OP_ATOMIC_I32_STORE16 = 0x1a, - WASM_OP_ATOMIC_I64_STORE8 = 0x1b, - WASM_OP_ATOMIC_I64_STORE16 = 0x1c, - WASM_OP_ATOMIC_I64_STORE32 = 0x1d, + WASM_OP_ATOMIC_I32_LOAD = 0x10, + WASM_OP_ATOMIC_I64_LOAD = 0x11, + WASM_OP_ATOMIC_I32_LOAD8_U = 0x12, + WASM_OP_ATOMIC_I32_LOAD16_U = 0x13, + WASM_OP_ATOMIC_I64_LOAD8_U = 0x14, + WASM_OP_ATOMIC_I64_LOAD16_U = 0x15, + WASM_OP_ATOMIC_I64_LOAD32_U = 0x16, + WASM_OP_ATOMIC_I32_STORE = 0x17, + WASM_OP_ATOMIC_I64_STORE = 0x18, + WASM_OP_ATOMIC_I32_STORE8 = 0x19, + WASM_OP_ATOMIC_I32_STORE16 = 0x1a, + WASM_OP_ATOMIC_I64_STORE8 = 0x1b, + WASM_OP_ATOMIC_I64_STORE16 = 0x1c, + WASM_OP_ATOMIC_I64_STORE32 = 0x1d, /* atomic add */ - WASM_OP_ATOMIC_RMW_I32_ADD = 0x1e, - WASM_OP_ATOMIC_RMW_I64_ADD = 0x1f, - WASM_OP_ATOMIC_RMW_I32_ADD8_U = 0x20, - WASM_OP_ATOMIC_RMW_I32_ADD16_U = 0x21, - WASM_OP_ATOMIC_RMW_I64_ADD8_U = 0x22, - WASM_OP_ATOMIC_RMW_I64_ADD16_U = 0x23, - WASM_OP_ATOMIC_RMW_I64_ADD32_U = 0x24, + WASM_OP_ATOMIC_RMW_I32_ADD = 0x1e, + WASM_OP_ATOMIC_RMW_I64_ADD = 0x1f, + WASM_OP_ATOMIC_RMW_I32_ADD8_U = 0x20, + WASM_OP_ATOMIC_RMW_I32_ADD16_U = 0x21, + WASM_OP_ATOMIC_RMW_I64_ADD8_U = 0x22, + WASM_OP_ATOMIC_RMW_I64_ADD16_U = 0x23, + WASM_OP_ATOMIC_RMW_I64_ADD32_U = 0x24, /* atomic sub */ - WASM_OP_ATOMIC_RMW_I32_SUB = 0x25, - WASM_OP_ATOMIC_RMW_I64_SUB = 0x26, - WASM_OP_ATOMIC_RMW_I32_SUB8_U = 0x27, - WASM_OP_ATOMIC_RMW_I32_SUB16_U = 0x28, - WASM_OP_ATOMIC_RMW_I64_SUB8_U = 0x29, - WASM_OP_ATOMIC_RMW_I64_SUB16_U = 0x2a, - WASM_OP_ATOMIC_RMW_I64_SUB32_U = 0x2b, + WASM_OP_ATOMIC_RMW_I32_SUB = 0x25, + WASM_OP_ATOMIC_RMW_I64_SUB = 0x26, + WASM_OP_ATOMIC_RMW_I32_SUB8_U = 0x27, + WASM_OP_ATOMIC_RMW_I32_SUB16_U = 0x28, + WASM_OP_ATOMIC_RMW_I64_SUB8_U = 0x29, + WASM_OP_ATOMIC_RMW_I64_SUB16_U = 0x2a, + WASM_OP_ATOMIC_RMW_I64_SUB32_U = 0x2b, /* atomic and */ - WASM_OP_ATOMIC_RMW_I32_AND = 0x2c, - WASM_OP_ATOMIC_RMW_I64_AND = 0x2d, - WASM_OP_ATOMIC_RMW_I32_AND8_U = 0x2e, - WASM_OP_ATOMIC_RMW_I32_AND16_U = 0x2f, - WASM_OP_ATOMIC_RMW_I64_AND8_U = 0x30, - WASM_OP_ATOMIC_RMW_I64_AND16_U = 0x31, - WASM_OP_ATOMIC_RMW_I64_AND32_U = 0x32, + WASM_OP_ATOMIC_RMW_I32_AND = 0x2c, + WASM_OP_ATOMIC_RMW_I64_AND = 0x2d, + WASM_OP_ATOMIC_RMW_I32_AND8_U = 0x2e, + WASM_OP_ATOMIC_RMW_I32_AND16_U = 0x2f, + WASM_OP_ATOMIC_RMW_I64_AND8_U = 0x30, + WASM_OP_ATOMIC_RMW_I64_AND16_U = 0x31, + WASM_OP_ATOMIC_RMW_I64_AND32_U = 0x32, /* atomic or */ - WASM_OP_ATOMIC_RMW_I32_OR = 0x33, - WASM_OP_ATOMIC_RMW_I64_OR = 0x34, - WASM_OP_ATOMIC_RMW_I32_OR8_U = 0x35, - WASM_OP_ATOMIC_RMW_I32_OR16_U = 0x36, - WASM_OP_ATOMIC_RMW_I64_OR8_U = 0x37, - WASM_OP_ATOMIC_RMW_I64_OR16_U = 0x38, - WASM_OP_ATOMIC_RMW_I64_OR32_U = 0x39, + WASM_OP_ATOMIC_RMW_I32_OR = 0x33, + WASM_OP_ATOMIC_RMW_I64_OR = 0x34, + WASM_OP_ATOMIC_RMW_I32_OR8_U = 0x35, + WASM_OP_ATOMIC_RMW_I32_OR16_U = 0x36, + WASM_OP_ATOMIC_RMW_I64_OR8_U = 0x37, + WASM_OP_ATOMIC_RMW_I64_OR16_U = 0x38, + WASM_OP_ATOMIC_RMW_I64_OR32_U = 0x39, /* atomic xor */ - WASM_OP_ATOMIC_RMW_I32_XOR = 0x3a, - WASM_OP_ATOMIC_RMW_I64_XOR = 0x3b, - WASM_OP_ATOMIC_RMW_I32_XOR8_U = 0x3c, - WASM_OP_ATOMIC_RMW_I32_XOR16_U = 0x3d, - WASM_OP_ATOMIC_RMW_I64_XOR8_U = 0x3e, - WASM_OP_ATOMIC_RMW_I64_XOR16_U = 0x3f, - WASM_OP_ATOMIC_RMW_I64_XOR32_U = 0x40, + WASM_OP_ATOMIC_RMW_I32_XOR = 0x3a, + WASM_OP_ATOMIC_RMW_I64_XOR = 0x3b, + WASM_OP_ATOMIC_RMW_I32_XOR8_U = 0x3c, + WASM_OP_ATOMIC_RMW_I32_XOR16_U = 0x3d, + WASM_OP_ATOMIC_RMW_I64_XOR8_U = 0x3e, + WASM_OP_ATOMIC_RMW_I64_XOR16_U = 0x3f, + WASM_OP_ATOMIC_RMW_I64_XOR32_U = 0x40, /* atomic xchg */ - WASM_OP_ATOMIC_RMW_I32_XCHG = 0x41, - WASM_OP_ATOMIC_RMW_I64_XCHG = 0x42, - WASM_OP_ATOMIC_RMW_I32_XCHG8_U = 0x43, - WASM_OP_ATOMIC_RMW_I32_XCHG16_U = 0x44, - WASM_OP_ATOMIC_RMW_I64_XCHG8_U = 0x45, - WASM_OP_ATOMIC_RMW_I64_XCHG16_U = 0x46, - WASM_OP_ATOMIC_RMW_I64_XCHG32_U = 0x47, + WASM_OP_ATOMIC_RMW_I32_XCHG = 0x41, + WASM_OP_ATOMIC_RMW_I64_XCHG = 0x42, + WASM_OP_ATOMIC_RMW_I32_XCHG8_U = 0x43, + WASM_OP_ATOMIC_RMW_I32_XCHG16_U = 0x44, + WASM_OP_ATOMIC_RMW_I64_XCHG8_U = 0x45, + WASM_OP_ATOMIC_RMW_I64_XCHG16_U = 0x46, + WASM_OP_ATOMIC_RMW_I64_XCHG32_U = 0x47, /* atomic cmpxchg */ - WASM_OP_ATOMIC_RMW_I32_CMPXCHG = 0x48, - WASM_OP_ATOMIC_RMW_I64_CMPXCHG = 0x49, - WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U = 0x4a, - WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U = 0x4b, - WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U = 0x4c, - WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U = 0x4d, - WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U = 0x4e, + WASM_OP_ATOMIC_RMW_I32_CMPXCHG = 0x48, + WASM_OP_ATOMIC_RMW_I64_CMPXCHG = 0x49, + WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U = 0x4a, + WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U = 0x4b, + WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U = 0x4c, + WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U = 0x4d, + WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U = 0x4e, } WASMAtomicEXTOpcode; -#ifdef __cplusplus -} -#endif - #if WASM_ENABLE_DEBUG_INTERP != 0 -#define DEF_DEBUG_BREAK_HANDLE(_name) \ - _name[DEBUG_OP_BREAK] = \ - HANDLE_OPCODE (DEBUG_OP_BREAK); /* 0xd6 */ +#define DEF_DEBUG_BREAK_HANDLE(_name) \ + _name[DEBUG_OP_BREAK] = HANDLE_OPCODE(DEBUG_OP_BREAK); /* 0xd6 */ #else #define DEF_DEBUG_BREAK_HANDLE(_name) #endif @@ -690,229 +685,233 @@ typedef enum WASMAtomicEXTOpcode { */ #define WASM_INSTRUCTION_NUM 256 -#define DEFINE_GOTO_TABLE(type, _name) \ -static type _name[WASM_INSTRUCTION_NUM] = { \ - HANDLE_OPCODE (WASM_OP_UNREACHABLE), /* 0x00 */ \ - HANDLE_OPCODE (WASM_OP_NOP), /* 0x01 */ \ - HANDLE_OPCODE (WASM_OP_BLOCK), /* 0x02 */ \ - HANDLE_OPCODE (WASM_OP_LOOP), /* 0x03 */ \ - HANDLE_OPCODE (WASM_OP_IF), /* 0x04 */ \ - HANDLE_OPCODE (WASM_OP_ELSE), /* 0x05 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x06), /* 0x06 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x07), /* 0x07 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x08), /* 0x08 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x09), /* 0x09 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x0a), /* 0x0a */ \ - HANDLE_OPCODE (WASM_OP_END), /* 0x0b */ \ - HANDLE_OPCODE (WASM_OP_BR), /* 0x0c */ \ - HANDLE_OPCODE (WASM_OP_BR_IF), /* 0x0d */ \ - HANDLE_OPCODE (WASM_OP_BR_TABLE), /* 0x0e */ \ - HANDLE_OPCODE (WASM_OP_RETURN), /* 0x0f */ \ - HANDLE_OPCODE (WASM_OP_CALL), /* 0x10 */ \ - HANDLE_OPCODE (WASM_OP_CALL_INDIRECT), /* 0x11 */ \ - HANDLE_OPCODE (WASM_OP_RETURN_CALL), /* 0x12 */ \ - HANDLE_OPCODE (WASM_OP_RETURN_CALL_INDIRECT), /* 0x13 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x14), /* 0x14 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x15), /* 0x15 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x16), /* 0x16 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x17), /* 0x17 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x18), /* 0x18 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x19), /* 0x19 */ \ - HANDLE_OPCODE (WASM_OP_DROP), /* 0x1a */ \ - HANDLE_OPCODE (WASM_OP_SELECT), /* 0x1b */ \ - HANDLE_OPCODE (WASM_OP_SELECT_T), /* 0x1c */ \ - HANDLE_OPCODE (WASM_OP_GET_GLOBAL_64), /* 0x1d */ \ - HANDLE_OPCODE (WASM_OP_SET_GLOBAL_64), /* 0x1e */ \ - HANDLE_OPCODE (WASM_OP_SET_GLOBAL_AUX_STACK), /* 0x1f */ \ - HANDLE_OPCODE (WASM_OP_GET_LOCAL), /* 0x20 */ \ - HANDLE_OPCODE (WASM_OP_SET_LOCAL), /* 0x21 */ \ - HANDLE_OPCODE (WASM_OP_TEE_LOCAL), /* 0x22 */ \ - HANDLE_OPCODE (WASM_OP_GET_GLOBAL), /* 0x23 */ \ - HANDLE_OPCODE (WASM_OP_SET_GLOBAL), /* 0x24 */ \ - HANDLE_OPCODE (WASM_OP_TABLE_GET), /* 0x25 */ \ - HANDLE_OPCODE (WASM_OP_TABLE_SET), /* 0x26 */ \ - HANDLE_OPCODE (WASM_OP_UNUSED_0x27), /* 0x27 */ \ - HANDLE_OPCODE (WASM_OP_I32_LOAD), /* 0x28 */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD), /* 0x29 */ \ - HANDLE_OPCODE (WASM_OP_F32_LOAD), /* 0x2a */ \ - HANDLE_OPCODE (WASM_OP_F64_LOAD), /* 0x2b */ \ - HANDLE_OPCODE (WASM_OP_I32_LOAD8_S), /* 0x2c */ \ - HANDLE_OPCODE (WASM_OP_I32_LOAD8_U), /* 0x2d */ \ - HANDLE_OPCODE (WASM_OP_I32_LOAD16_S), /* 0x2e */ \ - HANDLE_OPCODE (WASM_OP_I32_LOAD16_U), /* 0x2f */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD8_S), /* 0x30 */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD8_U), /* 0x31 */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD16_S), /* 0x32 */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD16_U), /* 0x33 */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD32_S), /* 0x34 */ \ - HANDLE_OPCODE (WASM_OP_I64_LOAD32_U), /* 0x35 */ \ - HANDLE_OPCODE (WASM_OP_I32_STORE), /* 0x36 */ \ - HANDLE_OPCODE (WASM_OP_I64_STORE), /* 0x37 */ \ - HANDLE_OPCODE (WASM_OP_F32_STORE), /* 0x38 */ \ - HANDLE_OPCODE (WASM_OP_F64_STORE), /* 0x39 */ \ - HANDLE_OPCODE (WASM_OP_I32_STORE8), /* 0x3a */ \ - HANDLE_OPCODE (WASM_OP_I32_STORE16), /* 0x3b */ \ - HANDLE_OPCODE (WASM_OP_I64_STORE8), /* 0x3c */ \ - HANDLE_OPCODE (WASM_OP_I64_STORE16), /* 0x3d */ \ - HANDLE_OPCODE (WASM_OP_I64_STORE32), /* 0x3e */ \ - HANDLE_OPCODE (WASM_OP_MEMORY_SIZE), /* 0x3f */ \ - HANDLE_OPCODE (WASM_OP_MEMORY_GROW), /* 0x40 */ \ - HANDLE_OPCODE (WASM_OP_I32_CONST), /* 0x41 */ \ - HANDLE_OPCODE (WASM_OP_I64_CONST), /* 0x42 */ \ - HANDLE_OPCODE (WASM_OP_F32_CONST), /* 0x43 */ \ - HANDLE_OPCODE (WASM_OP_F64_CONST), /* 0x44 */ \ - HANDLE_OPCODE (WASM_OP_I32_EQZ), /* 0x45 */ \ - HANDLE_OPCODE (WASM_OP_I32_EQ), /* 0x46 */ \ - HANDLE_OPCODE (WASM_OP_I32_NE), /* 0x47 */ \ - HANDLE_OPCODE (WASM_OP_I32_LT_S), /* 0x48 */ \ - HANDLE_OPCODE (WASM_OP_I32_LT_U), /* 0x49 */ \ - HANDLE_OPCODE (WASM_OP_I32_GT_S), /* 0x4a */ \ - HANDLE_OPCODE (WASM_OP_I32_GT_U), /* 0x4b */ \ - HANDLE_OPCODE (WASM_OP_I32_LE_S), /* 0x4c */ \ - HANDLE_OPCODE (WASM_OP_I32_LE_U), /* 0x4d */ \ - HANDLE_OPCODE (WASM_OP_I32_GE_S), /* 0x4e */ \ - HANDLE_OPCODE (WASM_OP_I32_GE_U), /* 0x4f */ \ - HANDLE_OPCODE (WASM_OP_I64_EQZ), /* 0x50 */ \ - HANDLE_OPCODE (WASM_OP_I64_EQ), /* 0x51 */ \ - HANDLE_OPCODE (WASM_OP_I64_NE), /* 0x52 */ \ - HANDLE_OPCODE (WASM_OP_I64_LT_S), /* 0x53 */ \ - HANDLE_OPCODE (WASM_OP_I64_LT_U), /* 0x54 */ \ - HANDLE_OPCODE (WASM_OP_I64_GT_S), /* 0x55 */ \ - HANDLE_OPCODE (WASM_OP_I64_GT_U), /* 0x56 */ \ - HANDLE_OPCODE (WASM_OP_I64_LE_S), /* 0x57 */ \ - HANDLE_OPCODE (WASM_OP_I64_LE_U), /* 0x58 */ \ - HANDLE_OPCODE (WASM_OP_I64_GE_S), /* 0x59 */ \ - HANDLE_OPCODE (WASM_OP_I64_GE_U), /* 0x5a */ \ - HANDLE_OPCODE (WASM_OP_F32_EQ), /* 0x5b */ \ - HANDLE_OPCODE (WASM_OP_F32_NE), /* 0x5c */ \ - HANDLE_OPCODE (WASM_OP_F32_LT), /* 0x5d */ \ - HANDLE_OPCODE (WASM_OP_F32_GT), /* 0x5e */ \ - HANDLE_OPCODE (WASM_OP_F32_LE), /* 0x5f */ \ - HANDLE_OPCODE (WASM_OP_F32_GE), /* 0x60 */ \ - HANDLE_OPCODE (WASM_OP_F64_EQ), /* 0x61 */ \ - HANDLE_OPCODE (WASM_OP_F64_NE), /* 0x62 */ \ - HANDLE_OPCODE (WASM_OP_F64_LT), /* 0x63 */ \ - HANDLE_OPCODE (WASM_OP_F64_GT), /* 0x64 */ \ - HANDLE_OPCODE (WASM_OP_F64_LE), /* 0x65 */ \ - HANDLE_OPCODE (WASM_OP_F64_GE), /* 0x66 */ \ - HANDLE_OPCODE (WASM_OP_I32_CLZ), /* 0x67 */ \ - HANDLE_OPCODE (WASM_OP_I32_CTZ), /* 0x68 */ \ - HANDLE_OPCODE (WASM_OP_I32_POPCNT), /* 0x69 */ \ - HANDLE_OPCODE (WASM_OP_I32_ADD), /* 0x6a */ \ - HANDLE_OPCODE (WASM_OP_I32_SUB), /* 0x6b */ \ - HANDLE_OPCODE (WASM_OP_I32_MUL), /* 0x6c */ \ - HANDLE_OPCODE (WASM_OP_I32_DIV_S), /* 0x6d */ \ - HANDLE_OPCODE (WASM_OP_I32_DIV_U), /* 0x6e */ \ - HANDLE_OPCODE (WASM_OP_I32_REM_S), /* 0x6f */ \ - HANDLE_OPCODE (WASM_OP_I32_REM_U), /* 0x70 */ \ - HANDLE_OPCODE (WASM_OP_I32_AND), /* 0x71 */ \ - HANDLE_OPCODE (WASM_OP_I32_OR), /* 0x72 */ \ - HANDLE_OPCODE (WASM_OP_I32_XOR), /* 0x73 */ \ - HANDLE_OPCODE (WASM_OP_I32_SHL), /* 0x74 */ \ - HANDLE_OPCODE (WASM_OP_I32_SHR_S), /* 0x75 */ \ - HANDLE_OPCODE (WASM_OP_I32_SHR_U), /* 0x76 */ \ - HANDLE_OPCODE (WASM_OP_I32_ROTL), /* 0x77 */ \ - HANDLE_OPCODE (WASM_OP_I32_ROTR), /* 0x78 */ \ - HANDLE_OPCODE (WASM_OP_I64_CLZ), /* 0x79 */ \ - HANDLE_OPCODE (WASM_OP_I64_CTZ), /* 0x7a */ \ - HANDLE_OPCODE (WASM_OP_I64_POPCNT), /* 0x7b */ \ - HANDLE_OPCODE (WASM_OP_I64_ADD), /* 0x7c */ \ - HANDLE_OPCODE (WASM_OP_I64_SUB), /* 0x7d */ \ - HANDLE_OPCODE (WASM_OP_I64_MUL), /* 0x7e */ \ - HANDLE_OPCODE (WASM_OP_I64_DIV_S), /* 0x7f */ \ - HANDLE_OPCODE (WASM_OP_I64_DIV_U), /* 0x80 */ \ - HANDLE_OPCODE (WASM_OP_I64_REM_S), /* 0x81 */ \ - HANDLE_OPCODE (WASM_OP_I64_REM_U), /* 0x82 */ \ - HANDLE_OPCODE (WASM_OP_I64_AND), /* 0x83 */ \ - HANDLE_OPCODE (WASM_OP_I64_OR), /* 0x84 */ \ - HANDLE_OPCODE (WASM_OP_I64_XOR), /* 0x85 */ \ - HANDLE_OPCODE (WASM_OP_I64_SHL), /* 0x86 */ \ - HANDLE_OPCODE (WASM_OP_I64_SHR_S), /* 0x87 */ \ - HANDLE_OPCODE (WASM_OP_I64_SHR_U), /* 0x88 */ \ - HANDLE_OPCODE (WASM_OP_I64_ROTL), /* 0x89 */ \ - HANDLE_OPCODE (WASM_OP_I64_ROTR), /* 0x8a */ \ - HANDLE_OPCODE (WASM_OP_F32_ABS), /* 0x8b */ \ - HANDLE_OPCODE (WASM_OP_F32_NEG), /* 0x8c */ \ - HANDLE_OPCODE (WASM_OP_F32_CEIL), /* 0x8d */ \ - HANDLE_OPCODE (WASM_OP_F32_FLOOR), /* 0x8e */ \ - HANDLE_OPCODE (WASM_OP_F32_TRUNC), /* 0x8f */ \ - HANDLE_OPCODE (WASM_OP_F32_NEAREST), /* 0x90 */ \ - HANDLE_OPCODE (WASM_OP_F32_SQRT), /* 0x91 */ \ - HANDLE_OPCODE (WASM_OP_F32_ADD), /* 0x92 */ \ - HANDLE_OPCODE (WASM_OP_F32_SUB), /* 0x93 */ \ - HANDLE_OPCODE (WASM_OP_F32_MUL), /* 0x94 */ \ - HANDLE_OPCODE (WASM_OP_F32_DIV), /* 0x95 */ \ - HANDLE_OPCODE (WASM_OP_F32_MIN), /* 0x96 */ \ - HANDLE_OPCODE (WASM_OP_F32_MAX), /* 0x97 */ \ - HANDLE_OPCODE (WASM_OP_F32_COPYSIGN), /* 0x98 */ \ - HANDLE_OPCODE (WASM_OP_F64_ABS), /* 0x99 */ \ - HANDLE_OPCODE (WASM_OP_F64_NEG), /* 0x9a */ \ - HANDLE_OPCODE (WASM_OP_F64_CEIL), /* 0x9b */ \ - HANDLE_OPCODE (WASM_OP_F64_FLOOR), /* 0x9c */ \ - HANDLE_OPCODE (WASM_OP_F64_TRUNC), /* 0x9d */ \ - HANDLE_OPCODE (WASM_OP_F64_NEAREST), /* 0x9e */ \ - HANDLE_OPCODE (WASM_OP_F64_SQRT), /* 0x9f */ \ - HANDLE_OPCODE (WASM_OP_F64_ADD), /* 0xa0 */ \ - HANDLE_OPCODE (WASM_OP_F64_SUB), /* 0xa1 */ \ - HANDLE_OPCODE (WASM_OP_F64_MUL), /* 0xa2 */ \ - HANDLE_OPCODE (WASM_OP_F64_DIV), /* 0xa3 */ \ - HANDLE_OPCODE (WASM_OP_F64_MIN), /* 0xa4 */ \ - HANDLE_OPCODE (WASM_OP_F64_MAX), /* 0xa5 */ \ - HANDLE_OPCODE (WASM_OP_F64_COPYSIGN), /* 0xa6 */ \ - HANDLE_OPCODE (WASM_OP_I32_WRAP_I64), /* 0xa7 */ \ - HANDLE_OPCODE (WASM_OP_I32_TRUNC_S_F32), /* 0xa8 */ \ - HANDLE_OPCODE (WASM_OP_I32_TRUNC_U_F32), /* 0xa9 */ \ - HANDLE_OPCODE (WASM_OP_I32_TRUNC_S_F64), /* 0xaa */ \ - HANDLE_OPCODE (WASM_OP_I32_TRUNC_U_F64), /* 0xab */ \ - HANDLE_OPCODE (WASM_OP_I64_EXTEND_S_I32), /* 0xac */ \ - HANDLE_OPCODE (WASM_OP_I64_EXTEND_U_I32), /* 0xad */ \ - HANDLE_OPCODE (WASM_OP_I64_TRUNC_S_F32), /* 0xae */ \ - HANDLE_OPCODE (WASM_OP_I64_TRUNC_U_F32), /* 0xaf */ \ - HANDLE_OPCODE (WASM_OP_I64_TRUNC_S_F64), /* 0xb0 */ \ - HANDLE_OPCODE (WASM_OP_I64_TRUNC_U_F64), /* 0xb1 */ \ - HANDLE_OPCODE (WASM_OP_F32_CONVERT_S_I32), /* 0xb2 */ \ - HANDLE_OPCODE (WASM_OP_F32_CONVERT_U_I32), /* 0xb3 */ \ - HANDLE_OPCODE (WASM_OP_F32_CONVERT_S_I64), /* 0xb4 */ \ - HANDLE_OPCODE (WASM_OP_F32_CONVERT_U_I64), /* 0xb5 */ \ - HANDLE_OPCODE (WASM_OP_F32_DEMOTE_F64), /* 0xb6 */ \ - HANDLE_OPCODE (WASM_OP_F64_CONVERT_S_I32), /* 0xb7 */ \ - HANDLE_OPCODE (WASM_OP_F64_CONVERT_U_I32), /* 0xb8 */ \ - HANDLE_OPCODE (WASM_OP_F64_CONVERT_S_I64), /* 0xb9 */ \ - HANDLE_OPCODE (WASM_OP_F64_CONVERT_U_I64), /* 0xba */ \ - HANDLE_OPCODE (WASM_OP_F64_PROMOTE_F32), /* 0xbb */ \ - HANDLE_OPCODE (WASM_OP_I32_REINTERPRET_F32), /* 0xbc */ \ - HANDLE_OPCODE (WASM_OP_I64_REINTERPRET_F64), /* 0xbd */ \ - HANDLE_OPCODE (WASM_OP_F32_REINTERPRET_I32), /* 0xbe */ \ - HANDLE_OPCODE (WASM_OP_F64_REINTERPRET_I64), /* 0xbf */ \ - HANDLE_OPCODE (WASM_OP_I32_EXTEND8_S), /* 0xc0 */ \ - HANDLE_OPCODE (WASM_OP_I32_EXTEND16_S), /* 0xc1 */ \ - HANDLE_OPCODE (WASM_OP_I64_EXTEND8_S), /* 0xc2 */ \ - HANDLE_OPCODE (WASM_OP_I64_EXTEND16_S), /* 0xc3 */ \ - HANDLE_OPCODE (WASM_OP_I64_EXTEND32_S), /* 0xc4 */ \ - HANDLE_OPCODE (WASM_OP_DROP_64), /* 0xc5 */ \ - HANDLE_OPCODE (WASM_OP_SELECT_64), /* 0xc6 */ \ - HANDLE_OPCODE (EXT_OP_GET_LOCAL_FAST), /* 0xc7 */ \ - HANDLE_OPCODE (EXT_OP_SET_LOCAL_FAST_I64), /* 0xc8 */ \ - HANDLE_OPCODE (EXT_OP_SET_LOCAL_FAST), /* 0xc9 */ \ - HANDLE_OPCODE (EXT_OP_TEE_LOCAL_FAST), /* 0xca */ \ - HANDLE_OPCODE (EXT_OP_TEE_LOCAL_FAST_I64), /* 0xcb */ \ - HANDLE_OPCODE (EXT_OP_COPY_STACK_TOP), /* 0xcc */ \ - HANDLE_OPCODE (EXT_OP_COPY_STACK_TOP_I64), /* 0xcd */ \ - HANDLE_OPCODE (EXT_OP_COPY_STACK_VALUES), /* 0xce */ \ - HANDLE_OPCODE (WASM_OP_IMPDEP), /* 0xcf */ \ - HANDLE_OPCODE (WASM_OP_REF_NULL), /* 0xd0 */ \ - HANDLE_OPCODE (WASM_OP_REF_IS_NULL), /* 0xd1 */ \ - HANDLE_OPCODE (WASM_OP_REF_FUNC), /* 0xd2 */ \ - HANDLE_OPCODE (EXT_OP_BLOCK), /* 0xd3 */ \ - HANDLE_OPCODE (EXT_OP_LOOP), /* 0xd4 */ \ - HANDLE_OPCODE (EXT_OP_IF), /* 0xd5 */ \ -}; \ -do { \ - _name[WASM_OP_MISC_PREFIX] = \ - HANDLE_OPCODE (WASM_OP_MISC_PREFIX); /* 0xfc */ \ - _name[WASM_OP_ATOMIC_PREFIX] = \ - HANDLE_OPCODE (WASM_OP_ATOMIC_PREFIX); /* 0xfe */ \ - DEF_DEBUG_BREAK_HANDLE(_name) \ -} while (0) -#endif /* end of _WASM_OPCODE_H */ +#define DEFINE_GOTO_TABLE(type, _name) \ + static type _name[WASM_INSTRUCTION_NUM] = { \ + HANDLE_OPCODE(WASM_OP_UNREACHABLE), /* 0x00 */ \ + HANDLE_OPCODE(WASM_OP_NOP), /* 0x01 */ \ + HANDLE_OPCODE(WASM_OP_BLOCK), /* 0x02 */ \ + HANDLE_OPCODE(WASM_OP_LOOP), /* 0x03 */ \ + HANDLE_OPCODE(WASM_OP_IF), /* 0x04 */ \ + HANDLE_OPCODE(WASM_OP_ELSE), /* 0x05 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x06), /* 0x06 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x07), /* 0x07 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x08), /* 0x08 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x09), /* 0x09 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x0a), /* 0x0a */ \ + HANDLE_OPCODE(WASM_OP_END), /* 0x0b */ \ + HANDLE_OPCODE(WASM_OP_BR), /* 0x0c */ \ + HANDLE_OPCODE(WASM_OP_BR_IF), /* 0x0d */ \ + HANDLE_OPCODE(WASM_OP_BR_TABLE), /* 0x0e */ \ + HANDLE_OPCODE(WASM_OP_RETURN), /* 0x0f */ \ + HANDLE_OPCODE(WASM_OP_CALL), /* 0x10 */ \ + HANDLE_OPCODE(WASM_OP_CALL_INDIRECT), /* 0x11 */ \ + HANDLE_OPCODE(WASM_OP_RETURN_CALL), /* 0x12 */ \ + HANDLE_OPCODE(WASM_OP_RETURN_CALL_INDIRECT), /* 0x13 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x14), /* 0x14 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x15), /* 0x15 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x16), /* 0x16 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x17), /* 0x17 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x18), /* 0x18 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x19), /* 0x19 */ \ + HANDLE_OPCODE(WASM_OP_DROP), /* 0x1a */ \ + HANDLE_OPCODE(WASM_OP_SELECT), /* 0x1b */ \ + HANDLE_OPCODE(WASM_OP_SELECT_T), /* 0x1c */ \ + HANDLE_OPCODE(WASM_OP_GET_GLOBAL_64), /* 0x1d */ \ + HANDLE_OPCODE(WASM_OP_SET_GLOBAL_64), /* 0x1e */ \ + HANDLE_OPCODE(WASM_OP_SET_GLOBAL_AUX_STACK), /* 0x1f */ \ + HANDLE_OPCODE(WASM_OP_GET_LOCAL), /* 0x20 */ \ + HANDLE_OPCODE(WASM_OP_SET_LOCAL), /* 0x21 */ \ + HANDLE_OPCODE(WASM_OP_TEE_LOCAL), /* 0x22 */ \ + HANDLE_OPCODE(WASM_OP_GET_GLOBAL), /* 0x23 */ \ + HANDLE_OPCODE(WASM_OP_SET_GLOBAL), /* 0x24 */ \ + HANDLE_OPCODE(WASM_OP_TABLE_GET), /* 0x25 */ \ + HANDLE_OPCODE(WASM_OP_TABLE_SET), /* 0x26 */ \ + HANDLE_OPCODE(WASM_OP_UNUSED_0x27), /* 0x27 */ \ + HANDLE_OPCODE(WASM_OP_I32_LOAD), /* 0x28 */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD), /* 0x29 */ \ + HANDLE_OPCODE(WASM_OP_F32_LOAD), /* 0x2a */ \ + HANDLE_OPCODE(WASM_OP_F64_LOAD), /* 0x2b */ \ + HANDLE_OPCODE(WASM_OP_I32_LOAD8_S), /* 0x2c */ \ + HANDLE_OPCODE(WASM_OP_I32_LOAD8_U), /* 0x2d */ \ + HANDLE_OPCODE(WASM_OP_I32_LOAD16_S), /* 0x2e */ \ + HANDLE_OPCODE(WASM_OP_I32_LOAD16_U), /* 0x2f */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD8_S), /* 0x30 */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD8_U), /* 0x31 */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD16_S), /* 0x32 */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD16_U), /* 0x33 */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD32_S), /* 0x34 */ \ + HANDLE_OPCODE(WASM_OP_I64_LOAD32_U), /* 0x35 */ \ + HANDLE_OPCODE(WASM_OP_I32_STORE), /* 0x36 */ \ + HANDLE_OPCODE(WASM_OP_I64_STORE), /* 0x37 */ \ + HANDLE_OPCODE(WASM_OP_F32_STORE), /* 0x38 */ \ + HANDLE_OPCODE(WASM_OP_F64_STORE), /* 0x39 */ \ + HANDLE_OPCODE(WASM_OP_I32_STORE8), /* 0x3a */ \ + HANDLE_OPCODE(WASM_OP_I32_STORE16), /* 0x3b */ \ + HANDLE_OPCODE(WASM_OP_I64_STORE8), /* 0x3c */ \ + HANDLE_OPCODE(WASM_OP_I64_STORE16), /* 0x3d */ \ + HANDLE_OPCODE(WASM_OP_I64_STORE32), /* 0x3e */ \ + HANDLE_OPCODE(WASM_OP_MEMORY_SIZE), /* 0x3f */ \ + HANDLE_OPCODE(WASM_OP_MEMORY_GROW), /* 0x40 */ \ + HANDLE_OPCODE(WASM_OP_I32_CONST), /* 0x41 */ \ + HANDLE_OPCODE(WASM_OP_I64_CONST), /* 0x42 */ \ + HANDLE_OPCODE(WASM_OP_F32_CONST), /* 0x43 */ \ + HANDLE_OPCODE(WASM_OP_F64_CONST), /* 0x44 */ \ + HANDLE_OPCODE(WASM_OP_I32_EQZ), /* 0x45 */ \ + HANDLE_OPCODE(WASM_OP_I32_EQ), /* 0x46 */ \ + HANDLE_OPCODE(WASM_OP_I32_NE), /* 0x47 */ \ + HANDLE_OPCODE(WASM_OP_I32_LT_S), /* 0x48 */ \ + HANDLE_OPCODE(WASM_OP_I32_LT_U), /* 0x49 */ \ + HANDLE_OPCODE(WASM_OP_I32_GT_S), /* 0x4a */ \ + HANDLE_OPCODE(WASM_OP_I32_GT_U), /* 0x4b */ \ + HANDLE_OPCODE(WASM_OP_I32_LE_S), /* 0x4c */ \ + HANDLE_OPCODE(WASM_OP_I32_LE_U), /* 0x4d */ \ + HANDLE_OPCODE(WASM_OP_I32_GE_S), /* 0x4e */ \ + HANDLE_OPCODE(WASM_OP_I32_GE_U), /* 0x4f */ \ + HANDLE_OPCODE(WASM_OP_I64_EQZ), /* 0x50 */ \ + HANDLE_OPCODE(WASM_OP_I64_EQ), /* 0x51 */ \ + HANDLE_OPCODE(WASM_OP_I64_NE), /* 0x52 */ \ + HANDLE_OPCODE(WASM_OP_I64_LT_S), /* 0x53 */ \ + HANDLE_OPCODE(WASM_OP_I64_LT_U), /* 0x54 */ \ + HANDLE_OPCODE(WASM_OP_I64_GT_S), /* 0x55 */ \ + HANDLE_OPCODE(WASM_OP_I64_GT_U), /* 0x56 */ \ + HANDLE_OPCODE(WASM_OP_I64_LE_S), /* 0x57 */ \ + HANDLE_OPCODE(WASM_OP_I64_LE_U), /* 0x58 */ \ + HANDLE_OPCODE(WASM_OP_I64_GE_S), /* 0x59 */ \ + HANDLE_OPCODE(WASM_OP_I64_GE_U), /* 0x5a */ \ + HANDLE_OPCODE(WASM_OP_F32_EQ), /* 0x5b */ \ + HANDLE_OPCODE(WASM_OP_F32_NE), /* 0x5c */ \ + HANDLE_OPCODE(WASM_OP_F32_LT), /* 0x5d */ \ + HANDLE_OPCODE(WASM_OP_F32_GT), /* 0x5e */ \ + HANDLE_OPCODE(WASM_OP_F32_LE), /* 0x5f */ \ + HANDLE_OPCODE(WASM_OP_F32_GE), /* 0x60 */ \ + HANDLE_OPCODE(WASM_OP_F64_EQ), /* 0x61 */ \ + HANDLE_OPCODE(WASM_OP_F64_NE), /* 0x62 */ \ + HANDLE_OPCODE(WASM_OP_F64_LT), /* 0x63 */ \ + HANDLE_OPCODE(WASM_OP_F64_GT), /* 0x64 */ \ + HANDLE_OPCODE(WASM_OP_F64_LE), /* 0x65 */ \ + HANDLE_OPCODE(WASM_OP_F64_GE), /* 0x66 */ \ + HANDLE_OPCODE(WASM_OP_I32_CLZ), /* 0x67 */ \ + HANDLE_OPCODE(WASM_OP_I32_CTZ), /* 0x68 */ \ + HANDLE_OPCODE(WASM_OP_I32_POPCNT), /* 0x69 */ \ + HANDLE_OPCODE(WASM_OP_I32_ADD), /* 0x6a */ \ + HANDLE_OPCODE(WASM_OP_I32_SUB), /* 0x6b */ \ + HANDLE_OPCODE(WASM_OP_I32_MUL), /* 0x6c */ \ + HANDLE_OPCODE(WASM_OP_I32_DIV_S), /* 0x6d */ \ + HANDLE_OPCODE(WASM_OP_I32_DIV_U), /* 0x6e */ \ + HANDLE_OPCODE(WASM_OP_I32_REM_S), /* 0x6f */ \ + HANDLE_OPCODE(WASM_OP_I32_REM_U), /* 0x70 */ \ + HANDLE_OPCODE(WASM_OP_I32_AND), /* 0x71 */ \ + HANDLE_OPCODE(WASM_OP_I32_OR), /* 0x72 */ \ + HANDLE_OPCODE(WASM_OP_I32_XOR), /* 0x73 */ \ + HANDLE_OPCODE(WASM_OP_I32_SHL), /* 0x74 */ \ + HANDLE_OPCODE(WASM_OP_I32_SHR_S), /* 0x75 */ \ + HANDLE_OPCODE(WASM_OP_I32_SHR_U), /* 0x76 */ \ + HANDLE_OPCODE(WASM_OP_I32_ROTL), /* 0x77 */ \ + HANDLE_OPCODE(WASM_OP_I32_ROTR), /* 0x78 */ \ + HANDLE_OPCODE(WASM_OP_I64_CLZ), /* 0x79 */ \ + HANDLE_OPCODE(WASM_OP_I64_CTZ), /* 0x7a */ \ + HANDLE_OPCODE(WASM_OP_I64_POPCNT), /* 0x7b */ \ + HANDLE_OPCODE(WASM_OP_I64_ADD), /* 0x7c */ \ + HANDLE_OPCODE(WASM_OP_I64_SUB), /* 0x7d */ \ + HANDLE_OPCODE(WASM_OP_I64_MUL), /* 0x7e */ \ + HANDLE_OPCODE(WASM_OP_I64_DIV_S), /* 0x7f */ \ + HANDLE_OPCODE(WASM_OP_I64_DIV_U), /* 0x80 */ \ + HANDLE_OPCODE(WASM_OP_I64_REM_S), /* 0x81 */ \ + HANDLE_OPCODE(WASM_OP_I64_REM_U), /* 0x82 */ \ + HANDLE_OPCODE(WASM_OP_I64_AND), /* 0x83 */ \ + HANDLE_OPCODE(WASM_OP_I64_OR), /* 0x84 */ \ + HANDLE_OPCODE(WASM_OP_I64_XOR), /* 0x85 */ \ + HANDLE_OPCODE(WASM_OP_I64_SHL), /* 0x86 */ \ + HANDLE_OPCODE(WASM_OP_I64_SHR_S), /* 0x87 */ \ + HANDLE_OPCODE(WASM_OP_I64_SHR_U), /* 0x88 */ \ + HANDLE_OPCODE(WASM_OP_I64_ROTL), /* 0x89 */ \ + HANDLE_OPCODE(WASM_OP_I64_ROTR), /* 0x8a */ \ + HANDLE_OPCODE(WASM_OP_F32_ABS), /* 0x8b */ \ + HANDLE_OPCODE(WASM_OP_F32_NEG), /* 0x8c */ \ + HANDLE_OPCODE(WASM_OP_F32_CEIL), /* 0x8d */ \ + HANDLE_OPCODE(WASM_OP_F32_FLOOR), /* 0x8e */ \ + HANDLE_OPCODE(WASM_OP_F32_TRUNC), /* 0x8f */ \ + HANDLE_OPCODE(WASM_OP_F32_NEAREST), /* 0x90 */ \ + HANDLE_OPCODE(WASM_OP_F32_SQRT), /* 0x91 */ \ + HANDLE_OPCODE(WASM_OP_F32_ADD), /* 0x92 */ \ + HANDLE_OPCODE(WASM_OP_F32_SUB), /* 0x93 */ \ + HANDLE_OPCODE(WASM_OP_F32_MUL), /* 0x94 */ \ + HANDLE_OPCODE(WASM_OP_F32_DIV), /* 0x95 */ \ + HANDLE_OPCODE(WASM_OP_F32_MIN), /* 0x96 */ \ + HANDLE_OPCODE(WASM_OP_F32_MAX), /* 0x97 */ \ + HANDLE_OPCODE(WASM_OP_F32_COPYSIGN), /* 0x98 */ \ + HANDLE_OPCODE(WASM_OP_F64_ABS), /* 0x99 */ \ + HANDLE_OPCODE(WASM_OP_F64_NEG), /* 0x9a */ \ + HANDLE_OPCODE(WASM_OP_F64_CEIL), /* 0x9b */ \ + HANDLE_OPCODE(WASM_OP_F64_FLOOR), /* 0x9c */ \ + HANDLE_OPCODE(WASM_OP_F64_TRUNC), /* 0x9d */ \ + HANDLE_OPCODE(WASM_OP_F64_NEAREST), /* 0x9e */ \ + HANDLE_OPCODE(WASM_OP_F64_SQRT), /* 0x9f */ \ + HANDLE_OPCODE(WASM_OP_F64_ADD), /* 0xa0 */ \ + HANDLE_OPCODE(WASM_OP_F64_SUB), /* 0xa1 */ \ + HANDLE_OPCODE(WASM_OP_F64_MUL), /* 0xa2 */ \ + HANDLE_OPCODE(WASM_OP_F64_DIV), /* 0xa3 */ \ + HANDLE_OPCODE(WASM_OP_F64_MIN), /* 0xa4 */ \ + HANDLE_OPCODE(WASM_OP_F64_MAX), /* 0xa5 */ \ + HANDLE_OPCODE(WASM_OP_F64_COPYSIGN), /* 0xa6 */ \ + HANDLE_OPCODE(WASM_OP_I32_WRAP_I64), /* 0xa7 */ \ + HANDLE_OPCODE(WASM_OP_I32_TRUNC_S_F32), /* 0xa8 */ \ + HANDLE_OPCODE(WASM_OP_I32_TRUNC_U_F32), /* 0xa9 */ \ + HANDLE_OPCODE(WASM_OP_I32_TRUNC_S_F64), /* 0xaa */ \ + HANDLE_OPCODE(WASM_OP_I32_TRUNC_U_F64), /* 0xab */ \ + HANDLE_OPCODE(WASM_OP_I64_EXTEND_S_I32), /* 0xac */ \ + HANDLE_OPCODE(WASM_OP_I64_EXTEND_U_I32), /* 0xad */ \ + HANDLE_OPCODE(WASM_OP_I64_TRUNC_S_F32), /* 0xae */ \ + HANDLE_OPCODE(WASM_OP_I64_TRUNC_U_F32), /* 0xaf */ \ + HANDLE_OPCODE(WASM_OP_I64_TRUNC_S_F64), /* 0xb0 */ \ + HANDLE_OPCODE(WASM_OP_I64_TRUNC_U_F64), /* 0xb1 */ \ + HANDLE_OPCODE(WASM_OP_F32_CONVERT_S_I32), /* 0xb2 */ \ + HANDLE_OPCODE(WASM_OP_F32_CONVERT_U_I32), /* 0xb3 */ \ + HANDLE_OPCODE(WASM_OP_F32_CONVERT_S_I64), /* 0xb4 */ \ + HANDLE_OPCODE(WASM_OP_F32_CONVERT_U_I64), /* 0xb5 */ \ + HANDLE_OPCODE(WASM_OP_F32_DEMOTE_F64), /* 0xb6 */ \ + HANDLE_OPCODE(WASM_OP_F64_CONVERT_S_I32), /* 0xb7 */ \ + HANDLE_OPCODE(WASM_OP_F64_CONVERT_U_I32), /* 0xb8 */ \ + HANDLE_OPCODE(WASM_OP_F64_CONVERT_S_I64), /* 0xb9 */ \ + HANDLE_OPCODE(WASM_OP_F64_CONVERT_U_I64), /* 0xba */ \ + HANDLE_OPCODE(WASM_OP_F64_PROMOTE_F32), /* 0xbb */ \ + HANDLE_OPCODE(WASM_OP_I32_REINTERPRET_F32), /* 0xbc */ \ + HANDLE_OPCODE(WASM_OP_I64_REINTERPRET_F64), /* 0xbd */ \ + HANDLE_OPCODE(WASM_OP_F32_REINTERPRET_I32), /* 0xbe */ \ + HANDLE_OPCODE(WASM_OP_F64_REINTERPRET_I64), /* 0xbf */ \ + HANDLE_OPCODE(WASM_OP_I32_EXTEND8_S), /* 0xc0 */ \ + HANDLE_OPCODE(WASM_OP_I32_EXTEND16_S), /* 0xc1 */ \ + HANDLE_OPCODE(WASM_OP_I64_EXTEND8_S), /* 0xc2 */ \ + HANDLE_OPCODE(WASM_OP_I64_EXTEND16_S), /* 0xc3 */ \ + HANDLE_OPCODE(WASM_OP_I64_EXTEND32_S), /* 0xc4 */ \ + HANDLE_OPCODE(WASM_OP_DROP_64), /* 0xc5 */ \ + HANDLE_OPCODE(WASM_OP_SELECT_64), /* 0xc6 */ \ + HANDLE_OPCODE(EXT_OP_GET_LOCAL_FAST), /* 0xc7 */ \ + HANDLE_OPCODE(EXT_OP_SET_LOCAL_FAST_I64), /* 0xc8 */ \ + HANDLE_OPCODE(EXT_OP_SET_LOCAL_FAST), /* 0xc9 */ \ + HANDLE_OPCODE(EXT_OP_TEE_LOCAL_FAST), /* 0xca */ \ + HANDLE_OPCODE(EXT_OP_TEE_LOCAL_FAST_I64), /* 0xcb */ \ + HANDLE_OPCODE(EXT_OP_COPY_STACK_TOP), /* 0xcc */ \ + HANDLE_OPCODE(EXT_OP_COPY_STACK_TOP_I64), /* 0xcd */ \ + HANDLE_OPCODE(EXT_OP_COPY_STACK_VALUES), /* 0xce */ \ + HANDLE_OPCODE(WASM_OP_IMPDEP), /* 0xcf */ \ + HANDLE_OPCODE(WASM_OP_REF_NULL), /* 0xd0 */ \ + HANDLE_OPCODE(WASM_OP_REF_IS_NULL), /* 0xd1 */ \ + HANDLE_OPCODE(WASM_OP_REF_FUNC), /* 0xd2 */ \ + HANDLE_OPCODE(EXT_OP_BLOCK), /* 0xd3 */ \ + HANDLE_OPCODE(EXT_OP_LOOP), /* 0xd4 */ \ + HANDLE_OPCODE(EXT_OP_IF), /* 0xd5 */ \ + }; \ + do { \ + _name[WASM_OP_MISC_PREFIX] = \ + HANDLE_OPCODE(WASM_OP_MISC_PREFIX); /* 0xfc */ \ + _name[WASM_OP_ATOMIC_PREFIX] = \ + HANDLE_OPCODE(WASM_OP_ATOMIC_PREFIX); /* 0xfe */ \ + DEF_DEBUG_BREAK_HANDLE(_name) \ + } while (0) +#ifdef __cplusplus +} +#endif + +#endif /* end of _WASM_OPCODE_H */ diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index e219ccf2f5..372d572fbb 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -30,8 +30,7 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) } static void -set_error_buf_v(char *error_buf, uint32 error_buf_size, - const char *format, ...) +set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...) { va_list args; char buf[128]; @@ -45,19 +44,18 @@ set_error_buf_v(char *error_buf, uint32 error_buf_size, } } -WASMModule* -wasm_load(const uint8 *buf, uint32 size, - char *error_buf, uint32 error_buf_size) +WASMModule * +wasm_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size) { return wasm_loader_load(buf, size, error_buf, error_buf_size); } -WASMModule* -wasm_load_from_sections(WASMSection *section_list, - char *error_buf, uint32_t error_buf_size) +WASMModule * +wasm_load_from_sections(WASMSection *section_list, char *error_buf, + uint32_t error_buf_size) { - return wasm_loader_load_from_sections(section_list, - error_buf, error_buf_size); + return wasm_loader_load_from_sections(section_list, error_buf, + error_buf_size); } void @@ -71,10 +69,8 @@ runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); return NULL; } @@ -102,8 +98,7 @@ get_sub_module_inst(const WASMModuleInstance *parent_module_inst, */ static void memories_deinstantiate(WASMModuleInstance *module_inst, - WASMMemoryInstance **memories, - uint32 count) + WASMMemoryInstance **memories, uint32 count) { uint32 i; if (memories) { @@ -115,9 +110,8 @@ memories_deinstantiate(WASMModuleInstance *module_inst, #endif #if WASM_ENABLE_SHARED_MEMORY != 0 if (memories[i]->is_shared) { - int32 ref_count = - shared_memory_dec_reference( - (WASMModuleCommon *)module_inst->module); + int32 ref_count = shared_memory_dec_reference( + (WASMModuleCommon *)module_inst->module); bh_assert(ref_count >= 0); /* if the reference count is not zero, @@ -138,16 +132,15 @@ memories_deinstantiate(WASMModuleInstance *module_inst, } } wasm_runtime_free(memories); - } - (void)module_inst; + } + (void)module_inst; } -static WASMMemoryInstance* -memory_instantiate(WASMModuleInstance *module_inst, - uint32 num_bytes_per_page, +static WASMMemoryInstance * +memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page, uint32 init_page_count, uint32 max_page_count, - uint32 heap_size, uint32 flags, - char *error_buf, uint32 error_buf_size) + uint32 heap_size, uint32 flags, char *error_buf, + uint32 error_buf_size) { WASMModule *module = module_inst->module; WASMMemoryInstance *memory; @@ -162,15 +155,14 @@ memory_instantiate(WASMModuleInstance *module_inst, /* shared memory */ if (is_shared_memory) { - WASMSharedMemNode *node = - wasm_module_get_shared_memory( - (WASMModuleCommon *)module_inst->module); + WASMSharedMemNode *node = wasm_module_get_shared_memory( + (WASMModuleCommon *)module_inst->module); /* If the memory of this module has been instantiated, return the memory instance directly */ if (node) { uint32 ref_count; ref_count = shared_memory_inc_reference( - (WASMModuleCommon *)module_inst->module); + (WASMModuleCommon *)module_inst->module); bh_assert(ref_count > 0); memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node); bh_assert(memory); @@ -181,8 +173,7 @@ memory_instantiate(WASMModuleInstance *module_inst, } #endif /* end of WASM_ENABLE_SHARED_MEMORY */ - if (heap_size > 0 - && module_inst->module->malloc_function != (uint32)-1 + if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1 && module_inst->module->free_function != (uint32)-1) { /* Disable app heap, use malloc/free function exported by wasm app to allocate/free memory instead */ @@ -203,16 +194,16 @@ memory_instantiate(WASMModuleInstance *module_inst, } else if (heap_size > 0) { if (module->aux_heap_base_global_index != (uint32)-1 - && module->aux_heap_base < num_bytes_per_page - * init_page_count) { + && module->aux_heap_base < num_bytes_per_page * init_page_count) { /* Insert app heap before __heap_base */ aux_heap_base = module->aux_heap_base; bytes_of_last_page = aux_heap_base % num_bytes_per_page; if (bytes_of_last_page == 0) bytes_of_last_page = num_bytes_per_page; bytes_to_page_end = num_bytes_per_page - bytes_of_last_page; - inc_page_count = (heap_size - bytes_to_page_end - + num_bytes_per_page - 1) / num_bytes_per_page; + inc_page_count = + (heap_size - bytes_to_page_end + num_bytes_per_page - 1) + / num_bytes_per_page; heap_offset = aux_heap_base; aux_heap_base += heap_size; @@ -227,15 +218,15 @@ memory_instantiate(WASMModuleInstance *module_inst, /* Adjust __heap_base global value */ global_idx = module->aux_heap_base_global_index; - global_addr = module_inst->global_data + - module_inst->globals[global_idx].data_offset; + global_addr = module_inst->global_data + + module_inst->globals[global_idx].data_offset; *(uint32 *)global_addr = aux_heap_base; LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base); } else { /* Insert app heap before new page */ - inc_page_count = (heap_size + num_bytes_per_page - 1) - / num_bytes_per_page; + inc_page_count = + (heap_size + num_bytes_per_page - 1) / num_bytes_per_page; heap_offset = num_bytes_per_page * init_page_count; heap_size = num_bytes_per_page * inc_page_count; if (heap_size > 0) @@ -266,15 +257,14 @@ memory_instantiate(WASMModuleInstance *module_inst, #endif /* Allocate memory space, addr data and global data */ - if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), - error_buf, error_buf_size))) { + if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), error_buf, + error_buf_size))) { return NULL; } if (memory_data_size > 0 && !(memory->memory_data = - runtime_malloc(memory_data_size, - error_buf, error_buf_size))) { + runtime_malloc(memory_data_size, error_buf, error_buf_size))) { goto fail1; } @@ -291,13 +281,13 @@ memory_instantiate(WASMModuleInstance *module_inst, if (heap_size > 0) { uint32 heap_struct_size = mem_allocator_get_heap_struct_size(); - if (!(memory->heap_handle = runtime_malloc((uint64)heap_struct_size, - error_buf, error_buf_size))) { + if (!(memory->heap_handle = runtime_malloc( + (uint64)heap_struct_size, error_buf, error_buf_size))) { goto fail2; } - if (!mem_allocator_create_with_struct_and_pool - (memory->heap_handle, heap_struct_size, - memory->heap_data, heap_size)) { + if (!mem_allocator_create_with_struct_and_pool( + memory->heap_handle, heap_struct_size, memory->heap_data, + heap_size)) { set_error_buf(error_buf, error_buf_size, "init app heap failed"); goto fail3; } @@ -313,8 +303,7 @@ memory_instantiate(WASMModuleInstance *module_inst, if (!shared_memory_set_memory_inst( (WASMModuleCommon *)module_inst->module, (WASMMemoryInstanceCommon *)memory)) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); goto fail5; } } @@ -343,20 +332,18 @@ memory_instantiate(WASMModuleInstance *module_inst, * Instantiate memories in a module. */ static WASMMemoryInstance ** -memories_instantiate(const WASMModule *module, - WASMModuleInstance *module_inst, +memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, uint32 heap_size, char *error_buf, uint32 error_buf_size) { WASMImport *import; - uint32 mem_index = 0, i, memory_count = - module->import_memory_count + module->memory_count; + uint32 mem_index = 0, i, + memory_count = module->import_memory_count + module->memory_count; uint64 total_size; WASMMemoryInstance **memories, *memory; - total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count; + total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count; - if (!(memories = runtime_malloc(total_size, - error_buf, error_buf_size))) { + if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } @@ -374,14 +361,14 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst_linked; if (!(module_inst_linked = get_sub_module_inst( - module_inst, import->u.memory.import_module))) { + module_inst, import->u.memory.import_module))) { set_error_buf(error_buf, error_buf_size, "unknown memory"); memories_deinstantiate(module_inst, memories, memory_count); return NULL; } if (!(memory = memories[mem_index++] = wasm_lookup_memory( - module_inst_linked, import->u.memory.field_name))) { + module_inst_linked, import->u.memory.field_name))) { set_error_buf(error_buf, error_buf_size, "unknown memory"); memories_deinstantiate(module_inst, memories, memory_count); return NULL; @@ -391,9 +378,9 @@ memories_instantiate(const WASMModule *module, #endif { if (!(memory = memories[mem_index++] = memory_instantiate( - module_inst, num_bytes_per_page, init_page_count, - max_page_count, actual_heap_size, flags, - error_buf, error_buf_size))) { + module_inst, num_bytes_per_page, init_page_count, + max_page_count, actual_heap_size, flags, error_buf, + error_buf_size))) { memories_deinstantiate(module_inst, memories, memory_count); return NULL; } @@ -409,13 +396,11 @@ memories_instantiate(const WASMModule *module, /* instantiate memories from memory section */ for (i = 0; i < module->memory_count; i++) { - if (!(memory = memories[mem_index++] = - memory_instantiate(module_inst, - module->memories[i].num_bytes_per_page, - module->memories[i].init_page_count, - module->memories[i].max_page_count, - heap_size, module->memories[i].flags, - error_buf, error_buf_size))) { + if (!(memory = memories[mem_index++] = memory_instantiate( + module_inst, module->memories[i].num_bytes_per_page, + module->memories[i].init_page_count, + module->memories[i].max_page_count, heap_size, + module->memories[i].flags, error_buf, error_buf_size))) { memories_deinstantiate(module_inst, memories, memory_count); return NULL; } @@ -430,8 +415,8 @@ memories_instantiate(const WASMModule *module, * for wasm code */ if (!(memory = memories[mem_index++] = - memory_instantiate(module_inst, 0, 0, 0, heap_size, 0, - error_buf, error_buf_size))) { + memory_instantiate(module_inst, 0, 0, 0, heap_size, 0, + error_buf, error_buf_size))) { memories_deinstantiate(module_inst, memories, memory_count); return NULL; } @@ -461,18 +446,16 @@ tables_deinstantiate(WASMTableInstance **tables, uint32 count) * Instantiate tables in a module. */ static WASMTableInstance ** -tables_instantiate(const WASMModule *module, - WASMModuleInstance *module_inst, +tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, char *error_buf, uint32 error_buf_size) { WASMImport *import; - uint32 table_index = 0, i, table_count = - module->import_table_count + module->table_count; - uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count; + uint32 table_index = 0, i, + table_count = module->import_table_count + module->table_count; + uint64 total_size = sizeof(WASMTableInstance *) * (uint64)table_count; WASMTableInstance **tables, *table; - if (!(tables = runtime_malloc(total_size, - error_buf, error_buf_size))) { + if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } @@ -485,15 +468,15 @@ tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst_linked = NULL; if (import->u.table.import_module) { - if (!(module_inst_linked = - get_sub_module_inst(module_inst, import->u.table.import_module))) { + if (!(module_inst_linked = get_sub_module_inst( + module_inst, import->u.table.import_module))) { set_error_buf(error_buf, error_buf_size, "unknown table"); tables_deinstantiate(tables, table_count); return NULL; } - if (!(table_inst_linked = wasm_lookup_table(module_inst_linked, - import->u.table.field_name))) { + if (!(table_inst_linked = wasm_lookup_table( + module_inst_linked, import->u.table.field_name))) { set_error_buf(error_buf, error_buf_size, "unknown table"); tables_deinstantiate(tables, table_count); return NULL; @@ -506,16 +489,16 @@ tables_instantiate(const WASMModule *module, { /* in order to save memory, alloc resource as few as possible */ max_size_fixed = import->u.table.possible_grow - ? import->u.table.max_size - : import->u.table.init_size; + ? import->u.table.max_size + : import->u.table.init_size; /* it is a built-in table, every module has its own */ total_size = offsetof(WASMTableInstance, base_addr); total_size += (uint64)max_size_fixed * sizeof(uint32); } - if (!(table = tables[table_index++] = runtime_malloc - (total_size, error_buf, error_buf_size))) { + if (!(table = tables[table_index++] = + runtime_malloc(total_size, error_buf, error_buf_size))) { tables_deinstantiate(tables, table_count); return NULL; } @@ -548,15 +531,14 @@ tables_instantiate(const WASMModule *module, /* in case, a module which imports this table will grow it */ max_size_fixed = module->tables[i].max_size; #else - max_size_fixed = - module->tables[i].possible_grow - ? module->tables[i].max_size - : module->tables[i].init_size; + max_size_fixed = module->tables[i].possible_grow + ? module->tables[i].max_size + : module->tables[i].init_size; #endif total_size += sizeof(uint32) * (uint64)max_size_fixed; - if (!(table = tables[table_index++] = runtime_malloc - (total_size, error_buf, error_buf_size))) { + if (!(table = tables[table_index++] = + runtime_malloc(total_size, error_buf, error_buf_size))) { tables_deinstantiate(tables, table_count); return NULL; } @@ -591,18 +573,16 @@ functions_deinstantiate(WASMFunctionInstance *functions, uint32 count) * Instantiate functions in a module. */ static WASMFunctionInstance * -functions_instantiate(const WASMModule *module, - WASMModuleInstance *module_inst, +functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, char *error_buf, uint32 error_buf_size) { WASMImport *import; - uint32 i, function_count = - module->import_function_count + module->function_count; + uint32 i, + function_count = module->import_function_count + module->function_count; uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count; WASMFunctionInstance *functions, *function; - if (!(functions = runtime_malloc(total_size, - error_buf, error_buf_size))) { + if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } @@ -614,23 +594,21 @@ functions_instantiate(const WASMModule *module, #if WASM_ENABLE_MULTI_MODULE != 0 if (import->u.function.import_module) { - function->import_module_inst = - get_sub_module_inst(module_inst, - import->u.function.import_module); + function->import_module_inst = get_sub_module_inst( + module_inst, import->u.function.import_module); if (function->import_module_inst) { function->import_func_inst = - wasm_lookup_function(function->import_module_inst, - import->u.function.field_name, NULL); + wasm_lookup_function(function->import_module_inst, + import->u.function.field_name, NULL); } } #endif /* WASM_ENABLE_MULTI_MODULE */ function->u.func_import = &import->u.function; - function->param_cell_num = - import->u.function.func_type->param_cell_num; + function->param_cell_num = import->u.function.func_type->param_cell_num; function->ret_cell_num = import->u.function.func_type->ret_cell_num; function->param_count = - (uint16)function->u.func_import->func_type->param_count; + (uint16)function->u.func_import->func_type->param_count; function->param_types = function->u.func_import->func_type->types; function->local_cell_num = 0; function->local_count = 0; @@ -648,7 +626,8 @@ functions_instantiate(const WASMModule *module, function->ret_cell_num = function->u.func->ret_cell_num; function->local_cell_num = function->u.func->local_cell_num; - function->param_count = (uint16)function->u.func->func_type->param_count; + function->param_count = + (uint16)function->u.func->func_type->param_count; function->local_count = (uint16)function->u.func->local_count; function->param_types = function->u.func->func_type->types; function->local_types = function->u.func->local_types; @@ -682,8 +661,8 @@ check_global_init_expr(const WASMModule *module, uint32 global_index, char *error_buf, uint32 error_buf_size) { if (global_index >= module->import_global_count + module->global_count) { - set_error_buf_v(error_buf, error_buf_size, - "unknown global %d", global_index); + set_error_buf_v(error_buf, error_buf_size, "unknown global %d", + global_index); return false; } @@ -708,20 +687,17 @@ check_global_init_expr(const WASMModule *module, uint32 global_index, * Instantiate globals in a module. */ static WASMGlobalInstance * -globals_instantiate(const WASMModule *module, - WASMModuleInstance *module_inst, +globals_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, uint32 *p_global_data_size, char *error_buf, uint32 error_buf_size) { WASMImport *import; uint32 global_data_offset = 0; - uint32 i, global_count = - module->import_global_count + module->global_count; + uint32 i, global_count = module->import_global_count + module->global_count; uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count; WASMGlobalInstance *globals, *global; - if (!(globals = runtime_malloc(total_size, - error_buf, error_buf_size))) { + if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } @@ -735,13 +711,13 @@ globals_instantiate(const WASMModule *module, #if WASM_ENABLE_MULTI_MODULE != 0 if (global_import->import_module) { if (!(global->import_module_inst = get_sub_module_inst( - module_inst, global_import->import_module))) { + module_inst, global_import->import_module))) { set_error_buf(error_buf, error_buf_size, "unknown global"); return NULL; } if (!(global->import_global_inst = wasm_lookup_global( - global->import_module_inst, global_import->field_name))) { + global->import_module_inst, global_import->field_name))) { set_error_buf(error_buf, error_buf_size, "unknown global"); return NULL; } @@ -783,9 +759,9 @@ globals_instantiate(const WASMModule *module, } bh_memcpy_s( - &(global->initial_value), sizeof(WASMValue), - &(globals[init_expr->u.global_index].initial_value), - sizeof(globals[init_expr->u.global_index].initial_value)); + &(global->initial_value), sizeof(WASMValue), + &(globals[init_expr->u.global_index].initial_value), + sizeof(globals[init_expr->u.global_index].initial_value)); } #if WASM_ENABLE_REF_TYPES != 0 else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) { @@ -814,7 +790,7 @@ get_export_count(const WASMModule *module, uint8 kind) WASMExport *export = module->exports; uint32 count = 0, i; - for (i = 0; i < module->export_count; i++, export++) + for (i = 0; i < module->export_count; i++, export ++) if (export->kind == kind) count++; @@ -834,23 +810,24 @@ export_functions_deinstantiate(WASMExportFuncInstance *functions) /** * Instantiate export functions in a module. */ -static WASMExportFuncInstance* +static WASMExportFuncInstance * export_functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, - uint32 export_func_count, - char *error_buf, uint32 error_buf_size) + uint32 export_func_count, char *error_buf, + uint32 error_buf_size) { WASMExportFuncInstance *export_funcs, *export_func; WASMExport *export = module->exports; uint32 i; - uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count; + uint64 total_size = + sizeof(WASMExportFuncInstance) * (uint64)export_func_count; - if (!(export_func = export_funcs = runtime_malloc - (total_size, error_buf, error_buf_size))) { + if (!(export_func = export_funcs = + runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } - for (i = 0; i < module->export_count; i++, export++) + for (i = 0; i < module->export_count; i++, export ++) if (export->kind == EXPORT_KIND_FUNC) { export_func->name = export->name; export_func->function = &module_inst->functions[export->index]; @@ -871,21 +848,22 @@ export_globals_deinstantiate(WASMExportGlobInstance *globals) static WASMExportGlobInstance * export_globals_instantiate(const WASMModule *module, - WASMModuleInstance *module_inst, - uint32 export_glob_count, char *error_buf, - uint32 error_buf_size) + WASMModuleInstance *module_inst, + uint32 export_glob_count, char *error_buf, + uint32 error_buf_size) { WASMExportGlobInstance *export_globals, *export_global; WASMExport *export = module->exports; uint32 i; - uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count; + uint64 total_size = + sizeof(WASMExportGlobInstance) * (uint64)export_glob_count; - if (!(export_global = export_globals = runtime_malloc - (total_size, error_buf, error_buf_size))) { + if (!(export_global = export_globals = + runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } - for (i = 0; i < module->export_count; i++, export++) + for (i = 0; i < module->export_count; i++, export ++) if (export->kind == EXPORT_KIND_GLOBAL) { export_global->name = export->name; export_global->global = &module_inst->globals[export->index]; @@ -905,7 +883,8 @@ execute_post_inst_function(WASMModuleInstance *module_inst) uint32 i; for (i = 0; i < module_inst->export_func_count; i++) - if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) { + if (!strcmp(module_inst->export_functions[i].name, + "__post_instantiate")) { post_inst_func = module_inst->export_functions[i].function; break; } @@ -933,7 +912,8 @@ execute_memory_init_function(WASMModuleInstance *module_inst) uint32 i; for (i = 0; i < module_inst->export_func_count; i++) - if (!strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) { + if (!strcmp(module_inst->export_functions[i].name, + "__wasm_call_ctors")) { memory_init_func = module_inst->export_functions[i].function; break; } @@ -948,8 +928,7 @@ execute_memory_init_function(WASMModuleInstance *module_inst) /* Not a valid function type, ignore it */ return true; - return wasm_create_exec_env_and_call_function(module_inst, - memory_init_func, + return wasm_create_exec_env_and_call_function(module_inst, memory_init_func, 0, NULL); } #endif @@ -971,8 +950,8 @@ execute_start_function(WASMModuleInstance *module_inst) static bool execute_malloc_function(WASMModuleInstance *module_inst, WASMFunctionInstance *malloc_func, - WASMFunctionInstance *retain_func, - uint32 size, uint32 *p_result) + WASMFunctionInstance *retain_func, uint32 size, + uint32 *p_result) { uint32 argv[2], argc; bool ret; @@ -992,12 +971,12 @@ execute_malloc_function(WASMModuleInstance *module_inst, argc = 2; } - ret = wasm_create_exec_env_and_call_function - (module_inst, malloc_func, argc, argv); + ret = wasm_create_exec_env_and_call_function(module_inst, malloc_func, argc, + argv); if (retain_func && ret) { - ret = wasm_create_exec_env_and_call_function - (module_inst, retain_func, 1, argv); + ret = wasm_create_exec_env_and_call_function(module_inst, retain_func, + 1, argv); } if (ret) @@ -1007,40 +986,39 @@ execute_malloc_function(WASMModuleInstance *module_inst, static bool execute_free_function(WASMModuleInstance *module_inst, - WASMFunctionInstance *free_func, - uint32 offset) + WASMFunctionInstance *free_func, uint32 offset) { uint32 argv[2]; argv[0] = offset; - return wasm_create_exec_env_and_call_function - (module_inst, free_func, 1, argv); + return wasm_create_exec_env_and_call_function(module_inst, free_func, 1, + argv); } #if WASM_ENABLE_MULTI_MODULE != 0 static bool sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst, - uint32 stack_size, uint32 heap_size, char *error_buf, - uint32 error_buf_size) + uint32 stack_size, uint32 heap_size, char *error_buf, + uint32 error_buf_size) { bh_list *sub_module_inst_list = module_inst->sub_module_inst_list; WASMRegisteredModule *sub_module_list_node = - bh_list_first_elem(module->import_module_list); + bh_list_first_elem(module->import_module_list); while (sub_module_list_node) { WASMSubModInstNode *sub_module_inst_list_node; - WASMModule *sub_module = (WASMModule*)sub_module_list_node->module; + WASMModule *sub_module = (WASMModule *)sub_module_list_node->module; WASMModuleInstance *sub_module_inst = - wasm_instantiate(sub_module, false, stack_size, heap_size, - error_buf, error_buf_size); + wasm_instantiate(sub_module, false, stack_size, heap_size, + error_buf, error_buf_size); if (!sub_module_inst) { LOG_DEBUG("instantiate %s failed", sub_module_list_node->module_name); return false; } - sub_module_inst_list_node = runtime_malloc - (sizeof(WASMSubModInstNode), error_buf, error_buf_size); + sub_module_inst_list_node = runtime_malloc(sizeof(WASMSubModInstNode), + error_buf, error_buf_size); if (!sub_module_inst_list_node) { LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d", sizeof(WASMSubModInstNode)); @@ -1050,9 +1028,9 @@ sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst, sub_module_inst_list_node->module_inst = sub_module_inst; sub_module_inst_list_node->module_name = - sub_module_list_node->module_name; + sub_module_list_node->module_name; bh_list_status ret = - bh_list_insert(sub_module_inst_list, sub_module_inst_list_node); + bh_list_insert(sub_module_inst_list, sub_module_inst_list_node); bh_assert(BH_LIST_SUCCESS == ret); (void)ret; @@ -1086,7 +1064,7 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf, for (i = 0; i < module->import_function_count; i++) { WASMFunctionImport *func = - &((module->import_functions + i)->u.function); + &((module->import_functions + i)->u.function); if (!func->func_ptr_linked #if WASM_ENABLE_MULTI_MODULE != 0 && !func->import_func_linked @@ -1131,10 +1109,9 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf, /** * Instantiate module */ -WASMModuleInstance* -wasm_instantiate(WASMModule *module, bool is_sub_inst, - uint32 stack_size, uint32 heap_size, - char *error_buf, uint32 error_buf_size) +WASMModuleInstance * +wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size, + uint32 heap_size, char *error_buf, uint32 error_buf_size) { WASMModuleInstance *module_inst; WASMGlobalInstance *globals = NULL, *global; @@ -1154,16 +1131,15 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, heap_size = APP_HEAP_SIZE_MAX; /* Allocate the memory */ - if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance), - error_buf, error_buf_size))) { + if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance), error_buf, + error_buf_size))) { return NULL; } module_inst->module = module; #if WASM_ENABLE_MULTI_MODULE != 0 - module_inst->sub_module_inst_list = - &module_inst->sub_module_inst_list_head; + module_inst->sub_module_inst_list = &module_inst->sub_module_inst_list_head; ret = sub_module_instantiate(module, module_inst, stack_size, heap_size, error_buf, error_buf_size); if (!ret) { @@ -1182,9 +1158,9 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, /* Instantiate global firstly to get the mutable data size */ global_count = module->import_global_count + module->global_count; if (global_count - && !(globals = globals_instantiate(module, module_inst, - &global_data_size, - error_buf, error_buf_size))) { + && !(globals = + globals_instantiate(module, module_inst, &global_data_size, + error_buf, error_buf_size))) { goto fail; } module_inst->global_count = global_count; @@ -1192,8 +1168,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, module_inst->memory_count = module->import_memory_count + module->memory_count; - module_inst->table_count = - module->import_table_count + module->table_count; + module_inst->table_count = module->import_table_count + module->table_count; module_inst->function_count = module->import_function_count + module->function_count; @@ -1201,42 +1176,38 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, module_inst->export_func_count = get_export_count(module, EXPORT_KIND_FUNC); #if WASM_ENABLE_MULTI_MODULE != 0 module_inst->export_tab_count = get_export_count(module, EXPORT_KIND_TABLE); - module_inst->export_mem_count = get_export_count(module, EXPORT_KIND_MEMORY); - module_inst->export_glob_count = get_export_count(module, EXPORT_KIND_GLOBAL); + module_inst->export_mem_count = + get_export_count(module, EXPORT_KIND_MEMORY); + module_inst->export_glob_count = + get_export_count(module, EXPORT_KIND_GLOBAL); #endif if (global_count > 0) { - if (!(module_inst->global_data = runtime_malloc - (global_data_size, error_buf, error_buf_size))) { + if (!(module_inst->global_data = runtime_malloc( + global_data_size, error_buf, error_buf_size))) { goto fail; } } /* Instantiate memories/tables/functions */ if ((module_inst->memory_count > 0 - && !(module_inst->memories = - memories_instantiate(module, - module_inst, - heap_size, error_buf, error_buf_size))) + && !(module_inst->memories = memories_instantiate( + module, module_inst, heap_size, error_buf, error_buf_size))) || (module_inst->table_count > 0 - && !(module_inst->tables = - tables_instantiate(module, - module_inst, - error_buf, error_buf_size))) + && !(module_inst->tables = tables_instantiate( + module, module_inst, error_buf, error_buf_size))) || (module_inst->function_count > 0 - && !(module_inst->functions = - functions_instantiate(module, - module_inst, - error_buf, error_buf_size))) + && !(module_inst->functions = functions_instantiate( + module, module_inst, error_buf, error_buf_size))) || (module_inst->export_func_count > 0 && !(module_inst->export_functions = export_functions_instantiate( - module, module_inst, module_inst->export_func_count, - error_buf, error_buf_size))) + module, module_inst, module_inst->export_func_count, + error_buf, error_buf_size))) #if WASM_ENABLE_MULTI_MODULE != 0 || (module_inst->export_glob_count > 0 && !(module_inst->export_globals = export_globals_instantiate( - module, module_inst, module_inst->export_glob_count, - error_buf, error_buf_size))) + module, module_inst, module_inst->export_glob_count, + error_buf, error_buf_size))) #endif ) { goto fail; @@ -1255,12 +1226,13 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: #endif - *(int32*)global_data = global->initial_value.i32; + *(int32 *)global_data = global->initial_value.i32; global_data += sizeof(int32); break; case VALUE_TYPE_I64: case VALUE_TYPE_F64: - bh_memcpy_s(global_data, (uint32)(global_data_end - global_data), + bh_memcpy_s(global_data, + (uint32)(global_data_end - global_data), &global->initial_value.i64, sizeof(int64)); global_data += sizeof(int64); break; @@ -1277,7 +1249,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, /* Initialize the memory data with data segment section */ module_inst->default_memory = - module_inst->memory_count ? module_inst->memories[0] : NULL; + module_inst->memory_count ? module_inst->memories[0] : NULL; for (i = 0; i < module->data_seg_count; i++) { WASMMemoryInstance *memory = NULL; @@ -1299,12 +1271,11 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, bh_assert(memory_data || memory_size == 0); bh_assert(data_seg->base_offset.init_expr_type - == INIT_EXPR_TYPE_I32_CONST + == INIT_EXPR_TYPE_I32_CONST || data_seg->base_offset.init_expr_type - == INIT_EXPR_TYPE_GET_GLOBAL); + == INIT_EXPR_TYPE_GET_GLOBAL); - if (data_seg->base_offset.init_expr_type - == INIT_EXPR_TYPE_GET_GLOBAL) { + if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) { if (!check_global_init_expr(module, data_seg->base_offset.u.global_index, error_buf, error_buf_size)) { @@ -1313,15 +1284,14 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, if (!globals || globals[data_seg->base_offset.u.global_index].type - != VALUE_TYPE_I32) { + != VALUE_TYPE_I32) { set_error_buf(error_buf, error_buf_size, "data segment does not fit"); goto fail; } data_seg->base_offset.u.i32 = - globals[data_seg->base_offset.u.global_index] - .initial_value.i32; + globals[data_seg->base_offset.u.global_index].initial_value.i32; } /* check offset */ @@ -1362,7 +1332,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, /* Initialize the table data with table segment section */ module_inst->default_table = - module_inst->table_count ? module_inst->tables[0] : NULL; + module_inst->table_count ? module_inst->tables[0] : NULL; /* in case there is no table */ for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count; i++) { @@ -1383,8 +1353,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 *table_data = (uint32 *)table->base_addr; #if WASM_ENABLE_MULTI_MODULE != 0 table_data = table->table_inst_linked - ? (uint32 *)table->table_inst_linked->base_addr - : table_data; + ? (uint32 *)table->table_inst_linked->base_addr + : table_data; #endif bh_assert(table_data); @@ -1395,11 +1365,14 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, /* init vec(funcidx) or vec(expr) */ bh_assert( - table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST - || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL + table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST + || table_seg->base_offset.init_expr_type + == INIT_EXPR_TYPE_GET_GLOBAL #if WASM_ENABLE_REF_TYPES != 0 - || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST - || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST + || table_seg->base_offset.init_expr_type + == INIT_EXPR_TYPE_FUNCREF_CONST + || table_seg->base_offset.init_expr_type + == INIT_EXPR_TYPE_REFNULL_CONST #endif ); @@ -1413,14 +1386,15 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, if (!globals || globals[table_seg->base_offset.u.global_index].type - != VALUE_TYPE_I32) { + != VALUE_TYPE_I32) { set_error_buf(error_buf, error_buf_size, "elements segment does not fit"); goto fail; } table_seg->base_offset.u.i32 = - globals[table_seg->base_offset.u.global_index].initial_value.i32; + globals[table_seg->base_offset.u.global_index] + .initial_value.i32; } /* check offset since length might negative */ @@ -1458,10 +1432,10 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, * so loader check is enough */ bh_memcpy_s( - table_data + table_seg->base_offset.u.i32, - (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32) - * sizeof(uint32)), - table_seg->func_indexes, (uint32)(length * sizeof(uint32))); + table_data + table_seg->base_offset.u.i32, + (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32) + * sizeof(uint32)), + table_seg->func_indexes, (uint32)(length * sizeof(uint32))); } /* module instance type */ @@ -1471,7 +1445,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, if (stack_size == 0) stack_size = DEFAULT_WASM_STACK_SIZE; #if WASM_ENABLE_SPEC_TEST != 0 - if (stack_size < 48 *1024) + if (stack_size < 48 * 1024) stack_size = 48 * 1024; #endif module_inst->default_wasm_stack_size = stack_size; @@ -1494,19 +1468,14 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, #if WASM_ENABLE_LIBC_WASI != 0 /* The sub-instance will get the wasi_ctx from main-instance */ if (!is_sub_inst) { - if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst, - module->wasi_args.dir_list, - module->wasi_args.dir_count, - module->wasi_args.map_dir_list, - module->wasi_args.map_dir_count, - module->wasi_args.env, - module->wasi_args.env_count, - module->wasi_args.argv, - module->wasi_args.argc, - module->wasi_args.stdio[0], - module->wasi_args.stdio[1], - module->wasi_args.stdio[2], - error_buf, error_buf_size)) { + if (!wasm_runtime_init_wasi( + (WASMModuleInstanceCommon *)module_inst, + module->wasi_args.dir_list, module->wasi_args.dir_count, + module->wasi_args.map_dir_list, module->wasi_args.map_dir_count, + module->wasi_args.env, module->wasi_args.env_count, + module->wasi_args.argv, module->wasi_args.argc, + module->wasi_args.stdio[0], module->wasi_args.stdio[1], + module->wasi_args.stdio[2], error_buf, error_buf_size)) { goto fail; } } @@ -1522,8 +1491,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, /* Execute __post_instantiate function */ if (!execute_post_inst_function(module_inst) || !execute_start_function(module_inst)) { - set_error_buf(error_buf, error_buf_size, - module_inst->cur_exception); + set_error_buf(error_buf, error_buf_size, module_inst->cur_exception); goto fail; } @@ -1547,8 +1515,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, #endif #if WASM_ENABLE_MEMORY_TRACING != 0 - wasm_runtime_dump_module_inst_mem_consumption - ((WASMModuleInstanceCommon *)module_inst); + wasm_runtime_dump_module_inst_mem_consumption( + (WASMModuleInstanceCommon *)module_inst); #endif (void)global_data_end; return module_inst; @@ -1574,16 +1542,16 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) which may allocated from global heap. */ /* Only destroy wasi ctx in the main module instance */ if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst); + wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); #endif if (module_inst->memory_count > 0) - memories_deinstantiate( - module_inst, - module_inst->memories, module_inst->memory_count); + memories_deinstantiate(module_inst, module_inst->memories, + module_inst->memory_count); tables_deinstantiate(module_inst->tables, module_inst->table_count); - functions_deinstantiate(module_inst->functions, module_inst->function_count); + functions_deinstantiate(module_inst->functions, + module_inst->function_count); globals_deinstantiate(module_inst->globals); export_functions_deinstantiate(module_inst->export_functions); #if WASM_ENABLE_MULTI_MODULE != 0 @@ -1594,7 +1562,7 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) wasm_runtime_free(module_inst->global_data); #if WASM_ENABLE_REF_TYPES != 0 - wasm_externref_cleanup((WASMModuleInstanceCommon*)module_inst); + wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst); #endif if (module_inst->exec_env_singleton) @@ -1611,9 +1579,9 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) wasm_runtime_free(module_inst); } -WASMFunctionInstance* -wasm_lookup_function(const WASMModuleInstance *module_inst, - const char *name, const char *signature) +WASMFunctionInstance * +wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name, + const char *signature) { uint32 i; for (i = 0; i < module_inst->export_func_count; i++) @@ -1640,7 +1608,7 @@ wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name) /** * using a strong assumption that one module instance only has * one memory instance - */ + */ (void)module_inst->export_memories; return module_inst->memories[0]; } @@ -1676,11 +1644,11 @@ clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst) } bool -wasm_call_function(WASMExecEnv *exec_env, - WASMFunctionInstance *function, +wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function, unsigned argc, uint32 argv[]) { - WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst; + WASMModuleInstance *module_inst = + (WASMModuleInstance *)exec_env->module_inst; /* set thread handle and stack boundary */ wasm_exec_env_set_thread_info(exec_env); @@ -1701,13 +1669,12 @@ wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst, #if WASM_ENABLE_THREAD_MGR != 0 WASMExecEnv *existing_exec_env = NULL; - if (!(existing_exec_env = exec_env = - wasm_clusters_search_exec_env( - (WASMModuleInstanceCommon*)module_inst))) { + if (!(existing_exec_env = exec_env = wasm_clusters_search_exec_env( + (WASMModuleInstanceCommon *)module_inst))) { #endif - if (!(exec_env = wasm_exec_env_create( - (WASMModuleInstanceCommon*)module_inst, - module_inst->default_wasm_stack_size))) { + if (!(exec_env = + wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst, + module_inst->default_wasm_stack_size))) { wasm_set_exception(module_inst, "allocate memory failed"); return false; } @@ -1748,18 +1715,16 @@ wasm_create_exec_env_singleton(WASMModuleInstance *module_inst) } void -wasm_set_exception(WASMModuleInstance *module_inst, - const char *exception) +wasm_set_exception(WASMModuleInstance *module_inst, const char *exception) { if (exception) - snprintf(module_inst->cur_exception, - sizeof(module_inst->cur_exception), + snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception), "Exception: %s", exception); else module_inst->cur_exception[0] = '\0'; } -const char* +const char * wasm_get_exception(WASMModuleInstance *module_inst) { if (module_inst->cur_exception[0] == '\0') @@ -1800,13 +1765,16 @@ wasm_dump_perf_profiling(const WASMModuleInstance *module_inst) } if (func_name) - os_printf(" func %s, execution time: %.3f ms, execution count: %d times\n", - func_name, module_inst->functions[i].total_exec_time / 1000.0f, - module_inst->functions[i].total_exec_cnt); + os_printf(" func %s, execution time: %.3f ms, execution count: %d " + "times\n", + func_name, + module_inst->functions[i].total_exec_time / 1000.0f, + module_inst->functions[i].total_exec_cnt); else - os_printf(" func %d, execution time: %.3f ms, execution count: %d times\n", - i, module_inst->functions[i].total_exec_time / 1000.0f, - module_inst->functions[i].total_exec_cnt); + os_printf(" func %d, execution time: %.3f ms, execution count: %d " + "times\n", + i, module_inst->functions[i].total_exec_time / 1000.0f, + module_inst->functions[i].total_exec_cnt); } } #endif @@ -1827,8 +1795,7 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size, if (memory->heap_handle) { addr = mem_allocator_malloc(memory->heap_handle, size); } - else if (module_inst->malloc_function - && module_inst->free_function) { + else if (module_inst->malloc_function && module_inst->free_function) { #if WASM_ENABLE_DEBUG_INTERP != 0 /* TODO: obviously, we can not create debug instance for * module malloc here, so, just disable the engine here, @@ -1839,10 +1806,9 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size, bool active = wasm_debug_get_engine_active(); wasm_debug_set_engine_active(false); #endif - if (!execute_malloc_function(module_inst, - module_inst->malloc_function, - module_inst->retain_function, - size, &offset)) { + if (!execute_malloc_function(module_inst, module_inst->malloc_function, + module_inst->retain_function, size, + &offset)) { #if WASM_ENABLE_DEBUG_INTERP != 0 wasm_debug_set_engine_active(active); #endif @@ -1917,7 +1883,7 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr) { if (ptr) { WASMMemoryInstance *memory = module_inst->default_memory; - uint8* addr; + uint8 *addr; if (!memory) { return; @@ -1925,25 +1891,21 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr) addr = memory->memory_data + ptr; - if (memory->heap_handle - && memory->heap_data <= addr + if (memory->heap_handle && memory->heap_data <= addr && addr < memory->heap_data_end) { mem_allocator_free(memory->heap_handle, addr); } - else if (module_inst->malloc_function - && module_inst->free_function + else if (module_inst->malloc_function && module_inst->free_function && memory->memory_data <= addr && addr < memory->memory_data_end) { #if WASM_ENABLE_DEBUG_INTERP != 0 - /*TODO: obviously, we can not create debug instance for module malloc here, - so, just disable the engine here, it is strange. the wasm's call should be - marshed to its own thread */ + /*TODO: obviously, we can not create debug instance for module + malloc here, so, just disable the engine here, it is strange. the + wasm's call should be marshed to its own thread */ bool active = wasm_debug_get_engine_active(); wasm_debug_set_engine_active(false); #endif - execute_free_function(module_inst, - module_inst->free_function, - ptr); + execute_free_function(module_inst, module_inst->free_function, ptr); #if WASM_ENABLE_DEBUG_INTERP != 0 wasm_debug_set_engine_active(active); #endif @@ -1952,12 +1914,12 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr) } uint32 -wasm_module_dup_data(WASMModuleInstance *module_inst, - const char *src, uint32 size) +wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src, + uint32 size) { char *buffer; - uint32 buffer_offset = wasm_module_malloc(module_inst, size, - (void**)&buffer); + uint32 buffer_offset = + wasm_module_malloc(module_inst, size, (void **)&buffer); if (buffer_offset != 0) { buffer = wasm_addr_app_to_native(module_inst, buffer_offset); bh_memcpy_s(buffer, size, src, size); @@ -1966,8 +1928,8 @@ wasm_module_dup_data(WASMModuleInstance *module_inst, } bool -wasm_validate_app_addr(WASMModuleInstance *module_inst, - uint32 app_offset, uint32 size) +wasm_validate_app_addr(WASMModuleInstance *module_inst, uint32 app_offset, + uint32 size) { WASMMemoryInstance *memory = module_inst->default_memory; uint32 memory_data_size; @@ -1992,8 +1954,8 @@ wasm_validate_app_addr(WASMModuleInstance *module_inst, } bool -wasm_validate_native_addr(WASMModuleInstance *module_inst, - void *native_ptr, uint32 size) +wasm_validate_native_addr(WASMModuleInstance *module_inst, void *native_ptr, + uint32 size) { WASMMemoryInstance *memory = module_inst->default_memory; uint8 *addr = (uint8 *)native_ptr; @@ -2007,8 +1969,7 @@ wasm_validate_native_addr(WASMModuleInstance *module_inst, goto fail; } - if (memory->memory_data <= addr - && addr + size <= memory->memory_data_end) { + if (memory->memory_data <= addr && addr + size <= memory->memory_data_end) { return true; } fail: @@ -2017,8 +1978,7 @@ wasm_validate_native_addr(WASMModuleInstance *module_inst, } void * -wasm_addr_app_to_native(WASMModuleInstance *module_inst, - uint32 app_offset) +wasm_addr_app_to_native(WASMModuleInstance *module_inst, uint32 app_offset) { WASMMemoryInstance *memory = module_inst->default_memory; uint8 *addr; @@ -2028,15 +1988,13 @@ wasm_addr_app_to_native(WASMModuleInstance *module_inst, addr = memory->memory_data + app_offset; - if (memory->memory_data <= addr - && addr < memory->memory_data_end) + if (memory->memory_data <= addr && addr < memory->memory_data_end) return addr; return NULL; } uint32 -wasm_addr_native_to_app(WASMModuleInstance *module_inst, - void *native_ptr) +wasm_addr_native_to_app(WASMModuleInstance *module_inst, void *native_ptr) { WASMMemoryInstance *memory = module_inst->default_memory; uint8 *addr = (uint8 *)native_ptr; @@ -2044,17 +2002,14 @@ wasm_addr_native_to_app(WASMModuleInstance *module_inst, if (!memory) return 0; - if (memory->memory_data <= addr - && addr < memory->memory_data_end) + if (memory->memory_data <= addr && addr < memory->memory_data_end) return (uint32)(addr - memory->memory_data); return 0; } bool -wasm_get_app_addr_range(WASMModuleInstance *module_inst, - uint32 app_offset, - uint32 *p_app_start_offset, - uint32 *p_app_end_offset) +wasm_get_app_addr_range(WASMModuleInstance *module_inst, uint32 app_offset, + uint32 *p_app_start_offset, uint32 *p_app_end_offset) { WASMMemoryInstance *memory = module_inst->default_memory; uint32 memory_data_size; @@ -2075,8 +2030,7 @@ wasm_get_app_addr_range(WASMModuleInstance *module_inst, } bool -wasm_get_native_addr_range(WASMModuleInstance *module_inst, - uint8 *native_ptr, +wasm_get_native_addr_range(WASMModuleInstance *module_inst, uint8 *native_ptr, uint8 **p_native_start_addr, uint8 **p_native_end_addr) { @@ -2086,8 +2040,7 @@ wasm_get_native_addr_range(WASMModuleInstance *module_inst, if (!memory) return false; - if (memory->memory_data <= addr - && addr < memory->memory_data_end) { + if (memory->memory_data <= addr && addr < memory->memory_data_end) { if (p_native_start_addr) *p_native_start_addr = memory->memory_data; if (p_native_end_addr) @@ -2137,25 +2090,27 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) } #endif - if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) { + if (!(new_memory_data = + wasm_runtime_realloc(memory_data, (uint32)total_size))) { if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) { return false; } if (memory_data) { - bh_memcpy_s(new_memory_data, (uint32)total_size, - memory_data, total_size_old); + bh_memcpy_s(new_memory_data, (uint32)total_size, memory_data, + total_size_old); wasm_runtime_free(memory_data); } } - memset(new_memory_data + total_size_old, - 0, (uint32)total_size - total_size_old); + memset(new_memory_data + total_size_old, 0, + (uint32)total_size - total_size_old); if (heap_size > 0) { if (mem_allocator_migrate(memory->heap_handle, (char *)heap_data_old - + (new_memory_data - memory_data), - heap_size) != 0) { + + (new_memory_data - memory_data), + heap_size) + != 0) { return false; } } @@ -2164,17 +2119,16 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) memory->cur_page_count = total_page_count; memory->heap_data = heap_data_old + (new_memory_data - memory_data); memory->heap_data_end = memory->heap_data + heap_size; - memory->memory_data_end = memory->memory_data - + memory->num_bytes_per_page - * total_page_count; + memory->memory_data_end = + memory->memory_data + memory->num_bytes_per_page * total_page_count; return true; } #if WASM_ENABLE_REF_TYPES != 0 bool -wasm_enlarge_table(WASMModuleInstance *module_inst, - uint32 table_idx, uint32 inc_entries, uint32 init_val) +wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx, + uint32 inc_entries, uint32 init_val) { uint32 entry_count, *new_table_data_start, i; WASMTableInstance *table_inst; @@ -2200,8 +2154,8 @@ wasm_enlarge_table(WASMModuleInstance *module_inst, /* fill in */ new_table_data_start = - (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr)) - + table_inst->cur_size; + (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr)) + + table_inst->cur_size; for (i = 0; i < inc_entries; ++i) { new_table_data_start[i] = init_val; } @@ -2212,18 +2166,15 @@ wasm_enlarge_table(WASMModuleInstance *module_inst, #endif /* WASM_ENABLE_REF_TYPES != 0 */ bool -wasm_call_indirect(WASMExecEnv *exec_env, - uint32_t tbl_idx, - uint32_t element_indices, - uint32_t argc, uint32_t argv[]) +wasm_call_indirect(WASMExecEnv *exec_env, uint32_t tbl_idx, + uint32_t element_indices, uint32_t argc, uint32_t argv[]) { WASMModuleInstance *module_inst = NULL; WASMTableInstance *table_inst = NULL; uint32_t function_indices = 0; WASMFunctionInstance *function_inst = NULL; - module_inst = - (WASMModuleInstance*)exec_env->module_inst; + module_inst = (WASMModuleInstance *)exec_env->module_inst; bh_assert(module_inst); table_inst = module_inst->tables[tbl_idx]; @@ -2241,7 +2192,7 @@ wasm_call_indirect(WASMExecEnv *exec_env, * please be aware that table_inst->base_addr may point * to another module's table **/ - function_indices = ((uint32_t*)table_inst->base_addr)[element_indices]; + function_indices = ((uint32_t *)table_inst->base_addr)[element_indices]; if (function_indices == NULL_REF) { wasm_set_exception(module_inst, "uninitialized element"); goto got_exception; @@ -2268,16 +2219,14 @@ wasm_call_indirect(WASMExecEnv *exec_env, #if WASM_ENABLE_THREAD_MGR != 0 bool -wasm_set_aux_stack(WASMExecEnv *exec_env, - uint32 start_offset, uint32 size) +wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size) { WASMModuleInstance *module_inst = - (WASMModuleInstance*)exec_env->module_inst; + (WASMModuleInstance *)exec_env->module_inst; uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index; uint32 data_end = module_inst->module->aux_data_end; uint32 stack_bottom = module_inst->module->aux_stack_bottom; - bool is_stack_before_data = - stack_bottom < data_end ? true : false; + bool is_stack_before_data = stack_bottom < data_end ? true : false; /* Check the aux stack space, currently we don't allocate space in heap */ if ((is_stack_before_data && (size > start_offset)) @@ -2287,10 +2236,9 @@ wasm_set_aux_stack(WASMExecEnv *exec_env, if (stack_top_idx != (uint32)-1) { /* The aux stack top is a wasm global, set the initial value for the global */ - uint8 *global_addr = - module_inst->global_data + - module_inst->globals[stack_top_idx].data_offset; - *(int32*)global_addr = start_offset; + uint8 *global_addr = module_inst->global_data + + module_inst->globals[stack_top_idx].data_offset; + *(int32 *)global_addr = start_offset; /* The aux stack boundary is a constant value, set the value to exec_env */ exec_env->aux_stack_boundary.boundary = start_offset - size; @@ -2302,18 +2250,15 @@ wasm_set_aux_stack(WASMExecEnv *exec_env, } bool -wasm_get_aux_stack(WASMExecEnv *exec_env, - uint32 *start_offset, uint32 *size) +wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size) { WASMModuleInstance *module_inst = - (WASMModuleInstance*)exec_env->module_inst; + (WASMModuleInstance *)exec_env->module_inst; /* The aux stack information is resolved in loader and store in module */ - uint32 stack_bottom = - module_inst->module->aux_stack_bottom; - uint32 total_aux_stack_size = - module_inst->module->aux_stack_size; + uint32 stack_bottom = module_inst->module->aux_stack_bottom; + uint32 total_aux_stack_size = module_inst->module->aux_stack_size; if (stack_bottom != 0 && total_aux_stack_size != 0) { if (start_offset) @@ -2340,23 +2285,23 @@ wasm_get_module_mem_consumption(const WASMModule *module, mem_conspn->types_size = sizeof(WASMType *) * module->type_count; for (i = 0; i < module->type_count; i++) { WASMType *type = module->types[i]; - size = offsetof(WASMType, types) + - sizeof(uint8) * (type->param_count + type->result_count); + size = offsetof(WASMType, types) + + sizeof(uint8) * (type->param_count + type->result_count); mem_conspn->types_size += size; } mem_conspn->imports_size = sizeof(WASMImport) * module->import_count; - mem_conspn->functions_size = sizeof(WASMFunction *) - * module->function_count; + mem_conspn->functions_size = + sizeof(WASMFunction *) * module->function_count; for (i = 0; i < module->function_count; i++) { WASMFunction *func = module->functions[i]; WASMType *type = func->func_type; size = sizeof(WASMFunction) + func->local_count + sizeof(uint16) * (type->param_count + func->local_count); #if WASM_ENABLE_FAST_INTERP != 0 - size += func->code_compiled_size - + sizeof(uint32) * func->const_cell_num; + size += + func->code_compiled_size + sizeof(uint32) * func->const_cell_num; #endif mem_conspn->functions_size += size; } @@ -2366,16 +2311,14 @@ wasm_get_module_mem_consumption(const WASMModule *module, mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count; mem_conspn->exports_size = sizeof(WASMExport) * module->export_count; - mem_conspn->table_segs_size = sizeof(WASMTableSeg) - * module->table_seg_count; + mem_conspn->table_segs_size = + sizeof(WASMTableSeg) * module->table_seg_count; for (i = 0; i < module->table_seg_count; i++) { WASMTableSeg *table_seg = &module->table_segments[i]; - mem_conspn->tables_size += sizeof(uint32) - * table_seg->function_count; + mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count; } - mem_conspn->data_segs_size = sizeof(WASMDataSeg*) - * module->data_seg_count; + mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count; for (i = 0; i < module->data_seg_count; i++) { mem_conspn->data_segs_size += sizeof(WASMDataSeg); } @@ -2384,8 +2327,8 @@ wasm_get_module_mem_consumption(const WASMModule *module, StringNode *node = module->const_str_list, *node_next; while (node) { node_next = node->next; - mem_conspn->const_strs_size += sizeof(StringNode) - + strlen(node->str) + 1; + mem_conspn->const_strs_size += + sizeof(StringNode) + strlen(node->str) + 1; node = node_next; } } @@ -2416,22 +2359,20 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance); - mem_conspn->memories_size = sizeof(WASMMemoryInstance *) - * module_inst->memory_count; + mem_conspn->memories_size = + sizeof(WASMMemoryInstance *) * module_inst->memory_count; for (i = 0; i < module_inst->memory_count; i++) { WASMMemoryInstance *memory = module_inst->memories[i]; size = sizeof(WASMMemoryInstance) + memory->num_bytes_per_page * memory->cur_page_count; mem_conspn->memories_size += size; - mem_conspn->app_heap_size += memory->heap_data_end - - memory->heap_data; + mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data; /* size of app heap structure */ - mem_conspn->memories_size += - mem_allocator_get_heap_struct_size(); + mem_conspn->memories_size += mem_allocator_get_heap_struct_size(); } - mem_conspn->tables_size = sizeof(WASMTableInstance *) - * module_inst->table_count; + mem_conspn->tables_size = + sizeof(WASMTableInstance *) * module_inst->table_count; for (i = 0; i < module_inst->table_count; i++) { WASMTableInstance *table = module_inst->tables[i]; #if WASM_ENABLE_MULTI_MODULE != 0 @@ -2447,20 +2388,20 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, mem_conspn->tables_size += size; } - mem_conspn->functions_size = sizeof(WASMFunctionInstance) - * module_inst->function_count; + mem_conspn->functions_size = + sizeof(WASMFunctionInstance) * module_inst->function_count; - mem_conspn->globals_size = sizeof(WASMGlobalInstance) - * module_inst->global_count; + mem_conspn->globals_size = + sizeof(WASMGlobalInstance) * module_inst->global_count; if (module_inst->global_count > 0) { WASMGlobalInstance *global = &module_inst->globals[module_inst->global_count - 1]; - mem_conspn->globals_size += global->data_offset - + wasm_value_type_size(global->type); + mem_conspn->globals_size += + global->data_offset + wasm_value_type_size(global->type); } - mem_conspn->exports_size = sizeof(WASMExportFuncInstance) - * module_inst->export_func_count; + mem_conspn->exports_size = + sizeof(WASMExportFuncInstance) * module_inst->export_func_count; mem_conspn->total_size += mem_conspn->module_inst_struct_size; mem_conspn->total_size += mem_conspn->memories_size; @@ -2469,7 +2410,7 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, mem_conspn->total_size += mem_conspn->globals_size; mem_conspn->total_size += mem_conspn->exports_size; } -#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) +#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \ || (WASM_ENABLE_MEMORY_TRACING != 0) */ #if WASM_ENABLE_DUMP_CALL_STACK != 0 @@ -2477,9 +2418,9 @@ void wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env) { WASMModuleInstance *module_inst = - (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env); + (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env); WASMInterpFrame *first_frame, - *cur_frame = wasm_exec_env_get_cur_frame(exec_env); + *cur_frame = wasm_exec_env_get_cur_frame(exec_env); uint32 n = 0; /* count frames includes a function */ @@ -2538,7 +2479,7 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env) uint32 i; for (i = 0; i < module_inst->export_func_count; i++) { WASMExportFuncInstance *export_func = - module_inst->export_functions + i; + module_inst->export_functions + i; if (export_func->function == func_inst) { func_name = export_func->name; break; diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 745f6fafd5..56871b3652 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -247,7 +247,7 @@ typedef struct WASMSubModInstNode { * * @return the code block of the function */ -static inline uint8* +static inline uint8 * wasm_get_func_code(WASMFunctionInstance *func) { #if WASM_ENABLE_FAST_INTERP == 0 @@ -264,34 +264,33 @@ wasm_get_func_code(WASMFunctionInstance *func) * * @return the code block end of the function */ -static inline uint8* +static inline uint8 * wasm_get_func_code_end(WASMFunctionInstance *func) { #if WASM_ENABLE_FAST_INTERP == 0 - return func->is_import_func - ? NULL : func->u.func->code + func->u.func->code_size; + return func->is_import_func ? NULL + : func->u.func->code + func->u.func->code_size; #else return func->is_import_func - ? NULL - : func->u.func->code_compiled + func->u.func->code_compiled_size; + ? NULL + : func->u.func->code_compiled + func->u.func->code_compiled_size; #endif } WASMModule * -wasm_load(const uint8 *buf, uint32 size, - char *error_buf, uint32 error_buf_size); +wasm_load(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size); WASMModule * -wasm_load_from_sections(WASMSection *section_list, - char *error_buf, uint32_t error_buf_size); +wasm_load_from_sections(WASMSection *section_list, char *error_buf, + uint32_t error_buf_size); void wasm_unload(WASMModule *module); WASMModuleInstance * -wasm_instantiate(WASMModule *module, bool is_sub_inst, - uint32 stack_size, uint32 heap_size, - char *error_buf, uint32 error_buf_size); +wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size, + uint32 heap_size, char *error_buf, uint32 error_buf_size); void wasm_dump_perf_profiling(const WASMModuleInstance *module_inst); @@ -300,8 +299,8 @@ void wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst); WASMFunctionInstance * -wasm_lookup_function(const WASMModuleInstance *module_inst, - const char *name, const char *signature); +wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name, + const char *signature); #if WASM_ENABLE_MULTI_MODULE != 0 WASMGlobalInstance * @@ -315,8 +314,7 @@ wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name); #endif bool -wasm_call_function(WASMExecEnv *exec_env, - WASMFunctionInstance *function, +wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function, unsigned argc, uint32 argv[]); bool @@ -330,7 +328,7 @@ wasm_create_exec_env_singleton(WASMModuleInstance *module_inst); void wasm_set_exception(WASMModuleInstance *module, const char *exception); -const char* +const char * wasm_get_exception(WASMModuleInstance *module); uint32 @@ -345,38 +343,32 @@ void wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr); uint32 -wasm_module_dup_data(WASMModuleInstance *module_inst, - const char *src, uint32 size); +wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src, + uint32 size); bool -wasm_validate_app_addr(WASMModuleInstance *module_inst, - uint32 app_offset, uint32 size); +wasm_validate_app_addr(WASMModuleInstance *module_inst, uint32 app_offset, + uint32 size); bool -wasm_validate_app_str_addr(WASMModuleInstance *module_inst, - uint32 app_offset); +wasm_validate_app_str_addr(WASMModuleInstance *module_inst, uint32 app_offset); bool -wasm_validate_native_addr(WASMModuleInstance *module_inst, - void *native_ptr, uint32 size); +wasm_validate_native_addr(WASMModuleInstance *module_inst, void *native_ptr, + uint32 size); void * -wasm_addr_app_to_native(WASMModuleInstance *module_inst, - uint32 app_offset); +wasm_addr_app_to_native(WASMModuleInstance *module_inst, uint32 app_offset); uint32 -wasm_addr_native_to_app(WASMModuleInstance *module_inst, - void *native_ptr); +wasm_addr_native_to_app(WASMModuleInstance *module_inst, void *native_ptr); bool -wasm_get_app_addr_range(WASMModuleInstance *module_inst, - uint32 app_offset, - uint32 *p_app_start_offset, - uint32 *p_app_end_offset); +wasm_get_app_addr_range(WASMModuleInstance *module_inst, uint32 app_offset, + uint32 *p_app_start_offset, uint32 *p_app_end_offset); bool -wasm_get_native_addr_range(WASMModuleInstance *module_inst, - uint8_t *native_ptr, +wasm_get_native_addr_range(WASMModuleInstance *module_inst, uint8_t *native_ptr, uint8_t **p_native_start_addr, uint8_t **p_native_end_addr); @@ -384,19 +376,15 @@ bool wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count); bool -wasm_call_indirect(WASMExecEnv *exec_env, - uint32_t tbl_idx, - uint32_t element_indices, - uint32_t argc, uint32_t argv[]); +wasm_call_indirect(WASMExecEnv *exec_env, uint32_t tbl_idx, + uint32_t element_indices, uint32_t argc, uint32_t argv[]); #if WASM_ENABLE_THREAD_MGR != 0 bool -wasm_set_aux_stack(WASMExecEnv *exec_env, - uint32 start_offset, uint32 size); +wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size); bool -wasm_get_aux_stack(WASMExecEnv *exec_env, - uint32 *start_offset, uint32 *size); +wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size); #endif void @@ -427,13 +415,12 @@ wasm_elem_is_declarative(uint32 mode) } bool -wasm_enlarge_table(WASMModuleInstance *module_inst, - uint32 table_idx, uint32 inc_entries, uint32 init_val); +wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx, + uint32 inc_entries, uint32 init_val); #endif /* WASM_ENABLE_REF_TYPES != 0 */ static inline WASMTableInstance * -wasm_get_table_inst(const WASMModuleInstance *module_inst, - const uint32 tbl_idx) +wasm_get_table_inst(const WASMModuleInstance *module_inst, const uint32 tbl_idx) { /* careful, it might be a table in another module */ WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx]; diff --git a/doc/source_debugging.md b/doc/source_debugging.md index 1543ad5d6b..60b08240e7 100644 --- a/doc/source_debugging.md +++ b/doc/source_debugging.md @@ -14,8 +14,12 @@ llvm-dwarfdump-12 test.wasm ``` ## Debugging with interpreter +1. Install dependent libraries +``` bash +apt update && apt install cmake make g++ libxml2-dev -y +``` -1. Build iwasm with source debugging feature +2. Build iwasm with source debugging feature ``` bash cd ${WAMR_ROOT}/product-mini/platforms/linux mkdir build && cd build @@ -23,21 +27,21 @@ cmake .. -DWAMR_BUILD_DEBUG_INTERP=1 make ``` -2. Execute iwasm with debug engine enabled +3. Execute iwasm with debug engine enabled ``` bash iwasm -g=127.0.0.1:1234 test.wasm ``` -3. Build customized lldb (assume you have already built llvm) +4. Build customized lldb (assume you have already cloned llvm) ``` bash cd ${WAMR_ROOT}/core/deps/llvm -git apply ../../../../build-scripts/lldb-wasm.patch -mkdir build && cd build -cmake ../llvm -DLLVM_ENABLE_PROJECTS="clang,lldb" -DLLVM_TARGETS_TO_BUILD:STRING="X86;WebAssembly" +git apply ../../../build-scripts/lldb-wasm.patch +mkdir build_lldb && cd build_lldb +cmake -DCMAKE_BUILD_TYPE:STRING="Release" -DLLVM_ENABLE_PROJECTS="clang;lldb" -DLLVM_TARGETS_TO_BUILD:STRING="X86;WebAssembly" -DLLVM_ENABLE_LIBXML2:BOOL=ON ../llvm make -j $(nproc) ``` -4. Launch customized lldb and connect to iwasm +5. Launch customized lldb and connect to iwasm ``` bash lldb (lldb) process connect -p wasm connect://127.0.0.1:1234 From c2143c19b36d42eb716a129a11fd334afe3c89f8 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 9 Oct 2021 00:46:11 +0800 Subject: [PATCH 6/6] Apply clang-format for iwasm include, common and aot files --- core/iwasm/aot/aot_intrinsic.c | 81 +- core/iwasm/aot/aot_intrinsic.h | 6 +- core/iwasm/aot/aot_loader.c | 755 +++++------ core/iwasm/aot/aot_reloc.h | 25 +- core/iwasm/aot/aot_runtime.c | 792 ++++++------ core/iwasm/aot/aot_runtime.h | 131 +- core/iwasm/aot/arch/aot_reloc_aarch64.c | 163 +-- core/iwasm/aot/arch/aot_reloc_arc.c | 88 +- core/iwasm/aot/arch/aot_reloc_arm.c | 84 +- core/iwasm/aot/arch/aot_reloc_mips.c | 16 +- core/iwasm/aot/arch/aot_reloc_riscv.c | 77 +- core/iwasm/aot/arch/aot_reloc_thumb.c | 63 +- core/iwasm/aot/arch/aot_reloc_x86_32.c | 48 +- core/iwasm/aot/arch/aot_reloc_x86_64.c | 119 +- core/iwasm/aot/arch/aot_reloc_xtensa.c | 56 +- core/iwasm/aot/debug/elf_parser.c | 14 +- core/iwasm/aot/debug/jit_debug.c | 14 +- core/iwasm/common/arch/invokeNative_general.c | 66 +- core/iwasm/common/wasm_application.c | 171 +-- core/iwasm/common/wasm_c_api.c | 1013 +++++++-------- core/iwasm/common/wasm_c_api_internal.h | 18 +- core/iwasm/common/wasm_exec_env.c | 23 +- core/iwasm/common/wasm_exec_env.h | 11 +- core/iwasm/common/wasm_memory.c | 20 +- core/iwasm/common/wasm_memory.h | 1 - core/iwasm/common/wasm_native.c | 93 +- core/iwasm/common/wasm_native.h | 3 +- core/iwasm/common/wasm_runtime_common.c | 1126 +++++++++-------- core/iwasm/common/wasm_runtime_common.h | 419 +++--- core/iwasm/common/wasm_shared_memory.c | 45 +- core/iwasm/common/wasm_shared_memory.h | 10 +- core/iwasm/include/aot_export.h | 22 +- core/iwasm/include/lib_export.h | 19 +- core/iwasm/include/wasm_c_api.h | 22 +- core/iwasm/include/wasm_export.h | 113 +- doc/source_debugging.md | 4 +- tests/wamr-test-suites/test_wamr.sh | 19 +- 37 files changed, 2855 insertions(+), 2895 deletions(-) diff --git a/core/iwasm/aot/aot_intrinsic.c b/core/iwasm/aot/aot_intrinsic.c index ce28d9d5eb..ba02dca0ba 100644 --- a/core/iwasm/aot/aot_intrinsic.c +++ b/core/iwasm/aot/aot_intrinsic.c @@ -11,43 +11,30 @@ typedef struct { uint64 flag; } aot_intrinsic; +/* clang-format off */ static const aot_intrinsic g_intrinsic_mapping[] = { - { "llvm.experimental.constrained.fadd.f32", "aot_intrinsic_fadd_f32", - AOT_INTRINSIC_FLAG_F32_FADD }, - { "llvm.experimental.constrained.fadd.f64", "aot_intrinsic_fadd_f64", - AOT_INTRINSIC_FLAG_F64_FADD }, - { "llvm.experimental.constrained.fsub.f32", "aot_intrinsic_fsub_f32", - AOT_INTRINSIC_FLAG_F32_FSUB }, - { "llvm.experimental.constrained.fsub.f64", "aot_intrinsic_fsub_f64", - AOT_INTRINSIC_FLAG_F64_FSUB }, - { "llvm.experimental.constrained.fmul.f32", "aot_intrinsic_fmul_f32", - AOT_INTRINSIC_FLAG_F32_FMUL }, - { "llvm.experimental.constrained.fmul.f64", "aot_intrinsic_fmul_f64", - AOT_INTRINSIC_FLAG_F64_FMUL }, - { "llvm.experimental.constrained.fdiv.f32", "aot_intrinsic_fdiv_f32", - AOT_INTRINSIC_FLAG_F32_FDIV }, - { "llvm.experimental.constrained.fdiv.f64", "aot_intrinsic_fdiv_f64", - AOT_INTRINSIC_FLAG_F64_FDIV }, + { "llvm.experimental.constrained.fadd.f32", "aot_intrinsic_fadd_f32", AOT_INTRINSIC_FLAG_F32_FADD }, + { "llvm.experimental.constrained.fadd.f64", "aot_intrinsic_fadd_f64", AOT_INTRINSIC_FLAG_F64_FADD }, + { "llvm.experimental.constrained.fsub.f32", "aot_intrinsic_fsub_f32", AOT_INTRINSIC_FLAG_F32_FSUB }, + { "llvm.experimental.constrained.fsub.f64", "aot_intrinsic_fsub_f64", AOT_INTRINSIC_FLAG_F64_FSUB }, + { "llvm.experimental.constrained.fmul.f32", "aot_intrinsic_fmul_f32", AOT_INTRINSIC_FLAG_F32_FMUL }, + { "llvm.experimental.constrained.fmul.f64", "aot_intrinsic_fmul_f64", AOT_INTRINSIC_FLAG_F64_FMUL }, + { "llvm.experimental.constrained.fdiv.f32", "aot_intrinsic_fdiv_f32", AOT_INTRINSIC_FLAG_F32_FDIV }, + { "llvm.experimental.constrained.fdiv.f64", "aot_intrinsic_fdiv_f64", AOT_INTRINSIC_FLAG_F64_FDIV }, { "llvm.fabs.f32", "aot_intrinsic_fabs_f32", AOT_INTRINSIC_FLAG_F32_FABS }, { "llvm.fabs.f64", "aot_intrinsic_fabs_f64", AOT_INTRINSIC_FLAG_F64_FABS }, { "llvm.ceil.f32", "aot_intrinsic_ceil_f32", AOT_INTRINSIC_FLAG_F32_CEIL }, { "llvm.ceil.f64", "aot_intrinsic_ceil_f64", AOT_INTRINSIC_FLAG_F64_CEIL }, - { "llvm.floor.f32", "aot_intrinsic_floor_f32", - AOT_INTRINSIC_FLAG_F32_FLOOR }, - { "llvm.floor.f64", "aot_intrinsic_floor_f64", - AOT_INTRINSIC_FLAG_F64_FLOOR }, - { "llvm.trunc.f32", "aot_intrinsic_trunc_f32", - AOT_INTRINSIC_FLAG_F32_TRUNC }, - { "llvm.trunc.f64", "aot_intrinsic_trunc_f64", - AOT_INTRINSIC_FLAG_F64_TRUNC }, + { "llvm.floor.f32", "aot_intrinsic_floor_f32", AOT_INTRINSIC_FLAG_F32_FLOOR }, + { "llvm.floor.f64", "aot_intrinsic_floor_f64", AOT_INTRINSIC_FLAG_F64_FLOOR }, + { "llvm.trunc.f32", "aot_intrinsic_trunc_f32", AOT_INTRINSIC_FLAG_F32_TRUNC }, + { "llvm.trunc.f64", "aot_intrinsic_trunc_f64", AOT_INTRINSIC_FLAG_F64_TRUNC }, { "llvm.rint.f32", "aot_intrinsic_rint_f32", AOT_INTRINSIC_FLAG_F32_RINT }, { "llvm.rint.f64", "aot_intrinsic_rint_f64", AOT_INTRINSIC_FLAG_F64_RINT }, { "llvm.sqrt.f32", "aot_intrinsic_sqrt_f32", AOT_INTRINSIC_FLAG_F32_SQRT }, { "llvm.sqrt.f64", "aot_intrinsic_sqrt_f64", AOT_INTRINSIC_FLAG_F64_SQRT }, - { "llvm.copysign.f32", "aot_intrinsic_copysign_f32", - AOT_INTRINSIC_FLAG_F32_COPYSIGN }, - { "llvm.copysign.f64", "aot_intrinsic_copysign_f64", - AOT_INTRINSIC_FLAG_F64_COPYSIGN }, + { "llvm.copysign.f32", "aot_intrinsic_copysign_f32", AOT_INTRINSIC_FLAG_F32_COPYSIGN }, + { "llvm.copysign.f64", "aot_intrinsic_copysign_f64", AOT_INTRINSIC_FLAG_F64_COPYSIGN }, { "llvm.minnum.f32", "aot_intrinsic_fmin_f32", AOT_INTRINSIC_FLAG_F32_MIN }, { "llvm.minnum.f64", "aot_intrinsic_fmin_f64", AOT_INTRINSIC_FLAG_F64_MIN }, { "llvm.maxnum.f32", "aot_intrinsic_fmax_f32", AOT_INTRINSIC_FLAG_F32_MAX }, @@ -58,23 +45,24 @@ static const aot_intrinsic g_intrinsic_mapping[] = { { "llvm.cttz.i64", "aot_intrinsic_ctz_i64", AOT_INTRINSIC_FLAG_I64_CTZ }, { "llvm.ctpop.i32", "aot_intrinsic_popcnt_i32", AOT_INTRINSIC_FLAG_I32_POPCNT }, { "llvm.ctpop.i64", "aot_intrinsic_popcnt_i64", AOT_INTRINSIC_FLAG_I64_POPCNT }, - { "f64_convert_i32_s", "aot_intrinsic_i32_to_f64", AOT_INTRINSIC_FLAG_I32_TO_F64}, - { "f64_convert_i32_u", "aot_intrinsic_u32_to_f64", AOT_INTRINSIC_FLAG_U32_TO_F64}, - { "f32_convert_i32_s", "aot_intrinsic_i32_to_f32", AOT_INTRINSIC_FLAG_I32_TO_F32}, - { "f32_convert_i32_u", "aot_intrinsic_u32_to_f32", AOT_INTRINSIC_FLAG_U32_TO_F32}, - { "f64_convert_i64_s", "aot_intrinsic_i64_to_f64", AOT_INTRINSIC_FLAG_I32_TO_F64}, - { "f64_convert_i64_u", "aot_intrinsic_u64_to_f64", AOT_INTRINSIC_FLAG_U64_TO_F64}, - { "f32_convert_i64_s", "aot_intrinsic_i64_to_f32", AOT_INTRINSIC_FLAG_I64_TO_F32}, - { "f32_convert_i64_u", "aot_intrinsic_u64_to_f32", AOT_INTRINSIC_FLAG_U64_TO_F32}, - { "i32_trunc_f64_u", "aot_intrinsic_f64_to_u32", AOT_INTRINSIC_FLAG_I32_TO_F64}, - { "f32_demote_f64", "aot_intrinsic_f64_to_f32", AOT_INTRINSIC_FLAG_F64_TO_F32}, - { "f64_promote_f32", "aot_intrinsic_f32_to_f64", AOT_INTRINSIC_FLAG_F32_TO_F64}, - { "f32_cmp", "aot_intrinsic_f32_cmp", AOT_INTRINSIC_FLAG_F32_CMP}, - { "f64_cmp", "aot_intrinsic_f64_cmp", AOT_INTRINSIC_FLAG_F64_CMP}, + { "f64_convert_i32_s", "aot_intrinsic_i32_to_f64", AOT_INTRINSIC_FLAG_I32_TO_F64 }, + { "f64_convert_i32_u", "aot_intrinsic_u32_to_f64", AOT_INTRINSIC_FLAG_U32_TO_F64 }, + { "f32_convert_i32_s", "aot_intrinsic_i32_to_f32", AOT_INTRINSIC_FLAG_I32_TO_F32 }, + { "f32_convert_i32_u", "aot_intrinsic_u32_to_f32", AOT_INTRINSIC_FLAG_U32_TO_F32 }, + { "f64_convert_i64_s", "aot_intrinsic_i64_to_f64", AOT_INTRINSIC_FLAG_I32_TO_F64 }, + { "f64_convert_i64_u", "aot_intrinsic_u64_to_f64", AOT_INTRINSIC_FLAG_U64_TO_F64 }, + { "f32_convert_i64_s", "aot_intrinsic_i64_to_f32", AOT_INTRINSIC_FLAG_I64_TO_F32 }, + { "f32_convert_i64_u", "aot_intrinsic_u64_to_f32", AOT_INTRINSIC_FLAG_U64_TO_F32 }, + { "i32_trunc_f64_u", "aot_intrinsic_f64_to_u32", AOT_INTRINSIC_FLAG_I32_TO_F64 }, + { "f32_demote_f64", "aot_intrinsic_f64_to_f32", AOT_INTRINSIC_FLAG_F64_TO_F32 }, + { "f64_promote_f32", "aot_intrinsic_f32_to_f64", AOT_INTRINSIC_FLAG_F32_TO_F64 }, + { "f32_cmp", "aot_intrinsic_f32_cmp", AOT_INTRINSIC_FLAG_F32_CMP }, + { "f64_cmp", "aot_intrinsic_f64_cmp", AOT_INTRINSIC_FLAG_F64_CMP }, }; +/* clang-format on */ static const uint32 g_intrinsic_count = - sizeof(g_intrinsic_mapping) / sizeof(aot_intrinsic); + sizeof(g_intrinsic_mapping) / sizeof(aot_intrinsic); float32 aot_intrinsic_fadd_f32(float32 a, float32 b) @@ -223,7 +211,7 @@ float64 aot_intrinsic_fmin_f64(float64 a, float64 b) { float64 c = fmin(a, b); - if (c==0 && a==b) + if (c == 0 && a == b) return signbit(a) ? a : b; return c; } @@ -243,7 +231,7 @@ float64 aot_intrinsic_fmax_f64(float64 a, float64 b) { float64 c = fmax(a, b); - if (c==0 && a==b) + if (c == 0 && a == b) return signbit(a) ? b : a; return c; } @@ -600,7 +588,8 @@ aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx) return; if (!strncmp(comp_ctx->target_arch, "thumb", 5)) { - if (!strcmp(comp_ctx->target_cpu, "cortex-m7")) {} + if (!strcmp(comp_ctx->target_cpu, "cortex-m7")) { + } else if (!strcmp(comp_ctx->target_cpu, "cortex-m4")) { add_f64_common_intrinsics(comp_ctx); } @@ -611,7 +600,7 @@ aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx) } } else if (!strncmp(comp_ctx->target_arch, "riscv", 5)) { - /* + /* * Note: Use builtin intrinsics since hardware float operation * will cause rodata relocation */ diff --git a/core/iwasm/aot/aot_intrinsic.h b/core/iwasm/aot/aot_intrinsic.h index 84a8be830f..3446ad558c 100644 --- a/core/iwasm/aot/aot_intrinsic.h +++ b/core/iwasm/aot/aot_intrinsic.h @@ -22,14 +22,15 @@ extern "C" { * - The lower 48 bits are the intrinsic capability mask */ -#define AOT_INTRINSIC_FLAG(group, number) \ +#define AOT_INTRINSIC_FLAG(group, number) \ ((((uint64)(group & 0xffffLL)) << 48) | ((uint64)1 << number)) #define AOT_INTRINSIC_FLAG_MASK (0x0000ffffffffffffLL) -#define AOT_INTRINSIC_GET_GROUP_FROM_FLAG(flag) \ +#define AOT_INTRINSIC_GET_GROUP_FROM_FLAG(flag) \ ((((uint64)flag) >> 48) & 0xffffLL) +/* clang-format off */ #define AOT_INTRINSIC_FLAG_F32_FADD AOT_INTRINSIC_FLAG(0, 0) #define AOT_INTRINSIC_FLAG_F32_FSUB AOT_INTRINSIC_FLAG(0, 1) #define AOT_INTRINSIC_FLAG_F32_FMUL AOT_INTRINSIC_FLAG(0, 2) @@ -83,6 +84,7 @@ extern "C" { #define AOT_INTRINSIC_FLAG_F64_TO_U64 AOT_INTRINSIC_FLAG(1, 23) #define AOT_INTRINSIC_FLAG_F64_TO_F32 AOT_INTRINSIC_FLAG(1, 24) #define AOT_INTRINSIC_FLAG_F64_CMP AOT_INTRINSIC_FLAG(1, 25) +/* clang-format on */ float32 aot_intrinsic_fadd_f32(float32 a, float32 b); diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 8c296f0eee..1923a6a2d9 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -26,14 +26,13 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { if (error_buf != NULL) { - snprintf(error_buf, error_buf_size, - "AOT module load failed: %s", string); + snprintf(error_buf, error_buf_size, "AOT module load failed: %s", + string); } } static void -set_error_buf_v(char *error_buf, uint32 error_buf_size, - const char *format, ...) +set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...) { va_list args; char buf[128]; @@ -42,8 +41,7 @@ set_error_buf_v(char *error_buf, uint32 error_buf_size, va_start(args, format); vsnprintf(buf, sizeof(buf), format, args); va_end(args); - snprintf(error_buf, error_buf_size, - "AOT module load failed: %s", buf); + snprintf(error_buf, error_buf_size, "AOT module load failed: %s", buf); } } @@ -99,110 +97,113 @@ check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length, return true; } -#define CHECK_BUF(buf, buf_end, length) do { \ - if (!check_buf(buf, buf_end, length, \ - error_buf, error_buf_size)) { \ - goto fail; \ - } \ -} while (0) +#define CHECK_BUF(buf, buf_end, length) \ + do { \ + if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \ + goto fail; \ + } \ + } while (0) -static uint8* +static uint8 * align_ptr(const uint8 *p, uint32 b) { uintptr_t v = (uintptr_t)p; uintptr_t m = b - 1; - return (uint8*)((v + m) & ~m); + return (uint8 *)((v + m) & ~m); } static inline uint64 GET_U64_FROM_ADDR(uint32 *addr) { - union { uint64 val; uint32 parts[2]; } u; + union { + uint64 val; + uint32 parts[2]; + } u; u.parts[0] = addr[0]; u.parts[1] = addr[1]; return u.val; } -#define TEMPLATE_READ(p, p_end, res, type) do { \ - if (sizeof(type) != sizeof(uint64)) \ - p = (uint8*)align_ptr(p, sizeof(type)); \ - else \ - /* align 4 bytes if type is uint64 */ \ - p = (uint8*)align_ptr(p, sizeof(uint32)); \ - CHECK_BUF(p, p_end, sizeof(type)); \ - if (sizeof(type) != sizeof(uint64)) \ - res = *(type*)p; \ - else \ - res = (type)GET_U64_FROM_ADDR((uint32*)p); \ - if (!is_little_endian()) \ - exchange_##type((uint8*)&res); \ - p += sizeof(type); \ - } while (0) +#define TEMPLATE_READ(p, p_end, res, type) \ + do { \ + if (sizeof(type) != sizeof(uint64)) \ + p = (uint8 *)align_ptr(p, sizeof(type)); \ + else \ + /* align 4 bytes if type is uint64 */ \ + p = (uint8 *)align_ptr(p, sizeof(uint32)); \ + CHECK_BUF(p, p_end, sizeof(type)); \ + if (sizeof(type) != sizeof(uint64)) \ + res = *(type *)p; \ + else \ + res = (type)GET_U64_FROM_ADDR((uint32 *)p); \ + if (!is_little_endian()) \ + exchange_##type((uint8 *)&res); \ + p += sizeof(type); \ + } while (0) #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8) #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16) #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32) #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64) -#define read_byte_array(p, p_end, addr, len) do { \ - CHECK_BUF(p, p_end, len); \ - memcpy(addr, p, len); \ - p += len; \ - } while (0) - -#define read_string(p, p_end, str) do { \ - uint16 str_len; \ - read_uint16(p, p_end, str_len); \ - CHECK_BUF(p, p_end, str_len); \ - if (!(str = const_str_set_insert \ - (p, str_len, module, \ - error_buf, error_buf_size))) { \ - goto fail; \ - } \ - p += str_len; \ - } while (0) +#define read_byte_array(p, p_end, addr, len) \ + do { \ + CHECK_BUF(p, p_end, len); \ + memcpy(addr, p, len); \ + p += len; \ + } while (0) + +#define read_string(p, p_end, str) \ + do { \ + uint16 str_len; \ + read_uint16(p, p_end, str_len); \ + CHECK_BUF(p, p_end, str_len); \ + if (!(str = const_str_set_insert(p, str_len, module, error_buf, \ + error_buf_size))) { \ + goto fail; \ + } \ + p += str_len; \ + } while (0) /* Legal values for bin_type */ -#define BIN_TYPE_ELF32L 0 /* 32-bit little endian */ -#define BIN_TYPE_ELF32B 1 /* 32-bit big endian */ -#define BIN_TYPE_ELF64L 2 /* 64-bit little endian */ -#define BIN_TYPE_ELF64B 3 /* 64-bit big endian */ -#define BIN_TYPE_COFF64 6 /* 64-bit little endian */ +#define BIN_TYPE_ELF32L 0 /* 32-bit little endian */ +#define BIN_TYPE_ELF32B 1 /* 32-bit big endian */ +#define BIN_TYPE_ELF64L 2 /* 64-bit little endian */ +#define BIN_TYPE_ELF64B 3 /* 64-bit big endian */ +#define BIN_TYPE_COFF64 6 /* 64-bit little endian */ /* Legal values for e_type (object file type). */ -#define E_TYPE_NONE 0 /* No file type */ -#define E_TYPE_REL 1 /* Relocatable file */ -#define E_TYPE_EXEC 2 /* Executable file */ -#define E_TYPE_DYN 3 /* Shared object file */ +#define E_TYPE_NONE 0 /* No file type */ +#define E_TYPE_REL 1 /* Relocatable file */ +#define E_TYPE_EXEC 2 /* Executable file */ +#define E_TYPE_DYN 3 /* Shared object file */ /* Legal values for e_machine (architecture). */ -#define E_MACHINE_386 3 /* Intel 80386 */ -#define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */ -#define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ -#define E_MACHINE_ARM 40 /* ARM/Thumb */ -#define E_MACHINE_AARCH64 183 /* AArch64 */ -#define E_MACHINE_ARC 45 /* Argonaut RISC Core */ -#define E_MACHINE_IA_64 50 /* Intel Merced */ -#define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */ -#define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */ -#define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */ +#define E_MACHINE_386 3 /* Intel 80386 */ +#define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */ +#define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ +#define E_MACHINE_ARM 40 /* ARM/Thumb */ +#define E_MACHINE_AARCH64 183 /* AArch64 */ +#define E_MACHINE_ARC 45 /* Argonaut RISC Core */ +#define E_MACHINE_IA_64 50 /* Intel Merced */ +#define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */ +#define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */ +#define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */ #define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */ -#define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */ -#define E_MACHINE_RISCV 243 /* RISC-V 32/64 */ +#define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */ +#define E_MACHINE_RISCV 243 /* RISC-V 32/64 */ #define E_MACHINE_WIN_X86_64 0x8664 /* Windowx x86-64 architecture */ /* Legal values for e_version */ -#define E_VERSION_CURRENT 1 /* Current version */ +#define E_VERSION_CURRENT 1 /* Current version */ static void * loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); return NULL; } @@ -210,15 +211,14 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) return mem; } -static char* +static char * const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, - char* error_buf, uint32 error_buf_size) + char *error_buf, uint32 error_buf_size) { HashMap *set = module->const_str_set; char *c_str, *value; - if (!(c_str = loader_malloc((uint32)len + 1, - error_buf, error_buf_size))) { + if (!(c_str = loader_malloc((uint32)len + 1, error_buf, error_buf_size))) { return NULL; } @@ -241,9 +241,9 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, } static bool -get_aot_file_target(AOTTargetInfo *target_info, - char *target_buf, uint32 target_buf_size, - char *error_buf, uint32 error_buf_size) +get_aot_file_target(AOTTargetInfo *target_info, char *target_buf, + uint32 target_buf_size, char *error_buf, + uint32 error_buf_size) { char *machine_type = NULL; switch (target_info->e_machine) { @@ -273,14 +273,14 @@ get_aot_file_target(AOTTargetInfo *target_info, break; default: set_error_buf_v(error_buf, error_buf_size, - "unknown machine type %d", - target_info->e_machine); + "unknown machine type %d", target_info->e_machine); return false; } if (strncmp(target_info->arch, machine_type, strlen(machine_type))) { - set_error_buf_v(error_buf, error_buf_size, - "machine type (%s) isn't consistent with target type (%s)", - machine_type, target_info->arch); + set_error_buf_v( + error_buf, error_buf_size, + "machine type (%s) isn't consistent with target type (%s)", + machine_type, target_info->arch); return false; } snprintf(target_buf, target_buf_size, "%s", target_info->arch); @@ -288,8 +288,8 @@ get_aot_file_target(AOTTargetInfo *target_info, } static bool -check_machine_info(AOTTargetInfo *target_info, - char *error_buf, uint32 error_buf_size) +check_machine_info(AOTTargetInfo *target_info, char *error_buf, + uint32 error_buf_size) { char target_expected[32], target_got[32]; @@ -311,8 +311,8 @@ check_machine_info(AOTTargetInfo *target_info, static bool load_target_info_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { AOTTargetInfo target_info; const uint8 *p = buf, *p_end = buf_end; @@ -325,12 +325,10 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end, read_uint32(p, p_end, target_info.e_version); read_uint32(p, p_end, target_info.e_flags); read_uint32(p, p_end, target_info.reserved); - read_byte_array(p, p_end, - target_info.arch, sizeof(target_info.arch)); + read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch)); if (p != buf_end) { - set_error_buf(error_buf, error_buf_size, - "invalid section size"); + set_error_buf(error_buf, error_buf_size, "invalid section size"); return false; } @@ -340,16 +338,17 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end, set_error_buf_v(error_buf, error_buf_size, "invalid target endian type, expected %s but got %s", is_little_endian() ? "little endian" : "big endian", - is_target_little_endian ? "little endian" : "big endian"); + is_target_little_endian ? "little endian" + : "big endian"); return false; } /* Check target bit width */ is_target_64_bit = target_info.bin_type & 2 ? true : false; - if ((sizeof(void*) == 8 ? true : false) != is_target_64_bit) { + if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) { set_error_buf_v(error_buf, error_buf_size, "invalid target bit width, expected %s but got %s", - sizeof(void*) == 8 ? "64-bit" : "32-bit", + sizeof(void *) == 8 ? "64-bit" : "32-bit", is_target_64_bit ? "64-bit" : "32-bit"); return false; } @@ -368,8 +367,7 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end, } if (target_info.e_version != E_VERSION_CURRENT) { - set_error_buf(error_buf, error_buf_size, - "invalid elf file version"); + set_error_buf(error_buf, error_buf_size, "invalid elf file version"); return false; } @@ -400,8 +398,8 @@ get_native_symbol_by_name(const char *name) static bool load_native_symbol_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; uint32 cnt; @@ -437,8 +435,7 @@ load_native_symbol_section(const uint8 *buf, const uint8 *buf_end, } static bool -load_custom_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, +load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -449,8 +446,8 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, switch (sub_section_type) { case AOT_CUSTOM_SECTION_NATIVE_SYMBOL: - if (!load_native_symbol_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_native_symbol_section(buf, buf_end, module, error_buf, + error_buf_size)) goto fail; break; default: @@ -463,8 +460,7 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, } static void -destroy_import_memories(AOTImportMemory *import_memories, - bool is_jit_mode) +destroy_import_memories(AOTImportMemory *import_memories, bool is_jit_mode) { if (!is_jit_mode) wasm_runtime_free(import_memories); @@ -485,8 +481,8 @@ destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count, static bool load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTMemInitData **data_list; @@ -496,7 +492,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count; if (!(module->mem_init_data_list = data_list = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -513,8 +509,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, read_uint64(buf, buf_end, init_expr_value); read_uint32(buf, buf_end, byte_count); size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count; - if (!(data_list[i] = loader_malloc - (size, error_buf, error_buf_size))) { + if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -526,8 +521,8 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, data_list[i]->offset.init_expr_type = (uint8)init_expr_type; data_list[i]->offset.u.i64 = (int64)init_expr_value; data_list[i]->byte_count = byte_count; - read_byte_array(buf, buf_end, - data_list[i]->bytes, data_list[i]->byte_count); + read_byte_array(buf, buf_end, data_list[i]->bytes, + data_list[i]->byte_count); } *p_buf = buf; @@ -537,8 +532,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_memory_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, +load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { uint32 i; @@ -552,7 +546,7 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, read_uint32(buf, buf_end, module->memory_count); total_size = sizeof(AOTMemory) * (uint64)module->memory_count; if (!(module->memories = - loader_malloc(total_size, error_buf, error_buf_size))) { + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -567,8 +561,8 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, /* load memory init data list */ if (module->mem_init_data_count > 0 - && !load_mem_init_data_list(&buf, buf_end, module, - error_buf, error_buf_size)) + && !load_mem_init_data_list(&buf, buf_end, module, error_buf, + error_buf_size)) return false; *p_buf = buf; @@ -605,10 +599,8 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count, } static bool -load_import_table_list(const uint8 **p_buf, - const uint8 *buf_end, - AOTModule *module, - char *error_buf, +load_import_table_list(const uint8 **p_buf, const uint8 *buf_end, + AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -619,7 +611,7 @@ load_import_table_list(const uint8 **p_buf, /* Allocate memory */ size = sizeof(AOTImportTable) * (uint64)module->import_table_count; if (!(module->import_tables = import_table = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -638,8 +630,8 @@ load_import_table_list(const uint8 **p_buf, } static bool -load_table_list(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, uint32 error_buf_size) +load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTTable *table; @@ -649,7 +641,7 @@ load_table_list(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTTable) * (uint64)module->table_count; if (!(module->tables = table = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -671,8 +663,8 @@ load_table_list(const uint8 **p_buf, const uint8 *buf_end, static bool load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTTableInitData **data_list; @@ -682,7 +674,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count; if (!(module->table_init_data_list = data_list = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -701,8 +693,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, size1 = sizeof(uint32) * (uint64)func_index_count; size = offsetof(AOTTableInitData, func_indexes) + size1; - if (!(data_list[i] = loader_malloc - (size, error_buf, error_buf_size))) { + if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -713,7 +704,8 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, data_list[i]->offset.init_expr_type = (uint8)init_expr_type; data_list[i]->offset.u.i64 = (int64)init_expr_value; data_list[i]->func_index_count = func_index_count; - read_byte_array(buf, buf_end, data_list[i]->func_indexes, (uint32)size1); + read_byte_array(buf, buf_end, data_list[i]->func_indexes, + (uint32)size1); } *p_buf = buf; @@ -723,8 +715,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_table_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, +load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -744,8 +735,8 @@ load_table_info(const uint8 **p_buf, const uint8 *buf_end, /* load table init data list */ if (module->table_init_data_count > 0 - && !load_table_init_data_list(&buf, buf_end, module, - error_buf, error_buf_size)) + && !load_table_init_data_list(&buf, buf_end, module, error_buf, + error_buf_size)) return false; *p_buf = buf; @@ -767,8 +758,7 @@ destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode) } static bool -load_func_types(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, +load_func_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -778,8 +768,8 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTFuncType *) * (uint64)module->func_type_count; - if (!(module->func_types = func_types = loader_malloc - (size, error_buf, error_buf_size))) { + if (!(module->func_types = func_types = + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -800,8 +790,7 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, size1 = (uint64)param_count + (uint64)result_count; size = offsetof(AOTFuncType, types) + size1; - if (!(func_types[i] = loader_malloc - (size, error_buf, error_buf_size))) { + if (!(func_types[i] = loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -810,8 +799,8 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1); param_cell_num = wasm_get_cell_num(func_types[i]->types, param_count); - ret_cell_num = wasm_get_cell_num(func_types[i]->types + param_count, - result_count); + ret_cell_num = + wasm_get_cell_num(func_types[i]->types + param_count, result_count); if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) { set_error_buf(error_buf, error_buf_size, "param count or result count too large"); @@ -830,8 +819,7 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, static bool load_func_type_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -857,8 +845,7 @@ destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode) static bool load_import_globals(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTImportGlobal *import_globals; @@ -871,13 +858,13 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count; if (!(module->import_globals = import_globals = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } /* Create each import global */ for (i = 0; i < module->import_global_count; i++) { - buf = (uint8*)align_ptr(buf, 2); + buf = (uint8 *)align_ptr(buf, 2); read_uint8(buf, buf_end, import_globals[i].type); read_uint8(buf, buf_end, import_globals[i].is_mutable); read_string(buf, buf_end, import_globals[i].module_name); @@ -885,9 +872,8 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end, #if WASM_ENABLE_LIBC_BUILTIN != 0 if (wasm_native_lookup_libc_builtin_global( - import_globals[i].module_name, - import_globals[i].global_name, - &tmp_global)) { + import_globals[i].module_name, import_globals[i].global_name, + &tmp_global)) { if (tmp_global.type != import_globals[i].type || tmp_global.is_mutable != import_globals[i].is_mutable) { set_error_buf(error_buf, error_buf_size, @@ -895,7 +881,7 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end, return false; } import_globals[i].global_data_linked = - tmp_global.global_data_linked; + tmp_global.global_data_linked; } #endif @@ -913,8 +899,8 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end, static bool load_import_global_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -922,8 +908,8 @@ load_import_global_info(const uint8 **p_buf, const uint8 *buf_end, /* load import globals */ if (module->import_global_count > 0 - && !load_import_globals(&buf, buf_end, module, - error_buf, error_buf_size)) + && !load_import_globals(&buf, buf_end, module, error_buf, + error_buf_size)) return false; *p_buf = buf; @@ -940,8 +926,7 @@ destroy_globals(AOTGlobal *globals, bool is_jit_mode) } static bool -load_globals(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, +load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -952,16 +937,16 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTGlobal) * (uint64)module->global_count; - if (!(module->globals = globals = loader_malloc - (size, error_buf, error_buf_size))) { + if (!(module->globals = globals = + loader_malloc(size, error_buf, error_buf_size))) { return false; } if (module->import_global_count > 0) { last_import_global = &module->import_globals[module->import_global_count - 1]; - data_offset = last_import_global->data_offset - + last_import_global->size; + data_offset = + last_import_global->data_offset + last_import_global->size; } /* Create each global */ @@ -997,8 +982,7 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_global_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, +load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -1017,16 +1001,14 @@ load_global_info(const uint8 **p_buf, const uint8 *buf_end, } static void -destroy_import_funcs(AOTImportFunc *import_funcs, - bool is_jit_mode) +destroy_import_funcs(AOTImportFunc *import_funcs, bool is_jit_mode) { if (!is_jit_mode) wasm_runtime_free(import_funcs); } static bool -load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, +load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const char *module_name, *field_name; @@ -1038,7 +1020,7 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTImportFunc) * (uint64)module->import_func_count; if (!(module->import_funcs = import_funcs = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -1049,18 +1031,17 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, set_error_buf(error_buf, error_buf_size, "unknown type"); return false; } - import_funcs[i].func_type = module->func_types[import_funcs[i].func_type_index]; + import_funcs[i].func_type = + module->func_types[import_funcs[i].func_type_index]; read_string(buf, buf_end, import_funcs[i].module_name); read_string(buf, buf_end, import_funcs[i].func_name); module_name = import_funcs[i].module_name; field_name = import_funcs[i].func_name; - import_funcs[i].func_ptr_linked = - wasm_native_resolve_symbol(module_name, field_name, - import_funcs[i].func_type, - &import_funcs[i].signature, - &import_funcs[i].attachment, - &import_funcs[i].call_conv_raw); + import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol( + module_name, field_name, import_funcs[i].func_type, + &import_funcs[i].signature, &import_funcs[i].attachment, + &import_funcs[i].call_conv_raw); #if WASM_ENABLE_LIBC_WASI != 0 if (!strcmp(import_funcs[i].module_name, "wasi_unstable") @@ -1077,8 +1058,7 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, static bool load_import_func_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -1086,8 +1066,7 @@ load_import_func_info(const uint8 **p_buf, const uint8 *buf_end, /* load import funcs */ if (module->import_func_count > 0 - && !load_import_funcs(&buf, buf_end, module, - error_buf, error_buf_size)) + && !load_import_funcs(&buf, buf_end, module, error_buf, error_buf_size)) return false; *p_buf = buf; @@ -1110,8 +1089,8 @@ destroy_object_data_sections(AOTObjectDataSection *data_sections, static bool load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTObjectDataSection *data_sections; @@ -1121,7 +1100,7 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count; if (!(module->data_sections = data_sections = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -1129,7 +1108,8 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, for (i = 0; i < module->data_section_count; i++) { int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE; #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64) + || defined(BUILD_TARGET_RISCV64_LP64D) \ + || defined(BUILD_TARGET_RISCV64_LP64) /* aot code and data in x86_64 must be in range 0 to 2G due to relocation for R_X86_64_32/32S/PC32 */ int map_flags = MMAP_MAP_32BIT; @@ -1142,10 +1122,9 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory for data */ if (data_sections[i].size > 0 - && !(data_sections[i].data = - os_mmap(NULL, data_sections[i].size, map_prot, map_flags))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + && !(data_sections[i].data = os_mmap(NULL, data_sections[i].size, + map_prot, map_flags))) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); return false; } #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) @@ -1157,8 +1136,8 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, #endif #endif - read_byte_array(buf, buf_end, - data_sections[i].data, data_sections[i].size); + read_byte_array(buf, buf_end, data_sections[i].data, + data_sections[i].size); } *p_buf = buf; @@ -1169,8 +1148,8 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, static bool load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf = *p_buf; @@ -1178,8 +1157,8 @@ load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end, /* load object data sections */ if (module->data_section_count > 0 - && !load_object_data_sections(&buf, buf_end, module, - error_buf, error_buf_size)) + && !load_object_data_sections(&buf, buf_end, module, error_buf, + error_buf_size)) return false; *p_buf = buf; @@ -1190,15 +1169,16 @@ load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end, static bool load_init_data_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size) || !load_table_info(&p, p_end, module, error_buf, error_buf_size) || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size) - || !load_import_global_info(&p, p_end, module, error_buf, error_buf_size) + || !load_import_global_info(&p, p_end, module, error_buf, + error_buf_size) || !load_global_info(&p, p_end, module, error_buf, error_buf_size) || !load_import_func_info(&p, p_end, module, error_buf, error_buf_size)) return false; @@ -1209,8 +1189,8 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, /* check start function index */ if (module->start_func_index != (uint32)-1 - && (module->start_func_index >= module->import_func_count - + module->func_count)) { + && (module->start_func_index + >= module->import_func_count + module->func_count)) { set_error_buf(error_buf, error_buf_size, "invalid start function index"); return false; @@ -1224,8 +1204,8 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, read_uint32(p, p_end, module->aux_stack_bottom); read_uint32(p, p_end, module->aux_stack_size); - if (!load_object_data_sections_info(&p, p_end, module, - error_buf, error_buf_size)) + if (!load_object_data_sections_info(&p, p_end, module, error_buf, + error_buf_size)) return false; if (p != p_end) { @@ -1240,15 +1220,13 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, } static bool -load_text_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, +load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { uint8 *plt_base; if (module->func_count > 0 && buf_end == buf) { - set_error_buf(error_buf, error_buf_size, - "invalid code size"); + set_error_buf(error_buf, error_buf_size, "invalid code size"); return false; } @@ -1256,9 +1234,9 @@ load_text_section(const uint8 *buf, const uint8 *buf_end, read_uint32(buf, buf_end, module->literal_size); /* literal data is at begining of the text section */ - module->literal = (uint8*)buf; - module->code = (void*)(buf + module->literal_size); - module->code_size = (uint32)(buf_end - (uint8*)module->code); + module->literal = (uint8 *)buf; + module->code = (void *)(buf + module->literal_size); + module->code_size = (uint32)(buf_end - (uint8 *)module->code); #if WASM_ENABLE_DEBUG_AOT != 0 module->elf_size = module->code_size; @@ -1289,8 +1267,7 @@ load_text_section(const uint8 *buf, const uint8 *buf_end, } static bool -load_function_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, +load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -1304,29 +1281,29 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, #endif #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) - unwind_info= (AOTUnwindInfo *)((uint8*)module->code + module->code_size - - sizeof(AOTUnwindInfo)); + unwind_info = (AOTUnwindInfo *)((uint8 *)module->code + module->code_size + - sizeof(AOTUnwindInfo)); unwind_info->Version = 1; unwind_info->Flags = UNW_FLAG_NHANDLER; - *(uint32*)&unwind_info->UnwindCode[0] = unwind_code_offset; + *(uint32 *)&unwind_info->UnwindCode[0] = unwind_code_offset; size = sizeof(RUNTIME_FUNCTION) * (uint64)module->func_count; if (size > 0 && !(rtl_func_table = module->rtl_func_table = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } #endif - size = sizeof(void*) * (uint64)module->func_count; + size = sizeof(void *) * (uint64)module->func_count; if (size > 0 - && !(module->func_ptrs = loader_malloc - (size, error_buf, error_buf_size))) { + && !(module->func_ptrs = + loader_malloc(size, error_buf, error_buf_size))) { return false; } for (i = 0; i < module->func_count; i++) { - if (sizeof(void*) == 8) { + if (sizeof(void *) == 8) { read_uint64(p, p_end, text_offset); } else { @@ -1339,10 +1316,10 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, "invalid function code offset"); return false; } - module->func_ptrs[i] = (uint8*)module->code + text_offset; + module->func_ptrs[i] = (uint8 *)module->code + text_offset; #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP) /* bits[0] of thumb function address must be 1 */ - module->func_ptrs[i] = (void*)((uintptr_t)module->func_ptrs[i] | 1); + module->func_ptrs[i] = (void *)((uintptr_t)module->func_ptrs[i] | 1); #endif #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) rtl_func_table[i].BeginAddress = (DWORD)text_offset; @@ -1356,7 +1333,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) if (module->func_count > 0) { rtl_func_table[module->func_count - 1].EndAddress = - (DWORD)(module->code_size - get_plt_table_size()); + (DWORD)(module->code_size - get_plt_table_size()); if (!RtlAddFunctionTable(rtl_func_table, module->func_count, (DWORD64)(uintptr_t)module->code)) { @@ -1373,7 +1350,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (module->start_func_index >= module->import_func_count) module->start_function = module->func_ptrs[module->start_func_index - - module->import_func_count]; + - module->import_func_count]; else /* TODO: fix start function can be import function issue */ module->start_function = NULL; @@ -1384,8 +1361,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, size = sizeof(uint32) * (uint64)module->func_count; if (size > 0 - && !(module->func_type_indexes = loader_malloc - (size, error_buf, error_buf_size))) { + && !(module->func_type_indexes = + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -1416,8 +1393,8 @@ destroy_exports(AOTExport *exports, bool is_jit_mode) } static bool -load_exports(const uint8 **p_buf, const uint8 *buf_end, - AOTModule *module, char *error_buf, uint32 error_buf_size) +load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, + char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; AOTExport *exports; @@ -1427,7 +1404,7 @@ load_exports(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTExport) * (uint64)module->export_count; if (!(module->exports = exports = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { return false; } @@ -1453,8 +1430,7 @@ load_exports(const uint8 **p_buf, const uint8 *buf_end, } static bool -load_export_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, +load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { const uint8 *p = buf, *p_end = buf_end; @@ -1466,8 +1442,7 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, return false; if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "invalid export section size"); + set_error_buf(error_buf, error_buf_size, "invalid export section size"); return false; } @@ -1476,7 +1451,6 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, return false; } - static void * get_data_section_addr(AOTModule *module, const char *section_name, uint32 *p_data_size) @@ -1501,8 +1475,8 @@ resolve_target_sym(const char *symbol, int32 *p_index) uint32 i, num = 0; SymbolMap *target_sym_map; - if (!(target_sym_map = get_target_symbol_map(&num))) - return NULL; + if (!(target_sym_map = get_target_symbol_map(&num))) + return NULL; for (i = 0; i < num; i++) if (!strcmp(target_sym_map[i].symbol_name, symbol)) { @@ -1567,18 +1541,18 @@ str2uint64(const char *buf, uint64 *p_res) #endif static bool -do_text_relocation(AOTModule *module, - AOTRelocationGroup *group, +do_text_relocation(AOTModule *module, AOTRelocationGroup *group, char *error_buf, uint32 error_buf_size) { bool is_literal = is_literal_relocation(group->section_name); uint8 *aot_text = is_literal ? module->literal : module->code; - uint32 aot_text_size = is_literal ? module->literal_size : module->code_size; + uint32 aot_text_size = + is_literal ? module->literal_size : module->code_size; uint32 i, func_index, symbol_len; #if defined(BH_PLATFORM_WINDOWS) uint32 xmm_plt_index = 0, real_plt_index = 0, float_plt_index = 0; #endif - char symbol_buf[128] = { 0 }, *symbol, *p; + char symbol_buf[128] = { 0 }, *symbol, *p; void *symbol_addr; AOTRelocation *relocation = group->relocations; @@ -1594,8 +1568,8 @@ do_text_relocation(AOTModule *module, if (symbol_len + 1 <= sizeof(symbol_buf)) symbol = symbol_buf; else { - if (!(symbol = loader_malloc(symbol_len + 1, - error_buf, error_buf_size))) { + if (!(symbol = loader_malloc(symbol_len + 1, error_buf, + error_buf_size))) { return false; } } @@ -1615,8 +1589,7 @@ do_text_relocation(AOTModule *module, else if (!strcmp(symbol, ".text")) { symbol_addr = module->code; } - else if (!strcmp(symbol, ".data") - || !strcmp(symbol, ".sdata") + else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata") || !strcmp(symbol, ".rdata") || !strcmp(symbol, ".rodata") /* ".rodata.cst4/8/16/.." */ @@ -1645,7 +1618,7 @@ do_text_relocation(AOTModule *module, symbol_addr = module->extra_plt_data + xmm_plt_index * 16; bh_memcpy_s(xmm_buf, sizeof(xmm_buf), symbol + strlen(XMM_PLT_PREFIX) + 16, 16); - if (!str2uint64(xmm_buf, (uint64*)symbol_addr)) { + if (!str2uint64(xmm_buf, (uint64 *)symbol_addr)) { set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; @@ -1653,7 +1626,7 @@ do_text_relocation(AOTModule *module, bh_memcpy_s(xmm_buf, sizeof(xmm_buf), symbol + strlen(XMM_PLT_PREFIX), 16); - if (!str2uint64(xmm_buf, (uint64*)((uint8*)symbol_addr + 8))) { + if (!str2uint64(xmm_buf, (uint64 *)((uint8 *)symbol_addr + 8))) { set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; @@ -1669,7 +1642,7 @@ do_text_relocation(AOTModule *module, + real_plt_index * 8; bh_memcpy_s(real_buf, sizeof(real_buf), symbol + strlen(REAL_PLT_PREFIX), 16); - if (!str2uint64(real_buf, (uint64*)symbol_addr)) { + if (!str2uint64(real_buf, (uint64 *)symbol_addr)) { set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; @@ -1685,7 +1658,7 @@ do_text_relocation(AOTModule *module, + module->real_plt_count * 8 + float_plt_index * 4; bh_memcpy_s(float_buf, sizeof(float_buf), symbol + strlen(REAL_PLT_PREFIX), 8); - if (!str2uint32(float_buf, (uint32*)symbol_addr)) { + if (!str2uint32(float_buf, (uint32 *)symbol_addr)) { set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; @@ -1702,13 +1675,10 @@ do_text_relocation(AOTModule *module, if (symbol != symbol_buf) wasm_runtime_free(symbol); - if (!apply_relocation(module, - aot_text, aot_text_size, - relocation->relocation_offset, - relocation->relocation_addend, - relocation->relocation_type, - symbol_addr, symbol_index, - error_buf, error_buf_size)) + if (!apply_relocation( + module, aot_text, aot_text_size, relocation->relocation_offset, + relocation->relocation_addend, relocation->relocation_type, + symbol_addr, symbol_index, error_buf, error_buf_size)) return false; } @@ -1721,8 +1691,7 @@ do_text_relocation(AOTModule *module, } static bool -do_data_relocation(AOTModule *module, - AOTRelocationGroup *group, +do_data_relocation(AOTModule *module, AOTRelocationGroup *group, char *error_buf, uint32 error_buf_size) { @@ -1747,8 +1716,7 @@ do_data_relocation(AOTModule *module, return false; } - data_addr = get_data_section_addr(module, data_section_name, - &data_size); + data_addr = get_data_section_addr(module, data_section_name, &data_size); if (group->relocation_count > 0 && !data_addr) { set_error_buf(error_buf, error_buf_size, @@ -1767,13 +1735,10 @@ do_data_relocation(AOTModule *module, return false; } - if (!apply_relocation(module, - data_addr, data_size, - relocation->relocation_offset, - relocation->relocation_addend, - relocation->relocation_type, - symbol_addr, -1, - error_buf, error_buf_size)) + if (!apply_relocation( + module, data_addr, data_size, relocation->relocation_offset, + relocation->relocation_addend, relocation->relocation_type, + symbol_addr, -1, error_buf, error_buf_size)) return false; } @@ -1781,8 +1746,7 @@ do_data_relocation(AOTModule *module, } static bool -validate_symbol_table(uint8 *buf, uint8 *buf_end, - uint32 *offsets, uint32 count, +validate_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *offsets, uint32 count, char *error_buf, uint32 error_buf_size) { uint32 i, str_len_addr = 0; @@ -1796,7 +1760,7 @@ validate_symbol_table(uint8 *buf, uint8 *buf_end, str_len_addr += (uint32)sizeof(uint16) + str_len; str_len_addr = align_uint(str_len_addr, 2); buf += str_len; - buf = (uint8*)align_ptr(buf, 2); + buf = (uint8 *)align_ptr(buf, 2); } if (buf == buf_end) @@ -1807,8 +1771,8 @@ validate_symbol_table(uint8 *buf, uint8 *buf_end, static bool load_relocation_section(const uint8 *buf, const uint8 *buf_end, - AOTModule *module, - char *error_buf, uint32 error_buf_size) + AOTModule *module, char *error_buf, + uint32 error_buf_size) { AOTRelocationGroup *groups = NULL, *group; uint32 symbol_count = 0; @@ -1831,9 +1795,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, symbol_buf = (uint8 *)buf; symbol_buf_end = symbol_buf + total_string_len; - if (!validate_symbol_table(symbol_buf, symbol_buf_end, - symbol_offsets, symbol_count, - error_buf, error_buf_size)) { + if (!validate_symbol_table(symbol_buf, symbol_buf_end, symbol_offsets, + symbol_count, error_buf, error_buf_size)) { set_error_buf(error_buf, error_buf_size, "validate symbol table failed"); goto fail; @@ -1849,7 +1812,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, uint8 *group_name; /* section name address is 4 bytes aligned. */ - buf = (uint8*)align_ptr(buf, sizeof(uint32)); + buf = (uint8 *)align_ptr(buf, sizeof(uint32)); read_uint32(buf, buf_end, name_index); if (name_index >= symbol_count) { @@ -1930,8 +1893,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, map_flags = MMAP_MAP_32BIT; if (size > UINT32_MAX - || !(module->extra_plt_data = os_mmap(NULL, (uint32)size, - map_prot, map_flags))) { + || !(module->extra_plt_data = + os_mmap(NULL, (uint32)size, map_prot, map_flags))) { set_error_buf(error_buf, error_buf_size, "mmap memory failed"); goto fail; } @@ -1957,7 +1920,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, uint8 *name_addr; /* section name address is 4 bytes aligned. */ - buf = (uint8*)align_ptr(buf, sizeof(uint32)); + buf = (uint8 *)align_ptr(buf, sizeof(uint32)); read_uint32(buf, buf_end, name_index); if (name_index >= symbol_count) { @@ -1969,10 +1932,9 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, name_addr = symbol_buf + symbol_offsets[name_index]; str_len = *(uint16 *)name_addr; - if (!(group->section_name = - const_str_set_insert(name_addr + sizeof(uint16), - (int32)str_len, module, - error_buf, error_buf_size))) { + if (!(group->section_name = const_str_set_insert( + name_addr + sizeof(uint16), (int32)str_len, module, error_buf, + error_buf_size))) { goto fail; } @@ -1981,7 +1943,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, /* Allocate memory for relocations */ size = sizeof(AOTRelocation) * (uint64)group->relocation_count; if (!(group->relocations = relocation = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { ret = false; goto fail; } @@ -2015,10 +1977,9 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, symbol_addr = symbol_buf + symbol_offsets[symbol_index]; str_len = *(uint16 *)symbol_addr; - if (!(relocation->symbol_name = - const_str_set_insert(symbol_addr + sizeof(uint16), - (int32)str_len, module, - error_buf, error_buf_size))) { + if (!(relocation->symbol_name = const_str_set_insert( + symbol_addr + sizeof(uint16), (int32)str_len, module, + error_buf, error_buf_size))) { goto fail; } } @@ -2029,7 +1990,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, #ifdef BH_PLATFORM_WINDOWS || !strcmp(group->section_name, ".text") #endif - ) { + ) { if (module->native_symbol_count > 0) { set_error_buf(error_buf, error_buf_size, "cannot apply relocation to text section " @@ -2052,8 +2013,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, if (module->code) { /* The layout is: literal size + literal + code (with plt table) */ uint8 *mmap_addr = module->literal - sizeof(uint32); - uint32 total_size = sizeof(uint32) - + module->literal_size + module->code_size; + uint32 total_size = + sizeof(uint32) + module->literal_size + module->code_size; os_mprotect(mmap_addr, total_size, map_prot); } @@ -2092,8 +2053,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, } static bool -load_from_sections(AOTModule *module, AOTSection *sections, - char *error_buf, uint32 error_buf_size) +load_from_sections(AOTModule *module, AOTSection *sections, char *error_buf, + uint32 error_buf_size) { AOTSection *section = sections; const uint8 *buf, *buf_end; @@ -2112,45 +2073,44 @@ load_from_sections(AOTModule *module, AOTSection *sections, || (last_section_type != (uint32)-1 && (section_type != last_section_type + 1 && section_type != AOT_SECTION_TYPE_CUSTOM))) { - set_error_buf(error_buf, error_buf_size, - "invalid section order"); + set_error_buf(error_buf, error_buf_size, "invalid section order"); return false; } last_section_type = section_type; switch (section_type) { case AOT_SECTION_TYPE_TARGET_INFO: - if (!load_target_info_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_target_info_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case AOT_SECTION_TYPE_INIT_DATA: - if (!load_init_data_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_init_data_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case AOT_SECTION_TYPE_TEXT: - if (!load_text_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_text_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case AOT_SECTION_TYPE_FUNCTION: - if (!load_function_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_function_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case AOT_SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_export_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case AOT_SECTION_TYPE_RELOCATION: - if (!load_relocation_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_relocation_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; case AOT_SECTION_TYPE_CUSTOM: - if (!load_custom_section(buf, buf_end, module, - error_buf, error_buf_size)) + if (!load_custom_section(buf, buf_end, module, error_buf, + error_buf_size)) return false; break; default: @@ -2164,8 +2124,7 @@ load_from_sections(AOTModule *module, AOTSection *sections, if (last_section_type != AOT_SECTION_TYPE_RELOCATION && last_section_type != AOT_SECTION_TYPE_CUSTOM) { - set_error_buf(error_buf, error_buf_size, - "section missing"); + set_error_buf(error_buf, error_buf_size, "section missing"); return false; } @@ -2182,8 +2141,7 @@ load_from_sections(AOTModule *module, AOTSection *sections, func_index = exports[i].index - module->import_func_count; func_type_index = module->func_type_indexes[func_index]; func_type = module->func_types[func_type_index]; - if (func_type->param_count == 1 - && func_type->result_count == 1 + 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) { bh_assert(module->malloc_func_index == (uint32)-1); @@ -2196,8 +2154,7 @@ load_from_sections(AOTModule *module, AOTSection *sections, func_index = exports[i].index - module->import_func_count; func_type_index = module->func_type_indexes[func_index]; func_type = module->func_types[func_type_index]; - if (func_type->param_count == 2 - && func_type->result_count == 1 + if (func_type->param_count == 2 && func_type->result_count == 1 && func_type->types[0] == VALUE_TYPE_I32 && func_type->types[1] == VALUE_TYPE_I32 && func_type->types[2] == VALUE_TYPE_I32) { @@ -2216,21 +2173,22 @@ load_from_sections(AOTModule *module, AOTSection *sections, if ((export_tmp->kind == EXPORT_KIND_FUNC) && (!strcmp(export_tmp->name, "__retain") || !strcmp(export_tmp->name, "__pin"))) { - func_index = export_tmp->index - - module->import_func_count; + func_index = + export_tmp->index - module->import_func_count; func_type_index = - module->func_type_indexes[func_index]; + module->func_type_indexes[func_index]; func_type = module->func_types[func_type_index]; 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) { - bh_assert( - module->retain_func_index == (uint32)-1); + bh_assert(module->retain_func_index + == (uint32)-1); module->retain_func_index = export_tmp->index; - LOG_VERBOSE( - "Found retain function, name: %s, index: %u", - export_tmp->name, export_tmp->index); + LOG_VERBOSE("Found retain function, name: %s, " + "index: %u", + export_tmp->name, + export_tmp->index); break; } } @@ -2248,8 +2206,7 @@ load_from_sections(AOTModule *module, AOTSection *sections, func_index = exports[i].index - module->import_func_count; func_type_index = module->func_type_indexes[func_index]; func_type = module->func_types[func_type_index]; - if (func_type->param_count == 1 - && func_type->result_count == 0 + if (func_type->param_count == 1 && func_type->result_count == 0 && func_type->types[0] == VALUE_TYPE_I32) { bh_assert(module->free_func_index == (uint32)-1); module->free_func_index = func_index; @@ -2265,19 +2222,20 @@ load_from_sections(AOTModule *module, AOTSection *sections, os_dcache_flush(); #if WASM_ENABLE_MEMORY_TRACING != 0 - wasm_runtime_dump_module_mem_consumption((WASMModuleCommon*)module); + wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module); #endif #if WASM_ENABLE_DEBUG_AOT != 0 if (!jit_code_entry_create(module->elf_hdr, module->elf_size)) { - set_error_buf(error_buf, error_buf_size, "create jit code entry failed"); + set_error_buf(error_buf, error_buf_size, + "create jit code entry failed"); return false; } #endif return true; } -static AOTModule* +static AOTModule * create_module(char *error_buf, uint32 error_buf_size) { AOTModule *module = @@ -2289,12 +2247,9 @@ create_module(char *error_buf, uint32 error_buf_size) module->module_type = Wasm_Module_AoT; - if (!(module->const_str_set = - bh_hash_map_create(32, false, - (HashFunc)wasm_string_hash, - (KeyEqualFunc)wasm_string_equal, - NULL, - wasm_runtime_free))) { + if (!(module->const_str_set = bh_hash_map_create( + 32, false, (HashFunc)wasm_string_hash, + (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) { set_error_buf(error_buf, error_buf_size, "create const string set failed"); wasm_runtime_free(module); @@ -2304,17 +2259,16 @@ create_module(char *error_buf, uint32 error_buf_size) return module; } -AOTModule* -aot_load_from_sections(AOTSection *section_list, - char *error_buf, uint32 error_buf_size) +AOTModule * +aot_load_from_sections(AOTSection *section_list, char *error_buf, + uint32 error_buf_size) { AOTModule *module = create_module(error_buf, error_buf_size); if (!module) return NULL; - if (!load_from_sections(module, section_list, - error_buf, error_buf_size)) { + if (!load_from_sections(module, section_list, error_buf, error_buf_size)) { aot_unload(module); return NULL; } @@ -2329,10 +2283,10 @@ destroy_sections(AOTSection *section_list, bool destroy_aot_text) AOTSection *section = section_list, *next; while (section) { next = section->next; - if (destroy_aot_text - && section->section_type == AOT_SECTION_TYPE_TEXT + if (destroy_aot_text && section->section_type == AOT_SECTION_TYPE_TEXT && section->section_body) - os_munmap((uint8*)section->section_body, section->section_body_size); + os_munmap((uint8 *)section->section_body, + section->section_body_size); wasm_runtime_free(section); section = next; } @@ -2376,10 +2330,9 @@ resolve_native_symbols(const uint8 *buf, uint32 size, uint32 *p_count, } static bool -create_sections(AOTModule *module, - const uint8 *buf, uint32 size, - AOTSection **p_section_list, - char *error_buf, uint32 error_buf_size) +create_sections(AOTModule *module, const uint8 *buf, uint32 size, + AOTSection **p_section_list, char *error_buf, + uint32 error_buf_size) { AOTSection *section_list = NULL, *section_list_end = NULL, *section; const uint8 *p = buf, *p_end = buf + size; @@ -2390,8 +2343,8 @@ create_sections(AOTModule *module, uint64 total_size; uint8 *aot_text; - if (!resolve_native_symbols(buf, size, &native_symbol_count, - error_buf, error_buf_size)) { + if (!resolve_native_symbols(buf, size, &native_symbol_count, error_buf, + error_buf_size)) { goto fail; } @@ -2405,30 +2358,31 @@ create_sections(AOTModule *module, read_uint32(p, p_end, section_size); CHECK_BUF(p, p_end, section_size); - if (!(section = - loader_malloc(sizeof(AOTSection), - error_buf, error_buf_size))) { + if (!(section = loader_malloc(sizeof(AOTSection), error_buf, + error_buf_size))) { goto fail; } memset(section, 0, sizeof(AOTSection)); section->section_type = (int32)section_type; - section->section_body = (uint8*)p; + section->section_body = (uint8 *)p; section->section_body_size = section_size; if (section_type == AOT_SECTION_TYPE_TEXT) { if ((section_size > 0) && (native_symbol_count == 0)) { - int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE - | MMAP_PROT_EXEC; + int map_prot = + MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64) - /* aot code and data in x86_64 must be in range 0 to 2G due to - relocation for R_X86_64_32/32S/PC32 */ + || defined(BUILD_TARGET_RISCV64_LP64D) \ + || defined(BUILD_TARGET_RISCV64_LP64) + /* aot code and data in x86_64 must be in range 0 to 2G due + to relocation for R_X86_64_32/32S/PC32 */ int map_flags = MMAP_MAP_32BIT; #else int map_flags = MMAP_MAP_NONE; #endif - total_size = (uint64)section_size + aot_get_plt_table_size(); + total_size = + (uint64)section_size + aot_get_plt_table_size(); total_size = (total_size + 3) & ~((uint64)3); if (total_size >= UINT32_MAX || !(aot_text = os_mmap(NULL, (uint32)total_size, @@ -2452,8 +2406,8 @@ create_sections(AOTModule *module, destory_aot_text = true; if ((uint32)total_size > section->section_body_size) { - memset(aot_text + (uint32)section_size, - 0, (uint32)total_size - section_size); + memset(aot_text + (uint32)section_size, 0, + (uint32)total_size - section_size); section->section_body_size = (uint32)total_size; } } @@ -2469,15 +2423,13 @@ create_sections(AOTModule *module, p += section_size; } else { - set_error_buf(error_buf, error_buf_size, - "invalid section id"); + set_error_buf(error_buf, error_buf_size, "invalid section id"); goto fail; } } if (!section_list) { - set_error_buf(error_buf, error_buf_size, - "create section list failed"); + set_error_buf(error_buf, error_buf_size, "create section list failed"); return false; } @@ -2490,8 +2442,8 @@ create_sections(AOTModule *module, } static bool -load(const uint8 *buf, uint32 size, AOTModule *module, - char *error_buf, uint32 error_buf_size) +load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf, + uint32 error_buf_size) { const uint8 *buf_end = buf + size; const uint8 *p = buf, *p_end = buf_end; @@ -2511,8 +2463,8 @@ load(const uint8 *buf, uint32 size, AOTModule *module, return false; } - if (!create_sections(module, buf, size, §ion_list, - error_buf, error_buf_size)) + if (!create_sections(module, buf, size, §ion_list, error_buf, + error_buf_size)) return false; ret = load_from_sections(module, section_list, error_buf, error_buf_size); @@ -2534,9 +2486,9 @@ load(const uint8 *buf, uint32 size, AOTModule *module, return false; } -AOTModule* -aot_load_from_aot_file(const uint8 *buf, uint32 size, - char *error_buf, uint32 error_buf_size) +AOTModule * +aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size) { AOTModule *module = create_module(error_buf, error_buf_size); @@ -2553,7 +2505,7 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, } #if WASM_ENABLE_JIT != 0 -static AOTModule* +static AOTModule * aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, char *error_buf, uint32 error_buf_size) { @@ -2571,7 +2523,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, /* Allocate memory for module */ if (!(module = - loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) { + loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) { return NULL; } @@ -2584,12 +2536,12 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, if (module->memory_count) { size = sizeof(AOTMemory) * (uint64)module->memory_count; if (!(module->memories = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { goto fail1; } - bh_memcpy_s(module->memories, (uint32)size, - comp_data->memories, (uint32)size); + bh_memcpy_s(module->memories, (uint32)size, comp_data->memories, + (uint32)size); } module->mem_init_data_list = comp_data->mem_init_data_list; @@ -2627,7 +2579,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, size = (uint64)module->func_count * sizeof(void *); if (size > 0 && !(module->func_ptrs = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { goto fail2; } @@ -2662,8 +2614,8 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, for (i = 0; i < comp_data->func_count; i++) { snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i); - if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->lazy_orcjit, - &func_addr, func_name))) { + if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->lazy_orcjit, &func_addr, + func_name))) { aot_handle_llvm_errmsg(error_buf, error_buf_size, "cannot lookup: ", error); goto fail3; @@ -2676,9 +2628,8 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, bh_assert(comp_ctx->exec_engine); for (i = 0; i < comp_data->func_count; i++) { snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i); - if (!(module->func_ptrs[i] = - (void *)LLVMGetFunctionAddress(comp_ctx->exec_engine, - func_name))) { + if (!(module->func_ptrs[i] = (void *)LLVMGetFunctionAddress( + comp_ctx->exec_engine, func_name))) { set_error_buf(error_buf, error_buf_size, "get function address failed"); goto fail3; @@ -2690,7 +2641,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, size = (uint64)module->func_count * sizeof(uint32); if (size > 0 && !(module->func_type_indexes = - loader_malloc(size, error_buf, error_buf_size))) { + loader_malloc(size, error_buf, error_buf_size))) { goto fail3; } for (i = 0; i < comp_data->func_count; i++) @@ -2701,8 +2652,8 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, module->start_func_index = comp_data->start_func_index; if (comp_data->start_func_index != (uint32)-1) { - bh_assert(comp_data->start_func_index < module->import_func_count - + module->func_count); + bh_assert(comp_data->start_func_index + < module->import_func_count + module->func_count); /* TODO: fix issue that start func cannot be import func */ if (comp_data->start_func_index >= module->import_func_count) { module->start_function = @@ -2754,9 +2705,9 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, return NULL; } -AOTModule* -aot_convert_wasm_module(WASMModule *wasm_module, - char *error_buf, uint32 error_buf_size) +AOTModule * +aot_convert_wasm_module(WASMModule *wasm_module, char *error_buf, + uint32 error_buf_size) { AOTCompData *comp_data; AOTCompContext *comp_ctx; @@ -2808,8 +2759,8 @@ aot_convert_wasm_module(WASMModule *wasm_module, goto fail2; } - aot_module = aot_load_from_comp_data(comp_data, comp_ctx, - error_buf, error_buf_size); + aot_module = + aot_load_from_comp_data(comp_data, comp_ctx, error_buf, error_buf_size); if (!aot_module) { goto fail2; } @@ -2839,8 +2790,7 @@ aot_unload(AOTModule *module) #endif if (module->import_memories) - destroy_import_memories(module->import_memories, - module->is_jit_mode); + destroy_import_memories(module->import_memories, module->is_jit_mode); if (module->memories) wasm_runtime_free(module->memories); @@ -2854,8 +2804,7 @@ aot_unload(AOTModule *module) wasm_runtime_free(module->native_symbol_list); if (module->import_tables) - destroy_import_tables(module->import_tables, - module->is_jit_mode); + destroy_import_tables(module->import_tables, module->is_jit_mode); if (module->tables) destroy_tables(module->tables, module->is_jit_mode); @@ -2866,25 +2815,20 @@ aot_unload(AOTModule *module) module->is_jit_mode); if (module->func_types) - destroy_func_types(module->func_types, - module->func_type_count, + destroy_func_types(module->func_types, module->func_type_count, module->is_jit_mode); if (module->import_globals) - destroy_import_globals(module->import_globals, - module->is_jit_mode); + destroy_import_globals(module->import_globals, module->is_jit_mode); if (module->globals) - destroy_globals(module->globals, - module->is_jit_mode); + destroy_globals(module->globals, module->is_jit_mode); if (module->import_funcs) - destroy_import_funcs(module->import_funcs, - module->is_jit_mode); + destroy_import_funcs(module->import_funcs, module->is_jit_mode); if (module->exports) - destroy_exports(module->exports, - module->is_jit_mode); + destroy_exports(module->exports, module->is_jit_mode); if (module->func_type_indexes) wasm_runtime_free(module->func_type_indexes); @@ -2898,8 +2842,8 @@ aot_unload(AOTModule *module) if (module->code && (module->native_symbol_count == 0)) { /* The layout is: literal size + literal + code (with plt table) */ uint8 *mmap_addr = module->literal - sizeof(uint32); - uint32 total_size = sizeof(uint32) - + module->literal_size + module->code_size; + uint32 total_size = + sizeof(uint32) + module->literal_size + module->code_size; os_munmap(mmap_addr, total_size); } @@ -2932,4 +2876,3 @@ aot_get_plt_table_size() { return get_plt_table_size(); } - diff --git a/core/iwasm/aot/aot_reloc.h b/core/iwasm/aot/aot_reloc.h index 73c498e711..f2296ba545 100644 --- a/core/iwasm/aot/aot_reloc.h +++ b/core/iwasm/aot/aot_reloc.h @@ -3,15 +3,23 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +#ifndef _AOT_RELOC_H_ +#define _AOT_RELOC_H_ + #include "aot_runtime.h" #include "aot_intrinsic.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { const char *symbol_name; void *symbol_addr; } SymbolMap; -#define REG_SYM(symbol) { #symbol, (void*)symbol } +/* clang-format off */ +#define REG_SYM(symbol) { #symbol, (void *)symbol } #if WASM_ENABLE_BULK_MEMORY != 0 #define REG_BULK_MEMORY_SYM() \ @@ -122,10 +130,11 @@ typedef struct { REG_AOT_TRACE_SYM() \ REG_INTRINSIC_SYM() \ -#define CHECK_RELOC_OFFSET(data_size) do { \ - if (!check_reloc_offset(target_section_size, reloc_offset, data_size, \ - error_buf, error_buf_size)) \ - return false; \ +#define CHECK_RELOC_OFFSET(data_size) do { \ + if (!check_reloc_offset(target_section_size, \ + reloc_offset, data_size, \ + error_buf, error_buf_size)) \ + return false; \ } while (0) SymbolMap * @@ -146,4 +155,10 @@ apply_relocation(AOTModule *module, uint64 reloc_offset, uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, int32 symbol_index, char *error_buf, uint32 error_buf_size); +/* clang-format off */ + +#ifdef __cplusplus +} +#endif +#endif /* end of _AOT_RELOC_H_ */ diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 2acf04edb5..28db2878e6 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -18,14 +18,13 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { if (error_buf != NULL) { - snprintf(error_buf, error_buf_size, - "AOT module instantiate failed: %s", string); + snprintf(error_buf, error_buf_size, "AOT module instantiate failed: %s", + string); } } static void -set_error_buf_v(char *error_buf, uint32 error_buf_size, - const char *format, ...) +set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...) { va_list args; char buf[128]; @@ -34,8 +33,8 @@ set_error_buf_v(char *error_buf, uint32 error_buf_size, va_start(args, format); vsnprintf(buf, sizeof(buf), format, args); va_end(args); - snprintf(error_buf, error_buf_size, - "AOT module instantiate failed: %s", buf); + snprintf(error_buf, error_buf_size, "AOT module instantiate failed: %s", + buf); } } @@ -44,10 +43,8 @@ runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); return NULL; } @@ -60,8 +57,8 @@ check_global_init_expr(const AOTModule *module, uint32 global_index, char *error_buf, uint32 error_buf_size) { if (global_index >= module->import_global_count + module->global_count) { - set_error_buf_v(error_buf, error_buf_size, - "unknown global %d", global_index); + set_error_buf_v(error_buf, error_buf_size, "unknown global %d", + global_index); return false; } @@ -83,8 +80,7 @@ check_global_init_expr(const AOTModule *module, uint32 global_index, } static void -init_global_data(uint8 *global_data, uint8 type, - WASMValue *initial_value) +init_global_data(uint8 *global_data, uint8 type, WASMValue *initial_value) { switch (type) { case VALUE_TYPE_I32: @@ -93,17 +89,17 @@ init_global_data(uint8 *global_data, uint8 type, case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: #endif - *(int32*)global_data = initial_value->i32; + *(int32 *)global_data = initial_value->i32; break; case VALUE_TYPE_I64: case VALUE_TYPE_F64: - bh_memcpy_s(global_data, sizeof(int64), - &initial_value->i64, sizeof(int64)); + bh_memcpy_s(global_data, sizeof(int64), &initial_value->i64, + sizeof(int64)); break; #if WASM_ENABLE_SIMD != 0 case VALUE_TYPE_V128: - bh_memcpy_s(global_data, sizeof(V128), - &initial_value->i64, sizeof(V128)); + bh_memcpy_s(global_data, sizeof(V128), &initial_value->i64, + sizeof(V128)); break; #endif default: @@ -117,14 +113,14 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, { uint32 i; InitializerExpression *init_expr; - uint8 *p = (uint8*)module_inst->global_data.ptr; + uint8 *p = (uint8 *)module_inst->global_data.ptr; AOTImportGlobal *import_global = module->import_globals; AOTGlobal *global = module->globals; /* Initialize import global data */ for (i = 0; i < module->import_global_count; i++, import_global++) { - bh_assert(import_global->data_offset == - (uint32)(p - (uint8*)module_inst->global_data.ptr)); + bh_assert(import_global->data_offset + == (uint32)(p - (uint8 *)module_inst->global_data.ptr)); init_global_data(p, import_global->type, &import_global->global_data_linked); p += import_global->size; @@ -132,8 +128,8 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, /* Initialize defined global data */ for (i = 0; i < module->global_count; i++, global++) { - bh_assert(global->data_offset == - (uint32)(p - (uint8*)module_inst->global_data.ptr)); + bh_assert(global->data_offset + == (uint32)(p - (uint8 *)module_inst->global_data.ptr)); init_expr = &global->init_expr; switch (init_expr->init_expr_type) { case INIT_EXPR_TYPE_GET_GLOBAL: @@ -142,9 +138,10 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, error_buf, error_buf_size)) { return false; } - init_global_data(p, global->type, - &module->import_globals[init_expr->u.global_index] - .global_data_linked); + init_global_data( + p, global->type, + &module->import_globals[init_expr->u.global_index] + .global_data_linked); break; } #if WASM_ENABLE_REF_TYPES != 0 @@ -163,8 +160,8 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, p += global->size; } - bh_assert(module_inst->global_data_size == - (uint32)(p - (uint8*)module_inst->global_data.ptr)); + bh_assert(module_inst->global_data_size + == (uint32)(p - (uint8 *)module_inst->global_data.ptr)); return true; } @@ -180,7 +177,7 @@ static inline AOTTableInstance * aot_get_table_inst(const AOTModuleInstance *module_inst, uint32 tbl_idx) { uint32 i = 0; - AOTTableInstance *tbl_inst = (AOTTableInstance*)module_inst->tables.ptr; + AOTTableInstance *tbl_inst = (AOTTableInstance *)module_inst->tables.ptr; while (i != tbl_idx) { tbl_inst = aot_next_tbl_inst(tbl_inst); @@ -196,7 +193,7 @@ table_instantiate(AOTModuleInstance *module_inst, AOTModule *module, { uint32 i, global_index, global_data_offset, base_offset, length; AOTTableInitData *table_seg; - AOTTableInstance *tbl_inst = (AOTTableInstance*)module_inst->tables.ptr; + AOTTableInstance *tbl_inst = (AOTTableInstance *)module_inst->tables.ptr; /* * treat import table like a local one until we enable module linking @@ -209,8 +206,7 @@ table_instantiate(AOTModuleInstance *module_inst, AOTModule *module, tbl_inst->max_size = aot_get_imp_tbl_data_slots(import_table); } else { - AOTTable *table = - module->tables + (i - module->import_table_count); + AOTTable *table = module->tables + (i - module->import_table_count); tbl_inst->cur_size = table->table_init_size; tbl_inst->max_size = aot_get_tbl_data_slots(table); } @@ -235,34 +231,30 @@ table_instantiate(AOTModuleInstance *module_inst, AOTModule *module, bh_assert(tbl_inst); bh_assert( - table_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST - || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL + table_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST + || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL #if WASM_ENABLE_REF_TYPES != 0 - || table_seg->offset.init_expr_type - == INIT_EXPR_TYPE_FUNCREF_CONST - || table_seg->offset.init_expr_type - == INIT_EXPR_TYPE_REFNULL_CONST + || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST + || table_seg->offset.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST #endif ); /* Resolve table data base offset */ - if (table_seg->offset.init_expr_type - == INIT_EXPR_TYPE_GET_GLOBAL) { + if (table_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) { global_index = table_seg->offset.u.global_index; - if (!check_global_init_expr(module, global_index, - error_buf, error_buf_size)) { + if (!check_global_init_expr(module, global_index, error_buf, + error_buf_size)) { return false; } if (global_index < module->import_global_count) global_data_offset = - module->import_globals[global_index].data_offset; + module->import_globals[global_index].data_offset; else global_data_offset = - module - ->globals[global_index - module->import_global_count] - .data_offset; + module->globals[global_index - module->import_global_count] + .data_offset; base_offset = *(uint32 *)((uint8 *)module_inst->global_data.ptr + global_data_offset); @@ -319,9 +311,8 @@ memories_deinstantiate(AOTModuleInstance *module_inst) if (memory_inst) { #if WASM_ENABLE_SHARED_MEMORY != 0 if (memory_inst->is_shared) { - int32 ref_count = - shared_memory_dec_reference( - (WASMModuleCommon *)module_inst->aot_module.ptr); + int32 ref_count = shared_memory_dec_reference( + (WASMModuleCommon *)module_inst->aot_module.ptr); bh_assert(ref_count >= 0); /* if the reference count is not zero, @@ -342,9 +333,9 @@ memories_deinstantiate(AOTModuleInstance *module_inst) #ifdef BH_PLATFORM_WINDOWS os_mem_decommit(memory_inst->memory_data.ptr, memory_inst->num_bytes_per_page - * memory_inst->cur_page_count); + * memory_inst->cur_page_count); #endif - os_munmap((uint8*)memory_inst->memory_data.ptr, + os_munmap((uint8 *)memory_inst->memory_data.ptr, 8 * (uint64)BH_GB); #endif } @@ -353,7 +344,7 @@ memories_deinstantiate(AOTModuleInstance *module_inst) wasm_runtime_free(module_inst->memories.ptr); } -static AOTMemoryInstance* +static AOTMemoryInstance * memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, AOTMemoryInstance *memory_inst, AOTMemory *memory, uint32 heap_size, char *error_buf, uint32 error_buf_size) @@ -364,7 +355,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, uint32 max_page_count = memory->mem_max_page_count; uint32 inc_page_count, aux_heap_base, global_idx; uint32 bytes_of_last_page, bytes_to_page_end; - uint32 heap_offset = num_bytes_per_page *init_page_count; + uint32 heap_offset = num_bytes_per_page * init_page_count; uint64 total_size; uint8 *p = NULL, *global_addr; #ifdef OS_ENABLE_HW_BOUND_CHECK @@ -385,11 +376,10 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, return the memory instance directly */ if (node) { uint32 ref_count; - ref_count = shared_memory_inc_reference( - (WASMModuleCommon *)module); + ref_count = shared_memory_inc_reference((WASMModuleCommon *)module); bh_assert(ref_count > 0); shared_memory_instance = - (AOTMemoryInstance *)shared_memory_get_memory_inst(node); + (AOTMemoryInstance *)shared_memory_get_memory_inst(node); bh_assert(shared_memory_instance); (void)ref_count; @@ -398,8 +388,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, } #endif - if (heap_size > 0 - && module->malloc_func_index != (uint32)-1 + if (heap_size > 0 && module->malloc_func_index != (uint32)-1 && module->free_func_index != (uint32)-1) { /* Disable app heap, use malloc/free function exported by wasm app to allocate/free memory instead */ @@ -420,16 +409,16 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, } else if (heap_size > 0) { if (module->aux_heap_base_global_index != (uint32)-1 - && module->aux_heap_base < num_bytes_per_page - * init_page_count) { + && module->aux_heap_base < num_bytes_per_page * init_page_count) { /* Insert app heap before __heap_base */ aux_heap_base = module->aux_heap_base; bytes_of_last_page = aux_heap_base % num_bytes_per_page; if (bytes_of_last_page == 0) bytes_of_last_page = num_bytes_per_page; bytes_to_page_end = num_bytes_per_page - bytes_of_last_page; - inc_page_count = (heap_size - bytes_to_page_end - + num_bytes_per_page - 1) / num_bytes_per_page; + inc_page_count = + (heap_size - bytes_to_page_end + num_bytes_per_page - 1) + / num_bytes_per_page; heap_offset = aux_heap_base; aux_heap_base += heap_size; @@ -445,15 +434,15 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, /* Adjust __heap_base global value */ global_idx = module->aux_heap_base_global_index - module->import_global_count; - global_addr = (uint8*)module_inst->global_data.ptr + - module->globals[global_idx].data_offset; + global_addr = (uint8 *)module_inst->global_data.ptr + + module->globals[global_idx].data_offset; *(uint32 *)global_addr = aux_heap_base; LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base); } else { /* Insert app heap before new page */ - inc_page_count = (heap_size + num_bytes_per_page - 1) - / num_bytes_per_page; + inc_page_count = + (heap_size + num_bytes_per_page - 1) / num_bytes_per_page; heap_offset = num_bytes_per_page * init_page_count; heap_size = num_bytes_per_page * inc_page_count; if (heap_size > 0) @@ -498,8 +487,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, * so the range of ea is 0 to 8G */ if (total_size >= UINT32_MAX - || !(p = mapped_mem = os_mmap(NULL, map_size, - MMAP_PROT_NONE, MMAP_MAP_NONE))) { + || !(p = mapped_mem = + os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { set_error_buf(error_buf, error_buf_size, "mmap memory failed"); return NULL; } @@ -539,46 +528,46 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, if (heap_size > 0) { uint32 heap_struct_size = mem_allocator_get_heap_struct_size(); - if (!(heap_handle = runtime_malloc((uint64)heap_struct_size, - error_buf, error_buf_size))) { + if (!(heap_handle = runtime_malloc((uint64)heap_struct_size, error_buf, + error_buf_size))) { goto fail1; } memory_inst->heap_handle.ptr = heap_handle; - if (!mem_allocator_create_with_struct_and_pool - (heap_handle, heap_struct_size, - memory_inst->heap_data.ptr, heap_size)) { - set_error_buf(error_buf, error_buf_size, - "init app heap failed"); + if (!mem_allocator_create_with_struct_and_pool( + heap_handle, heap_struct_size, memory_inst->heap_data.ptr, + heap_size)) { + set_error_buf(error_buf, error_buf_size, "init app heap failed"); goto fail2; } } if (total_size > 0) { - if (sizeof(uintptr_t) == sizeof(uint64)) { - memory_inst->mem_bound_check_1byte.u64 = total_size - 1; - memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; - memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; - memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; - memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; - } - else { - memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; - memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; - memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; - memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; - memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16; - } + if (sizeof(uintptr_t) == sizeof(uint64)) { + memory_inst->mem_bound_check_1byte.u64 = total_size - 1; + memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; + memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; + memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; + memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; + } + else { + memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; + memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; + memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; + memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; + memory_inst->mem_bound_check_16bytes.u32[0] = + (uint32)total_size - 16; + } } #if WASM_ENABLE_SHARED_MEMORY != 0 if (is_shared_memory) { memory_inst->is_shared = true; - if (!shared_memory_set_memory_inst((WASMModuleCommon *)module, - (WASMMemoryInstanceCommon *)memory_inst)) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + if (!shared_memory_set_memory_inst( + (WASMModuleCommon *)module, + (WASMMemoryInstanceCommon *)memory_inst)) { + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); goto fail3; } } @@ -609,7 +598,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, return NULL; } -static AOTMemoryInstance* +static AOTMemoryInstance * aot_get_default_memory(AOTModuleInstance *module_inst) { if (module_inst->memories.ptr) @@ -631,16 +620,15 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module, module_inst->memory_count = memory_count; total_size = sizeof(AOTPointer) * (uint64)memory_count; if (!(module_inst->memories.ptr = - runtime_malloc(total_size, error_buf, error_buf_size))) { + runtime_malloc(total_size, error_buf, error_buf_size))) { return false; } memories = module_inst->global_table_data.memory_instances; for (i = 0; i < memory_count; i++, memories++) { - memory_inst = - memory_instantiate(module_inst, module, - memories, &module->memories[i], - heap_size, error_buf, error_buf_size); + memory_inst = memory_instantiate(module_inst, module, memories, + &module->memories[i], heap_size, + error_buf, error_buf_size); if (!memory_inst) { return false; } @@ -658,17 +646,16 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module, continue; #endif - bh_assert(data_seg->offset.init_expr_type == - INIT_EXPR_TYPE_I32_CONST - || data_seg->offset.init_expr_type == - INIT_EXPR_TYPE_GET_GLOBAL); + bh_assert(data_seg->offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST + || data_seg->offset.init_expr_type + == INIT_EXPR_TYPE_GET_GLOBAL); /* Resolve memory data base offset */ if (data_seg->offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) { global_index = data_seg->offset.u.global_index; - if (!check_global_init_expr(module, global_index, - error_buf, error_buf_size)) { + if (!check_global_init_expr(module, global_index, error_buf, + error_buf_size)) { return false; } @@ -678,10 +665,10 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module, else global_data_offset = module->globals[global_index - module->import_global_count] - .data_offset; + .data_offset; - base_offset = *(uint32*) - ((uint8*)module_inst->global_data.ptr + global_data_offset); + base_offset = *(uint32 *)((uint8 *)module_inst->global_data.ptr + + global_data_offset); } else { base_offset = (uint32)data_seg->offset.u.i32; @@ -722,7 +709,7 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module, } if (memory_inst->memory_data.ptr) { - bh_memcpy_s((uint8*)memory_inst->memory_data.ptr + base_offset, + bh_memcpy_s((uint8 *)memory_inst->memory_data.ptr + base_offset, memory_inst->memory_data_size - base_offset, data_seg->bytes, length); } @@ -737,22 +724,22 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module, { uint32 i; void **func_ptrs; - uint64 total_size = - ((uint64)module->import_func_count + module->func_count) * sizeof(void*); + uint64 total_size = ((uint64)module->import_func_count + module->func_count) + * sizeof(void *); if (module->import_func_count + module->func_count == 0) return true; /* Allocate memory */ - if (!(module_inst->func_ptrs.ptr = runtime_malloc - (total_size, error_buf, error_buf_size))) { + if (!(module_inst->func_ptrs.ptr = + runtime_malloc(total_size, error_buf, error_buf_size))) { return false; } /* Set import function pointers */ - func_ptrs = (void**)module_inst->func_ptrs.ptr; + func_ptrs = (void **)module_inst->func_ptrs.ptr; for (i = 0; i < module->import_func_count; i++, func_ptrs++) { - *func_ptrs = (void*)module->import_funcs[i].func_ptr_linked; + *func_ptrs = (void *)module->import_funcs[i].func_ptr_linked; if (!*func_ptrs) { const char *module_name = module->import_funcs[i].module_name; const char *field_name = module->import_funcs[i].func_name; @@ -762,8 +749,8 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module, } /* Set defined function pointers */ - bh_memcpy_s(func_ptrs, sizeof(void*) * module->func_count, - module->func_ptrs, sizeof(void*) * module->func_count); + bh_memcpy_s(func_ptrs, sizeof(void *) * module->func_count, + module->func_ptrs, sizeof(void *) * module->func_count); return true; } @@ -773,20 +760,20 @@ init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module, { uint32 i; uint32 *func_type_index; - uint64 total_size = - ((uint64)module->import_func_count + module->func_count) * sizeof(uint32); + uint64 total_size = ((uint64)module->import_func_count + module->func_count) + * sizeof(uint32); if (module->import_func_count + module->func_count == 0) return true; /* Allocate memory */ if (!(module_inst->func_type_indexes.ptr = - runtime_malloc(total_size, error_buf, error_buf_size))) { + runtime_malloc(total_size, error_buf, error_buf_size))) { return false; } /* Set import function type indexes */ - func_type_index = (uint32*)module_inst->func_type_indexes.ptr; + func_type_index = (uint32 *)module_inst->func_type_indexes.ptr; for (i = 0; i < module->import_func_count; i++, func_type_index++) *func_type_index = module->import_funcs[i].func_type_index; @@ -809,7 +796,7 @@ create_export_funcs(AOTModuleInstance *module_inst, AOTModule *module, size = sizeof(AOTFunctionInstance) * (uint64)module_inst->export_func_count; if (!(module_inst->export_funcs.ptr = export_func = - runtime_malloc(size, error_buf, error_buf_size))) { + runtime_malloc(size, error_buf, error_buf_size))) { return false; } @@ -824,13 +811,13 @@ create_export_funcs(AOTModuleInstance *module_inst, AOTModule *module, } else { export_func->is_import_func = false; - func_index = export_func->func_index - - module->import_func_count; + func_index = + export_func->func_index - module->import_func_count; ftype_index = module->func_type_indexes[func_index]; export_func->u.func.func_type = - module->func_types[ftype_index]; + module->func_types[ftype_index]; export_func->u.func.func_ptr = - module->func_ptrs[func_index]; + module->func_ptrs[func_index]; } export_func++; } @@ -866,8 +853,7 @@ create_exports(AOTModuleInstance *module_inst, AOTModule *module, } } - return create_export_funcs(module_inst, module, - error_buf, error_buf_size); + return create_export_funcs(module_inst, module, error_buf, error_buf_size); } static bool @@ -898,22 +884,27 @@ execute_post_inst_function(AOTModuleInstance *module_inst) /* Not found */ return true; - return aot_create_exec_env_and_call_function(module_inst, post_inst_func, 0, NULL); + return aot_create_exec_env_and_call_function(module_inst, post_inst_func, 0, + NULL); } static bool execute_start_function(AOTModuleInstance *module_inst) { - AOTModule *module = (AOTModule*)module_inst->aot_module.ptr; + AOTModule *module = (AOTModule *)module_inst->aot_module.ptr; WASMExecEnv *exec_env; - typedef void (*F)(WASMExecEnv*); - union { F f; void *v; } u; + typedef void (*F)(WASMExecEnv *); + union { + F f; + void *v; + } u; if (!module->start_function) return true; - if (!(exec_env = wasm_exec_env_create((WASMModuleInstanceCommon*)module_inst, - module_inst->default_wasm_stack_size))) { + if (!(exec_env = + wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst, + module_inst->default_wasm_stack_size))) { aot_set_exception(module_inst, "allocate memory failed"); return false; } @@ -942,10 +933,9 @@ execute_memory_init_function(AOTModuleInstance *module_inst) } #endif -AOTModuleInstance* -aot_instantiate(AOTModule *module, bool is_sub_inst, - uint32 stack_size, uint32 heap_size, - char *error_buf, uint32 error_buf_size) +AOTModuleInstance * +aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size, + uint32 heap_size, char *error_buf, uint32 error_buf_size) { AOTModuleInstance *module_inst; const uint32 module_inst_struct_size = @@ -970,8 +960,8 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, for (i = 0; i != module->import_table_count; ++i) { table_size += offsetof(AOTTableInstance, data); table_size += - (uint64)sizeof(uint32) - * (uint64)aot_get_imp_tbl_data_slots(module->import_tables + i); + (uint64)sizeof(uint32) + * (uint64)aot_get_imp_tbl_data_slots(module->import_tables + i); } for (i = 0; i != module->table_count; ++i) { @@ -982,8 +972,8 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, total_size += table_size; /* Allocate module instance, global data, table data and heap data */ - if (!(module_inst = runtime_malloc(total_size, - error_buf, error_buf_size))) { + if (!(module_inst = + runtime_malloc(total_size, error_buf, error_buf_size))) { return NULL; } @@ -991,8 +981,8 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, module_inst->aot_module.ptr = module; /* Initialize global info */ - p = (uint8*)module_inst + module_inst_struct_size + - module_inst_mem_inst_size; + p = (uint8 *)module_inst + module_inst_struct_size + + module_inst_mem_inst_size; module_inst->global_data.ptr = p; module_inst->global_data_size = module->global_data_size; if (!global_instantiate(module_inst, module, error_buf, error_buf_size)) @@ -1001,16 +991,15 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, /* Initialize table info */ p += module->global_data_size; module_inst->tables.ptr = p; - module_inst->table_count = - module->table_count + module->import_table_count; + module_inst->table_count = module->table_count + module->import_table_count; /* Set all elements to -1 to mark them as uninitialized elements */ memset(module_inst->tables.ptr, 0xff, (uint32)table_size); if (!table_instantiate(module_inst, module, error_buf, error_buf_size)) goto fail; /* Initialize memory space */ - if (!memories_instantiate(module_inst, module, heap_size, - error_buf, error_buf_size)) + if (!memories_instantiate(module_inst, module, heap_size, error_buf, + error_buf_size)) goto fail; /* Initialize function pointers */ @@ -1026,19 +1015,14 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, #if WASM_ENABLE_LIBC_WASI != 0 if (!is_sub_inst) { - if (!wasm_runtime_init_wasi((WASMModuleInstanceCommon*)module_inst, - module->wasi_args.dir_list, - module->wasi_args.dir_count, - module->wasi_args.map_dir_list, - module->wasi_args.map_dir_count, - module->wasi_args.env, - module->wasi_args.env_count, - module->wasi_args.argv, - module->wasi_args.argc, - module->wasi_args.stdio[0], - module->wasi_args.stdio[1], - module->wasi_args.stdio[2], - error_buf, error_buf_size)) + if (!wasm_runtime_init_wasi( + (WASMModuleInstanceCommon *)module_inst, + module->wasi_args.dir_list, module->wasi_args.dir_count, + module->wasi_args.map_dir_list, module->wasi_args.map_dir_count, + module->wasi_args.env, module->wasi_args.env_count, + module->wasi_args.argv, module->wasi_args.argc, + module->wasi_args.stdio[0], module->wasi_args.stdio[1], + module->wasi_args.stdio[2], error_buf, error_buf_size)) goto fail; } #endif @@ -1047,23 +1031,23 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, if (stack_size == 0) stack_size = DEFAULT_WASM_STACK_SIZE; #if WASM_ENABLE_SPEC_TEST != 0 - if (stack_size < 48 *1024) + if (stack_size < 48 * 1024) stack_size = 48 * 1024; #endif module_inst->default_wasm_stack_size = stack_size; #if WASM_ENABLE_PERF_PROFILING != 0 - total_size = (uint64)sizeof(AOTFuncPerfProfInfo) * - (module->import_func_count + module->func_count); + total_size = (uint64)sizeof(AOTFuncPerfProfInfo) + * (module->import_func_count + module->func_count); if (!(module_inst->func_perf_profilings.ptr = - runtime_malloc(total_size, error_buf, error_buf_size))) { + runtime_malloc(total_size, error_buf, error_buf_size))) { goto fail; } #endif #if WASM_ENABLE_DUMP_CALL_STACK != 0 if (!(module_inst->frames.ptr = - runtime_malloc(sizeof(Vector), error_buf, error_buf_size))) { + runtime_malloc(sizeof(Vector), error_buf, error_buf_size))) { goto fail; } #endif @@ -1071,8 +1055,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, /* Execute __post_instantiate function and start function*/ if (!execute_post_inst_function(module_inst) || !execute_start_function(module_inst)) { - set_error_buf(error_buf, error_buf_size, - module_inst->cur_exception); + set_error_buf(error_buf, error_buf_size, module_inst->cur_exception); goto fail; } @@ -1096,8 +1079,8 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, #endif #if WASM_ENABLE_MEMORY_TRACING != 0 - wasm_runtime_dump_module_inst_mem_consumption - ((WASMModuleInstanceCommon *)module_inst); + wasm_runtime_dump_module_inst_mem_consumption( + (WASMModuleInstanceCommon *)module_inst); #endif return module_inst; @@ -1129,7 +1112,7 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) which may allocated from global heap. */ /* Only destroy wasi ctx in the main module instance */ if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon*)module_inst); + wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); #endif #if WASM_ENABLE_PERF_PROFILING != 0 @@ -1158,19 +1141,19 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) wasm_runtime_free(module_inst->func_type_indexes.ptr); if (module_inst->exec_env_singleton.ptr) - wasm_exec_env_destroy((WASMExecEnv *) - module_inst->exec_env_singleton.ptr); + wasm_exec_env_destroy( + (WASMExecEnv *)module_inst->exec_env_singleton.ptr); wasm_runtime_free(module_inst); } -AOTFunctionInstance* -aot_lookup_function(const AOTModuleInstance *module_inst, - const char *name, const char *signature) +AOTFunctionInstance * +aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, + const char *signature) { uint32 i; - AOTFunctionInstance *export_funcs = (AOTFunctionInstance *) - module_inst->export_funcs.ptr; + AOTFunctionInstance *export_funcs = + (AOTFunctionInstance *)module_inst->export_funcs.ptr; for (i = 0; i < module_inst->export_func_count; i++) if (!strcmp(export_funcs[i].func_name, name)) @@ -1197,17 +1180,16 @@ aot_signal_handler(void *sig_addr) uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT; /* Check whether current thread is running aot function */ - if (aot_exec_env - && aot_exec_env->handle == os_self_thread() + if (aot_exec_env && aot_exec_env->handle == os_self_thread() && (jmpbuf_node = aot_exec_env->jmpbuf_stack_top)) { /* Get mapped mem info of current instance */ module_inst = (AOTModuleInstance *)aot_exec_env->module_inst; /* Get the default memory instance */ memory_inst = aot_get_default_memory(module_inst); if (memory_inst) { - mapped_mem_start_addr = (uint8*)memory_inst->memory_data.ptr; - mapped_mem_end_addr = (uint8*)memory_inst->memory_data.ptr - + 8 * (uint64)BH_GB; + mapped_mem_start_addr = (uint8 *)memory_inst->memory_data.ptr; + mapped_mem_end_addr = + (uint8 *)memory_inst->memory_data.ptr + 8 * (uint64)BH_GB; } /* Get stack info of current thread */ @@ -1215,16 +1197,17 @@ aot_signal_handler(void *sig_addr) stack_min_addr = os_thread_get_stack_boundary(); if (memory_inst - && (mapped_mem_start_addr <= (uint8*)sig_addr - && (uint8*)sig_addr < mapped_mem_end_addr)) { + && (mapped_mem_start_addr <= (uint8 *)sig_addr + && (uint8 *)sig_addr < mapped_mem_end_addr)) { /* The address which causes segmentation fault is inside aot instance's guard regions */ - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS); + aot_set_exception_with_id(module_inst, + EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS); os_longjmp(jmpbuf_node->jmpbuf, 1); } - else if (stack_min_addr - page_size <= (uint8*)sig_addr - && (uint8*)sig_addr < stack_min_addr - + page_size * guard_page_count) { + else if (stack_min_addr - page_size <= (uint8 *)sig_addr + && (uint8 *)sig_addr + < stack_min_addr + page_size * guard_page_count) { /* The address which causes segmentation fault is inside native thread's guard page */ aot_set_exception_with_id(module_inst, EXCE_NATIVE_STACK_OVERFLOW); @@ -1232,12 +1215,12 @@ aot_signal_handler(void *sig_addr) } } } -#else /* else of BH_PLATFORM_WINDOWS */ +#else /* else of BH_PLATFORM_WINDOWS */ static LONG aot_exception_handler(EXCEPTION_POINTERS *exce_info) { PEXCEPTION_RECORD ExceptionRecord = exce_info->ExceptionRecord; - uint8 *sig_addr = (uint8*)ExceptionRecord->ExceptionInformation[1]; + uint8 *sig_addr = (uint8 *)ExceptionRecord->ExceptionInformation[1]; AOTModuleInstance *module_inst; AOTMemoryInstance *memory_inst; WASMJmpBuf *jmpbuf_node; @@ -1245,19 +1228,18 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info) uint8 *mapped_mem_end_addr = NULL; uint32 page_size = os_getpagesize(); - if (aot_exec_env - && aot_exec_env->handle == os_self_thread() + if (aot_exec_env && aot_exec_env->handle == os_self_thread() && (jmpbuf_node = aot_exec_env->jmpbuf_stack_top)) { - module_inst = (AOTModuleInstance*)aot_exec_env->module_inst; + module_inst = (AOTModuleInstance *)aot_exec_env->module_inst; if (ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { /* Get the default memory instance */ memory_inst = aot_get_default_memory(module_inst); if (memory_inst) { - mapped_mem_start_addr = (uint8*)memory_inst->memory_data.ptr; - mapped_mem_end_addr = (uint8*)memory_inst->memory_data.ptr - + 8 * (uint64)BH_GB; - if (mapped_mem_start_addr <= (uint8*)sig_addr - && (uint8*)sig_addr < mapped_mem_end_addr) { + mapped_mem_start_addr = (uint8 *)memory_inst->memory_data.ptr; + mapped_mem_end_addr = + (uint8 *)memory_inst->memory_data.ptr + 8 * (uint64)BH_GB; + if (mapped_mem_start_addr <= (uint8 *)sig_addr + && (uint8 *)sig_addr < mapped_mem_end_addr) { /* The address which causes segmentation fault is inside aot instance's guard regions. Set exception and let the aot func continue to run, when @@ -1312,11 +1294,11 @@ aot_signal_destroy() static bool invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, - const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *argv_ret) + const WASMType *func_type, + const char *signature, void *attachment, + uint32 *argv, uint32 argc, uint32 *argv_ret) { - AOTModuleInstance *module_inst = (AOTModuleInstance*)exec_env->module_inst; + AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; WASMExecEnv **p_aot_exec_env = &aot_exec_env; WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop; uint32 page_size = os_getpagesize(); @@ -1333,8 +1315,8 @@ 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. */ - if ((uint8*)&module_inst < exec_env->native_stack_boundary - + page_size * (guard_page_count + 1)) { + 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); return false; } @@ -1356,27 +1338,28 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, /* Quick call with func_ptr if the function signature is simple */ if (!signature && param_count == 1 && types[0] == VALUE_TYPE_I32) { if (result_count == 0) { - void (*NativeFunc)(WASMExecEnv*, uint32) = - (void (*)(WASMExecEnv*, uint32))func_ptr; + void (*NativeFunc)(WASMExecEnv *, uint32) = + (void (*)(WASMExecEnv *, uint32))func_ptr; NativeFunc(exec_env, argv[0]); ret = aot_get_exception(module_inst) ? false : true; } - else if (result_count == 1 && types[param_count] == VALUE_TYPE_I32) { - uint32 (*NativeFunc)(WASMExecEnv*, uint32) = - (uint32 (*)(WASMExecEnv*, uint32))func_ptr; + else if (result_count == 1 + && types[param_count] == VALUE_TYPE_I32) { + uint32 (*NativeFunc)(WASMExecEnv *, uint32) = + (uint32(*)(WASMExecEnv *, uint32))func_ptr; argv_ret[0] = NativeFunc(exec_env, argv[0]); ret = aot_get_exception(module_inst) ? false : true; } else { ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type, - signature, attachment, - argv, argc, argv_ret); + signature, attachment, argv, + argc, argv_ret); } } else { ret = wasm_runtime_invoke_native(exec_env, func_ptr, func_type, - signature, attachment, - argv, argc, argv_ret); + signature, attachment, argv, argc, + argv_ret); } #ifdef BH_PLATFORM_WINDOWS if ((exce = aot_get_exception(module_inst)) @@ -1412,11 +1395,10 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ bool -aot_call_function(WASMExecEnv *exec_env, - AOTFunctionInstance *function, +aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, unsigned argc, uint32 argv[]) { - AOTModuleInstance *module_inst = (AOTModuleInstance*)exec_env->module_inst; + AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; AOTFuncType *func_type = function->u.func.func_type; uint32 result_count = func_type->result_count; uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0; @@ -1434,9 +1416,11 @@ aot_call_function(WASMExecEnv *exec_env, uint64 size; /* Allocate memory all arguments */ - size = sizeof(uint32) * (uint64)argc /* original arguments */ - + sizeof(void*) * (uint64)ext_ret_count /* extra result values' addr */ - + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */ + size = + sizeof(uint32) * (uint64)argc /* original arguments */ + + sizeof(void *) + * (uint64)ext_ret_count /* extra result values' addr */ + + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */ if (size > sizeof(argv1_buf) && !(argv1 = runtime_malloc(size, module_inst->cur_exception, sizeof(module_inst->cur_exception)))) { @@ -1448,11 +1432,12 @@ aot_call_function(WASMExecEnv *exec_env, bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc); /* Get the extra result value's address */ - ext_rets = argv1 + argc + sizeof(void*)/sizeof(uint32) * ext_ret_count; + ext_rets = + argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; /* Append each extra result value's address to original arguments */ for (i = 0; i < ext_ret_count; i++) { - *(uintptr_t*)(argv1 + argc + sizeof(void*) / sizeof(uint32) * i) = + *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) = (uintptr_t)(ext_rets + cell_num); cell_num += wasm_value_type_cell_num(ext_ret_types[i]); } @@ -1512,9 +1497,10 @@ aot_call_function(WASMExecEnv *exec_env, bh_assert(0); break; } - ext_rets = argv1 + argc + sizeof(void*)/sizeof(uint32) * ext_ret_count; - bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, - ext_rets, sizeof(uint32) * cell_num); + ext_rets = + argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; + bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets, + sizeof(uint32) * cell_num); if (argv1 != argv1_buf) wasm_runtime_free(argv1); @@ -1549,8 +1535,8 @@ aot_call_function(WASMExecEnv *exec_env, bool aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst, - AOTFunctionInstance *func, - unsigned argc, uint32 argv[]) + AOTFunctionInstance *func, unsigned argc, + uint32 argv[]) { WASMExecEnv *exec_env = NULL, *existing_exec_env = NULL; bool ret; @@ -1558,14 +1544,14 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst, #if defined(OS_ENABLE_HW_BOUND_CHECK) existing_exec_env = exec_env = aot_exec_env; #elif WASM_ENABLE_THREAD_MGR != 0 - existing_exec_env = exec_env = wasm_clusters_search_exec_env( - (WASMModuleInstanceCommon*)module_inst); + existing_exec_env = exec_env = + wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst); #endif if (!existing_exec_env) { if (!(exec_env = - wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst, - module_inst->default_wasm_stack_size))) { + wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst, + module_inst->default_wasm_stack_size))) { aot_set_exception(module_inst, "allocate memory failed"); return false; } @@ -1589,20 +1575,17 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst, } void -aot_set_exception(AOTModuleInstance *module_inst, - const char *exception) +aot_set_exception(AOTModuleInstance *module_inst, const char *exception) { if (exception) - snprintf(module_inst->cur_exception, - sizeof(module_inst->cur_exception), + snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception), "Exception: %s", exception); else module_inst->cur_exception[0] = '\0'; } void -aot_set_exception_with_id(AOTModuleInstance *module_inst, - uint32 id) +aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id) { switch (id) { case EXCE_UNREACHABLE: @@ -1636,7 +1619,8 @@ aot_set_exception_with_id(AOTModuleInstance *module_inst, aot_set_exception(module_inst, "uninitialized element"); break; case EXCE_CALL_UNLINKED_IMPORT_FUNC: - aot_set_exception(module_inst, "failed to call unlinked import function"); + aot_set_exception(module_inst, + "failed to call unlinked import function"); break; case EXCE_NATIVE_STACK_OVERFLOW: aot_set_exception(module_inst, "native stack overflow"); @@ -1658,7 +1642,7 @@ aot_set_exception_with_id(AOTModuleInstance *module_inst, } } -const char* +const char * aot_get_exception(AOTModuleInstance *module_inst) { if (module_inst->cur_exception[0] == '\0') @@ -1676,8 +1660,8 @@ aot_clear_exception(AOTModuleInstance *module_inst) static bool execute_malloc_function(AOTModuleInstance *module_inst, AOTFunctionInstance *malloc_func, - AOTFunctionInstance *retain_func, - uint32 size, uint32 *p_result) + AOTFunctionInstance *retain_func, uint32 size, + uint32 *p_result) { uint32 argv[2], argc; bool ret; @@ -1702,12 +1686,12 @@ execute_malloc_function(AOTModuleInstance *module_inst, else #endif { - ret = aot_create_exec_env_and_call_function - (module_inst, malloc_func, argc, argv); + ret = aot_create_exec_env_and_call_function(module_inst, malloc_func, + argc, argv); if (retain_func && ret) { - ret = aot_create_exec_env_and_call_function - (module_inst, retain_func, 1, argv); + ret = aot_create_exec_env_and_call_function(module_inst, + retain_func, 1, argv); } } @@ -1718,8 +1702,7 @@ execute_malloc_function(AOTModuleInstance *module_inst, static bool execute_free_function(AOTModuleInstance *module_inst, - AOTFunctionInstance *free_func, - uint32 offset) + AOTFunctionInstance *free_func, uint32 offset) { uint32 argv[2]; @@ -1733,8 +1716,8 @@ execute_free_function(AOTModuleInstance *module_inst, else #endif { - return aot_create_exec_env_and_call_function - (module_inst, free_func, 1, argv); + return aot_create_exec_env_and_call_function(module_inst, free_func, 1, + argv); } } @@ -1764,8 +1747,7 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, if (module->retain_func_index != (uint32)-1) { malloc_func_name = "__new"; malloc_func_sig = "(ii)i"; - retain_func = - aot_lookup_function(module_inst, "__retain", "(i)i"); + retain_func = aot_lookup_function(module_inst, "__retain", "(i)i"); if (!retain_func) retain_func = aot_lookup_function(module_inst, "__pin", "(i)i"); bh_assert(retain_func); @@ -1775,17 +1757,14 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, malloc_func_sig = "(i)i"; } malloc_func = - aot_lookup_function(module_inst, - malloc_func_name, malloc_func_sig); + aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig); bh_assert(malloc_func); if (!execute_malloc_function(module_inst, malloc_func, retain_func, size, &offset)) { return 0; } - addr = offset - ? (uint8*)memory_inst->memory_data.ptr + offset - : NULL; + addr = offset ? (uint8 *)memory_inst->memory_data.ptr + offset : NULL; } if (!addr) { @@ -1804,12 +1783,12 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, } if (p_native_addr) *p_native_addr = addr; - return (uint32)(addr - (uint8*)memory_inst->memory_data.ptr); + return (uint32)(addr - (uint8 *)memory_inst->memory_data.ptr); } uint32 -aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, - uint32 size, void **p_native_addr) +aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size, + void **p_native_addr) { AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); uint8 *addr = NULL; @@ -1820,10 +1799,9 @@ aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, } if (memory_inst->heap_handle.ptr) { - addr = - mem_allocator_realloc(memory_inst->heap_handle.ptr, - (uint8*)memory_inst->memory_data.ptr + ptr, - size); + addr = mem_allocator_realloc( + memory_inst->heap_handle.ptr, + (uint8 *)memory_inst->memory_data.ptr + ptr, size); } /* Only support realloc in WAMR's app heap */ @@ -1841,7 +1819,7 @@ aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, if (p_native_addr) *p_native_addr = addr; - return (uint32)(addr - (uint8*)memory_inst->memory_data.ptr); + return (uint32)(addr - (uint8 *)memory_inst->memory_data.ptr); } void @@ -1857,7 +1835,7 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr) if (ptr) { uint8 *addr = (uint8 *)memory_inst->memory_data.ptr + ptr; if (memory_inst->heap_handle.ptr - &&(uint8 *)memory_inst->heap_data.ptr < addr + && (uint8 *)memory_inst->heap_data.ptr < addr && addr < (uint8 *)memory_inst->heap_data_end.ptr) { mem_allocator_free(memory_inst->heap_handle.ptr, addr); } @@ -1886,12 +1864,12 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr) } uint32 -aot_module_dup_data(AOTModuleInstance *module_inst, - const char *src, uint32 size) +aot_module_dup_data(AOTModuleInstance *module_inst, const char *src, + uint32 size) { char *buffer; - uint32 buffer_offset = aot_module_malloc(module_inst, size, - (void**)&buffer); + uint32 buffer_offset = + aot_module_malloc(module_inst, size, (void **)&buffer); if (buffer_offset != 0) { buffer = aot_addr_app_to_native(module_inst, buffer_offset); @@ -1901,8 +1879,8 @@ aot_module_dup_data(AOTModuleInstance *module_inst, } bool -aot_validate_app_addr(AOTModuleInstance *module_inst, - uint32 app_offset, uint32 size) +aot_validate_app_addr(AOTModuleInstance *module_inst, uint32 app_offset, + uint32 size) { AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); @@ -1911,7 +1889,7 @@ aot_validate_app_addr(AOTModuleInstance *module_inst, } /* integer overflow check */ - if(app_offset > UINT32_MAX - size) { + if (app_offset > UINT32_MAX - size) { goto fail; } @@ -1924,8 +1902,8 @@ aot_validate_app_addr(AOTModuleInstance *module_inst, } bool -aot_validate_native_addr(AOTModuleInstance *module_inst, - void *native_ptr, uint32 size) +aot_validate_native_addr(AOTModuleInstance *module_inst, void *native_ptr, + uint32 size) { AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); uint8 *addr = (uint8 *)native_ptr; @@ -1982,10 +1960,8 @@ aot_addr_native_to_app(AOTModuleInstance *module_inst, void *native_ptr) } bool -aot_get_app_addr_range(AOTModuleInstance *module_inst, - uint32 app_offset, - uint32 *p_app_start_offset, - uint32 *p_app_end_offset) +aot_get_app_addr_range(AOTModuleInstance *module_inst, uint32 app_offset, + uint32 *p_app_start_offset, uint32 *p_app_end_offset) { AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); uint32 memory_data_size; @@ -2007,8 +1983,7 @@ aot_get_app_addr_range(AOTModuleInstance *module_inst, } bool -aot_get_native_addr_range(AOTModuleInstance *module_inst, - uint8 *native_ptr, +aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr, uint8 **p_native_start_addr, uint8 **p_native_end_addr) { @@ -2076,20 +2051,20 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) } #endif - if (!(memory_data = wasm_runtime_realloc(memory_data_old, - (uint32)total_size))) { + if (!(memory_data = + wasm_runtime_realloc(memory_data_old, (uint32)total_size))) { if (!(memory_data = wasm_runtime_malloc((uint32)total_size))) { return false; } if (memory_data_old) { - bh_memcpy_s(memory_data, (uint32)total_size, - memory_data_old, total_size_old); + bh_memcpy_s(memory_data, (uint32)total_size, memory_data_old, + total_size_old); wasm_runtime_free(memory_data_old); } } - memset(memory_data + total_size_old, - 0, (uint32)total_size - total_size_old); + memset(memory_data + total_size_old, 0, + (uint32)total_size - total_size_old); memory_inst->cur_page_count = total_page_count; memory_inst->memory_data_size = (uint32)total_size; @@ -2098,8 +2073,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) if (heap_size > 0) { if (mem_allocator_migrate(memory_inst->heap_handle.ptr, - (char*)heap_data_old - + (memory_data - memory_data_old), + (char *)heap_data_old + + (memory_data - memory_data_old), heap_size)) { return false; } @@ -2162,7 +2137,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) if (os_mprotect(memory_inst->memory_data_end.ptr, num_bytes_per_page * inc_page_count, - MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { + MMAP_PROT_READ | MMAP_PROT_WRITE) + != 0) { #ifdef BH_PLATFORM_WINDOWS os_mem_decommit(memory_inst->memory_data_end.ptr, num_bytes_per_page * inc_page_count); @@ -2175,8 +2151,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) memory_inst->cur_page_count = total_page_count; memory_inst->memory_data_size = (uint32)total_size; - memory_inst->memory_data_end.ptr = (uint8 *)memory_inst->memory_data.ptr - + (uint32)total_size; + memory_inst->memory_data_end.ptr = + (uint8 *)memory_inst->memory_data.ptr + (uint32)total_size; if (sizeof(uintptr_t) == sizeof(uint64)) { memory_inst->mem_bound_check_1byte.u64 = total_size - 1; @@ -2197,11 +2173,11 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ bool -aot_is_wasm_type_equal(AOTModuleInstance *module_inst, - uint32 type1_idx, uint32 type2_idx) +aot_is_wasm_type_equal(AOTModuleInstance *module_inst, uint32 type1_idx, + uint32 type2_idx) { WASMType *type1, *type2; - AOTModule *module = (AOTModule*)module_inst->aot_module.ptr; + AOTModule *module = (AOTModule *)module_inst->aot_module.ptr; if (type1_idx >= module->func_type_count || type2_idx >= module->func_type_count) { @@ -2219,16 +2195,16 @@ aot_is_wasm_type_equal(AOTModuleInstance *module_inst, } bool -aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, - uint32 argc, uint32 *argv) +aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, + uint32 *argv) { - AOTModuleInstance *module_inst = (AOTModuleInstance*) - wasm_runtime_get_module_inst(exec_env); - AOTModule *aot_module = (AOTModule*)module_inst->aot_module.ptr; - uint32 *func_type_indexes = (uint32*)module_inst->func_type_indexes.ptr; + AOTModuleInstance *module_inst = + (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env); + AOTModule *aot_module = (AOTModule *)module_inst->aot_module.ptr; + uint32 *func_type_indexes = (uint32 *)module_inst->func_type_indexes.ptr; uint32 func_type_idx = func_type_indexes[func_idx]; AOTFuncType *func_type = aot_module->func_types[func_type_idx]; - void **func_ptrs = (void**)module_inst->func_ptrs.ptr; + void **func_ptrs = (void **)module_inst->func_ptrs.ptr; void *func_ptr = func_ptrs[func_idx]; AOTImportFunc *import_func; const char *signature; @@ -2249,35 +2225,34 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, attachment = import_func->attachment; if (import_func->call_conv_wasm_c_api) { return wasm_runtime_invoke_c_api_native( - (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc, - argv, import_func->wasm_c_api_with_env, attachment); + (WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc, + argv, import_func->wasm_c_api_with_env, attachment); } else if (!import_func->call_conv_raw) { signature = import_func->signature; - return wasm_runtime_invoke_native(exec_env, func_ptr, - func_type, signature, attachment, - argv, argc, argv); + return wasm_runtime_invoke_native(exec_env, func_ptr, func_type, + signature, attachment, argv, argc, + argv); } else { signature = import_func->signature; - return wasm_runtime_invoke_native_raw(exec_env, func_ptr, - func_type, signature, attachment, - argv, argc, argv); + return wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type, + signature, attachment, argv, argc, + argv); } } bool -aot_call_indirect(WASMExecEnv *exec_env, - uint32 tbl_idx, uint32 table_elem_idx, +aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx, uint32 argc, uint32 *argv) { - AOTModuleInstance *module_inst = (AOTModuleInstance*) - wasm_runtime_get_module_inst(exec_env); - AOTModule *aot_module = (AOTModule*)module_inst->aot_module.ptr; - uint32 *func_type_indexes = (uint32*)module_inst->func_type_indexes.ptr; + AOTModuleInstance *module_inst = + (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env); + AOTModule *aot_module = (AOTModule *)module_inst->aot_module.ptr; + uint32 *func_type_indexes = (uint32 *)module_inst->func_type_indexes.ptr; AOTTableInstance *tbl_inst; AOTFuncType *func_type; - void **func_ptrs = (void**)module_inst->func_ptrs.ptr, *func_ptr; + void **func_ptrs = (void **)module_inst->func_ptrs.ptr, *func_ptr; uint32 func_type_idx, func_idx, ext_ret_count; AOTImportFunc *import_func; const char *signature = NULL; @@ -2289,7 +2264,7 @@ aot_call_indirect(WASMExecEnv *exec_env, exec_env->native_stack_boundary must have been set, we don't set it again */ - if ((uint8*)&module_inst < exec_env->native_stack_boundary) { + if ((uint8 *)&module_inst < exec_env->native_stack_boundary) { aot_set_exception_with_id(module_inst, EXCE_NATIVE_STACK_OVERFLOW); return false; } @@ -2302,7 +2277,7 @@ aot_call_indirect(WASMExecEnv *exec_env, return false; } - func_idx = ((uint32*)tbl_inst->data)[table_elem_idx]; + func_idx = ((uint32 *)tbl_inst->data)[table_elem_idx]; if (func_idx == (uint32)-1) { aot_set_exception_with_id(module_inst, EXCE_UNINITIALIZED_ELEMENT); return false; @@ -2327,15 +2302,14 @@ aot_call_indirect(WASMExecEnv *exec_env, signature = import_func->signature; if (import_func->call_conv_raw) { attachment = import_func->attachment; - return wasm_runtime_invoke_native_raw(exec_env, func_ptr, - func_type, signature, - attachment, - argv, argc, argv); + return wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type, + signature, attachment, argv, + argc, argv); } } - ext_ret_count = func_type->result_count > 1 - ? func_type->result_count - 1 : 0; + ext_ret_count = + func_type->result_count > 1 ? func_type->result_count - 1 : 0; if (ext_ret_count > 0) { uint32 argv1_buf[32], *argv1 = argv1_buf; uint32 *ext_rets = NULL, *argv_ret = argv; @@ -2345,9 +2319,11 @@ aot_call_indirect(WASMExecEnv *exec_env, uint64 size; /* Allocate memory all arguments */ - size = sizeof(uint32) * (uint64)argc /* original arguments */ - + sizeof(void*) * (uint64)ext_ret_count /* extra result values' addr */ - + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */ + size = + sizeof(uint32) * (uint64)argc /* original arguments */ + + sizeof(void *) + * (uint64)ext_ret_count /* extra result values' addr */ + + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */ if (size > sizeof(argv1_buf) && !(argv1 = runtime_malloc(size, module_inst->cur_exception, sizeof(module_inst->cur_exception)))) { @@ -2359,18 +2335,18 @@ aot_call_indirect(WASMExecEnv *exec_env, bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc); /* Get the extra result value's address */ - ext_rets = argv1 + argc + sizeof(void*)/sizeof(uint32) * ext_ret_count; + ext_rets = + argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; /* Append each extra result value's address to original arguments */ for (i = 0; i < ext_ret_count; i++) { - *(uintptr_t*)(argv1 + argc + sizeof(void*) / sizeof(uint32) * i) = + *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) = (uintptr_t)(ext_rets + cell_num); cell_num += wasm_value_type_cell_num(ext_ret_types[i]); } - ret = invoke_native_internal(exec_env, func_ptr, - func_type, signature, attachment, - argv1, argc, argv); + ret = invoke_native_internal(exec_env, func_ptr, func_type, signature, + attachment, argv1, argc, argv); if (!ret || aot_get_exception(module_inst)) { if (argv1 != argv1_buf) wasm_runtime_free(argv1); @@ -2402,9 +2378,10 @@ aot_call_indirect(WASMExecEnv *exec_env, bh_assert(0); break; } - ext_rets = argv1 + argc + sizeof(void*)/sizeof(uint32) * ext_ret_count; - bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, - ext_rets, sizeof(uint32) * cell_num); + ext_rets = + argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; + bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets, + sizeof(uint32) * cell_num); if (argv1 != argv1_buf) wasm_runtime_free(argv1); @@ -2412,9 +2389,8 @@ aot_call_indirect(WASMExecEnv *exec_env, return true; } else { - ret = invoke_native_internal(exec_env, func_ptr, - func_type, signature, attachment, - argv, argc, argv); + ret = invoke_native_internal(exec_env, func_ptr, func_type, signature, + attachment, argv, argc, argv); if (clear_wasi_proc_exit_exception(module_inst)) return true; return ret; @@ -2435,8 +2411,8 @@ aot_memset(void *s, int c, size_t n) #if WASM_ENABLE_BULK_MEMORY != 0 bool -aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, - uint32 offset, uint32 len, uint32 dst) +aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset, + uint32 len, uint32 dst) { AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); AOTModule *aot_module; @@ -2447,7 +2423,8 @@ aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, aot_module = (AOTModule *)module_inst->aot_module.ptr; if (aot_module->is_jit_mode) { #if WASM_ENABLE_JIT != 0 - seg_len = aot_module->wasm_module->data_segments[seg_index]->data_length; + seg_len = + aot_module->wasm_module->data_segments[seg_index]->data_length; data = aot_module->wasm_module->data_segments[seg_index]->data; #endif } @@ -2466,8 +2443,7 @@ aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, maddr = aot_addr_app_to_native(module_inst, dst); - bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, - data + offset, len); + bh_memcpy_s(maddr, memory_inst->memory_data_size - dst, data + offset, len); return true; } @@ -2494,11 +2470,9 @@ aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index) #if WASM_ENABLE_THREAD_MGR != 0 bool -aot_set_aux_stack(WASMExecEnv *exec_env, - uint32 start_offset, uint32 size) +aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size) { - AOTModuleInstance *module_inst = - (AOTModuleInstance*)exec_env->module_inst; + AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; AOTModule *module = (AOTModule *)module_inst->aot_module.ptr; uint32 stack_top_idx = module->aux_stack_top_global_index; @@ -2514,10 +2488,10 @@ aot_set_aux_stack(WASMExecEnv *exec_env, if (stack_top_idx != (uint32)-1) { /* The aux stack top is a wasm global, set the initial value for the global */ - uint32 global_offset = - module->globals[stack_top_idx].data_offset; - uint8 *global_addr = (uint8 *)module_inst->global_data.ptr + global_offset; - *(int32*)global_addr = start_offset; + uint32 global_offset = module->globals[stack_top_idx].data_offset; + uint8 *global_addr = + (uint8 *)module_inst->global_data.ptr + global_offset; + *(int32 *)global_addr = start_offset; /* The aux stack boundary is a constant value, set the value to exec_env */ @@ -2530,11 +2504,9 @@ aot_set_aux_stack(WASMExecEnv *exec_env, } bool -aot_get_aux_stack(WASMExecEnv *exec_env, - uint32 *start_offset, uint32 *size) +aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size) { - AOTModuleInstance *module_inst = - (AOTModuleInstance*)exec_env->module_inst; + AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; AOTModule *module = (AOTModule *)module_inst->aot_module.ptr; /* The aux stack information is resolved in loader @@ -2555,13 +2527,12 @@ aot_get_aux_stack(WASMExecEnv *exec_env, #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0) static void -const_string_node_size_cb(void *key, void *value, - void *p_const_string_size) +const_string_node_size_cb(void *key, void *value, void *p_const_string_size) { - uint32 const_string_size = *(uint32*)p_const_string_size; + uint32 const_string_size = *(uint32 *)p_const_string_size; const_string_size += bh_hash_map_get_elem_struct_size(); const_string_size += strlen((const char *)value) + 1; - *(uint32*)p_const_string_size += const_string_size; + *(uint32 *)p_const_string_size += const_string_size; } void @@ -2577,8 +2548,8 @@ aot_get_module_mem_consumption(const AOTModule *module, mem_conspn->types_size = sizeof(AOTFuncType *) * module->func_type_count; for (i = 0; i < module->func_type_count; i++) { AOTFuncType *type = module->func_types[i]; - size = offsetof(AOTFuncType, types) + - sizeof(uint8) * (type->param_count + type->result_count); + size = offsetof(AOTFuncType, types) + + sizeof(uint8) * (type->param_count + type->result_count); mem_conspn->types_size += size; } @@ -2607,8 +2578,8 @@ aot_get_module_mem_consumption(const AOTModule *module, mem_conspn->table_segs_size += size; } - mem_conspn->data_segs_size = sizeof(AOTMemInitData *) - * module->mem_init_data_count; + mem_conspn->data_segs_size = + sizeof(AOTMemInitData *) * module->mem_init_data_count; for (i = 0; i < module->mem_init_data_count; i++) { mem_conspn->data_segs_size += sizeof(AOTMemInitData); } @@ -2619,14 +2590,14 @@ aot_get_module_mem_consumption(const AOTModule *module, mem_conspn->const_strs_size = bh_hash_map_get_struct_size(module->const_str_set); - bh_hash_map_traverse(module->const_str_set, - const_string_node_size_cb, - (void*)&const_string_size); + bh_hash_map_traverse(module->const_str_set, const_string_node_size_cb, + (void *)&const_string_size); mem_conspn->const_strs_size += const_string_size; } /* code size + literal size + object data section size */ - mem_conspn->aot_code_size = module->code_size + module->literal_size + mem_conspn->aot_code_size = + module->code_size + module->literal_size + sizeof(AOTObjectDataSection) * module->data_section_count; for (i = 0; i < module->data_section_count; i++) { AOTObjectDataSection *obj_data = module->data_sections + i; @@ -2666,11 +2637,10 @@ aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst, ((AOTMemoryInstance **)module_inst->memories.ptr)[i]; mem_conspn->memories_size += mem_inst->num_bytes_per_page * mem_inst->cur_page_count; - mem_conspn->app_heap_size = - (uint8 *) mem_inst->heap_data_end.ptr - (uint8 *) mem_inst->heap_data.ptr; + mem_conspn->app_heap_size = (uint8 *)mem_inst->heap_data_end.ptr + - (uint8 *)mem_inst->heap_data.ptr; /* size of app heap structure */ - mem_conspn->memories_size += - mem_allocator_get_heap_struct_size(); + mem_conspn->memories_size += mem_allocator_get_heap_struct_size(); } tbl_inst = module_inst->tables.ptr; @@ -2681,9 +2651,10 @@ aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst, } /* func_ptrs and func_type_indexes */ - mem_conspn->functions_size = (sizeof(void *) + sizeof(uint32)) * - (((AOTModule *)module_inst->aot_module.ptr)->import_func_count - + ((AOTModule *)module_inst->aot_module.ptr)->func_count); + mem_conspn->functions_size = + (sizeof(void *) + sizeof(uint32)) + * (((AOTModule *)module_inst->aot_module.ptr)->import_func_count + + ((AOTModule *)module_inst->aot_module.ptr)->func_count); mem_conspn->globals_size = module_inst->global_data_size; @@ -2697,7 +2668,7 @@ aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst, mem_conspn->total_size += mem_conspn->globals_size; mem_conspn->total_size += mem_conspn->exports_size; } -#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) +#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \ || (WASM_ENABLE_MEMORY_TRACING != 0) */ #if WASM_ENABLE_REF_TYPES != 0 @@ -2710,9 +2681,9 @@ aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx) } void -aot_table_init(AOTModuleInstance *module_inst, - uint32 tbl_idx, uint32 tbl_seg_idx, - uint32 length, uint32 src_offset, uint32 dst_offset) +aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, + uint32 tbl_seg_idx, uint32 length, uint32 src_offset, + uint32 dst_offset) { AOTTableInstance *tbl_inst; AOTTableInitData *tbl_seg; @@ -2730,33 +2701,30 @@ aot_table_init(AOTModuleInstance *module_inst, if (length + src_offset > tbl_seg->func_index_count || dst_offset + length > tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, - EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); + aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; } if (tbl_seg->is_dropped) { - aot_set_exception_with_id(module_inst, - EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); + aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; } if (!wasm_elem_is_passive(tbl_seg->mode)) { - aot_set_exception_with_id(module_inst, - EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); + aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; } bh_memcpy_s((uint8 *)tbl_inst + offsetof(AOTTableInstance, data) - + dst_offset * sizeof(uint32), + + dst_offset * sizeof(uint32), (tbl_inst->cur_size - dst_offset) * sizeof(uint32), tbl_seg->func_indexes + src_offset, length * sizeof(uint32)); } void -aot_table_copy(AOTModuleInstance *module_inst, - uint32 src_tbl_idx, uint32 dst_tbl_idx, - uint32 length, uint32 src_offset, uint32 dst_offset) +aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx, + uint32 dst_tbl_idx, uint32 length, uint32 src_offset, + uint32 dst_offset) { AOTTableInstance *src_tbl_inst, *dst_tbl_inst; @@ -2768,8 +2736,7 @@ aot_table_copy(AOTModuleInstance *module_inst, if ((uint64)src_offset + length > dst_tbl_inst->cur_size || (uint64)dst_offset + length > src_tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, - EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); + aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; } @@ -2777,16 +2744,16 @@ aot_table_copy(AOTModuleInstance *module_inst, /* if src_offset < dst_offset, copy from back to front */ /* merge all together */ bh_memmove_s((uint8 *)(dst_tbl_inst) + offsetof(AOTTableInstance, data) - + dst_offset * sizeof(uint32), + + dst_offset * sizeof(uint32), (dst_tbl_inst->cur_size - dst_offset) * sizeof(uint32), (uint8 *)(src_tbl_inst) + offsetof(AOTTableInstance, data) - + src_offset * sizeof(uint32), + + src_offset * sizeof(uint32), length * sizeof(uint32)); } void -aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, - uint32 length, uint32 val, uint32 data_offset) +aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length, + uint32 val, uint32 data_offset) { AOTTableInstance *tbl_inst; @@ -2794,8 +2761,7 @@ aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, bh_assert(tbl_inst); if (data_offset + length > tbl_inst->cur_size) { - aot_set_exception_with_id(module_inst, - EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); + aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; } @@ -2857,8 +2823,7 @@ get_func_name_from_index(const AOTModuleInstance *module_inst, for (i = 0; i < module->export_count; i++) { AOTExport export = module->exports[i]; - if (export.index == func_index - && export.kind == EXPORT_KIND_FUNC) { + if (export.index == func_index && export.kind == EXPORT_KIND_FUNC) { func_name = export.name; break; } @@ -2874,14 +2839,14 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index) AOTFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, sizeof(AOTFrame)); #if WASM_ENABLE_PERF_PROFILING != 0 - AOTModuleInstance *module_inst = - (AOTModuleInstance*)exec_env->module_inst; + AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; AOTFuncPerfProfInfo *func_perf_prof = - (AOTFuncPerfProfInfo*)module_inst->func_perf_profilings.ptr + func_index; + (AOTFuncPerfProfInfo *)module_inst->func_perf_profilings.ptr + + func_index; #endif if (!frame) { - aot_set_exception((AOTModuleInstance*)exec_env->module_inst, + aot_set_exception((AOTModuleInstance *)exec_env->module_inst, "auxiliary call stack overflow"); return false; } @@ -2906,14 +2871,14 @@ aot_free_frame(WASMExecEnv *exec_env) #if WASM_ENABLE_PERF_PROFILING != 0 cur_frame->func_perf_prof_info->total_exec_time += - os_time_get_boot_microsecond() - cur_frame->time_started; + os_time_get_boot_microsecond() - cur_frame->time_started; cur_frame->func_perf_prof_info->total_exec_cnt++; #endif wasm_exec_env_free_wasm_frame(exec_env, cur_frame); exec_env->cur_frame = (struct WASMInterpFrame *)prev_frame; } -#endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) +#endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \ || (WASM_ENABLE_PERF_PROFILING != 0) */ #if WASM_ENABLE_DUMP_CALL_STACK != 0 @@ -2922,8 +2887,7 @@ aot_dump_call_stack(WASMExecEnv *exec_env) { AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame, *first_frame = cur_frame; - AOTModuleInstance *module_inst = - (AOTModuleInstance *)exec_env->module_inst; + AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst; const char *func_name; uint32 n = 0; @@ -2947,8 +2911,7 @@ aot_dump_call_stack(WASMExecEnv *exec_env) /* release previous stack frames and create new ones */ if (!bh_vector_destroy(module_inst->frames.ptr) - || !bh_vector_init(module_inst->frames.ptr, n, - sizeof(WASMCApiFrame))) { + || !bh_vector_init(module_inst->frames.ptr, n, sizeof(WASMCApiFrame))) { return; } @@ -2974,8 +2937,8 @@ aot_dump_call_stack(WASMExecEnv *exec_env) void aot_dump_perf_profiling(const AOTModuleInstance *module_inst) { - AOTFuncPerfProfInfo *perf_prof = (AOTFuncPerfProfInfo *) - module_inst->func_perf_profilings.ptr; + AOTFuncPerfProfInfo *perf_prof = + (AOTFuncPerfProfInfo *)module_inst->func_perf_profilings.ptr; AOTModule *module = (AOTModule *)module_inst->aot_module.ptr; uint32 total_func_count = module->import_func_count + module->func_count, i; const char *func_name; @@ -2985,14 +2948,15 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst) func_name = get_func_name_from_index(module_inst, i); if (func_name) - os_printf(" func %s, execution time: %.3f ms, execution count: %d times\n", + os_printf(" func %s, execution time: %.3f ms, execution count: %d " + "times\n", func_name, perf_prof->total_exec_time / 1000.0f, perf_prof->total_exec_cnt); else - os_printf(" func %d, execution time: %.3f ms, execution count: %d times\n", + os_printf(" func %d, execution time: %.3f ms, execution count: %d " + "times\n", i, perf_prof->total_exec_time / 1000.0f, perf_prof->total_exec_cnt); } } #endif /* end of WASM_ENABLE_PERF_PROFILING */ - diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 70c0697ed1..4fe9889eeb 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -95,6 +95,7 @@ typedef struct AOTFunctionInstance { } AOTFunctionInstance; #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) +/* clang-format off */ typedef struct AOTUnwindInfo { uint8 Version : 3; uint8 Flags : 5; @@ -111,6 +112,7 @@ typedef struct AOTUnwindInfo { uint16 FrameOffset; } UnwindCode[1]; } AOTUnwindInfo; +/* clang-format on */ /* size of mov instruction and jmp instruction */ #define PLT_ITEM_SIZE 12 @@ -328,7 +330,7 @@ typedef struct AOTModuleInstance { * TODO: for now we treate imported table like a local table */ uint32 table_count; - /* points to global_data */ + /* points to global_data */ AOTPointer global_data; /* points to AOTTableInstance[] */ AOTPointer tables; @@ -373,15 +375,15 @@ typedef struct AOTModuleInstance { /* reserved */ uint32 reserved[6]; - /* - * +------------------------------+ <-- memories.ptr - * | #0 AOTMemoryInstance - * +------------------------------+ <-- global_data.ptr - * | global data - * +------------------------------+ <-- tables.ptr - * | AOTTableInstance[table_count] - * +------------------------------+ - */ + /* + * +------------------------------+ <-- memories.ptr + * | #0 AOTMemoryInstance + * +------------------------------+ <-- global_data.ptr + * | global data + * +------------------------------+ <-- tables.ptr + * | AOTTableInstance[table_count] + * +------------------------------+ + */ union { uint64 _make_it_8_byte_aligned_; AOTMemoryInstance memory_instances[1]; @@ -409,8 +411,7 @@ typedef struct AOTTargetInfo { char arch[16]; } AOTTargetInfo; -typedef struct AOTFuncPerfProfInfo -{ +typedef struct AOTFuncPerfProfInfo { /* total execution time */ uint64 total_exec_time; /* total execution count */ @@ -436,9 +437,9 @@ typedef struct AOTFrame { * * @return return AOT module loaded, NULL if failed */ -AOTModule* -aot_load_from_aot_file(const uint8 *buf, uint32 size, - char *error_buf, uint32 error_buf_size); +AOTModule * +aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size); /** * Load a AOT module from a specified AOT section list. @@ -449,9 +450,9 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, * * @return return AOT module loaded, NULL if failed */ -AOTModule* -aot_load_from_sections(AOTSection *section_list, - char *error_buf, uint32 error_buf_size); +AOTModule * +aot_load_from_sections(AOTSection *section_list, char *error_buf, + uint32 error_buf_size); #if WASM_ENABLE_JIT != 0 /** @@ -463,9 +464,9 @@ aot_load_from_sections(AOTSection *section_list, * * @return return AOT module loaded, NULL if failed */ -AOTModule* -aot_convert_wasm_module(WASMModule *wasm_module, - char *error_buf, uint32 error_buf_size); +AOTModule * +aot_convert_wasm_module(WASMModule *wasm_module, char *error_buf, + uint32 error_buf_size); #endif /** @@ -490,10 +491,9 @@ aot_unload(AOTModule *module); * * @return return the instantiated AOT module instance, NULL if failed */ -AOTModuleInstance* -aot_instantiate(AOTModule *module, bool is_sub_inst, - uint32 stack_size, uint32 heap_size, - char *error_buf, uint32 error_buf_size); +AOTModuleInstance * +aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size, + uint32 heap_size, char *error_buf, uint32 error_buf_size); /** * Deinstantiate a AOT module instance, destroy the resources. @@ -514,9 +514,9 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst); * * @return the function instance found */ -AOTFunctionInstance* -aot_lookup_function(const AOTModuleInstance *module_inst, - const char *name, const char *signature); +AOTFunctionInstance * +aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, + const char *signature); /** * Call the given AOT function of a AOT module instance with * arguments. @@ -533,8 +533,7 @@ aot_lookup_function(const AOTModuleInstance *module_inst, * the caller can call aot_get_exception to get exception info. */ bool -aot_call_function(WASMExecEnv *exec_env, - AOTFunctionInstance *function, +aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, unsigned argc, uint32 argv[]); bool @@ -553,12 +552,10 @@ aot_create_exec_env_singleton(AOTModuleInstance *module_inst); * @param exception current exception string */ void -aot_set_exception(AOTModuleInstance *module_inst, - const char *exception); +aot_set_exception(AOTModuleInstance *module_inst, const char *exception); void -aot_set_exception_with_id(AOTModuleInstance *module_inst, - uint32 id); +aot_set_exception_with_id(AOTModuleInstance *module_inst, uint32 id); /** * Get exception info of the AOT module instance. @@ -567,7 +564,7 @@ aot_set_exception_with_id(AOTModuleInstance *module_inst, * * @return the exception string */ -const char* +const char * aot_get_exception(AOTModuleInstance *module_inst); /** @@ -583,24 +580,23 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, void **p_native_addr); uint32 -aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, - uint32 size, void **p_native_addr); +aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size, + void **p_native_addr); void aot_module_free(AOTModuleInstance *module_inst, uint32 ptr); uint32 -aot_module_dup_data(AOTModuleInstance *module_inst, - const char *src, uint32 size); +aot_module_dup_data(AOTModuleInstance *module_inst, const char *src, + uint32 size); bool -aot_validate_app_addr(AOTModuleInstance *module_inst, - uint32 app_offset, uint32 size); - +aot_validate_app_addr(AOTModuleInstance *module_inst, uint32 app_offset, + uint32 size); bool -aot_validate_native_addr(AOTModuleInstance *module_inst, - void *native_ptr, uint32 size); +aot_validate_native_addr(AOTModuleInstance *module_inst, void *native_ptr, + uint32 size); void * aot_addr_app_to_native(AOTModuleInstance *module_inst, uint32 app_offset); @@ -609,14 +605,11 @@ uint32 aot_addr_native_to_app(AOTModuleInstance *module_inst, void *native_ptr); bool -aot_get_app_addr_range(AOTModuleInstance *module_inst, - uint32 app_offset, - uint32 *p_app_start_offset, - uint32 *p_app_end_offset); +aot_get_app_addr_range(AOTModuleInstance *module_inst, uint32 app_offset, + uint32 *p_app_start_offset, uint32 *p_app_end_offset); bool -aot_get_native_addr_range(AOTModuleInstance *module_inst, - uint8 *native_ptr, +aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr, uint8 **p_native_start_addr, uint8 **p_native_end_addr); @@ -633,19 +626,18 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count); * @return true if equal, false otherwise */ bool -aot_is_wasm_type_equal(AOTModuleInstance *module_inst, - uint32 type1_idx, uint32 type2_idx); +aot_is_wasm_type_equal(AOTModuleInstance *module_inst, uint32 type1_idx, + uint32 type2_idx); /** * Invoke native function from aot code */ bool -aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, - uint32 argc, uint32 *argv); +aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, + uint32 *argv); bool -aot_call_indirect(WASMExecEnv *exec_env, - uint32 tbl_idx, uint32 table_elem_idx, +aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx, uint32 argc, uint32 *argv); uint32 @@ -659,8 +651,8 @@ aot_memset(void *s, int c, size_t n); #if WASM_ENABLE_BULK_MEMORY != 0 bool -aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, - uint32 offset, uint32 len, uint32 dst); +aot_memory_init(AOTModuleInstance *module_inst, uint32 seg_index, uint32 offset, + uint32 len, uint32 dst); bool aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index); @@ -668,12 +660,10 @@ aot_data_drop(AOTModuleInstance *module_inst, uint32 seg_index); #if WASM_ENABLE_THREAD_MGR != 0 bool -aot_set_aux_stack(WASMExecEnv *exec_env, - uint32 start_offset, uint32 size); +aot_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size); bool -aot_get_aux_stack(WASMExecEnv *exec_env, - uint32 *start_offset, uint32 *size); +aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size); #endif #ifdef OS_ENABLE_HW_BOUND_CHECK @@ -697,18 +687,18 @@ void aot_drop_table_seg(AOTModuleInstance *module_inst, uint32 tbl_seg_idx); void -aot_table_init(AOTModuleInstance *module_inst, - uint32 tbl_idx, uint32 tbl_seg_idx, - uint32 length, uint32 src_offset, uint32 dst_offset); +aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, + uint32 tbl_seg_idx, uint32 length, uint32 src_offset, + uint32 dst_offset); void -aot_table_copy(AOTModuleInstance *module_inst, - uint32 src_tbl_idx, uint32 dst_tbl_idx, - uint32 length, uint32 src_offset, uint32 dst_offset); +aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx, + uint32 dst_tbl_idx, uint32 length, uint32 src_offset, + uint32 dst_offset); void -aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, - uint32 length, uint32 val, uint32 data_offset); +aot_table_fill(AOTModuleInstance *module_inst, uint32 tbl_idx, uint32 length, + uint32 val, uint32 data_offset); uint32 aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx, @@ -735,4 +725,3 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst); #endif #endif /* end of _AOT_RUNTIME_H_ */ - diff --git a/core/iwasm/aot/arch/aot_reloc_aarch64.c b/core/iwasm/aot/arch/aot_reloc_aarch64.c index 14e4ac7d39..0ca22b84b3 100644 --- a/core/iwasm/aot/arch/aot_reloc_aarch64.c +++ b/core/iwasm/aot/arch/aot_reloc_aarch64.c @@ -5,37 +5,39 @@ #include "aot_reloc.h" -#define R_AARCH64_MOVW_UABS_G0 263 -#define R_AARCH64_MOVW_UABS_G0_NC 264 -#define R_AARCH64_MOVW_UABS_G1 265 -#define R_AARCH64_MOVW_UABS_G1_NC 266 -#define R_AARCH64_MOVW_UABS_G2 267 -#define R_AARCH64_MOVW_UABS_G2_NC 268 -#define R_AARCH64_MOVW_UABS_G3 269 - -#define R_AARCH64_MOVW_SABS_G0 270 -#define R_AARCH64_MOVW_SABS_G1 271 -#define R_AARCH64_MOVW_SABS_G2 272 - -#define R_AARCH64_ADR_PREL_LO19 273 -#define R_AARCH64_ADR_PREL_LO21 274 -#define R_AARCH64_ADR_PREL_PG_HI21 275 -#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 - -#define R_AARCH64_ADD_ABS_LO12_NC 277 - -#define R_AARCH64_LDST8_ABS_LO12_NC 278 -#define R_AARCH64_LDST16_ABS_LO12_NC 284 -#define R_AARCH64_LDST32_ABS_LO12_NC 285 -#define R_AARCH64_LDST64_ABS_LO12_NC 286 -#define R_AARCH64_LDST128_ABS_LO12_NC 299 - -#define R_AARCH64_JUMP26 282 -#define R_AARCH64_CALL26 283 - +#define R_AARCH64_MOVW_UABS_G0 263 +#define R_AARCH64_MOVW_UABS_G0_NC 264 +#define R_AARCH64_MOVW_UABS_G1 265 +#define R_AARCH64_MOVW_UABS_G1_NC 266 +#define R_AARCH64_MOVW_UABS_G2 267 +#define R_AARCH64_MOVW_UABS_G2_NC 268 +#define R_AARCH64_MOVW_UABS_G3 269 + +#define R_AARCH64_MOVW_SABS_G0 270 +#define R_AARCH64_MOVW_SABS_G1 271 +#define R_AARCH64_MOVW_SABS_G2 272 + +#define R_AARCH64_ADR_PREL_LO19 273 +#define R_AARCH64_ADR_PREL_LO21 274 +#define R_AARCH64_ADR_PREL_PG_HI21 275 +#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 + +#define R_AARCH64_ADD_ABS_LO12_NC 277 + +#define R_AARCH64_LDST8_ABS_LO12_NC 278 +#define R_AARCH64_LDST16_ABS_LO12_NC 284 +#define R_AARCH64_LDST32_ABS_LO12_NC 285 +#define R_AARCH64_LDST64_ABS_LO12_NC 286 +#define R_AARCH64_LDST128_ABS_LO12_NC 299 + +#define R_AARCH64_JUMP26 282 +#define R_AARCH64_CALL26 283 + +/* clang-format off */ static SymbolMap target_sym_map[] = { REG_COMMON_SYMBOLS }; +/* clang-format on */ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) @@ -55,7 +57,7 @@ get_target_symbol_map(uint32 *sym_num) void get_current_target(char *target_buf, uint32 target_buf_size) { - const char * s = BUILD_TARGET; + const char *s = BUILD_TARGET; size_t s_size = sizeof(BUILD_TARGET); char *d = target_buf; @@ -64,14 +66,14 @@ get_current_target(char *target_buf, uint32 target_buf_size) s = BUILD_TARGET_AARCH64_DEFAULT; s_size = sizeof(BUILD_TARGET_AARCH64_DEFAULT); } - if(target_buf_size < s_size){ + if (target_buf_size < s_size) { s_size = target_buf_size; } while (--s_size) { if (*s >= 'A' && *s <= 'Z') *d++ = *s++ + 'a' - 'A'; else - *d++ = *s++ ; + *d++ = *s++; } /* Ensure the string is null byte ('\0') terminated */ *d = '\0'; @@ -90,15 +92,16 @@ init_plt_table(uint8 *plt) { uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); for (i = 0; i < num; i++) { - uint32 *p = (uint32*)plt; + uint32 *p = (uint32 *)plt; *p++ = 0xf81f0ffe; /* str x30, [sp, #-16]! */ - *p++ = 0x100000be; /* adr x30, #20 ;symbol addr is PC + 5 instructions below */ + *p++ = 0x100000be; /* adr x30, #20; symbol addr is PC + 5 instructions + below */ *p++ = 0xf94003de; /* ldr x30, [x30] */ *p++ = 0xd63f03c0; /* blr x30 */ *p++ = 0xf84107fe; /* ldr x30, [sp], #16 */ *p++ = 0xd61f03c0; /* br x30 */ /* symbol addr */ - *(uint64*)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; + *(uint64 *)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; p += 2; plt += get_plt_item_size(); } @@ -110,17 +113,18 @@ get_plt_table_size() return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); } -#define SIGN_EXTEND_TO_INT64(val, bits, val_ext) do { \ - int64 m = (int64)((uint64)1 << (bits - 1)); \ - val_ext = ((int64)val ^ m) - m; \ -} while (0) +#define SIGN_EXTEND_TO_INT64(val, bits, val_ext) \ + do { \ + int64 m = (int64)((uint64)1 << (bits - 1)); \ + val_ext = ((int64)val ^ m) - m; \ + } while (0) #define Page(expr) ((expr) & ~0xFFF) static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -132,22 +136,21 @@ check_reloc_offset(uint32 target_section_size, } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { case R_AARCH64_CALL26: { - void *S, *P = (void*)(target_section_addr + reloc_offset); + void *S, *P = (void *)(target_section_addr + reloc_offset); int64 X, A, initial_addend; int32 insn, imm26; CHECK_RELOC_OFFSET(sizeof(int32)); - insn = *(int32*)P; + insn = *(int32 *)P; imm26 = insn & 0x3FFFFFF; SIGN_EXTEND_TO_INT64(imm26 << 2, 28, initial_addend); A = initial_addend; @@ -164,17 +167,18 @@ apply_relocation(AOTModule *module, else { uint8 *plt; if (reloc_addend > 0) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: relocate to plt table " - "with reloc addend larger than 0 is unsupported."); - return false; + set_error_buf( + error_buf, error_buf_size, + "AOT module load failed: relocate to plt table " + "with reloc addend larger than 0 is unsupported."); + return false; } /* Symbol address is not an AOT function, * but a function of runtime or native. Its address is * beyond of the +-128MB space. Apply relocation with * the PLT which branch to the target symbol address. */ - S = plt = (uint8*)module->code + module->code_size + S = plt = (uint8 *)module->code + module->code_size - get_plt_table_size() + get_plt_item_size() * symbol_index; } @@ -191,7 +195,7 @@ apply_relocation(AOTModule *module, } /* write the imm26 back to instruction */ - *(int32*)P = (insn & 0xFC000000) | ((int32)((X >> 2) & 0x3FFFFFF)); + *(int32 *)P = (insn & 0xFC000000) | ((int32)((X >> 2) & 0x3FFFFFF)); break; } @@ -203,13 +207,14 @@ apply_relocation(AOTModule *module, case R_AARCH64_MOVW_UABS_G2_NC: case R_AARCH64_MOVW_UABS_G3: { - void *S = symbol_addr, *P = (void*)(target_section_addr + reloc_offset); + void *S = symbol_addr, + *P = (void *)(target_section_addr + reloc_offset); int64 X, A, initial_addend; int32 insn, imm16; CHECK_RELOC_OFFSET(sizeof(int32)); - insn = *(int32*)P; + insn = *(int32 *)P; imm16 = (insn >> 5) & 0xFFFF; SIGN_EXTEND_TO_INT64(imm16, 16, initial_addend); @@ -241,18 +246,22 @@ apply_relocation(AOTModule *module, switch (reloc_type) { case R_AARCH64_MOVW_UABS_G0: case R_AARCH64_MOVW_UABS_G0_NC: - *(int32*)P = (insn & 0xFFE0001F) | ((int32)((X & 0xFFFF) << 5)); + *(int32 *)P = + (insn & 0xFFE0001F) | ((int32)((X & 0xFFFF) << 5)); break; case R_AARCH64_MOVW_UABS_G1: case R_AARCH64_MOVW_UABS_G1_NC: - *(int32*)P = (insn & 0xFFE0001F) | ((int32)(((X >> 16) & 0xFFFF) << 5)); + *(int32 *)P = (insn & 0xFFE0001F) + | ((int32)(((X >> 16) & 0xFFFF) << 5)); break; case R_AARCH64_MOVW_UABS_G2: case R_AARCH64_MOVW_UABS_G2_NC: - *(int32*)P = (insn & 0xFFE0001F) | ((int32)(((X >> 32) & 0xFFFF) << 5)); + *(int32 *)P = (insn & 0xFFE0001F) + | ((int32)(((X >> 32) & 0xFFFF) << 5)); break; case R_AARCH64_MOVW_UABS_G3: - *(int32*)P = (insn & 0xFFE0001F) | ((int32)(((X >> 48) & 0xFFFF) << 5)); + *(int32 *)P = (insn & 0xFFE0001F) + | ((int32)(((X >> 48) & 0xFFFF) << 5)); break; default: bh_assert(0); @@ -264,13 +273,14 @@ apply_relocation(AOTModule *module, case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_ADR_PREL_PG_HI21_NC: { - void *S = symbol_addr, *P = (void*)(target_section_addr + reloc_offset); + void *S = symbol_addr, + *P = (void *)(target_section_addr + reloc_offset); int64 X, A, initial_addend; int32 insn, immhi19, immlo2, imm21; CHECK_RELOC_OFFSET(sizeof(int32)); - insn = *(int32*)P; + insn = *(int32 *)P; immhi19 = (insn >> 5) & 0x7FFFF; immlo2 = (insn >> 29) & 0x3; imm21 = (immhi19 << 2) | immlo2; @@ -290,20 +300,21 @@ apply_relocation(AOTModule *module, /* write the imm21 back to instruction */ immhi19 = (int32)(((X >> 12) >> 2) & 0x7FFFF); immlo2 = (int32)((X >> 12) & 0x3); - *(int32*)P = (insn & 0x9F00001F) | (immlo2 << 29) | (immhi19 << 5); + *(int32 *)P = (insn & 0x9F00001F) | (immlo2 << 29) | (immhi19 << 5); break; } case R_AARCH64_ADD_ABS_LO12_NC: { - void *S = symbol_addr, *P = (void*)(target_section_addr + reloc_offset); + void *S = symbol_addr, + *P = (void *)(target_section_addr + reloc_offset); int64 X, A, initial_addend; int32 insn, imm12; CHECK_RELOC_OFFSET(sizeof(int32)); - insn = *(int32*)P; + insn = *(int32 *)P; imm12 = (insn >> 10) & 0xFFF; SIGN_EXTEND_TO_INT64(imm12, 12, initial_addend); @@ -316,7 +327,7 @@ apply_relocation(AOTModule *module, /* No need to check overflow for this reloction type */ /* write the imm12 back to instruction */ - *(int32*)P = (insn & 0xFFC003FF) | ((int32)((X & 0xFFF) << 10)); + *(int32 *)P = (insn & 0xFFC003FF) | ((int32)((X & 0xFFF) << 10)); break; } @@ -326,13 +337,14 @@ apply_relocation(AOTModule *module, case R_AARCH64_LDST64_ABS_LO12_NC: case R_AARCH64_LDST128_ABS_LO12_NC: { - void *S = symbol_addr, *P = (void*)(target_section_addr + reloc_offset); + void *S = symbol_addr, + *P = (void *)(target_section_addr + reloc_offset); int64 X, A, initial_addend; int32 insn, imm12; CHECK_RELOC_OFFSET(sizeof(int32)); - insn = *(int32*)P; + insn = *(int32 *)P; imm12 = (insn >> 10) & 0xFFF; SIGN_EXTEND_TO_INT64(imm12, 12, initial_addend); @@ -347,19 +359,24 @@ apply_relocation(AOTModule *module, /* write the imm12 back to instruction */ switch (reloc_type) { case R_AARCH64_LDST8_ABS_LO12_NC: - *(int32*)P = (insn & 0xFFC003FF) | ((int32)((X & 0xFFF) << 10)); + *(int32 *)P = + (insn & 0xFFC003FF) | ((int32)((X & 0xFFF) << 10)); break; case R_AARCH64_LDST16_ABS_LO12_NC: - *(int32*)P = (insn & 0xFFC003FF) | ((int32)(((X & 0xFFF) >> 1) << 10)); + *(int32 *)P = (insn & 0xFFC003FF) + | ((int32)(((X & 0xFFF) >> 1) << 10)); break; case R_AARCH64_LDST32_ABS_LO12_NC: - *(int32*)P = (insn & 0xFFC003FF) | ((int32)(((X & 0xFFF) >> 2) << 10)); + *(int32 *)P = (insn & 0xFFC003FF) + | ((int32)(((X & 0xFFF) >> 2) << 10)); break; case R_AARCH64_LDST64_ABS_LO12_NC: - *(int32*)P = (insn & 0xFFC003FF) | ((int32)(((X & 0xFFF) >> 3) << 10)); + *(int32 *)P = (insn & 0xFFC003FF) + | ((int32)(((X & 0xFFF) >> 3) << 10)); break; case R_AARCH64_LDST128_ABS_LO12_NC: - *(int32*)P = (insn & 0xFFC003FF) | ((int32)(((X & 0xFFF) >> 4) << 10)); + *(int32 *)P = (insn & 0xFFC003FF) + | ((int32)(((X & 0xFFF) >> 4) << 10)); break; default: bh_assert(0); diff --git a/core/iwasm/aot/arch/aot_reloc_arc.c b/core/iwasm/aot/arch/aot_reloc_arc.c index d3dbcf2136..6c23ac679e 100644 --- a/core/iwasm/aot/arch/aot_reloc_arc.c +++ b/core/iwasm/aot/arch/aot_reloc_arc.c @@ -5,13 +5,14 @@ #include "aot_reloc.h" -#define R_ARC_S21H_PCREL 14 -#define R_ARC_S21W_PCREL 15 -#define R_ARC_S25H_PCREL 16 -#define R_ARC_S25W_PCREL 17 -#define R_ARC_32 4 -#define R_ARC_32_ME 27 - +#define R_ARC_S21H_PCREL 14 +#define R_ARC_S21W_PCREL 15 +#define R_ARC_S25H_PCREL 16 +#define R_ARC_S25W_PCREL 17 +#define R_ARC_32 4 +#define R_ARC_32_ME 27 + +/* clang-format off */ void __st_r13_to_r15(); void __st_r13_to_r16(); void __st_r13_to_r17(); @@ -56,10 +57,13 @@ void __subdf3(); void __subsf3(); void __truncdfsf2(); void __unorddf2(); +/* clang-format on */ static SymbolMap target_sym_map[] = { + /* clang-format off */ REG_COMMON_SYMBOLS REG_SYM(__st_r13_to_r15), + /* clang-format on */ REG_SYM(__st_r13_to_r16), REG_SYM(__st_r13_to_r17), REG_SYM(__st_r13_to_r18), @@ -81,29 +85,28 @@ static SymbolMap target_sym_map[] = { REG_SYM(__ld_r13_to_r23), REG_SYM(__ld_r13_to_r24), REG_SYM(__ld_r13_to_r25), - REG_SYM (__adddf3), - REG_SYM (__addsf3), - REG_SYM (__divdf3), - REG_SYM (__divdi3), - REG_SYM (__divsf3), - REG_SYM (__divsi3), - REG_SYM (__eqsf2), - REG_SYM (__extendsfdf2), - REG_SYM (__fixdfsi), - REG_SYM (__floatsidf), - REG_SYM (__floatsisf), - REG_SYM (__gedf2), - REG_SYM (__gtdf2), - REG_SYM (__ledf2), - REG_SYM (__lesf2), - REG_SYM (__ltdf2), - REG_SYM (__muldf3), - REG_SYM (__mulsf3), - REG_SYM (__subdf3), - REG_SYM (__subsf3), - REG_SYM (__truncdfsf2), - REG_SYM (__unorddf2), - + REG_SYM(__adddf3), + REG_SYM(__addsf3), + REG_SYM(__divdf3), + REG_SYM(__divdi3), + REG_SYM(__divsf3), + REG_SYM(__divsi3), + REG_SYM(__eqsf2), + REG_SYM(__extendsfdf2), + REG_SYM(__fixdfsi), + REG_SYM(__floatsidf), + REG_SYM(__floatsisf), + REG_SYM(__gedf2), + REG_SYM(__gtdf2), + REG_SYM(__ledf2), + REG_SYM(__lesf2), + REG_SYM(__ltdf2), + REG_SYM(__muldf3), + REG_SYM(__mulsf3), + REG_SYM(__subdf3), + REG_SYM(__subsf3), + REG_SYM(__truncdfsf2), + REG_SYM(__unorddf2), }; static void @@ -139,9 +142,9 @@ init_plt_table(uint8 *plt) } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -159,11 +162,10 @@ middle_endian_convert(uint32 insn) } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { case R_ARC_S25W_PCREL: @@ -172,7 +174,7 @@ apply_relocation(AOTModule *module, int32 addend, value; uintptr_t S, A, P; - CHECK_RELOC_OFFSET(sizeof(void*)); + CHECK_RELOC_OFFSET(sizeof(void *)); /* Convert from middle endian */ insn = middle_endian_convert(insn); @@ -187,7 +189,7 @@ apply_relocation(AOTModule *module, addend = addend << 2; /* (S + A) - P */ - S = (uintptr_t)(uint8*)symbol_addr; + S = (uintptr_t)(uint8 *)symbol_addr; A = (uintptr_t)reloc_addend; P = (uintptr_t)(target_section_addr + reloc_offset); P &= (uintptr_t)~3; @@ -209,11 +211,10 @@ apply_relocation(AOTModule *module, { uint32 insn; - CHECK_RELOC_OFFSET(sizeof(void*)); + CHECK_RELOC_OFFSET(sizeof(void *)); /* (S + A) */ - insn = (uint32)(uintptr_t) - ((uint8*)symbol_addr + reloc_addend); + insn = (uint32)(uintptr_t)((uint8 *)symbol_addr + reloc_addend); if (reloc_type == R_ARC_32_ME) /* Convert to middle endian */ @@ -234,4 +235,3 @@ apply_relocation(AOTModule *module, } return true; } - diff --git a/core/iwasm/aot/arch/aot_reloc_arm.c b/core/iwasm/aot/arch/aot_reloc_arm.c index e3e2d1a7fa..5d5091c639 100644 --- a/core/iwasm/aot/arch/aot_reloc_arm.c +++ b/core/iwasm/aot/arch/aot_reloc_arm.c @@ -5,10 +5,11 @@ #include "aot_reloc.h" -#define R_ARM_CALL 28 /* PC relative 24 bit (BL, BLX). */ -#define R_ARM_JMP24 29 /* PC relative 24 bit (B/BL). */ -#define R_ARM_ABS32 2 /* Direct 32 bit */ +#define R_ARM_CALL 28 /* PC relative 24 bit (BL, BLX). */ +#define R_ARM_JMP24 29 /* PC relative 24 bit (B/BL). */ +#define R_ARM_ABS32 2 /* Direct 32 bit */ +/* clang-format off */ void __divdi3(); void __udivdi3(); void __moddi3(); @@ -54,11 +55,14 @@ void __aeabi_fcmple(); void __aeabi_fcmpge(); void __aeabi_f2iz(); void __aeabi_f2d(); +/* clang-format on */ static SymbolMap target_sym_map[] = { + /* clang-format off */ REG_COMMON_SYMBOLS /* compiler-rt symbols that come from compiler(e.g. gcc) */ REG_SYM(__divdi3), + /* clang-format on */ REG_SYM(__udivdi3), REG_SYM(__umoddi3), REG_SYM(__divsi3), @@ -122,7 +126,7 @@ get_target_symbol_map(uint32 *sym_num) void get_current_target(char *target_buf, uint32 target_buf_size) { - const char * s = BUILD_TARGET; + const char *s = BUILD_TARGET; size_t s_size = sizeof(BUILD_TARGET); char *d = target_buf; @@ -131,14 +135,14 @@ get_current_target(char *target_buf, uint32 target_buf_size) s = BUILD_TARGET_ARM_DEFAULT; s_size = sizeof(BUILD_TARGET_ARM_DEFAULT); } - if(target_buf_size < s_size){ + if (target_buf_size < s_size) { s_size = target_buf_size; } while (--s_size) { if (*s >= 'A' && *s <= 'Z') *d++ = *s++ + 'a' - 'A'; else - *d++ = *s++ ; + *d++ = *s++; } /* Ensure the string is null byte ('\0') terminated */ *d = '\0'; @@ -163,7 +167,7 @@ init_plt_table(uint8 *plt) { uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); for (i = 0; i < num; i++) { - uint32 *p = (uint32*)plt; + uint32 *p = (uint32 *)plt; /* ldr pc, [pc] */ *p++ = 0xe59ff000; /* nop */ @@ -175,9 +179,9 @@ init_plt_table(uint8 *plt) } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -189,11 +193,10 @@ check_reloc_offset(uint32 target_section_size, } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { case R_ARM_CALL: @@ -201,10 +204,10 @@ apply_relocation(AOTModule *module, { intptr_t result; int32 RESULT_MASK = 0x03FFFFFE; - int32 insn = *(int32*)(target_section_addr + reloc_offset); + int32 insn = *(int32 *)(target_section_addr + reloc_offset); /* Initial addend: sign_extend(insn[23:0] << 2) */ - int32 initial_addend = ((insn & 0xFFFFFF) << 2) - | ((insn & 0x800000) ? 0xFC000000 : 0); + int32 initial_addend = + ((insn & 0xFFFFFF) << 2) | ((insn & 0x800000) ? 0xFC000000 : 0); CHECK_RELOC_OFFSET(sizeof(int32)); @@ -214,17 +217,18 @@ apply_relocation(AOTModule *module, * Suppose the symbol address is in +-32MB relative * to the relocation address. */ - /* operation: ((S + A) | T) - P where S is symbol address and T is 0 */ - result = (intptr_t) - ((uint8*)symbol_addr + reloc_addend - - (target_section_addr + reloc_offset)); + /* operation: ((S + A) | T) - P where S is symbol address and T + * is 0 */ + result = (intptr_t)((uint8 *)symbol_addr + reloc_addend + - (target_section_addr + reloc_offset)); } else { if (reloc_addend > 0) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: relocate to plt table " - "with reloc addend larger than 0 is unsupported."); - return false; + set_error_buf( + error_buf, error_buf_size, + "AOT module load failed: relocate to plt table " + "with reloc addend larger than 0 is unsupported."); + return false; } /* Symbol address is not an AOT function, @@ -232,12 +236,13 @@ apply_relocation(AOTModule *module, * beyond of the +-32MB space. Apply relocation with * the PLT which branch to the target symbol address. */ - /* operation: ((S + A) | T) - P where S is PLT address and T is 0 */ - uint8 *plt = (uint8*)module->code + module->code_size - get_plt_table_size() + /* operation: ((S + A) | T) - P where S is PLT address and T is + * 0 */ + uint8 *plt = (uint8 *)module->code + module->code_size + - get_plt_table_size() + get_plt_item_size() * symbol_index; - result = (intptr_t) - (plt + reloc_addend - - (target_section_addr + reloc_offset)); + result = (intptr_t)(plt + reloc_addend + - (target_section_addr + reloc_offset)); } result += initial_addend; @@ -250,20 +255,20 @@ apply_relocation(AOTModule *module, return false; } - *(int32*)(target_section_addr + reloc_offset) = - (int32) - ((insn & 0xff000000) - | (((int32)result & RESULT_MASK) >> 2)); + *(int32 *)(target_section_addr + reloc_offset) = + (int32)((insn & 0xff000000) + | (((int32)result & RESULT_MASK) >> 2)); break; } case R_ARM_ABS32: { intptr_t initial_addend; /* (S + A) | T where T is 0 */ - CHECK_RELOC_OFFSET(sizeof(void*)); - initial_addend = *(intptr_t*)(target_section_addr + (uint32)reloc_offset); - *(uint8**)(target_section_addr + reloc_offset) - = (uint8*)symbol_addr + initial_addend + reloc_addend; + CHECK_RELOC_OFFSET(sizeof(void *)); + initial_addend = + *(intptr_t *)(target_section_addr + (uint32)reloc_offset); + *(uint8 **)(target_section_addr + reloc_offset) = + (uint8 *)symbol_addr + initial_addend + reloc_addend; break; } @@ -278,4 +283,3 @@ apply_relocation(AOTModule *module, return true; } - diff --git a/core/iwasm/aot/arch/aot_reloc_mips.c b/core/iwasm/aot/arch/aot_reloc_mips.c index c1b3fa54c0..8e43cc031c 100644 --- a/core/iwasm/aot/arch/aot_reloc_mips.c +++ b/core/iwasm/aot/arch/aot_reloc_mips.c @@ -5,12 +5,14 @@ #include "aot_reloc.h" -#define R_MIPS_32 2 /* Direct 32 bit */ -#define R_MIPS_26 4 /* Direct 26 bit shifted */ +#define R_MIPS_32 2 /* Direct 32 bit */ +#define R_MIPS_26 4 /* Direct 26 bit shifted */ +/* clang-format off */ static SymbolMap target_sym_map[] = { REG_COMMON_SYMBOLS }; +/* clang-format on */ SymbolMap * get_target_symbol_map(uint32 *sym_num) @@ -44,11 +46,10 @@ get_plt_table_size() } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { /* TODO: implement relocation for mips */ @@ -66,4 +67,3 @@ apply_relocation(AOTModule *module, return true; } - diff --git a/core/iwasm/aot/arch/aot_reloc_riscv.c b/core/iwasm/aot/arch/aot_reloc_riscv.c index cb09b810a6..05b8d734db 100644 --- a/core/iwasm/aot/arch/aot_reloc_riscv.c +++ b/core/iwasm/aot/arch/aot_reloc_riscv.c @@ -5,25 +5,29 @@ #include "aot_reloc.h" -#define R_RISCV_32 1 -#define R_RISCV_64 2 -#define R_RISCV_CALL 18 +#define R_RISCV_32 1 +#define R_RISCV_64 2 +#define R_RISCV_CALL 18 #define R_RISCV_CALL_PLT 19 -#define R_RISCV_HI20 26 -#define R_RISCV_LO12_I 27 -#define R_RISCV_LO12_S 28 +#define R_RISCV_HI20 26 +#define R_RISCV_LO12_I 27 +#define R_RISCV_LO12_S 28 #define RV_OPCODE_SW 0x23 +/* clang-format off */ void __divdi3(); void __moddi3(); void __muldi3(); void __udivdi3(); void __umoddi3(); +/* clang-format on */ static SymbolMap target_sym_map[] = { + /* clang-format off */ REG_COMMON_SYMBOLS REG_SYM(__divdi3), + /* clang-format on */ REG_SYM(__moddi3), REG_SYM(__muldi3), REG_SYM(__udivdi3), @@ -132,20 +136,20 @@ init_plt_table(uint8 *plt) for (i = 0; i < num; i++) { p = plt; /* auipc t1, 0 */ - *(uint16*)p = 0x0317; + *(uint16 *)p = 0x0317; p += 2; - *(uint16*)p = 0x0000; + *(uint16 *)p = 0x0000; p += 2; /* ld t1, 8(t1) */ - *(uint16*)p = 0x3303; + *(uint16 *)p = 0x3303; p += 2; - *(uint16*)p = 0x00C3; + *(uint16 *)p = 0x00C3; p += 2; /* jr t1 */ - *(uint16*)p = 0x8302; + *(uint16 *)p = 0x8302; p += 2; /* nop */ - *(uint16*)p = 0x0001; + *(uint16 *)p = 0x0001; p += 2; bh_memcpy_s(p, 8, &target_sym_map[i].symbol_addr, 8); p += 8; @@ -159,15 +163,15 @@ typedef struct RelocTypeStrMap { char *reloc_str; } RelocTypeStrMap; -#define RELOC_TYPE_MAP(reloc_type) { reloc_type, #reloc_type } +#define RELOC_TYPE_MAP(reloc_type) \ + { \ + reloc_type, #reloc_type \ + } static RelocTypeStrMap reloc_type_str_maps[] = { - RELOC_TYPE_MAP(R_RISCV_32), - RELOC_TYPE_MAP(R_RISCV_CALL), - RELOC_TYPE_MAP(R_RISCV_CALL_PLT), - RELOC_TYPE_MAP(R_RISCV_HI20), - RELOC_TYPE_MAP(R_RISCV_LO12_I), - RELOC_TYPE_MAP(R_RISCV_LO12_S), + RELOC_TYPE_MAP(R_RISCV_32), RELOC_TYPE_MAP(R_RISCV_CALL), + RELOC_TYPE_MAP(R_RISCV_CALL_PLT), RELOC_TYPE_MAP(R_RISCV_HI20), + RELOC_TYPE_MAP(R_RISCV_LO12_I), RELOC_TYPE_MAP(R_RISCV_LO12_S), }; static const char * @@ -175,7 +179,8 @@ reloc_type_to_str(uint32 reloc_type) { uint32 i; - for (i = 0; i < sizeof(reloc_type_str_maps) / sizeof(RelocTypeStrMap); i++) { + for (i = 0; i < sizeof(reloc_type_str_maps) / sizeof(RelocTypeStrMap); + i++) { if (reloc_type_str_maps[i].reloc_type == reloc_type) return reloc_type_str_maps[i].reloc_str; } @@ -184,9 +189,9 @@ reloc_type_to_str(uint32 reloc_type) } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -198,11 +203,10 @@ check_reloc_offset(uint32 target_section_size, } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, uint32 reloc_type, - void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { int32 val, imm_hi, imm_lo, insn; uint8 *addr = target_section_addr + reloc_offset; @@ -211,7 +215,8 @@ apply_relocation(AOTModule *module, switch (reloc_type) { case R_RISCV_32: { - uint32 val_32 = (uint32)(uintptr_t)((uint8 *)symbol_addr + reloc_addend); + uint32 val_32 = + (uint32)(uintptr_t)((uint8 *)symbol_addr + reloc_addend); CHECK_RELOC_OFFSET(sizeof(uint32)); if (val_32 != (uintptr_t)((uint8 *)symbol_addr + reloc_addend)) { @@ -223,7 +228,8 @@ apply_relocation(AOTModule *module, } case R_RISCV_64: { - uint64 val_64 = (uint64)(uintptr_t)((uint8 *)symbol_addr + reloc_addend); + uint64 val_64 = + (uint64)(uintptr_t)((uint8 *)symbol_addr + reloc_addend); CHECK_RELOC_OFFSET(sizeof(uint64)); bh_memcpy_s(addr, 8, &val_64, 8); break; @@ -237,10 +243,10 @@ apply_relocation(AOTModule *module, if (val != (intptr_t)((uint8 *)symbol_addr - addr)) { if (symbol_index >= 0) { /* Call runtime function by plt code */ - symbol_addr = (uint8*)module->code + module->code_size + symbol_addr = (uint8 *)module->code + module->code_size - get_plt_table_size() + get_plt_item_size() * symbol_index; - val = (int32)(intptr_t)((uint8*)symbol_addr - addr); + val = (int32)(intptr_t)((uint8 *)symbol_addr - addr); } } @@ -253,8 +259,8 @@ apply_relocation(AOTModule *module, rv_add_val((uint16 *)addr, (imm_hi << 12)); if ((rv_get_val((uint16 *)(addr + 4)) & 0x7f) == RV_OPCODE_SW) { /* Adjust imm for SW : S-type */ - val = - (((int32)imm_lo >> 5) << 25) + (((int32)imm_lo & 0x1f) << 7); + val = (((int32)imm_lo >> 5) << 25) + + (((int32)imm_lo & 0x1f) << 7); rv_add_val((uint16 *)(addr + 4), val); } @@ -310,8 +316,7 @@ apply_relocation(AOTModule *module, addr = target_section_addr + reloc_offset; rv_calc_imm(val, &imm_hi, &imm_lo); - val = - (((int32)imm_lo >> 5) << 25) + (((int32)imm_lo & 0x1f) << 7); + val = (((int32)imm_lo >> 5) << 25) + (((int32)imm_lo & 0x1f) << 7); rv_add_val((uint16 *)addr, val); break; } diff --git a/core/iwasm/aot/arch/aot_reloc_thumb.c b/core/iwasm/aot/arch/aot_reloc_thumb.c index 19ed62c066..1da8c86d31 100644 --- a/core/iwasm/aot/arch/aot_reloc_thumb.c +++ b/core/iwasm/aot/arch/aot_reloc_thumb.c @@ -5,9 +5,10 @@ #include "aot_reloc.h" -#define R_ARM_THM_CALL 10 /* PC relative (Thumb BL and ARMv5 Thumb BLX). */ -#define R_ARM_THM_JMP24 30 /* B.W */ +#define R_ARM_THM_CALL 10 /* PC relative (Thumb BL and ARMv5 Thumb BLX). */ +#define R_ARM_THM_JMP24 30 /* B.W */ +/* clang-format off */ void __ltdf2(); void __adddf3(); void __eqdf2(); @@ -69,11 +70,14 @@ void __aeabi_fcmple(); void __aeabi_fcmpge(); void __aeabi_f2iz(); void __aeabi_f2d(); +/* clang-format on */ static SymbolMap target_sym_map[] = { + /* clang-format off */ REG_COMMON_SYMBOLS /* compiler-rt symbols that come from compiler(e.g. gcc) */ REG_SYM(__ltdf2), + /* clang-format on */ REG_SYM(__adddf3), REG_SYM(__eqdf2), REG_SYM(__unorddf2), @@ -162,14 +166,14 @@ get_current_target(char *target_buf, uint32 target_buf_size) s = BUILD_TARGET_THUMB_V4T; s_size = sizeof(BUILD_TARGET_THUMB_V4T); } - if(target_buf_size < s_size){ + if (target_buf_size < s_size) { s_size = target_buf_size; } while (--s_size) { if (*s >= 'A' && *s <= 'Z') *d++ = *s++ + 'a' - 'A'; else - *d++ = *s++ ; + *d++ = *s++; } /* Ensure the string is null byte ('\0') terminated */ *d = '\0'; @@ -194,7 +198,7 @@ init_plt_table(uint8 *plt) { uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap); for (i = 0; i < num; i++) { - uint16 *p = (uint16*)plt; + uint16 *p = (uint16 *)plt; /* nop */ *p++ = 0xbf00; /* push {r4} */ @@ -212,15 +216,15 @@ init_plt_table(uint8 *plt) /* nop */ *p++ = 0xbf00; /* symbol addr */ - *(uint32*)p = (uint32)(uintptr_t)target_sym_map[i].symbol_addr; + *(uint32 *)p = (uint32)(uintptr_t)target_sym_map[i].symbol_addr; plt += get_plt_item_size(); } } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -232,11 +236,10 @@ check_reloc_offset(uint32 target_section_size, } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { case R_ARM_THM_CALL: @@ -250,7 +253,7 @@ apply_relocation(AOTModule *module, CHECK_RELOC_OFFSET(sizeof(int32)); - reloc_addr = (int16*)(target_section_addr + reloc_offset); + reloc_addr = (int16 *)(target_section_addr + reloc_offset); initial_addend_0 = (*reloc_addr) & 0x7FF; initial_addend_1 = (*(reloc_addr + 1)) & 0x7FF; sign = (initial_addend_0 & 0x400) ? true : false; @@ -263,16 +266,20 @@ apply_relocation(AOTModule *module, * Suppose the symbol address is in +-4MB relative * to the relocation address. */ - /* operation: ((S + A) | T) - P where S is symbol address and T is 1 */ - result = (int32)(((intptr_t)((uint8*)symbol_addr + reloc_addend) | 1) - - (intptr_t)(target_section_addr + reloc_offset)); + /* operation: ((S + A) | T) - P where S is symbol address + and T is 1 */ + result = + (int32)(((intptr_t)((uint8 *)symbol_addr + reloc_addend) + | 1) + - (intptr_t)(target_section_addr + reloc_offset)); } else { if (reloc_addend > 0) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: relocate to plt table " - "with reloc addend larger than 0 is unsupported."); - return false; + set_error_buf( + error_buf, error_buf_size, + "AOT module load failed: relocate to plt table " + "with reloc addend larger than 0 is unsupported."); + return false; } /* Symbol address is not an AOT function, @@ -280,11 +287,14 @@ apply_relocation(AOTModule *module, * beyond of the +-4MB space. Apply relocation with * the PLT which branch to the target symbol address. */ - /* operation: ((S + A) | T) - P where S is PLT address and T is 1 */ - uint8 *plt = (uint8*)module->code + module->code_size - get_plt_table_size() + /* operation: ((S + A) | T) - P where S is PLT address + and T is 1 */ + uint8 *plt = (uint8 *)module->code + module->code_size + - get_plt_table_size() + get_plt_item_size() * symbol_index + 1; - result = (int32)(((intptr_t)plt | 1) - - (intptr_t)(target_section_addr + reloc_offset)); + result = + (int32)(((intptr_t)plt | 1) + - (intptr_t)(target_section_addr + reloc_offset)); } result += initial_addend; @@ -316,4 +326,3 @@ apply_relocation(AOTModule *module, } return true; } - diff --git a/core/iwasm/aot/arch/aot_reloc_x86_32.c b/core/iwasm/aot/arch/aot_reloc_x86_32.c index 2b13b1dab3..784468231d 100644 --- a/core/iwasm/aot/arch/aot_reloc_x86_32.c +++ b/core/iwasm/aot/arch/aot_reloc_x86_32.c @@ -5,17 +5,19 @@ #include "aot_reloc.h" -#define R_386_32 1 /* Direct 32 bit */ -#define R_386_PC32 2 /* PC relative 32 bit */ +#define R_386_32 1 /* Direct 32 bit */ +#define R_386_PC32 2 /* PC relative 32 bit */ #if !defined(_WIN32) && !defined(_WIN32_) +/* clang-format off */ void __divdi3(); void __udivdi3(); void __moddi3(); void __umoddi3(); +/* clang-format on */ #else -#pragma function (floor) -#pragma function (ceil) +#pragma function(floor) +#pragma function(ceil) static int64 __divdi3(int64 a, int64 b) @@ -42,6 +44,7 @@ __umoddi3(uint64 a, uint64 b) } #endif +/* clang-format off */ static SymbolMap target_sym_map[] = { REG_COMMON_SYMBOLS /* compiler-rt symbols that come from compiler(e.g. gcc) */ @@ -50,6 +53,7 @@ static SymbolMap target_sym_map[] = { REG_SYM(__moddi3), REG_SYM(__umoddi3) }; +/* clang-format on */ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) @@ -84,9 +88,9 @@ init_plt_table(uint8 *plt) } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -98,21 +102,20 @@ check_reloc_offset(uint32 target_section_size, } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { case R_386_32: { intptr_t value; - CHECK_RELOC_OFFSET(sizeof(void*)); - value = *(intptr_t*)(target_section_addr + (uint32)reloc_offset); - *(uint8**)(target_section_addr + reloc_offset) - = (uint8*)symbol_addr + reloc_addend + value; /* S + A */ + CHECK_RELOC_OFFSET(sizeof(void *)); + value = *(intptr_t *)(target_section_addr + (uint32)reloc_offset); + *(uint8 **)(target_section_addr + reloc_offset) = + (uint8 *)symbol_addr + reloc_addend + value; /* S + A */ break; } @@ -120,12 +123,12 @@ apply_relocation(AOTModule *module, { int32 value; - CHECK_RELOC_OFFSET(sizeof(void*)); - value = *(int32*)(target_section_addr + (uint32)reloc_offset); - *(uint32*)(target_section_addr + (uint32)reloc_offset) = (uint32) - ((uint8*)symbol_addr + (uint32)reloc_addend - - (uint8*)(target_section_addr + (uint32)reloc_offset) - + value); /* S + A - P */ + CHECK_RELOC_OFFSET(sizeof(void *)); + value = *(int32 *)(target_section_addr + (uint32)reloc_offset); + *(uint32 *)(target_section_addr + (uint32)reloc_offset) = + (uint32)((uint8 *)symbol_addr + (uint32)reloc_addend + - (uint8 *)(target_section_addr + (uint32)reloc_offset) + + value); /* S + A - P */ break; } @@ -139,4 +142,3 @@ apply_relocation(AOTModule *module, } return true; } - diff --git a/core/iwasm/aot/arch/aot_reloc_x86_64.c b/core/iwasm/aot/arch/aot_reloc_x86_64.c index a04b6a6eee..be5d09f8bc 100644 --- a/core/iwasm/aot/arch/aot_reloc_x86_64.c +++ b/core/iwasm/aot/arch/aot_reloc_x86_64.c @@ -6,30 +6,34 @@ #include "aot_reloc.h" #if !defined(BH_PLATFORM_WINDOWS) -#define R_X86_64_64 1 /* Direct 64 bit */ -#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ -#define R_X86_64_PLT32 4 /* 32 bit PLT address */ -#define R_X86_64_32 10 /* Direct 32 bit zero extended */ -#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ +#define R_X86_64_64 1 /* Direct 64 bit */ +#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ +#define R_X86_64_PLT32 4 /* 32 bit PLT address */ +#define R_X86_64_32 10 /* Direct 32 bit zero extended */ +#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ #else #ifndef IMAGE_REL_AMD64_ADDR64 #define IMAGE_REL_AMD64_ADDR64 1 /* The 64-bit VA of the relocation target */ #define IMAGE_REL_AMD64_ADDR32 2 /* The 32-bit VA of the relocation target */ +/* clang-format off */ #define IMAGE_REL_AMD64_REL32 4 /* The 32-bit relative address from the byte following the relocation*/ +/* clang-format on */ #endif #endif #if defined(BH_PLATFORM_WINDOWS) -#pragma function (floor) -#pragma function (ceil) -#pragma function (floorf) -#pragma function (ceilf) +#pragma function(floor) +#pragma function(ceil) +#pragma function(floorf) +#pragma function(ceilf) #endif +/* clang-format off */ static SymbolMap target_sym_map[] = { REG_COMMON_SYMBOLS }; +/* clang-format on */ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) @@ -61,8 +65,8 @@ get_plt_item_size() uint32 get_plt_table_size() { - uint32 size = get_plt_item_size() - * (sizeof(target_sym_map) / sizeof(SymbolMap)); + uint32 size = + get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap)); #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS) size += get_plt_item_size() + sizeof(AOTUnwindInfo); #endif @@ -80,7 +84,7 @@ init_plt_table(uint8 *plt) /* mov symbol_addr, rax */ *p++ = 0x48; *p++ = 0xB8; - *(uint64*)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; + *(uint64 *)p = (uint64)(uintptr_t)target_sym_map[i].symbol_addr; p += sizeof(uint64); /* jmp rax */ *p++ = 0xFF; @@ -92,19 +96,19 @@ init_plt_table(uint8 *plt) p = plt; /* mov exception_handler, rax */ *p++ = 0x48; - *p++ = 0xB8; - *(uint64*)p = 0;/*(uint64)(uintptr_t)aot_exception_handler;*/ + *p++ = 0xB8; + *(uint64 *)p = 0; /*(uint64)(uintptr_t)aot_exception_handler;*/ p += sizeof(uint64); /* jmp rax */ - *p++ = 0xFF; - *p++ = 0xE0; + *p++ = 0xFF; + *p++ = 0xE0; #endif } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -116,11 +120,10 @@ check_reloc_offset(uint32 target_section_size, } bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { #if !defined(BH_PLATFORM_WINDOWS) @@ -131,10 +134,10 @@ apply_relocation(AOTModule *module, { intptr_t value; - CHECK_RELOC_OFFSET(sizeof(void*)); - value = *(intptr_t*)(target_section_addr + (uint32)reloc_offset); - *(uint8**)(target_section_addr + reloc_offset) - = (uint8*)symbol_addr + reloc_addend + value; /* S + A */ + CHECK_RELOC_OFFSET(sizeof(void *)); + value = *(intptr_t *)(target_section_addr + (uint32)reloc_offset); + *(uint8 **)(target_section_addr + reloc_offset) = + (uint8 *)symbol_addr + reloc_addend + value; /* S + A */ break; } #if defined(BH_PLATFORM_WINDOWS) @@ -144,14 +147,14 @@ apply_relocation(AOTModule *module, uintptr_t target_addr; CHECK_RELOC_OFFSET(sizeof(void *)); - value = *(int32*)(target_section_addr + (uint32)reloc_offset); + value = *(int32 *)(target_section_addr + (uint32)reloc_offset); target_addr = (uintptr_t)symbol_addr + reloc_addend + value; if ((int32)target_addr != target_addr) { - set_error_buf( - error_buf, error_buf_size, - "AOT module load failed: " - "relocation truncated to fit IMAGE_REL_AMD64_ADDR32 failed. " - "Try using wamrc with --size-level=1 option."); + set_error_buf(error_buf, error_buf_size, + "AOT module load failed: " + "relocation truncated to fit " + "IMAGE_REL_AMD64_ADDR32 failed. " + "Try using wamrc with --size-level=1 option."); return false; } @@ -162,20 +165,21 @@ apply_relocation(AOTModule *module, #if !defined(BH_PLATFORM_WINDOWS) case R_X86_64_PC32: { - intptr_t target_addr = (intptr_t) /* S + A - P */ - ((uint8*)symbol_addr + reloc_addend - - (target_section_addr + reloc_offset)); + intptr_t target_addr = (intptr_t) /* S + A - P */ + ((uint8 *)symbol_addr + reloc_addend + - (target_section_addr + reloc_offset)); CHECK_RELOC_OFFSET(sizeof(int32)); if ((int32)target_addr != target_addr) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "relocation truncated to fit R_X86_64_PC32 failed. " - "Try using wamrc with --size-level=1 option."); + set_error_buf( + error_buf, error_buf_size, + "AOT module load failed: " + "relocation truncated to fit R_X86_64_PC32 failed. " + "Try using wamrc with --size-level=1 option."); return false; } - *(int32*)(target_section_addr + reloc_offset) = (int32)target_addr; + *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; break; } case R_X86_64_32: @@ -183,7 +187,7 @@ apply_relocation(AOTModule *module, { char buf[128]; uintptr_t target_addr = (uintptr_t) /* S + A */ - ((uint8*)symbol_addr + reloc_addend); + ((uint8 *)symbol_addr + reloc_addend); CHECK_RELOC_OFFSET(sizeof(int32)); @@ -192,16 +196,16 @@ apply_relocation(AOTModule *module, || (reloc_type == R_X86_64_32S && (int32)target_addr != (int64)target_addr)) { snprintf(buf, sizeof(buf), - "AOT module load failed: " - "relocation truncated to fit %s failed. " - "Try using wamrc with --size-level=1 option.", - reloc_type == R_X86_64_32 - ? "R_X86_64_32" : "R_X86_64_32S"); + "AOT module load failed: " + "relocation truncated to fit %s failed. " + "Try using wamrc with --size-level=1 option.", + reloc_type == R_X86_64_32 ? "R_X86_64_32" + : "R_X86_64_32S"); set_error_buf(error_buf, error_buf_size, buf); return false; } - *(int32*)(target_section_addr + reloc_offset) = (int32)target_addr; + *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; break; } #endif @@ -217,16 +221,16 @@ apply_relocation(AOTModule *module, CHECK_RELOC_OFFSET(sizeof(int32)); if (symbol_index >= 0) { - plt = (uint8*)module->code + module->code_size - get_plt_table_size() + plt = (uint8 *)module->code + module->code_size + - get_plt_table_size() + get_plt_item_size() * symbol_index; - target_addr = (intptr_t) /* L + A - P */ - (plt + reloc_addend - - (target_section_addr + reloc_offset)); + target_addr = (intptr_t) /* L + A - P */ + (plt + reloc_addend - (target_section_addr + reloc_offset)); } else { - target_addr = (intptr_t) /* L + A - P */ - ((uint8*)symbol_addr + reloc_addend - - (target_section_addr + reloc_offset)); + target_addr = (intptr_t) /* L + A - P */ + ((uint8 *)symbol_addr + reloc_addend + - (target_section_addr + reloc_offset)); } #if defined(BH_PLATFORM_WINDOWS) @@ -244,7 +248,7 @@ apply_relocation(AOTModule *module, "Try using wamrc with --size-level=1 option."); return false; } - *(int32*)(target_section_addr + reloc_offset) = (int32)target_addr; + *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; break; } @@ -259,4 +263,3 @@ apply_relocation(AOTModule *module, return true; } - diff --git a/core/iwasm/aot/arch/aot_reloc_xtensa.c b/core/iwasm/aot/arch/aot_reloc_xtensa.c index 91499a1736..d60231f8f5 100644 --- a/core/iwasm/aot/arch/aot_reloc_xtensa.c +++ b/core/iwasm/aot/arch/aot_reloc_xtensa.c @@ -5,9 +5,10 @@ #include "aot_reloc.h" -#define R_XTENSA_32 1 /* Direct 32 bit */ -#define R_XTENSA_SLOT0_OP 20 /* PC relative */ +#define R_XTENSA_32 1 /* Direct 32 bit */ +#define R_XTENSA_SLOT0_OP 20 /* PC relative */ +/* clang-format off */ /* for soft-float */ void __floatsidf(); void __divdf3(); @@ -38,9 +39,9 @@ static SymbolMap target_sym_map[] = { REG_SYM(__muldi3), REG_SYM(__modsi3), - REG_SYM(__divdi3), }; +/* clang-format on */ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) @@ -81,9 +82,9 @@ get_plt_table_size() } static bool -check_reloc_offset(uint32 target_section_size, - uint64 reloc_offset, uint32 reloc_data_size, - char *error_buf, uint32 error_buf_size) +check_reloc_offset(uint32 target_section_size, uint64 reloc_offset, + uint32 reloc_data_size, char *error_buf, + uint32 error_buf_size) { if (!(reloc_offset < (uint64)target_section_size && reloc_offset + reloc_data_size <= (uint64)target_section_size)) { @@ -106,18 +107,18 @@ put_imm16_to_addr(int16 imm16, int16 *addr) int8 bytes[8]; int32 *addr_aligned1, *addr_aligned2; - addr_aligned1 = (int32*)((intptr_t)addr & ~3); + addr_aligned1 = (int32 *)((intptr_t)addr & ~3); if ((intptr_t)addr % 4 != 3) { - *(int32*)bytes = *addr_aligned1; - *(int16*)(bytes + ((intptr_t)addr % 4)) = imm16; + *(int32 *)bytes = *addr_aligned1; + *(int16 *)(bytes + ((intptr_t)addr % 4)) = imm16; memcpy(addr_aligned1, bytes, 4); } else { - addr_aligned2 = (int32*)(((intptr_t)addr + 3) & ~3); - *(int32*)bytes = *addr_aligned1; - *(int32*)(bytes + 4) = *addr_aligned2; - *(int16*)(bytes + 3) = imm16; + addr_aligned2 = (int32 *)(((intptr_t)addr + 3) & ~3); + *(int32 *)bytes = *addr_aligned1; + *(int32 *)(bytes + 4) = *addr_aligned2; + *(int16 *)(bytes + 3) = imm16; memcpy(addr_aligned1, bytes, 8); } } @@ -142,11 +143,10 @@ typedef union { } l32r_insn_t; bool -apply_relocation(AOTModule *module, - uint8 *target_section_addr, uint32 target_section_size, - uint64 reloc_offset, uint64 reloc_addend, - uint32 reloc_type, void *symbol_addr, int32 symbol_index, - char *error_buf, uint32 error_buf_size) +apply_relocation(AOTModule *module, uint8 *target_section_addr, + uint32 target_section_size, uint64 reloc_offset, + uint64 reloc_addend, uint32 reloc_type, void *symbol_addr, + int32 symbol_index, char *error_buf, uint32 error_buf_size) { switch (reloc_type) { case R_XTENSA_32: @@ -161,19 +161,20 @@ apply_relocation(AOTModule *module, return false; } CHECK_RELOC_OFFSET(4); - initial_addend = *(int32*)insn_addr; - *(uint8**)insn_addr - = (uint8*)symbol_addr + initial_addend + reloc_addend; + initial_addend = *(int32 *)insn_addr; + *(uint8 **)insn_addr = + (uint8 *)symbol_addr + initial_addend + reloc_addend; break; } case R_XTENSA_SLOT0_OP: { uint8 *insn_addr = target_section_addr + reloc_offset; - /* Currently only l32r instruction generates R_XTENSA_SLOT0_OP relocation */ + /* Currently only l32r instruction generates R_XTENSA_SLOT0_OP + * relocation */ l32r_insn_t *l32r_insn = (l32r_insn_t *)insn_addr; uint8 *reloc_addr; - int32 relative_offset/*, initial_addend */; + int32 relative_offset /*, initial_addend */; int16 imm16; CHECK_RELOC_OFFSET(3); /* size of l32r instruction */ @@ -184,7 +185,7 @@ apply_relocation(AOTModule *module, initial_addend = (int32)imm16 << 2; */ - reloc_addr = (uint8*)symbol_addr + reloc_addend; + reloc_addr = (uint8 *)symbol_addr + reloc_addend; if ((intptr_t)reloc_addr & 3) { set_error_buf(error_buf, error_buf_size, @@ -193,9 +194,9 @@ apply_relocation(AOTModule *module, return false; } - relative_offset = (int32) - ((intptr_t)reloc_addr - - (((intptr_t)insn_addr + 3) & ~(intptr_t)3)); + relative_offset = + (int32)((intptr_t)reloc_addr + - (((intptr_t)insn_addr + 3) & ~(intptr_t)3)); /* relative_offset += initial_addend; */ /* check relative offset boundary */ @@ -228,4 +229,3 @@ apply_relocation(AOTModule *module, return true; } - diff --git a/core/iwasm/aot/debug/elf_parser.c b/core/iwasm/aot/debug/elf_parser.c index 57714ce6ee..2fe3e11f2b 100644 --- a/core/iwasm/aot/debug/elf_parser.c +++ b/core/iwasm/aot/debug/elf_parser.c @@ -114,12 +114,13 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size) if (sh_table) { read_section_header_table64(eh, sh_table); sh_str = get_section64(eh, sh_table[eh->e_shstrndx]); - for (i= 0; i < eh->e_shnum; i++) { + for (i = 0; i < eh->e_shnum; i++) { if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) { *offset = sh_table[i]->sh_offset; *size = sh_table[i]->sh_size; - sh_table[i]->sh_addr = (Elf64_Addr)(uintptr_t) - ((char *)buf + sh_table[i]->sh_offset); + sh_table[i]->sh_addr = + (Elf64_Addr)(uintptr_t)((char *)buf + + sh_table[i]->sh_offset); ret = true; break; } @@ -134,12 +135,13 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size) if (sh_table) { read_section_header_table(eh, sh_table); sh_str = get_section(eh, sh_table[eh->e_shstrndx]); - for (i= 0; i < eh->e_shnum; i++) { + for (i = 0; i < eh->e_shnum; i++) { if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) { *offset = sh_table[i]->sh_offset; *size = sh_table[i]->sh_size; - sh_table[i]->sh_addr = (Elf32_Addr)(uintptr_t) - ((char *)buf + sh_table[i]->sh_offset); + sh_table[i]->sh_addr = + (Elf32_Addr)(uintptr_t)((char *)buf + + sh_table[i]->sh_offset); ret = true; break; } diff --git a/core/iwasm/aot/debug/jit_debug.c b/core/iwasm/aot/debug/jit_debug.c index 8d79ff037f..310662f559 100644 --- a/core/iwasm/aot/debug/jit_debug.c +++ b/core/iwasm/aot/debug/jit_debug.c @@ -12,14 +12,14 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * Copyright (C) 2021 Ant Group. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ #include "bh_log.h" #include "bh_platform.h" -#include "wasm_runtime.h" +#include "../../interpreter/wasm_runtime.h" #include #include @@ -35,11 +35,13 @@ extern "C" { #endif -typedef enum { +/* clang-format off */ +typedef enum JITAction { JIT_NOACTION = 0, JIT_REGISTER_FN, JIT_UNREGISTER_FN } JITAction; +/* clang-format on */ typedef struct JITCodeEntry { struct JITCodeEntry *next_; @@ -78,7 +80,8 @@ void __attribute__((noinline)) __jit_debug_register_code() JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, NULL, NULL }; #else -extern void __jit_debug_register_code(); +extern void +__jit_debug_register_code(); extern JITDescriptor __jit_debug_descriptor; #endif @@ -168,8 +171,7 @@ jit_debug_engine_init() return true; } - if (!(jit_debug_engine = - wasm_runtime_malloc(sizeof(WASMJITDebugEngine)))) { + if (!(jit_debug_engine = wasm_runtime_malloc(sizeof(WASMJITDebugEngine)))) { LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory"); return false; } diff --git a/core/iwasm/common/arch/invokeNative_general.c b/core/iwasm/common/arch/invokeNative_general.c index 8d6e70f4f4..4799c9fa82 100644 --- a/core/iwasm/common/arch/invokeNative_general.c +++ b/core/iwasm/common/arch/invokeNative_general.c @@ -6,11 +6,12 @@ #include "../wasm_runtime_common.h" #include "../wasm_exec_env.h" -void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc) +void +invokeNative(void (*native_code)(), uint32 argv[], uint32 argc) { - bh_assert(argc >= sizeof(WASMExecEnv*)/sizeof(uint32)); + bh_assert(argc >= sizeof(WASMExecEnv *) / sizeof(uint32)); - switch(argc) { + switch (argc) { case 0: native_code(); break; @@ -33,53 +34,80 @@ void invokeNative(void (*native_code)(), uint32 argv[], uint32 argc) native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); break; case 7: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6]); break; case 8: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7]); break; case 9: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8]); break; case 10: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9]); break; case 11: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10]); break; case 12: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]); break; case 13: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12]); break; case 14: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13]); break; case 15: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14]); break; case 16: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14], argv[15]); break; case 17: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14], argv[15], argv[16]); break; case 18: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14], argv[15], argv[16], + argv[17]); break; case 19: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14], argv[15], argv[16], + argv[17], argv[18]); break; case 20: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]); + native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14], argv[15], argv[16], + argv[17], argv[18], argv[19]); break; default: { /* FIXME: If this happen, add more cases. */ - WASMExecEnv *exec_env = *(WASMExecEnv**)argv; + WASMExecEnv *exec_env = *(WASMExecEnv **)argv; WASMModuleInstanceCommon *module_inst = exec_env->module_inst; - wasm_runtime_set_exception(module_inst, "the argument number of native function exceeds maximum"); + wasm_runtime_set_exception( + module_inst, + "the argument number of native function exceeds maximum"); return; } } diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index b3e979fc9b..03771b17e9 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -24,15 +24,12 @@ runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst, { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { if (module_inst != NULL) { - wasm_runtime_set_exception(module_inst, - "allocate memory failed"); + wasm_runtime_set_exception(module_inst, "allocate memory failed"); } else if (error_buf != NULL) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); } return NULL; } @@ -52,29 +49,31 @@ static union { * Implementation of wasm_application_execute_main() */ -static WASMFunctionInstanceCommon* -resolve_function(const WASMModuleInstanceCommon *module_inst, - const char *name); +static WASMFunctionInstanceCommon * +resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name); static bool check_main_func_type(const WASMType *type) { if (!(type->param_count == 0 || type->param_count == 2) - ||type->result_count > 1) { - LOG_ERROR("WASM execute application failed: invalid main function type.\n"); + || type->result_count > 1) { + LOG_ERROR( + "WASM execute application failed: invalid main function type.\n"); return false; } if (type->param_count == 2 && !(type->types[0] == VALUE_TYPE_I32 - && type->types[1] == VALUE_TYPE_I32)) { - LOG_ERROR("WASM execute application failed: invalid main function type.\n"); + && type->types[1] == VALUE_TYPE_I32)) { + LOG_ERROR( + "WASM execute application failed: invalid main function type.\n"); return false; } if (type->result_count && type->types[type->param_count] != VALUE_TYPE_I32) { - LOG_ERROR("WASM execute application failed: invalid main function type.\n"); + LOG_ERROR( + "WASM execute application failed: invalid main function type.\n"); return false; } @@ -82,8 +81,8 @@ check_main_func_type(const WASMType *type) } bool -wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, - int32 argc, char *argv[]) +wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, + char *argv[]) { WASMFunctionInstanceCommon *func; WASMType *func_type = NULL; @@ -103,9 +102,9 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, the actual main function. Directly call main function may cause exception thrown. */ if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) - return wasm_runtime_create_exec_env_and_call_wasm( - module_inst, func, 0, NULL); - /* if no start function is found, we execute + return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, + 0, NULL); + /* If no start function was found, we execute the main function as normal */ } #endif /* end of WASM_ENABLE_LIBC_WASI */ @@ -113,25 +112,23 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, if (!(func = resolve_function(module_inst, "main")) && !(func = resolve_function(module_inst, "__main_argc_argv")) && !(func = resolve_function(module_inst, "_main"))) { - wasm_runtime_set_exception(module_inst, - "lookup main function failed"); + wasm_runtime_set_exception(module_inst, "lookup main function failed"); return false; } #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - is_import_func = ((WASMFunctionInstance*)func)->is_import_func; + is_import_func = ((WASMFunctionInstance *)func)->is_import_func; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - is_import_func = ((AOTFunctionInstance*)func)->is_import_func; + is_import_func = ((AOTFunctionInstance *)func)->is_import_func; } #endif if (is_import_func) { - wasm_runtime_set_exception(module_inst, - "lookup main function failed"); + wasm_runtime_set_exception(module_inst, "lookup main function failed"); return false; } @@ -157,34 +154,34 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc; if (total_size >= UINT32_MAX - || !(argv_buf_offset = - wasm_runtime_module_malloc(module_inst, (uint32)total_size, - (void**)&argv_buf))) { - wasm_runtime_set_exception(module_inst, - "allocate memory failed"); + || !(argv_buf_offset = wasm_runtime_module_malloc( + module_inst, (uint32)total_size, (void **)&argv_buf))) { + wasm_runtime_set_exception(module_inst, "allocate memory failed"); return false; } p = argv_buf; - argv_offsets = (uint32*)(p + total_argv_size); + argv_offsets = (uint32 *)(p + total_argv_size); p_end = p + total_size; for (i = 0; i < argc; i++) { - bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1)); + bh_memcpy_s(p, (uint32)(p_end - p), argv[i], + (uint32)(strlen(argv[i]) + 1)); argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf); p += strlen(argv[i]) + 1; } argc1 = 2; argv1[0] = (uint32)argc; - argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets); + argv1[1] = + (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets); } - ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, - argc1, argv1); + ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1, + argv1); if (ret && func_type->result_count > 0 && argc > 0 && argv) /* copy the return value */ - *(int*)argv = (int)argv1[0]; + *(int *)argv = (int)argv1[0]; if (argv_buf_offset) wasm_runtime_module_free(module_inst, argv_buf_offset); @@ -197,7 +194,7 @@ get_sub_module_inst(const WASMModuleInstance *parent_module_inst, const char *sub_module_name) { WASMSubModInstNode *node = - bh_list_first_elem(parent_module_inst->sub_module_inst_list); + bh_list_first_elem(parent_module_inst->sub_module_inst_list); while (node && strcmp(node->module_name, sub_module_name)) { node = bh_list_elem_next(node); @@ -241,9 +238,8 @@ parse_function_name(char *orig_function_name, char **p_module_name, * Implementation of wasm_application_execute_func() */ -static WASMFunctionInstanceCommon* -resolve_function(const WASMModuleInstanceCommon *module_inst, - const char *name) +static WASMFunctionInstanceCommon * +resolve_function(const WASMModuleInstanceCommon *module_inst, const char *name) { uint32 i = 0; WASMFunctionInstanceCommon *ret = NULL; @@ -268,8 +264,8 @@ resolve_function(const WASMModuleInstanceCommon *module_inst, LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name); if (sub_module_name) { - sub_module_inst = get_sub_module_inst( - (WASMModuleInstance *)module_inst, sub_module_name); + sub_module_inst = get_sub_module_inst((WASMModuleInstance *)module_inst, + sub_module_name); if (!sub_module_inst) { LOG_DEBUG("can not find a sub module named %s", sub_module_name); goto LEAVE; @@ -281,26 +277,26 @@ resolve_function(const WASMModuleInstanceCommon *module_inst, #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst; + WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst; #if WASM_ENABLE_MULTI_MODULE != 0 wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst; #endif /* WASM_ENABLE_MULTI_MODULE */ for (i = 0; i < wasm_inst->export_func_count; i++) { - if (!strcmp(wasm_inst->export_functions[i].name, function_name)) { + if (!strcmp(wasm_inst->export_functions[i].name, function_name)) { ret = wasm_inst->export_functions[i].function; break; - } + } } } #endif /* WASM_ENABLE_INTERP */ #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst; - AOTFunctionInstance *export_funcs = (AOTFunctionInstance *) - aot_inst->export_funcs.ptr; + AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst; + AOTFunctionInstance *export_funcs = + (AOTFunctionInstance *)aot_inst->export_funcs.ptr; for (i = 0; i < aot_inst->export_func_count; i++) { if (!strcmp(export_funcs[i].func_name, function_name)) { ret = &export_funcs[i]; @@ -323,14 +319,14 @@ union ieee754_float { /* This is the IEEE 754 single-precision format. */ union { struct { - unsigned int negative:1; - unsigned int exponent:8; - unsigned int mantissa:23; + unsigned int negative : 1; + unsigned int exponent : 8; + unsigned int mantissa : 23; } ieee_big_endian; struct { - unsigned int mantissa:23; - unsigned int exponent:8; - unsigned int negative:1; + unsigned int mantissa : 23; + unsigned int exponent : 8; + unsigned int negative : 1; } ieee_little_endian; } ieee; }; @@ -341,19 +337,19 @@ union ieee754_double { /* This is the IEEE 754 double-precision format. */ union { struct { - unsigned int negative:1; - unsigned int exponent:11; + unsigned int negative : 1; + unsigned int exponent : 11; /* Together these comprise the mantissa. */ - unsigned int mantissa0:20; - unsigned int mantissa1:32; + unsigned int mantissa0 : 20; + unsigned int mantissa1 : 32; } ieee_big_endian; struct { /* Together these comprise the mantissa. */ - unsigned int mantissa1:32; - unsigned int mantissa0:20; - unsigned int exponent:11; - unsigned int negative:1; + unsigned int mantissa1 : 32; + unsigned int mantissa0 : 20; + unsigned int exponent : 11; + unsigned int negative : 1; } ieee_little_endian; } ieee; }; @@ -382,7 +378,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func; + WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)func; if (wasm_func->is_import_func #if WASM_ENABLE_MULTI_MODULE != 0 && !wasm_func->import_func_inst @@ -404,8 +400,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, } if (type->param_count != (uint32)argc) { - wasm_runtime_set_exception(module_inst, - "invalid input argument count"); + wasm_runtime_set_exception(module_inst, "invalid input argument count"); goto fail; } @@ -413,8 +408,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num; total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2); - if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, - NULL, 0)))) { + if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) { goto fail; } @@ -433,7 +427,10 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, break; case VALUE_TYPE_I64: { - union { uint64 val; uint32 parts[2]; } u; + union { + uint64 val; + uint32 parts[2]; + } u; u.val = strtoull(argv[i], &endptr, 0); argv1[p++] = u.parts[0]; argv1[p++] = u.parts[1]; @@ -469,7 +466,10 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, } case VALUE_TYPE_F64: { - union { float64 val; uint32 parts[2]; } u; + union { + float64 val; + uint32 parts[2]; + } u; u.val = strtod(argv[i], &endptr); if (isnan(u.val)) { if (argv[i][0] == '-') { @@ -506,11 +506,11 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, { /* it likes 0x123\0x234 or 123\234 */ /* retrive first i64 */ - *(uint64*)(argv1 + p) = strtoull(argv[i], &endptr, 0); + *(uint64 *)(argv1 + p) = strtoull(argv[i], &endptr, 0); /* skip \ */ endptr++; /* retrive second i64 */ - *(uint64*)(argv1 + p + 2) = strtoull(endptr, &endptr, 0); + *(uint64 *)(argv1 + p + 2) = strtoull(endptr, &endptr, 0); p += 4; break; } @@ -541,7 +541,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, if (!wasm_externref_obj2ref(module_inst, extern_obj, &externref_idx)) { wasm_runtime_set_exception( - module_inst, "map extern object to ref failed"); + module_inst, "map extern object to ref failed"); goto fail; } argv1[p++] = externref_idx; @@ -554,8 +554,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, break; } if (endptr && *endptr != '\0' && *endptr != '_') { - snprintf(buf, sizeof(buf), "invalid input argument %d: %s", - i, argv[i]); + snprintf(buf, sizeof(buf), "invalid input argument %d: %s", i, + argv[i]); wasm_runtime_set_exception(module_inst, buf); goto fail; } @@ -563,8 +563,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, bh_assert(p == (int32)argc1); wasm_runtime_set_exception(module_inst, NULL); - if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, - argc1, argv1)) { + if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func, argc1, + argv1)) { goto fail; } @@ -579,12 +579,15 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, } case VALUE_TYPE_I64: { - union { uint64 val; uint32 parts[2]; } u; + union { + uint64 val; + uint32 parts[2]; + } u; u.parts[0] = argv1[k]; u.parts[1] = argv1[k + 1]; k += 2; #ifdef PRIx64 - os_printf("0x%"PRIx64":i64", u.val); + os_printf("0x%" PRIx64 ":i64", u.val); #else char buf[16]; if (sizeof(long) == 4) @@ -597,13 +600,16 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, } case VALUE_TYPE_F32: { - os_printf("%.7g:f32", *(float32*)(argv1 + k)); + os_printf("%.7g:f32", *(float32 *)(argv1 + k)); k++; break; } case VALUE_TYPE_F64: { - union { float64 val; uint32 parts[2]; } u; + union { + float64 val; + uint32 parts[2]; + } u; u.parts[0] = argv1[k]; u.parts[1] = argv1[k + 1]; k += 2; @@ -638,9 +644,10 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, #if WASM_ENABLE_SIMD != 0 case VALUE_TYPE_V128: { - uint64 *v = (uint64*)(argv1 + k); + uint64 *v = (uint64 *)(argv1 + k); #if defined(PRIx64) - os_printf("<0x%016"PRIx64" 0x%016"PRIx64">:v128", *v, *(v + 1)); + os_printf("<0x%016" PRIx64 " 0x%016" PRIx64 ">:v128", *v, + *(v + 1)); #else if (4 == sizeof(long)) { os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1)); diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 6bf60e2e78..e08363f84c 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -14,7 +14,7 @@ #endif #define ASSERT_NOT_IMPLEMENTED() bh_assert(!"not implemented") -#define UNREACHABLE() bh_assert(!"unreachable") +#define UNREACHABLE() bh_assert(!"unreachable") typedef struct wasm_module_ex_t wasm_module_ex_t; @@ -63,160 +63,159 @@ malloc_internal(uint64 size) } /* clang-format off */ -#define RETURN_OBJ(obj, obj_del_func) \ - return obj; \ -failed: \ - obj_del_func(obj); \ +#define RETURN_OBJ(obj, obj_del_func) \ + return obj; \ +failed: \ + obj_del_func(obj); \ return NULL; -#define RETURN_VOID(obj, obj_del_func) \ - return; \ -failed: \ - obj_del_func(obj); \ +#define RETURN_VOID(obj, obj_del_func) \ + return; \ +failed: \ + obj_del_func(obj); \ return; /* clang-format on */ /* Vectors */ -#define INIT_VEC(vector_p, init_func, ...) \ - do { \ - if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \ - goto failed; \ - } \ - \ - init_func(vector_p, ##__VA_ARGS__); \ - if (vector_p->size && !vector_p->data) { \ - LOG_DEBUG("%s failed", #init_func); \ - goto failed; \ - } \ +#define INIT_VEC(vector_p, init_func, ...) \ + do { \ + if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \ + goto failed; \ + } \ + \ + init_func(vector_p, ##__VA_ARGS__); \ + if (vector_p->size && !vector_p->data) { \ + LOG_DEBUG("%s failed", #init_func); \ + goto failed; \ + } \ } while (false) -#define DEINIT_VEC(vector_p, deinit_func) \ - if ((vector_p)) { \ - deinit_func(vector_p); \ - wasm_runtime_free(vector_p); \ - vector_p = NULL; \ +#define DEINIT_VEC(vector_p, deinit_func) \ + if ((vector_p)) { \ + deinit_func(vector_p); \ + wasm_runtime_free(vector_p); \ + vector_p = NULL; \ } -#define WASM_DEFINE_VEC(name) \ - void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t *out) \ - { \ - wasm_##name##_vec_new_uninitialized(out, 0); \ - } \ - void wasm_##name##_vec_new_uninitialized(own wasm_##name##_vec_t *out, \ - size_t size) \ - { \ - wasm_##name##_vec_new(out, size, NULL); \ +#define WASM_DEFINE_VEC(name) \ + void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t *out) \ + { \ + wasm_##name##_vec_new_uninitialized(out, 0); \ + } \ + void wasm_##name##_vec_new_uninitialized(own wasm_##name##_vec_t *out, \ + size_t size) \ + { \ + wasm_##name##_vec_new(out, size, NULL); \ } /* vectors with no ownership management of elements */ -#define WASM_DEFINE_VEC_PLAIN(name) \ - WASM_DEFINE_VEC(name) \ - void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \ - own wasm_##name##_t const data[]) \ - { \ - if (!out) { \ - return; \ - } \ - \ - memset(out, 0, sizeof(wasm_##name##_vec_t)); \ - \ - if (!size) { \ - return; \ - } \ - \ - if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t))) { \ - LOG_DEBUG("bh_vector_init failed"); \ - goto failed; \ - } \ - \ - if (data) { \ - uint32 size_in_bytes = 0; \ - size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t)); \ - bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \ - out->num_elems = size; \ - } \ - \ - RETURN_VOID(out, wasm_##name##_vec_delete) \ - } \ - void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \ - const wasm_##name##_vec_t *src) \ - { \ - wasm_##name##_vec_new(out, src->size, src->data); \ - } \ - void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \ - { \ - if (v) { \ - bh_vector_destroy((Vector *)v); \ - } \ +#define WASM_DEFINE_VEC_PLAIN(name) \ + WASM_DEFINE_VEC(name) \ + void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \ + own wasm_##name##_t const data[]) \ + { \ + if (!out) { \ + return; \ + } \ + \ + memset(out, 0, sizeof(wasm_##name##_vec_t)); \ + \ + if (!size) { \ + return; \ + } \ + \ + if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t))) { \ + LOG_DEBUG("bh_vector_init failed"); \ + goto failed; \ + } \ + \ + if (data) { \ + uint32 size_in_bytes = 0; \ + size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t)); \ + bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \ + out->num_elems = size; \ + } \ + \ + RETURN_VOID(out, wasm_##name##_vec_delete) \ + } \ + void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \ + const wasm_##name##_vec_t *src) \ + { \ + wasm_##name##_vec_new(out, src->size, src->data); \ + } \ + void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \ + { \ + if (v) { \ + bh_vector_destroy((Vector *)v); \ + } \ } /* vectors that own their elements */ -#define WASM_DEFINE_VEC_OWN(name, elem_destroy_func) \ - WASM_DEFINE_VEC(name) \ - void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \ - own wasm_##name##_t *const data[]) \ - { \ - if (!out) { \ - return; \ - } \ - \ - memset(out, 0, sizeof(wasm_##name##_vec_t)); \ - \ - if (!size) { \ - return; \ - } \ - \ - if (!bh_vector_init((Vector *)out, size, \ - sizeof(wasm_##name##_t *))) { \ - LOG_DEBUG("bh_vector_init failed"); \ - goto failed; \ - } \ - \ - if (data) { \ - uint32 size_in_bytes = 0; \ - size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t *)); \ - bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \ - out->num_elems = size; \ - } \ - \ - RETURN_VOID(out, wasm_##name##_vec_delete) \ - } \ - void wasm_##name##_vec_copy(own wasm_##name##_vec_t *out, \ - const wasm_##name##_vec_t *src) \ - { \ - size_t i = 0; \ - memset(out, 0, sizeof(Vector)); \ - \ - if (!src || !src->size) { \ - return; \ - } \ - \ - if (!bh_vector_init((Vector *)out, src->size, \ - sizeof(wasm_##name##_t *))) { \ - LOG_DEBUG("bh_vector_init failed"); \ - goto failed; \ - } \ - \ - for (i = 0; i != src->num_elems; ++i) { \ - if (!(out->data[i] = wasm_##name##_copy(src->data[i]))) { \ - LOG_DEBUG("wasm_%s_copy failed", #name); \ - goto failed; \ - } \ - } \ - out->num_elems = src->num_elems; \ - \ - RETURN_VOID(out, wasm_##name##_vec_delete) \ - } \ - void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \ - { \ - size_t i = 0; \ - if (!v) { \ - return; \ - } \ - for (i = 0; i != v->num_elems; ++i) { \ - elem_destroy_func(*(v->data + i)); \ - } \ - bh_vector_destroy((Vector *)v); \ +#define WASM_DEFINE_VEC_OWN(name, elem_destroy_func) \ + WASM_DEFINE_VEC(name) \ + void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \ + own wasm_##name##_t *const data[]) \ + { \ + if (!out) { \ + return; \ + } \ + \ + memset(out, 0, sizeof(wasm_##name##_vec_t)); \ + \ + if (!size) { \ + return; \ + } \ + \ + if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t *))) { \ + LOG_DEBUG("bh_vector_init failed"); \ + goto failed; \ + } \ + \ + if (data) { \ + uint32 size_in_bytes = 0; \ + size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t *)); \ + bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \ + out->num_elems = size; \ + } \ + \ + RETURN_VOID(out, wasm_##name##_vec_delete) \ + } \ + void wasm_##name##_vec_copy(own wasm_##name##_vec_t *out, \ + const wasm_##name##_vec_t *src) \ + { \ + size_t i = 0; \ + memset(out, 0, sizeof(Vector)); \ + \ + if (!src || !src->size) { \ + return; \ + } \ + \ + if (!bh_vector_init((Vector *)out, src->size, \ + sizeof(wasm_##name##_t *))) { \ + LOG_DEBUG("bh_vector_init failed"); \ + goto failed; \ + } \ + \ + for (i = 0; i != src->num_elems; ++i) { \ + if (!(out->data[i] = wasm_##name##_copy(src->data[i]))) { \ + LOG_DEBUG("wasm_%s_copy failed", #name); \ + goto failed; \ + } \ + } \ + out->num_elems = src->num_elems; \ + \ + RETURN_VOID(out, wasm_##name##_vec_delete) \ + } \ + void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \ + { \ + size_t i = 0; \ + if (!v) { \ + return; \ + } \ + for (i = 0; i != v->num_elems; ++i) { \ + elem_destroy_func(*(v->data + i)); \ + } \ + bh_vector_destroy((Vector *)v); \ } WASM_DEFINE_VEC_PLAIN(byte) @@ -241,13 +240,9 @@ void aot_compile_wasm_file_destroy(); uint8 * -aot_compile_wasm_file(const uint8 *wasm_file_buf, - uint32 wasm_file_size, - uint32 opt_level, - uint32 size_level, - char *error_buf, - uint32 error_buf_size, - uint32 *p_aot_file_size); +aot_compile_wasm_file(const uint8 *wasm_file_buf, uint32 wasm_file_size, + uint32 opt_level, uint32 size_level, char *error_buf, + uint32 error_buf_size, uint32 *p_aot_file_size); #endif /* Runtime Environment */ @@ -300,11 +295,11 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts) } init_args.mem_alloc_option.allocator.malloc_func = - opts->allocator.malloc_func; + opts->allocator.malloc_func; init_args.mem_alloc_option.allocator.free_func = - opts->allocator.free_func; + opts->allocator.free_func; init_args.mem_alloc_option.allocator.realloc_func = - opts->allocator.realloc_func; + opts->allocator.realloc_func; } else { init_args.mem_alloc_option.pool.heap_buf = NULL; @@ -347,7 +342,7 @@ wasm_engine_new() { if (!singleton_engine) { singleton_engine = - wasm_engine_new_internal(Alloc_With_System_Allocator, NULL); + wasm_engine_new_internal(Alloc_With_System_Allocator, NULL); } return singleton_engine; } @@ -463,8 +458,8 @@ static inline wasm_valkind_t val_type_rt_2_valkind(uint8 val_type_rt) { switch (val_type_rt) { -#define WAMR_VAL_TYPE_2_WASM_VAL_KIND(name) \ - case VALUE_TYPE_##name: \ +#define WAMR_VAL_TYPE_2_WASM_VAL_KIND(name) \ + case VALUE_TYPE_##name: \ return WASM_##name; WAMR_VAL_TYPE_2_WASM_VAL_KIND(I32) @@ -550,12 +545,13 @@ wasm_functype_new_internal(WASMType *type_rt) } } - /* WASMType->types[type_rt->param_count : type_rt->result_count) -> type->results */ + /* WASMType->types[type_rt->param_count : type_rt->result_count) -> + * type->results */ INIT_VEC(type->results, wasm_valtype_vec_new_uninitialized, type_rt->result_count); for (i = 0; i < type_rt->result_count; ++i) { if (!(result_type = wasm_valtype_new_internal( - *(type_rt->types + type_rt->param_count + i)))) { + *(type_rt->types + type_rt->param_count + i)))) { goto failed; } @@ -710,7 +706,7 @@ wasm_globaltype_new_internal(uint8 val_type_rt, bool is_mutable) } if (!(globaltype = wasm_globaltype_new( - val_type, is_mutable ? WASM_VAR : WASM_CONST))) { + val_type, is_mutable ? WASM_VAR : WASM_CONST))) { wasm_valtype_delete(val_type); } @@ -774,8 +770,7 @@ wasm_globaltype_mutability(const wasm_globaltype_t *global_type) } static wasm_tabletype_t * -wasm_tabletype_new_internal(uint8 val_type_rt, - uint32 init_size, +wasm_tabletype_new_internal(uint8 val_type_rt, uint32 init_size, uint32 max_size) { wasm_tabletype_t *table_type; @@ -935,46 +930,45 @@ wasm_externtype_kind(const wasm_externtype_t *extern_type) return extern_type->extern_kind; } -#define BASIC_FOUR_TYPE_LIST(V) \ - V(functype) \ - V(globaltype) \ - V(memorytype) \ +#define BASIC_FOUR_TYPE_LIST(V) \ + V(functype) \ + V(globaltype) \ + V(memorytype) \ V(tabletype) -#define WASM_EXTERNTYPE_AS_OTHERTYPE(name) \ - wasm_##name##_t *wasm_externtype_as_##name( \ - wasm_externtype_t *extern_type) \ - { \ - return (wasm_##name##_t *)extern_type; \ +#define WASM_EXTERNTYPE_AS_OTHERTYPE(name) \ + wasm_##name##_t *wasm_externtype_as_##name(wasm_externtype_t *extern_type) \ + { \ + return (wasm_##name##_t *)extern_type; \ } BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE) #undef WASM_EXTERNTYPE_AS_OTHERTYPE -#define WASM_OTHERTYPE_AS_EXTERNTYPE(name) \ - wasm_externtype_t *wasm_##name##_as_externtype(wasm_##name##_t *other) \ - { \ - return (wasm_externtype_t *)other; \ +#define WASM_OTHERTYPE_AS_EXTERNTYPE(name) \ + wasm_externtype_t *wasm_##name##_as_externtype(wasm_##name##_t *other) \ + { \ + return (wasm_externtype_t *)other; \ } BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE) #undef WASM_OTHERTYPE_AS_EXTERNTYPE -#define WASM_EXTERNTYPE_AS_OTHERTYPE_CONST(name) \ - const wasm_##name##_t *wasm_externtype_as_##name##_const( \ - const wasm_externtype_t *extern_type) \ - { \ - return (const wasm_##name##_t *)extern_type; \ +#define WASM_EXTERNTYPE_AS_OTHERTYPE_CONST(name) \ + const wasm_##name##_t *wasm_externtype_as_##name##_const( \ + const wasm_externtype_t *extern_type) \ + { \ + return (const wasm_##name##_t *)extern_type; \ } BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE_CONST) #undef WASM_EXTERNTYPE_AS_OTHERTYPE_CONST -#define WASM_OTHERTYPE_AS_EXTERNTYPE_CONST(name) \ - const wasm_externtype_t *wasm_##name##_as_externtype_const( \ - const wasm_##name##_t *other) \ - { \ - return (const wasm_externtype_t *)other; \ +#define WASM_OTHERTYPE_AS_EXTERNTYPE_CONST(name) \ + const wasm_externtype_t *wasm_##name##_as_externtype_const( \ + const wasm_##name##_t *other) \ + { \ + return (const wasm_externtype_t *)other; \ } BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE_CONST) @@ -990,12 +984,12 @@ wasm_externtype_copy(const wasm_externtype_t *src) } switch (src->extern_kind) { -#define COPY_EXTERNTYPE(NAME, name) \ - case WASM_EXTERN_##NAME: \ - { \ - extern_type = wasm_##name##_as_externtype( \ - wasm_##name##_copy(wasm_externtype_as_##name##_const(src))); \ - break; \ +#define COPY_EXTERNTYPE(NAME, name) \ + case WASM_EXTERN_##NAME: \ + { \ + extern_type = wasm_##name##_as_externtype( \ + wasm_##name##_copy(wasm_externtype_as_##name##_const(src))); \ + break; \ } COPY_EXTERNTYPE(FUNC, functype) COPY_EXTERNTYPE(GLOBAL, globaltype) @@ -1031,8 +1025,7 @@ wasm_externtype_delete(wasm_externtype_t *extern_type) wasm_tabletype_delete(wasm_externtype_as_tabletype(extern_type)); break; default: - LOG_WARNING("%s meets unsupported type", __FUNCTION__, - extern_type); + LOG_WARNING("%s meets unsupported type", __FUNCTION__, extern_type); break; } } @@ -1050,7 +1043,7 @@ wasm_importtype_new(own wasm_byte_vec_t *module_name, /* take ownership */ if (!(import_type->module_name = - malloc_internal(sizeof(wasm_byte_vec_t)))) { + malloc_internal(sizeof(wasm_byte_vec_t)))) { goto failed; } bh_memcpy_s(import_type->module_name, sizeof(wasm_byte_vec_t), module_name, @@ -1109,7 +1102,7 @@ wasm_importtype_copy(const wasm_importtype_t *src) } if (!(import_type = - wasm_importtype_new(&module_name, &name, extern_type))) { + wasm_importtype_new(&module_name, &name, extern_type))) { goto failed; } @@ -1298,10 +1291,8 @@ rt_val_to_wasm_val(const uint8 *data, uint8 val_type_rt, wasm_val_t *out) } bool -wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, - uint8 val_type_rt, - const wasm_val_t *v, - uint8 *data) +wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, uint8 val_type_rt, + const wasm_val_t *v, uint8 *data) { bool ret = true; switch (val_type_rt) { @@ -1325,7 +1316,7 @@ wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, case VALUE_TYPE_EXTERNREF: bh_assert(WASM_ANYREF == v->kind); ret = - wasm_externref_obj2ref(inst_comm_rt, v->of.ref, (uint32 *)data); + wasm_externref_obj2ref(inst_comm_rt, v->of.ref, (uint32 *)data); break; #endif default: @@ -1338,10 +1329,8 @@ wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, } wasm_ref_t * -wasm_ref_new_internal(wasm_store_t *store, - enum wasm_reference_kind kind, - uint32 ref_idx_rt, - WASMModuleInstanceCommon *inst_comm_rt) +wasm_ref_new_internal(wasm_store_t *store, enum wasm_reference_kind kind, + uint32 ref_idx_rt, WASMModuleInstanceCommon *inst_comm_rt) { wasm_ref_t *ref; @@ -1386,11 +1375,11 @@ wasm_ref_copy(const wasm_ref_t *src) src->inst_comm_rt); } -#define DELETE_HOST_INFO(obj) \ - if (obj->host_info.info) { \ - if (obj->host_info.finalizer) { \ - obj->host_info.finalizer(obj->host_info.info); \ - } \ +#define DELETE_HOST_INFO(obj) \ + if (obj->host_info.info) { \ + if (obj->host_info.finalizer) { \ + obj->host_info.finalizer(obj->host_info.info); \ + } \ } void @@ -1413,82 +1402,80 @@ wasm_ref_delete(own wasm_ref_t *ref) wasm_runtime_free(ref); } -#define WASM_DEFINE_REF_BASE(name) \ - bool wasm_##name##_same(const wasm_##name##_t *o1, \ - const wasm_##name##_t *o2) \ - { \ - return (!o1 && !o2) ? true \ - : (!o1 || !o2) ? false \ - : (o1->kind != o2->kind) \ - ? false \ - : o1->name##_idx_rt == o2->name##_idx_rt; \ - } \ - \ - void *wasm_##name##_get_host_info(const wasm_##name##_t *obj) \ - { \ - return obj ? obj->host_info.info : NULL; \ - } \ - \ - void wasm_##name##_set_host_info(wasm_##name##_t *obj, void *host_info) \ - { \ - if (obj) { \ - obj->host_info.info = host_info; \ - obj->host_info.finalizer = NULL; \ - } \ - } \ - \ - void wasm_##name##_set_host_info_with_finalizer( \ - wasm_##name##_t *obj, void *host_info, void (*finalizer)(void *)) \ - { \ - if (obj) { \ - obj->host_info.info = host_info; \ - obj->host_info.finalizer = finalizer; \ - } \ - } - -#define WASM_DEFINE_REF(name) \ - WASM_DEFINE_REF_BASE(name) \ - \ - wasm_ref_t *wasm_##name##_as_ref(wasm_##name##_t *name) \ - { \ - if (!name) { \ - return NULL; \ - } \ - \ - return wasm_ref_new_internal(name->store, WASM_REF_##name, \ - name->name##_idx_rt, \ - name->inst_comm_rt); \ - } \ - \ - const wasm_ref_t *wasm_##name##_as_ref_const(const wasm_##name##_t *name) \ - { \ - if (!name) { \ - return NULL; \ - } \ - \ - return wasm_ref_new_internal(name->store, WASM_REF_##name, \ - name->name##_idx_rt, \ - name->inst_comm_rt); \ - } \ - \ - wasm_##name##_t *wasm_ref_as_##name(wasm_ref_t *ref) \ - { \ - if (!ref || WASM_REF_##name != ref->kind) { \ - return NULL; \ - } \ - \ - return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \ - ref->inst_comm_rt); \ - } \ - \ - const wasm_##name##_t *wasm_ref_as_##name##_const(const wasm_ref_t *ref) \ - { \ - if (!ref || WASM_REF_##name != ref->kind) { \ - return NULL; \ - } \ - \ - return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \ - ref->inst_comm_rt); \ +#define WASM_DEFINE_REF_BASE(name) \ + bool wasm_##name##_same(const wasm_##name##_t *o1, \ + const wasm_##name##_t *o2) \ + { \ + return (!o1 && !o2) ? true \ + : (!o1 || !o2) ? false \ + : (o1->kind != o2->kind) \ + ? false \ + : o1->name##_idx_rt == o2->name##_idx_rt; \ + } \ + \ + void *wasm_##name##_get_host_info(const wasm_##name##_t *obj) \ + { \ + return obj ? obj->host_info.info : NULL; \ + } \ + \ + void wasm_##name##_set_host_info(wasm_##name##_t *obj, void *host_info) \ + { \ + if (obj) { \ + obj->host_info.info = host_info; \ + obj->host_info.finalizer = NULL; \ + } \ + } \ + \ + void wasm_##name##_set_host_info_with_finalizer( \ + wasm_##name##_t *obj, void *host_info, void (*finalizer)(void *)) \ + { \ + if (obj) { \ + obj->host_info.info = host_info; \ + obj->host_info.finalizer = finalizer; \ + } \ + } + +#define WASM_DEFINE_REF(name) \ + WASM_DEFINE_REF_BASE(name) \ + \ + wasm_ref_t *wasm_##name##_as_ref(wasm_##name##_t *name) \ + { \ + if (!name) { \ + return NULL; \ + } \ + \ + return wasm_ref_new_internal(name->store, WASM_REF_##name, \ + name->name##_idx_rt, name->inst_comm_rt); \ + } \ + \ + const wasm_ref_t *wasm_##name##_as_ref_const(const wasm_##name##_t *name) \ + { \ + if (!name) { \ + return NULL; \ + } \ + \ + return wasm_ref_new_internal(name->store, WASM_REF_##name, \ + name->name##_idx_rt, name->inst_comm_rt); \ + } \ + \ + wasm_##name##_t *wasm_ref_as_##name(wasm_ref_t *ref) \ + { \ + if (!ref || WASM_REF_##name != ref->kind) { \ + return NULL; \ + } \ + \ + return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \ + ref->inst_comm_rt); \ + } \ + \ + const wasm_##name##_t *wasm_ref_as_##name##_const(const wasm_ref_t *ref) \ + { \ + if (!ref || WASM_REF_##name != ref->kind) { \ + return NULL; \ + } \ + \ + return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \ + ref->inst_comm_rt); \ } WASM_DEFINE_REF_BASE(ref) @@ -1499,10 +1486,8 @@ WASM_DEFINE_REF(memory) WASM_DEFINE_REF(table) static wasm_frame_t * -wasm_frame_new(wasm_instance_t *instance, - size_t module_offset, - uint32 func_index, - size_t func_offset) +wasm_frame_new(wasm_instance_t *instance, size_t module_offset, + uint32 func_index, size_t func_offset) { wasm_frame_t *frame; @@ -1578,7 +1563,7 @@ wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt, #if WASM_ENABLE_INTERP != 0 if (inst_comm_rt->module_type == Wasm_Module_Bytecode) { if (!(error_info = - wasm_get_exception((WASMModuleInstance *)inst_comm_rt))) { + wasm_get_exception((WASMModuleInstance *)inst_comm_rt))) { return NULL; } } @@ -1587,7 +1572,7 @@ wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt, #if WASM_ENABLE_AOT != 0 if (inst_comm_rt->module_type == Wasm_Module_AoT) { if (!(error_info = - aot_get_exception((AOTModuleInstance *)inst_comm_rt))) { + aot_get_exception((AOTModuleInstance *)inst_comm_rt))) { return NULL; } } @@ -1737,8 +1722,8 @@ wasm_trap_trace(const wasm_trap_t *trap, own wasm_frame_vec_t *out) frame = ((wasm_frame_t *)trap->frames->data) + i; if (!(out->data[i] = - wasm_frame_new(frame->instance, frame->module_offset, - frame->func_index, frame->func_offset))) { + wasm_frame_new(frame->instance, frame->module_offset, + frame->func_index, frame->func_offset))) { goto failed; } out->num_elems++; @@ -1756,8 +1741,7 @@ wasm_trap_trace(const wasm_trap_t *trap, own wasm_frame_vec_t *out) } wasm_foreign_t * -wasm_foreign_new_internal(wasm_store_t *store, - uint32 foreign_idx_rt, +wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt, WASMModuleInstanceCommon *inst_comm_rt) { wasm_foreign_t *foreign = NULL; @@ -1857,7 +1841,8 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) pkg_type = get_package_type((uint8 *)binary->data, (uint32)binary->size); - /* whether the combination of compilation flags are compatable with the package type */ + /* whether the combination of compilation flags are compatable with the + * package type */ { bool result = false; #if WASM_ENABLE_INTERP != 0 @@ -1883,16 +1868,16 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0 if (Wasm_Module_Bytecode == pkg_type) { if (!(aot_file_buf = aot_compile_wasm_file( - (uint8 *)module_ex->binary->data, - (uint32)module_ex->binary->size, 3, 3, error_buf, - (uint32)sizeof(error_buf), &aot_file_size))) { + (uint8 *)module_ex->binary->data, + (uint32)module_ex->binary->size, 3, 3, error_buf, + (uint32)sizeof(error_buf), &aot_file_size))) { LOG_ERROR(error_buf); goto failed; } if (!(module_ex->module_comm_rt = - wasm_runtime_load(aot_file_buf, aot_file_size, error_buf, - (uint32)sizeof(error_buf)))) { + wasm_runtime_load(aot_file_buf, aot_file_size, error_buf, + (uint32)sizeof(error_buf)))) { LOG_ERROR(error_buf); goto failed; } @@ -1901,8 +1886,8 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) #endif { module_ex->module_comm_rt = wasm_runtime_load( - (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size, - error_buf, (uint32)sizeof(error_buf)); + (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size, + error_buf, (uint32)sizeof(error_buf)); if (!(module_ex->module_comm_rt)) { LOG_ERROR(error_buf); goto failed; @@ -1935,8 +1920,8 @@ wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary) return false; } - if ((module_rt = wasm_runtime_load( - (uint8 *)binary->data, (uint32)binary->size, error_buf, 128))) { + if ((module_rt = wasm_runtime_load((uint8 *)binary->data, + (uint32)binary->size, error_buf, 128))) { wasm_runtime_unload(module_rt); return true; } @@ -1973,12 +1958,10 @@ wasm_module_delete(wasm_module_t *module) } void -wasm_module_imports(const wasm_module_t *module, - own wasm_importtype_vec_t *out) +wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out) { uint32 i, import_func_count = 0, import_memory_count = 0, - import_global_count = 0, import_table_count = 0, - import_count = 0; + import_global_count = 0, import_table_count = 0, import_count = 0; wasm_byte_vec_t module_name = { 0 }, name = { 0 }; wasm_externtype_t *extern_type = NULL; wasm_importtype_t *import_type = NULL; @@ -2027,7 +2010,7 @@ wasm_module_imports(const wasm_module_t *module, #if WASM_ENABLE_INTERP != 0 if ((*module)->module_type == Wasm_Module_Bytecode) { WASMImport *import = - MODULE_INTERP(module)->import_functions + i; + MODULE_INTERP(module)->import_functions + i; module_name_rt = import->u.names.module_name; field_name_rt = import->u.names.field_name; type_rt = import->u.function.func_type; @@ -2081,8 +2064,8 @@ wasm_module_imports(const wasm_module_t *module, #if WASM_ENABLE_AOT != 0 if ((*module)->module_type == Wasm_Module_AoT) { - AOTImportGlobal *import = - MODULE_AOT(module)->import_globals + (i - import_func_count); + AOTImportGlobal *import = MODULE_AOT(module)->import_globals + + (i - import_func_count); module_name_rt = import->module_name; field_name_rt = import->global_name; val_type_rt = import->type; @@ -2102,15 +2085,15 @@ wasm_module_imports(const wasm_module_t *module, extern_type = wasm_globaltype_as_externtype(type); } else if (i < import_func_count + import_global_count - + import_memory_count) { + + import_memory_count) { wasm_memorytype_t *type = NULL; uint32 min_page = 0, max_page = 0; #if WASM_ENABLE_INTERP != 0 if ((*module)->module_type == Wasm_Module_Bytecode) { WASMImport *import = - MODULE_INTERP(module)->import_memories - + (i - import_func_count - import_global_count); + MODULE_INTERP(module)->import_memories + + (i - import_func_count - import_global_count); module_name_rt = import->u.names.module_name; field_name_rt = import->u.names.field_name; min_page = import->u.memory.init_page_count; @@ -2121,8 +2104,8 @@ wasm_module_imports(const wasm_module_t *module, #if WASM_ENABLE_AOT != 0 if ((*module)->module_type == Wasm_Module_AoT) { AOTImportMemory *import = - MODULE_AOT(module)->import_memories - + (i - import_func_count - import_global_count); + MODULE_AOT(module)->import_memories + + (i - import_func_count - import_global_count); module_name_rt = import->module_name; field_name_rt = import->memory_name; min_page = import->mem_init_page_count; @@ -2158,9 +2141,9 @@ wasm_module_imports(const wasm_module_t *module, #if WASM_ENABLE_INTERP != 0 if ((*module)->module_type == Wasm_Module_Bytecode) { WASMImport *import = - MODULE_INTERP(module)->import_tables - + (i - import_func_count - import_global_count - - import_memory_count); + MODULE_INTERP(module)->import_tables + + (i - import_func_count - import_global_count + - import_memory_count); module_name_rt = import->u.names.module_name; field_name_rt = import->u.names.field_name; elem_type_rt = import->u.table.elem_type; @@ -2172,9 +2155,9 @@ wasm_module_imports(const wasm_module_t *module, #if WASM_ENABLE_AOT != 0 if ((*module)->module_type == Wasm_Module_AoT) { AOTImportTable *import = - MODULE_AOT(module)->import_tables - + (i - import_func_count - import_global_count - - import_memory_count); + MODULE_AOT(module)->import_tables + + (i - import_func_count - import_global_count + - import_memory_count); module_name_rt = import->module_name; field_name_rt = import->table_name; elem_type_rt = VALUE_TYPE_FUNCREF; @@ -2200,7 +2183,7 @@ wasm_module_imports(const wasm_module_t *module, } if (!(import_type = - wasm_importtype_new(&module_name, &name, extern_type))) { + wasm_importtype_new(&module_name, &name, extern_type))) { goto failed; } @@ -2277,7 +2260,8 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out) goto failed; } - /* WASMExport -> (WASMType, (uint8, bool)) -> (wasm_functype_t, wasm_globaltype_t) -> wasm_externtype_t*/ + /* WASMExport -> (WASMType, (uint8, bool)) -> (wasm_functype_t, + * wasm_globaltype_t) -> wasm_externtype_t*/ switch (export->kind) { case EXPORT_KIND_FUNC: { @@ -2303,7 +2287,7 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out) bool mutability_rt = 0; if (!wasm_runtime_get_export_global_type( - *module, export, &val_type_rt, &mutability_rt)) { + *module, export, &val_type_rt, &mutability_rt)) { goto failed; } @@ -2321,12 +2305,12 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out) uint32 min_page = 0, max_page = 0; if (!wasm_runtime_get_export_memory_type( - *module, export, &min_page, &max_page)) { + *module, export, &min_page, &max_page)) { goto failed; } if (!(type = - wasm_memorytype_new_internal(min_page, max_page))) { + wasm_memorytype_new_internal(min_page, max_page))) { goto failed; } @@ -2340,12 +2324,12 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out) uint32 min_size = 0, max_size = 0; if (!wasm_runtime_get_export_table_type( - *module, export, &elem_type_rt, &min_size, &max_size)) { + *module, export, &elem_type_rt, &min_size, &max_size)) { goto failed; } - if (!(type = wasm_tabletype_new_internal( - elem_type_rt, min_size, max_size))) { + if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size, + max_size))) { goto failed; } @@ -2380,8 +2364,7 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out) } static wasm_func_t * -wasm_func_new_basic(wasm_store_t *store, - const wasm_functype_t *type, +wasm_func_new_basic(wasm_store_t *store, const wasm_functype_t *type, wasm_func_callback_t func_callback) { wasm_func_t *func = NULL; @@ -2404,10 +2387,8 @@ wasm_func_new_basic(wasm_store_t *store, } static wasm_func_t * -wasm_func_new_with_env_basic(wasm_store_t *store, - const wasm_functype_t *type, - wasm_func_callback_with_env_t callback, - void *env, +wasm_func_new_with_env_basic(wasm_store_t *store, const wasm_functype_t *type, + wasm_func_callback_with_env_t callback, void *env, void (*finalizer)(void *)) { wasm_func_t *func = NULL; @@ -2432,8 +2413,7 @@ wasm_func_new_with_env_basic(wasm_store_t *store, } wasm_func_t * -wasm_func_new(wasm_store_t *store, - const wasm_functype_t *type, +wasm_func_new(wasm_store_t *store, const wasm_functype_t *type, wasm_func_callback_t callback) { bh_assert(singleton_engine); @@ -2441,10 +2421,8 @@ wasm_func_new(wasm_store_t *store, } wasm_func_t * -wasm_func_new_with_env(wasm_store_t *store, - const wasm_functype_t *type, - wasm_func_callback_with_env_t callback, - void *env, +wasm_func_new_with_env(wasm_store_t *store, const wasm_functype_t *type, + wasm_func_callback_with_env_t callback, void *env, void (*finalizer)(void *)) { bh_assert(singleton_engine); @@ -2452,8 +2430,7 @@ wasm_func_new_with_env(wasm_store_t *store, } wasm_func_t * -wasm_func_new_internal(wasm_store_t *store, - uint16 func_idx_rt, +wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt, WASMModuleInstanceCommon *inst_comm_rt) { wasm_func_t *func = NULL; @@ -2477,26 +2454,27 @@ wasm_func_new_internal(wasm_store_t *store, bh_assert(func_idx_rt < ((WASMModuleInstance *)inst_comm_rt)->function_count); WASMFunctionInstance *func_interp = - ((WASMModuleInstance *)inst_comm_rt)->functions + func_idx_rt; + ((WASMModuleInstance *)inst_comm_rt)->functions + func_idx_rt; type_rt = func_interp->is_import_func - ? func_interp->u.func_import->func_type - : func_interp->u.func->func_type; + ? func_interp->u.func_import->func_type + : func_interp->u.func->func_type; } #endif #if WASM_ENABLE_AOT != 0 if (inst_comm_rt->module_type == Wasm_Module_AoT) { - /* use same index to trace the function type in AOTFuncType **func_types */ + /* use same index to trace the function type in AOTFuncType **func_types + */ AOTModule *module_aot = - ((AOTModuleInstance *)inst_comm_rt)->aot_module.ptr; + ((AOTModuleInstance *)inst_comm_rt)->aot_module.ptr; if (func_idx_rt < module_aot->import_func_count) { type_rt = (module_aot->import_funcs + func_idx_rt)->func_type; } else { type_rt = - module_aot - ->func_types[module_aot->func_type_indexes - [func_idx_rt - module_aot->import_func_count]]; + module_aot->func_types[module_aot->func_type_indexes + [func_idx_rt + - module_aot->import_func_count]]; } } #endif @@ -2563,10 +2541,10 @@ wasm_func_copy(const wasm_func_t *func) } if (!(cloned = func->with_env ? wasm_func_new_with_env_basic( - func->store, func->type, func->u.cb_env.cb, - func->u.cb_env.env, func->u.cb_env.finalizer) - : wasm_func_new_basic( - func->store, func->type, func->u.cb))) { + func->store, func->type, func->u.cb_env.cb, + func->u.cb_env.env, func->u.cb_env.finalizer) + : wasm_func_new_basic(func->store, func->type, + func->u.cb))) { goto failed; } @@ -2586,10 +2564,8 @@ wasm_func_type(const wasm_func_t *func) } static uint32 -params_to_argv(WASMModuleInstanceCommon *inst_comm_rt, - const wasm_val_t *params, - const wasm_valtype_vec_t *param_defs, - size_t param_arity, +params_to_argv(WASMModuleInstanceCommon *inst_comm_rt, const wasm_val_t *params, + const wasm_valtype_vec_t *param_defs, size_t param_arity, uint32 *out) { size_t i = 0; @@ -2630,8 +2606,7 @@ params_to_argv(WASMModuleInstanceCommon *inst_comm_rt, break; #if WASM_ENABLE_REF_TYPES != 0 case WASM_ANYREF: - if (!wasm_externref_obj2ref(inst_comm_rt, param->of.ref, - out)) { + if (!wasm_externref_obj2ref(inst_comm_rt, param->of.ref, out)) { goto failed; } @@ -2653,10 +2628,8 @@ params_to_argv(WASMModuleInstanceCommon *inst_comm_rt, } static uint32 -argv_to_results(const uint32 *results, - const wasm_valtype_vec_t *result_defs, - size_t result_arity, - wasm_val_t *out) +argv_to_results(const uint32 *results, const wasm_valtype_vec_t *result_defs, + size_t result_arity, wasm_val_t *out) { size_t i = 0; uint32 argc = 0; @@ -2738,8 +2711,7 @@ argv_to_results(const uint32 *results, } wasm_trap_t * -wasm_func_call(const wasm_func_t *func, - const wasm_val_vec_t *params, +wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params, wasm_val_vec_t *results) { /* parameters count as if all are uint32 */ @@ -2764,7 +2736,7 @@ wasm_func_call(const wasm_func_t *func, if (func->inst_comm_rt->module_type == Wasm_Module_AoT) { if (!(func_comm_rt = func->func_comm_rt)) { AOTModuleInstance *inst_aot = - (AOTModuleInstance *)func->inst_comm_rt; + (AOTModuleInstance *)func->inst_comm_rt; AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr; uint32 export_i = 0, export_func_j = 0; @@ -2773,8 +2745,8 @@ wasm_func_call(const wasm_func_t *func, if (export->kind == EXPORT_KIND_FUNC) { if (export->index == func->func_idx_rt) { func_comm_rt = - (AOTFunctionInstance *)inst_aot->export_funcs.ptr - + export_func_j; + (AOTFunctionInstance *)inst_aot->export_funcs.ptr + + export_func_j; ((wasm_func_t *)func)->func_comm_rt = func_comm_rt; break; } @@ -2872,8 +2844,7 @@ wasm_func_result_arity(const wasm_func_t *func) } wasm_global_t * -wasm_global_new(wasm_store_t *store, - const wasm_globaltype_t *global_type, +wasm_global_new(wasm_store_t *store, const wasm_globaltype_t *global_type, const wasm_val_t *init) { wasm_global_t *global = NULL; @@ -2971,18 +2942,17 @@ wasm_global_delete(wasm_global_t *global) #if WASM_ENABLE_INTERP != 0 static bool -interp_global_set(const WASMModuleInstance *inst_interp, - uint16 global_idx_rt, +interp_global_set(const WASMModuleInstance *inst_interp, uint16 global_idx_rt, const wasm_val_t *v) { const WASMGlobalInstance *global_interp = - inst_interp->globals + global_idx_rt; + inst_interp->globals + global_idx_rt; uint8 val_type_rt = global_interp->type; #if WASM_ENABLE_MULTI_MODULE != 0 uint8 *data = global_interp->import_global_inst - ? global_interp->import_module_inst->global_data - + global_interp->import_global_inst->data_offset - : inst_interp->global_data + global_interp->data_offset; + ? global_interp->import_module_inst->global_data + + global_interp->import_global_inst->data_offset + : inst_interp->global_data + global_interp->data_offset; #else uint8 *data = inst_interp->global_data + global_interp->data_offset; #endif @@ -2992,17 +2962,16 @@ interp_global_set(const WASMModuleInstance *inst_interp, } static bool -interp_global_get(const WASMModuleInstance *inst_interp, - uint16 global_idx_rt, +interp_global_get(const WASMModuleInstance *inst_interp, uint16 global_idx_rt, wasm_val_t *out) { WASMGlobalInstance *global_interp = inst_interp->globals + global_idx_rt; uint8 val_type_rt = global_interp->type; #if WASM_ENABLE_MULTI_MODULE != 0 uint8 *data = global_interp->import_global_inst - ? global_interp->import_module_inst->global_data - + global_interp->import_global_inst->data_offset - : inst_interp->global_data + global_interp->data_offset; + ? global_interp->import_module_inst->global_data + + global_interp->import_global_inst->data_offset + : inst_interp->global_data + global_interp->data_offset; #else uint8 *data = inst_interp->global_data + global_interp->data_offset; #endif @@ -3013,8 +2982,7 @@ interp_global_get(const WASMModuleInstance *inst_interp, #if WASM_ENABLE_AOT != 0 static bool -aot_global_set(const AOTModuleInstance *inst_aot, - uint16 global_idx_rt, +aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt, const wasm_val_t *v) { AOTModule *module_aot = inst_aot->aot_module.ptr; @@ -3028,21 +2996,20 @@ aot_global_set(const AOTModuleInstance *inst_aot, } else { data_offset = - module_aot->globals[global_idx_rt - module_aot->import_global_count] - .data_offset; + module_aot->globals[global_idx_rt - module_aot->import_global_count] + .data_offset; val_type_rt = - module_aot->globals[global_idx_rt - module_aot->import_global_count] - .type; + module_aot->globals[global_idx_rt - module_aot->import_global_count] + .type; } data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset); - return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, - val_type_rt, v, data); + return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, val_type_rt, + v, data); } static bool -aot_global_get(const AOTModuleInstance *inst_aot, - uint16 global_idx_rt, +aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt, wasm_val_t *out) { AOTModule *module_aot = inst_aot->aot_module.ptr; @@ -3056,11 +3023,11 @@ aot_global_get(const AOTModuleInstance *inst_aot, } else { data_offset = - module_aot->globals[global_idx_rt - module_aot->import_global_count] - .data_offset; + module_aot->globals[global_idx_rt - module_aot->import_global_count] + .data_offset; val_type_rt = - module_aot->globals[global_idx_rt - module_aot->import_global_count] - .type; + module_aot->globals[global_idx_rt - module_aot->import_global_count] + .type; } data = (uint8 *)inst_aot->global_data.ptr + data_offset; @@ -3131,8 +3098,7 @@ wasm_global_get(const wasm_global_t *global, wasm_val_t *out) } wasm_global_t * -wasm_global_new_internal(wasm_store_t *store, - uint16 global_idx_rt, +wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt, WASMModuleInstanceCommon *inst_comm_rt) { wasm_global_t *global = NULL; @@ -3157,7 +3123,7 @@ wasm_global_new_internal(wasm_store_t *store, #if WASM_ENABLE_INTERP != 0 if (inst_comm_rt->module_type == Wasm_Module_Bytecode) { WASMGlobalInstance *global_interp = - ((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt; + ((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt; val_type_rt = global_interp->type; is_mutable = global_interp->is_mutable; init = true; @@ -3173,14 +3139,14 @@ wasm_global_new_internal(wasm_store_t *store, if (global_idx_rt < module_aot->import_global_count) { AOTImportGlobal *global_import_aot = - module_aot->import_globals + global_idx_rt; + module_aot->import_globals + global_idx_rt; val_type_rt = global_import_aot->type; is_mutable = global_import_aot->is_mutable; } else { AOTGlobal *global_aot = - module_aot->globals - + (global_idx_rt - module_aot->import_global_count); + module_aot->globals + + (global_idx_rt - module_aot->import_global_count); val_type_rt = global_aot->type; is_mutable = global_aot->is_mutable; } @@ -3259,8 +3225,7 @@ wasm_table_new_basic(wasm_store_t *store, const wasm_tabletype_t *type) } wasm_table_t * -wasm_table_new_internal(wasm_store_t *store, - uint16 table_idx_rt, +wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt, WASMModuleInstanceCommon *inst_comm_rt) { wasm_table_t *table = NULL; @@ -3284,7 +3249,7 @@ wasm_table_new_internal(wasm_store_t *store, #if WASM_ENABLE_INTERP != 0 if (inst_comm_rt->module_type == Wasm_Module_Bytecode) { WASMTableInstance *table_interp = - ((WASMModuleInstance *)inst_comm_rt)->tables[table_idx_rt]; + ((WASMModuleInstance *)inst_comm_rt)->tables[table_idx_rt]; val_type_rt = table_interp->elem_type; init_size = table_interp->cur_size; max_size = table_interp->max_size; @@ -3299,15 +3264,15 @@ wasm_table_new_internal(wasm_store_t *store, if (table_idx_rt < module_aot->import_table_count) { AOTImportTable *table_aot = - module_aot->import_tables + table_idx_rt; + module_aot->import_tables + table_idx_rt; val_type_rt = VALUE_TYPE_FUNCREF; init_size = table_aot->table_init_size; max_size = table_aot->table_max_size; } else { AOTTable *table_aot = - module_aot->tables - + (table_idx_rt - module_aot->import_table_count); + module_aot->tables + + (table_idx_rt - module_aot->import_table_count); val_type_rt = VALUE_TYPE_FUNCREF; init_size = table_aot->table_init_size; max_size = table_aot->table_max_size; @@ -3325,7 +3290,7 @@ wasm_table_new_internal(wasm_store_t *store, } if (!(table->type = - wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) { + wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) { goto failed; } @@ -3337,8 +3302,7 @@ wasm_table_new_internal(wasm_store_t *store, /* will not actually apply this new table into the runtime */ wasm_table_t * -wasm_table_new(wasm_store_t *store, - const wasm_tabletype_t *table_type, +wasm_table_new(wasm_store_t *store, const wasm_tabletype_t *table_type, wasm_ref_t *init) { wasm_table_t *table; @@ -3406,8 +3370,8 @@ wasm_table_get(const wasm_table_t *table, wasm_table_size_t index) #if WASM_ENABLE_INTERP != 0 if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) { WASMTableInstance *table_interp = - ((WASMModuleInstance *)table->inst_comm_rt) - ->tables[table->table_idx_rt]; + ((WASMModuleInstance *)table->inst_comm_rt) + ->tables[table->table_idx_rt]; if (index >= table_interp->cur_size) { return NULL; } @@ -3419,7 +3383,7 @@ wasm_table_get(const wasm_table_t *table, wasm_table_size_t index) if (table->inst_comm_rt->module_type == Wasm_Module_AoT) { AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt; AOTTableInstance *table_aot = - (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt; + (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt; if (index >= table_aot->cur_size) { return NULL; } @@ -3440,8 +3404,7 @@ wasm_table_get(const wasm_table_t *table, wasm_table_size_t index) } bool -wasm_table_set(wasm_table_t *table, - wasm_table_size_t index, +wasm_table_set(wasm_table_t *table, wasm_table_size_t index, own wasm_ref_t *func_ref) { uint32 *p_func_idx_rt = NULL; @@ -3464,8 +3427,8 @@ wasm_table_set(wasm_table_t *table, #if WASM_ENABLE_INTERP != 0 if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) { WASMTableInstance *table_interp = - ((WASMModuleInstance *)table->inst_comm_rt) - ->tables[table->table_idx_rt]; + ((WASMModuleInstance *)table->inst_comm_rt) + ->tables[table->table_idx_rt]; if (index >= table_interp->cur_size) { return false; @@ -3473,7 +3436,7 @@ wasm_table_set(wasm_table_t *table, p_func_idx_rt = ((uint32 *)table_interp->base_addr) + index; function_count = - ((WASMModuleInstance *)table->inst_comm_rt)->function_count; + ((WASMModuleInstance *)table->inst_comm_rt)->function_count; } #endif @@ -3482,7 +3445,7 @@ wasm_table_set(wasm_table_t *table, AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt; AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr; AOTTableInstance *table_aot = - (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt; + (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt; if (index >= table_aot->cur_size) { return false; @@ -3521,8 +3484,8 @@ wasm_table_size(const wasm_table_t *table) #if WASM_ENABLE_INTERP != 0 if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) { WASMTableInstance *table_interp = - ((WASMModuleInstance *)table->inst_comm_rt) - ->tables[table->table_idx_rt]; + ((WASMModuleInstance *)table->inst_comm_rt) + ->tables[table->table_idx_rt]; return table_interp->cur_size; } #endif @@ -3534,13 +3497,13 @@ wasm_table_size(const wasm_table_t *table) if (table->table_idx_rt < module_aot->import_table_count) { AOTImportTable *table_aot = - module_aot->import_tables + table->table_idx_rt; + module_aot->import_tables + table->table_idx_rt; return table_aot->table_init_size; } else { AOTTable *table_aot = - module_aot->tables - + (table->table_idx_rt - module_aot->import_table_count); + module_aot->tables + + (table->table_idx_rt - module_aot->import_table_count); return table_aot->table_init_size; } } @@ -3554,8 +3517,7 @@ wasm_table_size(const wasm_table_t *table) } bool -wasm_table_grow(wasm_table_t *table, - wasm_table_size_t delta, +wasm_table_grow(wasm_table_t *table, wasm_table_size_t delta, own wasm_ref_t *init) { (void)table; @@ -3609,8 +3571,7 @@ wasm_memory_copy(const wasm_memory_t *src) } wasm_memory_t * -wasm_memory_new_internal(wasm_store_t *store, - uint16 memory_idx_rt, +wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt, WASMModuleInstanceCommon *inst_comm_rt) { wasm_memory_t *memory = NULL; @@ -3633,7 +3594,7 @@ wasm_memory_new_internal(wasm_store_t *store, #if WASM_ENABLE_INTERP != 0 if (inst_comm_rt->module_type == Wasm_Module_Bytecode) { WASMMemoryInstance *memory_interp = - ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt]; + ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt]; min_pages = memory_interp->cur_page_count; max_pages = memory_interp->max_page_count; init_flag = true; @@ -3710,9 +3671,9 @@ wasm_memory_data(wasm_memory_t *memory) #if WASM_ENABLE_INTERP != 0 if (module_inst_comm->module_type == Wasm_Module_Bytecode) { WASMModuleInstance *module_inst = - (WASMModuleInstance *)module_inst_comm; + (WASMModuleInstance *)module_inst_comm; WASMMemoryInstance *memory_inst = - module_inst->memories[memory->memory_idx_rt]; + module_inst->memories[memory->memory_idx_rt]; return (byte_t *)memory_inst->memory_data; } #endif @@ -3721,8 +3682,8 @@ wasm_memory_data(wasm_memory_t *memory) if (module_inst_comm->module_type == Wasm_Module_AoT) { AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm; AOTMemoryInstance *memory_inst = - ((AOTMemoryInstance **) - module_inst->memories.ptr)[memory->memory_idx_rt]; + ((AOTMemoryInstance **) + module_inst->memories.ptr)[memory->memory_idx_rt]; return (byte_t *)memory_inst->memory_data.ptr; } #endif @@ -3742,9 +3703,9 @@ wasm_memory_data_size(const wasm_memory_t *memory) #if WASM_ENABLE_INTERP != 0 if (module_inst_comm->module_type == Wasm_Module_Bytecode) { WASMModuleInstance *module_inst = - (WASMModuleInstance *)module_inst_comm; + (WASMModuleInstance *)module_inst_comm; WASMMemoryInstance *memory_inst = - module_inst->memories[memory->memory_idx_rt]; + module_inst->memories[memory->memory_idx_rt]; return memory_inst->cur_page_count * memory_inst->num_bytes_per_page; } #endif @@ -3753,8 +3714,8 @@ wasm_memory_data_size(const wasm_memory_t *memory) if (module_inst_comm->module_type == Wasm_Module_AoT) { AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm; AOTMemoryInstance *memory_inst = - ((AOTMemoryInstance **) - module_inst->memories.ptr)[memory->memory_idx_rt]; + ((AOTMemoryInstance **) + module_inst->memories.ptr)[memory->memory_idx_rt]; return memory_inst->cur_page_count * memory_inst->num_bytes_per_page; } #endif @@ -3774,9 +3735,9 @@ wasm_memory_size(const wasm_memory_t *memory) #if WASM_ENABLE_INTERP != 0 if (module_inst_comm->module_type == Wasm_Module_Bytecode) { WASMModuleInstance *module_inst = - (WASMModuleInstance *)module_inst_comm; + (WASMModuleInstance *)module_inst_comm; WASMMemoryInstance *memory_inst = - module_inst->memories[memory->memory_idx_rt]; + module_inst->memories[memory->memory_idx_rt]; return memory_inst->cur_page_count; } #endif @@ -3785,8 +3746,8 @@ wasm_memory_size(const wasm_memory_t *memory) if (module_inst_comm->module_type == Wasm_Module_AoT) { AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm; AOTMemoryInstance *memory_inst = - ((AOTMemoryInstance **) - module_inst->memories.ptr)[memory->memory_idx_rt]; + ((AOTMemoryInstance **) + module_inst->memories.ptr)[memory->memory_idx_rt]; return memory_inst->cur_page_count; } #endif @@ -3810,10 +3771,8 @@ wasm_memory_grow(wasm_memory_t *memory, wasm_memory_pages_t delta) #if WASM_ENABLE_INTERP != 0 static bool -interp_link_func(const wasm_instance_t *inst, - const WASMModule *module_interp, - uint16 func_idx_rt, - wasm_func_t *import) +interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp, + uint16 func_idx_rt, wasm_func_t *import) { WASMImport *imported_func_interp = NULL; wasm_func_t *cloned = NULL; @@ -3850,8 +3809,7 @@ interp_link_func(const wasm_instance_t *inst, } static bool -interp_link_global(const WASMModule *module_interp, - uint16 global_idx_rt, +interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt, wasm_global_t *import) { WASMImport *imported_global_interp = NULL; @@ -3868,22 +3826,22 @@ interp_link_global(const WASMModule *module_interp, case WASM_I32: bh_assert(VALUE_TYPE_I32 == imported_global_interp->u.global.type); imported_global_interp->u.global.global_data_linked.i32 = - import->init->of.i32; + import->init->of.i32; break; case WASM_I64: bh_assert(VALUE_TYPE_I64 == imported_global_interp->u.global.type); imported_global_interp->u.global.global_data_linked.i64 = - import->init->of.i64; + import->init->of.i64; break; case WASM_F32: bh_assert(VALUE_TYPE_F32 == imported_global_interp->u.global.type); imported_global_interp->u.global.global_data_linked.f32 = - import->init->of.f32; + import->init->of.f32; break; case WASM_F64: bh_assert(VALUE_TYPE_F64 == imported_global_interp->u.global.type); imported_global_interp->u.global.global_data_linked.f64 = - import->init->of.f64; + import->init->of.f64; break; default: return false; @@ -3895,8 +3853,7 @@ interp_link_global(const WASMModule *module_interp, } static uint32 -interp_link(const wasm_instance_t *inst, - const WASMModule *module_interp, +interp_link(const wasm_instance_t *inst, const WASMModule *module_interp, wasm_extern_t *imports[]) { uint32 i = 0; @@ -3970,8 +3927,8 @@ interp_process_export(wasm_store_t *store, { wasm_func_t *func; if (!(func = wasm_func_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_interp))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_interp))) { goto failed; } @@ -3982,8 +3939,8 @@ interp_process_export(wasm_store_t *store, { wasm_global_t *global; if (!(global = wasm_global_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_interp))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_interp))) { goto failed; } @@ -3994,8 +3951,8 @@ interp_process_export(wasm_store_t *store, { wasm_table_t *table; if (!(table = wasm_table_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_interp))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_interp))) { goto failed; } @@ -4006,8 +3963,8 @@ interp_process_export(wasm_store_t *store, { wasm_memory_t *memory; if (!(memory = wasm_memory_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_interp))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_interp))) { goto failed; } @@ -4035,10 +3992,8 @@ interp_process_export(wasm_store_t *store, #if WASM_ENABLE_AOT != 0 static bool -aot_link_func(const wasm_instance_t *inst, - const AOTModule *module_aot, - uint32 import_func_idx_rt, - wasm_func_t *import) +aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot, + uint32 import_func_idx_rt, wasm_func_t *import) { AOTImportFunc *import_aot_func = NULL; wasm_func_t *cloned = NULL; @@ -4071,8 +4026,7 @@ aot_link_func(const wasm_instance_t *inst, } static bool -aot_link_global(const AOTModule *module_aot, - uint16 global_idx_rt, +aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt, wasm_global_t *import) { AOTImportGlobal *import_aot_global = NULL; @@ -4116,8 +4070,7 @@ aot_link_global(const AOTModule *module_aot, } static uint32 -aot_link(const wasm_instance_t *inst, - const AOTModule *module_aot, +aot_link(const wasm_instance_t *inst, const AOTModule *module_aot, wasm_extern_t *imports[]) { uint32 i = 0; @@ -4169,8 +4122,7 @@ aot_link(const wasm_instance_t *inst, } static bool -aot_process_export(wasm_store_t *store, - const AOTModuleInstance *inst_aot, +aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot, wasm_extern_vec_t *externals) { uint32 i; @@ -4190,8 +4142,8 @@ aot_process_export(wasm_store_t *store, { wasm_func_t *func = NULL; if (!(func = wasm_func_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_aot))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_aot))) { goto failed; } @@ -4202,8 +4154,8 @@ aot_process_export(wasm_store_t *store, { wasm_global_t *global = NULL; if (!(global = wasm_global_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_aot))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_aot))) { goto failed; } @@ -4214,8 +4166,8 @@ aot_process_export(wasm_store_t *store, { wasm_table_t *table; if (!(table = wasm_table_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_aot))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_aot))) { goto failed; } @@ -4226,8 +4178,8 @@ aot_process_export(wasm_store_t *store, { wasm_memory_t *memory; if (!(memory = wasm_memory_new_internal( - store, export->index, - (WASMModuleInstanceCommon *)inst_aot))) { + store, export->index, + (WASMModuleInstanceCommon *)inst_aot))) { goto failed; } @@ -4263,21 +4215,17 @@ aot_process_export(wasm_store_t *store, #endif /* WASM_ENABLE_AOT */ wasm_instance_t * -wasm_instance_new(wasm_store_t *store, - const wasm_module_t *module, - const wasm_extern_vec_t *imports, - own wasm_trap_t **traps) +wasm_instance_new(wasm_store_t *store, const wasm_module_t *module, + const wasm_extern_vec_t *imports, own wasm_trap_t **traps) { return wasm_instance_new_with_args(store, module, imports, traps, KILOBYTE(32), KILOBYTE(32)); } wasm_instance_t * -wasm_instance_new_with_args(wasm_store_t *store, - const wasm_module_t *module, +wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module, const wasm_extern_vec_t *imports, - own wasm_trap_t **traps, - const uint32 stack_size, + own wasm_trap_t **traps, const uint32 stack_size, const uint32 heap_size) { char error_buf[128] = { 0 }; @@ -4308,8 +4256,9 @@ wasm_instance_new_with_args(wasm_store_t *store, import_count); if (import_count) { - uint32 actual_link_import_count = interp_link( - instance, MODULE_INTERP(module), (wasm_extern_t **)imports->data); + uint32 actual_link_import_count = + interp_link(instance, MODULE_INTERP(module), + (wasm_extern_t **)imports->data); /* make sure a complete import list */ if ((int32)import_count < 0 || import_count != actual_link_import_count) { @@ -4349,7 +4298,7 @@ wasm_instance_new_with_args(wasm_store_t *store, } instance->inst_comm_rt = wasm_runtime_instantiate( - *module, stack_size, heap_size, error_buf, sizeof(error_buf)); + *module, stack_size, heap_size, error_buf, sizeof(error_buf)); if (!instance->inst_comm_rt) { LOG_ERROR(error_buf); goto failed; @@ -4365,19 +4314,19 @@ wasm_instance_new_with_args(wasm_store_t *store, switch (import->kind) { case WASM_EXTERN_FUNC: wasm_extern_as_func(import)->inst_comm_rt = - instance->inst_comm_rt; + instance->inst_comm_rt; break; case WASM_EXTERN_GLOBAL: wasm_extern_as_global(import)->inst_comm_rt = - instance->inst_comm_rt; + instance->inst_comm_rt; break; case WASM_EXTERN_MEMORY: wasm_extern_as_memory(import)->inst_comm_rt = - instance->inst_comm_rt; + instance->inst_comm_rt; break; case WASM_EXTERN_TABLE: wasm_extern_as_table(import)->inst_comm_rt = - instance->inst_comm_rt; + instance->inst_comm_rt; break; default: goto failed; @@ -4387,15 +4336,15 @@ wasm_instance_new_with_args(wasm_store_t *store, /* build the exports list */ #if WASM_ENABLE_INTERP != 0 if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) { - uint32 export_cnt = - ((WASMModuleInstance *)instance->inst_comm_rt)->module->export_count; + uint32 export_cnt = ((WASMModuleInstance *)instance->inst_comm_rt) + ->module->export_count; INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized, export_cnt); - if (!interp_process_export( - store, (WASMModuleInstance *)instance->inst_comm_rt, - instance->exports)) { + if (!interp_process_export(store, + (WASMModuleInstance *)instance->inst_comm_rt, + instance->exports)) { goto failed; } @@ -4406,10 +4355,10 @@ wasm_instance_new_with_args(wasm_store_t *store, #if WASM_ENABLE_AOT != 0 if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) { uint32 export_cnt = - ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count - + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count - + ((AOTModuleInstance *)instance->inst_comm_rt)->export_tab_count - + ((AOTModuleInstance *)instance->inst_comm_rt)->export_mem_count; + ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count + + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count + + ((AOTModuleInstance *)instance->inst_comm_rt)->export_tab_count + + ((AOTModuleInstance *)instance->inst_comm_rt)->export_mem_count; INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized, export_cnt); @@ -4491,19 +4440,19 @@ wasm_extern_copy(const wasm_extern_t *src) switch (wasm_extern_kind(src)) { case WASM_EXTERN_FUNC: dst = wasm_func_as_extern( - wasm_func_copy(wasm_extern_as_func_const(src))); + wasm_func_copy(wasm_extern_as_func_const(src))); break; case WASM_EXTERN_GLOBAL: dst = wasm_global_as_extern( - wasm_global_copy(wasm_extern_as_global_const(src))); + wasm_global_copy(wasm_extern_as_global_const(src))); break; case WASM_EXTERN_MEMORY: dst = wasm_memory_as_extern( - wasm_memory_copy(wasm_extern_as_memory_const(src))); + wasm_memory_copy(wasm_extern_as_memory_const(src))); break; case WASM_EXTERN_TABLE: dst = wasm_table_as_extern( - wasm_table_copy(wasm_extern_as_table_const(src))); + wasm_table_copy(wasm_extern_as_table_const(src))); break; default: LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__, @@ -4576,16 +4525,16 @@ wasm_extern_type(const wasm_extern_t *external) switch (wasm_extern_kind(external)) { case WASM_EXTERN_FUNC: return wasm_functype_as_externtype( - wasm_func_type(wasm_extern_as_func_const(external))); + wasm_func_type(wasm_extern_as_func_const(external))); case WASM_EXTERN_GLOBAL: return wasm_globaltype_as_externtype( - wasm_global_type(wasm_extern_as_global_const(external))); + wasm_global_type(wasm_extern_as_global_const(external))); case WASM_EXTERN_MEMORY: return wasm_memorytype_as_externtype( - wasm_memory_type(wasm_extern_as_memory_const(external))); + wasm_memory_type(wasm_extern_as_memory_const(external))); case WASM_EXTERN_TABLE: return wasm_tabletype_as_externtype( - wasm_table_type(wasm_extern_as_table_const(external))); + wasm_table_type(wasm_extern_as_table_const(external))); default: LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__, external->kind); @@ -4594,45 +4543,45 @@ wasm_extern_type(const wasm_extern_t *external) return NULL; } -#define BASIC_FOUR_LIST(V) \ - V(func) \ - V(global) \ - V(memory) \ +#define BASIC_FOUR_LIST(V) \ + V(func) \ + V(global) \ + V(memory) \ V(table) -#define WASM_EXTERN_AS_OTHER(name) \ - wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \ - { \ - return (wasm_##name##_t *)external; \ +#define WASM_EXTERN_AS_OTHER(name) \ + wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \ + { \ + return (wasm_##name##_t *)external; \ } BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER) #undef WASM_EXTERN_AS_OTHER -#define WASM_OTHER_AS_EXTERN(name) \ - wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \ - { \ - return (wasm_extern_t *)other; \ +#define WASM_OTHER_AS_EXTERN(name) \ + wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \ + { \ + return (wasm_extern_t *)other; \ } BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN) #undef WASM_OTHER_AS_EXTERN -#define WASM_EXTERN_AS_OTHER_CONST(name) \ - const wasm_##name##_t *wasm_extern_as_##name##_const( \ - const wasm_extern_t *external) \ - { \ - return (const wasm_##name##_t *)external; \ +#define WASM_EXTERN_AS_OTHER_CONST(name) \ + const wasm_##name##_t *wasm_extern_as_##name##_const( \ + const wasm_extern_t *external) \ + { \ + return (const wasm_##name##_t *)external; \ } BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST) #undef WASM_EXTERN_AS_OTHER_CONST -#define WASM_OTHER_AS_EXTERN_CONST(name) \ - const wasm_extern_t *wasm_##name##_as_extern_const( \ - const wasm_##name##_t *other) \ - { \ - return (const wasm_extern_t *)other; \ +#define WASM_OTHER_AS_EXTERN_CONST(name) \ + const wasm_extern_t *wasm_##name##_as_extern_const( \ + const wasm_##name##_t *other) \ + { \ + return (const wasm_extern_t *)other; \ } BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST) diff --git a/core/iwasm/common/wasm_c_api_internal.h b/core/iwasm/common/wasm_c_api_internal.h index c77fa454a8..919d1524fc 100644 --- a/core/iwasm/common/wasm_c_api_internal.h +++ b/core/iwasm/common/wasm_c_api_internal.h @@ -213,33 +213,27 @@ struct wasm_instance_t { }; wasm_ref_t * -wasm_ref_new_internal(wasm_store_t *store, - enum wasm_reference_kind kind, +wasm_ref_new_internal(wasm_store_t *store, enum wasm_reference_kind kind, uint32 obj_idx_rt, WASMModuleInstanceCommon *inst_comm_rt); wasm_foreign_t * -wasm_foreign_new_internal(wasm_store_t *store, - uint32 foreign_idx_rt, +wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt, WASMModuleInstanceCommon *inst_comm_rt); wasm_func_t * -wasm_func_new_internal(wasm_store_t *store, - uint16 func_idx_rt, +wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt, WASMModuleInstanceCommon *inst_comm_rt); wasm_global_t * -wasm_global_new_internal(wasm_store_t *store, - uint16 global_idx_rt, +wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt, WASMModuleInstanceCommon *inst_comm_rt); wasm_memory_t * -wasm_memory_new_internal(wasm_store_t *store, - uint16 memory_idx_rt, +wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt, WASMModuleInstanceCommon *inst_comm_rt); wasm_table_t * -wasm_table_new_internal(wasm_store_t *store, - uint16 table_idx_rt, +wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt, WASMModuleInstanceCommon *inst_comm_rt); #endif /* _WASM_C_API_INTERNAL_H */ diff --git a/core/iwasm/common/wasm_exec_env.c b/core/iwasm/common/wasm_exec_env.c index c9e2bf5a4d..685542b793 100644 --- a/core/iwasm/common/wasm_exec_env.c +++ b/core/iwasm/common/wasm_exec_env.c @@ -27,8 +27,8 @@ WASMExecEnv * wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst, uint32 stack_size) { - uint64 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom) - + (uint64)stack_size; + uint64 total_size = + offsetof(WASMExecEnv, wasm_stack.s.bottom) + (uint64)stack_size; WASMExecEnv *exec_env; if (total_size >= UINT32_MAX @@ -122,22 +122,23 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst, if (!exec_env) return NULL; - /* Set the aux_stack_boundary and aux_stack_bottom */ #if WASM_ENABLE_INTERP != 0 + /* Set the aux_stack_boundary and aux_stack_bottom */ if (module_inst->module_type == Wasm_Module_Bytecode) { WASMModule *module = ((WASMModuleInstance *)module_inst)->module; exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom; - exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom - - module->aux_stack_size; + exec_env->aux_stack_boundary.boundary = + module->aux_stack_bottom - module->aux_stack_size; } #endif #if WASM_ENABLE_AOT != 0 + /* Set the aux_stack_boundary and aux_stack_bottom */ if (module_inst->module_type == Wasm_Module_AoT) { AOTModule *module = (AOTModule *)(((AOTModuleInstance *)module_inst)->aot_module.ptr); exec_env->aux_stack_bottom.bottom = module->aux_stack_bottom; - exec_env->aux_stack_boundary.boundary = module->aux_stack_bottom - - module->aux_stack_size; + exec_env->aux_stack_boundary.boundary = + module->aux_stack_bottom - module->aux_stack_size; } #endif @@ -150,8 +151,8 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst, #if WASM_ENABLE_DEBUG_INTERP != 0 wasm_debug_instance_create(cluster); #endif +#endif /* end of WASM_ENABLE_THREAD_MGR */ -#endif return exec_env; } @@ -162,16 +163,15 @@ wasm_exec_env_destroy(WASMExecEnv *exec_env) /* Terminate all sub-threads */ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); if (cluster) { -#if WASM_ENABLE_THREAD_MGR != 0 #if WASM_ENABLE_DEBUG_INTERP != 0 wasm_cluster_thread_exited(exec_env); wasm_debug_instance_destroy(cluster); -#endif #endif wasm_cluster_terminate_all_except_self(cluster, exec_env); wasm_cluster_del_exec_env(cluster, exec_env); } -#endif +#endif /* end of WASM_ENABLE_THREAD_MGR */ + wasm_exec_env_destroy_internal(exec_env); } @@ -224,4 +224,3 @@ wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env) return NULL; } #endif - diff --git a/core/iwasm/common/wasm_exec_env.h b/core/iwasm/common/wasm_exec_env.h index dc65144509..ea2fafd680 100644 --- a/core/iwasm/common/wasm_exec_env.h +++ b/core/iwasm/common/wasm_exec_env.h @@ -89,7 +89,7 @@ typedef struct WASMExecEnv { void *thread_ret_value; /* Must be provided by thread library */ - void* (*thread_start_routine)(void *); + void *(*thread_start_routine)(void *); void *thread_arg; /* pointer to the cluster */ @@ -192,8 +192,8 @@ wasm_exec_env_alloc_wasm_frame(WASMExecEnv *exec_env, unsigned size) #if WASM_ENABLE_MEMORY_PROFILING != 0 { - uint32 wasm_stack_used = exec_env->wasm_stack.s.top - - exec_env->wasm_stack.s.bottom; + uint32 wasm_stack_used = + exec_env->wasm_stack.s.top - exec_env->wasm_stack.s.bottom; if (wasm_stack_used > exec_env->max_wasm_stack_used) exec_env->max_wasm_stack_used = wasm_stack_used; } @@ -215,7 +215,7 @@ wasm_exec_env_free_wasm_frame(WASMExecEnv *exec_env, void *prev_top) * * @return the current WASM stack top pointer */ -static inline void* +static inline void * wasm_exec_env_wasm_stack_top(WASMExecEnv *exec_env) { return exec_env->wasm_stack.s.top; @@ -241,7 +241,7 @@ wasm_exec_env_set_cur_frame(WASMExecEnv *exec_env, * * @return the current frame pointer */ -static inline struct WASMInterpFrame* +static inline struct WASMInterpFrame * wasm_exec_env_get_cur_frame(WASMExecEnv *exec_env) { return exec_env->cur_frame; @@ -253,7 +253,6 @@ wasm_exec_env_get_module_inst(WASMExecEnv *exec_env); void wasm_exec_env_set_thread_info(WASMExecEnv *exec_env); - #if WASM_ENABLE_THREAD_MGR != 0 void * wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env); diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index 0b558cb51d..42f52cacfd 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -39,8 +39,7 @@ wasm_memory_init_with_pool(void *mem, unsigned int bytes) } static bool -wasm_memory_init_with_allocator(void *_malloc_func, - void *_realloc_func, +wasm_memory_init_with_allocator(void *_malloc_func, void *_realloc_func, void *_free_func) { if (_malloc_func && _free_func && _malloc_func != _free_func) { @@ -50,8 +49,8 @@ wasm_memory_init_with_allocator(void *_malloc_func, free_func = _free_func; return true; } - LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", - _malloc_func, _realloc_func, _free_func); + LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", _malloc_func, + _realloc_func, _free_func); return false; } @@ -63,9 +62,10 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, return wasm_memory_init_with_pool(alloc_option->pool.heap_buf, alloc_option->pool.heap_size); else if (mem_alloc_type == Alloc_With_Allocator) - return wasm_memory_init_with_allocator(alloc_option->allocator.malloc_func, - alloc_option->allocator.realloc_func, - alloc_option->allocator.free_func); + return wasm_memory_init_with_allocator( + alloc_option->allocator.malloc_func, + alloc_option->allocator.realloc_func, + alloc_option->allocator.free_func); else if (mem_alloc_type == Alloc_With_System_Allocator) return wasm_memory_init_with_allocator(os_malloc, os_realloc, os_free); else @@ -93,7 +93,8 @@ static inline void * wasm_runtime_malloc_internal(unsigned int size) { if (memory_mode == MEMORY_MODE_UNKNOWN) { - LOG_WARNING("wasm_runtime_malloc failed: memory hasn't been initialize.\n"); + LOG_WARNING( + "wasm_runtime_malloc failed: memory hasn't been initialize.\n"); return NULL; } else if (memory_mode == MEMORY_MODE_POOL) { @@ -108,7 +109,8 @@ static inline void * wasm_runtime_realloc_internal(void *ptr, unsigned int size) { if (memory_mode == MEMORY_MODE_UNKNOWN) { - LOG_WARNING("wasm_runtime_realloc failed: memory hasn't been initialize.\n"); + LOG_WARNING( + "wasm_runtime_realloc failed: memory hasn't been initialize.\n"); return NULL; } else if (memory_mode == MEMORY_MODE_POOL) { diff --git a/core/iwasm/common/wasm_memory.h b/core/iwasm/common/wasm_memory.h index 08157acfbe..8511ae647c 100644 --- a/core/iwasm/common/wasm_memory.h +++ b/core/iwasm/common/wasm_memory.h @@ -25,4 +25,3 @@ wasm_runtime_memory_destroy(); #endif #endif /* end of _WASM_MEMORY_H */ - diff --git a/core/iwasm/common/wasm_native.c b/core/iwasm/common/wasm_native.c index 1522b95e7c..15cb0e5c61 100644 --- a/core/iwasm/common/wasm_native.c +++ b/core/iwasm/common/wasm_native.c @@ -17,7 +17,7 @@ #define ENABLE_SORT_DEBUG 0 #if ENABLE_SORT_DEBUG != 0 -#include +#include #endif static NativeSymbolsList g_native_symbols_list = NULL; @@ -80,7 +80,7 @@ check_symbol_signature(const WASMType *type, const char *signature) #if WASM_ENABLE_REF_TYPES != 0 || (sig == 'i' && type->types[i] == VALUE_TYPE_EXTERNREF) #endif - ) + ) /* normal parameter */ continue; @@ -91,8 +91,7 @@ check_symbol_signature(const WASMType *type, const char *signature) if (sig == '*') { /* it is a pointer */ if (i + 1 < type->param_count - && type->types[i + 1] == VALUE_TYPE_I32 - && *p == '~') { + && type->types[i + 1] == VALUE_TYPE_I32 && *p == '~') { /* pointer length followed */ i++; p++; @@ -132,8 +131,8 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols) for (i = 0; i < n_native_symbols - 1; i++) { for (j = i + 1; j < n_native_symbols; j++) { - if (strcmp(native_symbols[i].symbol, - native_symbols[j].symbol) > 0) { + if (strcmp(native_symbols[i].symbol, native_symbols[j].symbol) + > 0) { temp = native_symbols[i]; native_symbols[i] = native_symbols[j]; native_symbols[j] = temp; @@ -143,7 +142,7 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols) } #else static void -swap_symbol(NativeSymbol* left, NativeSymbol* right) +swap_symbol(NativeSymbol *left, NativeSymbol *right) { NativeSymbol temp = *left; *left = *right; @@ -151,7 +150,7 @@ swap_symbol(NativeSymbol* left, NativeSymbol* right) } static void -quick_sort_symbols(NativeSymbol* native_symbols, int left, int right) +quick_sort_symbols(NativeSymbol *native_symbols, int left, int right) { NativeSymbol base_symbol; int pin_left = left; @@ -164,8 +163,8 @@ quick_sort_symbols(NativeSymbol* native_symbols, int left, int right) base_symbol = native_symbols[left]; while (left < right) { while (left < right - && strcmp(native_symbols[right].symbol, - base_symbol.symbol) > 0) { + && strcmp(native_symbols[right].symbol, base_symbol.symbol) + > 0) { right--; } @@ -175,8 +174,7 @@ quick_sort_symbols(NativeSymbol* native_symbols, int left, int right) } while (left < right - && strcmp(native_symbols[left].symbol, - base_symbol.symbol) < 0) { + && strcmp(native_symbols[left].symbol, base_symbol.symbol) < 0) { left++; } @@ -216,7 +214,7 @@ lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols, return NULL; } -void* +void * wasm_native_resolve_symbol(const char *module_name, const char *field_name, const WASMType *func_type, const char **p_signature, void **p_attachment, bool *p_call_conv_raw) @@ -229,15 +227,14 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name, while (node) { node_next = node->next; if (!strcmp(node->module_name, module_name)) { - if ((func_ptr = lookup_symbol(node->native_symbols, - node->n_native_symbols, - field_name, &signature, &attachment)) + if ((func_ptr = + lookup_symbol(node->native_symbols, node->n_native_symbols, + field_name, &signature, &attachment)) || (field_name[0] == '_' - && (func_ptr = lookup_symbol(node->native_symbols, - node->n_native_symbols, - field_name + 1, - &signature, &attachment)))) - break; + && (func_ptr = lookup_symbol( + node->native_symbols, node->n_native_symbols, + field_name + 1, &signature, &attachment)))) + break; } node = node_next; } @@ -246,7 +243,8 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name, if (signature && signature[0] != '\0') { /* signature is not empty, check its format */ if (!check_symbol_signature(func_type, signature)) { -#if WASM_ENABLE_WAMR_COMPILER == 0 /* Output warning except running aot compiler */ +#if WASM_ENABLE_WAMR_COMPILER == 0 + /* Output warning except running aot compiler */ LOG_WARNING("failed to check signature '%s' and resolve " "pointer params for import function (%s %s)\n", signature, module_name, field_name); @@ -270,10 +268,8 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name, } static bool -register_natives(const char *module_name, - NativeSymbol *native_symbols, - uint32 n_native_symbols, - bool call_conv_raw) +register_natives(const char *module_name, NativeSymbol *native_symbols, + uint32 n_native_symbols, bool call_conv_raw) { NativeSymbolsNode *node; #if ENABLE_SORT_DEBUG != 0 @@ -309,10 +305,10 @@ register_natives(const char *module_name, #if ENABLE_SORT_DEBUG != 0 gettimeofday(&end, NULL); - timer = 1000000 * (end.tv_sec - start.tv_sec) - + (end.tv_usec - start.tv_usec); - LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us", - module_name, n_native_symbols, timer); + timer = + 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec); + LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us", module_name, + n_native_symbols, timer); #endif return true; } @@ -322,7 +318,8 @@ wasm_native_register_natives(const char *module_name, NativeSymbol *native_symbols, uint32 n_native_symbols) { - return register_natives(module_name, native_symbols, n_native_symbols, false); + return register_natives(module_name, native_symbols, n_native_symbols, + false); } bool @@ -330,7 +327,8 @@ wasm_native_register_natives_raw(const char *module_name, NativeSymbol *native_symbols, uint32 n_native_symbols) { - return register_natives(module_name, native_symbols, n_native_symbols, true); + return register_natives(module_name, native_symbols, n_native_symbols, + true); } bool @@ -341,41 +339,40 @@ wasm_native_init() #if WASM_ENABLE_LIBC_BUILTIN != 0 n_native_symbols = get_libc_builtin_export_apis(&native_symbols); - if (!wasm_native_register_natives("env", - native_symbols, n_native_symbols)) + if (!wasm_native_register_natives("env", native_symbols, n_native_symbols)) return false; #endif /* WASM_ENABLE_LIBC_BUILTIN */ #if WASM_ENABLE_SPEC_TEST n_native_symbols = get_spectest_export_apis(&native_symbols); - if (!wasm_native_register_natives("spectest", - native_symbols, n_native_symbols)) + if (!wasm_native_register_natives("spectest", native_symbols, + n_native_symbols)) return false; #endif /* WASM_ENABLE_SPEC_TEST */ #if WASM_ENABLE_LIBC_WASI != 0 n_native_symbols = get_libc_wasi_export_apis(&native_symbols); - if (!wasm_native_register_natives("wasi_unstable", - native_symbols, n_native_symbols)) + if (!wasm_native_register_natives("wasi_unstable", native_symbols, + n_native_symbols)) return false; - if (!wasm_native_register_natives("wasi_snapshot_preview1", - native_symbols, n_native_symbols)) + if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols, + n_native_symbols)) return false; #endif #if WASM_ENABLE_BASE_LIB != 0 n_native_symbols = get_base_lib_export_apis(&native_symbols); if (n_native_symbols > 0 - && !wasm_native_register_natives("env", - native_symbols, n_native_symbols)) + && !wasm_native_register_natives("env", native_symbols, + n_native_symbols)) return false; #endif #if WASM_ENABLE_APP_FRAMEWORK != 0 n_native_symbols = get_ext_lib_export_apis(&native_symbols); if (n_native_symbols > 0 - && !wasm_native_register_natives("env", - native_symbols, n_native_symbols)) + && !wasm_native_register_natives("env", native_symbols, + n_native_symbols)) return false; #endif @@ -385,16 +382,16 @@ wasm_native_init() n_native_symbols = get_lib_pthread_export_apis(&native_symbols); if (n_native_symbols > 0 - && !wasm_native_register_natives("env", - native_symbols, n_native_symbols)) + && !wasm_native_register_natives("env", native_symbols, + n_native_symbols)) return false; #endif #if WASM_ENABLE_LIBC_EMCC != 0 n_native_symbols = get_libc_emcc_export_apis(&native_symbols); if (n_native_symbols > 0 - && !wasm_native_register_natives("env", - native_symbols, n_native_symbols)) + && !wasm_native_register_natives("env", native_symbols, + n_native_symbols)) return false; #endif /* WASM_ENABLE_LIBC_EMCC */ diff --git a/core/iwasm/common/wasm_native.h b/core/iwasm/common/wasm_native.h index d76d238a81..2777b38b2b 100644 --- a/core/iwasm/common/wasm_native.h +++ b/core/iwasm/common/wasm_native.h @@ -49,7 +49,7 @@ wasm_native_lookup_libc_builtin_global(const char *module_name, * * @return the native function pointer if success, NULL otherwise */ -void* +void * wasm_native_resolve_symbol(const char *module_name, const char *field_name, const WASMType *func_type, const char **p_signature, void **p_attachment, bool *p_call_conv_raw); @@ -75,4 +75,3 @@ wasm_native_destroy(); #endif #endif /* end of _WASM_NATIVE_H */ - diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index e7db08f5b1..286c2200c0 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -30,10 +30,10 @@ #include "../common/wasm_c_api_internal.h" #if WASM_ENABLE_MULTI_MODULE != 0 -/* - * a safety insurance to prevent - * circular depencies leading a stack overflow - * try break early +/** + * A safety insurance to prevent + * circular depencies which leads stack overflow + * try to break early */ typedef struct LoadingModule { bh_list_link l; @@ -45,9 +45,9 @@ static bh_list loading_module_list_head; static bh_list *const loading_module_list = &loading_module_list_head; static korp_mutex loading_module_list_lock; -/* - * a list about all exported functions, globals, memories, tables of every - * fully loaded module +/** + * A list to store all exported functions/globals/memories/tables + * of every fully loaded module */ static bh_list registered_module_list_head; static bh_list *const registered_module_list = ®istered_module_list_head; @@ -79,15 +79,12 @@ runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst, { void *mem; - if (size >= UINT32_MAX - || !(mem = wasm_runtime_malloc((uint32)size))) { + if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { if (module_inst != NULL) { - wasm_runtime_set_exception(module_inst, - "allocate memory failed"); + wasm_runtime_set_exception(module_inst, "allocate memory failed"); } else if (error_buf != NULL) { - set_error_buf(error_buf, error_buf_size, - "allocate memory failed"); + set_error_buf(error_buf, error_buf_size, "allocate memory failed"); } return NULL; } @@ -186,11 +183,9 @@ wasm_runtime_env_init() static bool wasm_runtime_exec_env_check(WASMExecEnv *exec_env) { - return exec_env - && exec_env->module_inst - && exec_env->wasm_stack_size > 0 - && exec_env->wasm_stack.s.top_boundary == - exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size + return exec_env && exec_env->module_inst && exec_env->wasm_stack_size > 0 + && exec_env->wasm_stack.s.top_boundary + == exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary; } @@ -265,8 +260,8 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args) #if WASM_ENABLE_DEBUG_INTERP != 0 if (strlen(init_args->ip_addr)) if (!wasm_debug_engine_init(init_args->ip_addr, - init_args->platform_port, - init_args->instance_port)) { + init_args->platform_port, + init_args->instance_port)) { wasm_runtime_destroy(); return false; } @@ -342,29 +337,29 @@ wasm_runtime_register_module_internal(const char *module_name, WASMModuleCommon *module, uint8 *orig_file_buf, uint32 orig_file_buf_size, - char *error_buf, - uint32_t error_buf_size) + char *error_buf, uint32_t error_buf_size) { WASMRegisteredModule *node = NULL; node = wasm_runtime_find_module_registered_by_reference(module); - if (node) { /* module has been registered */ + if (node) { /* module has been registered */ if (node->module_name) { /* module has name */ - if (!module_name || strcmp(node->module_name, module_name)) { - /* module has different name */ - LOG_DEBUG("module(%p) has been registered with name %s", - module, node->module_name); - set_error_buf(error_buf, error_buf_size, - "Register module failed: " - "failed to rename the module"); - return false; - } - else { - /* module has the same name */ - LOG_DEBUG("module(%p) has been registered with the same name %s", - module, node->module_name); - return true; - } + if (!module_name || strcmp(node->module_name, module_name)) { + /* module has different name */ + LOG_DEBUG("module(%p) has been registered with name %s", module, + node->module_name); + set_error_buf(error_buf, error_buf_size, + "Register module failed: " + "failed to rename the module"); + return false; + } + else { + /* module has the same name */ + LOG_DEBUG( + "module(%p) has been registered with the same name %s", + module, node->module_name); + return true; + } } else { /* module has empyt name, reset it */ @@ -420,9 +415,8 @@ wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module, return false; } - return wasm_runtime_register_module_internal( - module_name, module, NULL, 0, - error_buf, error_buf_size); + return wasm_runtime_register_module_internal(module_name, module, NULL, 0, + error_buf, error_buf_size); } void @@ -453,8 +447,7 @@ wasm_runtime_find_module_registered(const char *module_name) module = bh_list_first_elem(registered_module_list); while (module) { module_next = bh_list_elem_next(module); - if (module->module_name - && !strcmp(module_name, module->module_name)) { + if (module->module_name && !strcmp(module_name, module->module_name)) { break; } module = module_next; @@ -512,13 +505,12 @@ wasm_runtime_destroy_registered_module_list() } bool -wasm_runtime_add_loading_module(const char *module_name, - char *error_buf, uint32 error_buf_size) +wasm_runtime_add_loading_module(const char *module_name, char *error_buf, + uint32 error_buf_size) { LOG_DEBUG("add %s into a loading list", module_name); LoadingModule *loadingModule = - runtime_malloc(sizeof(LoadingModule), NULL, - error_buf, error_buf_size); + runtime_malloc(sizeof(LoadingModule), NULL, error_buf, error_buf_size); if (!loadingModule) { return false; @@ -600,8 +592,7 @@ wasm_runtime_destroy_loading_module_list() bool wasm_runtime_is_built_in_module(const char *module_name) { - return (!strcmp("env", module_name) - || !strcmp("wasi_unstable", module_name) + return (!strcmp("env", module_name) || !strcmp("wasi_unstable", module_name) || !strcmp("wasi_snapshot_preview1", module_name) #if WASM_ENABLE_SPEC_TEST != 0 || !strcmp("spectest", module_name) @@ -611,11 +602,11 @@ wasm_runtime_is_built_in_module(const char *module_name) #if WASM_ENABLE_THREAD_MGR != 0 bool -wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, - uint32 start_offset, uint32 size) +wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, + uint32 size) { - WASMModuleInstanceCommon *module_inst - = wasm_exec_env_get_module_inst(exec_env); + WASMModuleInstanceCommon *module_inst = + wasm_exec_env_get_module_inst(exec_env); #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { return wasm_set_aux_stack(exec_env, start_offset, size); @@ -630,11 +621,11 @@ wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, } bool -wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, - uint32 *start_offset, uint32 *size) +wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, + uint32 *size) { - WASMModuleInstanceCommon *module_inst - = wasm_exec_env_get_module_inst(exec_env); + WASMModuleInstanceCommon *module_inst = + wasm_exec_env_get_module_inst(exec_env); #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { return wasm_get_aux_stack(exec_env, start_offset, size); @@ -656,15 +647,13 @@ wasm_runtime_set_max_thread_num(uint32 num) #endif /* end of WASM_ENABLE_THREAD_MGR */ static WASMModuleCommon * -register_module_with_null_name(WASMModuleCommon *module_common, - char *error_buf, uint32 error_buf_size) +register_module_with_null_name(WASMModuleCommon *module_common, char *error_buf, + uint32 error_buf_size) { #if WASM_ENABLE_MULTI_MODULE != 0 if (module_common) { - if (!wasm_runtime_register_module_internal(NULL, module_common, - NULL, 0, - error_buf, - error_buf_size)) { + if (!wasm_runtime_register_module_internal(NULL, module_common, NULL, 0, + error_buf, error_buf_size)) { wasm_runtime_unload(module_common); return NULL; } @@ -678,8 +667,8 @@ register_module_with_null_name(WASMModuleCommon *module_common, } WASMModuleCommon * -wasm_runtime_load(const uint8 *buf, uint32 size, - char *error_buf, uint32 error_buf_size) +wasm_runtime_load(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size) { WASMModuleCommon *module_common = NULL; @@ -690,28 +679,28 @@ wasm_runtime_load(const uint8 *buf, uint32 size, if (!module) return NULL; - if (!(aot_module = aot_convert_wasm_module(module, - error_buf, error_buf_size))) { + if (!(aot_module = + aot_convert_wasm_module(module, error_buf, error_buf_size))) { wasm_unload(module); return NULL; } - module_common = (WASMModuleCommon*)aot_module; - return register_module_with_null_name(module_common, - error_buf, error_buf_size); + module_common = (WASMModuleCommon *)aot_module; + return register_module_with_null_name(module_common, error_buf, + error_buf_size); #elif WASM_ENABLE_INTERP != 0 - module_common = (WASMModuleCommon*) - wasm_load(buf, size, error_buf, error_buf_size); - return register_module_with_null_name(module_common, - error_buf, error_buf_size); + module_common = + (WASMModuleCommon *)wasm_load(buf, size, error_buf, error_buf_size); + return register_module_with_null_name(module_common, error_buf, + error_buf_size); #endif } else if (get_package_type(buf, size) == Wasm_Module_AoT) { #if WASM_ENABLE_AOT != 0 - module_common = (WASMModuleCommon*) - aot_load_from_aot_file(buf, size, error_buf, error_buf_size); - return register_module_with_null_name(module_common, - error_buf, error_buf_size); + module_common = (WASMModuleCommon *)aot_load_from_aot_file( + buf, size, error_buf, error_buf_size); + return register_module_with_null_name(module_common, error_buf, + error_buf_size); #endif } @@ -719,8 +708,8 @@ wasm_runtime_load(const uint8 *buf, uint32 size, set_error_buf(error_buf, error_buf_size, "WASM module load failed: unexpected end"); else - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: magic header not detected"); + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: magic header not detected"); return NULL; } @@ -732,20 +721,18 @@ wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot, #if WASM_ENABLE_INTERP != 0 if (!is_aot) { - module_common = (WASMModuleCommon*) - wasm_load_from_sections(section_list, - error_buf, error_buf_size); - return register_module_with_null_name(module_common, - error_buf, error_buf_size); + module_common = (WASMModuleCommon *)wasm_load_from_sections( + section_list, error_buf, error_buf_size); + return register_module_with_null_name(module_common, error_buf, + error_buf_size); } #endif #if WASM_ENABLE_AOT != 0 if (is_aot) { - module_common = (WASMModuleCommon*) - aot_load_from_sections(section_list, - error_buf, error_buf_size); - return register_module_with_null_name(module_common, - error_buf, error_buf_size); + module_common = (WASMModuleCommon *)aot_load_from_sections( + section_list, error_buf, error_buf_size); + return register_module_with_null_name(module_common, error_buf, + error_buf_size); } #endif @@ -767,14 +754,14 @@ wasm_runtime_unload(WASMModuleCommon *module) #if WASM_ENABLE_INTERP != 0 if (module->module_type == Wasm_Module_Bytecode) { - wasm_unload((WASMModule*)module); + wasm_unload((WASMModule *)module); return; } #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) { - aot_unload((AOTModule*)module); + aot_unload((AOTModule *)module); return; } #endif @@ -787,17 +774,15 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst, { #if WASM_ENABLE_INTERP != 0 if (module->module_type == Wasm_Module_Bytecode) - return (WASMModuleInstanceCommon*) - wasm_instantiate((WASMModule*)module, is_sub_inst, - stack_size, heap_size, - error_buf, error_buf_size); + return (WASMModuleInstanceCommon *)wasm_instantiate( + (WASMModule *)module, is_sub_inst, stack_size, heap_size, error_buf, + error_buf_size); #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) - return (WASMModuleInstanceCommon*) - aot_instantiate((AOTModule*)module, is_sub_inst, - stack_size, heap_size, - error_buf, error_buf_size); + return (WASMModuleInstanceCommon *)aot_instantiate( + (AOTModule *)module, is_sub_inst, stack_size, heap_size, error_buf, + error_buf_size); #endif set_error_buf(error_buf, error_buf_size, "Instantiate module failed, invalid module type"); @@ -805,13 +790,12 @@ wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst, } WASMModuleInstanceCommon * -wasm_runtime_instantiate(WASMModuleCommon *module, - uint32 stack_size, uint32 heap_size, - char *error_buf, uint32 error_buf_size) +wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, + uint32 heap_size, char *error_buf, + uint32 error_buf_size) { - return wasm_runtime_instantiate_internal(module, false, - stack_size, heap_size, - error_buf, error_buf_size); + return wasm_runtime_instantiate_internal( + module, false, stack_size, heap_size, error_buf, error_buf_size); } void @@ -820,13 +804,13 @@ wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - wasm_deinstantiate((WASMModuleInstance*)module_inst, is_sub_inst); + wasm_deinstantiate((WASMModuleInstance *)module_inst, is_sub_inst); return; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - aot_deinstantiate((AOTModuleInstance*)module_inst, is_sub_inst); + aot_deinstantiate((AOTModuleInstance *)module_inst, is_sub_inst); return; } #endif @@ -894,12 +878,12 @@ wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module) #if WASM_ENABLE_INTERP != 0 if (module->module_type == Wasm_Module_Bytecode) { - wasm_get_module_mem_consumption((WASMModule*)module, &mem_conspn); + wasm_get_module_mem_consumption((WASMModule *)module, &mem_conspn); } #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) { - aot_get_module_mem_consumption((AOTModule*)module, &mem_conspn); + aot_get_module_mem_consumption((AOTModule *)module, &mem_conspn); } #endif @@ -922,20 +906,20 @@ wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module) } void -wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon - *module_inst) +wasm_runtime_dump_module_inst_mem_consumption( + const WASMModuleInstanceCommon *module_inst) { WASMModuleInstMemConsumption mem_conspn = { 0 }; #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - wasm_get_module_inst_mem_consumption((WASMModuleInstance*)module_inst, + wasm_get_module_inst_mem_consumption((WASMModuleInstance *)module_inst, &mem_conspn); } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - aot_get_module_inst_mem_consumption((AOTModuleInstance*)module_inst, + aot_get_module_inst_mem_consumption((AOTModuleInstance *)module_inst, &mem_conspn); } #endif @@ -955,8 +939,8 @@ wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon void wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env) { - uint32 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom) - + exec_env->wasm_stack_size; + uint32 total_size = + offsetof(WASMExecEnv, wasm_stack.s.bottom) + exec_env->wasm_stack_size; os_printf("Exec env memory consumption, total size: %u\n", total_size); os_printf(" exec env struct size: %u\n", @@ -986,16 +970,15 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env) #if WASM_ENABLE_INTERP != 0 if (module_inst_common->module_type == Wasm_Module_Bytecode) { WASMModuleInstance *wasm_module_inst = - (WASMModuleInstance*)module_inst_common; + (WASMModuleInstance *)module_inst_common; WASMModule *wasm_module = wasm_module_inst->module; - module_common = (WASMModuleCommon*)wasm_module; + module_common = (WASMModuleCommon *)wasm_module; if (wasm_module_inst->memories) { heap_handle = wasm_module_inst->memories[0]->heap_handle; } - wasm_get_module_inst_mem_consumption - (wasm_module_inst, &module_inst_mem_consps); - wasm_get_module_mem_consumption - (wasm_module, &module_mem_consps); + wasm_get_module_inst_mem_consumption(wasm_module_inst, + &module_inst_mem_consps); + wasm_get_module_mem_consumption(wasm_module, &module_mem_consps); if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1) max_aux_stack_used = wasm_module_inst->max_aux_stack_used; } @@ -1003,19 +986,17 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env) #if WASM_ENABLE_AOT != 0 if (module_inst_common->module_type == Wasm_Module_AoT) { AOTModuleInstance *aot_module_inst = - (AOTModuleInstance*)module_inst_common; - AOTModule *aot_module = - (AOTModule*)aot_module_inst->aot_module.ptr; - module_common = (WASMModuleCommon*)aot_module; + (AOTModuleInstance *)module_inst_common; + AOTModule *aot_module = (AOTModule *)aot_module_inst->aot_module.ptr; + module_common = (WASMModuleCommon *)aot_module; if (aot_module_inst->memories.ptr) { AOTMemoryInstance **memories = - (AOTMemoryInstance **)aot_module_inst->memories.ptr; + (AOTMemoryInstance **)aot_module_inst->memories.ptr; heap_handle = memories[0]->heap_handle.ptr; } - aot_get_module_inst_mem_consumption - (aot_module_inst, &module_inst_mem_consps); - aot_get_module_mem_consumption - (aot_module, &module_mem_consps); + aot_get_module_inst_mem_consumption(aot_module_inst, + &module_inst_mem_consps); + aot_get_module_mem_consumption(aot_module, &module_mem_consps); } #endif @@ -1026,8 +1007,7 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env) } total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom) - + exec_env->wasm_stack_size - + module_mem_consps.total_size + + exec_env->wasm_stack_size + module_mem_consps.total_size + module_inst_mem_consps.total_size; os_printf("\nMemory consumption summary (bytes):\n"); @@ -1035,7 +1015,8 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env) wasm_runtime_dump_module_inst_mem_consumption(module_inst_common); wasm_runtime_dump_exec_env_mem_consumption(exec_env); os_printf("\nTotal memory consumption of module, module inst and " - "exec env: %u\n", total_size); + "exec env: %u\n", + total_size); os_printf("Total interpreter stack used: %u\n", exec_env->max_wasm_stack_used); @@ -1046,7 +1027,7 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env) os_printf("Total app heap used: %u\n", app_heap_peak_size); } -#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) +#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \ || (WASM_ENABLE_MEMORY_TRACING != 0) */ #if WASM_ENABLE_PERF_PROFILING != 0 @@ -1055,12 +1036,12 @@ wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - wasm_dump_perf_profiling((WASMModuleInstance*)module_inst); + wasm_dump_perf_profiling((WASMModuleInstance *)module_inst); } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - aot_dump_perf_profiling((AOTModuleInstance*)module_inst); + aot_dump_perf_profiling((AOTModuleInstance *)module_inst); } #endif } @@ -1099,17 +1080,15 @@ wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function, #if WASM_ENABLE_INTERP != 0 if (module_type == Wasm_Module_Bytecode) { WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function; - type = wasm_func->is_import_func - ? wasm_func->u.func_import->func_type - : wasm_func->u.func->func_type; + type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type + : wasm_func->u.func->func_type; } #endif #if WASM_ENABLE_AOT != 0 if (module_type == Wasm_Module_AoT) { AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function; - type = aot_func->is_import_func - ? aot_func->u.func_import->func_type - : aot_func->u.func.func_type; + type = aot_func->is_import_func ? aot_func->u.func_import->func_type + : aot_func->u.func.func_type; } #endif @@ -1117,20 +1096,18 @@ wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function, } WASMFunctionInstanceCommon * -wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst, +wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst, const char *name, const char *signature) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return (WASMFunctionInstanceCommon*) - wasm_lookup_function((const WASMModuleInstance*)module_inst, - name, signature); + return (WASMFunctionInstanceCommon *)wasm_lookup_function( + (const WASMModuleInstance *)module_inst, name, signature); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return (WASMFunctionInstanceCommon*) - aot_lookup_function((const AOTModuleInstance*)module_inst, - name, signature); + return (WASMFunctionInstanceCommon *)aot_lookup_function( + (const AOTModuleInstance *)module_inst, name, signature); #endif return NULL; } @@ -1143,7 +1120,7 @@ wasm_runtime_reclaim_externref(WASMExecEnv *exec_env, { uint32 i = 0, cell_num = 0; WASMType *func_type = wasm_runtime_get_function_type( - function, exec_env->module_inst->module_type); + function, exec_env->module_inst->module_type); bh_assert(func_type); while (i < func_type->result_count) { @@ -1181,8 +1158,8 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env, bool wasm_runtime_call_wasm(WASMExecEnv *exec_env, - WASMFunctionInstanceCommon *function, - uint32 argc, uint32 argv[]) + WASMFunctionInstanceCommon *function, uint32 argc, + uint32 argv[]) { bool ret = false; @@ -1197,15 +1174,13 @@ wasm_runtime_call_wasm(WASMExecEnv *exec_env, #if WASM_ENABLE_INTERP != 0 if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) - ret = wasm_call_function(exec_env, - (WASMFunctionInstance*)function, - argc, argv); + ret = wasm_call_function(exec_env, (WASMFunctionInstance *)function, + argc, argv); #endif #if WASM_ENABLE_AOT != 0 if (exec_env->module_inst->module_type == Wasm_Module_AoT) - ret = aot_call_function(exec_env, - (AOTFunctionInstance*)function, - argc, argv); + ret = aot_call_function(exec_env, (AOTFunctionInstance *)function, argc, + argv); #endif #if WASM_ENABLE_REF_TYPES != 0 @@ -1216,8 +1191,7 @@ wasm_runtime_call_wasm(WASMExecEnv *exec_env, } static uint32 -parse_args_to_uint32_array(WASMType *type, - uint32 num_args, wasm_val_t *args, +parse_args_to_uint32_array(WASMType *type, uint32 num_args, wasm_val_t *args, uint32 *out_argv) { uint32 i, p; @@ -1229,7 +1203,10 @@ parse_args_to_uint32_array(WASMType *type, break; case WASM_I64: { - union { uint64 val; uint32 parts[2]; } u; + union { + uint64 val; + uint32 parts[2]; + } u; u.val = args[i].of.i64; out_argv[p++] = u.parts[0]; out_argv[p++] = u.parts[1]; @@ -1237,14 +1214,20 @@ parse_args_to_uint32_array(WASMType *type, } case WASM_F32: { - union { float32 val; uint32 part; } u; + union { + float32 val; + uint32 part; + } u; u.val = args[i].of.f32; out_argv[p++] = u.part; break; } case WASM_F64: { - union { float64 val; uint32 parts[2]; } u; + union { + float64 val; + uint32 parts[2]; + } u; u.val = args[i].of.f64; out_argv[p++] = u.parts[0]; out_argv[p++] = u.parts[1]; @@ -1259,8 +1242,7 @@ parse_args_to_uint32_array(WASMType *type, } static uint32 -parse_uint32_array_to_results(WASMType *type, - uint32 argc, uint32 *argv, +parse_uint32_array_to_results(WASMType *type, uint32 argc, uint32 *argv, wasm_val_t *out_results) { uint32 i, p; @@ -1273,7 +1255,10 @@ parse_uint32_array_to_results(WASMType *type, break; case VALUE_TYPE_I64: { - union { uint64 val; uint32 parts[2]; } u; + union { + uint64 val; + uint32 parts[2]; + } u; u.parts[0] = argv[p++]; u.parts[1] = argv[p++]; out_results[i].kind = WASM_I64; @@ -1282,7 +1267,10 @@ parse_uint32_array_to_results(WASMType *type, } case VALUE_TYPE_F32: { - union { float32 val; uint32 part; } u; + union { + float32 val; + uint32 part; + } u; u.part = argv[p++]; out_results[i].kind = WASM_F32; out_results[i].of.f32 = u.val; @@ -1290,7 +1278,10 @@ parse_uint32_array_to_results(WASMType *type, } case VALUE_TYPE_F64: { - union { float64 val; uint32 parts[2]; } u; + union { + float64 val; + uint32 parts[2]; + } u; u.parts[0] = argv[p++]; u.parts[1] = argv[p++]; out_results[i].kind = WASM_F64; @@ -1320,7 +1311,8 @@ wasm_runtime_call_wasm_a(WASMExecEnv *exec_env, type = wasm_runtime_get_function_type(function, module_type); if (!type) { - LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be enabled at least one."); + LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be " + "enabled at least one."); goto fail1; } @@ -1328,18 +1320,22 @@ wasm_runtime_call_wasm_a(WASMExecEnv *exec_env, cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num; if (num_results != type->result_count) { - LOG_ERROR("The result value number does not match the function declaration."); + LOG_ERROR( + "The result value number does not match the function declaration."); goto fail1; } if (num_args != type->param_count) { - LOG_ERROR("The argument value number does not match the function declaration."); + LOG_ERROR("The argument value number does not match the function " + "declaration."); goto fail1; } total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2); - if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL, 0))) { - wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed"); + if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL, + 0))) { + wasm_runtime_set_exception(exec_env->module_inst, + "allocate memory failed"); goto fail1; } @@ -1347,7 +1343,8 @@ wasm_runtime_call_wasm_a(WASMExecEnv *exec_env, if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv))) goto fail2; - ret_num = parse_uint32_array_to_results(type, type->ret_cell_num, argv, results); + ret_num = + parse_uint32_array_to_results(type, type->ret_cell_num, argv, results); bh_assert(ret_num == num_results); (void)ret_num; @@ -1383,8 +1380,10 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, "function declaration."); goto fail1; } - if (!(args = runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) { - wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed"); + if (!(args = + runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) { + wasm_runtime_set_exception(exec_env->module_inst, + "allocate memory failed"); goto fail1; } @@ -1405,7 +1404,7 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, break; case VALUE_TYPE_F64: args[i].kind = WASM_F64; - args[i].of.f64 = va_arg(vargs, float64);; + args[i].of.f64 = va_arg(vargs, float64); break; default: bh_assert(0); @@ -1422,23 +1421,23 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, } bool -wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst, - WASMFunctionInstanceCommon *function, - uint32 argc, uint32 argv[]) +wasm_runtime_create_exec_env_and_call_wasm( + WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function, + uint32 argc, uint32 argv[]) { bool ret = false; #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) ret = wasm_create_exec_env_and_call_function( - (WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function, - argc, argv); + (WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function, + argc, argv); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) ret = aot_create_exec_env_and_call_function( - (AOTModuleInstance *)module_inst, (AOTFunctionInstance *)function, - argc, argv); + (AOTModuleInstance *)module_inst, (AOTFunctionInstance *)function, + argc, argv); #endif return ret; } @@ -1448,7 +1447,8 @@ wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_create_exec_env_singleton((WASMModuleInstance *)module_inst); + return wasm_create_exec_env_singleton( + (WASMModuleInstance *)module_inst); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) @@ -1466,8 +1466,8 @@ wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst) #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return (WASMExecEnv *) - ((AOTModuleInstance *)module_inst)->exec_env_singleton.ptr; + return (WASMExecEnv *)((AOTModuleInstance *)module_inst) + ->exec_env_singleton.ptr; #endif return NULL; } @@ -1478,29 +1478,29 @@ wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - wasm_set_exception((WASMModuleInstance*)module_inst, exception); + wasm_set_exception((WASMModuleInstance *)module_inst, exception); return; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - aot_set_exception((AOTModuleInstance*)module_inst, exception); + aot_set_exception((AOTModuleInstance *)module_inst, exception); return; } #endif } -const char* +const char * wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - return wasm_get_exception((WASMModuleInstance*)module_inst); + return wasm_get_exception((WASMModuleInstance *)module_inst); } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - return aot_get_exception((AOTModuleInstance*)module_inst); + return aot_get_exception((AOTModuleInstance *)module_inst); } #endif return NULL; @@ -1518,13 +1518,13 @@ wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - ((WASMModuleInstance*)module_inst)->custom_data = custom_data; + ((WASMModuleInstance *)module_inst)->custom_data = custom_data; return; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - ((AOTModuleInstance*)module_inst)->custom_data.ptr = custom_data; + ((AOTModuleInstance *)module_inst)->custom_data.ptr = custom_data; return; } #endif @@ -1541,16 +1541,16 @@ wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst, #endif } -void* +void * wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return ((WASMModuleInstance*)module_inst)->custom_data; + return ((WASMModuleInstance *)module_inst)->custom_data; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return ((AOTModuleInstance*)module_inst)->custom_data.ptr; + return ((AOTModuleInstance *)module_inst)->custom_data.ptr; #endif return NULL; } @@ -1561,12 +1561,12 @@ wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_module_malloc((WASMModuleInstance*)module_inst, size, + return wasm_module_malloc((WASMModuleInstance *)module_inst, size, p_native_addr); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_module_malloc((AOTModuleInstance*)module_inst, size, + return aot_module_malloc((AOTModuleInstance *)module_inst, size, p_native_addr); #endif return 0; @@ -1578,13 +1578,13 @@ wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_module_realloc((WASMModuleInstance*)module_inst, ptr, - size, p_native_addr); + return wasm_module_realloc((WASMModuleInstance *)module_inst, ptr, size, + p_native_addr); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_module_realloc((AOTModuleInstance*)module_inst, ptr, - size, p_native_addr); + return aot_module_realloc((AOTModuleInstance *)module_inst, ptr, size, + p_native_addr); #endif return 0; } @@ -1594,13 +1594,13 @@ wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - wasm_module_free((WASMModuleInstance*)module_inst, ptr); + wasm_module_free((WASMModuleInstance *)module_inst, ptr); return; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - aot_module_free((AOTModuleInstance*)module_inst, ptr); + aot_module_free((AOTModuleInstance *)module_inst, ptr); return; } #endif @@ -1612,12 +1612,13 @@ wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - return wasm_module_dup_data((WASMModuleInstance*)module_inst, src, size); + return wasm_module_dup_data((WASMModuleInstance *)module_inst, src, + size); } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - return aot_module_dup_data((AOTModuleInstance*)module_inst, src, size); + return aot_module_dup_data((AOTModuleInstance *)module_inst, src, size); } #endif return 0; @@ -1629,12 +1630,12 @@ wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_validate_app_addr((WASMModuleInstance*)module_inst, + return wasm_validate_app_addr((WASMModuleInstance *)module_inst, app_offset, size); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_validate_app_addr((AOTModuleInstance*)module_inst, + return aot_validate_app_addr((AOTModuleInstance *)module_inst, app_offset, size); #endif return false; @@ -1647,8 +1648,8 @@ wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst, uint32 app_end_offset; char *str, *str_end; - if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset, - NULL, &app_end_offset)) + if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset, NULL, + &app_end_offset)) goto fail; str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset); @@ -1670,12 +1671,12 @@ wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_validate_native_addr((WASMModuleInstance*)module_inst, + return wasm_validate_native_addr((WASMModuleInstance *)module_inst, native_ptr, size); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_validate_native_addr((AOTModuleInstance*)module_inst, + return aot_validate_native_addr((AOTModuleInstance *)module_inst, native_ptr, size); #endif return false; @@ -1687,12 +1688,12 @@ wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_addr_app_to_native((WASMModuleInstance*)module_inst, + return wasm_addr_app_to_native((WASMModuleInstance *)module_inst, app_offset); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_addr_app_to_native((AOTModuleInstance*)module_inst, + return aot_addr_app_to_native((AOTModuleInstance *)module_inst, app_offset); #endif return NULL; @@ -1704,12 +1705,12 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_addr_native_to_app((WASMModuleInstance*)module_inst, + return wasm_addr_native_to_app((WASMModuleInstance *)module_inst, native_ptr); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_addr_native_to_app((AOTModuleInstance*)module_inst, + return aot_addr_native_to_app((AOTModuleInstance *)module_inst, native_ptr); #endif return 0; @@ -1717,19 +1718,18 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst, bool wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst, - uint32 app_offset, - uint32 *p_app_start_offset, + uint32 app_offset, uint32 *p_app_start_offset, uint32 *p_app_end_offset) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_get_app_addr_range((WASMModuleInstance*)module_inst, - app_offset, p_app_start_offset, - p_app_end_offset); + return wasm_get_app_addr_range((WASMModuleInstance *)module_inst, + app_offset, p_app_start_offset, + p_app_end_offset); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_get_app_addr_range((AOTModuleInstance*)module_inst, + return aot_get_app_addr_range((AOTModuleInstance *)module_inst, app_offset, p_app_start_offset, p_app_end_offset); #endif @@ -1744,13 +1744,13 @@ wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return wasm_get_native_addr_range((WASMModuleInstance*)module_inst, + return wasm_get_native_addr_range((WASMModuleInstance *)module_inst, native_ptr, p_native_start_addr, p_native_end_addr); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return aot_get_native_addr_range((AOTModuleInstance*)module_inst, + return aot_get_native_addr_range((AOTModuleInstance *)module_inst, native_ptr, p_native_start_addr, p_native_end_addr); #endif @@ -1762,11 +1762,11 @@ wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return ((WASMModuleInstance*)module_inst)->temp_ret; + return ((WASMModuleInstance *)module_inst)->temp_ret; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return ((AOTModuleInstance*)module_inst)->temp_ret; + return ((AOTModuleInstance *)module_inst)->temp_ret; #endif return 0; } @@ -1777,14 +1777,14 @@ wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - ((WASMModuleInstance*)module_inst)->temp_ret = temp_ret; + ((WASMModuleInstance *)module_inst)->temp_ret = temp_ret; return; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - ((AOTModuleInstance*)module_inst)->temp_ret = temp_ret; - return; + ((AOTModuleInstance *)module_inst)->temp_ret = temp_ret; + return; } #endif } @@ -1794,11 +1794,11 @@ wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return ((WASMModuleInstance*)module_inst)->llvm_stack; + return ((WASMModuleInstance *)module_inst)->llvm_stack; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return ((AOTModuleInstance*)module_inst)->llvm_stack; + return ((AOTModuleInstance *)module_inst)->llvm_stack; #endif return 0; } @@ -1809,14 +1809,14 @@ wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - ((WASMModuleInstance*)module_inst)->llvm_stack = llvm_stack; + ((WASMModuleInstance *)module_inst)->llvm_stack = llvm_stack; return; } #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - ((AOTModuleInstance*)module_inst)->llvm_stack = llvm_stack; - return; + ((AOTModuleInstance *)module_inst)->llvm_stack = llvm_stack; + return; } #endif } @@ -1827,13 +1827,12 @@ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, { #if WASM_ENABLE_INTERP != 0 if (module->module_type == Wasm_Module_Bytecode) - return wasm_enlarge_memory((WASMModuleInstance*)module, + return wasm_enlarge_memory((WASMModuleInstance *)module, inc_page_count); #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) - return aot_enlarge_memory((AOTModuleInstance*)module, - inc_page_count); + return aot_enlarge_memory((AOTModuleInstance *)module, inc_page_count); #endif return false; } @@ -1841,22 +1840,21 @@ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, #if WASM_ENABLE_LIBC_WASI != 0 void -wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, - const char *dir_list[], uint32 dir_count, - const char *map_dir_list[], uint32 map_dir_count, - const char *env_list[], uint32 env_count, - char *argv[], int argc, - int stdinfd, int stdoutfd, int stderrfd) +wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[], + uint32 dir_count, const char *map_dir_list[], + uint32 map_dir_count, const char *env_list[], + uint32 env_count, char *argv[], int argc, + int stdinfd, int stdoutfd, int stderrfd) { WASIArguments *wasi_args = NULL; #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0 if (module->module_type == Wasm_Module_Bytecode) - wasi_args = &((WASMModule*)module)->wasi_args; + wasi_args = &((WASMModule *)module)->wasi_args; #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) - wasi_args = &((AOTModule*)module)->wasi_args; + wasi_args = &((AOTModule *)module)->wasi_args; #endif if (wasi_args) { @@ -1875,18 +1873,14 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, } void -wasm_runtime_set_wasi_args(WASMModuleCommon *module, - const char *dir_list[], uint32 dir_count, - const char *map_dir_list[], uint32 map_dir_count, - const char *env_list[], uint32 env_count, - char *argv[], int argc) +wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[], + uint32 dir_count, const char *map_dir_list[], + uint32 map_dir_count, const char *env_list[], + uint32 env_count, char *argv[], int argc) { - wasm_runtime_set_wasi_args_ex(module, - dir_list, dir_count, - map_dir_list, map_dir_count, - env_list, env_count, - argv, argc, - -1, -1, -1); + wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list, + map_dir_count, env_list, env_count, argv, + argc, -1, -1, -1); } #if WASM_ENABLE_UVWASI == 0 @@ -1894,9 +1888,8 @@ bool wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, const char *dir_list[], uint32 dir_count, const char *map_dir_list[], uint32 map_dir_count, - const char *env[], uint32 env_count, - char *argv[], uint32 argc, - int stdinfd, int stdoutfd, int stderrfd, + const char *env[], uint32 env_count, char *argv[], + uint32 argc, int stdinfd, int stdoutfd, int stderrfd, char *error_buf, uint32 error_buf_size) { WASIContext *wasi_ctx; @@ -1916,8 +1909,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, char *path, resolved_path[PATH_MAX]; uint32 i; - if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, - error_buf, error_buf_size))) { + if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, error_buf, + error_buf_size))) { return false; } @@ -1925,13 +1918,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode - && !((WASMModuleInstance*)module_inst)->default_memory) + && !((WASMModuleInstance *)module_inst)->default_memory) return true; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT - && !((AOTModuleInstance*)module_inst)-> - global_table_data.memory_instances[0].memory_data.ptr) + && !((AOTModuleInstance *)module_inst) + ->global_table_data.memory_instances[0] + .memory_data.ptr) return true; #endif @@ -1941,11 +1935,11 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, total_size = sizeof(char *) * (uint64)argc; if (total_size >= UINT32_MAX - || (total_size > 0 && - !(argv_list = wasm_runtime_malloc((uint32)total_size))) + || (total_size > 0 + && !(argv_list = wasm_runtime_malloc((uint32)total_size))) || argv_buf_size >= UINT32_MAX - || (argv_buf_size > 0 && - !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) { + || (argv_buf_size > 0 + && !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: allocate memory failed"); goto fail; @@ -1983,7 +1977,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table))) || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats))) || !(argv_environ = - wasm_runtime_malloc(sizeof(struct argv_environ_values)))) { + wasm_runtime_malloc(sizeof(struct argv_environ_values)))) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: allocate memory failed"); goto fail; @@ -2005,11 +1999,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, } fd_prestats_inited = true; - if (!argv_environ_init(argv_environ, - argv_buf, argv_buf_size, - argv_list, argc, - env_buf, env_buf_size, - env_list, env_count)) { + if (!argv_environ_init(argv_environ, argv_buf, argv_buf_size, argv_list, + argc, env_buf, env_buf_size, env_list, env_count)) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: " "init argument environment failed"); @@ -2020,7 +2011,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */ if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0) || !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1) - || !fd_table_insert_existing(curfds, 2, (stderrfd != -1) ? stderrfd : 2)) { + || !fd_table_insert_existing(curfds, 2, + (stderrfd != -1) ? stderrfd : 2)) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: init fd table failed"); goto fail; @@ -2083,7 +2075,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, wasm_runtime_free(env_list); return false; } -#else /* else of WASM_ENABLE_UVWASI == 0 */ +#else /* else of WASM_ENABLE_UVWASI == 0 */ static void * wasm_uvwasi_malloc(size_t size, void *mem_user_data) { @@ -2100,8 +2092,7 @@ wasm_uvwasi_free(void *ptr, void *mem_user_data) } static void * -wasm_uvwasi_calloc(size_t nmemb, size_t size, - void *mem_user_data) +wasm_uvwasi_calloc(size_t nmemb, size_t size, void *mem_user_data) { uint64 total_size = (uint64)nmemb * size; return runtime_malloc(total_size, NULL, NULL, 0); @@ -2109,8 +2100,7 @@ wasm_uvwasi_calloc(size_t nmemb, size_t size, } static void * -wasm_uvwasi_realloc(void *ptr, size_t size, - void *mem_user_data) +wasm_uvwasi_realloc(void *ptr, size_t size, void *mem_user_data) { if (size >= UINT32_MAX) { return NULL; @@ -2118,6 +2108,7 @@ wasm_uvwasi_realloc(void *ptr, size_t size, return wasm_runtime_realloc(ptr, (uint32)size); } +/* clang-format off */ static uvwasi_mem_t uvwasi_allocator = { .mem_user_data = 0, .malloc = wasm_uvwasi_malloc, @@ -2125,14 +2116,14 @@ static uvwasi_mem_t uvwasi_allocator = { .calloc = wasm_uvwasi_calloc, .realloc = wasm_uvwasi_realloc }; +/* clang-format on */ bool wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, const char *dir_list[], uint32 dir_count, const char *map_dir_list[], uint32 map_dir_count, - const char *env[], uint32 env_count, - char *argv[], uint32 argc, - int stdinfd, int stdoutfd, int stderrfd, + const char *env[], uint32 env_count, char *argv[], + uint32 argc, int stdinfd, int stdoutfd, int stderrfd, char *error_buf, uint32 error_buf_size) { uvwasi_t *uvwasi = NULL; @@ -2142,8 +2133,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, uint32 i; bool ret = false; - uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, - error_buf, error_buf_size); + uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, error_buf, + error_buf_size); if (!uvwasi) return false; @@ -2153,30 +2144,31 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, init_options.argc = argc; init_options.argv = (const char **)argv; init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in; - init_options.out = (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out; - init_options.err = (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err; + init_options.out = + (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out; + init_options.err = + (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err; if (dir_count > 0) { init_options.preopenc = dir_count; total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc; - init_options.preopens = - (uvwasi_preopen_t *)runtime_malloc(total_size, module_inst, - error_buf, error_buf_size); + init_options.preopens = (uvwasi_preopen_t *)runtime_malloc( + total_size, module_inst, error_buf, error_buf_size); if (init_options.preopens == NULL) goto fail; for (i = 0; i < init_options.preopenc; i++) { init_options.preopens[i].real_path = dir_list[i]; init_options.preopens[i].mapped_path = - (i < map_dir_count) ? map_dir_list[i] : dir_list[i]; + (i < map_dir_count) ? map_dir_list[i] : dir_list[i]; } } if (env_count > 0) { total_size = sizeof(char *) * (uint64)(env_count + 1); - envp = runtime_malloc(total_size, module_inst, - error_buf, error_buf_size); + envp = + runtime_malloc(total_size, module_inst, error_buf, error_buf_size); if (envp == NULL) goto fail; @@ -2198,7 +2190,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, fail: if (envp) - wasm_runtime_free((void*)envp); + wasm_runtime_free((void *)envp); if (init_options.preopens) wasm_runtime_free(init_options.preopens); @@ -2215,13 +2207,13 @@ wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode - && ((WASMModuleInstance*)module_inst)->module->is_wasi_module) + && ((WASMModuleInstance *)module_inst)->module->is_wasi_module) return true; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT - && ((AOTModule*)((AOTModuleInstance*)module_inst)->aot_module.ptr) - ->is_wasi_module) + && ((AOTModule *)((AOTModuleInstance *)module_inst)->aot_module.ptr) + ->is_wasi_module) return true; #endif return false; @@ -2234,7 +2226,7 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst) #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { - WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst; + WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst; WASMFunctionInstance *func; for (i = 0; i < wasm_inst->export_func_count; i++) { if (!strcmp(wasm_inst->export_functions[i].name, "_start")) { @@ -2245,7 +2237,7 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst) "invalid function type.\n"); return NULL; } - return (WASMFunctionInstanceCommon*)func; + return (WASMFunctionInstanceCommon *)func; } } return NULL; @@ -2254,9 +2246,9 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst) #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { - AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst; - AOTFunctionInstance *export_funcs = (AOTFunctionInstance *) - aot_inst->export_funcs.ptr; + AOTModuleInstance *aot_inst = (AOTModuleInstance *)module_inst; + AOTFunctionInstance *export_funcs = + (AOTFunctionInstance *)aot_inst->export_funcs.ptr; for (i = 0; i < aot_inst->export_func_count; i++) { if (!strcmp(export_funcs[i].func_name, "_start")) { AOTFuncType *func_type = export_funcs[i].u.func.func_type; @@ -2266,7 +2258,7 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst) "invalid function type.\n"); return NULL; } - return (WASMFunctionInstanceCommon*)&export_funcs[i]; + return (WASMFunctionInstanceCommon *)&export_funcs[i]; } } return NULL; @@ -2324,11 +2316,11 @@ wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return ((WASMModuleInstance*)module_inst)->wasi_ctx; + return ((WASMModuleInstance *)module_inst)->wasi_ctx; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr; + return ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr; #endif return NULL; } @@ -2339,29 +2331,28 @@ wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst, { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - ((WASMModuleInstance*)module_inst)->wasi_ctx = wasi_ctx; + ((WASMModuleInstance *)module_inst)->wasi_ctx = wasi_ctx; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr = wasi_ctx; + ((AOTModuleInstance *)module_inst)->wasi_ctx.ptr = wasi_ctx; #endif } #endif /* end of WASM_ENABLE_LIBC_WASI */ -WASMModuleCommon* +WASMModuleCommon * wasm_exec_env_get_module(WASMExecEnv *exec_env) { WASMModuleInstanceCommon *module_inst = wasm_runtime_get_module_inst(exec_env); #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - return (WASMModuleCommon*) - ((WASMModuleInstance*)module_inst)->module; + return (WASMModuleCommon *)((WASMModuleInstance *)module_inst)->module; #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - return (WASMModuleCommon*) - ((AOTModuleInstance*)module_inst)->aot_module.ptr; + return (WASMModuleCommon *)((AOTModuleInstance *)module_inst) + ->aot_module.ptr; #endif return NULL; } @@ -2378,8 +2369,8 @@ wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, uint32 n_native_symbols) { - return wasm_native_register_natives(module_name, - native_symbols, n_native_symbols); + return wasm_native_register_natives(module_name, native_symbols, + n_native_symbols); } bool @@ -2387,18 +2378,18 @@ wasm_runtime_register_natives_raw(const char *module_name, NativeSymbol *native_symbols, uint32 n_native_symbols) { - return wasm_native_register_natives_raw(module_name, - native_symbols, n_native_symbols); + return wasm_native_register_natives_raw(module_name, native_symbols, + n_native_symbols); } bool wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *argv_ret) + void *attachment, uint32 *argv, uint32 argc, + uint32 *argv_ret) { WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env); - typedef void (*NativeRawFuncPtr)(WASMExecEnv*, uint64*); + typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *); NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr; uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size; uint32 *argv_src = argv, i, argc1, ptr_len; @@ -2408,8 +2399,8 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, argc1 = func_type->param_count; if (argc1 > sizeof(argv_buf) / sizeof(uint64)) { size = sizeof(uint64) * (uint64)argc1; - if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, - NULL, 0))) { + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL, + 0))) { return false; } } @@ -2421,7 +2412,7 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, switch (func_type->types[i]) { case VALUE_TYPE_I32: { - *(uint32*)argv_dst = arg_i32 = *argv_src++; + *(uint32 *)argv_dst = arg_i32 = *argv_src++; if (signature) { if (signature[i + 1] == '*') { /* param is a pointer */ @@ -2432,35 +2423,40 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, /* pointer without length followed */ ptr_len = 1; - if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len)) + if (!wasm_runtime_validate_app_addr(module, arg_i32, + ptr_len)) goto fail; - *(uintptr_t*)argv_dst = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + *(uintptr_t *)argv_dst = + (uintptr_t)wasm_runtime_addr_app_to_native(module, + arg_i32); } else if (signature[i + 1] == '$') { /* param is a string */ - if (!wasm_runtime_validate_app_str_addr(module, arg_i32)) + if (!wasm_runtime_validate_app_str_addr(module, + arg_i32)) goto fail; - *(uintptr_t*)argv_dst = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + *(uintptr_t *)argv_dst = + (uintptr_t)wasm_runtime_addr_app_to_native(module, + arg_i32); } } break; } case VALUE_TYPE_I64: case VALUE_TYPE_F64: - bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, sizeof(uint32) * 2); + bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, + sizeof(uint32) * 2); argv_src += 2; break; case VALUE_TYPE_F32: - *(float32*)argv_dst = *(float32*)argv_src++; + *(float32 *)argv_dst = *(float32 *)argv_src++; break; #if WASM_ENABLE_REF_TYPES != 0 case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: - *(uint32*)argv_dst = *argv_src++; + *(uint32 *)argv_dst = *argv_src++; break; #endif default: @@ -2480,14 +2476,15 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: #endif - argv_ret[0] = *(uint32*)argv1; + argv_ret[0] = *(uint32 *)argv1; break; case VALUE_TYPE_F32: - *(float32*)argv_ret = *(float32*)argv1; + *(float32 *)argv_ret = *(float32 *)argv1; break; case VALUE_TYPE_I64: case VALUE_TYPE_F64: - bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, sizeof(uint64)); + bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, + sizeof(uint64)); break; default: bh_assert(0); @@ -2500,7 +2497,7 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, fail: if (argv1 != argv_buf) wasm_runtime_free(argv1); - return ret; + return ret; } /** @@ -2508,39 +2505,40 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, */ /* The invoke native implementation on ARM platform with VFP co-processor */ -#if defined(BUILD_TARGET_ARM_VFP) \ - || defined(BUILD_TARGET_THUMB_VFP) \ - || defined(BUILD_TARGET_RISCV32_ILP32D) \ - || defined(BUILD_TARGET_RISCV32_ILP32) \ - || defined(BUILD_TARGET_ARC) +#if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) \ + || defined(BUILD_TARGET_RISCV32_ILP32D) \ + || defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_ARC) typedef void (*GenericFunctionPointer)(); -int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks); - -typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32*, uint32); -typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32*, uint32); -typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32*,uint32); -typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32*, uint32); -typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32*, uint32); - -static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative; -static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative; +int64 +invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks); + +typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32 *, uint32); +typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32 *, uint32); +typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32 *, uint32); +typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32 *, uint32); +typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32 *, uint32); + +static Float64FuncPtr invokeNative_Float64 = + (Float64FuncPtr)(uintptr_t)invokeNative; +static Float32FuncPtr invokeNative_Float32 = + (Float32FuncPtr)(uintptr_t)invokeNative; static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative; static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative; static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative; #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) -#define MAX_REG_INTS 4 +#define MAX_REG_INTS 4 #define MAX_REG_FLOATS 16 #else -#define MAX_REG_INTS 8 +#define MAX_REG_INTS 8 #define MAX_REG_FLOATS 8 #endif bool wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *argv_ret) + void *attachment, uint32 *argv, uint32 argc, + uint32 *argv_ret) { WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env); /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */ @@ -2582,8 +2580,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, #endif n_ints += 2; } -#if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D) \ - || defined(BUILD_TARGET_ARC) +#if defined(BUILD_TARGET_RISCV32_ILP32) \ + || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC) /* part in register, part in stack */ else if (n_ints == MAX_REG_INTS - 1) { n_ints++; @@ -2632,7 +2630,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, n_stacks += 2; } break; -#else /* BUILD_TARGET_RISCV32_ILP32D */ +#else /* BUILD_TARGET_RISCV32_ILP32D */ case VALUE_TYPE_F32: case VALUE_TYPE_F64: if (n_fps < MAX_REG_FLOATS) { @@ -2651,7 +2649,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, ints += 2; } else { - /* 64-bit data in stack must be 8 bytes aligned in riscv32 */ + /* 64-bit data in stack must be 8 bytes aligned in riscv32 + */ if (n_stacks & 1) n_stacks++; n_stacks += 2; @@ -2681,8 +2680,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, if (argc1 > sizeof(argv_buf) / sizeof(uint32)) { size = sizeof(uint32) * (uint32)argc1; - if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, - NULL, 0))) { + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL, + 0))) { return false; } } @@ -2720,19 +2719,21 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, /* pointer without length followed */ ptr_len = 1; - if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len)) + if (!wasm_runtime_validate_app_addr(module, arg_i32, + ptr_len)) goto fail; - arg_i32 = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native( + module, arg_i32); } else if (signature[i + 1] == '$') { /* param is a string */ - if (!wasm_runtime_validate_app_str_addr(module, arg_i32)) + if (!wasm_runtime_validate_app_str_addr(module, + arg_i32)) goto fail; - arg_i32 = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native( + module, arg_i32); } } @@ -2764,8 +2765,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, ints[n_ints++] = *argv_src++; ints[n_ints++] = *argv_src++; } -#if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D) \ - || defined(BUILD_TARGET_ARC) +#if defined(BUILD_TARGET_RISCV32_ILP32) \ + || defined(BUILD_TARGET_RISCV32_ILP32D) || defined(BUILD_TARGET_ARC) else if (n_ints == MAX_REG_INTS - 1) { ints[n_ints++] = *argv_src++; stacks[n_stacks++] = *argv_src++; @@ -2787,9 +2788,9 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, case VALUE_TYPE_F32: { if (n_fps < MAX_REG_FLOATS) - *(float32*)&fps[n_fps++] = *(float32*)argv_src++; + *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++; else - *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++; + *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++; break; } case VALUE_TYPE_F64: @@ -2821,19 +2822,19 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, } break; } -#else /* BUILD_TARGET_RISCV32_ILP32D */ +#else /* BUILD_TARGET_RISCV32_ILP32D */ case VALUE_TYPE_F32: case VALUE_TYPE_F64: { if (n_fps < MAX_REG_FLOATS) { if (func_type->types[i] == VALUE_TYPE_F32) { - *(float32*)&fps[n_fps * 2] = *(float32*)argv_src++; + *(float32 *)&fps[n_fps * 2] = *(float32 *)argv_src++; /* NaN boxing, the upper bits of a valid NaN-boxed value must be all 1s. */ fps[n_fps * 2 + 1] = 0xFFFFFFFF; } else { - *(float64*)&fps[n_fps * 2] = *(float64*)argv_src; + *(float64 *)&fps[n_fps * 2] = *(float64 *)argv_src; argv_src += 2; } n_fps++; @@ -2841,29 +2842,30 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, else if (func_type->types[i] == VALUE_TYPE_F32 && n_ints < MAX_REG_INTS) { /* use int reg firstly if available */ - *(float32*)&ints[n_ints++] = *(float32*)argv_src++; + *(float32 *)&ints[n_ints++] = *(float32 *)argv_src++; } else if (func_type->types[i] == VALUE_TYPE_F64 && n_ints < MAX_REG_INTS - 1) { /* use int regs firstly if available */ if (n_ints & 1) n_ints++; - *(float64*)&ints[n_ints] = *(float64*)argv_src; + *(float64 *)&ints[n_ints] = *(float64 *)argv_src; n_ints += 2; argv_src += 2; } else { - /* 64-bit data in stack must be 8 bytes aligned in riscv32 */ + /* 64-bit data in stack must be 8 bytes aligned in riscv32 + */ if (n_stacks & 1) n_stacks++; if (func_type->types[i] == VALUE_TYPE_F32) { - *(float32*)&stacks[n_stacks] = *(float32*)argv_src++; + *(float32 *)&stacks[n_stacks] = *(float32 *)argv_src++; /* NaN boxing, the upper bits of a valid NaN-boxed value must be all 1s. */ stacks[n_stacks + 1] = 0xFFFFFFFF; } else { - *(float64*)&stacks[n_stacks] = *(float64*)argv_src; + *(float64 *)&stacks[n_stacks] = *(float64 *)argv_src; argv_src += 2; } n_stacks += 2; @@ -2880,9 +2882,9 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, /* Save extra result values' address to argv1 */ for (i = 0; i < ext_ret_count; i++) { if (n_ints < MAX_REG_INTS) - ints[n_ints++] = *(uint32*)argv_src++; + ints[n_ints++] = *(uint32 *)argv_src++; else - stacks[n_stacks++] = *(uint32*)argv_src++; + stacks[n_stacks++] = *(uint32 *)argv_src++; } exec_env->attachment = attachment; @@ -2896,16 +2898,20 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: #endif - argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks); + argv_ret[0] = + (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks); break; case VALUE_TYPE_I64: - PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks)); + PUT_I64_TO_ADDR(argv_ret, + invokeNative_Int64(func_ptr, argv1, n_stacks)); break; case VALUE_TYPE_F32: - *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks); + *(float32 *)argv_ret = + invokeNative_Float32(func_ptr, argv1, n_stacks); break; case VALUE_TYPE_F64: - PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks)); + PUT_F64_TO_ADDR( + argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks)); break; default: bh_assert(0); @@ -2921,25 +2927,24 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, wasm_runtime_free(argv1); return ret; } -#endif /* end of defined(BUILD_TARGET_ARM_VFP) - || defined(BUILD_TARGET_THUMB_VFP) \ - || defined(BUILD_TARGET_RISCV32_ILP32D) - || defined(BUILD_TARGET_RISCV32_ILP32) +#endif /* end of defined(BUILD_TARGET_ARM_VFP) \ + || defined(BUILD_TARGET_THUMB_VFP) \ + || defined(BUILD_TARGET_RISCV32_ILP32D) \ + || defined(BUILD_TARGET_RISCV32_ILP32) \ || defined(BUILD_TARGET_ARC) */ -#if defined(BUILD_TARGET_X86_32) \ - || defined(BUILD_TARGET_ARM) \ - || defined(BUILD_TARGET_THUMB) \ - || defined(BUILD_TARGET_MIPS) \ +#if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_ARM) \ + || defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_MIPS) \ || defined(BUILD_TARGET_XTENSA) typedef void (*GenericFunctionPointer)(); -int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz); +int64 +invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz); -typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32); -typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32); -typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32); -typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32); -typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32); +typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32); +typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32); +typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32 *, uint32); +typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32 *, uint32); +typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32 *, uint32); static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative; static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative; @@ -2957,8 +2962,8 @@ word_copy(uint32 *dest, uint32 *src, unsigned num) bool wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *argv_ret) + void *attachment, uint32 *argv, uint32 argc, + uint32 *argv_ret) { WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env); uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0; @@ -2978,14 +2983,14 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, if (argc1 > sizeof(argv_buf) / sizeof(uint32)) { size = sizeof(uint32) * (uint64)argc1; - if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, - NULL, 0))) { + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL, + 0))) { return false; } } - for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++) - argv1[j++] = ((uint32*)&exec_env)[i]; + for (i = 0; i < sizeof(WASMExecEnv *) / sizeof(uint32); i++) + argv1[j++] = ((uint32 *)&exec_env)[i]; for (i = 0; i < func_type->param_count; i++) { switch (func_type->types[i]) { @@ -3003,19 +3008,21 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, /* pointer without length followed */ ptr_len = 1; - if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len)) + if (!wasm_runtime_validate_app_addr(module, arg_i32, + ptr_len)) goto fail; - arg_i32 = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native( + module, arg_i32); } else if (signature[i + 1] == '$') { /* param is a string */ - if (!wasm_runtime_validate_app_str_addr(module, arg_i32)) + if (!wasm_runtime_validate_app_str_addr(module, + arg_i32)) goto fail; - arg_i32 = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + arg_i32 = (uintptr_t)wasm_runtime_addr_app_to_native( + module, arg_i32); } } @@ -3061,16 +3068,20 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: #endif - argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1); + argv_ret[0] = + (uint32)invokeNative_Int32(func_ptr, argv1, argc1); break; case VALUE_TYPE_I64: - PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1)); + PUT_I64_TO_ADDR(argv_ret, + invokeNative_Int64(func_ptr, argv1, argc1)); break; case VALUE_TYPE_F32: - *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1); + *(float32 *)argv_ret = + invokeNative_Float32(func_ptr, argv1, argc1); break; case VALUE_TYPE_F64: - PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1)); + PUT_F64_TO_ADDR(argv_ret, + invokeNative_Float64(func_ptr, argv1, argc1)); break; default: bh_assert(0); @@ -3087,17 +3098,15 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, return ret; } -#endif /* end of defined(BUILD_TARGET_X86_32) \ - || defined(BUILD_TARGET_ARM) \ +#endif /* end of defined(BUILD_TARGET_X86_32) \ + || defined(BUILD_TARGET_ARM) \ || defined(BUILD_TARGET_THUMB) \ - || defined(BUILD_TARGET_MIPS) \ + || defined(BUILD_TARGET_MIPS) \ || defined(BUILD_TARGET_XTENSA) */ -#if defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_AARCH64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ - || defined(BUILD_TARGET_RISCV64_LP64) +#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ + || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ + || defined(BUILD_TARGET_RISCV64_LP64) #if WASM_ENABLE_SIMD != 0 #ifdef v128 @@ -3116,9 +3125,10 @@ typedef union __declspec(intrin_type) __declspec(align(8)) v128 { unsigned __int64 m128i_u64[2]; } v128; #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64) -typedef long long v128 __attribute__ ((__vector_size__ (16), - __may_alias__, __aligned__ (1))); + || defined(BUILD_TARGET_RISCV64_LP64D) \ + || defined(BUILD_TARGET_RISCV64_LP64) +typedef long long v128 + __attribute__((__vector_size__(16), __may_alias__, __aligned__(1))); #elif defined(BUILD_TARGET_AARCH64) #include typedef uint32x4_t __m128i; @@ -3128,37 +3138,39 @@ typedef uint32x4_t __m128i; #endif /* end of WASM_ENABLE_SIMD != 0 */ typedef void (*GenericFunctionPointer)(); -int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks); - -typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64); -typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64); -typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*, uint64); -typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64); -typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64); - -static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative; -static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative; +int64 +invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks); + +typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64 *, uint64); +typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64 *, uint64); +typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64); +typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64); +typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64); + +static Float64FuncPtr invokeNative_Float64 = + (Float64FuncPtr)(uintptr_t)invokeNative; +static Float32FuncPtr invokeNative_Float32 = + (Float32FuncPtr)(uintptr_t)invokeNative; static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative; static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative; static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative; #if WASM_ENABLE_SIMD != 0 -typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64*, uint64); +typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64); static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative; #endif #if defined(_WIN32) || defined(_WIN32_) -#define MAX_REG_FLOATS 4 -#define MAX_REG_INTS 4 +#define MAX_REG_FLOATS 4 +#define MAX_REG_INTS 4 #else /* else of defined(_WIN32) || defined(_WIN32_) */ -#define MAX_REG_FLOATS 8 -#if defined(BUILD_TARGET_AARCH64) \ - || defined(BUILD_TARGET_RISCV64_LP64D) \ +#define MAX_REG_FLOATS 8 +#if defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) -#define MAX_REG_INTS 8 +#define MAX_REG_INTS 8 #else -#define MAX_REG_INTS 6 -#endif /* end of defined(BUILD_TARGET_AARCH64) \ +#define MAX_REG_INTS 6 +#endif /* end of defined(BUILD_TARGET_AARCH64) \ || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) */ #endif /* end of defined(_WIN32) || defined(_WIN32_) */ @@ -3166,8 +3178,8 @@ static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative; bool wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *argv_ret) + void *attachment, uint32 *argv, uint32 argc, + uint32 *argv_ret) { WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env); uint64 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size, arg_i64; @@ -3194,16 +3206,15 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, #endif #if WASM_ENABLE_SIMD == 0 - argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count - + ext_ret_count; + argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count + ext_ret_count; #else argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2 - + ext_ret_count; + + ext_ret_count; #endif if (argc1 > sizeof(argv_buf) / sizeof(uint64)) { size = sizeof(uint64) * (uint64)argc1; - if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, - NULL, 0))) { + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, NULL, + 0))) { return false; } } @@ -3216,7 +3227,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, fps = (v128 *)argv1; ints = (uint64 *)(fps + MAX_REG_FLOATS); #endif -#else /* else of BUILD_TARGET_RISCV64_LP64 */ +#else /* else of BUILD_TARGET_RISCV64_LP64 */ ints = argv1; #endif /* end of BUILD_TARGET_RISCV64_LP64 */ stacks = ints + MAX_REG_INTS; @@ -3239,19 +3250,21 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, /* pointer without length followed */ ptr_len = 1; - if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len)) + if (!wasm_runtime_validate_app_addr(module, arg_i32, + ptr_len)) goto fail; - arg_i64 = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native( + module, arg_i32); } else if (signature[i + 1] == '$') { /* param is a string */ - if (!wasm_runtime_validate_app_str_addr(module, arg_i32)) + if (!wasm_runtime_validate_app_str_addr(module, + arg_i32)) goto fail; - arg_i64 = (uintptr_t) - wasm_runtime_addr_app_to_native(module, arg_i32); + arg_i64 = (uintptr_t)wasm_runtime_addr_app_to_native( + module, arg_i32); } } if (n_ints < MAX_REG_INTS) @@ -3262,25 +3275,25 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, } case VALUE_TYPE_I64: if (n_ints < MAX_REG_INTS) - ints[n_ints++] = *(uint64*)argv_src; + ints[n_ints++] = *(uint64 *)argv_src; else - stacks[n_stacks++] = *(uint64*)argv_src; + stacks[n_stacks++] = *(uint64 *)argv_src; argv_src += 2; break; case VALUE_TYPE_F32: if (n_fps < MAX_REG_FLOATS) { - *(float32*)&fps[n_fps++] = *(float32*)argv_src++; + *(float32 *)&fps[n_fps++] = *(float32 *)argv_src++; } else { - *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++; + *(float32 *)&stacks[n_stacks++] = *(float32 *)argv_src++; } break; case VALUE_TYPE_F64: if (n_fps < MAX_REG_FLOATS) { - *(float64*)&fps[n_fps++] = *(float64*)argv_src; + *(float64 *)&fps[n_fps++] = *(float64 *)argv_src; } else { - *(float64*)&stacks[n_stacks++] = *(float64*)argv_src; + *(float64 *)&stacks[n_stacks++] = *(float64 *)argv_src; } argv_src += 2; break; @@ -3296,10 +3309,10 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, #if WASM_ENABLE_SIMD != 0 case VALUE_TYPE_V128: if (n_fps < MAX_REG_FLOATS) { - *(v128*)&fps[n_fps++] = *(v128*)argv_src; + *(v128 *)&fps[n_fps++] = *(v128 *)argv_src; } else { - *(v128*)&stacks[n_stacks++] = *(v128*)argv_src; + *(v128 *)&stacks[n_stacks++] = *(v128 *)argv_src; n_stacks++; } argv_src += 4; @@ -3314,9 +3327,9 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, /* Save extra result values' address to argv1 */ for (i = 0; i < ext_ret_count; i++) { if (n_ints < MAX_REG_INTS) - ints[n_ints++] = *(uint64*)argv_src; + ints[n_ints++] = *(uint64 *)argv_src; else - stacks[n_stacks++] = *(uint64*)argv_src; + stacks[n_stacks++] = *(uint64 *)argv_src; argv_src += 2; } @@ -3332,20 +3345,25 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, case VALUE_TYPE_FUNCREF: case VALUE_TYPE_EXTERNREF: #endif - argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks); + argv_ret[0] = + (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks); break; case VALUE_TYPE_I64: - PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks)); + PUT_I64_TO_ADDR(argv_ret, + invokeNative_Int64(func_ptr, argv1, n_stacks)); break; case VALUE_TYPE_F32: - *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks); + *(float32 *)argv_ret = + invokeNative_Float32(func_ptr, argv1, n_stacks); break; case VALUE_TYPE_F64: - PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks)); + PUT_F64_TO_ADDR( + argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks)); break; #if WASM_ENABLE_SIMD != 0 case VALUE_TYPE_V128: - *(v128*)argv_ret = invokeNative_V128(func_ptr, argv1, n_stacks); + *(v128 *)argv_ret = + invokeNative_V128(func_ptr, argv1, n_stacks); break; #endif default: @@ -3363,15 +3381,14 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, return ret; } -#endif /* end of defined(BUILD_TARGET_X86_64) \ - || defined(BUILD_TARGET_AMD_64) \ - || defined(BUILD_TARGET_AARCH64) \ +#endif /* end of defined(BUILD_TARGET_X86_64) \ + || defined(BUILD_TARGET_AMD_64) \ + || defined(BUILD_TARGET_AARCH64) \ || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) */ bool -wasm_runtime_call_indirect(WASMExecEnv *exec_env, - uint32_t element_indices, +wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32_t element_indices, uint32_t argc, uint32_t argv[]) { if (!wasm_runtime_exec_env_check(exec_env)) { @@ -3427,8 +3444,8 @@ wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2) bh_memcpy_s(&u2, 8, bytes + 8, 8); if (!is_little_endian()) { - exchange_uint64((uint8*)&u1); - exchange_uint64((uint8*)&u2); + exchange_uint64((uint8 *)&u1); + exchange_uint64((uint8 *)&u2); *ret1 = u2; *ret2 = u1; } @@ -3457,7 +3474,7 @@ wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env) wasm_cluster_destroy_spawned_exec_env(exec_env); } -static void* +static void * wasm_runtime_thread_routine(void *arg) { WASMThreadArg *thread_arg = (WASMThreadArg *)arg; @@ -3550,13 +3567,11 @@ wasm_externref_map_init() if (os_mutex_init(&externref_lock) != 0) return false; - if (!(externref_map = bh_hash_map_create(32, false, - wasm_externref_hash, - wasm_externref_equal, - NULL, + if (!(externref_map = bh_hash_map_create(32, false, wasm_externref_hash, + wasm_externref_equal, NULL, wasm_runtime_free))) { - os_mutex_destroy(&externref_lock); - return false; + os_mutex_destroy(&externref_lock); + return false; } externref_global_id = 1; @@ -3581,8 +3596,8 @@ lookup_extobj_callback(void *key, void *value, void *user_data) { uint32 externref_idx = (uint32)(uintptr_t)key; ExternRefMapNode *node = (ExternRefMapNode *)value; - LookupExtObj_UserData *user_data_lookup = (LookupExtObj_UserData *) - user_data; + LookupExtObj_UserData *user_data_lookup = + (LookupExtObj_UserData *)user_data; if (node->extern_obj == user_data_lookup->node.extern_obj && node->module_inst == user_data_lookup->node.module_inst) { @@ -3592,8 +3607,8 @@ lookup_extobj_callback(void *key, void *value, void *user_data) } bool -wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, - void *extern_obj, uint32 *p_externref_idx) +wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj, + uint32 *p_externref_idx) { LookupExtObj_UserData lookup_user_data; ExternRefMapNode *node; @@ -3607,7 +3622,7 @@ wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, /* Lookup hashmap firstly */ bh_hash_map_traverse(externref_map, lookup_extobj_callback, - (void*)&lookup_user_data); + (void *)&lookup_user_data); if (lookup_user_data.found) { *p_externref_idx = lookup_user_data.externref_idx; os_mutex_unlock(&externref_lock); @@ -3615,8 +3630,7 @@ wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, } /* Not found in hashmap */ - if (externref_global_id == NULL_REF - || externref_global_id == 0) { + if (externref_global_id == NULL_REF || externref_global_id == 0) { goto fail1; } @@ -3630,9 +3644,8 @@ wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, externref_idx = externref_global_id; - if (!bh_hash_map_insert(externref_map, - (void*)(uintptr_t)externref_idx, - (void*)node)) { + if (!bh_hash_map_insert(externref_map, (void *)(uintptr_t)externref_idx, + (void *)node)) { goto fail2; } @@ -3657,8 +3670,7 @@ wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj) } os_mutex_lock(&externref_lock); - node = bh_hash_map_find(externref_map, - (void*)(uintptr_t)externref_idx); + node = bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx); os_mutex_unlock(&externref_lock); if (!node) @@ -3672,8 +3684,8 @@ static void reclaim_extobj_callback(void *key, void *value, void *user_data) { ExternRefMapNode *node = (ExternRefMapNode *)value; - WASMModuleInstanceCommon *module_inst = (WASMModuleInstanceCommon *) - user_data; + WASMModuleInstanceCommon *module_inst = + (WASMModuleInstanceCommon *)user_data; if (node->module_inst == module_inst) { if (!node->marked && !node->retained) { @@ -3692,8 +3704,8 @@ mark_externref(uint32 externref_idx) ExternRefMapNode *node; if (externref_idx != NULL_REF) { - node = bh_hash_map_find(externref_map, - (void*)(uintptr_t)externref_idx); + node = + bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx); if (node) { node->marked = true; } @@ -3712,7 +3724,7 @@ interp_mark_all_externrefs(WASMModuleInstance *module_inst) global = module_inst->globals; for (i = 0; i < module_inst->global_count; i++, global++) { if (global->type == VALUE_TYPE_EXTERNREF) { - externref_idx = *(uint32*)(global_data + global->data_offset); + externref_idx = *(uint32 *)(global_data + global->data_offset); mark_externref(externref_idx); } } @@ -3739,7 +3751,7 @@ aot_mark_all_externrefs(AOTModuleInstance *module_inst) const AOTTable *table = module->tables; const AOTGlobal *global = module->globals; const AOTTableInstance *table_inst = - (AOTTableInstance *)module_inst->tables.ptr; + (AOTTableInstance *)module_inst->tables.ptr; for (i = 0; i < module->global_count; i++, global++) { if (global->type == VALUE_TYPE_EXTERNREF) { @@ -3766,15 +3778,15 @@ wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst) os_mutex_lock(&externref_lock); #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) - interp_mark_all_externrefs((WASMModuleInstance*)module_inst); + interp_mark_all_externrefs((WASMModuleInstance *)module_inst); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) - aot_mark_all_externrefs((AOTModuleInstance*)module_inst); + aot_mark_all_externrefs((AOTModuleInstance *)module_inst); #endif bh_hash_map_traverse(externref_map, reclaim_extobj_callback, - (void*)module_inst); + (void *)module_inst); os_mutex_unlock(&externref_lock); } @@ -3782,8 +3794,8 @@ static void cleanup_extobj_callback(void *key, void *value, void *user_data) { ExternRefMapNode *node = (ExternRefMapNode *)value; - WASMModuleInstanceCommon *module_inst = (WASMModuleInstanceCommon *) - user_data; + WASMModuleInstanceCommon *module_inst = + (WASMModuleInstanceCommon *)user_data; if (node->module_inst == module_inst) { bh_hash_map_remove(externref_map, key, NULL, NULL); @@ -3796,7 +3808,7 @@ wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst) { os_mutex_lock(&externref_lock); bh_hash_map_traverse(externref_map, cleanup_extobj_callback, - (void*)module_inst); + (void *)module_inst); os_mutex_unlock(&externref_lock); } @@ -3808,8 +3820,8 @@ wasm_externref_retain(uint32 externref_idx) os_mutex_lock(&externref_lock); if (externref_idx != NULL_REF) { - node = bh_hash_map_find(externref_map, - (void*)(uintptr_t)externref_idx); + node = + bh_hash_map_find(externref_map, (void *)(uintptr_t)externref_idx); if (node) { node->retained = true; os_mutex_unlock(&externref_lock); @@ -3826,8 +3838,8 @@ wasm_externref_retain(uint32 externref_idx) void wasm_runtime_dump_call_stack(WASMExecEnv *exec_env) { - WASMModuleInstanceCommon *module_inst - = wasm_exec_env_get_module_inst(exec_env); + WASMModuleInstanceCommon *module_inst = + wasm_exec_env_get_module_inst(exec_env); #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { wasm_interp_dump_call_stack(exec_env); @@ -3843,21 +3855,19 @@ wasm_runtime_dump_call_stack(WASMExecEnv *exec_env) bool wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm, - const WASMExport *export, - WASMType **out) + const WASMExport *export, WASMType **out) { #if WASM_ENABLE_INTERP != 0 if (module_comm->module_type == Wasm_Module_Bytecode) { WASMModule *module = (WASMModule *)module_comm; if (export->index < module->import_function_count) { - *out = - module->import_functions[export->index].u.function.func_type; + *out = module->import_functions[export->index].u.function.func_type; } else { *out = - module->functions[export->index - module->import_function_count] - ->func_type; + module->functions[export->index - module->import_function_count] + ->func_type; } return true; } @@ -3869,12 +3879,12 @@ wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm, if (export->index < module->import_func_count) { *out = module->func_types[module->import_funcs[export->index] - .func_type_index]; + .func_type_index]; } else { - *out = - module->func_types[module->func_type_indexes - [export->index - module->import_func_count]]; + *out = module->func_types + [module->func_type_indexes[export->index + - module->import_func_count]]; } return true; } @@ -3885,8 +3895,7 @@ wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm, bool wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm, const WASMExport *export, - uint8 *out_val_type, - bool *out_mutability) + uint8 *out_val_type, bool *out_mutability) { #if WASM_ENABLE_INTERP != 0 if (module_comm->module_type == Wasm_Module_Bytecode) { @@ -3894,13 +3903,13 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm, if (export->index < module->import_global_count) { WASMGlobalImport *import_global = - &((module->import_globals + export->index)->u.global); + &((module->import_globals + export->index)->u.global); *out_val_type = import_global->type; *out_mutability = import_global->is_mutable; } else { WASMGlobal *global = - module->globals + (export->index - module->import_global_count); + module->globals + (export->index - module->import_global_count); *out_val_type = global->type; *out_mutability = global->is_mutable; } @@ -3914,13 +3923,13 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm, if (export->index < module->import_global_count) { AOTImportGlobal *import_global = - module->import_globals + export->index; + module->import_globals + export->index; *out_val_type = import_global->type; *out_mutability = import_global->is_mutable; } else { AOTGlobal *global = - module->globals + (export->index - module->import_global_count); + module->globals + (export->index - module->import_global_count); *out_val_type = global->type; *out_mutability = global->is_mutable; } @@ -3933,8 +3942,7 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm, bool wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, const WASMExport *export, - uint32 *out_min_page, - uint32 *out_max_page) + uint32 *out_min_page, uint32 *out_max_page) { #if WASM_ENABLE_INTERP != 0 if (module_comm->module_type == Wasm_Module_Bytecode) { @@ -3942,13 +3950,14 @@ wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, if (export->index < module->import_memory_count) { WASMMemoryImport *import_memory = - &((module->import_memories + export->index)->u.memory); + &((module->import_memories + export->index)->u.memory); *out_min_page = import_memory->init_page_count; *out_max_page = import_memory->max_page_count; } else { WASMMemory *memory = - module->memories + (export->index - module->import_memory_count); + module->memories + + (export->index - module->import_memory_count); *out_min_page = memory->init_page_count; *out_max_page = memory->max_page_count; } @@ -3962,13 +3971,13 @@ wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, if (export->index < module->import_memory_count) { AOTImportMemory *import_memory = - module->import_memories + export->index; + module->import_memories + export->index; *out_min_page = import_memory->mem_init_page_count; *out_max_page = import_memory->mem_max_page_count; } else { - AOTMemory *memory = - module->memories + (export->index - module->import_memory_count); + AOTMemory *memory = module->memories + + (export->index - module->import_memory_count); *out_min_page = memory->mem_init_page_count; *out_max_page = memory->mem_max_page_count; } @@ -3981,8 +3990,7 @@ wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, bool wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm, const WASMExport *export, - uint8 *out_elem_type, - uint32 *out_min_size, + uint8 *out_elem_type, uint32 *out_min_size, uint32 *out_max_size) { #if WASM_ENABLE_INTERP != 0 @@ -3991,14 +3999,14 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm, if (export->index < module->import_table_count) { WASMTableImport *import_table = - &((module->import_tables + export->index)->u.table); + &((module->import_tables + export->index)->u.table); *out_elem_type = import_table->elem_type; *out_min_size = import_table->init_size; *out_max_size = import_table->max_size; } else { WASMTable *table = - module->tables + (export->index - module->import_table_count); + module->tables + (export->index - module->import_table_count); *out_elem_type = table->elem_type; *out_min_size = table->init_size; *out_max_size = table->max_size; @@ -4013,14 +4021,14 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm, if (export->index < module->import_table_count) { AOTImportTable *import_table = - module->import_tables + export->index; + module->import_tables + export->index; *out_elem_type = VALUE_TYPE_FUNCREF; *out_min_size = import_table->table_init_size; *out_max_size = import_table->table_max_size; } else { AOTTable *table = - module->tables + (export->index - module->import_table_count); + module->tables + (export->index - module->import_table_count); *out_elem_type = table->elem_type; *out_min_size = table->table_init_size; *out_max_size = table->table_max_size; @@ -4028,13 +4036,11 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm, return true; } #endif - return false; + return false; } static inline bool -argv_to_params(wasm_val_t *out_params, - const uint32 *argv, - WASMType *func_type) +argv_to_params(wasm_val_t *out_params, const uint32 *argv, WASMType *func_type) { wasm_val_t *param = out_params; uint32 i = 0, *u32; @@ -4087,10 +4093,8 @@ argv_to_params(wasm_val_t *out_params, } static inline bool -results_to_argv(WASMModuleInstanceCommon *module_inst, - uint32 *out_argv, - const wasm_val_t *results, - WASMType *func_type) +results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv, + const wasm_val_t *results, WASMType *func_type) { const wasm_val_t *result = results; uint32 *argv = out_argv, *u32, i; @@ -4128,8 +4132,8 @@ results_to_argv(WASMModuleInstanceCommon *module_inst, bool wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, void *func_ptr, WASMType *func_type, - uint32 argc, uint32 *argv, - bool with_env, void *wasm_c_api_env) + uint32 argc, uint32 *argv, bool with_env, + void *wasm_c_api_env) { wasm_val_t params_buf[16], results_buf[4]; wasm_val_t *params = params_buf, *results = results_buf; @@ -4172,7 +4176,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, } else { wasm_func_callback_with_env_t callback = - (wasm_func_callback_with_env_t)func_ptr; + (wasm_func_callback_with_env_t)func_ptr; trap = callback(wasm_c_api_env, ¶ms_vec, &results_vec); } @@ -4180,14 +4184,14 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, if (trap->message->data) { /* since trap->message->data does not end with '\0' */ char trap_message[128] = { 0 }; - bh_memcpy_s( - trap_message, 127, trap->message->data, - (trap->message->size < 127 ? (uint32)trap->message->size : 127)); + bh_memcpy_s(trap_message, 127, trap->message->data, + (trap->message->size < 127 ? (uint32)trap->message->size + : 127)); wasm_runtime_set_exception(module_inst, trap_message); } else { wasm_runtime_set_exception( - module_inst, "native function throw unknown exception"); + module_inst, "native function throw unknown exception"); } wasm_trap_delete(trap); goto fail; diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index cd13a2a09d..b115a8a121 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -27,151 +27,187 @@ extern "C" { #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 -#define PUT_I64_TO_ADDR(addr, value) do { \ - *(int64*)(addr) = (int64)(value); \ - } while (0) -#define PUT_F64_TO_ADDR(addr, value) do { \ - *(float64*)(addr) = (float64)(value); \ - } while (0) - -#define GET_I64_FROM_ADDR(addr) (*(int64*)(addr)) -#define GET_F64_FROM_ADDR(addr) (*(float64*)(addr)) +#define PUT_I64_TO_ADDR(addr, value) \ + do { \ + *(int64 *)(addr) = (int64)(value); \ + } while (0) +#define PUT_F64_TO_ADDR(addr, value) \ + do { \ + *(float64 *)(addr) = (float64)(value); \ + } while (0) + +#define GET_I64_FROM_ADDR(addr) (*(int64 *)(addr)) +#define GET_F64_FROM_ADDR(addr) (*(float64 *)(addr)) /* For STORE opcodes */ #define STORE_I64 PUT_I64_TO_ADDR -#define STORE_U32(addr, value) do { \ - *(uint32*)(addr) = (uint32)(value); \ - } while (0) -#define STORE_U16(addr, value) do { \ - *(uint16*)(addr) = (uint16)(value); \ - } while (0) +#define STORE_U32(addr, value) \ + do { \ + *(uint32 *)(addr) = (uint32)(value); \ + } while (0) +#define STORE_U16(addr, value) \ + do { \ + *(uint16 *)(addr) = (uint16)(value); \ + } while (0) /* For LOAD opcodes */ -#define LOAD_I64(addr) (*(int64*)(addr)) -#define LOAD_F64(addr) (*(float64*)(addr)) -#define LOAD_I32(addr) (*(int32*)(addr)) -#define LOAD_U32(addr) (*(uint32*)(addr)) -#define LOAD_I16(addr) (*(int16*)(addr)) -#define LOAD_U16(addr) (*(uint16*)(addr)) - -#define STORE_PTR(addr, ptr) do { \ - *(void**)addr = (void*)ptr; \ - } while (0) - -#else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */ - -#define PUT_I64_TO_ADDR(addr, value) do { \ - uint32 *addr_u32 = (uint32*)(addr); \ - union { int64 val; uint32 parts[2]; } u; \ - u.val = (int64)(value); \ - addr_u32[0] = u.parts[0]; \ - addr_u32[1] = u.parts[1]; \ - } while (0) -#define PUT_F64_TO_ADDR(addr, value) do { \ - uint32 *addr_u32 = (uint32*)(addr); \ - union { float64 val; uint32 parts[2]; } u; \ - u.val = (value); \ - addr_u32[0] = u.parts[0]; \ - addr_u32[1] = u.parts[1]; \ - } while (0) +#define LOAD_I64(addr) (*(int64 *)(addr)) +#define LOAD_F64(addr) (*(float64 *)(addr)) +#define LOAD_I32(addr) (*(int32 *)(addr)) +#define LOAD_U32(addr) (*(uint32 *)(addr)) +#define LOAD_I16(addr) (*(int16 *)(addr)) +#define LOAD_U16(addr) (*(uint16 *)(addr)) + +#define STORE_PTR(addr, ptr) \ + do { \ + *(void **)addr = (void *)ptr; \ + } while (0) + +#else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */ + +#define PUT_I64_TO_ADDR(addr, value) \ + do { \ + uint32 *addr_u32 = (uint32 *)(addr); \ + union { \ + int64 val; \ + uint32 parts[2]; \ + } u; \ + u.val = (int64)(value); \ + addr_u32[0] = u.parts[0]; \ + addr_u32[1] = u.parts[1]; \ + } while (0) +#define PUT_F64_TO_ADDR(addr, value) \ + do { \ + uint32 *addr_u32 = (uint32 *)(addr); \ + union { \ + float64 val; \ + uint32 parts[2]; \ + } u; \ + u.val = (value); \ + addr_u32[0] = u.parts[0]; \ + addr_u32[1] = u.parts[1]; \ + } while (0) static inline int64 GET_I64_FROM_ADDR(uint32 *addr) { - union { int64 val; uint32 parts[2]; } u; + union { + int64 val; + uint32 parts[2]; + } u; u.parts[0] = addr[0]; u.parts[1] = addr[1]; return u.val; } static inline float64 -GET_F64_FROM_ADDR (uint32 *addr) +GET_F64_FROM_ADDR(uint32 *addr) { - union { float64 val; uint32 parts[2]; } u; + union { + float64 val; + uint32 parts[2]; + } u; u.parts[0] = addr[0]; u.parts[1] = addr[1]; return u.val; } /* For STORE opcodes */ -#define STORE_I64(addr, value) do { \ - uintptr_t addr1 = (uintptr_t)(addr); \ - union { int64 val; uint32 u32[2]; \ - uint16 u16[4]; uint8 u8[8]; } u; \ - if ((addr1 & (uintptr_t)7) == 0) \ - *(int64*)(addr) = (int64)(value); \ - else { \ - u.val = (int64)(value); \ - if ((addr1 & (uintptr_t)3) == 0) { \ - ((uint32*)(addr))[0] = u.u32[0]; \ - ((uint32*)(addr))[1] = u.u32[1]; \ - } \ - else if ((addr1 & (uintptr_t)1) == 0) { \ - ((uint16*)(addr))[0] = u.u16[0]; \ - ((uint16*)(addr))[1] = u.u16[1]; \ - ((uint16*)(addr))[2] = u.u16[2]; \ - ((uint16*)(addr))[3] = u.u16[3]; \ - } \ - else { \ - int32 t; \ - for (t = 0; t < 8; t++) \ - ((uint8*)(addr))[t] = u.u8[t]; \ - } \ - } \ - } while (0) - -#define STORE_U32(addr, value) do { \ - uintptr_t addr1 = (uintptr_t)(addr); \ - union { uint32 val; \ - uint16 u16[2]; uint8 u8[4]; } u; \ - if ((addr1 & (uintptr_t)3) == 0) \ - *(uint32*)(addr) = (uint32)(value); \ - else { \ - u.val = (uint32)(value); \ - if ((addr1 & (uintptr_t)1) == 0) { \ - ((uint16*)(addr))[0] = u.u16[0]; \ - ((uint16*)(addr))[1] = u.u16[1]; \ - } \ - else { \ - ((uint8*)(addr))[0] = u.u8[0]; \ - ((uint8*)(addr))[1] = u.u8[1]; \ - ((uint8*)(addr))[2] = u.u8[2]; \ - ((uint8*)(addr))[3] = u.u8[3]; \ - } \ - } \ - } while (0) - -#define STORE_U16(addr, value) do { \ - union { uint16 val; uint8 u8[2]; } u; \ - u.val = (uint16)(value); \ - ((uint8*)(addr))[0] = u.u8[0]; \ - ((uint8*)(addr))[1] = u.u8[1]; \ - } while (0) +#define STORE_I64(addr, value) \ + do { \ + uintptr_t addr1 = (uintptr_t)(addr); \ + union { \ + int64 val; \ + uint32 u32[2]; \ + uint16 u16[4]; \ + uint8 u8[8]; \ + } u; \ + if ((addr1 & (uintptr_t)7) == 0) \ + *(int64 *)(addr) = (int64)(value); \ + else { \ + u.val = (int64)(value); \ + if ((addr1 & (uintptr_t)3) == 0) { \ + ((uint32 *)(addr))[0] = u.u32[0]; \ + ((uint32 *)(addr))[1] = u.u32[1]; \ + } \ + else if ((addr1 & (uintptr_t)1) == 0) { \ + ((uint16 *)(addr))[0] = u.u16[0]; \ + ((uint16 *)(addr))[1] = u.u16[1]; \ + ((uint16 *)(addr))[2] = u.u16[2]; \ + ((uint16 *)(addr))[3] = u.u16[3]; \ + } \ + else { \ + int32 t; \ + for (t = 0; t < 8; t++) \ + ((uint8 *)(addr))[t] = u.u8[t]; \ + } \ + } \ + } while (0) + +#define STORE_U32(addr, value) \ + do { \ + uintptr_t addr1 = (uintptr_t)(addr); \ + union { \ + uint32 val; \ + uint16 u16[2]; \ + uint8 u8[4]; \ + } u; \ + if ((addr1 & (uintptr_t)3) == 0) \ + *(uint32 *)(addr) = (uint32)(value); \ + else { \ + u.val = (uint32)(value); \ + if ((addr1 & (uintptr_t)1) == 0) { \ + ((uint16 *)(addr))[0] = u.u16[0]; \ + ((uint16 *)(addr))[1] = u.u16[1]; \ + } \ + else { \ + ((uint8 *)(addr))[0] = u.u8[0]; \ + ((uint8 *)(addr))[1] = u.u8[1]; \ + ((uint8 *)(addr))[2] = u.u8[2]; \ + ((uint8 *)(addr))[3] = u.u8[3]; \ + } \ + } \ + } while (0) + +#define STORE_U16(addr, value) \ + do { \ + union { \ + uint16 val; \ + uint8 u8[2]; \ + } u; \ + u.val = (uint16)(value); \ + ((uint8 *)(addr))[0] = u.u8[0]; \ + ((uint8 *)(addr))[1] = u.u8[1]; \ + } while (0) /* For LOAD opcodes */ static inline int64 LOAD_I64(void *addr) { uintptr_t addr1 = (uintptr_t)addr; - union { int64 val; uint32 u32[2]; - uint16 u16[4]; uint8 u8[8]; } u; + union { + int64 val; + uint32 u32[2]; + uint16 u16[4]; + uint8 u8[8]; + } u; if ((addr1 & (uintptr_t)7) == 0) - return *(int64*)addr; + return *(int64 *)addr; if ((addr1 & (uintptr_t)3) == 0) { - u.u32[0] = ((uint32*)addr)[0]; - u.u32[1] = ((uint32*)addr)[1]; + u.u32[0] = ((uint32 *)addr)[0]; + u.u32[1] = ((uint32 *)addr)[1]; } else if ((addr1 & (uintptr_t)1) == 0) { - u.u16[0] = ((uint16*)addr)[0]; - u.u16[1] = ((uint16*)addr)[1]; - u.u16[2] = ((uint16*)addr)[2]; - u.u16[3] = ((uint16*)addr)[3]; + u.u16[0] = ((uint16 *)addr)[0]; + u.u16[1] = ((uint16 *)addr)[1]; + u.u16[2] = ((uint16 *)addr)[2]; + u.u16[3] = ((uint16 *)addr)[3]; } else { int32 t; for (t = 0; t < 8; t++) - u.u8[t] = ((uint8*)addr)[t]; + u.u8[t] = ((uint8 *)addr)[t]; } return u.val; } @@ -180,25 +216,29 @@ static inline float64 LOAD_F64(void *addr) { uintptr_t addr1 = (uintptr_t)addr; - union { float64 val; uint32 u32[2]; - uint16 u16[4]; uint8 u8[8]; } u; + union { + float64 val; + uint32 u32[2]; + uint16 u16[4]; + uint8 u8[8]; + } u; if ((addr1 & (uintptr_t)7) == 0) - return *(float64*)addr; + return *(float64 *)addr; if ((addr1 & (uintptr_t)3) == 0) { - u.u32[0] = ((uint32*)addr)[0]; - u.u32[1] = ((uint32*)addr)[1]; + u.u32[0] = ((uint32 *)addr)[0]; + u.u32[1] = ((uint32 *)addr)[1]; } else if ((addr1 & (uintptr_t)1) == 0) { - u.u16[0] = ((uint16*)addr)[0]; - u.u16[1] = ((uint16*)addr)[1]; - u.u16[2] = ((uint16*)addr)[2]; - u.u16[3] = ((uint16*)addr)[3]; + u.u16[0] = ((uint16 *)addr)[0]; + u.u16[1] = ((uint16 *)addr)[1]; + u.u16[2] = ((uint16 *)addr)[2]; + u.u16[3] = ((uint16 *)addr)[3]; } else { int32 t; for (t = 0; t < 8; t++) - u.u8[t] = ((uint8*)addr)[t]; + u.u8[t] = ((uint8 *)addr)[t]; } return u.val; } @@ -207,19 +247,23 @@ static inline int32 LOAD_I32(void *addr) { uintptr_t addr1 = (uintptr_t)addr; - union { int32 val; uint16 u16[2]; uint8 u8[4]; } u; + union { + int32 val; + uint16 u16[2]; + uint8 u8[4]; + } u; if ((addr1 & (uintptr_t)3) == 0) - return *(int32*)addr; + return *(int32 *)addr; if ((addr1 & (uintptr_t)1) == 0) { - u.u16[0] = ((uint16*)addr)[0]; - u.u16[1] = ((uint16*)addr)[1]; + u.u16[0] = ((uint16 *)addr)[0]; + u.u16[1] = ((uint16 *)addr)[1]; } else { - u.u8[0] = ((uint8*)addr)[0]; - u.u8[1] = ((uint8*)addr)[1]; - u.u8[2] = ((uint8*)addr)[2]; - u.u8[3] = ((uint8*)addr)[3]; + u.u8[0] = ((uint8 *)addr)[0]; + u.u8[1] = ((uint8 *)addr)[1]; + u.u8[2] = ((uint8 *)addr)[2]; + u.u8[3] = ((uint8 *)addr)[3]; } return u.val; } @@ -228,13 +272,16 @@ static inline int16 LOAD_I16(void *addr) { uintptr_t addr1 = (uintptr_t)addr; - union { int16 val; uint8 u8[2]; } u; + union { + int16 val; + uint8 u8[2]; + } u; if ((addr1 & (uintptr_t)1)) { - u.u8[0] = ((uint8*)addr)[0]; - u.u8[1] = ((uint8*)addr)[1]; + u.u8[0] = ((uint8 *)addr)[0]; + u.u8[1] = ((uint8 *)addr)[1]; return u.val; } - return *(int16*)addr; + return *(int16 *)addr; } #define LOAD_U32(addr) ((uint32)LOAD_I32(addr)) @@ -246,7 +293,7 @@ LOAD_I16(void *addr) #define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr) #endif -#endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */ +#endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */ typedef struct WASMModuleCommon { /* Module type, for module loaded from WASM bytecode binary, @@ -361,8 +408,8 @@ get_package_type(const uint8 *buf, uint32 size); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMModuleCommon * -wasm_runtime_load(const uint8 *buf, uint32 size, - char *error_buf, uint32 error_buf_size); +wasm_runtime_load(const uint8 *buf, uint32 size, char *error_buf, + uint32 error_buf_size); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMModuleCommon * @@ -386,9 +433,9 @@ wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst, /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * -wasm_runtime_instantiate(WASMModuleCommon *module, - uint32 stack_size, uint32 heap_size, - char *error_buf, uint32 error_buf_size); +wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, + uint32 heap_size, char *error_buf, + uint32 error_buf_size); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN void @@ -396,7 +443,7 @@ wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon * -wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst, +wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst, const char *name, const char *signature); /* Internal API */ @@ -432,8 +479,8 @@ wasm_runtime_get_user_data(WASMExecEnv *exec_env); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool wasm_runtime_call_wasm(WASMExecEnv *exec_env, - WASMFunctionInstanceCommon *function, - uint32 argc, uint32 argv[]); + WASMFunctionInstanceCommon *function, uint32 argc, + uint32 argv[]); WASM_RUNTIME_API_EXTERN bool wasm_runtime_call_wasm_a(WASMExecEnv *exec_env, @@ -465,14 +512,13 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, * the caller can call wasm_runtime_get_exception to get exception info. */ bool -wasm_runtime_call_indirect(WASMExecEnv *exec_env, - uint32 element_indices, +wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices, uint32 argc, uint32 argv[]); bool -wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst, - WASMFunctionInstanceCommon *function, - uint32 argc, uint32 argv[]); +wasm_runtime_create_exec_env_and_call_wasm( + WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function, + uint32 argc, uint32 argv[]); bool wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst); @@ -482,8 +528,8 @@ wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool -wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, - int32 argc, char *argv[]); +wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, + char *argv[]); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool @@ -559,8 +605,7 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst, /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst, - uint32 app_offset, - uint32 *p_app_start_offset, + uint32 app_offset, uint32 *p_app_start_offset, uint32 *p_app_end_offset); /* See wasm_export.h for description */ @@ -600,8 +645,7 @@ wasm_runtime_register_module_internal(const char *module_name, WASMModuleCommon *module, uint8 *orig_file_buf, uint32 orig_file_buf_size, - char *error_buf, - uint32 error_buf_size); + char *error_buf, uint32 error_buf_size); void wasm_runtime_unregister_module(const WASMModuleCommon *module); @@ -610,8 +654,8 @@ bool wasm_runtime_is_module_registered(const char *module_name); bool -wasm_runtime_add_loading_module(const char *module_name, - char *error_buf, uint32 error_buf_size); +wasm_runtime_add_loading_module(const char *module_name, char *error_buf, + uint32 error_buf_size); void wasm_runtime_delete_loading_module(const char *module_name); @@ -628,30 +672,28 @@ wasm_runtime_is_built_in_module(const char *module_name); #if WASM_ENABLE_THREAD_MGR != 0 bool -wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, - uint32 *start_offset, uint32 *size); +wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, + uint32 *size); bool -wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, - uint32 start_offset, uint32 size); +wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, + uint32 size); #endif #if WASM_ENABLE_LIBC_WASI != 0 WASM_RUNTIME_API_EXTERN void -wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, - const char *dir_list[], uint32 dir_count, - const char *map_dir_list[], uint32 map_dir_count, - const char *env_list[], uint32 env_count, - char *argv[], int argc, - int stdinfd, int stdoutfd, int stderrfd); +wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[], + uint32 dir_count, const char *map_dir_list[], + uint32 map_dir_count, const char *env_list[], + uint32 env_count, char *argv[], int argc, + int stdinfd, int stdoutfd, int stderrfd); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN void -wasm_runtime_set_wasi_args(WASMModuleCommon *module, - const char *dir_list[], uint32 dir_count, - const char *map_dir_list[], uint32 map_dir_count, - const char *env_list[], uint32 env_count, - char *argv[], int argc); +wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[], + uint32 dir_count, const char *map_dir_list[], + uint32 map_dir_count, const char *env_list[], + uint32 env_count, char *argv[], int argc); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool @@ -665,9 +707,8 @@ bool wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, const char *dir_list[], uint32 dir_count, const char *map_dir_list[], uint32 map_dir_count, - const char *env[], uint32 env_count, - char *argv[], uint32 argc, - int stdinfd, int stdoutfd, int stderrfd, + const char *env[], uint32 env_count, char *argv[], + uint32 argc, int stdinfd, int stdoutfd, int stderrfd, char *error_buf, uint32 error_buf_size); void @@ -685,8 +726,8 @@ wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst); #if WASM_ENABLE_REF_TYPES != 0 /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool -wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, - void *extern_obj, uint32 *p_externref_idx); +wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj, + uint32 *p_externref_idx); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool @@ -711,7 +752,7 @@ wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst); #endif /* end of WASM_ENABLE_REF_TYPES */ /* Get module of the current exec_env */ -WASMModuleCommon* +WASMModuleCommon * wasm_exec_env_get_module(WASMExecEnv *exec_env); /** @@ -722,7 +763,8 @@ wasm_exec_env_get_module(WASMExecEnv *exec_env); * @return return true if enlarge successfully, false otherwise */ bool -wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count); +wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, + uint32 inc_page_count); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool @@ -739,14 +781,14 @@ wasm_runtime_register_natives_raw(const char *module_name, bool wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *ret); + void *attachment, uint32 *argv, uint32 argc, + uint32 *ret); bool wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, const WASMType *func_type, const char *signature, - void *attachment, - uint32 *argv, uint32 argc, uint32 *ret); + void *attachment, uint32 *argv, uint32 argc, + uint32 *ret); void wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2); @@ -755,8 +797,8 @@ void wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module); void -wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon - *module_inst); +wasm_runtime_dump_module_inst_mem_consumption( + const WASMModuleInstanceCommon *module_inst); void wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env); @@ -773,37 +815,32 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env, bool wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm, - const WASMExport *export_, - WASMType **out); + const WASMExport *export_, WASMType **out); bool wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm, const WASMExport *export_, - uint8 *out_val_type, - bool *out_mutability); + uint8 *out_val_type, bool *out_mutability); bool wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm, const WASMExport *export_, - uint32 *out_min_page, - uint32 *out_max_page); + uint32 *out_min_page, uint32 *out_max_page); bool wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm, const WASMExport *export_, - uint8 *out_elem_type, - uint32 *out_min_size, + uint8 *out_elem_type, uint32 *out_min_size, uint32 *out_max_size); bool wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, void *func_ptr, WASMType *func_type, - uint32 argc, uint32 *argv, - bool with_env, void *wasm_c_api_env); + uint32 argc, uint32 *argv, bool with_env, + void *wasm_c_api_env); #ifdef __cplusplus } #endif #endif /* end of _WASM_COMMON_H */ - diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index cef0aa364d..65c98d4ef3 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -10,9 +10,12 @@ static bh_list shared_memory_list_head; static bh_list *const shared_memory_list = &shared_memory_list_head; static korp_mutex shared_memory_list_lock; +/* clang-format off */ enum { - S_WAITING, S_NOTIFIED + S_WAITING, + S_NOTIFIED }; +/* clang-format on */ typedef struct AtomicWaitInfo { korp_mutex wait_list_lock; @@ -45,11 +48,9 @@ wasm_shared_memory_init() if (os_mutex_init(&shared_memory_list_lock) != 0) return false; /* wait map not exists, create new map */ - if (!(wait_map = - bh_hash_map_create(32, true, - (HashFunc)wait_address_hash, - (KeyEqualFunc)wait_address_equal, - NULL, destroy_wait_info))) { + if (!(wait_map = bh_hash_map_create(32, true, (HashFunc)wait_address_hash, + (KeyEqualFunc)wait_address_equal, NULL, + destroy_wait_info))) { os_mutex_destroy(&shared_memory_list_lock); return false; } @@ -66,7 +67,7 @@ wasm_shared_memory_destroy() } } -static WASMSharedMemNode* +static WASMSharedMemNode * search_module(WASMModuleCommon *module) { WASMSharedMemNode *node; @@ -86,7 +87,7 @@ search_module(WASMModuleCommon *module) return NULL; } -WASMSharedMemNode* +WASMSharedMemNode * wasm_module_get_shared_memory(WASMModuleCommon *module) { return search_module(module); @@ -128,13 +129,13 @@ shared_memory_dec_reference(WASMModuleCommon *module) return -1; } -WASMMemoryInstanceCommon* +WASMMemoryInstanceCommon * shared_memory_get_memory_inst(WASMSharedMemNode *node) { return node->memory_inst; } -WASMSharedMemNode* +WASMSharedMemNode * shared_memory_set_memory_inst(WASMModuleCommon *module, WASMMemoryInstanceCommon *memory) { @@ -223,16 +224,15 @@ acquire_wait_info(void *address, bool create) AtomicWaitInfo *wait_info = NULL; bh_list_status ret; - wait_info = (AtomicWaitInfo *) - bh_hash_map_find(wait_map, address); + wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address); if (!create) return wait_info; /* No wait info on this address, create new info */ if (!wait_info) { - if (!(wait_info = - (AtomicWaitInfo *)wasm_runtime_malloc(sizeof(AtomicWaitInfo)))) + if (!(wait_info = (AtomicWaitInfo *)wasm_runtime_malloc( + sizeof(AtomicWaitInfo)))) return NULL; memset(wait_info, 0, sizeof(AtomicWaitInfo)); @@ -247,8 +247,7 @@ acquire_wait_info(void *address, bool create) return NULL; } - if (!bh_hash_map_insert(wait_map, address, - (void *)wait_info)) { + if (!bh_hash_map_insert(wait_map, address, (void *)wait_info)) { os_mutex_destroy(&wait_info->wait_list_lock); wasm_runtime_free(wait_info); return NULL; @@ -283,8 +282,7 @@ destroy_wait_info(void *wait_info) } static void -release_wait_info(HashMap *wait_map, - AtomicWaitInfo *wait_info, void *address) +release_wait_info(HashMap *wait_map, AtomicWaitInfo *wait_info, void *address) { if (wait_info->wait_list->len == 0) { bh_hash_map_remove(wait_map, address, NULL, NULL); @@ -333,8 +331,8 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address, os_mutex_lock(&wait_info->wait_list_lock); - if ((!wait64 && *(uint32*)address != (uint32)expect) - || (wait64 && *(uint64*)address != expect)) { + if ((!wait64 && *(uint32 *)address != (uint32)expect) + || (wait64 && *(uint64 *)address != expect)) { os_mutex_unlock(&wait_info->wait_list_lock); return 1; } @@ -375,8 +373,7 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address, if (timeout < 0) timeout = BHT_WAIT_FOREVER; - os_cond_reltimedwait(&wait_node->wait_cond, - &wait_node->wait_lock, timeout); + os_cond_reltimedwait(&wait_node->wait_cond, &wait_node->wait_lock, timeout); os_mutex_unlock(&wait_node->wait_lock); @@ -400,8 +397,8 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address, } uint32 -wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, - void *address, uint32 count) +wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address, + uint32 count) { uint32 notify_result; AtomicWaitInfo *wait_info; diff --git a/core/iwasm/common/wasm_shared_memory.h b/core/iwasm/common/wasm_shared_memory.h index f05e595782..bc6f8945cf 100644 --- a/core/iwasm/common/wasm_shared_memory.h +++ b/core/iwasm/common/wasm_shared_memory.h @@ -37,7 +37,7 @@ wasm_shared_memory_init(); void wasm_shared_memory_destroy(); -WASMSharedMemNode* +WASMSharedMemNode * wasm_module_get_shared_memory(WASMModuleCommon *module); int32 @@ -46,10 +46,10 @@ shared_memory_inc_reference(WASMModuleCommon *module); int32 shared_memory_dec_reference(WASMModuleCommon *module); -WASMMemoryInstanceCommon* +WASMMemoryInstanceCommon * shared_memory_get_memory_inst(WASMSharedMemNode *node); -WASMSharedMemNode* +WASMSharedMemNode * shared_memory_set_memory_inst(WASMModuleCommon *module, WASMMemoryInstanceCommon *memory); @@ -58,8 +58,8 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address, uint64 expect, int64 timeout, bool wait64); uint32 -wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, - void *address, uint32 count); +wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address, + uint32 count); #ifdef __cplusplus } diff --git a/core/iwasm/include/aot_export.h b/core/iwasm/include/aot_export.h index 476e2cb352..f323a5858e 100644 --- a/core/iwasm/include/aot_export.h +++ b/core/iwasm/include/aot_export.h @@ -9,7 +9,6 @@ #include #include - #ifdef __cplusplus extern "C" { #endif @@ -27,9 +26,9 @@ void aot_destroy_comp_data(aot_comp_data_t comp_data); #if WASM_ENABLE_DEBUG_AOT != 0 -typedef void * dwar_extractor_handle_t; +typedef void *dwar_extractor_handle_t; dwar_extractor_handle_t -create_dwarf_extractor(aot_comp_data_t comp_data, char * file_name); +create_dwarf_extractor(aot_comp_data_t comp_data, char *file_name); #endif enum { @@ -39,7 +38,7 @@ enum { AOT_LLVMIR_OPT_FILE, }; -typedef struct AOTCompOption{ +typedef struct AOTCompOption { bool is_jit_mode; bool is_indirect_mode; char *target_arch; @@ -62,8 +61,7 @@ typedef struct AOTCompOption{ } AOTCompOption, *aot_comp_option_t; aot_comp_context_t -aot_create_comp_context(aot_comp_data_t comp_data, - aot_comp_option_t option); +aot_create_comp_context(aot_comp_data_t comp_data, aot_comp_option_t option); void aot_destroy_comp_context(aot_comp_context_t comp_ctx); @@ -78,8 +76,7 @@ bool aot_emit_object_file(aot_comp_context_t comp_ctx, const char *file_name); bool -aot_emit_aot_file(aot_comp_context_t comp_ctx, - aot_comp_data_t comp_data, +aot_emit_aot_file(aot_comp_context_t comp_ctx, aot_comp_data_t comp_data, const char *file_name); void @@ -88,16 +85,15 @@ aot_destroy_aot_file(uint8_t *aot_file); bool aot_compile_wasm_file_init(); -uint8_t* +uint8_t * aot_compile_wasm_file(const uint8_t *wasm_file_buf, uint32_t wasm_file_size, - uint32_t opt_level, uint32_t size_level, - char *error_buf, uint32_t error_buf_size, - uint32_t *p_aot_file_size); + uint32_t opt_level, uint32_t size_level, char *error_buf, + uint32_t error_buf_size, uint32_t *p_aot_file_size); void aot_compile_wasm_file_destroy(); -char* +char * aot_get_last_error(); uint32_t diff --git a/core/iwasm/include/lib_export.h b/core/iwasm/include/lib_export.h index 3a4c02d43b..e4829e4fe4 100644 --- a/core/iwasm/include/lib_export.h +++ b/core/iwasm/include/lib_export.h @@ -21,18 +21,22 @@ typedef struct NativeSymbol { void *attachment; } NativeSymbol; -#define EXPORT_WASM_API(symbol) {#symbol, (void*)symbol, NULL, NULL} -#define EXPORT_WASM_API2(symbol) {#symbol, (void*)symbol##_wrapper, NULL, NULL} +/* clang-format off */ +#define EXPORT_WASM_API(symbol) \ + { #symbol, (void *)symbol, NULL, NULL } +#define EXPORT_WASM_API2(symbol) \ + { #symbol, (void *)symbol##_wrapper, NULL, NULL } #define EXPORT_WASM_API_WITH_SIG(symbol, signature) \ - {#symbol, (void*)symbol, signature, NULL} + { #symbol, (void *)symbol, signature, NULL } #define EXPORT_WASM_API_WITH_SIG2(symbol, signature) \ - {#symbol, (void*)symbol##_wrapper, signature, NULL} + { #symbol, (void *)symbol##_wrapper, signature, NULL } #define EXPORT_WASM_API_WITH_ATT(symbol, signature, attachment) \ - {#symbol, (void*)symbol, signature, attachment} + { #symbol, (void *)symbol, signature, attachment } #define EXPORT_WASM_API_WITH_ATT2(symbol, signature, attachment) \ - {#symbol, (void*)symbol##_wrapper, signature, attachment} + { #symbol, (void *)symbol##_wrapper, signature, attachment } +/* clang-format on */ /** * Get the exported APIs of base lib @@ -48,5 +52,4 @@ get_base_lib_export_apis(NativeSymbol **p_base_lib_apis); } #endif -#endif - +#endif /* end of _LIB_EXPORT_H_ */ diff --git a/core/iwasm/include/wasm_c_api.h b/core/iwasm/include/wasm_c_api.h index b1a9df84c4..6722093a4a 100644 --- a/core/iwasm/include/wasm_c_api.h +++ b/core/iwasm/include/wasm_c_api.h @@ -1,7 +1,7 @@ // WebAssembly C API -#ifndef WASM_H -#define WASM_H +#ifndef _WASM_C_API_H_ +#define _WASM_C_API_H_ #include #include @@ -11,11 +11,11 @@ #ifndef WASM_API_EXTERN #if defined(_MSC_BUILD) - #if defined(COMPILING_WASM_RUNTIME_API) - #define WASM_API_EXTERN __declspec(dllexport) - #else - #define WASM_API_EXTERN __declspec(dllimport) - #endif +#if defined(COMPILING_WASM_RUNTIME_API) +#define WASM_API_EXTERN __declspec(dllexport) +#else +#define WASM_API_EXTERN __declspec(dllimport) +#endif #else #define WASM_API_EXTERN #endif @@ -25,6 +25,8 @@ extern "C" { #endif +/* clang-format off */ + /////////////////////////////////////////////////////////////////////////////// // Auxiliaries @@ -776,8 +778,10 @@ static inline void* wasm_val_ptr(const wasm_val_t* val) { #undef own +/* clang-format on */ + #ifdef __cplusplus -} // extern "C" +} // extern "C" #endif -#endif // #ifdef WASM_H +#endif // #ifdef _WASM_C_API_H_ diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 0a15066aba..9b33f9f4c7 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -10,14 +10,13 @@ #include #include "lib_export.h" - #ifndef WASM_RUNTIME_API_EXTERN -#if defined(_MSC_BUILD ) - #if defined(COMPILING_WASM_RUNTIME_API) - #define WASM_RUNTIME_API_EXTERN __declspec(dllexport) - #else - #define WASM_RUNTIME_API_EXTERN __declspec(dllimport) - #endif +#if defined(_MSC_BUILD) +#if defined(COMPILING_WASM_RUNTIME_API) +#define WASM_RUNTIME_API_EXTERN __declspec(dllexport) +#else +#define WASM_RUNTIME_API_EXTERN __declspec(dllimport) +#endif #else #define WASM_RUNTIME_API_EXTERN #endif @@ -27,6 +26,8 @@ extern "C" { #endif +/* clang-format off */ + #define get_module_inst(exec_env) \ wasm_runtime_get_module_inst(exec_env) @@ -48,13 +49,12 @@ extern "C" { #define module_free(offset) \ wasm_runtime_module_free(module_inst, offset) -#define native_raw_return_type(type, args) type *raw_ret = (type*)(args) +#define native_raw_return_type(type, args) type *raw_ret = (type *)(args) -#define native_raw_get_arg(type, name, args) type name = *((type*)(args++)) +#define native_raw_get_arg(type, name, args) type name = *((type *)(args++)) #define native_raw_set_return(val) *raw_ret = (val) - #ifndef WASM_MODULE_T_DEFINED #define WASM_MODULE_T_DEFINED /* Uninstantiated WASM module loaded from WASM binary file @@ -158,14 +158,14 @@ enum wasm_valkind_enum { struct wasm_ref_t; typedef struct wasm_val_t { - wasm_valkind_t kind; - union { - int32_t i32; - int64_t i64; - float f32; - double f64; - struct wasm_ref_t* ref; - } of; + wasm_valkind_t kind; + union { + int32_t i32; + int64_t i64; + float f32; + double f64; + struct wasm_ref_t *ref; + } of; } wasm_val_t; #endif @@ -652,10 +652,11 @@ wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst, /** * Similar to wasm_runtime_validate_app_addr(), except that the size parameter - * is not provided. This function validates the app string address, check whether it - * belongs to WASM module instance's address space, or in its heap space or - * memory space. Moreover, it checks whether it is the offset of a string that - * is end with '\0'. + * is not provided. This function validates the app string address, check + * whether it belongs to WASM module instance's address space, or in its heap + * space or memory space. Moreover, it checks whether it is the offset of a + * string that is end with '\0'. + * * @param module_inst the WASM module instance * @param app_str_offset the app address of the string to validate, which is a * relative address @@ -724,12 +725,15 @@ wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst, uint32_t *p_app_end_offset); /** - * Get the native address range (absolute address) that a native address belongs to + * Get the native address range (absolute address) that a native address + * belongs to * * @param module_inst the WASM module instance * @param native_ptr the native address to retrieve - * @param p_native_start_addr buffer to output the native start address if not NULL - * @param p_native_end_addr buffer to output the native end address if not NULL + * @param p_native_start_addr buffer to output the native start address + * if not NULL + * @param p_native_end_addr buffer to output the native end address + * if not NULL * * @return true if success, false otherwise. */ @@ -740,31 +744,31 @@ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst, uint8_t **p_native_end_addr); /** - * Register native functions with same module name - * - * @param module_name the module name of the native functions - * @param native_symbols specifies an array of NativeSymbol structures which - * contain the names, function pointers and signatures - * Note: WASM runtime will not allocate memory to clone the data, so - * user must ensure the array can be used forever - * Meanings of letters in function signature: - * 'i': the parameter is i32 type - * 'I': the parameter is i64 type - * 'f': the parameter is f32 type - * 'F': the parameter is f64 type - * '*': 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 - * is gotten from the following parameter, if not, the checked - * length of the pointer is 1. - * '~': the parameter is the pointer's length with i32 type, and must - * follow after '*' - * '$': the parameter is a string (i32 in WASM), and runtime will - * auto check its boundary before calling the native function - * @param n_native_symbols specifies the number of native symbols in the array - * - * @return true if success, false otherwise - */ + * Register native functions with same module name + * + * @param module_name the module name of the native functions + * @param native_symbols specifies an array of NativeSymbol structures which + * contain the names, function pointers and signatures + * Note: WASM runtime will not allocate memory to clone the data, so + * user must ensure the array can be used forever + * Meanings of letters in function signature: + * 'i': the parameter is i32 type + * 'I': the parameter is i64 type + * 'f': the parameter is f32 type + * 'F': the parameter is f64 type + * '*': 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 + * is gotten from the following parameter, if not, the checked + * length of the pointer is 1. + * '~': the parameter is the pointer's length with i32 type, and must + * follow after '*' + * '$': the parameter is a string (i32 in WASM), and runtime will + * auto check its boundary before calling the native function + * @param n_native_symbols specifies the number of native symbols in the array + * + * @return true if success, false otherwise + */ WASM_RUNTIME_API_EXTERN bool wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, @@ -773,7 +777,7 @@ wasm_runtime_register_natives(const char *module_name, /** * Register native functions with same module name, similar to * wasm_runtime_register_natives, the difference is that runtime passes raw - * arguments to native API, which means that the native API should be defined as: + * arguments to native API, which means that the native API should be defined as * void foo(wasm_exec_env_t exec_env, uint64 *args); * and native API should extract arguments one by one from args array with macro * native_raw_get_arg @@ -802,8 +806,7 @@ wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env); * @param user_data the user data to be set */ WASM_RUNTIME_API_EXTERN void -wasm_runtime_set_user_data(wasm_exec_env_t exec_env, - void *user_data); +wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data); /** * Get the user data within execution environment. * @@ -835,7 +838,7 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst); /* wasm thread callback function type */ -typedef void* (*wasm_thread_callback_t)(wasm_exec_env_t, void *); +typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *); /* wasm thread type */ typedef uintptr_t wasm_thread_t; @@ -938,6 +941,8 @@ wasm_externref_retain(uint32_t externref_idx); WASM_RUNTIME_API_EXTERN void wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env); +/* clang-format on */ + #ifdef __cplusplus } #endif diff --git a/doc/source_debugging.md b/doc/source_debugging.md index 60b08240e7..4c09c3e18f 100644 --- a/doc/source_debugging.md +++ b/doc/source_debugging.md @@ -36,7 +36,7 @@ iwasm -g=127.0.0.1:1234 test.wasm ``` bash cd ${WAMR_ROOT}/core/deps/llvm git apply ../../../build-scripts/lldb-wasm.patch -mkdir build_lldb && cd build_lldb +mkdir build-lldb && cd build-lldb cmake -DCMAKE_BUILD_TYPE:STRING="Release" -DLLVM_ENABLE_PROJECTS="clang;lldb" -DLLVM_TARGETS_TO_BUILD:STRING="X86;WebAssembly" -DLLVM_ENABLE_LIBXML2:BOOL=ON ../llvm make -j $(nproc) ``` @@ -87,4 +87,4 @@ wamrc -o test.aot test.wasm lldb-12 iwasm -- test.aot ``` -Then you can use lldb commands to debug both wamr runtime and your wasm application in ***current terminal*** \ No newline at end of file +Then you can use lldb commands to debug both wamr runtime and your wasm application in ***current terminal*** diff --git a/tests/wamr-test-suites/test_wamr.sh b/tests/wamr-test-suites/test_wamr.sh index 0f585955ea..afccb23d8a 100755 --- a/tests/wamr-test-suites/test_wamr.sh +++ b/tests/wamr-test-suites/test_wamr.sh @@ -278,7 +278,7 @@ function spec_test() # restore from XX_ignore_cases.patch # resotre branch git checkout -B master - git reset --hard f9770eb75117cac0c878feaa5eaf4a4d9dda61f5 + git reset --hard 397399a70565609bf142d211891724e21bffd01f git apply ../../spec-test-script/ignore_cases.patch # udpate thread cases @@ -291,7 +291,7 @@ function spec_test() # fetch spec for threads proposal git fetch threads git reset --hard HEAD - git checkout threads/master + git checkout threads/main git apply ../../spec-test-script/thread_proposal_ignore_cases.patch fi @@ -335,16 +335,16 @@ function spec_test() exit 1 ;; esac - if [ ! -f /tmp/wabt-1.0.23-${WABT_PLATFORM}.tar.gz ]; then + if [ ! -f /tmp/wabt-1.0.24-${WABT_PLATFORM}.tar.gz ]; then wget \ - https://github.com/WebAssembly/wabt/releases/download/1.0.23/wabt-1.0.23-${WABT_PLATFORM}.tar.gz \ + https://github.com/WebAssembly/wabt/releases/download/1.0.24/wabt-1.0.24-${WABT_PLATFORM}.tar.gz \ -P /tmp fi cd /tmp \ - && tar zxf wabt-1.0.23-${WABT_PLATFORM}.tar.gz \ + && tar zxf wabt-1.0.24-${WABT_PLATFORM}.tar.gz \ && mkdir -p ${WORK_DIR}/wabt/out/gcc/Release/ \ - && install wabt-1.0.23/bin/wa* ${WORK_DIR}/wabt/out/gcc/Release/ \ + && install wabt-1.0.24/bin/wa* ${WORK_DIR}/wabt/out/gcc/Release/ \ && cd - fi else @@ -356,12 +356,7 @@ function spec_test() echo "upate wabt" cd wabt git pull - if [[ ${ENABLE_SIMD} == 0 ]]; then - # Use latest version of wabt if simd cases are not tested - git reset --hard c6cd63316ac53208900cda4d1089a22618b85256 - else - git reset --hard origin/main - fi + git reset --hard origin/main cd .. make -C wabt gcc-release fi