Skip to content

Commit def6bd0

Browse files
committed
Merge branch 'main' into user-after-free
2 parents a8ae97e + befdc7c commit def6bd0

24 files changed

+830
-144
lines changed

.github/workflows/bandit.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ permissions:
1111
contents: read
1212

1313
jobs:
14-
linux:
14+
bandit:
1515
name: Bandit
16-
runs-on: ubuntu-latest
17-
16+
strategy:
17+
matrix:
18+
os: [ubuntu-latest, windows-latest]
19+
runs-on: ${{matrix.os}}
20+
1821
steps:
1922
- name: Clone the git repo
2023
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
2124

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

25-
# Scan is run only for the 'tools' folder.
28+
# Scan all files, except for dev. scripts
2629
- name: Run Bandit
27-
run: |
28-
bandit -r tools
30+
run: bandit -r . -x ./scripts/

scripts/verify_license.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

source/adapters/level_zero/device.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(
718718
}
719719
}
720720
}
721-
return ReturnValue(std::min(GlobalMemSize, FreeMemory));
721+
if (MemCount > 0) {
722+
return ReturnValue(std::min(GlobalMemSize, FreeMemory));
723+
} else {
724+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
725+
}
722726
}
723727
case UR_DEVICE_INFO_MEMORY_CLOCK_RATE: {
724728
// If there are not any memory modules then return 0.

source/adapters/opencl/adapter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "common.hpp"
12+
#include "logger/ur_logger.hpp"
1213

1314
struct ur_adapter_handle_t_ {
1415
std::atomic<uint32_t> RefCount = 0;
1516
std::mutex Mutex;
17+
logger::Logger &log = logger::get_logger("opencl");
1618
};
1719

1820
static ur_adapter_handle_t_ *adapter = nullptr;

source/adapters/opencl/command_buffer.cpp

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
2121

2222
cl_context CLContext = cl_adapter::cast<cl_context>(hContext);
2323
cl_ext::clCreateCommandBufferKHR_fn clCreateCommandBufferKHR = nullptr;
24-
cl_int Res =
24+
UR_RETURN_ON_FAILURE(
2525
cl_ext::getExtFuncFromContext<decltype(clCreateCommandBufferKHR)>(
2626
CLContext, cl_ext::ExtFuncPtrCache->clCreateCommandBufferKHRCache,
27-
cl_ext::CreateCommandBufferName, &clCreateCommandBufferKHR);
28-
29-
if (!clCreateCommandBufferKHR || Res != CL_SUCCESS)
30-
return UR_RESULT_ERROR_INVALID_OPERATION;
27+
cl_ext::CreateCommandBufferName, &clCreateCommandBufferKHR));
3128

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

5250
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
5351
cl_ext::clRetainCommandBufferKHR_fn clRetainCommandBuffer = nullptr;
54-
cl_int Res = cl_ext::getExtFuncFromContext<decltype(clRetainCommandBuffer)>(
55-
CLContext, cl_ext::ExtFuncPtrCache->clRetainCommandBufferKHRCache,
56-
cl_ext::RetainCommandBufferName, &clRetainCommandBuffer);
57-
58-
if (!clRetainCommandBuffer || Res != CL_SUCCESS)
59-
return UR_RESULT_ERROR_INVALID_OPERATION;
52+
UR_RETURN_ON_FAILURE(
53+
cl_ext::getExtFuncFromContext<decltype(clRetainCommandBuffer)>(
54+
CLContext, cl_ext::ExtFuncPtrCache->clRetainCommandBufferKHRCache,
55+
cl_ext::RetainCommandBufferName, &clRetainCommandBuffer));
6056

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

6965
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
7066
cl_ext::clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHR = nullptr;
71-
cl_int Res =
67+
UR_RETURN_ON_FAILURE(
7268
cl_ext::getExtFuncFromContext<decltype(clReleaseCommandBufferKHR)>(
7369
CLContext, cl_ext::ExtFuncPtrCache->clReleaseCommandBufferKHRCache,
74-
cl_ext::ReleaseCommandBufferName, &clReleaseCommandBufferKHR);
75-
76-
if (!clReleaseCommandBufferKHR || Res != CL_SUCCESS)
77-
return UR_RESULT_ERROR_INVALID_OPERATION;
70+
cl_ext::ReleaseCommandBufferName, &clReleaseCommandBufferKHR));
7871

