Add standalone cases (#3536)

This commit is contained in:
Zhang, Yi 2024-06-19 16:40:37 +08:00 committed by GitHub
parent 7f94d183ac
commit 16e70f99aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
129 changed files with 3880 additions and 3 deletions

BIN
tests/standalone/bfs/bfs.wasm Executable file

Binary file not shown.

24
tests/standalone/bfs/run.sh Executable file
View File

@ -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

Binary file not shown.

View File

@ -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

Binary file not shown.

23
tests/standalone/blake3/run.sh Executable file
View File

@ -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

Binary file not shown.

27
tests/standalone/brotli/run.sh Executable file
View File

@ -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

Binary file not shown.

23
tests/standalone/c-ray/run.sh Executable file
View File

@ -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

View File

@ -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

View File

@ -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

Binary file not shown.

View File

@ -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

Binary file not shown.

Binary file not shown.

View File

@ -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

Binary file not shown.

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
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;
}

View File

@ -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

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
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;
}

View File

@ -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

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
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;
}

Binary file not shown.

View File

@ -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

BIN
tests/standalone/fasta/fasta.wasm Executable file

Binary file not shown.

24
tests/standalone/fasta/run.sh Executable file
View File

@ -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

Binary file not shown.

View File

@ -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

Binary file not shown.

24
tests/standalone/hashset/run.sh Executable file
View File

@ -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

View File

@ -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 ""

Binary file not shown.

Binary file not shown.

View File

@ -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

BIN
tests/standalone/nbody/nbody.wasm Executable file

Binary file not shown.

24
tests/standalone/nbody/run.sh Executable file
View File

@ -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

Binary file not shown.

View File

@ -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

23
tests/standalone/simple/run.sh Executable file
View File

@ -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

Binary file not shown.

31
tests/standalone/smallpt/run.sh Executable file
View File

@ -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

Binary file not shown.

Binary file not shown.

131
tests/standalone/standalone.sh Executable file
View File

@ -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 ==============*"

24
tests/standalone/stream/run.sh Executable file
View File

@ -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

Binary file not shown.

View File

@ -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

Binary file not shown.

Binary file not shown.

View File

@ -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

Binary file not shown.

24
tests/standalone/test-go/run.sh Executable file
View File

@ -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

Binary file not shown.

View File

@ -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)

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <inttypes.h>
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;
}

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#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);
}

View File

@ -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)

View File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
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;
}

View File

@ -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

Binary file not shown.

View File

@ -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

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,68 @@
/*
* This file is copied from https://web.dev/articles/wasm-threads
*/
#include <pthread.h>
#include <stdio.h>
/* 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;
}

Binary file not shown.

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
/* 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;
}

Binary file not shown.

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

Binary file not shown.

View File

@ -0,0 +1,91 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

Binary file not shown.

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

Binary file not shown.

View File

@ -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

View File

@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
/* 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;
}

Binary file not shown.

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
/* 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;
}

Binary file not shown.

View File

@ -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)

View File

@ -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.
>
> **&#9745; Only indicates** there is a WASM test case to **test the atomicity directly.** The atomicity of other opcodes **without &#9745;** 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) &#9745;
- Arithmetic:
- [ADD opcode](#arithmetic-add-sub-and-xchg) &#9745;
- [SUB opcode](#arithmetic-add-sub-and-xchg) &#9745;
- [XCHG opcode](#arithmetic-add-sub-and-xchg) &#9745;
- Logical:
- [AND opcode](#logical-or-xor-and)
- [OR opcode](#logical-or-xor-and)
- [XOR opcode](#logical-or-xor-and) &#9745;
- [LOAD](#atomic-ldstfence)
- [STORE](#atomic-ldstfence) &#9745;
- [FENCE](#atomic-ldstfence) &#9745;
- [WAIT & NOTIFY](#atomic-waitnotify) &#9745;
## 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&&notify.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&notify
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&&notify.c](./atomic_wait_notify.c) to test it anyways.

View File

@ -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 <stdio.h>
#include <pthread.h>
#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;
}

View File

@ -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;
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <pthread.h>
// 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;
}

View File

@ -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;
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
#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;
}

View File

@ -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;
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
// 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;
}

View File

@ -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;
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
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;
}

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More