Skip to content

fix(opencl) prevent sign extension in CL.createPlatformCapabilities#1087

Closed
PeterPan94 wants to merge 1 commit intoLWJGL:masterfrom
PeterPan94:opencl-fix
Closed

fix(opencl) prevent sign extension in CL.createPlatformCapabilities#1087
PeterPan94 wants to merge 1 commit intoLWJGL:masterfrom
PeterPan94:opencl-fix

Conversation

@PeterPan94
Copy link
Copy Markdown

The specification for clGetDeviceIDs allows the implementation to error on any value not explicitly listed. CL_DEVICE_TYPE_ALL is defined as the integer 0xFFFF_FFFF = -1. When directly passed to nclGetDeviceIDs, the sign is extended to 0xFFFF_FFFF_FFFF_FFFFl = -1l. As the specification only requires the implementation to accept 0xFFFF_FFFFl, this may lead to nclGetDeviceIDs failing and returning CL_INVALID_DEVICE_TYPE.

I encountered this bug using the opencl-mesa drivers on arch linux. This fix only enables createPlatformCapabilities to return successfully. The problem still occurs anytime CL_DEVICE_TYPE_ALL is passed to (n)clGetDeviceIDs. This can be prevented by the user using the same method as this fix to prevent sign extension. As this isn't ideal, the types of CL_DEVICE_TYPE_* should probably be changed to long in a release where breaking backwards compatibility is acceptable if possible.

An other option would be to trim the upper 32 bits of the long passed to (n)clGetDeviceIDs to undo the sign extension retroactively, to fix this problem without breaking compatibility. I briefly tried implementing this approach, but was unsure how to approach it, as the java code blocks won't apply to nclGetDeviceIDs while nativeBeforeCall creates JNI code and needs to load a library.

CL_DEVICE_TYPE_ALL is defined as the integer 0xFFFF_FFFF = -1. When
directly passed to nclGetDeviceIDs, the sign is extended to
0xFFFF_FFFF_FFFF_FFFFl = -1l. As the specification only requires the
implementation to accept 0xFFFF_FFFFl, this may lead to nclGetDeviceIDs
failing and returning CL_INVALID_DEVICE_TYPE.
@Spasi Spasi closed this in fa6b6ff Nov 2, 2025
@Spasi
Copy link
Copy Markdown
Member

Spasi commented Nov 2, 2025

Hey @PeterPan94,

I have changed the constants themselves to long and also fixed every constant that derives from cl_ulong (cl_bitfield, cl_properties, etc.). This is API breaking but, since the next release is 3.4.0, it's a good time to make this change.

I have a plan to avoid such issues in the future, but it requires a refactoring of the generator type system. It's also a prerequisite for LWJGL 4, to migrate constants to value types.

Thanks!

@Spasi Spasi added the Type: Bug label Nov 2, 2025
@PeterPan94
Copy link
Copy Markdown
Author

Thank you very much for your quick response!

@PeterPan94 PeterPan94 deleted the opencl-fix branch November 3, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants