Fix data/elem drop (#2747)
Currently, `data.drop` instruction is implemented by directly modifying the underlying module. It breaks use cases where you have multiple instances sharing a single loaded module. `elem.drop` has the same problem too. This PR fixes the issue by keeping track of which data/elem segments have been dropped by using bitmaps for each module instances separately, and add a sample to demonstrate the issue and make the CI run it. Also add a missing check of dropped elements to the fast-jit `table.init`. Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2735 Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/2772
This commit is contained in:
1
samples/shared-module/.gitignore
vendored
Normal file
1
samples/shared-module/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/out/
|
||||
95
samples/shared-module/CMakeLists.txt
Normal file
95
samples/shared-module/CMakeLists.txt
Normal file
@ -0,0 +1,95 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required (VERSION 3.14)
|
||||
|
||||
include(CheckPIESupported)
|
||||
|
||||
project (shared-module)
|
||||
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
|
||||
################ runtime settings ################
|
||||
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
|
||||
if (APPLE)
|
||||
add_definitions(-DBH_PLATFORM_DARWIN)
|
||||
endif ()
|
||||
|
||||
# Reset default linker flags
|
||||
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
||||
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
||||
|
||||
# WAMR features switch
|
||||
|
||||
# Set WAMR_BUILD_TARGET, currently values supported:
|
||||
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
|
||||
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
|
||||
if (NOT DEFINED WAMR_BUILD_TARGET)
|
||||
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
|
||||
set (WAMR_BUILD_TARGET "AARCH64")
|
||||
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
|
||||
set (WAMR_BUILD_TARGET "RISCV64")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
# Build as X86_64 by default in 64-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_64")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
# Build as X86_32 by default in 32-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_32")
|
||||
else ()
|
||||
message(SEND_ERROR "Unsupported build target platform!")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set (CMAKE_BUILD_TYPE Debug)
|
||||
endif ()
|
||||
|
||||
set (WAMR_BUILD_INTERP 1)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
set (WAMR_BUILD_JIT 0)
|
||||
|
||||
# fast interpreter
|
||||
# set (WAMR_BUILD_FAST_INTERP 1)
|
||||
|
||||
# fast-jit
|
||||
# set (WAMR_BUILD_FAST_JIT 1)
|
||||
|
||||
# llvm jit
|
||||
# set (WAMR_BUILD_JIT 1)
|
||||
# set (LLVM_DIR /usr/local/opt/llvm@14/lib/cmake/llvm)
|
||||
|
||||
set (WAMR_BUILD_REF_TYPES 1)
|
||||
|
||||
if (NOT MSVC)
|
||||
# linker flags
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
|
||||
endif ()
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
|
||||
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
|
||||
endif ()
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
# build out vmlib
|
||||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
|
||||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
|
||||
|
||||
################ application related ################
|
||||
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
|
||||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
||||
|
||||
add_executable (shared-module src/main.c ${UNCOMMON_SHARED_SOURCE})
|
||||
|
||||
check_pie_supported()
|
||||
set_target_properties (shared-module PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
if (APPLE)
|
||||
target_link_libraries (shared-module vmlib -lm -ldl -lpthread ${LLVM_AVAILABLE_LIBS})
|
||||
else ()
|
||||
target_link_libraries (shared-module vmlib -lm -ldl -lpthread -lrt ${LLVM_AVAILABLE_LIBS})
|
||||
endif ()
|
||||
5
samples/shared-module/README.md
Normal file
5
samples/shared-module/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
The "shared-module" sample project
|
||||
==================================
|
||||
|
||||
This sample demonstrates a bug described in:
|
||||
https://github.com/bytecodealliance/wasm-micro-runtime/issues/2735.
|
||||
63
samples/shared-module/build.sh
Executable file
63
samples/shared-module/build.sh
Executable file
@ -0,0 +1,63 @@
|
||||
#
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
CURR_DIR=$PWD
|
||||
WAMR_DIR=${PWD}/../..
|
||||
OUT_DIR=${PWD}/out
|
||||
|
||||
WASM_APPS=${PWD}/wasm-apps
|
||||
|
||||
|
||||
rm -rf ${OUT_DIR}
|
||||
mkdir ${OUT_DIR}
|
||||
mkdir ${OUT_DIR}/wasm-apps
|
||||
|
||||
|
||||
echo "##################### build shared-module project"
|
||||
cd ${CURR_DIR}
|
||||
mkdir -p cmake_build
|
||||
cd cmake_build
|
||||
cmake ..
|
||||
make -j ${nproc}
|
||||
if [ $? != 0 ];then
|
||||
echo "BUILD_FAIL shared-module exit as $?\n"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
cp -a shared-module ${OUT_DIR}
|
||||
|
||||
printf "\n"
|
||||
|
||||
echo "##################### build wasm apps"
|
||||
|
||||
cd ${WASM_APPS}
|
||||
|
||||
for i in `ls *.wat`
|
||||
do
|
||||
APP_SRC="$i"
|
||||
OUT_FILE=${i%.*}.wasm
|
||||
|
||||
# Note: the CI installs wabt in /opt/wabt
|
||||
if type wat2wasm; then
|
||||
WAT2WASM=${WAT2WASM:-wat2wasm}
|
||||
elif [ -x /opt/wabt/bin/wat2wasm ]; then
|
||||
WAT2WASM=${WAT2WASM:-/opt/wabt/bin/wat2wasm}
|
||||
fi
|
||||
|
||||
${WAT2WASM} -o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
|
||||
|
||||
# aot
|
||||
# wamrc -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
# mv ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
|
||||
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
|
||||
echo "build ${OUT_FILE} success"
|
||||
else
|
||||
echo "build ${OUT_FILE} fail"
|
||||
fi
|
||||
done
|
||||
echo "##################### build wasm apps done"
|
||||
3
samples/shared-module/run.sh
Executable file
3
samples/shared-module/run.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
out/shared-module -f out/wasm-apps/testapp.wasm
|
||||
206
samples/shared-module/src/main.c
Normal file
206
samples/shared-module/src/main.c
Normal file
@ -0,0 +1,206 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "bh_read_file.h"
|
||||
#include "bh_getopt.h"
|
||||
|
||||
void
|
||||
print_usage(void)
|
||||
{
|
||||
fprintf(stdout, "Options:\r\n");
|
||||
fprintf(stdout, " -f [path of wasm file] \n");
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv_main[])
|
||||
{
|
||||
int exit_code = 1;
|
||||
static char global_heap_buf[512 * 1024];
|
||||
char *buffer;
|
||||
char error_buf[128];
|
||||
int opt;
|
||||
char *wasm_path = NULL;
|
||||
|
||||
const unsigned int N = 4;
|
||||
wasm_module_t module = NULL;
|
||||
wasm_module_inst_t module_inst[N];
|
||||
wasm_exec_env_t exec_env[N];
|
||||
const char *name_test_data_drop = "test_data_drop";
|
||||
const char *name_test_elem_drop = "test_elem_drop";
|
||||
wasm_function_inst_t func_test_data_drop[N];
|
||||
wasm_function_inst_t func_test_elem_drop[N];
|
||||
unsigned int i;
|
||||
unsigned int iter;
|
||||
uint32 buf_size, stack_size = 8092, heap_size = 8092;
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
module_inst[i] = NULL;
|
||||
exec_env[i] = NULL;
|
||||
func_test_data_drop[i] = NULL;
|
||||
func_test_elem_drop[i] = NULL;
|
||||
}
|
||||
|
||||
RuntimeInitArgs init_args;
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
wasm_path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
print_usage();
|
||||
return 0;
|
||||
case '?':
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (optind == 1) {
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(&init_args, 0, sizeof(init_args));
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
|
||||
|
||||
if (!buffer) {
|
||||
printf("Open wasm app file [%s] failed.\n", wasm_path);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
if (!module) {
|
||||
printf("Load wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
module_inst[i] = wasm_runtime_instantiate(module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf));
|
||||
|
||||
if (!module_inst[i]) {
|
||||
printf("Instantiate wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
exec_env[i] = wasm_runtime_create_exec_env(module_inst[i], stack_size);
|
||||
if (!exec_env[i]) {
|
||||
printf("Create wasm execution environment failed.\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
func_test_data_drop[i] = wasm_runtime_lookup_function(
|
||||
module_inst[i], name_test_data_drop, NULL);
|
||||
if (!func_test_data_drop[i]) {
|
||||
printf("The wasm function %s is not found.\n", name_test_data_drop);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
func_test_elem_drop[i] = wasm_runtime_lookup_function(
|
||||
module_inst[i], name_test_elem_drop, NULL);
|
||||
if (!func_test_elem_drop[i]) {
|
||||
printf("The wasm function %s is not found.\n", name_test_elem_drop);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
for (iter = 0; iter < 2; iter++) {
|
||||
/*
|
||||
* as we drop data/table in the first iteration,
|
||||
* the later iterations should trap.
|
||||
*/
|
||||
const bool should_trap = iter > 0;
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
uint32_t argv[1] = {};
|
||||
if (wasm_runtime_call_wasm(exec_env[i], func_test_data_drop[i], 0,
|
||||
argv)) {
|
||||
uint32_t result = argv[0];
|
||||
printf(
|
||||
"Native finished calling wasm function: %s, return: %x\n",
|
||||
name_test_data_drop, result);
|
||||
if (result != 0x64636261) { /* "abcd" */
|
||||
printf("unexpected return value\n");
|
||||
goto fail;
|
||||
}
|
||||
if (should_trap) {
|
||||
printf("a trap is expected\n");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
else if (should_trap) {
|
||||
printf("call wasm function %s failed as expected. error: %s\n",
|
||||
name_test_data_drop,
|
||||
wasm_runtime_get_exception(module_inst[i]));
|
||||
}
|
||||
else {
|
||||
printf("call wasm function %s failed. error: %s\n",
|
||||
name_test_data_drop,
|
||||
wasm_runtime_get_exception(module_inst[i]));
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
wasm_runtime_clear_exception(module_inst[i]);
|
||||
|
||||
uint32_t argv[1] = {};
|
||||
if (wasm_runtime_call_wasm(exec_env[i], func_test_elem_drop[i], 0,
|
||||
argv)) {
|
||||
uint32_t result = argv[0];
|
||||
printf(
|
||||
"Native finished calling wasm function: %s, return: %x\n",
|
||||
name_test_elem_drop, result);
|
||||
if (result != 0) {
|
||||
printf("unexpected return value\n");
|
||||
goto fail;
|
||||
}
|
||||
if (should_trap) {
|
||||
printf("a trap is expected\n");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
else if (should_trap) {
|
||||
printf("call wasm function %s failed as expected. error: %s\n",
|
||||
name_test_elem_drop,
|
||||
wasm_runtime_get_exception(module_inst[i]));
|
||||
}
|
||||
else {
|
||||
printf("call wasm function %s failed. error: %s\n",
|
||||
name_test_elem_drop,
|
||||
wasm_runtime_get_exception(module_inst[i]));
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit_code = 0;
|
||||
fail:
|
||||
for (i = 0; i < N; i++) {
|
||||
if (exec_env[i])
|
||||
wasm_runtime_destroy_exec_env(exec_env[i]);
|
||||
if (module_inst[i])
|
||||
wasm_runtime_deinstantiate(module_inst[i]);
|
||||
}
|
||||
if (module)
|
||||
wasm_runtime_unload(module);
|
||||
if (buffer)
|
||||
BH_FREE(buffer);
|
||||
wasm_runtime_destroy();
|
||||
return exit_code;
|
||||
}
|
||||
22
samples/shared-module/wasm-apps/testapp.wat
Normal file
22
samples/shared-module/wasm-apps/testapp.wat
Normal file
@ -0,0 +1,22 @@
|
||||
;; Copyright (C) 2023 Midokura Japan KK. All rights reserved.
|
||||
;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
(module
|
||||
(func (export "test_data_drop") (result i32)
|
||||
(memory.init 0 (i32.const 0) (i32.const 0) (i32.const 4))
|
||||
data.drop 0
|
||||
(i32.load (i32.const 0))
|
||||
)
|
||||
(func (export "test_elem_drop") (result i32)
|
||||
(table.init 0 (i32.const 0) (i32.const 0) (i32.const 4))
|
||||
elem.drop 0
|
||||
i32.const 3
|
||||
table.get 0
|
||||
ref.is_null
|
||||
)
|
||||
(func $f)
|
||||
(memory 1 1)
|
||||
(table 4 4 funcref)
|
||||
(data "abcd")
|
||||
(elem func $f $f $f $f)
|
||||
)
|
||||
Reference in New Issue
Block a user