From 5827b1b0af7d6620667d9a5097f4a969ee534e9b Mon Sep 17 00:00:00 2001 From: nashit hayyat Date: Fri, 20 Nov 2026 22:19:25 +0530 Subject: [PATCH] clamp OpenCL platform and device counts in create_opencl_context clGetPlatformIDs and clGetDeviceIDs report the number of platforms or devices available, which can exceed the number of entries written to the caller's array. create_opencl_context iterated up to those counts, reading uninitialized stack slots past the end of platforms[4] and passing them to clGetPlatformInfo as handles. Clamp both counts to the array sizes, and do the same in the test helper in gpu_context.h. --- src/runtime/opencl.cpp | 9 ++ test/common/gpu_context.h | 5 + test/correctness/CMakeLists.txt | 1 + test/correctness/opencl_platform_count.cpp | 116 +++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 test/correctness/opencl_platform_count.cpp diff --git a/src/runtime/opencl.cpp b/src/runtime/opencl.cpp index 55b30e95c485..d0e859b5d245 100644 --- a/src/runtime/opencl.cpp +++ b/src/runtime/opencl.cpp @@ -417,6 +417,11 @@ WEAK int create_opencl_context(void *user_context, cl_context *ctx, cl_command_q if (err != CL_SUCCESS) { return error_opencl(user_context, err, "clGetPlatformIDs failed"); } + // platform_count is the number of platforms available, which can be more + // than the number written to platforms. + if (platform_count > max_platforms) { + platform_count = max_platforms; + } cl_platform_id platform = nullptr; @@ -490,6 +495,10 @@ WEAK int create_opencl_context(void *user_context, cl_context *ctx, cl_command_q if (err != CL_SUCCESS) { return error_opencl(user_context, err, "clGetDeviceIDs failed"); } + // Likewise, deviceCount can be more than the number written to devices. + if (deviceCount > maxDevices) { + deviceCount = maxDevices; + } // If the user indicated a specific device index to use, use // that. Note that this is an index within the set of devices diff --git a/test/common/gpu_context.h b/test/common/gpu_context.h index 474e837a91f4..e5a203058c65 100644 --- a/test/common/gpu_context.h +++ b/test/common/gpu_context.h @@ -48,6 +48,11 @@ inline bool create_opencl_context(cl_context &cl_ctx, cl_command_queue &cl_q) { printf("Failed to get device\n"); return false; } + // deviceCount is the number of devices available, which can be more than + // the number written to devices. + if (deviceCount > maxDevices) { + deviceCount = maxDevices; + } cl_device_id dev = devices[deviceCount - 1]; diff --git a/test/correctness/CMakeLists.txt b/test/correctness/CMakeLists.txt index aaad8860ba72..03d22677928a 100644 --- a/test/correctness/CMakeLists.txt +++ b/test/correctness/CMakeLists.txt @@ -698,6 +698,7 @@ tests( nested_parallel_stack.cpp nested_shiftinwards.cpp oddly_sized_output.cpp + opencl_platform_count.cpp parallel.cpp parallel_alloc.cpp parallel_fork.cpp diff --git a/test/correctness/opencl_platform_count.cpp b/test/correctness/opencl_platform_count.cpp new file mode 100644 index 000000000000..d646e2b48600 --- /dev/null +++ b/test/correctness/opencl_platform_count.cpp @@ -0,0 +1,116 @@ +#include "Halide.h" +#include +#include +#include + +using namespace Halide; + +// clGetPlatformIDs reports the number of platforms available, which can be +// larger than the number of entries the caller asked for. This test installs +// a fake OpenCL library that does exactly that, and checks that the OpenCL +// runtime only uses the platform handles that were actually written. + +namespace { + +const int max_fake_platforms = 64; +int fake_platforms[max_fake_platforms]; +uint32_t platforms_written = 0; + +int32_t fake_clGetPlatformIDs(uint32_t num_entries, void **platforms, uint32_t *num_platforms) { + if (platforms) { + platforms_written = std::min(num_entries, max_fake_platforms); + for (uint32_t i = 0; i < platforms_written; i++) { + platforms[i] = &fake_platforms[i]; + } + } + if (num_platforms) { + *num_platforms = num_entries + 4; + } + return 0; +} + +int32_t fake_clGetPlatformInfo(void *platform, uint32_t param_name, size_t value_size, void *value, size_t *value_size_ret) { + bool valid = false; + for (uint32_t i = 0; i < platforms_written; i++) { + valid |= (platform == &fake_platforms[i]); + } + if (!valid) { + fprintf(stderr, "clGetPlatformInfo called with a platform that clGetPlatformIDs did not return: %p\n", platform); + exit(1); + } + const char name[] = "Fake Platform"; + if (value && value_size >= sizeof(name)) { + memcpy(value, name, sizeof(name)); + } + if (value_size_ret) { + *value_size_ret = sizeof(name); + } + return 0; +} + +int32_t fake_unexpected_call() { + fprintf(stderr, "Saw an unexpected OpenCL call\n"); + exit(1); +} + +int fake_library; + +void *my_load_library_impl(const char *name) { + return &fake_library; +} + +void *my_get_library_symbol_impl(void *lib, const char *name) { + if (!strcmp(name, "clGetPlatformIDs")) { + return (void *)fake_clGetPlatformIDs; + } else if (!strcmp(name, "clGetPlatformInfo")) { + return (void *)fake_clGetPlatformInfo; + } + return (void *)fake_unexpected_call; +} + +void my_error_handler(JITUserContext *u, const char *msg) { + // Emitting "error.*:" to stdout or stderr will cause CMake to report the + // test as a failure on Windows, regardless of error code returned, + // hence the abbreviation to "err". + if (!strstr(msg, "Failed to find platform")) { + fprintf(stderr, "Saw unexpected err: %s\n", msg); + exit(1); + } + printf("Saw expected err: %s\n", msg); + printf("Success!\n"); + exit(0); +} + +} // namespace + +int main(int argc, char **argv) { + Target target = get_jit_target_from_environment(); + if (!target.has_feature(Target::OpenCL)) { + printf("[SKIP] OpenCL not enabled.\n"); + return 0; + } + + // Ask for a platform that none of the fake platforms match, so that the + // runtime looks at every platform it thinks it has. +#ifdef _WIN32 + _putenv_s("HL_OCL_PLATFORM_NAME", "No Such Platform"); +#else + setenv("HL_OCL_PLATFORM_NAME", "No Such Platform", 1); +#endif + + JITHandlers handlers; + handlers.custom_load_library = my_load_library_impl; + handlers.custom_get_library_symbol = my_get_library_symbol_impl; + Internal::JITSharedRuntime::set_default_handlers(handlers); + + Var x, y, xi, yi; + Func f; + f(x, y) = cast(x + y); + f.gpu_tile(x, y, xi, yi, 8, 8, TailStrategy::Auto, DeviceAPI::OpenCL); + f.jit_handlers().custom_error = my_error_handler; + + Buffer out = f.realize({64, 64}, target); + + fprintf(stderr, "Should not get here.\n"); + return 1; +}