Skip to content

Commit

Permalink
Merge branch 'main' into user-after-free
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZyne committed Apr 7, 2024
2 parents a8ae97e + befdc7c commit def6bd0
Show file tree
Hide file tree
Showing 24 changed files with 830 additions and 144 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/bandit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ permissions:
contents: read

jobs:
linux:
bandit:
name: Bandit
runs-on: ubuntu-latest

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{matrix.os}}

steps:
- name: Clone the git repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Install pip packages
run: pip install -r third_party/requirements.txt

# Scan is run only for the 'tools' folder.
# Scan all files, except for dev. scripts
- name: Run Bandit
run: |
bandit -r tools
run: bandit -r . -x ./scripts/
2 changes: 1 addition & 1 deletion scripts/verify_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def verify_file_has_license(file):
with open(file, 'r') as in_file:
contents = in_file.read(300)
contents = in_file.read(400)
if "SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception" not in contents:
raise Exception(f"{file} does not contain a license!")

Expand Down
6 changes: 5 additions & 1 deletion source/adapters/level_zero/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(
}
}
}
return ReturnValue(std::min(GlobalMemSize, FreeMemory));
if (MemCount > 0) {
return ReturnValue(std::min(GlobalMemSize, FreeMemory));
} else {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
}
case UR_DEVICE_INFO_MEMORY_CLOCK_RATE: {
// If there are not any memory modules then return 0.
Expand Down
2 changes: 2 additions & 0 deletions source/adapters/opencl/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
//===----------------------------------------------------------------------===//

#include "common.hpp"
#include "logger/ur_logger.hpp"

struct ur_adapter_handle_t_ {
std::atomic<uint32_t> RefCount = 0;
std::mutex Mutex;
logger::Logger &log = logger::get_logger("opencl");
};

static ur_adapter_handle_t_ *adapter = nullptr;
Expand Down
80 changes: 27 additions & 53 deletions source/adapters/opencl/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hContext);
cl_ext::clCreateCommandBufferKHR_fn clCreateCommandBufferKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCreateCommandBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCreateCommandBufferKHRCache,
cl_ext::CreateCommandBufferName, &clCreateCommandBufferKHR);

if (!clCreateCommandBufferKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::CreateCommandBufferName, &clCreateCommandBufferKHR));

cl_int Res = CL_SUCCESS;
auto CLCommandBuffer = clCreateCommandBufferKHR(
1, cl_adapter::cast<cl_command_queue *>(&Queue), nullptr, &Res);
CL_RETURN_ON_FAILURE_AND_SET_NULL(Res, phCommandBuffer);
Expand All @@ -51,12 +49,10 @@ urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) {

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clRetainCommandBufferKHR_fn clRetainCommandBuffer = nullptr;
cl_int Res = cl_ext::getExtFuncFromContext<decltype(clRetainCommandBuffer)>(
CLContext, cl_ext::ExtFuncPtrCache->clRetainCommandBufferKHRCache,
cl_ext::RetainCommandBufferName, &clRetainCommandBuffer);

if (!clRetainCommandBuffer || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clRetainCommandBuffer)>(
CLContext, cl_ext::ExtFuncPtrCache->clRetainCommandBufferKHRCache,
cl_ext::RetainCommandBufferName, &clRetainCommandBuffer));

CL_RETURN_ON_FAILURE(clRetainCommandBuffer(hCommandBuffer->CLCommandBuffer));
return UR_RESULT_SUCCESS;
Expand All @@ -68,13 +64,10 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clReleaseCommandBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clReleaseCommandBufferKHRCache,
cl_ext::ReleaseCommandBufferName, &clReleaseCommandBufferKHR);

if (!clReleaseCommandBufferKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::ReleaseCommandBufferName, &clReleaseCommandBufferKHR));

CL_RETURN_ON_FAILURE(
clReleaseCommandBufferKHR(hCommandBuffer->CLCommandBuffer));
Expand All @@ -85,13 +78,10 @@ UR_APIEXPORT ur_result_t UR_APICALL
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clFinalizeCommandBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clFinalizeCommandBufferKHRCache,
cl_ext::FinalizeCommandBufferName, &clFinalizeCommandBufferKHR);

if (!clFinalizeCommandBufferKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::FinalizeCommandBufferName, &clFinalizeCommandBufferKHR));

CL_RETURN_ON_FAILURE(
clFinalizeCommandBufferKHR(hCommandBuffer->CLCommandBuffer));
Expand All @@ -109,13 +99,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandNDRangeKernelKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandNDRangeKernelKHRCache,
cl_ext::CommandNRRangeKernelName, &clCommandNDRangeKernelKHR);

if (!clCommandNDRangeKernelKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::CommandNRRangeKernelName, &clCommandNDRangeKernelKHR));

CL_RETURN_ON_FAILURE(clCommandNDRangeKernelKHR(
hCommandBuffer->CLCommandBuffer, nullptr, nullptr,
Expand Down Expand Up @@ -157,12 +144,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clCommandCopyBufferKHR_fn clCommandCopyBufferKHR = nullptr;
cl_int Res = cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferKHRCache,
cl_ext::CommandCopyBufferName, &clCommandCopyBufferKHR);

if (!clCommandCopyBufferKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferKHRCache,
cl_ext::CommandCopyBufferName, &clCommandCopyBufferKHR));

CL_RETURN_ON_FAILURE(clCommandCopyBufferKHR(
hCommandBuffer->CLCommandBuffer, nullptr,
Expand Down Expand Up @@ -193,13 +178,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferRectKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferRectKHRCache,
cl_ext::CommandCopyBufferRectName, &clCommandCopyBufferRectKHR);

if (!clCommandCopyBufferRectKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::CommandCopyBufferRectName, &clCommandCopyBufferRectKHR));

CL_RETURN_ON_FAILURE(clCommandCopyBufferRectKHR(
hCommandBuffer->CLCommandBuffer, nullptr,
Expand Down Expand Up @@ -283,12 +265,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferFillExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clCommandFillBufferKHR_fn clCommandFillBufferKHR = nullptr;
cl_int Res = cl_ext::getExtFuncFromContext<decltype(clCommandFillBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandFillBufferKHRCache,
cl_ext::CommandFillBufferName, &clCommandFillBufferKHR);

if (!clCommandFillBufferKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandFillBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandFillBufferKHRCache,
cl_ext::CommandFillBufferName, &clCommandFillBufferKHR));

CL_RETURN_ON_FAILURE(clCommandFillBufferKHR(
hCommandBuffer->CLCommandBuffer, nullptr,
Expand Down Expand Up @@ -339,13 +319,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clEnqueueCommandBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clEnqueueCommandBufferKHRCache,
cl_ext::EnqueueCommandBufferName, &clEnqueueCommandBufferKHR);

if (!clEnqueueCommandBufferKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::EnqueueCommandBufferName, &clEnqueueCommandBufferKHR));

const uint32_t NumberOfQueues = 1;

Expand Down Expand Up @@ -382,13 +359,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp(

cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
cl_ext::clGetCommandBufferInfoKHR_fn clGetCommandBufferInfoKHR = nullptr;
cl_int Res =
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clGetCommandBufferInfoKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clGetCommandBufferInfoKHRCache,
cl_ext::GetCommandBufferInfoName, &clGetCommandBufferInfoKHR);

if (!clGetCommandBufferInfoKHR || Res != CL_SUCCESS)
return UR_RESULT_ERROR_INVALID_OPERATION;
cl_ext::GetCommandBufferInfoName, &clGetCommandBufferInfoKHR));

