Skip to content

Commit

Permalink
Refactor kernel triggering in fuzz tests
Browse files Browse the repository at this point in the history
- Properly set up and trigger the test kernel within the fuzz test.
- Change the test kernel used from "bar" to "fill".
- Refactor env vars used in test.
  • Loading branch information
PatKamin authored and kbenzie committed Jul 3, 2024
1 parent e802057 commit 03db30f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions test/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Intel Corporation
# Copyright (C) 2023-2024 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
Expand Down Expand Up @@ -38,7 +38,7 @@ function(add_fuzz_test name label)
ENVIRONMENT "${ENV_VARS}")
# TODO: Should we check if this sanitizer flag is available?
target_compile_options(${TEST_TARGET_NAME} PRIVATE -g -fsanitize=fuzzer)
target_compile_definitions(${TEST_TARGET_NAME} PRIVATE -DKERNEL_IL_PATH="${UR_CONFORMANCE_DEVICE_BINARIES_DIR}/bar/sycl_spir641.spv")
target_compile_definitions(${TEST_TARGET_NAME} PRIVATE -DKERNEL_IL_PATH="${UR_CONFORMANCE_DEVICE_BINARIES_DIR}/fill/spir64.bin.0")
target_include_directories(${TEST_TARGET_NAME} PRIVATE ${UR_CONFORMANCE_DEVICE_BINARIES_DIR})

add_dependencies(${TEST_TARGET_NAME} generate_device_binaries)
Expand Down
40 changes: 29 additions & 11 deletions test/fuzz/urFuzz.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 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
Expand Down Expand Up @@ -353,36 +353,54 @@ int ur_program_create_with_il(TestState &state) {
}

std::vector<char> il_bin;
ur_program_handle_t program = nullptr;
ur_kernel_handle_t kernel = nullptr;
ur_queue_handle_t queue = nullptr;
ur_event_handle_t event = nullptr;
ur_program_handle_t program;
ur_kernel_handle_t kernel;
ur_queue_handle_t queue;
ur_event_handle_t event;
auto &context = state.contexts[state.context_num]->handle;
auto &device = state.devices[state.device_num];
// TODO: Use some generic utility to retrieve/use kernels
std::string kernel_name =
uur::device_binaries::program_kernel_map["bar"][0];
uur::device_binaries::program_kernel_map["fill"][0];

il_bin = state.load_kernel_source();
if (il_bin.empty()) {
return -1;
}

constexpr int vec_size = 64;
std::vector<int> vec(vec_size, 0);

urProgramCreateWithIL(context, il_bin.data(), il_bin.size(), nullptr,
&program);
urProgramBuild(context, program, nullptr);

ur_mem_handle_t memory_buffer;
urMemBufferCreate(context, UR_MEM_FLAG_READ_WRITE, vec_size * sizeof(int),
nullptr, &memory_buffer);
urKernelCreate(program, kernel_name.data(), &kernel);
urKernelSetArgMemObj(kernel, 0, nullptr, memory_buffer);

urQueueCreate(context, device, nullptr, &queue);

const uint32_t nDim = 3;
const size_t gWorkOffset[] = {0, 0, 0};
const size_t gWorkSize[] = {128, 128, 128};
urEnqueueMemBufferWrite(queue, memory_buffer, true, 0,
vec_size * sizeof(int), vec.data(), 0, nullptr,
&event);
urEventWait(1, &event);
urEventRelease(event);

urEnqueueKernelLaunch(queue, kernel, nDim, gWorkOffset, gWorkSize, nullptr,
0, nullptr, &event);
constexpr uint32_t nDim = 3;
const size_t gWorkOffset[] = {0, 0, 0};
const size_t gWorkSize[] = {vec_size * 4, 1, 1};
const size_t lWorkSize[] = {1, 1, 1};

urEnqueueKernelLaunch(queue, kernel, nDim, gWorkOffset, gWorkSize,
lWorkSize, 0, nullptr, &event);
urEventWait(1, &event);
urEventRelease(event);

urQueueFinish(queue);
urMemRelease(memory_buffer);
urQueueRelease(queue);
urKernelRelease(kernel);
urProgramRelease(program);
Expand Down
8 changes: 6 additions & 2 deletions test/fuzz/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 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
Expand Down Expand Up @@ -105,7 +105,11 @@ struct TestState {
uint8_t context_num;

TestState(std::unique_ptr<FuzzedDataProvider> data_provider)
: data_provider(std::move(data_provider)) {}
: data_provider(std::move(data_provider)) {
num_adapters = 0;
num_platforms = 0;
num_devices = 0;
}

template <typename IntType> int get_next_input_data(IntType *data) {
if (data_provider->remaining_bytes() < sizeof(IntType)) {
Expand Down

0 comments on commit 03db30f

Please sign in to comment.