Skip to content

Commit

Permalink
Update cmake files and wamr-test-suites to support collect code cover…
Browse files Browse the repository at this point in the history
…age (bytecodealliance#1992)

Support collecting code coverage with wamr-test-suites script by using
lcov and genhtml tools, eg.:
  cd tests/wamr-test-suites
  ./test_wamr.sh -s spec -b -P -C

The default code coverage and html files are generated at:
  tests/wamr-test-suites/workspace/wamr.lcov
  tests/wamr-test-suites/workspace/wamr-lcov.zip

And update wamr-test-suites scripts to support testing GC spec cases to
avoid frequent synchronization conflicts between branch main and dev/gc.
  • Loading branch information
wenyongh authored Feb 28, 2023
1 parent b4f0228 commit 9b9ae0c
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 116 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ if (NOT DEFINED WAMR_BUILD_REF_TYPES)
set (WAMR_BUILD_REF_TYPES 0)
endif ()

if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})

include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
Expand Down
9 changes: 9 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,12 @@ if (WAMR_BUILD_WASM_CACHE EQUAL 1)
add_definitions (-DWASM_ENABLE_WASM_CACHE=1)
message (" Wasm files cache enabled")
endif ()
if (WAMR_BUILD_GC_HEAP_VERIFY EQUAL 1)
add_definitions (-DWASM_ENABLE_GC_VERIFY=1)
message (" GC heap verification enabled")
endif ()
if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
message (" Collect code coverage enabled")
endif ()
5 changes: 0 additions & 5 deletions core/iwasm/libraries/wasi-nn/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
set (WAMR_BUILD_SIMD 0)
endif ()

if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)

include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
Expand Down
4 changes: 0 additions & 4 deletions product-mini/platforms/linux-sgx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ if (NOT DEFINED WAMR_BUILD_SGX_IPFS)
set (WAMR_BUILD_SGX_IPFS 0)
endif ()

if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic \
Expand Down
4 changes: 0 additions & 4 deletions product-mini/platforms/linux-sgx/CMakeLists_minimal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
set (WAMR_BUILD_LIB_PTHREAD 0)
endif ()

if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic \
Expand Down
5 changes: 0 additions & 5 deletions product-mini/platforms/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
set (WAMR_BUILD_SIMD 0)
endif ()

if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
Expand Down
4 changes: 0 additions & 4 deletions product-mini/platforms/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
set (WAMR_BUILD_SIMD 0)
endif ()

if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
Expand Down
4 changes: 0 additions & 4 deletions samples/sgx-ra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ set (WAMR_BUILD_FAST_INTERP 1)
set (WAMR_BUILD_LIB_RATS 1)

