CMakeLists.txt 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_pki_certificate.c
  50. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c)
  51. add_library(open62541-fuzzplugins OBJECT ${fuzzing_plugin_sources})
  52. add_dependencies(open62541-fuzzplugins open62541)
  53. # the fuzzer test are built directly on the open62541 object files. so they can
  54. # access symbols that are hidden/not exported to the shared library
  55. set(FUZZER_TARGETS)
  56. macro(add_fuzzer FUZZER_NAME FUZZER_SOURCE)
  57. add_executable(${FUZZER_NAME} $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-fuzzplugins> ${FUZZER_SOURCE} ${ARGN})
  58. target_link_libraries(${FUZZER_NAME} ${LIBS})
  59. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
  60. assign_source_group(${FUZZER_SOURCE})
  61. list(APPEND FUZZER_TARGETS ${FUZZER_NAME})
  62. endmacro()
  63. # Add new fuzzers here
  64. add_fuzzer(fuzz_binary_message fuzz_binary_message.cc)
  65. add_fuzzer(fuzz_binary_decode fuzz_binary_decode.cc)
  66. file(GLOB CORPUS_FILES ${PROJECT_SOURCE_DIR}/tests/fuzz/fuzz_binary_message_corpus/generated/*)
  67. SET(CORPUS_CMDS "")
  68. FOREACH(f ${CORPUS_FILES})
  69. LIST(APPEND CORPUS_CMDS COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message "${f}")
  70. ENDFOREACH(f)
  71. add_custom_target(run_fuzzer ${CORPUS_CMDS}
  72. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  73. DEPENDS ${FUZZER_TARGETS}
  74. ${MAYBE_USES_TERMINAL})