Repository navigation
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9723 +/- ##
==========================================
- Coverage 73.40% 73.30% -0.10%
==========================================
Files 266 266
Lines 83749 83749
Branches 20573 20573
==========================================
- Hits 61474 61392 -82
- Misses 15808 15832 +24
- Partials 6467 6525 +58 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mcourteaux
left a comment
There was a problem hiding this comment.
LGTM. Good catch. I don't think all these AI-generated tests for such small things that will never break again are really required. @alexreinking @abadams opinions on tests like this? Our CI time is already big.
|
The runtime clamp is the fix on its own, so the test is easy to drop if you'd rather not spend CI on it. It prints [SKIP] unless the JIT target has OpenCL, so for most configs it's only the build cost, but I'm fine pulling it out whichever way you all land. |
create_opencl_contextinsrc/runtime/opencl.cppcallsclGetPlatformIDs(4, platforms, &platform_count)and then loopsi < platform_countoverplatforms[i]. Per the OpenCL spec,num_platformsreturns the number of platforms available, not the number written, so on a machine with more than 4 installed platforms (andHL_OCL_PLATFORM_NAMEnaming one that isn't among the first 4) the loop reads uninitialized stack slots past the end of the array and hands them toclGetPlatformInfoas platform handles.clGetDeviceIDs/deviceCounthas the same issue against the 128-entrydevicesarray, both in the auto-select loop and theHL_GPU_DEVICEbounds check. The fix clamps each count to the array size right after the query.test/common/gpu_context.hhad the same pattern withdevices[deviceCount - 1]and a 4-entry array, so it gets the same clamp.The new
correctness_opencl_platform_counttest uses JIT handlers to install a fake OpenCL library whoseclGetPlatformIDsreports 4 more platforms than requested. Without the fix the runtime callsclGetPlatformInfowith0x616C5020656B6146(stale stack bytes, "Fake Pla" from the name buffer); with it the runtime reports "Failed to find platform". Likeload_library, it prints[SKIP]unless the JIT target has OpenCL. I ran it withHL_JIT_TARGET=host-openclon Windows (MSVC, LLVM 22), along withcorrectness_load_library,correctness_custom_opencl_contextandcorrectness_bounds. clang-format, keep-sorted and the CMake list/style checks pass.Checklist