CMakeLists.txt 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. include_directories(${PROJECT_SOURCE_DIR}/include)
  2. include_directories(${PROJECT_SOURCE_DIR}/deps)
  3. include_directories(${PROJECT_SOURCE_DIR}/src)
  4. include_directories(${PROJECT_SOURCE_DIR}/src/server)
  5. include_directories(${PROJECT_SOURCE_DIR}/plugins)
  6. include_directories(${PROJECT_SOURCE_DIR}/tests/testing-plugins)
  7. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  8. remove_definitions(-std=c99 -Wmissing-prototypes -Wstrict-prototypes)
  9. set (CMAKE_CXX_STANDARD 11)
  10. if (NOT UA_BUILD_OSS_FUZZ)
  11. if(NOT "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang")
  12. MESSAGE(FATAL_ERROR "To build fuzzing, you need to use Clang as the compiler")
  13. endif()
  14. # oss-fuzz builds already include these flags
  15. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
  16. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fsanitize=address")
  17. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -fsanitize=address")
  18. else()
  19. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -gline-tables-only -fsanitize=address -fsanitize-address-use-after-scope -fsanitize-coverage=trace-pc-guard,trace-cmp")
  20. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -gline-tables-only -fsanitize=address -fsanitize-address-use-after-scope -fsanitize-coverage=trace-pc-guard,trace-cmp")
  21. endif()
  22. set(LIBS Fuzzer)
  23. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  24. else()
  25. set(LIBS $ENV{LIB_FUZZING_ENGINE})
  26. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $ENV{OUT})
  27. endif()
  28. list(APPEND LIBS "${open62541_LIBRARIES}")
  29. # Use different plugins for testing
  30. set(fuzzing_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_tcp.c
  31. ${PROJECT_SOURCE_DIR}/plugins/ua_clock.c
  32. ${PROJECT_SOURCE_DIR}/tests/testing-plugins/testing_networklayers.c
  33. ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.c
  34. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.c
  35. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.c
  36. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
  37. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c)
  38. add_library(open62541-fuzzplugins OBJECT ${fuzzing_plugin_sources})
  39. add_dependencies(open62541-fuzzplugins open62541)
  40. # the fuzzer test are built directly on the open62541 object files. so they can
  41. # access symbols that are hidden/not exported to the shared library
  42. set(FUZZER_TARGETS)
  43. macro(add_fuzzer FUZZER_NAME FUZZER_SOURCE)
  44. add_executable(${FUZZER_NAME} $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-fuzzplugins> ${FUZZER_SOURCE} ${ARGN})
  45. target_link_libraries(${FUZZER_NAME} ${LIBS})
  46. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
  47. assign_source_group(${FUZZER_SOURCE})
  48. list(APPEND FUZZER_TARGETS ${FUZZER_NAME})
  49. endmacro()
  50. # Add new fuzzers here
  51. add_fuzzer(fuzz_binary_message fuzz_binary_message.cc)
  52. add_fuzzer(fuzz_binary_decode fuzz_binary_decode.cc)
  53. file(GLOB CORPUS_FILES ${PROJECT_SOURCE_DIR}/tests/fuzz/fuzz_binary_message_corpus/generated/*.bin)
  54. set(CORPUS_CMDS)
  55. SET(CORPUS_CMDS "")
  56. FOREACH(f ${CORPUS_FILES})
  57. LIST(APPEND CORPUS_CMDS COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message "${f}")
  58. ENDFOREACH(f)
  59. add_custom_target(
  60. run_fuzzer
  61. ${CORPUS_CMDS}
  62. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  63. DEPENDS ${FUZZER_TARGETS}
  64. ${MAYBE_USES_TERMINAL})