From 2df6d4455d32bc0f9012e57fd088523fef49b29b Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Mon, 9 Dec 2024 09:05:46 -0800 Subject: [PATCH 1/2] [SYCL][Test] Introduce fuzzing for SYCL config file --- sycl/tools/CMakeLists.txt | 1 + sycl/tools/fuzzers/CMakeLists.txt | 1 + .../fuzzers/sycl-config-fuzzer/CMakeLists.txt | 40 ++++++++++++ .../sycl-config-fuzzer/corpus/all-options.txt | 35 +++++++++++ .../sycl-config-fuzzer/corpus/clrf.txt | 2 + .../sycl-config-fuzzer/corpus/comments.txt | 4 ++ .../corpus/duplicated-options.txt | 2 + .../corpus/multiple-options.txt | 2 + .../corpus/single-option.txt | 1 + .../corpus/unknown-option.txt | 1 + .../fuzzers/sycl-config-fuzzer/empty.txt | 0 .../tools/fuzzers/sycl-config-fuzzer/main.cpp | 63 +++++++++++++++++++ 12 files changed, 152 insertions(+) create mode 100644 sycl/tools/fuzzers/CMakeLists.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/all-options.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/clrf.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/comments.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/duplicated-options.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/multiple-options.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/single-option.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/corpus/unknown-option.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/empty.txt create mode 100644 sycl/tools/fuzzers/sycl-config-fuzzer/main.cpp diff --git a/sycl/tools/CMakeLists.txt b/sycl/tools/CMakeLists.txt index ed11e98b1f9c8..463a373c5b319 100644 --- a/sycl/tools/CMakeLists.txt +++ b/sycl/tools/CMakeLists.txt @@ -15,3 +15,4 @@ if (SYCL_ENABLE_XPTI_TRACING) endif() endif() +add_subdirectory(fuzzers) diff --git a/sycl/tools/fuzzers/CMakeLists.txt b/sycl/tools/fuzzers/CMakeLists.txt new file mode 100644 index 0000000000000..6cd2da2cacbbe --- /dev/null +++ b/sycl/tools/fuzzers/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(sycl-config-fuzzer) diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt new file mode 100644 index 0000000000000..1fe2b29672c38 --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt @@ -0,0 +1,40 @@ +#set(LLVM_LINK_COMPONENTS + # Support # For dealing with temporary files + #) + +message(WARNING ${LLVM_LIB_FUZZING_ENGINE}) + +add_executable(sycl-config-fuzzer + EXCLUDE_FROM_ALL + main.cpp +) + +if (TARGET sycl-config-fuzzer) + target_link_libraries(sycl-config-fuzzer + PRIVATE + ${LLVM_LIB_FUZZING_ENGINE} + # We link the whole SYCL RT at object level + $ + $ + ) + set_target_properties(sycl-config-fuzzer PROPERTIES FOLDER "SYCL/Fuzzers") + target_include_directories(sycl-config-fuzzer + PRIVATE SYSTEM + ${SYCL_SOURCE_DIR}/source/ + ) + + set(CORPUS_BINARY_DIR ${CMAKE_BINARY_DIR}/fuzzer-artifacts/sycl-config-corpus) + + add_custom_command(TARGET sycl-config-fuzzer PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/fuzzer-artifacts/sycl-config-artifacts + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/corpus ${CORPUS_BINARY_DIR} + ) + + add_custom_target(fuzz-sycl-config + COMMENT "Running the SYCL config fuzzer for a minute..." + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/fuzzer-artifacts/sycl-config-artifacts + COMMAND $ ${CORPUS_BINARY_DIR} -artifact_prefix=config- -max_total_time=60 + USES_TERMINAL + ) + set_target_properties(fuzz-sycl-config PROPERTIES FOLDER "SYCL/Fuzzers") +endif() diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/all-options.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/all-options.txt new file mode 100644 index 0000000000000..5cc050805e983 --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/all-options.txt @@ -0,0 +1,35 @@ +SYCL_PRINT_EXECUTION_GRAPH=1 +SYCL_DISABLE_EXECUTION_GRAPH_CLEANUP=0 +SYCL_DISABLE_POST_ENQUEUE_CLEANUP=1 +SYCL_DEVICE_ALLOWLIST=a;b;c +SYCL_UR_TRACE=-1 +SYCL_PARALLEL_FOR_RANGE_ROUNDING_TRACE=1 +SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING=0 +SYCL_PARALLEL_FOR_RANGE_ROUNDING_PARAMS=32;4;4 +SYCL_DEVICELIB_NO_FALLBACK=1 +SYCL_PROGRAM_LINK_OPTIONS="-ze-opt-disable" +SYCL_PROGRAM_COMPILE_OPTIONS="-ze-opt-level=2" +SYCL_PROGRAM_APPEND_LINK_OPTIONS=2 +SYCL_PROGRAM_APPEND_COMPILE_OPTIONS=1 +SYCL_HOST_UNIFIED_MEMORY=1 +SYCL_CACHE_DIR="C:/path/to/some/dir with spaces" +SYCL_CACHE_TRACE=1 +SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD=32 +SYCL_CACHE_DISABLE_PERSISTENT=1 +SYCL_CACHE_PERSISTENT=1 +SYCL_CACHE_EVICTION_DISABLE=0 +SYCL_CACHE_MAX_SIZE=-1 +SYCL_CACHE_THRESHOLD=64 +SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE=32 +SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE=128 +INTEL_ENABLE_OFFLOAD_ANNOTATIONS=1 +SYCL_ENABLE_DEFAULT_CONTEXTS=0 +SYCL_QUEUE_THREAD_POOL_SIZE=16 +SYCL_RT_WARNING_LEVEL=4 +SYCL_REDUCTION_PREFERRED_WORKGROUP_SIZE=256 +ONEAPI_DEVICE_SELECTOR="*:gpu;opencl:gpu" +SYCL_ENABLE_FUSION_CACHING=0 +SYCL_CACHE_IN_MEM=1 +SYCL_JIT_AMDGCN_PTX_KERNELS=1 +SYCL_JIT_AMDGCN_PTX_TARGET_CPU=1 +SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES="+A,-B" diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/clrf.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/clrf.txt new file mode 100644 index 0000000000000..f5cfea6ec45e3 --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/clrf.txt @@ -0,0 +1,2 @@ +SYCL_CACHE_DIR=c:/path/to/dir +SYCL_QUEUE_THREAD_POOL_SIZE=23 diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/comments.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/comments.txt new file mode 100644 index 0000000000000..c6bd7fc7902cc --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/comments.txt @@ -0,0 +1,4 @@ +# Comment +OPTION=value # comment +#comment +OPTION2=value #comment diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/duplicated-options.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/duplicated-options.txt new file mode 100644 index 0000000000000..d3cd59bf0c39a --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/duplicated-options.txt @@ -0,0 +1,2 @@ +SYCL_UR_TRACE=1 +SYCL_UR_TRACE=-1 diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/multiple-options.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/multiple-options.txt new file mode 100644 index 0000000000000..5134df97c10da --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/multiple-options.txt @@ -0,0 +1,2 @@ +SYCL_UR_TRACE=-1 +ONEAPI_DEVICE_SELECTOR=opencl:gpu diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/single-option.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/single-option.txt new file mode 100644 index 0000000000000..40b58fd82c7fc --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/single-option.txt @@ -0,0 +1 @@ +SYCL_CACHE_IN_MEM=1 diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/unknown-option.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/unknown-option.txt new file mode 100644 index 0000000000000..a573b592aaa8c --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/corpus/unknown-option.txt @@ -0,0 +1 @@ +UNKNOWN_OPTION=some-random-value diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/empty.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/empty.txt new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/main.cpp b/sycl/tools/fuzzers/sycl-config-fuzzer/main.cpp new file mode 100644 index 0000000000000..fba51d2828b12 --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/main.cpp @@ -0,0 +1,63 @@ +//===------- main.cpp - Fuzz SYCL config file parsing ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include + +#include +#include +#include +#include + +bool contains(const std::string &haystack, const std::string &needle) { + return haystack.find(needle) != std::string::npos; +} + +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { + // Create (or re-create) a temporary file which will store fuzzed config and + // write fuzzed data into it. + { + std::fstream file("sycl-config-temp.txt", std::ios::out | std::ios::binary); + if (!file.is_open()) { + std::cerr << "Failed to create sycl-config-temp.txt!" << std::endl; + return 1; + } + + try { + file.write(reinterpret_cast(data), size); + } catch (std::exception &e) { + std::cerr + << "Error happened during writing data into sycl-config-temp.txt: " + << e.what() << std::endl; + return 2; + } + + if (file.bad() || file.fail()) { + std::cerr << "Error happened during writing data into sycl-config-temp.txt!" + << std::endl; + return 3; + } + } + + // Make sure that the right config file will be read + setenv("SYCL_CONFIG_FILE_NAME", "sycl-config-temp.txt", 1); + + try { + sycl::detail::readConfig(/* ForceInitialization = */ true); + } catch (sycl::exception &e) { + // There are format requirements for the config file. To make sure that no + // false positives are emitted if randomly-generated input looks _like_ a + // valid config, but violate format requirements, we ignore certain + // exceptions here. + if (!contains(e.what(), "SPACE found at the beginning/end") && + !contains(e.what(), "The value contains more than") && + !contains(e.what(), "Variable name is more than")) + throw e; + } + + return 0; +} From dfad129459247b152d6b81df5e025d9c4c2b8483 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Wed, 16 Apr 2025 02:46:42 -0700 Subject: [PATCH 2/2] Tweaks --- .../fuzzers/sycl-config-fuzzer/CMakeLists.txt | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt b/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt index 1fe2b29672c38..664eacf5b9c22 100644 --- a/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt @@ -1,22 +1,35 @@ -#set(LLVM_LINK_COMPONENTS - # Support # For dealing with temporary files - #) - -message(WARNING ${LLVM_LIB_FUZZING_ENGINE}) - add_executable(sycl-config-fuzzer EXCLUDE_FROM_ALL main.cpp + # ${SYCL_SOURCE_DIR}/source/detail/config.cpp + # ${SYCL_SOURCE_DIR}/source/exception.cpp + # ${SYCL_SOURCE_DIR}/source/detail/os_util.cpp ) if (TARGET sycl-config-fuzzer) + target_compile_options( + sycl-config-fuzzer + PRIVATE + -fsanitize=fuzzer + #-fsanitize=address + -fprofile-instr-generate -fcoverage-mapping + -g + ) + target_link_libraries(sycl-config-fuzzer PRIVATE - ${LLVM_LIB_FUZZING_ENGINE} - # We link the whole SYCL RT at object level $ - $ + $> ) + + target_link_options( + sycl-config-fuzzer + PRIVATE + -fsanitize=fuzzer + -fprofile-instr-generate -fcoverage-mapping + #-fsanitize=address + ) + set_target_properties(sycl-config-fuzzer PROPERTIES FOLDER "SYCL/Fuzzers") target_include_directories(sycl-config-fuzzer PRIVATE SYSTEM @@ -33,7 +46,7 @@ if (TARGET sycl-config-fuzzer) add_custom_target(fuzz-sycl-config COMMENT "Running the SYCL config fuzzer for a minute..." WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/fuzzer-artifacts/sycl-config-artifacts - COMMAND $ ${CORPUS_BINARY_DIR} -artifact_prefix=config- -max_total_time=60 + COMMAND $ ${CORPUS_BINARY_DIR} -artifact_prefix=config- -max_total_time=600 USES_TERMINAL ) set_target_properties(fuzz-sycl-config PROPERTIES FOLDER "SYCL/Fuzzers")