ASAN使用脚本实例

#!/bin/sh
#########################################################################
# File Name: cpnt_asan_start.sh
# Author: vitor
# Created Time: 2018.6.6
#########################################################################


#asan clean function
asan_clean()
{
    rm -r ${ASAN_PATH}/log
    rm -r ${ASAN_PATH}/*html
    rm -r ${ASAN_PATH}/build*_asan
    rm -r ${ASAN_PATH}/*_sdv
    rm -r ${ASAN_PATH}/*_st
    pkill _st
    pkill _sdv
    mkdir ${ASAN_PATH}/log
}


asan_help()
{
   echo "  "
   echo "=======================How to use============================="
   echo "Usage:           The script need one parameter."
   echo "Example:         $0 clean"
   echo "                                                              "
   echo "=====================Cmd param List==========================="
   echo "CmdParam         Detail"
   echo "clean            rm asan info"
   echo "all              src and test code compiled"
   echo "test             only test code compiled"
}


asan_src_build()
{
    echo "**************Begin Build SSP_SUSE11_X86_64 SRC_POOLS**********************"
    cd $SRC_PATH
    if [ ! -d "configs" ]; then
        mkdir configs
    fi
    
    cp ${WORK_DIR}/resource/suse11_x86_64_asan.py configs/
    rm -r build
    scons distclean
    scons cfg=configs/suse11_x86_64_asan_asan.py -j8
    
    # backup
    if [ -d "${ASAN_PATH}/build_suse11_x86_64_asan" ]; then
        rm -r ${ASAN_PATH}/build_suse11_x86_64_asan
    fi
    cp -r build ${ASAN_PATH}/build_suse11_x86_64_asan


    echo "**************End Build SSP_SUSE11_X86_64 SRC_POOLS**********************"
    cd $WORK_DIR
}


asan_testcode_build()
{
    if [ ! -d "${TEST_PATH}" ]; then
        echo "[ERROR] ${TEST_PATH} doesn't exist!(Line $LINENO)"
        exit 0
    elif [ ! -d "${SRC_PATH}" ]; then
        echo "[ERROR]${SRC_PATH} doesn't exist!"
        exit 0
    elif  [ ! -d "${ASAN_PATH}/build_suse11_x86_64_asan" ]; then
        echo "[ERROR] SRC_POOLS doesn't exist!"
        exit 0  
    fi
    
    CUNITSRC_DIR=${TEST_PATH}/teststub/src/CUnit
    LIB_DIR=${TEST_PATH}/teststub/linux/lib


    #copy pools
    if [ -d "${SRC_PATH}/build" ]; then
        rm -r ${SRC_PATH}/build/*
    fi
    cp -r -f ${ASAN_PATH}/build_suse11_x86_64_asan/*  ${SRC_PATH}/build
    
    #delete dpgs
    ${WORK_DIR}/del_pools.sh ${SRC_PATH}/build/dpg_pools/*suse11_x86_64_gnu_* ${TEST_MODULE}
    
    echo "**************Begin Build  SSP TESTCODE PROJECT**********************"


    cp ${WORK_DIR}/resource/SConstruct_suse11_x86_64_asan ${TEST_PATH}


    if [ ! -f "${LIB_DIR}/libcunit_suse11_x86_64_bySuiteIndex.a" ]; then
        cd $CUNITSRC_DIR
        echo "[INFO]CUnit build Begin"
        scons -j 32  cfg=${CONFIGS_PATH}/suse11_x86_64.py
        cp build/building_blocks/*suse11_x86_64_gnu_*/cunit/lib/libcunit_1.1.1.a ${LIB_DIR}/libcunit_suse11_x86_64_bySuiteIndex.a    
    fi


    cd ${TEST_PATH}/src
    
    #copy mocking functions
    find -maxdepth 3 -name "${TEST_MODULE}" -type d -exec cp ${WORK_DIR}/resource/memOpt_mock.c {}/ \;


    if [ -d "${TEST_PATH}/build" ]; then
        rm -r ${TEST_PATH}/build
    fi


    echo "[INFO] ${TEST_MODULE}_suse11_x86_64 build Begin......"
    cd ${TEST_PATH}
    scons -f SConstruct_suse11_x86_64_asan ${TEST_MODULE} -j10
    echo "**************End Build ${TEST_MODULE}_suse11_x86_64**********************"


    echo "[INFO] Copy ${TEST_MODULE}"


    if [ -d "${ASAN_PATH}/${TEST_MODULE}" ]; then
        rm -r ${ASAN_PATH}/${TEST_MODULE}    
    fi
    if [ ! -f "${TEST_PATH}/build/bin/${TEST_TYPE}/${TEST_MODULE}/${TEST_MODULE}" ]; then
        echo "[ERROR]Compile ${TEST_MODULE} failed!" 
        exit 0
    fi


    mkdir ${ASAN_PATH}/${TEST_MODULE}


    cd ${TEST_PATH}
    cp -r -f build/bin/${TEST_TYPE}/${TEST_MODULE}/* ${ASAN_PATH}/${TEST_MODULE}/


    echo "**************End Build  SSP TESTCODE PROJECT**********************"
    cd $WORK_DIR
}


asan_so_make_copy()
{
    cd ${TEST_PATH}/src/dbg/cpnt_so


    if [ -d "$SRC_PATH/src/dbg/inspect/cpnt/" ]; then
       find $SRC_PATH/src/dbg/inspect/cpnt/ -type f -name *.ph | xargs -i cp {} ./include/
       find $SRC_PATH/src/dbg/inspect/cpnt/ -type f -name *.h | xargs -i cp {} ./include/
    fi


    if [ -d "$SRC_PATH/build/" ]; then
       find $SRC_PATH/build/ -type f | grep include | grep build_root | xargs -i cp {} ./include/
    fi


    export PATH=${PATH_OLD}
    scons distclean
    scons cfg=$CONFIGS_PATH/euler_x86_64_dorado.py -j8


    rm -r output
    mkdir -p output
    find ./build/ -type f -name *.so | xargs -i cp {} ./output/;
    find ./build/ -type f -name *.a | xargs -i cp {} ./output/;
    find ./build/ -type f -name *.bin | xargs -i cp {} ./output/;


    cd ./src/cpntso/
    make
   
    #copy file
    cd $ASAN_PATH/$TEST_MODULE
    
    mkdir -p ./cfg/
    mkdir -p ./so/
    mkdir -p ./sdvso/
    cp -rf ${TEST_PATH}/src/dbg/cpnt_so/src/cpntcfg/* ./cfg/
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTest_2.1.1.so ./so/libinstance.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestWithUndef_2.1.1.so ./so/libinstancewithundef.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestShell_2.1.1.so ./so/libshell.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestShell_2.1.1.so ./so/libinst.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestMoreThanShell_2.1.1.so ./so/libinstancemorethanshell.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestStatic_2.1.1.a ./so/libinstance.a


    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestStrip_2.1.1.so ./so/libCpntTestStrip_2.1.1.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestMultiSection_2.1.1.so ./so/libCpntTestMultiSection_2.1.1.so
    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntTestSameFileSameSymbol_2.1.1.so ./so/libCpntTestSameFileSameSymbol_2.1.1.so


    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libinstance_32.so ./so/libinstance_32.so


    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/libCpntSdvTest* ./sdvso/


    cp -f ${TEST_PATH}/src/dbg/cpnt_so/output/*.bin ./sdvso/
    
    cd $WORK_DIR
}


asan_test_run()
{
    if [ ! -d "${ASAN_PATH}/$TEST_MODULE" ]; then
        echo "[ERROR] ${ASAN_PATH}/$TEST_MODULE doesn't exist!"
        exit 0
    fi

    # 运行时需要设置环境变量
    export LD_PRELOAD=${COMPILER_PATH}/lib64/libasan.so.3
 export ASAN_OPTIONS=halt_on_error=0:use_sigaltstack=0:handle_segv=0:quarantine_size=4194304:fast_unwind_on_fatal=1:fast_unwind_on_check=1:fast_unwind_on_malloc=1:allow_user_segv_handler=1:malloc_context_size=15:force_check_leak=1:detect_stack_use_after_return=1:check_initialization_order=1:log_path=/home/vitor/cpnt/cpnt_sdv_asan.log
    ulimit -v unlimited
    
    if [ ! -d $ASAN_PATH/$TEST_MODULE ]; then
        echo "[ERROR]$TEST_MODULE not exist!"
        exit 0
    fi
    
    #run test prj
    cd $ASAN_PATH/$TEST_MODULE
    ./${TEST_MODULE} -c ${TEST_MODULE}.cfg -m 2
}




#=========================================BEGIN==========================================
if [ $# != 1 ]; then
   asan_help
   exit 0
fi


WORK_DIR=$(cd $(dirname $0); pwd)


#ASAN_PATH需要根据实际情况配置
ASAN_PATH=${WORK_DIR}/../asan
LOG_PATH=${ASAN_PATH}/log


#PRJ_PATH需要根据实际情况配置
PRJ_PATH=${WORK_DIR}/../platform_proj/module
SRC_PATH=${PRJ_PATH}/src/modulev2
TEST_PATH=${PRJ_PATH}/testcode/v2test
CONFIGS_PATH=${PRJ_PATH}/configs


#for test code
TEST_MODULE=cpnt_sdv
TEST_TYPE=sdv


if [ $1 = "clean" ]; then
   asan_clean
   exit 0
elif [ $1 = "--help" ]; then
   asan_help
   exit 0      
elif [ $1 != "all" -a $1 != "test" -a $1 != "run" ]; then  
   asan_help
   exit 0    
fi


echo "[INFO] WORK_DIR is $WORK_DIR"  
chmod 777 ${WORK_DIR}/*.sh


#set gcc
#COMPILER_PATH需要根据实际情况配置
COMPILER_PATH=/home/vitor/cpnt/gcc-6.1.0-x64


PATH_OLD=$PATH
PATH_NEW=${COMPILER_PATH}/bin:${PATH}
export PATH=${PATH_NEW}


cd ${WORK_DIR}


if [ ! -d "${ASAN_PATH}" ]; then
        mkdir ${ASAN_PATH}
elif [ ! -d "${LOG_PATH}" ]; then
        mkdir ${LOG_PATH}
fi


if [ $1 = "all" ]; then
   asan_src_build
   asan_testcode_build
   asan_so_make_copy
   asan_test_run
elif [ $1 = "test" ]; then
   asan_testcode_build
   asan_so_make_copy
   asan_test_run
else
   asan_test_run
fi

你可能感兴趣的:(linux)