[XPTI][SYCL] Fix undefined behavior in hash_t::bit_count() - #23078
Open
uditagarwal97 wants to merge 3 commits into
Open
[XPTI][SYCL] Fix undefined behavior in hash_t::bit_count()#23078uditagarwal97 wants to merge 3 commits into
hash_t::bit_count()#23078uditagarwal97 wants to merge 3 commits into
Conversation
std::log2(0) is -inf and converting it to unsigned is UB; the result was then used as a shift count, which is UB again. Compute the bit count with integer arithmetic instead, which also removes the pre-existing imprecision for large values that the FIXME above the function described. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: uditagarwal97 <16324601+uditagarwal97@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Full-width values still cause undefined 64-bit shifts in packing paths, and regression coverage is missing.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Replaces floating-point bit counting in XPTI hashing with exact integer arithmetic.
Changes:
- Handles zero without undefined conversion.
- Computes exact bit counts for large integers.
- Preserves one-bit allocation for zero-valued fields.
File summaries
| File | Description |
|---|---|
xpti/include/xpti/xpti_data_types.h |
Implements integer-based hash_t::bit_count(). |
Review details
Suppressed comments (1)
xpti/include/xpti/xpti_data_types.h:163
- For inputs at or above
2^63, this returns 64, butcompact()andcompact_short()immediately use the result as the shift count of auint64_t. A shift by the type width is still undefined, so the advertisedUINT64_MAXcase remains undefined in the packing path. Please handle a 64-bit field explicitly before shifting (for example, clear the accumulated value before OR-ing the full-width field), or constrain and validate the accepted input range.
return count;
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+156
to
+163
| if (value == 0) | ||
| return 1; | ||
| unsigned count = 0; | ||
| while (value) { | ||
| ++count; | ||
| value >>= 1; | ||
| } | ||
| return count; |
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
xpti::hash_t::bit_count()computed the number of bits needed to represent a value as:This has two defects, both flagged by UndefinedBehaviorSanitizer:
value == 0is undefined behaviour.std::log2(0)is-inf, and converting-inftounsignedis UB. On x86-64 the conversion yields0x80000000, so the function returned0x80000001, which is then used as a shift count incompact()/compact_short()— UB a second time. A zerolineorcolumnis the common case, not a corner case:sycl/include/sycl/detail/code_location.hppdefaults both to 0, so this fires on ordinary tracing paths.Imprecision for large values. The FIXME above the function already documented it: not every
uint64_tis representable asdouble, solog2can round up and the result is one too large. Measured against an exact integer implementation, every value from2^49-1upward is off by one (e.g.2^49-1: 50 vs the correct 49;UINT64_MAX: 65 vs 64).UBSan diagnostic (build configured with
-DLLVM_USE_SANITIZER=Address;Undefined; the runtime is built with-fno-sanitize-recover=all, so this aborts the process):It reproduced in 46 SYCL unit tests — any test that emits a tracepoint with an unknown line/column.
Fix
Replace the floating-point computation with an integer loop, and keep the historical "a zero field still occupies one bit" behaviour so the packing in
compact()/compact_short()does not change shape:Returning 1 for 0 matters: with 0 the neighbouring fields would shift into the zero field's place, and
(file=1,func=1,line=0,col=1)and(file=1,func=1,line=1,col=0)would collide. Over a small sampled domain, that variant raised the number of colliding pairs from 4016 to 6339, so it is kept at 1.Impact on hash values
Values change only where the old code was UB or imprecise:
00x80000001(UB)11 .. 2^482^49-1 .. UINT64_MAXCo-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com