Skip to content

Commit

Permalink
Test parts of API with libFuzzer.
Browse files Browse the repository at this point in the history
  • Loading branch information
PatKamin committed Aug 10, 2023
1 parent fb05463 commit f960b6d
Show file tree
Hide file tree
Showing 8 changed files with 710 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
build_type: Release
compiler: {c: clang, cxx: clang++}
libbacktrace: '-DVAL_USE_LIBBACKTRACE_BACKTRACE=OFF'
fuzztest: ON
- os: 'ubuntu-22.04'
build_type: Release
compiler: {c: gcc, cxx: g++}
Expand All @@ -25,6 +26,7 @@ jobs:
build_type: Release
compiler: {c: clang, cxx: clang++}
libbacktrace: '-DVAL_USE_LIBBACKTRACE_BACKTRACE=ON'
fuzztest: ON
- os: 'ubuntu-20.04'
build_type: Release
compiler: {c: gcc-7, cxx: g++-7}
Expand Down Expand Up @@ -88,6 +90,11 @@ jobs:
working-directory: ${{github.workspace}}/build
run: ctest -C ${{matrix.build_type}} --output-on-failure -L "python|umf|loader|validation|tracing|unit|urtrace"

- name: Fuzz test
working-directory: ${{github.workspace}}/build
if: matrix.fuzztest == 'ON'
run: ctest -C ${{matrix.build_type}} --output-on-failure -L "fuzz"

adapter-build:
name: Build - Adapters on Ubuntu
strategy:
Expand Down
63 changes: 63 additions & 0 deletions source/adapters/null/ur_null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,68 @@ context_t::context_t() {
}
return UR_RESULT_SUCCESS;
};

//////////////////////////////////////////////////////////////////////////
urDdiTable.USM.pfnHostAlloc =
[](ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
ur_usm_pool_handle_t pool, size_t size, void **ppMem) {
if (size == 0) {
*ppMem = nullptr;
return UR_RESULT_ERROR_UNSUPPORTED_SIZE;
}
*ppMem = malloc(size);
if (ppMem == nullptr) {
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return UR_RESULT_SUCCESS;
};

//////////////////////////////////////////////////////////////////////////
urDdiTable.USM.pfnDeviceAlloc =
[](ur_context_handle_t hContext, ur_device_handle_t hDevice,
const ur_usm_desc_t *pUSMDesc, ur_usm_pool_handle_t pool,
size_t size, void **ppMem) {
if (size == 0) {
*ppMem = nullptr;
return UR_RESULT_ERROR_UNSUPPORTED_SIZE;
}
*ppMem = malloc(size);
if (ppMem == nullptr) {
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return UR_RESULT_SUCCESS;
};

//////////////////////////////////////////////////////////////////////////
urDdiTable.USM.pfnFree = [](ur_context_handle_t hContext, void *pMem) {
free(pMem);
return UR_RESULT_SUCCESS;
};

//////////////////////////////////////////////////////////////////////////
urDdiTable.USM.pfnGetMemAllocInfo =
[](ur_context_handle_t hContext, const void *pMem,
ur_usm_alloc_info_t propName, size_t propSize, void *pPropValue,
size_t *pPropSizeRet) {
switch (propName) {
case UR_USM_ALLOC_INFO_TYPE:
*reinterpret_cast<ur_usm_type_t *>(pPropValue) =
pMem ? UR_USM_TYPE_DEVICE : UR_USM_TYPE_UNKNOWN;
if (pPropSizeRet != nullptr) {
*pPropSizeRet = sizeof(ur_usm_type_t);
}
break;
case UR_USM_ALLOC_INFO_SIZE:
*reinterpret_cast<size_t *>(pPropValue) = pMem ? SIZE_MAX : 0;
if (pPropSizeRet != nullptr) {
*pPropSizeRet = sizeof(size_t);
}
break;
default:
pPropValue = nullptr;
break;
}
return UR_RESULT_SUCCESS;
};
}
} // namespace driver
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ add_subdirectory(unit)
if(UR_BUILD_TOOLS)
add_subdirectory(tools)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_subdirectory(fuzz)
endif()
2 changes: 1 addition & 1 deletion test/conformance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if(DEFINED UR_DPCXX)
add_custom_target(generate_device_binaries)

set(UR_CONFORMANCE_DEVICE_BINARIES_DIR
"${CMAKE_CURRENT_BINARY_DIR}/device_binaries/")
"${CMAKE_CURRENT_BINARY_DIR}/device_binaries" CACHE INTERNAL UR_CONFORMANCE_DEVICE_BINARIES_DIR)
file(MAKE_DIRECTORY ${UR_CONFORMANCE_DEVICE_BINARIES_DIR})

if(DEFINED UR_CONFORMANCE_TARGET_TRIPLES)
Expand Down
7 changes: 5 additions & 2 deletions test/conformance/source/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ std::string KernelsEnvironment::getSupportedILPostfix(uint32_t device_index) {

return IL.str();
}

#include <iostream>
std::string
KernelsEnvironment::getKernelSourcePath(const std::string &kernel_name,
uint32_t device_index) {
std::stringstream path;
path << kernel_options.kernel_directory << "/" << kernel_name;
std::cout << "path=" << path.str() << std::endl;
std::string il_postfix = getSupportedILPostfix(device_index);
std::cout << "il_postfix=" << il_postfix << std::endl;

if (il_postfix.empty()) {
return {};
Expand All @@ -300,6 +302,8 @@ KernelsEnvironment::getKernelSourcePath(const std::string &kernel_name,
std::string binary_name;
for (const auto &entry : filesystem::directory_iterator(path.str())) {
auto file_name = entry.path().filename().string();
std::cout << "file_name=" << file_name << std::endl;

if (file_name.find(il_postfix) != std::string::npos) {
binary_name = file_name;
break;
Expand All @@ -322,7 +326,6 @@ void KernelsEnvironment::LoadSource(
std::shared_ptr<std::vector<char>> &binary_out) {
std::string source_path =
instance->getKernelSourcePath(kernel_name, device_index);

if (source_path.empty()) {
FAIL() << error;
}
Expand Down
35 changes: 35 additions & 0 deletions test/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (C) 2023 Intel Corporation
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

function(add_fuzz_test name)
set(TEST_TARGET_NAME fuzztest-${name})
add_ur_executable(${TEST_TARGET_NAME}
${ARGN})
target_link_libraries(${TEST_TARGET_NAME}
PRIVATE
${PROJECT_NAME}::loader
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
-fsanitize=fuzzer -fprofile-instr-generate -fcoverage-mapping)
add_test(NAME ${TEST_TARGET_NAME}
COMMAND ${TEST_TARGET_NAME} -max_total_time=600 -seed=1 -shrink=1 -verbosity=1
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_tests_properties(${TEST_TARGET_NAME} PROPERTIES
LABELS "fuzz"
ENVIRONMENT
"XPTI_TRACE_ENABLE=1"
"XPTI_FRAMEWORK_DISPATCHER=$<TARGET_FILE:xptifw>"
"XPTI_SUBSCRIBERS=$<TARGET_FILE:collector>"
"UR_ENABLE_LAYERS=UR_LAYER_TRACING"
"UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_null>\"")
target_compile_options(${TEST_TARGET_NAME} PRIVATE -g -fsanitize=fuzzer -fprofile-instr-generate -fcoverage-mapping)
target_compile_definitions(${TEST_TARGET_NAME} PRIVATE -DKERNEL_IL_PATH="${UR_CONFORMANCE_DEVICE_BINARIES_DIR}/bar/sycl_spir641.spv")
target_include_directories(${TEST_TARGET_NAME} PRIVATE ${UR_CONFORMANCE_DEVICE_BINARIES_DIR})

add_dependencies(${TEST_TARGET_NAME} generate_device_binaries)
endfunction()

add_fuzz_test(base
urFuzz.cpp)
Loading

0 comments on commit f960b6d

Please sign in to comment.