Skip to content

Commit f4577ff

Browse files
[SYCL] Fix stack smashing due to different sizes of cl_bool and bool (#9561)
This patch fixes stack smashing happened during querying of available aspects list, and also during calling `sycl-ls --verbose`. The stack smashing happened due to different sizes of `cl_bool` and `bool` types. Patch changes `cl_bool` to `bool` because there is no need in `cl_bool` in this part of the code, and also adds a (probably unnecessary) diagnostic which returns `PI_ERROR_INVALID_VALUE` - the same diagnostic is a part of OpenCL spec for `clGetDeviceInfo` function.
1 parent 3be3865 commit f4577ff

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

sycl/plugins/opencl/pi_opencl.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ pi_result piDeviceGetInfo(pi_device device, pi_device_info paramName,
605605
}
606606
case PI_DEVICE_INFO_ATOMIC_64: {
607607
cl_int ret_err = CL_SUCCESS;
608-
cl_bool result = CL_FALSE;
608+
bool result = false;
609+
if (paramValueSize < sizeof(result))
610+
return PI_ERROR_INVALID_VALUE;
609611
bool supported = false;
610612

611613
ret_err = checkDeviceExtensions(
@@ -616,18 +618,22 @@ pi_result piDeviceGetInfo(pi_device device, pi_device_info paramName,
616618
return static_cast<pi_result>(ret_err);
617619

618620
result = supported;
619-
std::memcpy(paramValue, &result, sizeof(cl_bool));
621+
std::memcpy(paramValue, &result, sizeof(result));
620622
return PI_SUCCESS;
621623
}
622624
case PI_EXT_ONEAPI_DEVICE_INFO_BFLOAT16_MATH_FUNCTIONS: {
623625
// bfloat16 math functions are not yet supported on Intel GPUs.
624-
cl_bool result = false;
625-
std::memcpy(paramValue, &result, sizeof(cl_bool));
626+
bool result = false;
627+
if (paramValueSize < sizeof(result))
628+
return PI_ERROR_INVALID_VALUE;
629+
std::memcpy(paramValue, &result, sizeof(result));
626630
return PI_SUCCESS;
627631
}
628632
case PI_DEVICE_INFO_IMAGE_SRGB: {
629-
cl_bool result = true;
630-
std::memcpy(paramValue, &result, sizeof(cl_bool));
633+
bool result = true;
634+
if (paramValueSize < sizeof(result))
635+
return PI_ERROR_INVALID_VALUE;
636+
std::memcpy(paramValue, &result, sizeof(result));
631637
return PI_SUCCESS;
632638
}
633639
case PI_DEVICE_INFO_BUILD_ON_SUBDEVICE: {
@@ -637,8 +643,10 @@ pi_result piDeviceGetInfo(pi_device device, pi_device_info paramName,
637643

638644
// FIXME: here we assume that program built for a root GPU device can be
639645
// used on its sub-devices without re-building
640-
cl_bool result = (res == CL_SUCCESS) && (devType == CL_DEVICE_TYPE_GPU);
641-
std::memcpy(paramValue, &result, sizeof(cl_bool));
646+
bool result = (res == CL_SUCCESS) && (devType == CL_DEVICE_TYPE_GPU);
647+
if (paramValueSize < sizeof(result))
648+
return PI_ERROR_INVALID_VALUE;
649+
std::memcpy(paramValue, &result, sizeof(result));
642650
return PI_SUCCESS;
643651
}
644652
case PI_EXT_ONEAPI_DEVICE_INFO_MAX_WORK_GROUPS_3D:
@@ -715,7 +723,9 @@ pi_result piDeviceGetInfo(pi_device device, pi_device_info paramName,
715723
}
716724
case PI_EXT_INTEL_DEVICE_INFO_MEM_CHANNEL_SUPPORT: {
717725
cl_int ret_err = CL_SUCCESS;
718-
cl_bool result = CL_FALSE;
726+
bool result = false;
727+
if (paramValueSize < sizeof(result))
728+
return PI_ERROR_INVALID_VALUE;
719729
bool supported = false;
720730

721731
ret_err =
@@ -725,7 +735,7 @@ pi_result piDeviceGetInfo(pi_device device, pi_device_info paramName,
725735
return static_cast<pi_result>(ret_err);
726736

727737
result = supported;
728-
std::memcpy(paramValue, &result, sizeof(cl_bool));
738+
std::memcpy(paramValue, &result, sizeof(result));
729739
return PI_SUCCESS;
730740
}
731741
default:

0 commit comments

Comments
 (0)