CMakeLists.txt 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if (UA_BUILD_FUZZING_CORPUS)
  9. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  10. add_definitions(-DUA_CORPUS_OUTPUT_DIR="${PROJECT_BINARY_DIR}/corpus/custom")
  11. file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/corpus/custom)
  12. add_executable(corpus_generator ua_debug_dump_pkgs_file.c corpus_generator.c)
  13. target_link_libraries(corpus_generator open62541 ${open62541_LIBRARIES})
  14. assign_source_group(corpus_generator)
  15. add_dependencies(corpus_generator open62541-amalgamation-header)
  16. set_target_properties(corpus_generator PROPERTIES FOLDER "open62541/corpus_generator")
  17. set_target_properties(corpus_generator PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
  18. return()
  19. endif()
  20. remove_definitions(-std=c99 -Wmissing-prototypes -Wstrict-prototypes)
  21. set (CMAKE_CXX_STANDARD 11)
  22. if (NOT UA_BUILD_OSS_FUZZ)
  23. if(NOT "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang")
  24. MESSAGE(FATAL_ERROR "To build fuzzing, you need to use Clang as the compiler")
  25. endif()
  26. # oss-fuzz builds already include these flags
  27. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
  28. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fsanitize=address")
  29. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -fsanitize=address")
  30. else()
  31. 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")
  32. 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")
  33. endif()
  34. set(LIBS Fuzzer)
  35. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  36. else()
  37. set(LIBS $ENV{LIB_FUZZING_ENGINE})
  38. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $ENV{OUT})
  39. endif()
  40. list(APPEND LIBS "${open62541_LIBRARIES}")
  41. # Use different plugins for testing
  42. set(fuzzing_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_tcp.c
  43. ${PROJECT_SOURCE_DIR}/tests/testing-plugins/testing_clock.c
  44. ${PROJECT_SOURCE_DIR}/tests/testing-plugins/testing_networklayers.c
  45. ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.c
  46. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.c
  47. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.c
  48. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
  49. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c)
  50. add_library(open62541-fuzzplugins OBJECT ${fuzzing_plugin_sources})
  51. add_dependencies(open62541-fuzzplugins open62541)
  52. # the fuzzer test are built directly on the open62541 object files. so they can
  53. # access symbols that are hidden/not exported to the shared library
  54. set(FUZZER_TARGETS)
  55. macro(add_fuzzer FUZZER_NAME FUZZER_SOURCE)
  56. add_executable(${FUZZER_NAME} $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-fuzzplugins> ${FUZZER_SOURCE} ${ARGN})
  57. target_link_libraries(${FUZZER_NAME} ${LIBS})
  58. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
  59. assign_source_group(${FUZZER_SOURCE})
  60. list(APPEND FUZZER_TARGETS ${FUZZER_NAME})
  61. endmacro()
  62. # Add new fuzzers here
  63. add_fuzzer(fuzz_binary_message fuzz_binary_message.cc)
  64. add_fuzzer(fuzz_binary_decode fuzz_binary_decode.cc)
  65. file(GLOB CORPUS_FILES ${PROJECT_SOURCE_DIR}/tests/fuzz/fuzz_binary_message_corpus/generated/*)
  66. SET(CORPUS_CMDS "")
  67. FOREACH(f ${CORPUS_FILES})
  68. LIST(APPEND CORPUS_CMDS COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message "${f}")
  69. ENDFOREACH(f)
  70. add_custom_target(run_fuzzer ${CORPUS_CMDS}
  71. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  72. DEPENDS ${FUZZER_TARGETS}
  73. ${MAYBE_USES_TERMINAL})