44# Check that the options are properly set for
55# the fuzzing project
66if (onnxruntime_FUZZ_ENABLED)
7- message (STATUS "Building dependency protobuf-mutator and libfuzzer" )
8-
9- # set the options used to control the protobuf-mutator build
10- set (PROTOBUF_LIBRARIES ${PROTOBUF_LIB} )
11- set (LIB_PROTO_MUTATOR_TESTING OFF )
12-
13- # include the protobuf-mutator CMakeLists.txt rather than the projects CMakeLists.txt to avoid target clashes
14- # with google test
15- add_subdirectory ("external/libprotobuf-mutator/src" )
16-
17- # add the appropriate include directory and compilation flags
18- # needed by the protobuf-mutator target and the libfuzzer
19- set (PROTOBUF_MUT_INCLUDE_DIRS "external/libprotobuf-mutator" )
20- onnxruntime_add_include_to_target(protobuf-mutator ${PROTOBUF_LIB} )
21- onnxruntime_add_include_to_target(protobuf-mutator-libfuzzer ${PROTOBUF_LIB} )
22- target_include_directories (protobuf-mutator PRIVATE ${INCLUDE_DIRECTORIES} ${PROTOBUF_MUT_INCLUDE_DIRS} )
23- target_include_directories (protobuf-mutator-libfuzzer PRIVATE ${INCLUDE_DIRECTORIES} ${PROTOBUF_MUT_INCLUDE_DIRS} )
7+ message (STATUS "Building dependency protobuf-mutator and libfuzzer" )
8+
9+ # set the options used to control the protobuf-mutator build
10+ set (PROTOBUF_LIBRARIES ${PROTOBUF_LIB} )
11+ set (LIB_PROTO_MUTATOR_TESTING OFF )
12+
13+ # include the protobuf-mutator CMakeLists.txt rather than the projects CMakeLists.txt to avoid target clashes
14+ # with google test
15+ add_subdirectory ("external/libprotobuf-mutator/src" )
16+
17+ # add the appropriate include directory and compilation flags
18+ # needed by the protobuf-mutator target and the libfuzzer
19+ set (PROTOBUF_MUT_INCLUDE_DIRS "external/libprotobuf-mutator" )
20+ onnxruntime_add_include_to_target(protobuf-mutator ${PROTOBUF_LIB} )
21+ onnxruntime_add_include_to_target(protobuf-mutator-libfuzzer ${PROTOBUF_LIB} )
22+ target_include_directories (protobuf-mutator PRIVATE ${INCLUDE_DIRECTORIES} ${PROTOBUF_MUT_INCLUDE_DIRS} )
23+ target_include_directories (protobuf-mutator-libfuzzer PRIVATE ${INCLUDE_DIRECTORIES} ${PROTOBUF_MUT_INCLUDE_DIRS} )
24+
2425 if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
2526 # MSVC-specific compiler options
2627 target_compile_options (protobuf-mutator PRIVATE "/wd4244" "/wd4245" "/wd4267" "/wd4100" "/wd4456" )
@@ -44,42 +45,96 @@ if (onnxruntime_FUZZ_ENABLED)
4445 )
4546 endif ()
4647
47- # add Fuzzing Engine Build Configuration
48- message (STATUS "Building Fuzzing engine" )
48+ # add Fuzzing Engine Build Configuration
49+ message (STATUS "Building Fuzzing engine" )
50+
51+ # set Fuzz root directory
52+ set (SEC_FUZZ_ROOT ${TEST_SRC_DIR} /fuzzing)
53+
54+ # Security fuzzing engine src file reference
55+ set (SEC_FUZ_SRC "${SEC_FUZZ_ROOT} /src/BetaDistribution.cpp"
56+ "${SEC_FUZZ_ROOT} /src/OnnxPrediction.cpp"
57+ "${SEC_FUZZ_ROOT} /src/testlog.cpp"
58+ "${SEC_FUZZ_ROOT} /src/test.cpp" )
59+
60+ # compile the executables
61+ onnxruntime_add_executable(onnxruntime_security_fuzz ${SEC_FUZ_SRC} )
62+
63+ # compile with c++17
64+ target_compile_features (onnxruntime_security_fuzz PUBLIC cxx_std_17)
4965
50- # set Fuzz root directory
51- set (SEC_FUZZ_ROOT ${TEST_SRC_DIR} /fuzzing )
66+ # Security fuzzing engine header file reference
67+ onnxruntime_add_include_to_target(onnxruntime_security_fuzz onnx onnxruntime )
5268
53- # Security fuzzing engine src file reference
54- set (SEC_FUZ_SRC "${SEC_FUZZ_ROOT} /src/BetaDistribution.cpp"
55- "${SEC_FUZZ_ROOT} /src/OnnxPrediction.cpp"
56- "${SEC_FUZZ_ROOT} /src/testlog.cpp"
57- "${SEC_FUZZ_ROOT} /src/test.cpp" )
69+ # Assign all include to one variable
70+ set (SEC_FUZ_INC "${SEC_FUZZ_ROOT} /include" )
71+ set (INCLUDE_FILES ${SEC_FUZ_INC} "$<TARGET_PROPERTY:protobuf-mutator,INCLUDE_DIRECTORIES>" )
5872
59- # compile the executables
60- onnxruntime_add_executable (onnxruntime_security_fuzz ${SEC_FUZ_SRC } )
73+ # add all these include directory to the Fuzzing engine
74+ target_include_directories (onnxruntime_security_fuzz PRIVATE ${INCLUDE_FILES } )
6175
62- # compile with c++17
63- target_compile_features (onnxruntime_security_fuzz PUBLIC cxx_std_17 )
76+ # add link libraries to the project
77+ target_link_libraries (onnxruntime_security_fuzz onnx_proto onnxruntime protobuf-mutator ${PROTOBUF_LIB} )
6478
65- # Security fuzzing engine header file reference
66- onnxruntime_add_include_to_target (onnxruntime_security_fuzz onnx onnxruntime)
79+ # add the dependencies
80+ add_dependencies (onnxruntime_security_fuzz onnx_proto onnxruntime protobuf-mutator ${PROTOBUF_LIB} )
6781
68- # Assign all include to one variable
69- set (SEC_FUZ_INC "${SEC_FUZZ_ROOT} /include" )
70- set (INCLUDE_FILES ${SEC_FUZ_INC} "$<TARGET_PROPERTY:protobuf-mutator,INCLUDE_DIRECTORIES>" )
82+ # copy the shared libraries (DLLs on Windows, SOs on Linux) to the execution directory
83+ add_custom_command (TARGET onnxruntime_security_fuzz POST_BUILD
84+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> $<TARGET_FILE_DIR:onnxruntime_security_fuzz>
85+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${PROTOBUF_LIB} > $<TARGET_FILE_DIR:onnxruntime_security_fuzz>)
7186
72- # add all these include directory to the Fuzzing engine
73- target_include_directories (onnxruntime_security_fuzz PRIVATE ${INCLUDE_FILES} )
87+ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
88+ # Add a second fuzzer that uses libFuzzer in fuzzer/libfuzzer
89+ message (STATUS "Building libProtoBufFuzzer-based fuzzer" )
7490
75- # add link libraries the project
76- target_link_libraries (onnxruntime_security_fuzz onnx_proto onnxruntime protobuf-mutator ${PROTOBUF_LIB} )
91+ # Set source files for the libFuzzer
92+ set (LIBFUZZER_SRC "${SEC_FUZZ_ROOT} /src/OnnxPrediction.cpp"
93+ "${SEC_FUZZ_ROOT} /src/testlog.cpp"
94+ "${SEC_FUZZ_ROOT} /ort_libfuzzer/OrtProtoLibfuzzer.cpp" )
7795
78- # add the dependencies
79- add_dependencies (onnxruntime_security_fuzz onnx_proto onnxruntime protobuf-mutator ${PROTOBUF_LIB} )
96+ # Compile the libFuzzer-based fuzzer
97+ onnxruntime_add_executable(onnxruntime_proto_libfuzzer ${LIBFUZZER_SRC} )
98+ # Security fuzzing engine header file reference
99+ onnxruntime_add_include_to_target(onnxruntime_proto_libfuzzer onnx onnxruntime)
100+ # Set include directories for libFuzzer
101+ target_include_directories (onnxruntime_proto_libfuzzer PRIVATE ${INCLUDE_FILES} )
80102
81- # copy the dlls to the execution directory
82- add_custom_command (TARGET onnxruntime_security_fuzz POST_BUILD
83- COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> $<TARGET_FILE_DIR:onnxruntime_security_fuzz>
84- COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${PROTOBUF_LIB} > $<TARGET_FILE_DIR:onnxruntime_security_fuzz>)
103+ # Add link libraries for libFuzzer
104+ target_link_libraries (onnxruntime_proto_libfuzzer onnx_proto onnxruntime protobuf-mutator protobuf-mutator-libfuzzer -fsanitize=fuzzer,address ${PROTOBUF_LIB} )
105+
106+ # Add the dependencies for libFuzzer
107+ add_dependencies (onnxruntime_proto_libfuzzer onnx_proto onnxruntime protobuf-mutator protobuf-mutator-libfuzzer ${PROTOBUF_LIB} )
108+
109+ # Copy shared libraries for libFuzzer
110+ add_custom_command (TARGET onnxruntime_proto_libfuzzer POST_BUILD
111+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> $<TARGET_FILE_DIR:onnxruntime_proto_libfuzzer>
112+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${PROTOBUF_LIB} > $<TARGET_FILE_DIR:onnxruntime_proto_libfuzzer>)
113+ # Add a second fuzzer that uses libFuzzer in fuzzer/libfuzzer
114+ message (STATUS "Building libBufFuzzer-based fuzzer" )
115+
116+ # Set source files for the libFuzzer
117+ set (LIBFUZZER_SRC "${SEC_FUZZ_ROOT} /src/OnnxPrediction.cpp"
118+ "${SEC_FUZZ_ROOT} /src/testlog.cpp"
119+ "${SEC_FUZZ_ROOT} /ort_libfuzzer/OrtLibfuzzer.cpp" )
120+
121+ # Compile the libFuzzer-based fuzzer
122+ onnxruntime_add_executable(onnxruntime_libfuzzer_fuzz ${LIBFUZZER_SRC} )
123+ # Security fuzzing engine header file reference
124+ onnxruntime_add_include_to_target(onnxruntime_libfuzzer_fuzz onnx onnxruntime)
125+ # Set include directories for libFuzzer
126+ target_compile_definitions (onnxruntime_libfuzzer_fuzz PRIVATE GOOGLE_PROTOBUF_NO_LOGGING=1)
127+ target_include_directories (onnxruntime_libfuzzer_fuzz PRIVATE ${INCLUDE_FILES} )
128+
129+ # Add link libraries for libFuzzer
130+ target_link_libraries (onnxruntime_libfuzzer_fuzz onnx_proto onnxruntime protobuf-mutator protobuf-mutator-libfuzzer -fsanitize=fuzzer,address ${PROTOBUF_LIB} )
131+
132+ # Add the dependencies for libFuzzer
133+ add_dependencies (onnxruntime_libfuzzer_fuzz onnx_proto onnxruntime protobuf-mutator protobuf-mutator-libfuzzer ${PROTOBUF_LIB} )
134+
135+ # Copy shared libraries for libFuzzer
136+ add_custom_command (TARGET onnxruntime_libfuzzer_fuzz POST_BUILD
137+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> $<TARGET_FILE_DIR:onnxruntime_libfuzzer_fuzz>
138+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${PROTOBUF_LIB} > $<TARGET_FILE_DIR:onnxruntime_libfuzzer_fuzz>)
139+ endif ()
85140endif ()
0 commit comments