CMakeLists.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. add_definitions(-DUA_sleep_ms=UA_comboSleep)
  9. if(NOT MSVC)
  10. add_definitions(-Wno-deprecated-declarations)
  11. endif()
  12. if (UA_BUILD_FUZZING_CORPUS)
  13. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  14. add_definitions(-DUA_CORPUS_OUTPUT_DIR="${PROJECT_BINARY_DIR}/corpus/custom")
  15. file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/corpus/custom)
  16. add_executable(corpus_generator ua_debug_dump_pkgs_file.c corpus_generator.c)
  17. target_link_libraries(corpus_generator open62541 ${open62541_LIBRARIES})
  18. assign_source_group(corpus_generator)
  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/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_ziptree.c
  51. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_hashmap.c
  52. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
  53. ${PROJECT_SOURCE_DIR}/plugins/ua_pki_default.c
  54. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_none.c
  55. )
  56. if(UA_ENABLE_ENCRYPTION)
  57. list(APPEND fuzzing_plugin_sources
  58. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/securitypolicy_mbedtls_common.c
  59. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic128rsa15.c
  60. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic256.c
  61. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic256sha256.c)
  62. endif()
  63. add_library(open62541-fuzzplugins OBJECT
  64. ${fuzzing_plugin_sources}
  65. ${PROJECT_SOURCE_DIR}/arch/${UA_ARCHITECTURE}/ua_architecture_functions.c)
  66. add_dependencies(open62541-fuzzplugins open62541)
  67. # the fuzzer test are built directly on the open62541 object files. so they can
  68. # access symbols that are hidden/not exported to the shared library
  69. set(FUZZER_TARGETS)
  70. macro(add_fuzzer FUZZER_NAME FUZZER_SOURCE)
  71. add_executable(${FUZZER_NAME} $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-fuzzplugins> ${FUZZER_SOURCE} ${ARGN})
  72. target_link_libraries(${FUZZER_NAME} ${LIBS})
  73. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
  74. target_include_directories(${FUZZER_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/deps/mdnsd)
  75. assign_source_group(${FUZZER_SOURCE})
  76. list(APPEND FUZZER_TARGETS ${FUZZER_NAME})
  77. endmacro()
  78. # Add new fuzzers here
  79. add_fuzzer(fuzz_binary_message fuzz_binary_message.cc)
  80. add_fuzzer(fuzz_tcp_message fuzz_tcp_message.cc)
  81. add_fuzzer(fuzz_binary_decode fuzz_binary_decode.cc)
  82. add_fuzzer(fuzz_src_ua_util fuzz_src_ua_util.cc)
  83. # Add fuzzer for mdns dependency. It's currently not fuzzed separately.
  84. # See also: https://github.com/google/oss-fuzz/pull/2928
  85. add_fuzzer(fuzz_mdns_message ${PROJECT_SOURCE_DIR}/deps/mdnsd/tests/fuzz/fuzz_mdns_message.cc)
  86. add_fuzzer(fuzz_mdns_xht ${PROJECT_SOURCE_DIR}/deps/mdnsd/tests/fuzz/fuzz_mdns_xht.cc)
  87. add_fuzzer(fuzz_base64_encode fuzz_base64_encode.cc)
  88. add_fuzzer(fuzz_base64_decode fuzz_base64_decode.cc)
  89. if(UA_ENABLE_JSON_ENCODING)
  90. add_fuzzer(fuzz_json_decode fuzz_json_decode.cc)
  91. add_fuzzer(fuzz_json_decode_encode fuzz_json_decode_encode.cc)
  92. endif()
  93. # Run fuzzer on existing corpus to avoid regression
  94. file(GLOB CORPUS_FILES ${PROJECT_SOURCE_DIR}/tests/fuzz/fuzz_binary_message_corpus/generated/*)
  95. SET(CORPUS_CMDS "")
  96. FOREACH(f ${CORPUS_FILES})
  97. LIST(APPEND CORPUS_CMDS COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_binary_message "${f}")
  98. ENDFOREACH(f)
  99. file(GLOB CORPUS_FILES ${PROJECT_SOURCE_DIR}/deps/mdnsd/tests/fuzz/fuzz_mdns_message_corpus/*)
  100. FOREACH(f ${CORPUS_FILES})
  101. LIST(APPEND CORPUS_CMDS COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/fuzz_mdns_message "${f}")
  102. ENDFOREACH(f)
  103. add_custom_target(run_fuzzer ${CORPUS_CMDS}
  104. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  105. DEPENDS ${FUZZER_TARGETS}
  106. ${MAYBE_USES_TERMINAL})