# compiling and linking flags
if (COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
endif ()

set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic \
Expand Down
5 changes: 5 additions & 0 deletions samples/wasi-threads/wasm-apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (CMAKE_ASM_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (CMAKE_EXE_LINKER_FLAGS "-target wasm32-wasi-threads")

if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1)
set (CMAKE_C_FLAGS "")
set (CMAKE_CXX_FLAGS "")
endif ()

function (compile_sample SOURCE_FILE)
get_filename_component (FILE_NAME ${SOURCE_FILE} NAME_WLE)
set (WASM_MODULE ${FILE_NAME}.wasm)
Expand Down
46 changes: 43 additions & 3 deletions tests/wamr-test-suites/spec-test-script/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

"""
The script itself has to be put under the same directory with the "spec".
To run a single non-GC case with interpreter mode:
cd workspace
python3 runtest.py --wast2wasm wabt/bin/wat2wasm --interpreter iwasm \
spec/test/core/xxx.wast
To run a single non-GC case with aot mode:
cd workspace
python3 runtest.py --aot --wast2wasm wabt/bin/wat2wasm --interpreter iwasm \
--aot-compiler wamrc spec/test/core/xxx.wast
To run a single GC case:
cd workspace
python3 runtest.py --wast2wasm spec/interpreter/wasm --interpreter iwasm \
--aot-compiler wamrc --gc spec/test/core/xxx.wast
"""

PLATFORM_NAME = os.uname().sysname.lower()
Expand All @@ -22,9 +34,9 @@
IWASM_QEMU_CMD = "iwasm"
SPEC_TEST_DIR = "spec/test/core"
WAST2WASM_CMD = "./wabt/out/gcc/Release/wat2wasm"
SPEC_INTERPRETER_CMD = "spec/interpreter/wasm"
WAMRC_CMD = "../../../wamr-compiler/build/wamrc"


class TargetAction(argparse.Action):
TARGET_MAP = {
"ARMV7_VFP": "armv7",
Expand All @@ -51,6 +63,7 @@ def ignore_the_case(
multi_module_flag=False,
multi_thread_flag=False,
simd_flag=False,
gc_flag=False,
xip_flag=False,
qemu_flag=False
):
Expand All @@ -63,6 +76,10 @@ def ignore_the_case(
if "i386" == target and case_name in ["float_exprs"]:
return True

if gc_flag:
if case_name in ["type-canon", "type-equivalence", "type-rec"]:
return True;

if sgx_flag:
if case_name in ["conversions", "f32_bitwise", "f64_bitwise"]:
return True
Expand All @@ -76,7 +93,9 @@ def ignore_the_case(
return True

if qemu_flag:
if case_name in ["f32_bitwise", "f64_bitwise", "loop", "f64", "f64_cmp", "conversions", "f32", "f32_cmp", "float_exprs", "float_misc", "select", "memory_grow"]:
if case_name in ["f32_bitwise", "f64_bitwise", "loop", "f64", "f64_cmp",
"conversions", "f32", "f32_cmp", "float_exprs",
"float_misc", "select", "memory_grow"]:
return True

return False
Expand Down Expand Up @@ -109,6 +128,7 @@ def test_case(
xip_flag=False,
clean_up_flag=True,
verbose_flag=True,
gc_flag=False,
qemu_flag=False,
qemu_firmware='',
log='',
Expand All @@ -124,14 +144,15 @@ def test_case(
multi_module_flag,
multi_thread_flag,
simd_flag,
gc_flag,
xip_flag,
qemu_flag
):
return True

CMD = ["python3", "runtest.py"]
CMD.append("--wast2wasm")
CMD.append(WAST2WASM_CMD)
CMD.append(WAST2WASM_CMD if not gc_flag else SPEC_INTERPRETER_CMD)
CMD.append("--interpreter")
if sgx_flag:
CMD.append(IWASM_SGX_CMD)
Expand Down Expand Up @@ -171,6 +192,9 @@ def test_case(
if not clean_up_flag:
CMD.append("--no_cleanup")

if gc_flag:
CMD.append("--gc")

if log != '':
CMD.append("--log-dir")
CMD.append(log)
Expand Down Expand Up @@ -231,6 +255,7 @@ def test_suite(
xip_flag=False,
clean_up_flag=True,
verbose_flag=True,
gc_flag=False,
parl_flag=False,
qemu_flag=False,
qemu_firmware='',
Expand All @@ -246,6 +271,10 @@ def test_suite(
simd_case_list = sorted(suite_path.glob("simd/*.wast"))
case_list.extend(simd_case_list)

if gc_flag:
gc_case_list = sorted(suite_path.glob("gc/*.wast"))
case_list.extend(gc_case_list)

case_count = len(case_list)
failed_case = 0
successful_case = 0
Expand All @@ -268,6 +297,7 @@ def test_suite(
xip_flag,
clean_up_flag,
verbose_flag,
gc_flag,
qemu_flag,
qemu_firmware,
log,
Expand Down Expand Up @@ -304,6 +334,7 @@ def test_suite(
xip_flag,
clean_up_flag,
verbose_flag,
gc_flag,
qemu_flag,
qemu_firmware,
log,
Expand Down Expand Up @@ -414,6 +445,13 @@ def main():
dest="verbose_flag",
help="Close real time output while running cases, only show last words of failed ones",
)
parser.add_argument(
"--gc",
action="store_true",
default=False,
dest="gc_flag",
help="Running with GC feature",
)
parser.add_argument(
"cases",
metavar="path_to__case",
Expand Down Expand Up @@ -446,6 +484,7 @@ def main():
options.xip_flag,
options.clean_up_flag,
options.verbose_flag,
options.gc_flag,
options.parl_flag,
options.qemu_flag,
options.qemu_firmware,
Expand All @@ -469,6 +508,7 @@ def main():
options.xip_flag,
options.clean_up_flag,
options.verbose_flag,
options.gc_flag,
options.qemu_flag,
options.qemu_firmware,
options.log
Expand Down
80 changes: 80 additions & 0 deletions tests/wamr-test-suites/spec-test-script/collect_coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

readonly WORK_DIR=$PWD
readonly WAMR_DIR=${WORK_DIR}/../../..
readonly DST_COV_FILE=$1
readonly SRC_COV_DIR=$2
readonly SRC_TEMP_COV_FILE=wamr_temp.lcov
readonly SRC_COV_FILE=wamr.lcov

# get dest folder
dir=$(dirname ${DST_COV_FILE})
pushd ${dir} > /dev/null 2>&1
readonly DST_COV_DIR=${PWD}
popd > /dev/null 2>&1

if [[ ! -d ${SRC_COV_DIR} ]]; then
echo "${SRC_COV_DIR} doesn't exist, ignore code coverage collection"
exit
fi

echo "Start to collect code coverage of ${SRC_COV_DIR} .."

pushd ${SRC_COV_DIR} > /dev/null 2>&1

# collect all code coverage data
lcov -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1
# extract code coverage data of WAMR source files
lcov -r ${SRC_TEMP_COV_FILE} -o ${SRC_TEMP_COV_FILE} \
-rc lcov_branch_coverage=1 \
"*/usr/*" "*/_deps/*" "*/deps/*" "*/tests/unit/*" \
"*/llvm/include/*" "*/include/llvm/*" "*/samples/*" \
"*/app-framework/*" "*/test-tools/*"

if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
if [[ -s ${DST_COV_FILE} ]]; then
# merge code coverage data
lcov --rc lcov_branch_coverage=1 \
--add-tracefile ${SRC_TEMP_COV_FILE} \
-a ${DST_COV_FILE} -o ${SRC_COV_FILE}
# backup the original lcov file
cp -a ${DST_COV_FILE} "${DST_COV_FILE}.orig"
# replace the lcov file
cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
else
cp -a ${SRC_TEMP_COV_FILE} ${SRC_COV_FILE}
cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
fi

# get ignored prefix path
dir=$(dirname ${WAMR_DIR}/../..)
pushd ${dir} > /dev/null 2>&1
prefix_full_path=${PWD}
popd > /dev/null 2>&1

# generate html output for merged code coverage data
rm -fr ${DST_COV_DIR}/wamr-lcov
genhtml -t "WAMR Code Coverage" \
--rc lcov_branch_coverage=1 --prefix=${prefix_full_path} \
-o ${DST_COV_DIR}/wamr-lcov \
${DST_COV_FILE}

cd ${DST_COV_DIR}
rm -f wamr-lcov.zip
zip -r -q -o wamr-lcov.zip wamr-lcov
rm -fr wamr-lcov

echo "Code coverage file ${DST_COV_FILE} was generated or appended"
echo "Code coverage html ${DST_COV_DIR}/wamr-lcov.zip was generated"
else
echo "generate code coverage html failed"
fi

echo ""

popd > /dev/null 2>&1
Loading

0 comments on commit 9b9ae0c

Please sign in to comment.