diff --git a/tests/standalone/bfs/bfs.wasm b/tests/standalone/bfs/bfs.wasm new file mode 100755 index 000000000..1081cb7f4 Binary files /dev/null and b/tests/standalone/bfs/bfs.wasm differ diff --git a/tests/standalone/bfs/run.sh b/tests/standalone/bfs/run.sh new file mode 100755 index 000000000..37742edfe --- /dev/null +++ b/tests/standalone/bfs/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run bfs.wasm" + ${IWASM_CMD} --heap-size=0 -f bfs bfs.wasm +else + echo "============> compile bfs.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o bfs.aot bfs.wasm \ + || ${WAMRC_CMD} -o bfs.aot bfs.wasm + echo "============> run bfs.aot" + ${IWASM_CMD} --heap-size=0 -f bfs bfs.aot +fi + diff --git a/tests/standalone/binary-trees/binary_trees.wasm b/tests/standalone/binary-trees/binary_trees.wasm new file mode 100755 index 000000000..0153412cf Binary files /dev/null and b/tests/standalone/binary-trees/binary_trees.wasm differ diff --git a/tests/standalone/binary-trees/run.sh b/tests/standalone/binary-trees/run.sh new file mode 100755 index 000000000..31d64622c --- /dev/null +++ b/tests/standalone/binary-trees/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run binary_trees.wasm" + ${IWASM_CMD} binary_trees.wasm 14 +else + echo "============> compile binary_trees.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o binary_trees.aot binary_trees.wasm \ + || ${WAMRC_CMD} -o binary_trees.aot binary_trees.wasm + echo "============> run binary_trees.aot" + ${IWASM_CMD} binary_trees.aot 18 +fi + diff --git a/tests/standalone/blake3/blake3.wasm b/tests/standalone/blake3/blake3.wasm new file mode 100755 index 000000000..7f6cd7a87 Binary files /dev/null and b/tests/standalone/blake3/blake3.wasm differ diff --git a/tests/standalone/blake3/run.sh b/tests/standalone/blake3/run.sh new file mode 100755 index 000000000..e7e185ede --- /dev/null +++ b/tests/standalone/blake3/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run blake3.wasm" + ${IWASM_CMD} blake3.wasm +else + echo "============> compile blake3.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o blake3.aot blake3.wasm \ + || ${WAMRC_CMD} -o blake3.aot blake3.wasm + echo "============> run blake3.aot" + ${IWASM_CMD} blake3.aot +fi diff --git a/tests/standalone/brotli/brotli.wasm b/tests/standalone/brotli/brotli.wasm new file mode 100644 index 000000000..093c02e9c Binary files /dev/null and b/tests/standalone/brotli/brotli.wasm differ diff --git a/tests/standalone/brotli/run.sh b/tests/standalone/brotli/run.sh new file mode 100755 index 000000000..1ca308eb1 --- /dev/null +++ b/tests/standalone/brotli/run.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +# Download alice29.txt +wget https://raw.githubusercontent.com/google/brotli/master/tests/testdata/alice29.txt + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run brotli.wasm" + cat alice29.txt | ${IWASM_CMD} brotli.wasm -c > alice29.txt.comp +else + echo "============> compile brotli.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o brotli.aot brotli.wasm \ + || ${WAMRC_CMD} -o brotli.aot brotli.wasm + echo "============> run brotli.aot" + cat alice29.txt | ${IWASM_CMD} brotli.aot -c > alice29.txt.comp +fi + diff --git a/tests/standalone/c-ray/c_ray.wasm b/tests/standalone/c-ray/c_ray.wasm new file mode 100644 index 000000000..78b153d1f Binary files /dev/null and b/tests/standalone/c-ray/c_ray.wasm differ diff --git a/tests/standalone/c-ray/run.sh b/tests/standalone/c-ray/run.sh new file mode 100755 index 000000000..a6ae95a43 --- /dev/null +++ b/tests/standalone/c-ray/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run c_ray.wasm" + cat scene | ${IWASM_CMD} c_ray.wasm -s 1024x768 > foo.ppm +else + echo "============> compile c_ray.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o c_ray.aot c_ray.wasm \ + || ${WAMRC_CMD} -o c_ray.aot c_ray.wasm + echo "============> run c_ray.aot" + cat scene | ${IWASM_CMD} c_ray.aot -s 1024x768 > foo.ppm +fi diff --git a/tests/standalone/c-ray/scene b/tests/standalone/c-ray/scene new file mode 100644 index 000000000..082ef1196 --- /dev/null +++ b/tests/standalone/c-ray/scene @@ -0,0 +1,18 @@ +# spheres +# position radius color shininess reflectivity +s -1.5 -0.3 -1 0.7 1.0 0.2 0.05 50.0 0.3 +s 1.5 -0.4 0 0.6 0.1 0.85 1.0 50.0 0.4 + +# walls +s 0 -1000 2 999 0.1 0.2 0.6 80.0 0.8 + +# bouncing ball +s 0 0 2 1 1.0 0.5 0.1 60.0 0.7 + +# lights... +l -50 100 -50 +l 40 40 150 + +# camera (there can be only one!) +# position FOV target +c 0 6 -17 45 0 -1 0 diff --git a/tests/standalone/c-wasm-simd128-example/c_wasm_simd128_example.wasm b/tests/standalone/c-wasm-simd128-example/c_wasm_simd128_example.wasm new file mode 100755 index 000000000..98cc70233 Binary files /dev/null and b/tests/standalone/c-wasm-simd128-example/c_wasm_simd128_example.wasm differ diff --git a/tests/standalone/c-wasm-simd128-example/run.sh b/tests/standalone/c-wasm-simd128-example/run.sh new file mode 100755 index 000000000..a1c646842 --- /dev/null +++ b/tests/standalone/c-wasm-simd128-example/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run c_wasm_simd128_example.wasm" + ${IWASM_CMD} c_wasm_simd128_example.wasm +else + echo "============> compile c_wasm_simd128_example.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o c_wasm_simd128_example.aot c_wasm_simd128_example.wasm \ + || ${WAMRC_CMD} -o c_wasm_simd128_example.aot c_wasm_simd128_example.wasm + echo "============> run c_wasm_simd128_example.aot" + ${IWASM_CMD} c_wasm_simd128_example.aot +fi diff --git a/tests/standalone/cat-sync/cat_sync.wasm b/tests/standalone/cat-sync/cat_sync.wasm new file mode 100755 index 000000000..c53068b85 Binary files /dev/null and b/tests/standalone/cat-sync/cat_sync.wasm differ diff --git a/tests/standalone/cat-sync/run.sh b/tests/standalone/cat-sync/run.sh new file mode 100755 index 000000000..1baadc767 --- /dev/null +++ b/tests/standalone/cat-sync/run.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +dd if=/dev/urandom of=./random bs=4k count=1k + +if [[ $1 != "--aot" ]]; then + echo "============> run cat_sync.wasm" + ${IWASM_CMD} cat_sync.wasm 0 < ./random > image.ppm +else + echo "============> compile cat_sync.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o cat_sync.aot cat_sync.wasm \ + || ${WAMRC_CMD} -o cat_sync.aot cat_sync.wasm + echo "============> run cat_sync.aot" + ${IWASM_CMD} cat_sync.aot 0 < ./random > image.ppm +fi + diff --git a/tests/standalone/coremark/coremark_wasi.wasm b/tests/standalone/coremark/coremark_wasi.wasm new file mode 100644 index 000000000..d2d3c96e8 Binary files /dev/null and b/tests/standalone/coremark/coremark_wasi.wasm differ diff --git a/tests/standalone/coremark/coremark_wasi_nofp.wasm b/tests/standalone/coremark/coremark_wasi_nofp.wasm new file mode 100644 index 000000000..c77716fcf Binary files /dev/null and b/tests/standalone/coremark/coremark_wasi_nofp.wasm differ diff --git a/tests/standalone/coremark/run.sh b/tests/standalone/coremark/run.sh new file mode 100755 index 000000000..f2641eb4f --- /dev/null +++ b/tests/standalone/coremark/run.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run coremark_wasi_nofp.wasm" + ${IWASM_CMD} coremark_wasi_nofp.wasm + echo "============> run coremark_wasi.wasm" + ${IWASM_CMD} coremark_wasi.wasm +else + echo "============> compile coremark_wasi_nofp.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o coremark_wasi_nofp.aot coremark_wasi_nofp.wasm \ + || ${WAMRC_CMD} -o coremark_wasi_nofp.aot coremark_wasi_nofp.wasm + echo "============> run coremark_wasi_nofp.aot" + ${IWASM_CMD} coremark_wasi_nofp.aot + + echo "============> compile coremark_wasi.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o coremark_wasi.aot coremark_wasi.wasm \ + || ${WAMRC_CMD} -o coremark_wasi.aot coremark_wasi.wasm + echo "============> run coremark_wasi.aot" + ${IWASM_CMD} coremark_wasi.aot +fi + diff --git a/tests/standalone/dhrystone/dhrystone.wasm b/tests/standalone/dhrystone/dhrystone.wasm new file mode 100755 index 000000000..7c31879a0 Binary files /dev/null and b/tests/standalone/dhrystone/dhrystone.wasm differ diff --git a/tests/standalone/dhrystone/run.sh b/tests/standalone/dhrystone/run.sh new file mode 100755 index 000000000..3570e5f7d --- /dev/null +++ b/tests/standalone/dhrystone/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi + +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run dhrystone.wasm" + ${IWASM_CMD} --heap-size=16384 dhrystone.wasm +else + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o dhrystone.aot dhrystone.wasm \ + || ${WAMRC_CMD} -o dhrystone.aot dhrystone.wasm + echo "============> run dhrystone.aot" + ${IWASM_CMD} --heap-size=16384 dhrystone.aot +fi diff --git a/tests/standalone/dump-call-stack/run.sh b/tests/standalone/dump-call-stack/run.sh new file mode 100755 index 000000000..64c508887 --- /dev/null +++ b/tests/standalone/dump-call-stack/run.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +readonly WAMR_DIR="$PWD/../../.." +readonly IWASM_CMD="$PWD/build/iwasm" +readonly WAMRC_CMD="$PWD/../../../wamr-compiler/build/wamrc" + +PLATFORM=$(uname -s | tr A-Z a-z) + +if [[ $1 == "--classic-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0" +elif [[ $1 == "--fast-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1" +elif [[ $1 == "--fast-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1" +elif [[ $1 == "--jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_JIT=1" +elif [[ $1 == "--multi-tier-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1" +fi + +TARGET="X86_64" +if [[ $3 = "X86_32" ]]; then + TARGET="X86_32" +fi + +echo "============> test dump-call-stack" + +rm -fr build && mkdir build && cd build +cmake ${WAMR_DIR}/product-mini/platforms/${PLATFORM} ${CMAKE_FLAGS} \ + -DWAMR_BUILD_DUMP_CALL_STACK=1 -DWAMR_BUILD_TARGET=${TARGET} +make -j ${nproc} > /dev/null 2>&1 +cd .. + +echo "============> compile test-malloc to wasm" +/opt/wasi-sdk/bin/clang -O3 -o test-malloc.wasm wasm-app/main.c \ + -Wl,--export-all -Wl,--export=__heap_base,--export=__data_end + +if [[ $1 != "--aot" ]]; then + echo "============> run test-malloc.wasm" + ${IWASM_CMD} test-malloc.wasm +else + echo "============> compile test-malloc.wasm to aot" + ${WAMRC_CMD} --enable-dump-call-stack --emit-custom-sections=name -o test-malloc.aot test-malloc.wasm + echo "============> run test-malloc.aot" + ${IWASM_CMD} test-malloc.aot +fi diff --git a/tests/standalone/dump-call-stack/wasm-app/main.c b/tests/standalone/dump-call-stack/wasm-app/main.c new file mode 100644 index 000000000..8c87a4f82 --- /dev/null +++ b/tests/standalone/dump-call-stack/wasm-app/main.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + printf("Hello World!\n"); + + char *buf = "1234", *buf1 = "12345"; + + printf("result is %f \n", 22.00); + + printf("Hello World!\n"); + + buf = malloc(1024); + + printf("malloc func ptr: %p\n", malloc); + + printf("##buf: %p\n", buf); + + if (!buf) { + printf("malloc buf failed\n"); + return -1; + } + + printf("buf ptr: %p\n", buf); + + sprintf(buf, "%s", "1234\n"); + printf("buf: %s", buf); + + buf1 = strdup(buf); + printf("buf1: %s\n", buf1); + + free(buf1); + free(buf); + + printf("buf[65536]: %c\n", buf[65536 * 10]); + + return 0; +} diff --git a/tests/standalone/dump-mem-profiling/run.sh b/tests/standalone/dump-mem-profiling/run.sh new file mode 100755 index 000000000..afe4f7099 --- /dev/null +++ b/tests/standalone/dump-mem-profiling/run.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +readonly WAMR_DIR="$PWD/../../.." +readonly IWASM_CMD="$PWD/build/iwasm" +readonly WAMRC_CMD="$PWD/../../../wamr-compiler/build/wamrc" + +PLATFORM=$(uname -s | tr A-Z a-z) + +if [[ $1 == "--classic-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0" +elif [[ $1 == "--fast-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1" +elif [[ $1 == "--fast-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1" +elif [[ $1 == "--jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_JIT=1" +elif [[ $1 == "--multi-tier-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1" +fi + +TARGET="X86_64" +if [[ $3 = "X86_32" ]]; then + TARGET="X86_32" +fi + +echo "============> test dump-mem-profiling" + +rm -fr build && mkdir build && cd build +cmake ${WAMR_DIR}/product-mini/platforms/${PLATFORM} ${CMAKE_FLAGS} \ + -DWAMR_BUILD_DUMP_CALL_STACK=1 -DWAMR_BUILD_MEMORY_PROFILING=1 \ + -DWAMR_BUILD_TARGET=${TARGET} +make -j ${nproc} > /dev/null 2>&1 +cd .. + +echo "============> compile test-malloc to wasm" +/opt/wasi-sdk/bin/clang -O3 -o test-malloc.wasm wasm-app/main.c \ + -Wl,--export-all -Wl,--export=__heap_base,--export=__data_end + +if [[ $1 != "--aot" ]]; then + echo "============> run test-malloc.wasm" + ${IWASM_CMD} test-malloc.wasm +else + echo "============> compile test-malloc.wasm to aot" + ${WAMRC_CMD} --enable-dump-call-stack -o test-malloc.aot test-malloc.wasm + echo "============> run test-malloc.aot" + ${IWASM_CMD} test-malloc.aot +fi diff --git a/tests/standalone/dump-mem-profiling/wasm-app/main.c b/tests/standalone/dump-mem-profiling/wasm-app/main.c new file mode 100644 index 000000000..8c87a4f82 --- /dev/null +++ b/tests/standalone/dump-mem-profiling/wasm-app/main.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + printf("Hello World!\n"); + + char *buf = "1234", *buf1 = "12345"; + + printf("result is %f \n", 22.00); + + printf("Hello World!\n"); + + buf = malloc(1024); + + printf("malloc func ptr: %p\n", malloc); + + printf("##buf: %p\n", buf); + + if (!buf) { + printf("malloc buf failed\n"); + return -1; + } + + printf("buf ptr: %p\n", buf); + + sprintf(buf, "%s", "1234\n"); + printf("buf: %s", buf); + + buf1 = strdup(buf); + printf("buf1: %s\n", buf1); + + free(buf1); + free(buf); + + printf("buf[65536]: %c\n", buf[65536 * 10]); + + return 0; +} diff --git a/tests/standalone/dump-perf-profiling/run.sh b/tests/standalone/dump-perf-profiling/run.sh new file mode 100755 index 000000000..26edda314 --- /dev/null +++ b/tests/standalone/dump-perf-profiling/run.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +readonly WAMR_DIR="$PWD/../../.." +readonly IWASM_CMD="$PWD/build/iwasm" +readonly WAMRC_CMD="$PWD/../../../wamr-compiler/build/wamrc" + +PLATFORM=$(uname -s | tr A-Z a-z) + +if [[ $1 == "--classic-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0" +elif [[ $1 == "--fast-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1" +elif [[ $1 == "--fast-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1" +elif [[ $1 == "--jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_JIT=1" +elif [[ $1 == "--multi-tier-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1" +fi + +TARGET="X86_64" +if [[ $3 = "X86_32" ]]; then + TARGET="X86_32" +fi + +echo "============> test dump-perf-profiling" + +rm -fr build && mkdir build && cd build +cmake ${WAMR_DIR}/product-mini/platforms/${PLATFORM} ${CMAKE_FLAGS} \ + -DWAMR_BUILD_PERF_PROFILING=1 -DWAMR_BUILD_TARGET=${TARGET} +make -j ${nproc} > /dev/null 2>&1 +cd .. + +echo "============> compile test-malloc to wasm" +/opt/wasi-sdk/bin/clang -O3 -o test-malloc.wasm wasm-app/main.c \ + -Wl,--export-all -Wl,--export=__heap_base,--export=__data_end + +if [[ $1 != "--aot" ]]; then + echo "============> run test-malloc.wasm" + ${IWASM_CMD} test-malloc.wasm +else + echo "============> compile test-malloc.wasm to aot" + ${WAMRC_CMD} --enable-perf-profiling -o test-malloc.aot test-malloc.wasm + echo "============> run test-malloc.aot" + ${IWASM_CMD} test-malloc.aot +fi diff --git a/tests/standalone/dump-perf-profiling/wasm-app/main.c b/tests/standalone/dump-perf-profiling/wasm-app/main.c new file mode 100644 index 000000000..8c87a4f82 --- /dev/null +++ b/tests/standalone/dump-perf-profiling/wasm-app/main.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + printf("Hello World!\n"); + + char *buf = "1234", *buf1 = "12345"; + + printf("result is %f \n", 22.00); + + printf("Hello World!\n"); + + buf = malloc(1024); + + printf("malloc func ptr: %p\n", malloc); + + printf("##buf: %p\n", buf); + + if (!buf) { + printf("malloc buf failed\n"); + return -1; + } + + printf("buf ptr: %p\n", buf); + + sprintf(buf, "%s", "1234\n"); + printf("buf: %s", buf); + + buf1 = strdup(buf); + printf("buf1: %s\n", buf1); + + free(buf1); + free(buf); + + printf("buf[65536]: %c\n", buf[65536 * 10]); + + return 0; +} diff --git a/tests/standalone/fannkuch-redux/fannkuch_redux.wasm b/tests/standalone/fannkuch-redux/fannkuch_redux.wasm new file mode 100755 index 000000000..884a43e48 Binary files /dev/null and b/tests/standalone/fannkuch-redux/fannkuch_redux.wasm differ diff --git a/tests/standalone/fannkuch-redux/run.sh b/tests/standalone/fannkuch-redux/run.sh new file mode 100755 index 000000000..8e0c4312a --- /dev/null +++ b/tests/standalone/fannkuch-redux/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run fannkuch_redux.wasm" + ${IWASM_CMD} fannkuch_redux.wasm 10 +else + echo "============> compile fannkuch_redux.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o fannkuch_redux.aot fannkuch_redux.wasm \ + || ${WAMRC_CMD} -o fannkuch_redux.aot fannkuch_redux.wasm + echo "============> run fannkuch_redux.aot" + ${IWASM_CMD} fannkuch_redux.aot 11 +fi + diff --git a/tests/standalone/fasta/fasta.wasm b/tests/standalone/fasta/fasta.wasm new file mode 100755 index 000000000..47d11ba4a Binary files /dev/null and b/tests/standalone/fasta/fasta.wasm differ diff --git a/tests/standalone/fasta/run.sh b/tests/standalone/fasta/run.sh new file mode 100755 index 000000000..be277739a --- /dev/null +++ b/tests/standalone/fasta/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run fasta.wasm" + ${IWASM_CMD} fasta.wasm 1000000 > image.ppm +else + echo "============> compile fasta.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o fasta.aot fasta.wasm \ + || ${WAMRC_CMD} -o fasta.aot fasta.wasm + echo "============> run fasta.aot" + ${IWASM_CMD} fasta.aot 10000000 > image.ppm +fi + diff --git a/tests/standalone/gcc-loops/gcc_loops.wasm b/tests/standalone/gcc-loops/gcc_loops.wasm new file mode 100755 index 000000000..7ef1809ef Binary files /dev/null and b/tests/standalone/gcc-loops/gcc_loops.wasm differ diff --git a/tests/standalone/gcc-loops/run.sh b/tests/standalone/gcc-loops/run.sh new file mode 100755 index 000000000..f288a9682 --- /dev/null +++ b/tests/standalone/gcc-loops/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run gcc_loops.wasm" + ${IWASM_CMD} gcc_loops.wasm 1 +else + echo "============> compile gcc_loops.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o gcc_loops.aot gcc_loops.wasm \ + || ${WAMRC_CMD} -o gcc_loops.aot gcc_loops.wasm + echo "============> run gcc_loops.aot" + ${IWASM_CMD} gcc_loops.aot 1 +fi + diff --git a/tests/standalone/hashset/HashSet.wasm b/tests/standalone/hashset/HashSet.wasm new file mode 100755 index 000000000..e54a998fc Binary files /dev/null and b/tests/standalone/hashset/HashSet.wasm differ diff --git a/tests/standalone/hashset/run.sh b/tests/standalone/hashset/run.sh new file mode 100755 index 000000000..2528006a1 --- /dev/null +++ b/tests/standalone/hashset/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run HashSet.wasm" + ${IWASM_CMD} HashSet.wasm +else + echo "============> compile HashSet.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o HashSet.aot HashSet.wasm \ + || ${WAMRC_CMD} -o HashSet.aot HashSet.wasm + echo "============> run HashSet.aot" + ${IWASM_CMD} HashSet.aot +fi + diff --git a/tests/standalone/iwasm-wamrc/run.sh b/tests/standalone/iwasm-wamrc/run.sh new file mode 100755 index 000000000..8f34b04fb --- /dev/null +++ b/tests/standalone/iwasm-wamrc/run.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +${IWASM_CMD} --help +echo "" + +${WAMRC_CMD} --help +echo "" diff --git a/tests/standalone/mandelbrot/mandel.wasm b/tests/standalone/mandelbrot/mandel.wasm new file mode 100755 index 000000000..559c5f9ee Binary files /dev/null and b/tests/standalone/mandelbrot/mandel.wasm differ diff --git a/tests/standalone/mandelbrot/mandel_dd.wasm b/tests/standalone/mandelbrot/mandel_dd.wasm new file mode 100755 index 000000000..75753d6d8 Binary files /dev/null and b/tests/standalone/mandelbrot/mandel_dd.wasm differ diff --git a/tests/standalone/mandelbrot/run.sh b/tests/standalone/mandelbrot/run.sh new file mode 100755 index 000000000..7f3fb6a30 --- /dev/null +++ b/tests/standalone/mandelbrot/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run mandel_dd.wasm" + ${IWASM_CMD} mandel_dd.wasm > image.ppm +else + echo "============> compile mandel_dd.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o mandel_dd.aot mandel_dd.wasm \ + || ${WAMRC_CMD} -o mandel_dd.aot mandel_dd.wasm + echo "============> run mandel_dd.aot" + ${IWASM_CMD} mandel_dd.aot > image.ppm +fi + diff --git a/tests/standalone/nbody/nbody.wasm b/tests/standalone/nbody/nbody.wasm new file mode 100755 index 000000000..681eec857 Binary files /dev/null and b/tests/standalone/nbody/nbody.wasm differ diff --git a/tests/standalone/nbody/run.sh b/tests/standalone/nbody/run.sh new file mode 100755 index 000000000..2a1a06855 --- /dev/null +++ b/tests/standalone/nbody/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run nbody.wasm" + ${IWASM_CMD} nbody.wasm 5000000 +else + echo "============> compile nbody.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o nbody.aot nbody.wasm \ + || ${WAMRC_CMD} -o nbody.aot nbody.wasm + echo "============> run nbody.aot" + ${IWASM_CMD} nbody.aot 50000000 +fi + diff --git a/tests/standalone/raymarcher/raymarcher.wasm b/tests/standalone/raymarcher/raymarcher.wasm new file mode 100755 index 000000000..a94a5a5e8 Binary files /dev/null and b/tests/standalone/raymarcher/raymarcher.wasm differ diff --git a/tests/standalone/raymarcher/run.sh b/tests/standalone/raymarcher/run.sh new file mode 100755 index 000000000..d1f99d888 --- /dev/null +++ b/tests/standalone/raymarcher/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run raymarcher.wasm" + ${IWASM_CMD} raymarcher.wasm > a.log 2>&1 +else + echo "============> compile raymarcher.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o raymarcher.aot raymarcher.wasm \ + || ${WAMRC_CMD} -o raymarcher.aot raymarcher.wasm + echo "============> run raymarcher.aot" + ${IWASM_CMD} raymarcher.aot > a.log 2>&1 +fi diff --git a/tests/standalone/simple/run.sh b/tests/standalone/simple/run.sh new file mode 100755 index 000000000..fb25f17f8 --- /dev/null +++ b/tests/standalone/simple/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run simple.wasm" + ${IWASM_CMD} simple.wasm +else + echo "============> compile simple.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o simple.aot simple.wasm \ + || ${WAMRC_CMD} -o simple.aot simple.wasm + echo "============> run simple.aot" + ${IWASM_CMD} simple.aot +fi diff --git a/tests/standalone/simple/simple.wasm b/tests/standalone/simple/simple.wasm new file mode 100755 index 000000000..5b5891edf Binary files /dev/null and b/tests/standalone/simple/simple.wasm differ diff --git a/tests/standalone/smallpt/run.sh b/tests/standalone/smallpt/run.sh new file mode 100755 index 000000000..60a092d06 --- /dev/null +++ b/tests/standalone/smallpt/run.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run smallpt.wasm" + ${IWASM_CMD} --stack-size=2048000 smallpt.wasm > image.ppm + echo "============> run smallpt_ex.wasm" + ${IWASM_CMD} --stack-size=2048000 smallpt_ex.wasm > image.ppm +else + echo "============> compile smallpt.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o smallpt.aot smallpt.wasm \ + || ${WAMRC_CMD} -o smallpt.aot smallpt.wasm + echo "============> run smallpt.aot" + ${IWASM_CMD} smallpt.aot > image.ppm + echo "============> compile smallpt_ex.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o smallpt_ex.aot smallpt_ex.wasm \ + || ${WAMRC_CMD} -o smallpt_ex.aot smallpt_ex.wasm + echo "============> run smallpt_ex.aot" + ${IWASM_CMD} smallpt_ex.aot > image.ppm +fi + diff --git a/tests/standalone/smallpt/smallpt.wasm b/tests/standalone/smallpt/smallpt.wasm new file mode 100755 index 000000000..855c64c8a Binary files /dev/null and b/tests/standalone/smallpt/smallpt.wasm differ diff --git a/tests/standalone/smallpt/smallpt_ex.wasm b/tests/standalone/smallpt/smallpt_ex.wasm new file mode 100755 index 000000000..d39f097c0 Binary files /dev/null and b/tests/standalone/smallpt/smallpt_ex.wasm differ diff --git a/tests/standalone/standalone.sh b/tests/standalone/standalone.sh new file mode 100755 index 000000000..75788894d --- /dev/null +++ b/tests/standalone/standalone.sh @@ -0,0 +1,131 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +# get every run.sh under standalone sub-direcotry +# +# for f in folders_include_run_sh +# cd sub-direcoty +# ./run.sh + +# Usage: +# ./standalone.sh --aot|--classic-interp|--fast-interp|--fast-jit|--jit|--multi-tier-jit \ +# [--sgx|--no-sgx] [--thread|--no-thread] [--simd|--no-simd] +# +# Note: +# The order of the options can not be exchanged +# + +if [[ $1 == "--classic-interp" ]]; then + # long time to run gcc-loops, smallpt and tsf with classic interpreter + IGNORE_LIST+=("gcc-loops" "smallpt" "tsf") +fi +if [[ $1 == "--classic-interp" || $1 == "--fast-interp" ]]; then + # long time to run mandelbrot with interpreter + IGNORE_LIST+=("mandelbrot") +fi +if [[ $1 == "--jit" ]]; then + # long time to compile test-printf with llvm-jit + IGNORE_LIST+=("test-printf") +fi +if [[ $2 == "--sgx" ]]; then + # require to allocate/mmap large memory in sgx + # need to modify Enclave.config.xml + IGNORE_LIST+=("stream") +fi +if [[ $3 != "--thread" ]]; then + # test-pthread requires thread support + IGNORE_LIST+=("test-pthread") +fi +if [[ $4 != "--simd" || $1 == "--classic-interp" || $1 == "--fast-interp" + || $1 == "--fast-jit" ]]; then + # blake3 and c-wasm-simd128-example require SIMD support + IGNORE_LIST+=("blake3" "c-wasm-simd128-example") +fi + +if [[ -z $5 ]]; then + TARGET="X86_64" +else + TARGET=$5 +fi + +function contain() +{ + # [$1, $-1) + local list=${@:0:${#}} + # [$-1] + local item=${@:${#}} + [[ ${list} =~ (^| )${item}($| ) ]] && return 0 || return 1 +} + +total_num=0 +failed_num=0 +failed_list=() +passed_num=0 +passed_list=() + +echo "*-------------- start testing standalone test cases --------------*" +for f in $(find . -name "run.sh" -type f | sort -n | awk -F "/" '{print $2}') +do + if contain "${IGNORE_LIST[@]}" ${f};then + echo "ignore ${f} case" + continue + else + cd $f + if [[ $2 == "--sgx" ]]; then + ./run.sh $1 --sgx ${TARGET} + else + ./run.sh $1 --no-sgx ${TARGET} + fi + + retval=$? + if [ ${retval} -ne 0 ] && [ ${retval} -ne 1 ]; then + echo "" + echo "run $f test failed, 'run.sh' returns ${retval}" + echo "" + failed_num=$((failed_num + 1)) + failed_list+=("$f") + else + echo "" + echo "run $f test successfully, 'run.sh' returns ${retval}" + echo "" + passed_num=$((passed_num + 1)) + passed_list+=("$f") + fi + cd .. + fi +done +total_num=$((failed_num+passed_num)) +echo "*-------------- standalone test cases finish --------------*" +echo "" +echo "" +echo "*================ Standalone Test Report Start ==============*" +echo "" +echo "Total: ${total_num}" +echo "Passed: ${passed_num}" +echo "Failed: ${failed_num}" + + +if [ ${passed_num} -gt 0 ]; then + echo "" + echo "******************************************************************" + echo "Passed cases list:" + echo "" + for passed_case in "${passed_list[@]}"; do + echo " $passed_case" + done +fi + +if [ ${failed_num} -gt 0 ]; then + echo "" + echo "******************************************************************" + echo "Failed cases list:" + for failed_case in "${failed_list[@]}"; do + echo " $failed_case" + done +fi + +echo "" +echo "*================ Standalone Test Report End ==============*" diff --git a/tests/standalone/stream/run.sh b/tests/standalone/stream/run.sh new file mode 100755 index 000000000..4f9d9ca80 --- /dev/null +++ b/tests/standalone/stream/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run stream.wasm" + ${IWASM_CMD} stream.wasm +else + echo "============> compile stream.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o stream.aot stream.wasm \ + || ${WAMRC_CMD} -o stream.aot stream.wasm + echo "============> run stream.aot" + ${IWASM_CMD} stream.aot +fi + diff --git a/tests/standalone/stream/stream.wasm b/tests/standalone/stream/stream.wasm new file mode 100644 index 000000000..fc6d268cf Binary files /dev/null and b/tests/standalone/stream/stream.wasm differ diff --git a/tests/standalone/string-view/run.sh b/tests/standalone/string-view/run.sh new file mode 100755 index 000000000..f25830fdd --- /dev/null +++ b/tests/standalone/string-view/run.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_string.wasm" + ${IWASM_CMD} test_string.wasm + ${IWASM_CMD} test_string_view.wasm +else + echo "============> compile test_string.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_string.aot test_string.wasm \ + || ${WAMRC_CMD} -o test_string.aot test_string.wasm + echo "============> compile test_string_view.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_string_view.aot test_string_view.wasm \ + || ${WAMRC_CMD} -o test_string_view.aot test_string_view.wasm + echo "============> run test_string.aot" + ${IWASM_CMD} test_string.aot + echo "============> run test_string_view.aot" + ${IWASM_CMD} test_string_view.aot +fi + diff --git a/tests/standalone/string-view/test_string.wasm b/tests/standalone/string-view/test_string.wasm new file mode 100755 index 000000000..e3d79e818 Binary files /dev/null and b/tests/standalone/string-view/test_string.wasm differ diff --git a/tests/standalone/string-view/test_string_view.wasm b/tests/standalone/string-view/test_string_view.wasm new file mode 100755 index 000000000..112e306d4 Binary files /dev/null and b/tests/standalone/string-view/test_string_view.wasm differ diff --git a/tests/standalone/test-aes/run.sh b/tests/standalone/test-aes/run.sh new file mode 100755 index 000000000..101d904e3 --- /dev/null +++ b/tests/standalone/test-aes/run.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_aes.wasm" + if [[ $1 != "--multi-tier-jit" ]]; then + ${IWASM_CMD} --heap-size=16384 test_aes.wasm + else + echo "============> run test_aes.wasm with interp" + ${IWASM_CMD} --heap-size=16384 --interp test_aes.wasm + echo "============> run test_aes.wasm with fast-jit" + ${IWASM_CMD} --heap-size=16384 --fast-jit test_aes.wasm + echo "============> run test_aes.wasm with llvm-jit" + ${IWASM_CMD} --heap-size=16384 --llvm-jit test_aes.wasm + echo "============> run test_aes.wasm with multi-tier-jit" + ${IWASM_CMD} --heap-size=16384 --multi-tier-jit test_aes.wasm + fi +else + echo "============> compile test_aes.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_aes.aot test_aes.wasm \ + || ${WAMRC_CMD} -o test_aes.aot test_aes.wasm + echo "============> run test_aes.aot" + ${IWASM_CMD} --heap-size=16384 test_aes.aot +fi + diff --git a/tests/standalone/test-aes/test_aes.wasm b/tests/standalone/test-aes/test_aes.wasm new file mode 100755 index 000000000..47c431eff Binary files /dev/null and b/tests/standalone/test-aes/test_aes.wasm differ diff --git a/tests/standalone/test-go/run.sh b/tests/standalone/test-go/run.sh new file mode 100755 index 000000000..4280695be --- /dev/null +++ b/tests/standalone/test-go/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_go.wasm" + ${IWASM_CMD} test_go.wasm +else + echo "============> compile test_go.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_go.aot test_go.wasm \ + || ${WAMRC_CMD} -o test_go.aot test_go.wasm + echo "============> run test_go.aot" + ${IWASM_CMD} test_go.aot +fi + diff --git a/tests/standalone/test-go/test_go.wasm b/tests/standalone/test-go/test_go.wasm new file mode 100755 index 000000000..8e9335a61 Binary files /dev/null and b/tests/standalone/test-go/test_go.wasm differ diff --git a/tests/standalone/test-invoke-native/CMakeLists.txt b/tests/standalone/test-invoke-native/CMakeLists.txt new file mode 100644 index 000000000..54cec530a --- /dev/null +++ b/tests/standalone/test-invoke-native/CMakeLists.txt @@ -0,0 +1,127 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required (VERSION 3.14) + +project (iwasm) +# set (CMAKE_VERBOSE_MAKEFILE 1) + +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 "") + +set (CMAKE_C_STANDARD 99) +set (CMAKE_CXX_STANDARD 17) + +# Set WAMR_BUILD_TARGET, currently values supported: +# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", "MIPS", "XTENSA" +if (NOT DEFINED WAMR_BUILD_TARGET) + if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + 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") + else () + # Build as X86_32 by default in 32-bit platform + set (WAMR_BUILD_TARGET "X86_32") + endif () +endif () + +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif () + +if (NOT DEFINED WAMR_BUILD_INTERP) + # Enable Interpreter by default + set (WAMR_BUILD_INTERP 1) +endif () + +if (NOT DEFINED WAMR_BUILD_AOT) + # Enable AOT by default. + set (WAMR_BUILD_AOT 1) +endif () + +if (NOT DEFINED WAMR_BUILD_JIT) + # Disable JIT by default. + set (WAMR_BUILD_JIT 0) +endif () + +if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN) + # Enable libc builtin support by default + set (WAMR_BUILD_LIBC_BUILTIN 1) +endif () + +if (NOT DEFINED WAMR_BUILD_LIBC_WASI) + # Enable libc wasi support by default + set (WAMR_BUILD_LIBC_WASI 1) +endif () + +if (NOT DEFINED WAMR_BUILD_FAST_INTERP) + # Enable fast interpreter + set (WAMR_BUILD_FAST_INTERP 1) +endif () + +if (NOT DEFINED WAMR_BUILD_MULTI_MODULE) + # Enable multiple modules + set (WAMR_BUILD_MULTI_MODULE 0) +endif () + +if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD) + # Disable pthread library by default + set (WAMR_BUILD_LIB_PTHREAD 0) +endif () + +if (NOT DEFINED WAMR_BUILD_MINI_LOADER) + # Disable wasm mini loader by default + set (WAMR_BUILD_MINI_LOADER 0) +endif () + +if (NOT DEFINED WAMR_BUILD_SIMD) + # Disable SIMD by default + set (WAMR_BUILD_SIMD 0) +endif () + +set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..) + +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) +add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) + +set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") +if (NOT WAMR_BUILD_PLATFORM STREQUAL "darwin") + set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE") +endif () + +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow") +# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion") + +set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused") + +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 () + +# The following flags are to enhance security, but it may impact performance, +# we disable them by default. +#if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") +# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv -D_FORTIFY_SOURCE=2") +#endif () +#set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4") +#set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now") + +include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) + +add_executable (test_invoke_native main.c test_invoke_native.c ${UNCOMMON_SHARED_SOURCE}) + +install (TARGETS test_invoke_native DESTINATION bin) + +target_link_libraries (test_invoke_native vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread) + diff --git a/tests/standalone/test-invoke-native/main.c b/tests/standalone/test-invoke-native/main.c new file mode 100644 index 000000000..d0f5c7b5a --- /dev/null +++ b/tests/standalone/test-invoke-native/main.c @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +#include "bh_platform.h" +#include "wasm_export.h" + +static char global_heap_buf[10 * 1024 * 1024] = { 0 }; + +void +test_invoke_native(); + +int +main(int argc, char *argv[]) +{ + RuntimeInitArgs init_args; + + memset(&init_args, 0, sizeof(RuntimeInitArgs)); + + 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); + + /* initialize runtime environment */ + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); + return -1; + } + + test_invoke_native(); + + /* destroy runtime environment */ + wasm_runtime_destroy(); + return 0; +} diff --git a/tests/standalone/test-invoke-native/run.sh b/tests/standalone/test-invoke-native/run.sh new file mode 100755 index 000000000..f786aee0f --- /dev/null +++ b/tests/standalone/test-invoke-native/run.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $1 == "--classic-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0" +elif [[ $1 == "--fast-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1" +elif [[ $1 == "--fast-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1" +elif [[ $1 == "--jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_JIT=1" +elif [[ $1 == "--multi-tier-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1" +fi + +TARGET="X86_64" +if [[ $3 = "X86_32" ]]; then + TARGET="X86_32" +fi + +echo "============> test dump-invoke-native" + +rm -fr build +mkdir build && cd build +cmake .. ${CMAKE_FLAGS} -DWAMR_BUILD_TARGET=${TARGET} +make -j ${nproc} > /dev/null 2>&1 +cd .. + +echo "============> run test-invoke-native" +./build/test_invoke_native diff --git a/tests/standalone/test-invoke-native/test_invoke_native.c b/tests/standalone/test-invoke-native/test_invoke_native.c new file mode 100644 index 000000000..46794a87c --- /dev/null +++ b/tests/standalone/test-invoke-native/test_invoke_native.c @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_runtime.h" + +static void +test_native_args1(WASMModuleInstance *module_inst, int arg0, uint64_t arg1, + float arg2, double arg3, int arg4, int64_t arg5, int64_t arg6, + int arg7, double arg8, float arg9, int arg10, double arg11, + float arg12, int64_t arg13, uint64_t arg14, float arg15, + double arg16, int64_t arg17, uint64_t arg18, float arg19) +{ + printf("##test_native_args1 result:\n"); + printf("arg0: 0x%X, arg1: 0x%X%08X, arg2: %f, arg3: %f\n", arg0, + (int32)(arg1 >> 32), (int32)arg1, arg2, arg3); + printf("arg4: 0x%X, arg5: 0x%X%08X, arg6: 0x%X%08X, arg7: 0x%X\n", arg4, + (int32)(arg5 >> 32), (int32)arg5, (int32)(arg6 >> 32), (int32)arg6, + arg7); + printf("arg8: %f, arg9: %f, arg10: 0x%X, arg11: %f\n", arg8, arg9, arg10, + arg11); + printf("arg12: %f, arg13: 0x%X%08X, arg14: 0x%X%08X, arg15: %f\n", arg12, + (int32)(arg13 >> 32), (int32)arg13, (int32)(arg14 >> 32), + (int32)arg14, arg15); + printf("arg16: %f, arg17: 0x%X%08X, arg18: 0x%X%08X, arg19: %f\n", arg16, + (int32)(arg17 >> 32), (int32)arg17, (int32)(arg18 >> 32), + (int32)arg18, arg19); +} + +static void +test_native_args2(WASMModuleInstance *module_inst, uint64_t arg1, float arg2, + double arg3, int arg4, int64_t arg5, int64_t arg6, int arg7, + double arg8, float arg9, int arg10, double arg11, float arg12, + int64_t arg13, uint64_t arg14, float arg15, double arg16, + int64_t arg17, uint64_t arg18, float arg19) +{ + printf("##test_native_args2 result:\n"); + printf("arg1: 0x%X%08X, arg2: %f, arg3: %f\n", (int32)(arg1 >> 32), + (int32)arg1, arg2, arg3); + printf("arg4: 0x%X, arg5: 0x%X%08X, arg6: 0x%X%08X, arg7: 0x%X\n", arg4, + (int32)(arg5 >> 32), (int32)arg5, (int32)(arg6 >> 32), (int32)arg6, + arg7); + printf("arg8: %f, arg9: %f, arg10: 0x%X, arg11: %f\n", arg8, arg9, arg10, + arg11); + printf("arg12: %f, arg13: 0x%X%08X, arg14: 0x%X%08X, arg15: %f\n", arg12, + (int32)(arg13 >> 32), (int32)arg13, (int32)(arg14 >> 32), + (int32)arg14, arg15); + printf("arg16: %f, arg17: 0x%X%08X, arg18: 0x%X%08X, arg19: %f\n", arg16, + (int32)(arg17 >> 32), (int32)arg17, (int32)(arg18 >> 32), + (int32)arg18, arg19); +} + +static int32 +test_return_i32(WASMModuleInstance *module_inst) +{ + return 0x12345678; +} + +static int64 +test_return_i64(WASMModuleInstance *module_inst) +{ + return 0x12345678ABCDEFFFll; +} + +static float32 +test_return_f32(WASMModuleInstance *module_inst) +{ + return 1234.5678f; +} + +static float64 +test_return_f64(WASMModuleInstance *module_inst) +{ + return 87654321.12345678; +} + +#define STORE_I64(addr, value) \ + do { \ + union { \ + int64 val; \ + uint32 parts[2]; \ + } u; \ + u.val = (int64)(value); \ + (addr)[0] = u.parts[0]; \ + (addr)[1] = u.parts[1]; \ + } while (0) + +#define STORE_F64(addr, value) \ + do { \ + union { \ + float64 val; \ + uint32 parts[2]; \ + } u; \ + u.val = (value); \ + (addr)[0] = u.parts[0]; \ + (addr)[1] = u.parts[1]; \ + } while (0) + +#define I32 VALUE_TYPE_I32 +#define I64 VALUE_TYPE_I64 +#define F32 VALUE_TYPE_F32 +#define F64 VALUE_TYPE_F64 + +typedef struct WASMTypeTest { + uint16 param_count; + /* only one result is supported currently */ + uint16 result_count; + uint16 param_cell_num; + uint16 ret_cell_num; + /* types of params and results */ + uint8 types[128]; +} WASMTypeTest; + +void +test_invoke_native() +{ + uint32 argv[128], *p = argv; + WASMTypeTest func_type1 = { 20, 0, 0, 0, { I32, I64, F32, F64, I32, + I64, I64, I32, F64, F32, + I32, F64, F32, I64, I64, + F32, F64, I64, I64, F32 } }; + WASMTypeTest func_type2 = { 19, + 0, + 0, + 0, + { I64, F32, F64, I32, I64, I64, I32, F64, F32, + I32, F64, F32, I64, I64, F32, F64, I64, I64, + F32 } }; + WASMTypeTest func_type_i32 = { 0, 1, 0, 0, { I32 } }; + WASMTypeTest func_type_i64 = { 0, 1, 0, 0, { I64 } }; + WASMTypeTest func_type_f32 = { 0, 1, 0, 0, { F32 } }; + WASMTypeTest func_type_f64 = { 0, 1, 0, 0, { F64 } }; + WASMModuleInstance module_inst = { 0 }; + WASMExecEnv exec_env = { 0 }; + + module_inst.module_type = Wasm_Module_Bytecode; + exec_env.module_inst = (WASMModuleInstanceCommon *)&module_inst; + + *p++ = 0x12345678; + STORE_I64(p, 0xFFFFFFFF87654321ll); + p += 2; + *(float32 *)p++ = 1234.5678f; + STORE_F64(p, 567890.1234); + p += 2; + + *p++ = 0x11111111; + STORE_I64(p, 0xAAAAAAAABBBBBBBBll); + p += 2; + STORE_I64(p, 0x7788888899ll); + p += 2; + *p++ = 0x3456; + + STORE_F64(p, 8888.7777); + p += 2; + *(float32 *)p++ = 7777.8888f; + *p++ = 0x66666; + STORE_F64(p, 999999.88888); + p += 2; + + *(float32 *)p++ = 555555.22f; + STORE_I64(p, 0xBBBBBAAAAAAAAll); + p += 2; + STORE_I64(p, 0x3333AAAABBBBll); + p += 2; + *(float32 *)p++ = 88.77f; + + STORE_F64(p, 9999.01234); + p += 2; + STORE_I64(p, 0x1111122222222ll); + p += 2; + STORE_I64(p, 0x444455555555ll); + p += 2; + *(float32 *)p++ = 77.88f; + + wasm_runtime_invoke_native(&exec_env, test_native_args1, + (WASMType *)&func_type1, NULL, NULL, argv, + p - argv, argv); + printf("\n"); + + wasm_runtime_invoke_native(&exec_env, test_native_args2, + (WASMType *)&func_type2, NULL, NULL, argv + 1, + p - argv - 1, argv); + printf("\n"); + + wasm_runtime_invoke_native(&exec_env, test_return_i32, + (WASMType *)&func_type_i32, NULL, NULL, NULL, 0, + argv); + printf("test_return_i32: 0x%X\n\n", argv[0]); + + wasm_runtime_invoke_native(&exec_env, test_return_i64, + (WASMType *)&func_type_i64, NULL, NULL, NULL, 0, + argv); + printf("test_return_i64: 0x%X%08X\n\n", (int32)((*(int64 *)argv) >> 32), + (int32)(*(int64 *)argv)); + + wasm_runtime_invoke_native(&exec_env, test_return_f32, + (WASMType *)&func_type_f32, NULL, NULL, NULL, 0, + argv); + printf("test_return_f32: %f\n\n", *(float32 *)argv); + + wasm_runtime_invoke_native(&exec_env, test_return_f64, + (WASMType *)&func_type_f64, NULL, NULL, NULL, 0, + argv); + printf("test_return_f64: %f\n\n", *(float64 *)argv); +} diff --git a/tests/standalone/test-malloc/main.c b/tests/standalone/test-malloc/main.c new file mode 100644 index 000000000..9405700d8 --- /dev/null +++ b/tests/standalone/test-malloc/main.c @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + char *buf, *buf1; + + printf("Hello World!\n"); + + buf = malloc(1024); + + printf("malloc func ptr: %p\n", malloc); + + printf("##buf: %p\n", buf); + + if (!buf) { + printf("malloc buf failed\n"); + return -1; + } + + printf("buf ptr: %p\n", buf); + + memset(buf, 0, 1024); + + sprintf(buf, "%s", "1234\n"); + printf("buf: %s", buf); + + buf1 = strdup(buf); + printf("buf1: %s\n", buf1); + + free(buf1); + free(buf); + + printf("buf[65536]: %c\n", buf[65536]); + + return 0; +} diff --git a/tests/standalone/test-malloc/run.sh b/tests/standalone/test-malloc/run.sh new file mode 100755 index 000000000..1649c345f --- /dev/null +++ b/tests/standalone/test-malloc/run.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +echo "============> compile test-malloc to wasm" +/opt/wasi-sdk/bin/clang -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \ + -o test-malloc.wasm main.c \ + -Wl,--export=main -Wl,--export=__main_argc_argv \ + -Wl,--export=__heap_base,--export=__data_end -Wl,--no-entry \ + -nostdlib -Wl,--allow-undefined + +if [[ $1 != "--aot" ]]; then + echo "============> run test-malloc.wasm" + ${IWASM_CMD} --heap-size=16384 test-malloc.wasm +else + echo "============> compile test-malloc.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test-malloc.aot test-malloc.wasm \ + || ${WAMRC_CMD} -o test-malloc.aot test-malloc.wasm + echo "============> run test-malloc.aot" + ${IWASM_CMD} --heap-size=16384 test-malloc.aot +fi + diff --git a/tests/standalone/test-module-malloc/CMakeLists.txt b/tests/standalone/test-module-malloc/CMakeLists.txt new file mode 100644 index 000000000..8081f9fa4 --- /dev/null +++ b/tests/standalone/test-module-malloc/CMakeLists.txt @@ -0,0 +1,88 @@ +# 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(native_lib) + +################ 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 are: +# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", +# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]" +if (NOT DEFINED WAMR_BUILD_TARGET) + if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)") + set (WAMR_BUILD_TARGET "AARCH64") + elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64") + set (WAMR_BUILD_TARGET "RISCV64") + elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) + # Build as X86_64 by default in 64-bit platform + set (WAMR_BUILD_TARGET "X86_64") + elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) + # Build as X86_32 by default in 32-bit platform + set (WAMR_BUILD_TARGET "X86_32") + else () + message(SEND_ERROR "Unsupported build target platform!") + endif () +endif () + +if (NOT CMAKE_BUILD_TYPE) + set (CMAKE_BUILD_TYPE Release) +endif () + +if (NOT WAMR_BUILD_INTERP) + set (WAMR_BUILD_INTERP 1) +endif () +if (NOT WAMR_BUILD_AOT) + set (WAMR_BUILD_AOT 1) +endif () +set (WAMR_BUILD_LIBC_BUILTIN 1) +set (WAMR_BUILD_LIBC_WASI 1) + +# compiling and linking 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") + +################ wasm application ############### +add_subdirectory(wasm-app) + +# build out libiwasm +set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..) +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) + +# Note: we build libiwasm as a shared library here so that it can be +# shared between iwasm and native libraries. +add_library(libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE}) +set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm) + +################ wamr runtime ################### +include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) + +set (RUNTIME_SOURCE_ALL + ${WAMR_ROOT_DIR}/product-mini/platforms/posix/main.c + ${UNCOMMON_SHARED_SOURCE} +) + +add_executable (iwasm ${RUNTIME_SOURCE_ALL}) + +check_pie_supported() +set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON) + +target_link_libraries(iwasm libiwasm -lpthread -lm -ldl) + +################ native libraries ############### +add_library (test_module_malloc SHARED test_module_malloc.c) diff --git a/tests/standalone/test-module-malloc/run.sh b/tests/standalone/test-module-malloc/run.sh new file mode 100755 index 000000000..644544f08 --- /dev/null +++ b/tests/standalone/test-module-malloc/run.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $1 == "--classic-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0" +elif [[ $1 == "--fast-interp" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1" +elif [[ $1 == "--fast-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1" +elif [[ $1 == "--jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_JIT=1" +elif [[ $1 == "--multi-tier-jit" ]]; then + CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1" +fi + +TARGET="X86_64" +if [[ $3 = "X86_32" ]]; then + TARGET="X86_32" + WAMRC_FLAGS="--target=i386" +fi + +readonly WAMRC_CMD="$PWD/../../../wamr-compiler/build/wamrc" + +echo "============> test test-module-malloc" + +if [[ $1 != "--aot" ]]; then + rm -fr build && mkdir build && cd build + cmake .. -DWAMR_BUILD_TARGET=${TARGET} + make -j > /dev/null 2>&1 + ./iwasm --native-lib=./libtest_module_malloc.so wasm-app/test.wasm + if [ ${TARGET} == "X86_64" ]; then + echo "============> test test-module-malloc with hw bound check disabled" + cd .. && rm -fr build && mkdir build && cd build + cmake .. -DWAMR_BUILD_TARGET=${TARGET} -DWAMR_DISABLE_HW_BOUND_CHECK=1 + make clean + make -j > /dev/null 2>&1 + ./iwasm --native-lib=./libtest_module_malloc.so wasm-app/test.wasm + fi +else + rm -fr build && mkdir build && cd build + cmake .. -DWAMR_BUILD_TARGET=${TARGET} + make -j > /dev/null 2>&1 + ${WAMRC_CMD} ${WAMRC_FLAGS} -o wasm-app/test.aot wasm-app/test.wasm + ./iwasm --native-lib=./libtest_module_malloc.so wasm-app/test.aot + if [ ${TARGET} == "X86_64" ]; then + echo "============> test test-module-malloc with hw bound check disabled" + cd .. && rm -fr build && mkdir build && cd build + cmake .. -DWAMR_BUILD_TARGET=${TARGET} -DWAMR_DISABLE_HW_BOUND_CHECK=1 + make clean + make -j > /dev/null 2>&1 + ${WAMRC_CMD} ${WAMRC_FLAGS} --bounds-checks=1 -o wasm-app/test.aot wasm-app/test.wasm + ./iwasm --native-lib=./libtest_module_malloc.so wasm-app/test.aot + fi +fi + diff --git a/tests/standalone/test-module-malloc/test_module_malloc.c b/tests/standalone/test-module-malloc/test_module_malloc.c new file mode 100644 index 000000000..6863f101a --- /dev/null +++ b/tests/standalone/test-module-malloc/test_module_malloc.c @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +#include "wasm_export.h" + +static uint32_t +test_module_malloc_wrapper(wasm_exec_env_t exec_env, uint32_t buf_size) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + return wasm_runtime_module_malloc(module_inst, buf_size, NULL); +} + +static void +test_module_free_wrapper(wasm_exec_env_t exec_env, uint32_t ptr) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + + wasm_runtime_module_free(module_inst, ptr); +} + +/* clang-format off */ +#define REG_NATIVE_FUNC(func_name, signature) \ + { #func_name, func_name##_wrapper, signature, NULL } + +static NativeSymbol native_symbols[] = { + REG_NATIVE_FUNC(test_module_malloc, "(i)i"), + REG_NATIVE_FUNC(test_module_free, "(i)") +}; +/* clang-format on */ + +uint32_t +get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols) +{ + *p_module_name = "env"; + *p_native_symbols = native_symbols; + return sizeof(native_symbols) / sizeof(NativeSymbol); +} diff --git a/tests/standalone/test-module-malloc/wasm-app/CMakeLists.txt b/tests/standalone/test-module-malloc/wasm-app/CMakeLists.txt new file mode 100644 index 000000000..c87187709 --- /dev/null +++ b/tests/standalone/test-module-malloc/wasm-app/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required(VERSION 3.0) +project(wasm-app) + +set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) + +if (APPLE) + set (HAVE_FLAG_SEARCH_PATHS_FIRST 0) + set (CMAKE_C_LINK_FLAGS "") + set (CMAKE_CXX_LINK_FLAGS "") +endif () + +set (CMAKE_SYSTEM_PROCESSOR wasm32) + +if (NOT DEFINED WASI_SDK_DIR) + set (WASI_SDK_DIR "/opt/wasi-sdk") +endif () + +set (CMAKE_C_COMPILER_TARGET "wasm32") +set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang") + +set (CMAKE_EXE_LINKER_FLAGS + "-Wl,--max-memory=131072 -z stack-size=8192 \ + -Wl,--export=malloc \ + -Wl,--export=free \ + -Wl,--allow-undefined" +) + +add_executable(test.wasm main.c) +target_link_libraries(test.wasm) diff --git a/tests/standalone/test-module-malloc/wasm-app/main.c b/tests/standalone/test-module-malloc/wasm-app/main.c new file mode 100644 index 000000000..fa8ad4d79 --- /dev/null +++ b/tests/standalone/test-module-malloc/wasm-app/main.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +void * +test_module_malloc(unsigned buf_size); + +void +test_module_free(void *buf); + +int +main(int argc, char **argv) +{ + char *buf = NULL; + + printf("Hello World!\n"); + + buf = test_module_malloc(1024); + if (buf) { + printf("module_malloc(1024) success, return %p\n", buf); + snprintf(buf, 1024, "%s", "Hello world!\n"); + } + else { + printf("module_malloc(1024) failed!\n"); + return -1; + } + + test_module_free(buf); + + buf = test_module_malloc(32 * 1024 * 1024); + if (!buf) { + printf("module_malloc(32MB) failed => expected, not an issue\n"); + } + else { + printf("module_malloc(32MB) success, unexpected!\n"); + return -1; + } + + return 0; +} diff --git a/tests/standalone/test-parson/run.sh b/tests/standalone/test-parson/run.sh new file mode 100755 index 000000000..409992b6e --- /dev/null +++ b/tests/standalone/test-parson/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_parson.wasm" + ${IWASM_CMD} --heap-size=16384 test_parson.wasm +else + echo "============> compile test_parson.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_parson.aot test_parson.wasm \ + || ${WAMRC_CMD} -o test_parson.aot test_parson.wasm + echo "============> run test_parson.aot" + ${IWASM_CMD} --heap-size=16384 test_parson.aot +fi + diff --git a/tests/standalone/test-parson/test_parson.wasm b/tests/standalone/test-parson/test_parson.wasm new file mode 100755 index 000000000..0b38a6560 Binary files /dev/null and b/tests/standalone/test-parson/test_parson.wasm differ diff --git a/tests/standalone/test-printf/run.sh b/tests/standalone/test-printf/run.sh new file mode 100755 index 000000000..959bad273 --- /dev/null +++ b/tests/standalone/test-printf/run.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_printf_builtin.wasm" + ${IWASM_CMD} test_printf_builtin.wasm > a.log 2>&1 + echo "============> run test_printf_wasi.wasm" + ${IWASM_CMD} test_printf_wasi.wasm > b.log 2>&1 +else + echo "============> compile test_printf_builtin.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_printf_builtin.aot test_printf_builtin.wasm \ + || ${WAMRC_CMD} -o test_printf_builtin.aot test_printf_builtin.wasm + echo "============> run test_printf_builtin.aot" + ${IWASM_CMD} test_printf_builtin.aot > a.log 2>&1 + echo "============> compile test_printf_wasi.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_printf_wasi.aot test_printf_wasi.wasm \ + || ${WAMRC_CMD} -o test_printf_wasi.aot test_printf_wasi.wasm + echo "============> run test_printf_wasi.aot" + ${IWASM_CMD} test_printf_wasi.aot > b.log 2>&1 +fi + diff --git a/tests/standalone/test-printf/test_printf_builtin.wasm b/tests/standalone/test-printf/test_printf_builtin.wasm new file mode 100644 index 000000000..99af31473 Binary files /dev/null and b/tests/standalone/test-printf/test_printf_builtin.wasm differ diff --git a/tests/standalone/test-printf/test_printf_wasi.wasm b/tests/standalone/test-printf/test_printf_wasi.wasm new file mode 100644 index 000000000..40914f10a Binary files /dev/null and b/tests/standalone/test-printf/test_printf_wasi.wasm differ diff --git a/tests/standalone/test-pthread/create_join.c b/tests/standalone/test-pthread/create_join.c new file mode 100644 index 000000000..a1cdee025 --- /dev/null +++ b/tests/standalone/test-pthread/create_join.c @@ -0,0 +1,68 @@ +/* + * This file is copied from https://web.dev/articles/wasm-threads + */ + +#include +#include + +/* Calculate Fibonacci numbers shared function */ +int +fibonacci(int iterations) +{ + int val = 1; + int last = 0; + + if (iterations == 0) { + return 0; + } + for (int i = 1; i < iterations; i++) { + int seq; + + seq = val + last; + last = val; + val = seq; + } + return val; +} + +int bg = 42; + +/* Start function for the background thread */ +void * +bg_func(void *arg) +{ + int *iter = (void *)arg; + + *iter = fibonacci(*iter); + printf("bg number: %d\n", *iter); + return arg; +} + +/* Foreground thread and main entry point */ +int +main(int argc, char *argv[]) +{ + int fg_val = 54; + int bg_val = 42; + pthread_t bg_thread; + + /* Create the background thread */ + if (pthread_create(&bg_thread, NULL, bg_func, &bg_val)) { + printf("Thread create failed"); + return 1; + } + + /* Calculate on the foreground thread */ + fg_val = fibonacci(fg_val); + + /* Wait for background thread to finish */ + if (pthread_join(bg_thread, NULL)) { + printf("Thread join failed"); + return 2; + } + + /* Show the result from background and foreground threads */ + printf("Fib(42) is %d, Fib(6 * 9) is %d\n", bg_val, fg_val); + + return 0; +} diff --git a/tests/standalone/test-pthread/create_join.wasm b/tests/standalone/test-pthread/create_join.wasm new file mode 100755 index 000000000..8c2cf7db5 Binary files /dev/null and b/tests/standalone/test-pthread/create_join.wasm differ diff --git a/tests/standalone/test-pthread/main_thread_return.c b/tests/standalone/test-pthread/main_thread_return.c new file mode 100644 index 000000000..db208cbb7 --- /dev/null +++ b/tests/standalone/test-pthread/main_thread_return.c @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +/* Start function for the background thread */ +void * +bg_func(void *arg) +{ + uint32_t i; + + printf("Thread start.\n"); + + for (i = 0; i < 100000000; i++) + arg += i; + return arg; +} + +/* Foreground thread and main entry point */ +int +main(int argc, char *argv[]) +{ + pthread_t bg_thread; + + if (pthread_create(&bg_thread, NULL, bg_func, NULL)) { + printf("Thread create failed"); + return 1; + } + printf("Thread created.\n"); + + /* Return the process directly, runtime should terminate + the sub threads and exit the whole wasm module */ + printf("Process exit.\n"); + printf("Test success.\n"); + + return 0; +} diff --git a/tests/standalone/test-pthread/main_thread_return.wasm b/tests/standalone/test-pthread/main_thread_return.wasm new file mode 100755 index 000000000..eafb5b1ae Binary files /dev/null and b/tests/standalone/test-pthread/main_thread_return.wasm differ diff --git a/tests/standalone/test-pthread/pthread_cond.c b/tests/standalone/test-pthread/pthread_cond.c new file mode 100644 index 000000000..47e49625e --- /dev/null +++ b/tests/standalone/test-pthread/pthread_cond.c @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +pthread_mutex_t mutex; +static pthread_cond_t cond; + +typedef struct test { + int test1; + int test2; +} Test; + +Test t1; + +void * +thread(void *arg) +{ + pthread_mutex_lock(&mutex); + printf("thread signal\n"); + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + + return NULL; +} + +int +main() +{ + pthread_t p; + + pthread_mutex_init(&mutex, NULL); + pthread_cond_init(&cond, NULL); + + printf("parent begin\n"); + + pthread_mutex_lock(&mutex); + pthread_create(&p, NULL, thread, NULL); + + pthread_cond_wait(&cond, &mutex); + pthread_mutex_unlock(&mutex); + + printf("parend end\n"); + + pthread_join(p, NULL); + + pthread_cond_destroy(&cond); + pthread_mutex_destroy(&mutex); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/pthread_cond.wasm b/tests/standalone/test-pthread/pthread_cond.wasm new file mode 100755 index 000000000..35c72c469 Binary files /dev/null and b/tests/standalone/test-pthread/pthread_cond.wasm differ diff --git a/tests/standalone/test-pthread/pthread_key.c b/tests/standalone/test-pthread/pthread_key.c new file mode 100644 index 000000000..58bb07bc7 --- /dev/null +++ b/tests/standalone/test-pthread/pthread_key.c @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +pthread_key_t key; +pthread_mutex_t lock; + +struct test_struct { + int i; + int k; +}; + +typedef struct point { + int x; + int y; +} Point; + +void * +child1(void *arg) +{ + struct test_struct struct_data; + struct_data.i = 10; + struct_data.k = 5; + pthread_mutex_lock(&lock); + pthread_setspecific(key, &struct_data); + printf("thread1--address of struct_data is --> %p\n", &(struct_data)); + printf("thread1--from pthread_getspecific(key) get the pointer and it " + "points to --> %p\n", + (struct test_struct *)pthread_getspecific(key)); + printf("thread1--from pthread_getspecific(key) get the pointer and print " + "it's content:\nstruct_data.i:%d\nstruct_data.k: %d\n", + ((struct test_struct *)pthread_getspecific(key))->i, + ((struct test_struct *)pthread_getspecific(key))->k); + printf("------------------------------------------------------\n"); + pthread_mutex_unlock(&lock); +} + +void * +child2(void *arg) +{ + int temp = 20; + + pthread_mutex_lock(&lock); + printf("thread2--temp's address is %p\n", &temp); + pthread_setspecific(key, &temp); + printf("thread2--from pthread_getspecific(key) get the pointer and it " + "points to --> %p\n", + (int *)pthread_getspecific(key)); + printf("thread2--from pthread_getspecific(key) get the pointer and print " + "it's content --> temp:%d\n", + *((int *)pthread_getspecific(key))); + printf("------------------------------------------------------\n"); + pthread_mutex_unlock(&lock); +} + +int +main(void) +{ + Point p; + p.x = 10; + p.y = 20; + pthread_t tid1, tid2; + + pthread_mutex_init(&lock, NULL); + + pthread_key_create(&key, NULL); + pthread_create(&tid1, NULL, child1, NULL); + pthread_create(&tid2, NULL, child2, NULL); + + pthread_mutex_lock(&lock); + printf("main--temp's address is %p\n", &p); + pthread_setspecific(key, &p); + printf("main--from pthread_getspecific(key) get the pointer and it points " + "to --> %p\n", + (int *)pthread_getspecific(key)); + printf("main--from pthread_getspecific(key) get the pointer and print " + "it's content --> x:%d, y:%d\n", + ((Point *)pthread_getspecific(key))->x, + ((Point *)pthread_getspecific(key))->y); + pthread_mutex_unlock(&lock); + + pthread_join(tid1, NULL); + pthread_join(tid2, NULL); + pthread_key_delete(key); + return 0; +} diff --git a/tests/standalone/test-pthread/pthread_key.wasm b/tests/standalone/test-pthread/pthread_key.wasm new file mode 100755 index 000000000..168bf4a0e Binary files /dev/null and b/tests/standalone/test-pthread/pthread_key.wasm differ diff --git a/tests/standalone/test-pthread/pthread_mutex.c b/tests/standalone/test-pthread/pthread_mutex.c new file mode 100755 index 000000000..4ac5be20d --- /dev/null +++ b/tests/standalone/test-pthread/pthread_mutex.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +typedef struct ct_sum { + int sum; + pthread_mutex_t lock; +} ct_sum; + +void * +add1(void *cnt) +{ + pthread_mutex_lock(&(((ct_sum *)cnt)->lock)); + printf("get lock thread1 id:%lu\n", pthread_self()); + + int i; + for (i = 0; i < 50; i++) { + (*(ct_sum *)cnt).sum += i; + } + + pthread_mutex_unlock(&(((ct_sum *)cnt)->lock)); + pthread_exit(NULL); + return 0; +} + +void * +add2(void *cnt) +{ + int i; + cnt = (ct_sum *)cnt; + pthread_mutex_lock(&(((ct_sum *)cnt)->lock)); + printf("get lock thread2 id:%lu\n", pthread_self()); + + for (i = 50; i < 101; i++) { + (*(ct_sum *)cnt).sum += i; + } + + pthread_mutex_unlock(&(((ct_sum *)cnt)->lock)); + pthread_exit(NULL); + return 0; +} + +int +main(void) +{ + int i; + pthread_t ptid1, ptid2; + int sum = 0; + ct_sum cnt; + pthread_mutex_init(&(cnt.lock), NULL); + cnt.sum = 0; + + pthread_create(&ptid1, NULL, add1, &cnt); + pthread_create(&ptid2, NULL, add2, &cnt); + + pthread_join(ptid1, NULL); + pthread_join(ptid2, NULL); + + pthread_mutex_lock(&(cnt.lock)); + printf("sum %d\n", cnt.sum); + pthread_mutex_unlock(&(cnt.lock)); + + pthread_mutex_destroy(&(cnt.lock)); + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/pthread_mutex.wasm b/tests/standalone/test-pthread/pthread_mutex.wasm new file mode 100755 index 000000000..d4d7180d5 Binary files /dev/null and b/tests/standalone/test-pthread/pthread_mutex.wasm differ diff --git a/tests/standalone/test-pthread/run.sh b/tests/standalone/test-pthread/run.sh new file mode 100755 index 000000000..cd7e07cad --- /dev/null +++ b/tests/standalone/test-pthread/run.sh @@ -0,0 +1,133 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]]; then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run create_join.wasm" + ${IWASM_CMD} --heap-size=16384 create_join.wasm + echo "============> run main_thread_return.wasm" + ${IWASM_CMD} --heap-size=16384 main_thread_return.wasm + echo "============> run thread_cancel.wasm" + ${IWASM_CMD} --heap-size=16384 thread_cancel.wasm + echo "============> run thread_exit.wasm" + ${IWASM_CMD} --heap-size=16384 thread_exit.wasm + echo "============> run wasi.wasm" + ${IWASM_CMD} --heap-size=16384 wasi.wasm + echo "============> run pthread_mutex.wasm" + ${IWASM_CMD} --heap-size=16384 pthread_mutex.wasm + echo "============> run pthread_cond.wasm" + ${IWASM_CMD} --heap-size=16384 pthread_cond.wasm + echo "============> run pthread_key.wasm" + ${IWASM_CMD} --heap-size=16384 pthread_key.wasm +else + echo "============> compile create_join.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o create_join.aot create_join.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o create_join.aot create_join.wasm + fi + echo "============> run create_join.aot" + ${IWASM_CMD} --heap-size=16384 create_join.aot + + echo "============> compile main_thread_return.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o main_thread_return.aot main_thread_return.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o main_thread_return.aot main_thread_return.wasm + fi + echo "============> run main_thread_return.aot" + ${IWASM_CMD} --heap-size=16384 main_thread_return.aot + + echo "============> compile thread_cancel.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o thread_cancel.aot thread_cancel.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o thread_cancel.aot thread_cancel.wasm + fi + echo "============> run thread_cancel.aot" + ${IWASM_CMD} --heap-size=16384 thread_cancel.aot + + echo "============> compile thread_exit.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o thread_exit.aot thread_exit.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o thread_exit.aot thread_exit.wasm + fi + echo "============> run thread_exit.aot" + ${IWASM_CMD} --heap-size=16384 thread_exit.aot + + echo "============> compile wasi.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o wasi.aot wasi.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o wasi.aot wasi.wasm + fi + echo "============> run wasi.aot" + ${IWASM_CMD} --heap-size=16384 wasi.aot + + echo "============> compile pthread_mutex.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o pthread_mutex.aot pthread_mutex.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o pthread_mutex.aot pthread_mutex.wasm + fi + echo "============> run pthread_mutex.aot" + ${IWASM_CMD} --heap-size=16384 pthread_mutex.aot + + echo "============> compile pthread_cond.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o pthread_cond.aot pthread_cond.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o pthread_cond.aot pthread_cond.wasm + fi + echo "============> run pthread_cond.aot" + ${IWASM_CMD} --heap-size=16384 pthread_cond.aot + + echo "============> compile pthread_key.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread -o pthread_key.aot pthread_key.wasm + else + ${WAMRC_CMD} --enable-multi-thread -o pthread_key.aot pthread_key.wasm + fi + echo "============> run pthread_key.aot" + ${IWASM_CMD} --heap-size=16384 pthread_key.aot +fi + +cd threads-opcode-wasm-apps +rm -rf build && mkdir build && cd build +cmake .. && make +cd ../.. + +wasm_files=$(ls threads-opcode-wasm-apps/build/*.wasm) + +for wasm_file in $wasm_files; do + wasm_file_name="${wasm_file%.wasm}" + # avoid keep printing warning: warning: SGX pthread_cond_timedwait isn't supported, calling pthread_cond_wait instead! + if [[ $2 == "--sgx" ]] && [[ ${wasm_file_name} == *"atomic_wait_notify"* ]]; then + echo "============> didn't run ${wasm_file_name} on sgx aot mode, it will output too much warning info" + continue + fi + + if [[ $1 != "--aot" ]]; then + echo "============> run ${wasm_file_name}.wasm" + ${IWASM_CMD} --heap-size=16384 ${wasm_file_name}.wasm + else + echo "============> compile ${wasm_file_name}.wasm to aot" + if [[ $2 == "--sgx" ]]; then + ${WAMRC_CMD} -sgx --enable-multi-thread --opt-level=0 -o ${wasm_file_name}.aot ${wasm_file_name}.wasm + else + ${WAMRC_CMD} --enable-multi-thread --opt-level=0 -o ${wasm_file_name}.aot ${wasm_file_name}.wasm + fi + echo "============> run ${wasm_file_name}.aot" + ${IWASM_CMD} --heap-size=16384 ${wasm_file_name}.aot + fi +done diff --git a/tests/standalone/test-pthread/thread_cancel.c b/tests/standalone/test-pthread/thread_cancel.c new file mode 100644 index 000000000..648fdc7fa --- /dev/null +++ b/tests/standalone/test-pthread/thread_cancel.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +/* Start function for the background thread */ +void * +bg_func(void *arg) +{ + printf("Thread start.\n"); + /* This thread will never exit unless canceled by other thread */ + while (1) + ; +} + +/* Foreground thread and main entry point */ +int +main(int argc, char *argv[]) +{ + pthread_t bg_thread; + + if (pthread_create(&bg_thread, NULL, bg_func, NULL)) { + printf("Thread create failed"); + return 1; + } + printf("Thread created.\n"); + + /* Cancel the sub thread */ + pthread_cancel(bg_thread); + + printf("Sub-thread Canceled.\n"); + printf("Test success.\n"); + + return 0; +} diff --git a/tests/standalone/test-pthread/thread_cancel.wasm b/tests/standalone/test-pthread/thread_cancel.wasm new file mode 100755 index 000000000..1ef9f5a5b Binary files /dev/null and b/tests/standalone/test-pthread/thread_cancel.wasm differ diff --git a/tests/standalone/test-pthread/thread_exit.c b/tests/standalone/test-pthread/thread_exit.c new file mode 100644 index 000000000..697a376ea --- /dev/null +++ b/tests/standalone/test-pthread/thread_exit.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +/* Start function for the background thread */ +void * +bg_func(void *arg) +{ + printf("Thread start.\n"); + while (1) { + pthread_exit(NULL); + } +} + +/* Foreground thread and main entry point */ +int +main(int argc, char *argv[]) +{ + pthread_t bg_thread; + + if (pthread_create(&bg_thread, NULL, bg_func, NULL)) { + printf("Thread create failed"); + return 1; + } + printf("Thread created.\n"); + + /* Wait for background thread to finish */ + if (pthread_join(bg_thread, NULL)) { + printf("Thread join failed"); + return 2; + } + + printf("Sub-thread exit.\n"); + printf("Test success.\n"); + + return 0; +} diff --git a/tests/standalone/test-pthread/thread_exit.wasm b/tests/standalone/test-pthread/thread_exit.wasm new file mode 100755 index 000000000..db7607435 Binary files /dev/null and b/tests/standalone/test-pthread/thread_exit.wasm differ diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/CMakeLists.txt b/tests/standalone/test-pthread/threads-opcode-wasm-apps/CMakeLists.txt new file mode 100644 index 000000000..4f8e21ac8 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/CMakeLists.txt @@ -0,0 +1,61 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required(VERSION 3.14) +project(wasm-apps) + +set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../wamr) + +if (APPLE) + set (HAVE_FLAG_SEARCH_PATHS_FIRST 0) + set (CMAKE_C_LINK_FLAGS "") + set (CMAKE_CXX_LINK_FLAGS "") +endif () +# have to be debug for peterson lock algorithm +set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_SYSTEM_PROCESSOR wasm32) +set (CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot) + +if (NOT DEFINED WASI_SDK_DIR) + set (WASI_SDK_DIR "/opt/wasi-sdk") +endif () + +set (CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments") +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=32768") +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,--shared-memory,--max-memory=131072, \ + -Wl,--no-entry,--strip-all, \ + -Wl,--export=__heap_base,--export=__data_end \ + -Wl,--export=__wasm_call_ctors \ + -Wl,--export=main -Wl,--export=__main_argc_argv \ + -Wl,--allow-undefined" + #-Wl,--allow-undefined-file=${DEFINED_SYMBOLS}" +) + +add_executable(atomic_add_sub.wasm atomic_add_sub.c) +target_link_libraries(atomic_add_sub.wasm) + +# use peterson lock to test the atomicity of opcode: fence store xchg +add_executable(peterson_native.wasm peterson_native.c) +target_link_libraries(peterson_native.wasm) + +add_executable(atomic_fence.wasm atomic_fence.c) +target_link_libraries(atomic_fence.wasm) + +add_executable(atomic_store.wasm atomic_store.c) +target_link_libraries(atomic_store.wasm) + +add_executable(atomic_xchg.wasm atomic_xchg.c) +target_link_libraries(atomic_xchg.wasm) + +add_executable(atomic_logical.wasm atomic_logical.c) +target_link_libraries(atomic_logical.wasm) + +add_executable(atomic_wait_notify.wasm atomic_wait_notify.c) +target_link_libraries(atomic_wait_notify.wasm) \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/README.md b/tests/standalone/test-pthread/threads-opcode-wasm-apps/README.md new file mode 100644 index 000000000..b6a73f04e --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/README.md @@ -0,0 +1,73 @@ +# "Unit Test" Testsuite for Threads Opcode + +These tests are meant to **test the atomicity** of threads code. Initially, they were meant to test if the fast JIT implementation is correct, but they can also be used to test other running modes. The semantics correctness (operating with the correct number of bytes in memory and returning the correct value) have already been tested in spec tests (single-thread environment). + +## Test Cases Opcode Coverage + +> **Atomicity** of **all** threads opcodes are **fully tested** with these cases. +> +> **☑ Only indicates** there is a WASM test case to **test the atomicity directly.** The atomicity of other opcodes **without ☑** is **tested indirectly.** Indirect testing means that it is either implicit with other cases in this directory or tested pragmatically correct (atomicity and semantic correctness together) in pthread or WASI-threads test cases. + +Click the link to see the details of how each opcode is tested. + +- RMW (Read-Modify-Write): + - [CMPXCHG opcode](#cmpxchg) ☑ + - Arithmetic: + - [ADD opcode](#arithmetic-add-sub-and-xchg) ☑ + - [SUB opcode](#arithmetic-add-sub-and-xchg) ☑ + - [XCHG opcode](#arithmetic-add-sub-and-xchg) ☑ + - Logical: + - [AND opcode](#logical-or-xor-and) + - [OR opcode](#logical-or-xor-and) + - [XOR opcode](#logical-or-xor-and) ☑ + +- [LOAD](#atomic-ldstfence) + +- [STORE](#atomic-ldstfence) ☑ + +- [FENCE](#atomic-ldstfence) ☑ + +- [WAIT & NOTIFY](#atomic-waitnotify) ☑ + +## Details + +### atomic rmw + +#### arithmetic (`add`, `sub`) and xchg + +- `add, sub`: in [atomic_add_sub.c](./atomic_add_sub.c), **__atomic_fetch_add/sub()** to generate wasm opcode atomic.add/sub + +- `xchg`: in x86-64 implementation, wasm code atomic `store` and `xchg` generate same asm instruction xchg, should be enough to only test with store(tested in [atomic_store.c](./atomic_store.c)). But add a `atomic_xchg.c` to use **__atomic_exchange()** to generate wasm opcode xchg and test it anyways(tested in [atomic_xchg.c](./atomic_xchg.c)). + +#### logical `or`, `xor`, `and` + +- logical `or`, `xor`, `and`: those three opcodes are similar, it all generate a loop, inside which uses corresponding asm instruction do logical operation and locked cmpxchg to atomically modify the memory. So in a sense, test it will implicitly test the atomicity of cmpxchg. + + in [atomic_logical.c](./atomic_logical.c), tested `xor` wasm opcode to test the atomicity of the generated loop: + + - make use of operation "n `xor` n -> 0", when n range from 1 to 9999, 4 thread concurrently xor the same variable, the final result should be 0. + + The generated loop of `xor` is atomic -> generated loop of `or`, `and` is also atomic + +#### cmpxchg + +- wasm opcode `cmpxchg` already tested together with other opcodes in multiple wasi-thread cases. Logical opcodes generate asm instruction lock cmpxchg, the atomicity of generated asm code is proven in logical opcode. In [atomic_wait&¬ify.c](./atomic_wait_notify.c), it also tests the opcode `cmpxchg` + +### atomic ld/st/fence + +use peterson lock algorithm, in [atomic_fence.c](./atomic_fence.c) to test the atomicity of `fence` + +> PS: since the interpreter is relatively slow compared to JIT/AOT mode, it's less likely(almost impossible) to trigger processor-level behavior: instructions to be ordered within a single thread + +The prerequisite for peterson lock properly is that load and store have to be `Sequential Consistency`, which can be achieved use: + +1. LOAD (without fence) and STORE + MFENCE -> use it to test `fence` opcode +2. LOAD (without fence) and LOCK XCHG -> use it to test atomic `store` opcode +3. MFENCE + LOAD and STORE (without fence) +4. LOCK XADD ( 0 ) and STORE (without fence) + +### atomic wait¬ify + +Actually in every pthread tests, it will generate `wait` and `notify`, it is also tested in in multiple wasi-thread cases. + +But add a [atomic_wait&¬ify.c](./atomic_wait_notify.c) to test it anyways. diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_add_sub.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_add_sub.c new file mode 100644 index 000000000..5ebfa39d9 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_add_sub.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +#define MAX_NUM_THREADS 4 +#define NUM_ITER 100000 + +int g_add_count = 0; +int g_sub_count = 0; + +static void * +thread(void *arg) +{ + for (int i = 0; i < NUM_ITER; i++) { + __atomic_fetch_add(&g_add_count, 1, __ATOMIC_SEQ_CST); + __atomic_fetch_sub(&g_sub_count, 1, __ATOMIC_SEQ_CST); + } + + return NULL; +} + +int +main(int argc, char **argv) +{ + pthread_t tids[MAX_NUM_THREADS]; + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (pthread_create(&tids[i], NULL, thread, NULL) != 0) { + printf("Thread creation failed\n"); + } + } + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (pthread_join(tids[i], NULL) != 0) { + printf("Thread join failed\n"); + } + } + + printf("Value of counter after add update: %d (expected=%d)\n", g_add_count, + MAX_NUM_THREADS * NUM_ITER); + if (g_add_count != MAX_NUM_THREADS * NUM_ITER) { + __builtin_trap(); + } + + printf("Value of counter after sub update: %d (expected=%d)\n", g_sub_count, + -(MAX_NUM_THREADS * NUM_ITER)); + if (g_sub_count != -(MAX_NUM_THREADS * NUM_ITER)) { + __builtin_trap(); + } + + return -1; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_fence.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_fence.c new file mode 100644 index 000000000..f25f1383e --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_fence.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include "peterson.h" + +void +peterson_lock_acquire(peterson_lock_t *lock, int thread_id) +{ + // this threads wants to enter the cs + lock->flag[thread_id] = true; + + // assume the other thread has priority + int other_thread = 1 - thread_id; + + lock->turn = other_thread; + + // generate fence in wasm + __atomic_thread_fence(__ATOMIC_SEQ_CST); + + while (lock->turn == other_thread && lock->flag[other_thread]) { + // Busy wait + } +} + +int +main() +{ + pthread_t thread1, thread2; + + printf("============ test peterson lock using atomic fence ============\n"); + run_test(&thread1, &thread2, test_peterson_lock_atomicity); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_logical.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_logical.c new file mode 100644 index 000000000..0900749d0 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_logical.c @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include +#include + +// x XOR x -> 0 +// even num of thread -> g_val should end up with its original value +#define MAX_NUM_THREADS 4 +#define NUM_ITER 199999 + +int g_val = 5050; + +static void * +thread(void *arg) +{ + for (int i = 0; i < NUM_ITER; i++) { + __atomic_fetch_xor(&g_val, i, __ATOMIC_SEQ_CST); + } + + return NULL; +} + +int +main(int argc, char **argv) +{ + + pthread_t tids[MAX_NUM_THREADS]; + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (pthread_create(&tids[i], NULL, thread, NULL) != 0) { + printf("Thread creation failed\n"); + } + } + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (pthread_join(tids[i], NULL) != 0) { + printf("Thread join failed\n"); + } + } + + printf("Global value after update: %d (expected=%d)\n", g_val, 5050); + if (g_val != 5050) { + __builtin_trap(); + } + + return -1; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_store.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_store.c new file mode 100644 index 000000000..9dda9f0a4 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_store.c @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include "peterson.h" + +void +peterson_lock_acquire(peterson_lock_t *lock, int thread_id) +{ + // this threads wants to enter the cs + __atomic_store(&lock->flag[thread_id], &(bool){ true }, __ATOMIC_SEQ_CST); + + // assume the other thread has priority + int other_thread = 1 - thread_id; + + __atomic_store(&lock->turn, &other_thread, __ATOMIC_SEQ_CST); + + while (lock->turn == other_thread && lock->flag[other_thread]) { + // Busy wait + } +} + +int +main() +{ + pthread_t thread1, thread2; + + printf("============ test peterson lock using atomic store ============\n"); + run_test(&thread1, &thread2, test_peterson_lock_atomicity); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_wait_notify.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_wait_notify.c new file mode 100644 index 000000000..ecbf67868 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_wait_notify.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include +#include +#include + +#define MAX_NUM_THREADS 4 +#define NUM_ITER 100000 + +int g_val; +int my_mutex; + +int +try_lock() +{ + return __atomic_compare_exchange(&my_mutex, &(int){ 0 }, &(int){ 1 }, false, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); +} + +void +lock_acquire() +{ + while (!try_lock()) { + // expected value (1 => locked) + __builtin_wasm_memory_atomic_wait32(&my_mutex, 1, -1); + } +} +void +lock_release() +{ + // unlock the mutex + __atomic_store(&my_mutex, &(int){ 0 }, __ATOMIC_SEQ_CST); + // notify 1 waiter + __builtin_wasm_memory_atomic_notify(&my_mutex, 1); +} + +static void * +thread(void *arg) +{ + for (int i = 0; i < NUM_ITER; i++) { + lock_acquire(); + g_val++; + lock_release(); + } + + return NULL; +} + +int +main() +{ + pthread_t tids[MAX_NUM_THREADS]; + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (pthread_create(&tids[i], NULL, thread, NULL) != 0) { + printf("Thread creation failed\n"); + } + } + + for (int i = 0; i < MAX_NUM_THREADS; i++) { + if (pthread_join(tids[i], NULL) != 0) { + printf("Thread join failed\n"); + } + } + + printf("Value of counter after add update: %d (expected=%d)\n", g_val, + MAX_NUM_THREADS * NUM_ITER); + if (g_val != MAX_NUM_THREADS * NUM_ITER) { + __builtin_trap(); + } + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_xchg.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_xchg.c new file mode 100644 index 000000000..7bfda72a8 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/atomic_xchg.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include "peterson.h" + +void +peterson_lock_acquire(peterson_lock_t *lock, int thread_id) +{ + bool xchg_ret1; + int xchg_ret2; + // this threads wants to enter the cs + __atomic_exchange(&lock->flag[thread_id], &(bool){ true }, &xchg_ret1, + __ATOMIC_SEQ_CST); + + // assume the other thread has priority + int other_thread = 1 - thread_id; + + __atomic_exchange(&lock->turn, &other_thread, &xchg_ret2, __ATOMIC_SEQ_CST); + + while (lock->turn == other_thread && lock->flag[other_thread]) { + // Busy wait + } +} + +int +main() +{ + pthread_t thread1, thread2; + + printf("============ test peterson lock using atomic xchg ============\n"); + run_test(&thread1, &thread2, test_peterson_lock_atomicity); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/peterson.h b/tests/standalone/test-pthread/threads-opcode-wasm-apps/peterson.h new file mode 100644 index 000000000..7ddc6207f --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/peterson.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include +#include +#include + +// Peterson's algorithm for mutual exclusion +#define ITERATIONS 15000000 + +typedef struct { + bool flag[2]; + int turn; +} peterson_lock_t; + +static int counter = 0; +static peterson_lock_t lock; + +void +peterson_lock_acquire(peterson_lock_t *lock, int thread_id); + +void +peterson_lock_release(peterson_lock_t *lock, int thread_id) +{ + lock->flag[thread_id] = false; +} + +void * +test_peterson_lock_atomicity(void *arg) +{ + int thread_id = (int)(long)arg; + + for (int i = 0; i < ITERATIONS; ++i) { + peterson_lock_acquire(&lock, thread_id); + counter++; + peterson_lock_release(&lock, thread_id); + } + + return NULL; +} + +int +run_test(pthread_t *thread1_ptr, pthread_t *thread2_ptr, + void *(*start_routine)(void *)) +{ + lock.flag[0] = false; + lock.flag[1] = false; + lock.turn = 0; + counter = 0; + + pthread_create(thread1_ptr, NULL, start_routine, (void *)0); + pthread_create(thread2_ptr, NULL, start_routine, (void *)1); + + pthread_join(*thread1_ptr, NULL); + pthread_join(*thread2_ptr, NULL); + + printf("Expected counter value: %d\n", ITERATIONS * 2); + printf("Actual counter value: %d\n", counter); + if (counter != ITERATIONS * 2) + __builtin_trap(); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/threads-opcode-wasm-apps/peterson_native.c b/tests/standalone/test-pthread/threads-opcode-wasm-apps/peterson_native.c new file mode 100644 index 000000000..9887982c5 --- /dev/null +++ b/tests/standalone/test-pthread/threads-opcode-wasm-apps/peterson_native.c @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include "peterson.h" + +void +peterson_lock_acquire(peterson_lock_t *lock, int thread_id) +{ + // this threads wants to enter the cs + lock->flag[thread_id] = true; + + // assume the other thread has priority + int other_thread = 1 - thread_id; + + lock->turn = other_thread; + + while (lock->turn == other_thread && lock->flag[other_thread]) { + // Busy wait + } +} + +int +main() +{ + pthread_t thread1, thread2; + + printf("============ test naive peterson lock(should trap) ============\n"); + printf("============ May not be able to observe in interpreter mode " + "============\n"); + run_test(&thread1, &thread2, test_peterson_lock_atomicity); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/wasi.c b/tests/standalone/test-pthread/wasi.c new file mode 100644 index 000000000..1129f219f --- /dev/null +++ b/tests/standalone/test-pthread/wasi.c @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +struct member { + int a; + char *b; +} temp = { 8, "zieckey" }; + +void * +create(void *arg) +{ + printf("new thread ... \n"); + sleep(5); + + pthread_exit(&temp); +} + +int +main(int argc, char *argv[]) +{ + int error; + pthread_t tid; + struct member *c; + + error = pthread_create(&tid, NULL, create, NULL); + + if (error) { + printf("new thread is not created ... \n"); + return -1; + } + printf("main ... \n"); + + error = pthread_join(tid, (void *)&c); + + if (error) { + printf("new thread is not exit ... \n"); + return -2; + } + + printf("c->a = %d \n", c->a); + printf("c->b = %s \n", c->b); + sleep(1); + + return 0; +} \ No newline at end of file diff --git a/tests/standalone/test-pthread/wasi.wasm b/tests/standalone/test-pthread/wasi.wasm new file mode 100755 index 000000000..45f6cd8c4 Binary files /dev/null and b/tests/standalone/test-pthread/wasi.wasm differ diff --git a/tests/standalone/test-running-modes/build_c_embed.sh b/tests/standalone/test-running-modes/build_c_embed.sh new file mode 100755 index 000000000..d69440c43 --- /dev/null +++ b/tests/standalone/test-running-modes/build_c_embed.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +set -e + +cd c-embed +rm -fr build && mkdir build +cmake $1 -B build +cmake --build build -j >/dev/null 2>&1 diff --git a/tests/standalone/test-running-modes/build_iwasm.sh b/tests/standalone/test-running-modes/build_iwasm.sh new file mode 100755 index 000000000..41835053d --- /dev/null +++ b/tests/standalone/test-running-modes/build_iwasm.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +set -e + +PLATFORM=$(uname -s | tr A-Z a-z) + +readonly WAMR_DIR="$PWD/../../.." +rm -fr build && mkdir build && cd build +cmake ${WAMR_DIR}/product-mini/platforms/${PLATFORM} $1 +make -j > /dev/null 2>&1 +cd .. diff --git a/tests/standalone/test-running-modes/c-embed/CMakeLists.txt b/tests/standalone/test-running-modes/c-embed/CMakeLists.txt new file mode 100644 index 000000000..52064ac44 --- /dev/null +++ b/tests/standalone/test-running-modes/c-embed/CMakeLists.txt @@ -0,0 +1,59 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. +project(c_embed_test) + +cmake_minimum_required(VERSION 3.14) + +include(CheckPIESupported) + +string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM) +if (APPLE) + add_definitions(-DBH_PLATFORM_DARWIN) +endif () + +set (CMAKE_C_STANDARD 99) +set (CMAKE_CXX_STANDARD 17) + +set(WAMR_BUILD_TARGET "X86_64") +set(WAMR_BUILD_INTERP 1) +set(WAMR_BUILD_FAST_INTERP 0) +set(WAMR_BUILD_AOT 0) +set(WAMR_BUILD_LIBC_BUILTIN 1) +set(WAMR_BUILD_LIBC_WASI 1) +set(WAMR_BUILD_SIMD 1) +set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../..) + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") +if (NOT WAMR_BUILD_PLATFORM STREQUAL "darwin") + set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE") +endif () +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused") + +# build out vmlib +include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) +add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) + +# application related +include(${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) +add_executable(c_embed_test src/main.c ${UNCOMMON_SHARED_SOURCE}) + +target_link_libraries(c_embed_test vmlib m ${LLVM_AVAILABLE_LIBS}) + +add_custom_command(TARGET c_embed_test POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_LIST_DIR}/../wasm-apps/mytest.wasm ${CMAKE_CURRENT_LIST_DIR}/../wasm-apps/hello.wasm + ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Copy main.wasm and hello.wasm to the build directory" + ) diff --git a/tests/standalone/test-running-modes/c-embed/src/main.c b/tests/standalone/test-running-modes/c-embed/src/main.c new file mode 100644 index 000000000..a9db017b2 --- /dev/null +++ b/tests/standalone/test-running-modes/c-embed/src/main.c @@ -0,0 +1,208 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "bh_read_file.h" +#include "wasm_export.h" + +RunningMode +str_to_running_mode(char const *str) +{ + RunningMode running_mode = 0; +#if WASM_ENABLE_INTERP != 0 + if (!strcmp(str, "interp")) { + running_mode = Mode_Interp; + } +#endif +#if WASM_ENABLE_FAST_JIT != 0 + else if (!strcmp(str, "fast-jit")) { + running_mode = Mode_Fast_JIT; + } +#endif +#if WASM_ENABLE_JIT != 0 + else if (!strcmp(str, "llvm-jit")) { + running_mode = Mode_LLVM_JIT; + } +#endif +#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \ + && WASM_ENABLE_LAZY_JIT != 0 + else if (!strcmp(str, "multi-tier-jit")) { + running_mode = Mode_Multi_Tier_JIT; + } +#endif + return running_mode; +} + +int +one_time_run_wasm(RunningMode default_running_mode, + RunningMode module_running_mode) +{ + char *buffer, *another_buffer, error_buf[128]; + wasm_module_t module = NULL, another_module = NULL; + wasm_module_inst_t module_inst = NULL, another_module_inst = NULL; + wasm_function_inst_t main_func = NULL, echo_func = NULL; + wasm_exec_env_t exec_env = NULL, another_exec_env = NULL; + uint32_t size, stack_size = 8092, heap_size = 8092; + + if (wasm_runtime_is_running_mode_supported(default_running_mode)) { + printf("Support running mode: %d\n", default_running_mode); + } + else { + printf("This runtime Doesn't support running mode: %d\n", + default_running_mode); + goto fail; + } + if (wasm_runtime_set_default_running_mode(default_running_mode)) { + printf("Successfully set default running mode: %d\n", + default_running_mode); + } + else { + printf("Set default running mode: %d failed\n", default_running_mode); + goto fail; + } + + /* module 1 */ + if (!(buffer = bh_read_file_to_buffer("mytest.wasm", &size))) { + printf("Open wasm app file %s failed.\n", "mytest.wasm"); + goto fail; + } + if (!(module = wasm_runtime_load((uint8_t *)buffer, size, error_buf, + sizeof(error_buf)))) { + printf("Load wasm module failed. error: %s\n", error_buf); + goto fail; + } + if (!(module_inst = wasm_runtime_instantiate( + module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { + printf("Instantiate wasm module failed. error: %s\n", error_buf); + goto fail; + } + if (!(exec_env = wasm_runtime_create_exec_env(module_inst, stack_size))) { + printf("Create wasm execution environment failed.\n"); + goto fail; + } + /* module 2 */ + if (!(another_buffer = bh_read_file_to_buffer("hello.wasm", &size))) { + printf("Open wasm app file %s failed.\n", "hello.wasm"); + goto fail; + } + if (!(another_module = wasm_runtime_load((uint8_t *)another_buffer, size, + error_buf, sizeof(error_buf)))) { + printf("Load wasm module failed. error: %s\n", error_buf); + goto fail; + } + if (!(another_module_inst = + wasm_runtime_instantiate(another_module, stack_size, heap_size, + error_buf, sizeof(error_buf)))) { + printf("Instantiate wasm module failed. error: %s\n", error_buf); + goto fail; + } + if (!(another_exec_env = + wasm_runtime_create_exec_env(another_module_inst, stack_size))) { + printf("Create wasm execution environment failed.\n"); + goto fail; + } + + /* run main function in module 1 */ + uint32 wasm_argv[2]; + if (!(main_func = + wasm_runtime_lookup_function(module_inst, "__main_argc_argv"))) { + printf("The main wasm function from module 1 is not found.\n"); + goto fail; + } + + wasm_argv[0] = 3; + if (wasm_runtime_call_wasm(exec_env, main_func, 2, wasm_argv)) { + printf("Run module 1 in running mode: %d\n", + wasm_runtime_get_running_mode(module_inst)); + assert(default_running_mode + == wasm_runtime_get_running_mode(module_inst)); + printf("Wasm main function return: %d\n", wasm_argv[0]); + } + else { + printf("%s\n", wasm_runtime_get_exception(module_inst)); + goto fail; + } + + /* run echo function in module 2 */ + if (!(wasm_runtime_set_running_mode(another_module_inst, + module_running_mode))) { + + printf("Set running mode for module instance failed\n"); + goto fail; + } + if (!(echo_func = + wasm_runtime_lookup_function(another_module_inst, "echo"))) { + printf("The echo wasm function from module 2 is not found.\n"); + goto fail; + } + + wasm_argv[0] = 5; + if (wasm_runtime_call_wasm(another_exec_env, echo_func, 1, wasm_argv)) { + printf("Run module 2 in running mode: %d\n", + wasm_runtime_get_running_mode(another_module_inst)); + assert(module_running_mode + == wasm_runtime_get_running_mode(another_module_inst)); + printf("Wasm echo function return: %d\n\n", wasm_argv[0]); + } + else { + printf("%s\n", wasm_runtime_get_exception(another_module_inst)); + goto fail; + } + +fail: + if (exec_env) + wasm_runtime_destroy_exec_env(exec_env); + if (another_exec_env) + wasm_runtime_destroy_exec_env(another_exec_env); + if (module_inst) + wasm_runtime_deinstantiate(module_inst); + if (another_module_inst) + wasm_runtime_deinstantiate(another_module_inst); + if (module) + wasm_runtime_unload(module); + if (another_module) + wasm_runtime_unload(another_module); + + return 0; +} + +int +main(int argc, char const *argv[]) +{ + + RunningMode default_running_mode = 0, module_running_mode = 0; + + for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { + if (!strncmp(argv[0], "--default-running-mode=", 23)) { + default_running_mode = str_to_running_mode(argv[0] + 23); + } + else if (!strncmp(argv[0], "--module-running-mode=", 22)) { + module_running_mode = str_to_running_mode(argv[0] + 22); + } + } + + /* all the runtime memory allocations are restricted in the global_heap_buf + * array */ + static char global_heap_buf[512 * 1024]; + RuntimeInitArgs init_args; + memset(&init_args, 0, sizeof(RuntimeInitArgs)); + /* configure the memory allocator for the runtime */ + 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); + + /* initialize runtime environment with user configurations*/ + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); + return -1; + } + + for (int i = 0; i < 1; ++i) { + one_time_run_wasm(default_running_mode, module_running_mode); + } + + wasm_runtime_destroy(); + + return 0; +} diff --git a/tests/standalone/test-running-modes/compile_wasm_app.sh b/tests/standalone/test-running-modes/compile_wasm_app.sh new file mode 100755 index 000000000..9cf9ee6ca --- /dev/null +++ b/tests/standalone/test-running-modes/compile_wasm_app.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +set -e +readonly COMPILE_CMD="/opt/wasi-sdk/bin/clang" + +if ! test -x ${COMPILE_CMD}; then + echo "wasi-sdk could not be found" + exit +fi + +${COMPILE_CMD} -O3 \ + -z stack-size=8192 -nostdlib -Wl,--allow-undefined -Wl,--no-entry -Wl,--initial-memory=65536 \ + -o wasm-apps/mytest.wasm wasm-apps/mytest.c \ + -Wl,--export=__main_argc_argv + +${COMPILE_CMD} -O3 \ + -Wl,--no-entry -nostdlib \ + -o wasm-apps/hello.wasm wasm-apps/hello.c \ + -Wl,--export=echo diff --git a/tests/standalone/test-running-modes/run.sh b/tests/standalone/test-running-modes/run.sh new file mode 100755 index 000000000..c652cc8d5 --- /dev/null +++ b/tests/standalone/test-running-modes/run.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +set -e +if [[ $2 == "--sgx" ]];then + echo "running modes feature on SGX isn't supported yet, ignored." + exit 0 +else + readonly IWASM_CMD="$PWD/build/iwasm" +fi + +echo "============> test test-running-modes" + +./compile_wasm_app.sh + +# multi-tier jit +# test iwasm +./build_iwasm.sh "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1" +${IWASM_CMD} --heap-size=16384 wasm-apps/mytest.wasm +${IWASM_CMD} --heap-size=16384 --interp wasm-apps/mytest.wasm +${IWASM_CMD} --heap-size=16384 --fast-jit wasm-apps/mytest.wasm +${IWASM_CMD} --heap-size=16384 --llvm-jit wasm-apps/mytest.wasm +${IWASM_CMD} --heap-size=16384 --llvm-jit --llvm-jit-size-level=1 wasm-apps/mytest.wasm +${IWASM_CMD} --heap-size=16384 --llvm-jit --llvm-jit-size-level=2 --llvm-jit-opt-level=1 wasm-apps/mytest.wasm +${IWASM_CMD} --heap-size=16384 --multi-tier-jit wasm-apps/mytest.wasm + +# test c embed api +./build_c_embed.sh "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1" +cd c-embed/build +./c_embed_test --default-running-mode=llvm-jit --module-running-mode=multi-tier-jit diff --git a/tests/standalone/test-running-modes/test_c_embed_api_thoroughly.py b/tests/standalone/test-running-modes/test_c_embed_api_thoroughly.py new file mode 100755 index 000000000..63e871e4a --- /dev/null +++ b/tests/standalone/test-running-modes/test_c_embed_api_thoroughly.py @@ -0,0 +1,76 @@ +#!python3 +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + + +import os +from collections import OrderedDict + + +def CLI_ARGS_GENREATOR(running_modes_supported: list[str]) -> list[str]: + res = [] + list_2d = [["--default-running-mode={} --module-running-mode={}".format(i, j) + for i in running_modes_supported] for j in running_modes_supported] + for list_1d in list_2d: + res.extend(list_1d) + return res + + +def main(): + RUNNING_MODES: list[str] = [ + "interp", + "fast-jit", + "llvm-jit", + "multi-tier-jit", + ] + + COMPILE_FLAGS: list[str] = [ + "-DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=0", + "-DWAMR_BUILD_FAST_JIT=1", + "-DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=1", + "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1", + "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=0", + ] + + # Python 3.7+: Dictionary iteration order is guaranteed to be in order of insertion. + # just to be safe, using orderreddict + # key: value -> compile mode, {"compile_flag": CMake compile flag, "iwasm_cli_args": array of CLI args tested} + test_options = OrderedDict({ + "INTERP": {"compile_flag": COMPILE_FLAGS[0], "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES[:1])}, + "FAST_JIT": {"compile_flag": COMPILE_FLAGS[1], "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES[:2])}, + "LLVM_JIT": {"compile_flag": COMPILE_FLAGS[2], + "cli_args": CLI_ARGS_GENREATOR([RUNNING_MODES[0], RUNNING_MODES[2]])}, + "MULTI_TIER_JIT": {"compile_flag": COMPILE_FLAGS[3], "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES)}, + "EAGER_JIT_WITH_BOTH_JIT": {"compile_flag": COMPILE_FLAGS[4], + "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES[:3])} + }) + + build_cmd = "./build_c_embed.sh \"{build_flag}\"" + run_cmd = "cd c-embed/build && ./c_embed_test {cli_args}" + + for compile_mode in test_options.keys(): + build_flag: str = test_options[compile_mode]["compile_flag"] + cli_args_li: list = test_options[compile_mode]["cli_args"] + # compile + print("\r\n\r\nCompile C program embed WAMR in {} mode".format(compile_mode)) + ret = os.system(build_cmd.format(build_flag=build_flag)) + if ret: + print("Compile failed") + # iter over cli args combination + for cli_args in cli_args_li: + print(run_cmd.format(cli_args=cli_args)) + ret = os.system(run_cmd.format(cli_args=cli_args)) + if ret: + break + else: # if inner for loop finish normally + continue + + # if break from inner for loop + print("Run failed") + break + + +if __name__ == '__main__': + main() diff --git a/tests/standalone/test-running-modes/test_iwasm_thoroughly.py b/tests/standalone/test-running-modes/test_iwasm_thoroughly.py new file mode 100755 index 000000000..a5af29101 --- /dev/null +++ b/tests/standalone/test-running-modes/test_iwasm_thoroughly.py @@ -0,0 +1,71 @@ +#!python3 +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +import os +from collections import OrderedDict + + +def main(): + IWASM_CMD = "../../../wamr/product-mini/platforms/linux/build/iwasm" + + IWASM_CLI_ARGS: list[str] = [ + "--heap-size=16384 --interp", + "--heap-size=16384 --fast-jit", + "--heap-size=16384 --llvm-jit", + "--heap-size=16384 --multi-tier-jit", + "--heap-size=16384 --llvm-jit --llvm-jit-size-level=1", + "--heap-size=16384 --llvm-jit --llvm-jit-size-level=2 --llvm-jit-opt-level=1" + ] + + COMPILE_FLAGS: list[str] = [ + "-DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=0", + "-DWAMR_BUILD_FAST_JIT=1", + "-DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=1", + "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1", + "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=0", + ] + + # Python 3.7+: Dictionary iteration order is guaranteed to be in order of insertion. + # just to be safe, using orderreddict + # key: value -> compile mode, {"compile_flag": CMake compile flag, "iwasm_cli_args": array of CLI args tested} + test_options = OrderedDict({ + "INTERP": {"compile_flag": COMPILE_FLAGS[0], "iwasm_cli_args": IWASM_CLI_ARGS[:1]}, + "FAST_JIT": {"compile_flag": COMPILE_FLAGS[1], "iwasm_cli_args": IWASM_CLI_ARGS[:2]}, + "LLVM_JIT": {"compile_flag": COMPILE_FLAGS[2], "iwasm_cli_args": [IWASM_CLI_ARGS[0], IWASM_CLI_ARGS[2]]}, + "MULTI_TIER_JIT": {"compile_flag": COMPILE_FLAGS[3], "iwasm_cli_args": IWASM_CLI_ARGS}, + "EAGER_JIT_WITH_BOTH_JIT": {"compile_flag": COMPILE_FLAGS[4], + "iwasm_cli_args": IWASM_CLI_ARGS[:3] + IWASM_CLI_ARGS[4:]} + }) + + build_cmd = "./build_iwasm.sh \"{build_flag}\"" + wasm_file = "wasm-apps/mytest.wasm" + run_cmd = "{IWASM_CMD} {cli_args} " + wasm_file + + for compile_mode in test_options.keys(): + build_flag: str = test_options[compile_mode]["compile_flag"] + cli_args_li: list = test_options[compile_mode]["iwasm_cli_args"] + # compile + print("\r\n\r\nCompile iwasm in {} mode".format(compile_mode)) + ret = os.system(build_cmd.format(build_flag=build_flag)) + if ret: + print("Compile failed") + # iter over cli args combination + for cli_args in cli_args_li: + print(run_cmd.format(IWASM_CMD=IWASM_CMD, cli_args=cli_args)) + ret = os.system(run_cmd.format( + IWASM_CMD=IWASM_CMD, cli_args=cli_args)) + if ret: + break + else: # if inner for loop finish normally + continue + + # if break from inner for loop + print("Run failed") + break + + +if __name__ == '__main__': + main() diff --git a/tests/standalone/test-running-modes/wasm-apps/hello.c b/tests/standalone/test-running-modes/wasm-apps/hello.c new file mode 100644 index 000000000..47cadcf07 --- /dev/null +++ b/tests/standalone/test-running-modes/wasm-apps/hello.c @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +double +foo(double d) +{ + return d / 3.0; +} + +double +maybe_min(double d, double e) +{ + return d < e ? d : e; +} + +double +factor(double a, double b, double c) +{ + return (a * c) + (b * c); +} + +int +echo(int a) +{ + double b = foo(14.5); + double c = maybe_min(12.2, 15.4); + double d = factor(a, b, c); + return 2 * a; +} \ No newline at end of file diff --git a/tests/standalone/test-running-modes/wasm-apps/hello.wasm b/tests/standalone/test-running-modes/wasm-apps/hello.wasm new file mode 100755 index 000000000..aedd4f4f2 Binary files /dev/null and b/tests/standalone/test-running-modes/wasm-apps/hello.wasm differ diff --git a/tests/standalone/test-running-modes/wasm-apps/mytest.c b/tests/standalone/test-running-modes/wasm-apps/mytest.c new file mode 100644 index 000000000..cd5239588 --- /dev/null +++ b/tests/standalone/test-running-modes/wasm-apps/mytest.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ +#include +#include + +int +recursive(int a) +{ + if (a > 0) { + return recursive(a - 1) + 1; + } + else + return 0; +} + +int +testFunction(int *input, int length) +{ + int sum = 0; + for (int i = 0; i < length; ++i) { + sum += input[i]; + } + return sum; +} + +int +main(int argc, char **argv) +{ + + int arr[5] = { 1, 2, 3, 4, 5 }; + testFunction(arr, recursive(5)); + + char *buf; + + printf("Hello world!\n"); + + buf = malloc(1024); + if (!buf) { + printf("malloc buf failed\n"); + return -1; + } + + printf("buf ptr: %p\n", buf); + + snprintf(buf, 1024, "%s", "1234\n"); + printf("buf: %s", buf); + + free(buf); + return 0; +} diff --git a/tests/standalone/test-running-modes/wasm-apps/mytest.wasm b/tests/standalone/test-running-modes/wasm-apps/mytest.wasm new file mode 100755 index 000000000..68c9c9cf0 Binary files /dev/null and b/tests/standalone/test-running-modes/wasm-apps/mytest.wasm differ diff --git a/tests/standalone/test-stddef/run.sh b/tests/standalone/test-stddef/run.sh new file mode 100755 index 000000000..fceed2dda --- /dev/null +++ b/tests/standalone/test-stddef/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_stddef_cpp_5_1.wasm" + ${IWASM_CMD} test_stddef_cpp_5_1.wasm +else + echo "============> compile test_stddef_cpp_5_1.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_stddef_cpp_5_1.aot test_stddef_cpp_5_1.wasm \ + || ${WAMRC_CMD} -o test_stddef_cpp_5_1.aot test_stddef_cpp_5_1.wasm + echo "============> run test_stddef_cpp_5_1.aot" + ${IWASM_CMD} test_stddef_cpp_5_1.aot +fi diff --git a/tests/standalone/test-stddef/test_stddef_cpp_5_1.wasm b/tests/standalone/test-stddef/test_stddef_cpp_5_1.wasm new file mode 100644 index 000000000..5c995598b Binary files /dev/null and b/tests/standalone/test-stddef/test_stddef_cpp_5_1.wasm differ diff --git a/tests/standalone/test-strchr/main.c b/tests/standalone/test-strchr/main.c new file mode 100644 index 000000000..24dcc9277 --- /dev/null +++ b/tests/standalone/test-strchr/main.c @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +int +main(int argc, char **argv) +{ + char *str = malloc(32); + printf("str ptr: %p\n", str); + + sprintf(str, "%s", "123456"); + printf("str: %s\n", str); + + char *str1 = strchr(str, '3'); + printf("str1 ptr: %p\n", str1); + printf("str1: %s\n", str1); + + free(str); + return 0; +} diff --git a/tests/standalone/test-strchr/run.sh b/tests/standalone/test-strchr/run.sh new file mode 100755 index 000000000..6db864f4c --- /dev/null +++ b/tests/standalone/test-strchr/run.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +echo "============> compile test-strchr to wasm" +/opt/wasi-sdk/bin/clang -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \ + -Wl,--export=main -Wl,--export=__main_argc_argv \ + -o test-strchr.wasm main.c \ + -Wl,--export=__heap_base,--export=__data_end -Wl,--no-entry \ + -nostdlib -Wl,--allow-undefined + +if [[ $1 != "--aot" ]]; then + echo "============> run test-strchr.wasm" + ${IWASM_CMD} --heap-size=16384 test-strchr.wasm +else + echo "============> compile test-strchr.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test-strchr.aot test-strchr.wasm \ + || ${WAMRC_CMD} -o test-strchr.aot test-strchr.wasm + echo "============> run test-strchr.aot" + ${IWASM_CMD} --heap-size=16384 test-strchr.aot +fi + diff --git a/tests/standalone/test-ts1/app.ts b/tests/standalone/test-ts1/app.ts new file mode 100644 index 000000000..1d602804f --- /dev/null +++ b/tests/standalone/test-ts1/app.ts @@ -0,0 +1,9 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +import * as console from "./console"; + +let message: string = "Hello World"; +console.log(message); diff --git a/tests/standalone/test-ts1/build.sh b/tests/standalone/test-ts1/build.sh new file mode 100755 index 000000000..f987c832d --- /dev/null +++ b/tests/standalone/test-ts1/build.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +#asc app.ts console.ts --debug -o app.wasm + +asc app.ts console.ts -o test-ts1.wasm diff --git a/tests/standalone/test-ts1/console.ts b/tests/standalone/test-ts1/console.ts new file mode 100644 index 000000000..dbaf9cbe1 --- /dev/null +++ b/tests/standalone/test-ts1/console.ts @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +@external("env", "puts") +declare function printf(a: ArrayBuffer): i32; + +export function log(a: string): void { + printf(String.UTF8.encode(a, true)); +} + +export function log_number(a: number): void { + printf(String.UTF8.encode(a.toString())); +} diff --git a/tests/standalone/test-ts1/run.sh b/tests/standalone/test-ts1/run.sh new file mode 100755 index 000000000..d2e8f78b7 --- /dev/null +++ b/tests/standalone/test-ts1/run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run test_ts1.wasm" + ${IWASM_CMD} -f log_number test_ts1.wasm 1234.56 +else + echo "============> compile test_ts1.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test_ts1.aot test_ts1.wasm \ + || ${WAMRC_CMD} -o test_ts1.aot test_ts1.wasm + echo "============> run test_ts1.aot" + ${IWASM_CMD} -f log_number test_ts1.aot 1234.56 +fi + diff --git a/tests/standalone/test-ts1/test_ts1.wasm b/tests/standalone/test-ts1/test_ts1.wasm new file mode 100644 index 000000000..cc5e30b9f Binary files /dev/null and b/tests/standalone/test-ts1/test_ts1.wasm differ diff --git a/tests/standalone/test-wasi1/main.c b/tests/standalone/test-wasi1/main.c new file mode 100644 index 000000000..c48336155 --- /dev/null +++ b/tests/standalone/test-wasi1/main.c @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + int n, m; + char buf[BUFSIZ]; + + if (argc != 3) { + fprintf(stderr, "usage: %s \n", argv[0]); + exit(1); + } + + printf("##open %s\n", argv[1]); + int in = open(argv[1], O_RDONLY); + if (in < 0) { + fprintf(stderr, "error opening input %s: %s\n", argv[1], + strerror(errno)); + exit(1); + } + + printf("##open %s\n", argv[2]); + int out = open(argv[2], O_WRONLY | O_CREAT, 0660); + if (out < 0) { + fprintf(stderr, "error opening output %s: %s\n", argv[2], + strerror(errno)); + exit(1); + } + + printf("##read content of %s, and write it to %s\n", argv[1], argv[2]); + while ((n = read(in, buf, BUFSIZ)) > 0) { + while (n > 0) { + m = write(out, buf, n); + if (m < 0) { + fprintf(stderr, "write error: %s\n", strerror(errno)); + exit(1); + } + n -= m; + } + } + + if (n < 0) { + fprintf(stderr, "read error: %s\n", strerror(errno)); + exit(1); + } + + printf("##success.\n"); + return EXIT_SUCCESS; +} diff --git a/tests/standalone/test-wasi1/run.sh b/tests/standalone/test-wasi1/run.sh new file mode 100755 index 000000000..77a17d65c --- /dev/null +++ b/tests/standalone/test-wasi1/run.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +echo "============> compile test-wasi1 to wasm" +/opt/wasi-sdk/bin/clang -O3 -z stack-size=16384 -Wl,--initial-memory=131072 \ + -Wl,--export=main -Wl,--export=__heap_base,--export=__data_end \ + -Wl,--allow-undefined \ + -o test-wasi1.wasm main.c + +if [[ $1 != "--aot" ]]; then + echo "============> run test-wasi1.wasm" + ${IWASM_CMD} --dir=. --dir=/tmp test-wasi1.wasm test.txt /tmp/somewhere.txt +else + echo "============> compile test-wasi1.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test-wasi1.aot test-wasi1.wasm \ + || ${WAMRC_CMD} -o test-wasi1.aot test-wasi1.wasm + echo "============> run test-wasi1.aot" + ${IWASM_CMD} --dir=. --dir=/tmp test-wasi1.aot test.txt /tmp/somewhere.txt +fi + diff --git a/tests/standalone/test-wasi1/test.txt b/tests/standalone/test-wasi1/test.txt new file mode 100644 index 000000000..cbcfbaa1d --- /dev/null +++ b/tests/standalone/test-wasi1/test.txt @@ -0,0 +1,2 @@ +hello world! +1234 diff --git a/tests/standalone/test-wasi2/main.c b/tests/standalone/test-wasi2/main.c new file mode 100644 index 000000000..d8cdb6a24 --- /dev/null +++ b/tests/standalone/test-wasi2/main.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +uint64_t +get_time_us() +{ + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + return 0; + } + + return ((uint64_t)ts.tv_sec) * 1000 * 1000 + ((uint64_t)ts.tv_nsec) / 1000; +} + +int +main(int argc, char **argv) +{ + uint64_t time_start, time_end; + struct pollfd fds[2]; + int ret; + + printf("sleep 2 seconds\n"); + time_start = get_time_us(); + ret = sleep(2); + time_end = get_time_us(); + + printf("sleep return %u\n", ret); + printf("time sleeped: %u\n", (uint32_t)(time_end - time_start)); + if (time_end - time_start < 2 * 1000000) { + printf("Test sleep failed!\n"); + return -1; + } + + /* watch stdin for input */ + fds[0].fd = STDIN_FILENO; + fds[0].events = POLLIN; + + /* watch stdout for ability to write */ + fds[1].fd = STDOUT_FILENO; + fds[1].events = POLLOUT; + + printf("poll with 5 seconds\n"); + ret = poll(fds, 2, 5 * 1000); + + if (ret == -1) { + perror("poll"); + return 1; + } + + if (!ret) { + printf("Test poll failed, %d seconds elapsed!\n", 5); + return 0; + } + + if (fds[0].revents & POLLIN) + printf("stdin is readable\n"); + + if (fds[1].revents & POLLOUT) + printf("stdout is writable\n"); + + printf("Test finished\n"); + return 0; +} diff --git a/tests/standalone/test-wasi2/run.sh b/tests/standalone/test-wasi2/run.sh new file mode 100755 index 000000000..9ec8fb3e0 --- /dev/null +++ b/tests/standalone/test-wasi2/run.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +echo "============> compile test-wasi2 to wasm" +/opt/wasi-sdk/bin/clang -O3 -z stack-size=16384 -Wl,--initial-memory=131072 \ + -Wl,--export=main -Wl,--export=__heap_base,--export=__data_end \ + -Wl,--allow-undefined \ + -o test-wasi2.wasm main.c + +if [[ $1 != "--aot" ]]; then + echo "============> run test-wasi2.wasm" + ${IWASM_CMD} test-wasi2.wasm +else + echo "============> compile test-wasi2.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o test-wasi2.aot test-wasi2.wasm \ + || ${WAMRC_CMD} -o test-wasi2.aot test-wasi2.wasm + echo "============> run test-wasi2.aot" + ${IWASM_CMD} test-wasi2.aot +fi + diff --git a/tests/standalone/tsf/run.sh b/tests/standalone/tsf/run.sh new file mode 100755 index 000000000..47ac15b18 --- /dev/null +++ b/tests/standalone/tsf/run.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +if [[ $2 == "--sgx" ]];then + readonly IWASM_CMD="../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm" +else + readonly IWASM_CMD="../../../product-mini/platforms/linux/build/iwasm" +fi +readonly WAMRC_CMD="../../../wamr-compiler/build/wamrc" + +if [[ $1 != "--aot" ]]; then + echo "============> run tsf.wasm" + if [[ $1 != "--multi-tier-jit" ]]; then + ${IWASM_CMD} --stack-size=1048576 --dir=. tsf.wasm + else + echo "============> run tsf.wasm with fast-jit" + ${IWASM_CMD} --fast-jit --stack-size=1048576 --dir=. tsf.wasm + echo "============> run tsf.wasm with llvm-jit" + ${IWASM_CMD} --llvm-jit --stack-size=1048576 --dir=. tsf.wasm + echo "============> run tsf.wasm with multi-tier-jit" + ${IWASM_CMD} --multi-tier-jit --stack-size=1048576 --dir=. tsf.wasm + fi +else + echo "============> compile tsf.wasm to aot" + [[ $2 == "--sgx" ]] && ${WAMRC_CMD} -sgx -o tsf.aot tsf.wasm \ + || ${WAMRC_CMD} -o tsf.aot tsf.wasm + echo "============> run tsf.aot" + ${IWASM_CMD} --stack-size=1048576 --dir=. tsf.aot +fi + diff --git a/tests/standalone/tsf/tsf.wasm b/tests/standalone/tsf/tsf.wasm new file mode 100755 index 000000000..79b15dcbf Binary files /dev/null and b/tests/standalone/tsf/tsf.wasm differ diff --git a/tests/wamr-test-suites/test_wamr.sh b/tests/wamr-test-suites/test_wamr.sh index d3d9cb65c..135940e1d 100755 --- a/tests/wamr-test-suites/test_wamr.sh +++ b/tests/wamr-test-suites/test_wamr.sh @@ -14,7 +14,8 @@ function help() { echo "test_wamr.sh [options]" echo "-c clean previous test results, not start test" - echo "-s {suite_name} test only one suite (spec|malformed|wasi_certification|wamr_compiler)" + echo "-s {suite_name} test only one suite (spec|standalone|malformed|wasi_certification|" + echo " unit|wamr_compiler)" echo "-m set compile target of iwasm(x86_64|x86_32|armv7|armv7_vfp|thumbv7|thumbv7_vfp|" echo " riscv32|riscv32_ilp32f|riscv32_ilp32d|riscv64|" echo " riscv64_lp64f|riscv64_lp64d|aarch64|aarch64_vfp)" @@ -997,7 +998,8 @@ function trigger() # if we're running the wasi certification tests. if [[ $TEST_CASE_ARR ]]; then for test in "${TEST_CASE_ARR[@]}"; do - if [[ "$test" == "wasi_certification" ]]; then + if [[ "$test" == "wasi_certification" + || "$test" == "standalone" ]]; then EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_LIBC_UVWASI=0 -DWAMR_BUILD_LIBC_WASI=1" break fi @@ -1104,7 +1106,7 @@ if [[ $TEST_CASE_ARR ]];then trigger || (echo "TEST FAILED"; exit 1) else # test all suite, ignore polybench and libsodium because of long time cost - TEST_CASE_ARR=("spec" "malformed") + TEST_CASE_ARR=("spec" "malformed" "standalone") : ' if [[ $COLLECT_CODE_COVERAGE == 1 ]];then # add polybench if collecting code coverage data