if (propName != UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT) {
return UR_RESULT_ERROR_INVALID_ENUMERATION;
Expand Down
4 changes: 2 additions & 2 deletions source/adapters/opencl/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//===----------------------------------------------------------------------===//

#include "common.hpp"

#include "logger/ur_logger.hpp"
namespace cl_adapter {

/* Global variables for urPlatformGetLastError() */
Expand Down Expand Up @@ -91,7 +91,7 @@ ur_result_t mapCLErrorToUR(cl_int Result) {
}

void cl_adapter::die(const char *Message) {
std::cerr << "ur_die: " << Message << "\n";
logger::always("ur_die: {}", Message);
std::terminate();
}

Expand Down
6 changes: 3 additions & 3 deletions source/adapters/opencl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ static ur_result_t getExtFuncFromContext(cl_context Context,
if (It != FPtrMap.end()) {
auto F = It->second;
// if cached that extension is not available return nullptr and
// UR_RESULT_ERROR_INVALID_VALUE
// UR_RESULT_ERROR_UNSUPPORTED_FEATURE
*Fptr = F;
return F ? UR_RESULT_SUCCESS : UR_RESULT_ERROR_INVALID_VALUE;
return F ? UR_RESULT_SUCCESS : UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

cl_uint DeviceCount;
Expand Down Expand Up @@ -409,7 +409,7 @@ static ur_result_t getExtFuncFromContext(cl_context Context,
if (!FuncPtr) {
// Cache that the extension is not available
FPtrMap[Context] = nullptr;
return UR_RESULT_ERROR_INVALID_VALUE;
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

*Fptr = FuncPtr;
Expand Down
64 changes: 41 additions & 23 deletions source/adapters/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
return ReturnValue(
static_cast<ur_memory_order_capability_flags_t>(URCapabilities));
}

case UR_DEVICE_INFO_ATOMIC_MEMORY_SCOPE_CAPABILITIES: {
/* Initialize result to minimum mandated capabilities according to
* SYCL2020 4.6.3.2. Because scopes are hierarchical, wider scopes support
Expand Down Expand Up @@ -624,6 +625,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
return ReturnValue(
static_cast<ur_memory_scope_capability_flags_t>(URCapabilities));
}

case UR_DEVICE_INFO_ATOMIC_FENCE_ORDER_CAPABILITIES: {
/* Initialize result to minimum mandated capabilities according to
* SYCL2020 4.6.3.2 */
Expand Down Expand Up @@ -671,6 +673,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
return ReturnValue(
static_cast<ur_memory_order_capability_flags_t>(URCapabilities));
}

case UR_DEVICE_INFO_ATOMIC_FENCE_SCOPE_CAPABILITIES: {
/* Initialize result to minimum mandated capabilities according to
* SYCL2020 4.6.3.2. Because scopes are hierarchical, wider scopes support
Expand All @@ -686,38 +689,53 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(
cl_adapter::cast<cl_device_id>(hDevice), DevVer));

cl_device_atomic_capabilities CLCapabilities;
auto convertCapabilities =
[](cl_device_atomic_capabilities CLCapabilities) {
ur_memory_scope_capability_flags_t URCapabilities = 0;
/* Because scopes are hierarchical, wider scopes support all narrower
* scopes. At a minimum, each device must support WORK_ITEM,
* SUB_GROUP and WORK_GROUP.
* (https://github.com/KhronosGroup/SYCL-Docs/pull/382). We already
* initialized to these minimum mandated capabilities. Just check
* wider scopes. */
if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_DEVICE) {
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE;
}

if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_ALL_DEVICES) {
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
}
return URCapabilities;
};

if (DevVer >= oclv::V3_0) {
cl_device_atomic_capabilities CLCapabilities;
CL_RETURN_ON_FAILURE(clGetDeviceInfo(
cl_adapter::cast<cl_device_id>(hDevice),
CL_DEVICE_ATOMIC_FENCE_CAPABILITIES,
sizeof(cl_device_atomic_capabilities), &CLCapabilities, nullptr));

assert((CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_WORK_GROUP) &&
"Violates minimum mandated guarantee");
URCapabilities |= convertCapabilities(CLCapabilities);
} else if (DevVer >= oclv::V2_0) {
/* OpenCL 2.x minimum mandated capabilities are WORK_GROUP | DEVICE |
ALL_DEVICES */
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE |
UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;

/* Because scopes are hierarchical, wider scopes support all narrower
* scopes. At a minimum, each device must support WORK_ITEM, SUB_GROUP and
* WORK_GROUP. (https://github.com/KhronosGroup/SYCL-Docs/pull/382). We
* already initialized to these minimum mandated capabilities. Just check
* wider scopes. */
if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_DEVICE) {
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE;
}

if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_ALL_DEVICES) {
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
}
} else {
/* This info is only available in OpenCL version >= 3.0. Just return
* minimum mandated capabilities for older versions. OpenCL 1.x minimum
* mandated capabilities are WORK_GROUP, we already initialized using it.
*/
if (DevVer >= oclv::V2_0) {
/* OpenCL 2.x minimum mandated capabilities are WORK_GROUP | DEVICE |
* ALL_DEVICES */
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE |
UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
// FIXME: Special case for Intel FPGA driver which is currently an
// OpenCL 1.2 device but is more capable than the default. This is a
// temporary work around until the Intel FPGA driver is updated to
// OpenCL 3.0. If the query is successful, then use the result but do
// not return an error if the query is unsuccessful as this is expected
// of an OpenCL 1.2 driver.
cl_device_atomic_capabilities CLCapabilities;
if (CL_SUCCESS == clGetDeviceInfo(cl_adapter::cast<cl_device_id>(hDevice),
CL_DEVICE_ATOMIC_FENCE_CAPABILITIES,
sizeof(cl_device_atomic_capabilities),
&CLCapabilities, nullptr)) {
URCapabilities |= convertCapabilities(CLCapabilities);
}
}

Expand Down
Loading

0 comments on commit def6bd0

Please sign in to comment.