Skip to content

clamp OpenCL platform and device counts in create_opencl_context - #9723

Open
Nashit-h wants to merge 1 commit into
halide:mainfrom
Nashit-h:opencl-platform-count-clamp
Open

Nashit-h wants to merge 1 commit into
halide:mainfrom
Nashit-h:opencl-platform-count-clamp

Conversation

@Nashit-h

Copy link
Copy Markdown
Contributor

create_opencl_context in src/runtime/opencl.cpp calls clGetPlatformIDs(4, platforms, &platform_count) and then loops i < platform_count over platforms[i]. Per the OpenCL spec, num_platforms returns the number of platforms available, not the number written, so on a machine with more than 4 installed platforms (and HL_OCL_PLATFORM_NAME naming one that isn't among the first 4) the loop reads uninitialized stack slots past the end of the array and hands them to clGetPlatformInfo as platform handles. clGetDeviceIDs / deviceCount has the same issue against the 128-entry devices array, both in the auto-select loop and the HL_GPU_DEVICE bounds check. The fix clamps each count to the array size right after the query. test/common/gpu_context.h had the same pattern with devices[deviceCount - 1] and a 4-entry array, so it gets the same clamp.

The new correctness_opencl_platform_count test uses JIT handlers to install a fake OpenCL library whose clGetPlatformIDs reports 4 more platforms than requested. Without the fix the runtime calls clGetPlatformInfo with 0x616C5020656B6146 (stale stack bytes, "Fake Pla" from the name buffer); with it the runtime reports "Failed to find platform". Like load_library, it prints [SKIP] unless the JIT target has OpenCL. I ran it with HL_JIT_TARGET=host-opencl on Windows (MSVC, LLVM 22), along with correctness_load_library, correctness_custom_opencl_context and correctness_bounds. clang-format, keep-sorted and the CMake list/style checks pass.

Checklist

  • Tests added or updated (not required for docs, CI config, or typo fixes)
  • Documentation updated (if public API changed)
  • Python bindings updated (if public API changed)
  • Benchmarks are included here if the change is intended to affect performance.
  • Commits include AI attribution where applicable (see Code of Conduct)

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

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.30%. Comparing base (05113e0) to head (5827b1b).
⚠️ Report is 6 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcourteaux mcourteaux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@alexreinking alexreinking added the backport:pending Needs to be cherry-picked to the active release branch label Nov 29, 2026
@Nashit-h

Copy link
Copy Markdown
Contributor Author

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:pending Needs to be cherry-picked to the active release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants