From dc0d890e27c5c507e641f064329e81da4cbf456e Mon Sep 17 00:00:00 2001 From: Xuejun Yang Date: Wed, 20 May 2020 18:24:02 +0000 Subject: [PATCH 1/2] Enabled code coverage measurement --- .azure-pipelines/scripts/measure_code_cov.sh | 31 +++++++++ .azure-pipelines/scripts/measure_one_cov.sh | 70 ++++++++++++++++++++ config.mak | 7 ++ src/Makefile | 2 +- tests/basic/abort/Makefile | 2 +- tests/basic/clock/Makefile | 2 +- tests/basic/exit/Makefile | 2 +- tests/basic/global_vars_test/Makefile | 2 +- tests/basic/helloworld/Makefile | 2 +- tests/basic/illegal_instructions/Makefile | 2 +- tests/basic/nonroot_halt_test/Makefile | 2 +- tests/basic/pthread_join/Makefile | 2 +- tests/basic/signal/Makefile | 2 +- tests/basic/sleep/Makefile | 2 +- tests/basic/stat/Makefile | 2 +- tests/virtio/ping_test/Makefile | 2 +- 16 files changed, 121 insertions(+), 13 deletions(-) create mode 100755 .azure-pipelines/scripts/measure_code_cov.sh create mode 100755 .azure-pipelines/scripts/measure_one_cov.sh diff --git a/.azure-pipelines/scripts/measure_code_cov.sh b/.azure-pipelines/scripts/measure_code_cov.sh new file mode 100755 index 000000000..98a368c94 --- /dev/null +++ b/.azure-pipelines/scripts/measure_code_cov.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +if [ -z $SGXLKL_ROOT ]; then + echo "ERROR: 'SGXLKL_ROOT' is undefined. Please export SGXLKL_ROOT= source code repository" + exit 1 +fi + +# TODO: add samples to code coverage measurement. +# For now, only measure anything under 'tests' except for LTP. +test_folder_name=$SGXLKL_ROOT/tests +test_folder_identifier="Makefile" +test_exception_list="ltp" + +file_list=( $(sudo find $test_folder_name -name $test_folder_identifier | grep -v "$test_exception_list") ) + +total_tests=${#file_list[@]} +counter=0 + +rm -f $SGXLKL_ROOT/total_cov.info + +for file in ${file_list[@]}; +do + counter=$(($counter + 1)) + folder=$(dirname $file) + echo "$counter/$total_tests: Measuring code coverage in $folder" + cd $folder + $SGXLKL_ROOT/.azure-pipelines/scripts/measure_one_cov.sh + make clean +done + +echo "Done! All coverage data are aggregated to $SGXLKL_ROOT/total_cov.info" \ No newline at end of file diff --git a/.azure-pipelines/scripts/measure_one_cov.sh b/.azure-pipelines/scripts/measure_one_cov.sh new file mode 100755 index 000000000..c80a6157a --- /dev/null +++ b/.azure-pipelines/scripts/measure_one_cov.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +if [ -z $SGXLKL_ROOT ]; then + echo "ERROR: 'SGXLKL_ROOT' is undefined. Please export SGXLKL_ROOT= source code repository" + exit 1 +fi + +if [ ! -f "Makefile" ]; then + echo "ERROR: ${0} can only be invoked from a directory that contains Makefile" + exit 1 +fi + +# Get the timeout from the test module +DEFAULT_TIMEOUT=300 +timeout=$(make gettimeout 2> /dev/null) +[[ $? != 0 ]] && timeout=$DEFAULT_TIMEOUT +echo "Execution timeout: $timeout" + +timeout --kill-after=$(($timeout + 15)) $timeout make run-hw +make_exit=$? + +if [ $make_exit != 0 ]; then + echo "ERROR: run-hw failed with error code $make_exit" + exit $make_exit +fi + +timeout --kill-after=$(($timeout + 15)) $timeout make run-sw +make_exit=$? + +if [ $make_exit != 0 ]; then + echo "ERROR: run-sw failed with error code $make_exit" + exit $make_exit +fi + +if ls *.img 1> /dev/null 2>&1; then + mkdir img + sudo umount img + sudo mount -o loop *.img img + sudo rm -rf $SGXLKL_ROOT/cov + mkdir $SGXLKL_ROOT/cov + + # Gather all necessary files for lcov + cp -r $SGXLKL_ROOT/src/* $SGXLKL_ROOT/cov + sudo cp -r img/home/xuejun/sgx-lkl/build_musl $SGXLKL_ROOT/cov + sudo cp -r img/home/xuejun/sgx-lkl/sgx-lkl-musl $SGXLKL_ROOT/cov + sudo cp -r $SGXLKL_ROOT/build_musl $SGXLKL_ROOT/cov + sudo cp -r $SGXLKL_ROOT/sgx-lkl-musl $SGXLKL_ROOT/cov + + echo "Creating $SGXLKL_ROOT/cov.info" + sudo lcov -d $SGXLKL_ROOT/cov -c -o $SGXLKL_ROOT/cov.info + + # Accumulate the coverage data with data from other tests + if [ ! -f "$SGXLKL_ROOT/total_cov.info" ]; then + echo "Copy the code coverage for the 1st run" + sudo mv $SGXLKL_ROOT/cov.info $SGXLKL_ROOT/total_cov.info + else + echo "Aggregating code coverage to $SGXLKL_ROOT/total_cov.info..." + sudo lcov -a $SGXLKL_ROOT/total_cov.info -a $SGXLKL_ROOT/cov.info -o $SGXLKL_ROOT/total_cov.info + fi + + # clean up + echo "Cleaning up..." + sudo umount img + rm -rf img +else + echo "ERROR: disk image is not created" + exit 1 +fi + +exit 0 \ No newline at end of file diff --git a/config.mak b/config.mak index 2c20c22ad..9ca9e07a6 100755 --- a/config.mak +++ b/config.mak @@ -9,6 +9,9 @@ LKL_DEBUG ?= false # Select libc version (currently only musl libc is supported) LIBC ?= musl +# Measure code coverage +CODE_COVERAGE ?= false + SGXLKL_ROOT ?= $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) $(info $$SGXLKL_ROOT = [${SGXLKL_ROOT}]) @@ -131,6 +134,10 @@ else endif +ifeq ($(CODE_COVERAGE),true) + SGXLKL_CFLAGS_ENCLAVE_EXTRA += -fprofile-arcs -ftest-coverage +endif + ifeq ($(DEBUG),true) CMAKE_BUILD_TYPE=Debug else diff --git a/src/Makefile b/src/Makefile index b9706fab4..a033bfc6a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,7 +11,7 @@ CROSS_COMPILE = RANLIB = $(CROSS_COMPILE)ranlib CFLAGS_MAIN = -I$(OE_SDK_INCLUDES) -CFLAGS_ENCLAVE = -I$(OE_SDK_INCLUDES) -I${OE_SDK_INCLUDES}/openenclave/3rdparty -fPIE +CFLAGS_ENCLAVE = ${SGXLKL_CFLAGS_ENCLAVE_EXTRA} -I$(OE_SDK_INCLUDES) -I${OE_SDK_INCLUDES}/openenclave/3rdparty -fPIE LINK_MAIN = GIT_VERSION = "$(shell git describe --dirty --always --tags || echo unknown)" diff --git a/tests/basic/abort/Makefile b/tests/basic/abort/Makefile index 2c27c38e3..d45b1c8c6 100644 --- a/tests/basic/abort/Makefile +++ b/tests/basic/abort/Makefile @@ -4,7 +4,7 @@ PROG=abort PROG_C=abort.c DISK_IMAGE=sgxlkl-abort.img -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/clock/Makefile b/tests/basic/clock/Makefile index 543c18073..c12963bb7 100644 --- a/tests/basic/clock/Makefile +++ b/tests/basic/clock/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=clock PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/exit/Makefile b/tests/basic/exit/Makefile index bb3684a37..b982e2501 100644 --- a/tests/basic/exit/Makefile +++ b/tests/basic/exit/Makefile @@ -7,7 +7,7 @@ TEST3=raise-test SRCS=$(wildcard *.c) DISK_IMAGE=sgxlkl-exit-test.img -IMAGE_SIZE=5M +IMAGE_SIZE=50M SGXLKL_ENV=SGXLKL_VERBOSE=1 SGXLKL_KERNEL_VERBOSE=1 SGXLKL_TRACE_SIGNAL=1 diff --git a/tests/basic/global_vars_test/Makefile b/tests/basic/global_vars_test/Makefile index f00215590..56d999d82 100644 --- a/tests/basic/global_vars_test/Makefile +++ b/tests/basic/global_vars_test/Makefile @@ -3,7 +3,7 @@ include ../../common.mk PROG=global_vars_test PROG_SRC=$(PROG).c PROG_ARGS=-a -b -c hello -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/helloworld/Makefile b/tests/basic/helloworld/Makefile index 20818648f..bed35cea0 100644 --- a/tests/basic/helloworld/Makefile +++ b/tests/basic/helloworld/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=helloworld PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/illegal_instructions/Makefile b/tests/basic/illegal_instructions/Makefile index 45d9ba5f1..398a6f547 100644 --- a/tests/basic/illegal_instructions/Makefile +++ b/tests/basic/illegal_instructions/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=illegal_instructions-test PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/nonroot_halt_test/Makefile b/tests/basic/nonroot_halt_test/Makefile index ba3bf0212..cafff9f93 100644 --- a/tests/basic/nonroot_halt_test/Makefile +++ b/tests/basic/nonroot_halt_test/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=nonroot_halt_test PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/pthread_join/Makefile b/tests/basic/pthread_join/Makefile index e1154cbee..8133816ef 100644 --- a/tests/basic/pthread_join/Makefile +++ b/tests/basic/pthread_join/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=pthread_join-test PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/signal/Makefile b/tests/basic/signal/Makefile index aca922b30..634edaa98 100644 --- a/tests/basic/signal/Makefile +++ b/tests/basic/signal/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=signal PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/sleep/Makefile b/tests/basic/sleep/Makefile index 5d14f865a..f6f4dfc8d 100644 --- a/tests/basic/sleep/Makefile +++ b/tests/basic/sleep/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=sleep-test PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/basic/stat/Makefile b/tests/basic/stat/Makefile index 1f6824103..b357bbc9f 100644 --- a/tests/basic/stat/Makefile +++ b/tests/basic/stat/Makefile @@ -2,7 +2,7 @@ include ../../common.mk PROG=stat PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 diff --git a/tests/virtio/ping_test/Makefile b/tests/virtio/ping_test/Makefile index 407cd97fa..64c73cfa7 100644 --- a/tests/virtio/ping_test/Makefile +++ b/tests/virtio/ping_test/Makefile @@ -4,7 +4,7 @@ SHELL := /bin/bash PROG=dummy_server PROG_SRC=$(PROG).c -IMAGE_SIZE=5M +IMAGE_SIZE=50M EXECUTION_TIMEOUT=60 From 2f157ae095bcb9bf490718dc55a6ac24517fca48 Mon Sep 17 00:00:00 2001 From: Xuejun Yang Date: Fri, 29 May 2020 19:24:20 +0000 Subject: [PATCH 2/2] Address reiew comments and update commit sha to sgx-lkl-musl --- .azure-pipelines/scripts/measure_one_cov.sh | 19 +++---------------- sgx-lkl-musl | 2 +- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/.azure-pipelines/scripts/measure_one_cov.sh b/.azure-pipelines/scripts/measure_one_cov.sh index c80a6157a..2d67f5109 100755 --- a/.azure-pipelines/scripts/measure_one_cov.sh +++ b/.azure-pipelines/scripts/measure_one_cov.sh @@ -13,24 +13,11 @@ fi # Get the timeout from the test module DEFAULT_TIMEOUT=300 timeout=$(make gettimeout 2> /dev/null) -[[ $? != 0 ]] && timeout=$DEFAULT_TIMEOUT +[[ $? -ne 0 ]] && timeout=$DEFAULT_TIMEOUT echo "Execution timeout: $timeout" timeout --kill-after=$(($timeout + 15)) $timeout make run-hw -make_exit=$? - -if [ $make_exit != 0 ]; then - echo "ERROR: run-hw failed with error code $make_exit" - exit $make_exit -fi - timeout --kill-after=$(($timeout + 15)) $timeout make run-sw -make_exit=$? - -if [ $make_exit != 0 ]; then - echo "ERROR: run-sw failed with error code $make_exit" - exit $make_exit -fi if ls *.img 1> /dev/null 2>&1; then mkdir img @@ -41,8 +28,8 @@ if ls *.img 1> /dev/null 2>&1; then # Gather all necessary files for lcov cp -r $SGXLKL_ROOT/src/* $SGXLKL_ROOT/cov - sudo cp -r img/home/xuejun/sgx-lkl/build_musl $SGXLKL_ROOT/cov - sudo cp -r img/home/xuejun/sgx-lkl/sgx-lkl-musl $SGXLKL_ROOT/cov + sudo cp -r img$SGXLKL_ROOT/build_musl $SGXLKL_ROOT/cov + sudo cp -r img$SGXLKL_ROOT/sgx-lkl-musl $SGXLKL_ROOT/cov sudo cp -r $SGXLKL_ROOT/build_musl $SGXLKL_ROOT/cov sudo cp -r $SGXLKL_ROOT/sgx-lkl-musl $SGXLKL_ROOT/cov diff --git a/sgx-lkl-musl b/sgx-lkl-musl index c7fe5df1a..00e2fcb96 160000 --- a/sgx-lkl-musl +++ b/sgx-lkl-musl @@ -1 +1 @@ -Subproject commit c7fe5df1a863ce35b56cf778d9a1be80958776ad +Subproject commit 00e2fcb9600bb87a541a88e6772cb423c56090e3