CMakeLists.txt 4.7 KB

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