CMakeLists.txt 4.5 KB

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