mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2026-05-10 06:44:18 +00:00
add: unit cases from NVU val
This commit is contained in:
parent
ac2e90a559
commit
55cbcaf5cd
File diff suppressed because it is too large
Load Diff
|
|
@ -5,67 +5,136 @@ cmake_minimum_required(VERSION 3.14)
|
|||
project(wasm-apps)
|
||||
|
||||
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
|
||||
set(WAMRC_ROOT_DIR ${WAMR_ROOT_DIR}/wamr-compiler/build)
|
||||
|
||||
# Find WAMRC
|
||||
set(WAMRC_ROOT_DIR ${WAMR_ROOT_DIR}/wamr-compiler)
|
||||
find_program(WAMRC_BIN wamrc HINTS ${WAMRC_ROOT_DIR}/build REQUIRED)
|
||||
set(CMAKE_SYSTEM_PROCESSOR wasm32)
|
||||
set(CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
|
||||
|
||||
# Set architecture-specific WAMRC flags
|
||||
if (WAMR_BUILD_TARGET STREQUAL "X86_32")
|
||||
set(WAMRC_SHARED_HEAP_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-heap --target=i386)
|
||||
set(WAMRC_SHARED_HEAP_CHAIN_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-chain --target=i386)
|
||||
else ()
|
||||
set(WAMRC_SHARED_HEAP_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-heap)
|
||||
set(WAMRC_SHARED_HEAP_CHAIN_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-chain)
|
||||
if (NOT DEFINED WASI_SDK_DIR)
|
||||
set(WASI_SDK_DIR "/opt/wasi-sdk")
|
||||
endif ()
|
||||
|
||||
#
|
||||
# C -> Wasm
|
||||
#
|
||||
set(CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=8192 -nostdlib -O0")
|
||||
set(CMAKE_C_COMPILER_TARGET "wasm32")
|
||||
set(CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
|
||||
|
||||
set(DEFINED_SYMBOLS
|
||||
"${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
|
||||
|
||||
set(CMAKE_EXE_LINKER_FLAGS
|
||||
"-Wl,--no-entry \
|
||||
-Wl,--initial-memory=65536 \
|
||||
-Wl,--export-all \
|
||||
-Wl,--allow-undefined"
|
||||
)
|
||||
|
||||
if (WAMR_BUILD_TARGET STREQUAL "X86_32")
|
||||
set (WAMR_COMPILER_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-heap --target=i386)
|
||||
set (WAMR_COMPILER_CHAIN_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-chain --target=i386)
|
||||
else ()
|
||||
set (WAMR_COMPILER_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-heap)
|
||||
set (WAMR_COMPILER_CHAIN_FLAGS --opt-level=3 --bounds-checks=1 --enable-shared-chain)
|
||||
endif ()
|
||||
|
||||
function(copy_wasm TARGET_NAME)
|
||||
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/../
|
||||
COMMENT "Copy ${TARGET_NAME} to the same directory of google test"
|
||||
)
|
||||
endfunction()
|
||||
|
||||
function(compile_and_copy_aot_from TARGET_NAME)
|
||||
string(REPLACE ".wasm" ".aot" AOT_TARGET ${TARGET_NAME})
|
||||
string(REPLACE ".wasm" "_chain.aot" AOT_CHAIN_TARGET ${TARGET_NAME})
|
||||
|
||||
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
|
||||
COMMAND ${WAMRC_ROOT_DIR}/wamrc ${WAMR_COMPILER_FLAGS}
|
||||
-o ${AOT_TARGET}
|
||||
${TARGET_NAME}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${AOT_TARGET}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/../
|
||||
COMMAND ${WAMRC_ROOT_DIR}/wamrc ${WAMR_COMPILER_CHAIN_FLAGS}
|
||||
-o ${AOT_CHAIN_TARGET}
|
||||
${TARGET_NAME}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${AOT_CHAIN_TARGET}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/../
|
||||
COMMENT "Compile and copy ${AOT_TARGET} to the same directory of google test"
|
||||
)
|
||||
endfunction()
|
||||
|
||||
add_executable(test.wasm test.c)
|
||||
target_compile_options(test.wasm PUBLIC -nostdlib -O0 -pthread)
|
||||
target_link_options(test.wasm PRIVATE
|
||||
-nostdlib
|
||||
LINKER:--no-entry
|
||||
LINKER:--initial-memory=65536
|
||||
LINKER:--allow-undefined
|
||||
LINKER:--export-all
|
||||
-z stack-size=1024
|
||||
)
|
||||
target_link_libraries(test.wasm)
|
||||
copy_wasm(test.wasm)
|
||||
compile_and_copy_aot_from(test.wasm)
|
||||
|
||||
add_executable(test_addr_conv.wasm test_addr_conv.c)
|
||||
target_compile_options(test_addr_conv.wasm PUBLIC -nostdlib -O0 -pthread)
|
||||
target_link_options(test_addr_conv.wasm PRIVATE
|
||||
-nostdlib
|
||||
LINKER:--no-entry
|
||||
LINKER:--initial-memory=65536
|
||||
LINKER:--allow-undefined
|
||||
LINKER:--export-all
|
||||
-z stack-size=1024
|
||||
target_link_libraries(test_addr_conv.wasm)
|
||||
copy_wasm(test_addr_conv.wasm)
|
||||
compile_and_copy_aot_from(test_addr_conv.wasm)
|
||||
|
||||
# copy and compile aot for bulk memory test
|
||||
set(SOURCE_WASM ${CMAKE_CURRENT_SOURCE_DIR}/bulk-memory/test_bulk_memory.wasm)
|
||||
set(BUILD_WASM ${CMAKE_CURRENT_BINARY_DIR}/../test_bulk_memory.wasm)
|
||||
set(OUTPUT_AOT ${CMAKE_CURRENT_BINARY_DIR}/../test_bulk_memory.aot)
|
||||
set(OUTPUT_CHAIN_AOT ${CMAKE_CURRENT_BINARY_DIR}/../test_bulk_memory_chain.aot)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${BUILD_WASM}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${SOURCE_WASM}
|
||||
${BUILD_WASM}
|
||||
DEPENDS ${SOURCE_WASM}
|
||||
COMMENT "Copying bulk memory WASM to build directory"
|
||||
)
|
||||
|
||||
# Compile AOT files (combined target)
|
||||
add_custom_target(compile_aot ALL
|
||||
COMMAND ${WAMRC_BIN} ${WAMRC_SHARED_HEAP_FLAGS} -o test.aot test.wasm
|
||||
COMMAND ${WAMRC_BIN} ${WAMRC_SHARED_HEAP_CHAIN_FLAGS} -o test_chain.aot test.wasm
|
||||
COMMAND ${WAMRC_BIN} ${WAMRC_SHARED_HEAP_FLAGS} -o test_addr_conv.aot test_addr_conv.wasm
|
||||
COMMAND ${WAMRC_BIN} ${WAMRC_SHARED_HEAP_CHAIN_FLAGS} -o test_addr_conv_chain.aot test_addr_conv.wasm
|
||||
DEPENDS test.wasm test_addr_conv.wasm
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTPUT_AOT}
|
||||
COMMAND ${WAMRC_ROOT_DIR}/wamrc ${WAMR_COMPILER_FLAGS}
|
||||
-o ${OUTPUT_AOT}
|
||||
${BUILD_WASM}
|
||||
COMMAND ${WAMRC_ROOT_DIR}/wamrc ${WAMR_COMPILER_CHAIN_FLAGS}
|
||||
-o ${OUTPUT_CHAIN_AOT}
|
||||
${BUILD_WASM}
|
||||
DEPENDS ${BUILD_WASM}
|
||||
COMMENT "Compiling bulk memory AOT from copied WASM"
|
||||
)
|
||||
|
||||
# Install WASM files
|
||||
set(WASM_FILES
|
||||
${CMAKE_CURRENT_BINARY_DIR}/test.wasm
|
||||
${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv.wasm
|
||||
add_custom_target(compile_bulk_memory_aot ALL
|
||||
DEPENDS ${OUTPUT_AOT}
|
||||
)
|
||||
install(FILES ${WASM_FILES} DESTINATION .)
|
||||
|
||||
# Install AOT files
|
||||
set(AOT_FILES
|
||||
${CMAKE_CURRENT_BINARY_DIR}/test.aot
|
||||
${CMAKE_CURRENT_BINARY_DIR}/test_chain.aot
|
||||
${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv.aot
|
||||
${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv_chain.aot
|
||||
|
||||
# copy val test
|
||||
set(SOURCE_SANDBOX_WASM ${CMAKE_CURRENT_SOURCE_DIR}/nvu_val/test_sandbox.wasm)
|
||||
set(BUILD_SANDBOX_WASM ${CMAKE_CURRENT_BINARY_DIR}/../test_sandbox.wasm)
|
||||
add_custom_command(
|
||||
OUTPUT ${BUILD_SANDBOX_WASM}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${SOURCE_SANDBOX_WASM}
|
||||
${BUILD_SANDBOX_WASM}
|
||||
DEPENDS ${SOURCE_SANDBOX_WASM}
|
||||
COMMENT "Copying test_sandbox.wasm to build directory"
|
||||
)
|
||||
install(FILES ${AOT_FILES} DESTINATION .)
|
||||
add_custom_target(copy_test_sandbox ALL
|
||||
DEPENDS ${BUILD_SANDBOX_WASM}
|
||||
)
|
||||
|
||||
|
||||
set(SOURCE_RUNTIME_WASM ${CMAKE_CURRENT_SOURCE_DIR}/nvu_val/test_runtime.wasm)
|
||||
set(BUILD_RUNTIME_WASM ${CMAKE_CURRENT_BINARY_DIR}/../test_runtime.wasm)
|
||||
add_custom_command(
|
||||
OUTPUT ${BUILD_RUNTIME_WASM}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${SOURCE_RUNTIME_WASM}
|
||||
${BUILD_RUNTIME_WASM}
|
||||
DEPENDS ${SOURCE_RUNTIME_WASM}
|
||||
COMMENT "Copying test_runtime.wasm to build directory"
|
||||
)
|
||||
add_custom_target(copy_test_runtime ALL
|
||||
DEPENDS ${BUILD_RUNTIME_WASM}
|
||||
)
|
||||
BIN
tests/unit/shared-heap/wasm-apps/nvu_val/test_runtime.wasm
Normal file
BIN
tests/unit/shared-heap/wasm-apps/nvu_val/test_runtime.wasm
Normal file
Binary file not shown.
|
|
@ -0,0 +1,7 @@
|
|||
(module
|
||||
;; Export a function for division operation
|
||||
(func (export "divide_by_zero") (param $numerator i32) (result i32)
|
||||
;; Directly perform division by zero
|
||||
(i32.div_s (local.get $numerator) (i32.const 0))
|
||||
)
|
||||
)
|
||||
BIN
tests/unit/shared-heap/wasm-apps/nvu_val/test_sandbox.wasm
Normal file
BIN
tests/unit/shared-heap/wasm-apps/nvu_val/test_sandbox.wasm
Normal file
Binary file not shown.
37
tests/unit/shared-heap/wasm-apps/nvu_val/test_sandbox.wat
Normal file
37
tests/unit/shared-heap/wasm-apps/nvu_val/test_sandbox.wat
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
(module
|
||||
;; Define memory with initial size = 1 page (64KiB) and max size = 4 pages
|
||||
(memory 1 4)
|
||||
|
||||
;; Function to grow memory dynamically
|
||||
(func (export "grow_memory") (param $pages i32) (result i32)
|
||||
(memory.grow (local.get $pages)))
|
||||
|
||||
;; Function to return current memory size in pages
|
||||
(func (export "memory_size") (result i32)
|
||||
(memory.size))
|
||||
|
||||
;; Function to store an integer value at a specific address
|
||||
(func (export "store") (param $addr i32) (param $val i32)
|
||||
(i32.store align=4 (local.get $addr) (local.get $val)))
|
||||
|
||||
;; Function to load an integer value from a specific address
|
||||
(func (export "load") (param $addr i32) (result i32)
|
||||
(i32.load align=4 (local.get $addr)))
|
||||
|
||||
;; Data segment initialization (valid offset)
|
||||
(data (i32.const 0) "valid_data")
|
||||
)
|
||||
|
||||
;; ;; Test initial memory size
|
||||
;; (assert_return (invoke "memory_size") (i32.const 1))
|
||||
|
||||
;; ;; Test memory growth
|
||||
;; (assert_return (invoke "grow_memory" (i32.const 1)) (i32.const 1)) ;; Grow by 1 page
|
||||
;; (assert_return (invoke "memory_size") (i32.const 2)) ;; Verify new size
|
||||
;; (assert_return (invoke "grow_memory" (i32.const 2)) (i32.const 2)) ;; Grow to max size
|
||||
;; (assert_return (invoke "memory_size") (i32.const 4)) ;; Verify max size
|
||||
;; (assert_return (invoke "grow_memory" (i32.const 1)) (i32.const -1)) ;; Exceed max size
|
||||
|
||||
;; ;; Test unaligned store and load
|
||||
;; (invoke "store" (i32.const 4) (i32.const 100)) ;; Store value 100 at address 4
|
||||
;; (assert_return (invoke "load" (i32.const 4)) (i32.const 100)) ;; Load value from address 4
|
||||
Loading…
Reference in New Issue
Block a user