CMakeLists.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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)
  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 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -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 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -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_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. add_library(open62541-fuzzplugins OBJECT ${fuzzing_plugin_sources})
  38. add_dependencies(open62541-fuzzplugins open62541)
  39. # the fuzzer test are built directly on the open62541 object files. so they can
  40. # access symbols that are hidden/not exported to the shared library
  41. set(FUZZER_TARGETS)
  42. macro(add_fuzzer FUZZER_NAME FUZZER_SOURCE)
  43. add_executable(${FUZZER_NAME} $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-fuzzplugins> ${FUZZER_SOURCE} ${ARGN})
  44. target_link_libraries(${FUZZER_NAME} ${LIBS})
  45. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
  46. assign_source_group(${FUZZER_SOURCE})
  47. list(APPEND FUZZER_TARGETS ${FUZZER_NAME})
  48. endmacro()
  49. # Add new fuzzers here
  50. add_fuzzer(fuzz_binary_message fuzz_binary_message.cc)
  51. add_fuzzer(fuzz_binary_decode fuzz_binary_decode.cc)
  52. add_custom_target(
  53. run_fuzzer
  54. COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message ${CMAKE_CURRENT_SOURCE_DIR}/fuzz_binary_message_corpus/client_Browse.bin
  55. COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message ${CMAKE_CURRENT_SOURCE_DIR}/fuzz_binary_message_corpus/client_CLO.bin
  56. COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message ${CMAKE_CURRENT_SOURCE_DIR}/fuzz_binary_message_corpus/client_CreateActivateSession.bin
  57. COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message ${CMAKE_CURRENT_SOURCE_DIR}/fuzz_binary_message_corpus/client_HELOPN.bin
  58. COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message ${CMAKE_CURRENT_SOURCE_DIR}/fuzz_binary_message_corpus/client_Read.bin
  59. COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message ${CMAKE_CURRENT_SOURCE_DIR}/fuzz_binary_message_corpus/client_Write.bin
  60. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  61. DEPENDS ${FUZZER_TARGETS}
  62. ${MAYBE_USES_TERMINAL})