1cmake_minimum_required(VERSION 3.5) 2 3# project name 4project (ubsan_test) 5 6set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../exe) 7 8# Add executable 9file(GLOB TESTCASES src/*.cpp) 10foreach(srcfile ${TESTCASES}) 11 get_filename_component(testname ${srcfile} NAME_WE) 12 set(CMAKE_CXX_FLAGS "-fsanitize=undefined") 13 14 if (${testname} STREQUAL "add-overflow") 15 add_executable(add-overflow-32 ${srcfile}) 16 target_compile_definitions(add-overflow-32 PRIVATE ADD_I32) 17 add_executable(add-overflow-64 ${srcfile}) 18 target_compile_definitions(add-overflow-64 PRIVATE ADD_I64) 19 add_executable(add-overflow-128 ${srcfile}) 20 target_compile_definitions(add-overflow-128 PRIVATE ADD_I128) 21 elseif(${testname} STREQUAL "div-zero") 22 add_executable(div-zero-0 ${srcfile}) 23 target_compile_definitions(div-zero-0 PRIVATE DIVIDEND=0) 24 add_executable(div-zero-1U ${srcfile}) 25 target_compile_definitions(div-zero-1U PRIVATE DIVIDEND=1U) 26 add_executable(div-zero-123 ${srcfile}) 27 target_compile_definitions(div-zero-123 PRIVATE "DIVIDEND=intmax(123)") 28 add_executable(div-zero-1-5 ${srcfile}) 29 target_compile_options(div-zero-1-5 PRIVATE -fsanitize=float-divide-by-zero) 30 target_compile_definitions(div-zero-1-5 PRIVATE DIVIDEND=1.5) 31 elseif(${testname} STREQUAL "shift") 32 set(UBSAN_SHIFT_FLAG "-fno-sanitize-recover=shift") 33 set(CMAKE_CXX_FLAGS "") 34 # LSH_OVERFLOW 35 # CHECK-LSH_OVERFLOW: shift.cpp:[[@LINE+1]]:5: runtime error: left shift of negative value -2147483648 36 add_executable(shift-lsh-overflow ${srcfile}) 37 target_compile_options(shift-lsh-overflow PRIVATE -fsanitize=shift ${UBSAN_SHIFT_FLAG} "-DLSH_OVERFLOW" "-DOP=<<") 38 add_executable(shift-lsh-overflow-1 ${srcfile}) 39 target_compile_options(shift-lsh-overflow-1 PRIVATE -fsanitize=shift-base ${UBSAN_SHIFT_FLAG} "-DLSH_OVERFLOW" "-DOP=<<") 40 add_executable(shift-lsh-overflow-2 ${srcfile}) 41 target_compile_options(shift-lsh-overflow-2 PRIVATE -fsanitize=shift-exponent ${UBSAN_SHIFT_FLAG} "-DLSH_OVERFLOW" "-DOP=<<") 42 # TOO_LOW 43 # CHECK-TOO_LOW: shift.cpp:[[@LINE+1]]:5: runtime error: shift exponent -3 is negative 44 add_executable(shift-too-low ${srcfile}) 45 target_compile_options(shift-too-low PRIVATE ${UBSAN_SHIFT_FLAG} "-DTOO_LOW" "-DOP=<<") 46 # TOO_HIGH 47 # CHECK-TOO_HIGH: shift.cpp:[[@LINE+1]]:5: runtime error: shift exponent 32 is too large for 32-bit type 'int' 48 add_executable(shift-high ${srcfile}) 49 target_compile_options(shift-high PRIVATE ${UBSAN_SHIFT_FLAG} "-DTOO_HIGH" "-DOP=>>") 50 #end 51 elseif(${testname} STREQUAL "uadd-overflow") 52 add_executable(uadd-overflow-32 ${srcfile}) 53 target_compile_definitions(uadd-overflow-32 PRIVATE ADD_I32) 54 add_executable(uadd-overflow-64 ${srcfile}) 55 target_compile_definitions(add-overflow-64 PRIVATE ADD_I64) 56 add_executable(uadd-overflow-128 ${srcfile}) 57 target_compile_definitions(uadd-overflow-128 PRIVATE ADD_I128) 58 elseif(${testname} STREQUAL "cast-overflow") 59 # ld.lld: error: undefined symbol: __sync_val_compare_and_swap_1 60 continue() 61 elseif(${testname} STREQUAL "unreachable") 62 # undefined symbol: returns_unexpectedly 63 continue() 64 else() 65 add_executable(${testname} ${srcfile}) 66 endif() 67endforeach() 68 69file(GLOB CASES_C src/*.c) 70foreach(src_c ${CASES_C}) 71 get_filename_component(name_c ${src_c} NAME_WE) 72 set(CMAKE_CXX_FLAGS "-fsanitize=undefined") 73 if(${name_c} STREQUAL "vla") 74 # did not work as expected 75 add_executable(vla ${src_c}) 76 target_compile_options(shift-lsh-overflow-1 PRIVATE -fsanitize=vla-bound -O3) 77 endif() 78endforeach()