CMakeLists.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_default.c
  52. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_none.c
  53. )
  54. if(UA_ENABLE_ENCRYPTION)
  55. list(APPEND fuzzing_plugin_sources
  56. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/securitypolicy_mbedtls_common.c
  57. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic128rsa15.c
  58. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic256.c
  59. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic256sha256.c)
  60. endif()
  61. add_library(open62541-fuzzplugins OBJECT
  62. ${fuzzing_plugin_sources}
  63. ${PROJECT_SOURCE_DIR}/arch/${UA_ARCHITECTURE}/ua_architecture_functions.c)
  64. add_dependencies(open62541-fuzzplugins open62541)
  65. # the fuzzer test are built directly on the open62541 object files. so they can
  66. # access symbols that are hidden/not exported to the shared library
  67. set(FUZZER_TARGETS)
  68. macro(add_fuzzer FUZZER_NAME FUZZER_SOURCE)
  69. add_executable(${FUZZER_NAME} $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-fuzzplugins> ${FUZZER_SOURCE} ${ARGN})
  70. target_link_libraries(${FUZZER_NAME} ${LIBS})
  71. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
  72. assign_source_group(${FUZZER_SOURCE})
  73. list(APPEND FUZZER_TARGETS ${FUZZER_NAME})
  74. endmacro()
  75. # Add new fuzzers here
  76. add_fuzzer(fuzz_binary_message fuzz_binary_message.cc)
  77. add_fuzzer(fuzz_binary_decode fuzz_binary_decode.cc)
  78. add_fuzzer(fuzz_src_ua_util fuzz_src_ua_util.cc)
  79. if(UA_ENABLE_JSON_ENCODING)
  80. add_fuzzer(fuzz_json_decode fuzz_json_decode.cc)
  81. add_fuzzer(fuzz_json_decode_encode fuzz_json_decode_encode.cc)
  82. endif()
  83. file(GLOB CORPUS_FILES ${PROJECT_SOURCE_DIR}/tests/fuzz/fuzz_binary_message_corpus/generated/*)
  84. SET(CORPUS_CMDS "")
  85. FOREACH(f ${CORPUS_FILES})
  86. LIST(APPEND CORPUS_CMDS COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message "${f}")
  87. ENDFOREACH(f)
  88. add_custom_target(run_fuzzer ${CORPUS_CMDS}
  89. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  90. DEPENDS ${FUZZER_TARGETS}
  91. ${MAYBE_USES_TERMINAL})