OpenCL application crashes when clCreateContext is invoked with an invalid device


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 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


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


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

2 #include <CL/opencl.h>
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);
10 cl_device_id device_ids[1];
11 device_ids[0] = nullptr;
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};
17 cl_context context =
18 clCreateContext(properties, 1, device_ids, nullptr, nullptr, nullptr);
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/”.

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