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..664eacf5b9c22 --- /dev/null +++ b/sycl/tools/fuzzers/sycl-config-fuzzer/CMakeLists.txt @@ -0,0 +1,53 @@ +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 + $ + $> + ) + + 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 + ${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=600 + 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; +}