OpenCL application crashes when clCreateContext is invoked with an invalid device

Hi,

I want to report a bug in the NVIDIA OpenCL runtime. When my application calls clCreateContext() with an invalid device (nullptr) the application crashes with a segmentation fault (in the OpenCL runtime).

In my concrete case “cl_uint num_devices” is 1 and “const cl_device_id *devices” is !nullptr but devices[0] stores a nullptr.

The documentation https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clCreateContext.html explains two cases:

  • "CL_INVALID_VALUE if devices is NULL; if num_devices is equal to zero; or if pfn_notify is NULL but user_data is not NULL. "
  • “CL_INVALID_DEVICE if devices contains an invalid device.”

So I would expect to get CL_INVALID_DEVICE instead of a crash

Jerry

OS: Ubuntu 18.04
Card: GeForce GTX 1080
Driver Version 390.116

Hi,

this is a minimal reproducer–it assumes that only one OpenCL runtime (the NVIDIA one) is available.

1
2 #include <CL/opencl.h>
3
4 int main() {
5 cl_uint platform_count{0};
6 clGetPlatformIDs(0, nullptr, &platform_count);
7 cl_platform_id platform_ids[platform_count];
8 clGetPlatformIDs(platform_count, platform_ids, &platform_count);
9
10 cl_device_id device_ids[1];
11 device_ids[0] = nullptr;
12
13 const cl_context_properties property{
14 reinterpret_cast<cl_context_properties>(platform_ids[0])};
15 const cl_context_properties properties[3]{CL_CONTEXT_PLATFORM, property, 0};
16
17 cl_context context =
18 clCreateContext(properties, 1, device_ids, nullptr, nullptr, nullptr);
19
20 return 0;
21 }

Compile with

$ g++ -g main.cpp -lOpenCL && ./a.out

Stacktrace from GDB

Reading symbols from a.out…done.
(gdb) run
Starting program: /tmp/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff658403a in ?? () from /usr/lib/x86_64-linux-gnu/libnvidia-opencl.so.1
(gdb) bt
#0 0x00007ffff658403a in ?? () from /usr/lib/x86_64-linux-gnu/libnvidia-opencl.so.1
#1 0x000055555555486f in main () at main.cpp:18

Jerry