7972
CL_RETURN_ON_FAILURE(
8073
clReleaseCommandBufferKHR(hCommandBuffer->CLCommandBuffer));
@@ -85,13 +78,10 @@ UR_APIEXPORT ur_result_t UR_APICALL
8578
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
8679
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
8780
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
88-
cl_int Res =
81+
UR_RETURN_ON_FAILURE(
8982
cl_ext::getExtFuncFromContext<decltype(clFinalizeCommandBufferKHR)>(
9083
CLContext, cl_ext::ExtFuncPtrCache->clFinalizeCommandBufferKHRCache,
91-
cl_ext::FinalizeCommandBufferName, &clFinalizeCommandBufferKHR);
92-
93-
if (!clFinalizeCommandBufferKHR || Res != CL_SUCCESS)
94-
return UR_RESULT_ERROR_INVALID_OPERATION;
84+
cl_ext::FinalizeCommandBufferName, &clFinalizeCommandBufferKHR));
9585

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

110100
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
111101
cl_ext::clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHR = nullptr;
112-
cl_int Res =
102+
UR_RETURN_ON_FAILURE(
113103
cl_ext::getExtFuncFromContext<decltype(clCommandNDRangeKernelKHR)>(
114104
CLContext, cl_ext::ExtFuncPtrCache->clCommandNDRangeKernelKHRCache,
115-
cl_ext::CommandNRRangeKernelName, &clCommandNDRangeKernelKHR);
116-
117-
if (!clCommandNDRangeKernelKHR || Res != CL_SUCCESS)
118-
return UR_RESULT_ERROR_INVALID_OPERATION;
105+
cl_ext::CommandNRRangeKernelName, &clCommandNDRangeKernelKHR));
119106

120107
CL_RETURN_ON_FAILURE(clCommandNDRangeKernelKHR(
121108
hCommandBuffer->CLCommandBuffer, nullptr, nullptr,
@@ -157,12 +144,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(
157144

158145
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
159146
cl_ext::clCommandCopyBufferKHR_fn clCommandCopyBufferKHR = nullptr;
160-
cl_int Res = cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferKHR)>(
161-
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferKHRCache,
162-
cl_ext::CommandCopyBufferName, &clCommandCopyBufferKHR);
163-
164-
if (!clCommandCopyBufferKHR || Res != CL_SUCCESS)
165-
return UR_RESULT_ERROR_INVALID_OPERATION;
147+
UR_RETURN_ON_FAILURE(
148+
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferKHR)>(
149+
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferKHRCache,
150+
cl_ext::CommandCopyBufferName, &clCommandCopyBufferKHR));
166151

167152
CL_RETURN_ON_FAILURE(clCommandCopyBufferKHR(
168153
hCommandBuffer->CLCommandBuffer, nullptr,
@@ -193,13 +178,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp(
193178

194179
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
195180
cl_ext::clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHR = nullptr;
196-
cl_int Res =
181+
UR_RETURN_ON_FAILURE(
197182
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferRectKHR)>(
198183
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferRectKHRCache,
199-
cl_ext::CommandCopyBufferRectName, &clCommandCopyBufferRectKHR);
200-
201-
if (!clCommandCopyBufferRectKHR || Res != CL_SUCCESS)
202-
return UR_RESULT_ERROR_INVALID_OPERATION;
184+
cl_ext::CommandCopyBufferRectName, &clCommandCopyBufferRectKHR));
203185

204186
CL_RETURN_ON_FAILURE(clCommandCopyBufferRectKHR(
205187
hCommandBuffer->CLCommandBuffer, nullptr,
@@ -283,12 +265,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferFillExp(
283265

284266
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
285267
cl_ext::clCommandFillBufferKHR_fn clCommandFillBufferKHR = nullptr;
286-
cl_int Res = cl_ext::getExtFuncFromContext<decltype(clCommandFillBufferKHR)>(
287-
CLContext, cl_ext::ExtFuncPtrCache->clCommandFillBufferKHRCache,
288-
cl_ext::CommandFillBufferName, &clCommandFillBufferKHR);
289-
290-
if (!clCommandFillBufferKHR || Res != CL_SUCCESS)
291-
return UR_RESULT_ERROR_INVALID_OPERATION;
268+
UR_RETURN_ON_FAILURE(
269+
cl_ext::getExtFuncFromContext<decltype(clCommandFillBufferKHR)>(
270+
CLContext, cl_ext::ExtFuncPtrCache->clCommandFillBufferKHRCache,
271+
cl_ext::CommandFillBufferName, &clCommandFillBufferKHR));
292272

293273
CL_RETURN_ON_FAILURE(clCommandFillBufferKHR(
294274
hCommandBuffer->CLCommandBuffer, nullptr,
@@ -339,13 +319,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
339319

340320
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
341321
cl_ext::clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHR = nullptr;
342-
cl_int Res =
322+
UR_RETURN_ON_FAILURE(
343323
cl_ext::getExtFuncFromContext<decltype(clEnqueueCommandBufferKHR)>(
344324
CLContext, cl_ext::ExtFuncPtrCache->clEnqueueCommandBufferKHRCache,
345-
cl_ext::EnqueueCommandBufferName, &clEnqueueCommandBufferKHR);
346-
347-
if (!clEnqueueCommandBufferKHR || Res != CL_SUCCESS)
348-
return UR_RESULT_ERROR_INVALID_OPERATION;
325+
cl_ext::EnqueueCommandBufferName, &clEnqueueCommandBufferKHR));
349326

350327
const uint32_t NumberOfQueues = 1;
351328

@@ -382,13 +359,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp(
382359

383360
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
384361
cl_ext::clGetCommandBufferInfoKHR_fn clGetCommandBufferInfoKHR = nullptr;
385-
cl_int Res =
362+
UR_RETURN_ON_FAILURE(
386363
cl_ext::getExtFuncFromContext<decltype(clGetCommandBufferInfoKHR)>(
387364
CLContext, cl_ext::ExtFuncPtrCache->clGetCommandBufferInfoKHRCache,
388-
cl_ext::GetCommandBufferInfoName, &clGetCommandBufferInfoKHR);
389-
390-
if (!clGetCommandBufferInfoKHR || Res != CL_SUCCESS)
391-
return UR_RESULT_ERROR_INVALID_OPERATION;
365+
cl_ext::GetCommandBufferInfoName, &clGetCommandBufferInfoKHR));
392366

393367
if (propName != UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT) {
394368
return UR_RESULT_ERROR_INVALID_ENUMERATION;

source/adapters/opencl/common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "common.hpp"
12-
12+
#include "logger/ur_logger.hpp"
1313
namespace cl_adapter {
1414

1515
/* Global variables for urPlatformGetLastError() */
@@ -91,7 +91,7 @@ ur_result_t mapCLErrorToUR(cl_int Result) {
9191
}
9292

9393
void cl_adapter::die(const char *Message) {
94-
std::cerr << "ur_die: " << Message << "\n";
94+
logger::always("ur_die: {}", Message);
9595
std::terminate();
9696
}
9797

source/adapters/opencl/common.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ static ur_result_t getExtFuncFromContext(cl_context Context,
373373
if (It != FPtrMap.end()) {
374374
auto F = It->second;
375375
// if cached that extension is not available return nullptr and
376-
// UR_RESULT_ERROR_INVALID_VALUE
376+
// UR_RESULT_ERROR_UNSUPPORTED_FEATURE
377377
*Fptr = F;
378-
return F ? UR_RESULT_SUCCESS : UR_RESULT_ERROR_INVALID_VALUE;
378+
return F ? UR_RESULT_SUCCESS : UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
379379
}
380380

381381
cl_uint DeviceCount;
@@ -409,7 +409,7 @@ static ur_result_t getExtFuncFromContext(cl_context Context,
409409
if (!FuncPtr) {
410410
// Cache that the extension is not available
411411
FPtrMap[Context] = nullptr;
412-
return UR_RESULT_ERROR_INVALID_VALUE;
412+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
413413
}
414414

415415
*Fptr = FuncPtr;

source/adapters/opencl/device.cpp

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
569569
return ReturnValue(
570570
static_cast<ur_memory_order_capability_flags_t>(URCapabilities));
571571
}
572+
572573
case UR_DEVICE_INFO_ATOMIC_MEMORY_SCOPE_CAPABILITIES: {
573574
/* Initialize result to minimum mandated capabilities according to
574575
* SYCL2020 4.6.3.2. Because scopes are hierarchical, wider scopes support
@@ -624,6 +625,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
624625
return ReturnValue(
625626
static_cast<ur_memory_scope_capability_flags_t>(URCapabilities));
626627
}
628+
627629
case UR_DEVICE_INFO_ATOMIC_FENCE_ORDER_CAPABILITIES: {
628630
/* Initialize result to minimum mandated capabilities according to
629631
* SYCL2020 4.6.3.2 */
@@ -671,6 +673,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
671673
return ReturnValue(
672674
static_cast<ur_memory_order_capability_flags_t>(URCapabilities));
673675
}
676+
674677
case UR_DEVICE_INFO_ATOMIC_FENCE_SCOPE_CAPABILITIES: {
675678
/* Initialize result to minimum mandated capabilities according to
676679
* SYCL2020 4.6.3.2. Because scopes are hierarchical, wider scopes support
@@ -686,38 +689,53 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
686689
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(
687690
cl_adapter::cast<cl_device_id>(hDevice), DevVer));
688691

689-
cl_device_atomic_capabilities CLCapabilities;
692+
auto convertCapabilities =
693+
[](cl_device_atomic_capabilities CLCapabilities) {
694+
ur_memory_scope_capability_flags_t URCapabilities = 0;
695+
/* Because scopes are hierarchical, wider scopes support all narrower
696+
* scopes. At a minimum, each device must support WORK_ITEM,
697+
* SUB_GROUP and WORK_GROUP.
698+
* (https://github.com/KhronosGroup/SYCL-Docs/pull/382). We already
699+
* initialized to these minimum mandated capabilities. Just check
700+
* wider scopes. */
701+
if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_DEVICE) {
702+
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE;
703+
}
704+
705+
if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_ALL_DEVICES) {
706+
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
707+
}
708+
return URCapabilities;
709+
};
710+
690711
if (DevVer >= oclv::V3_0) {
712+
cl_device_atomic_capabilities CLCapabilities;
691713
CL_RETURN_ON_FAILURE(clGetDeviceInfo(
692714
cl_adapter::cast<cl_device_id>(hDevice),
693715
CL_DEVICE_ATOMIC_FENCE_CAPABILITIES,
694716
sizeof(cl_device_atomic_capabilities), &CLCapabilities, nullptr));
695-
696717
assert((CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_WORK_GROUP) &&
697718
"Violates minimum mandated guarantee");
719+
URCapabilities |= convertCapabilities(CLCapabilities);
720+
} else if (DevVer >= oclv::V2_0) {
721+
/* OpenCL 2.x minimum mandated capabilities are WORK_GROUP | DEVICE |
722+
ALL_DEVICES */
723+
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE |
724+
UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
698725

699-
/* Because scopes are hierarchical, wider scopes support all narrower
700-
* scopes. At a minimum, each device must support WORK_ITEM, SUB_GROUP and
701-
* WORK_GROUP. (https://github.com/KhronosGroup/SYCL-Docs/pull/382). We
702-
* already initialized to these minimum mandated capabilities. Just check
703-
* wider scopes. */
704-
if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_DEVICE) {
705-
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE;
706-
}
707-
708-
if (CLCapabilities & CL_DEVICE_ATOMIC_SCOPE_ALL_DEVICES) {
709-
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
710-
}
711726
} else {
712-
/* This info is only available in OpenCL version >= 3.0. Just return
713-
* minimum mandated capabilities for older versions. OpenCL 1.x minimum
714-
* mandated capabilities are WORK_GROUP, we already initialized using it.
715-
*/
716-
if (DevVer >= oclv::V2_0) {
717-
/* OpenCL 2.x minimum mandated capabilities are WORK_GROUP | DEVICE |
718-
* ALL_DEVICES */
719-
URCapabilities |= UR_MEMORY_SCOPE_CAPABILITY_FLAG_DEVICE |
720-
UR_MEMORY_SCOPE_CAPABILITY_FLAG_SYSTEM;
727+
// FIXME: Special case for Intel FPGA driver which is currently an
728+
// OpenCL 1.2 device but is more capable than the default. This is a
729+
// temporary work around until the Intel FPGA driver is updated to
730+
// OpenCL 3.0. If the query is successful, then use the result but do
731+
// not return an error if the query is unsuccessful as this is expected
732+
// of an OpenCL 1.2 driver.
733+
cl_device_atomic_capabilities CLCapabilities;
734+
if (CL_SUCCESS == clGetDeviceInfo(cl_adapter::cast<cl_device_id>(hDevice),
735+
CL_DEVICE_ATOMIC_FENCE_CAPABILITIES,
736+
sizeof(cl_device_atomic_capabilities),
737+
&CLCapabilities, nullptr)) {
738+
URCapabilities |= convertCapabilities(CLCapabilities);
721739
}
722740
}
723741

0 commit comments

Comments
 (0)