Skip to content

UCS/BITMAP: Add static check for bitmap loop index size - #12080

Merged
brminich merged 2 commits into
openucx:masterfrom
guy-ealey-morag:static-bitmap-index-check
Oct 19, 2026
Merged

brminich merged 2 commits into
openucx:masterfrom
guy-ealey-morag:static-bitmap-index-check

Conversation

@guy-ealey-morag

Copy link
Copy Markdown
Contributor

What?

Make UCS_STATIC_BITMAP_FOR_EACH_BIT fail the build when the loop index type can't hold the bitmap size, and add a gtest for the size check.

Why?

If the index type is too narrow for a given static bitmap size, that value wraps to 0 and we get an infinite loop.
This came up in #12054: the bitmap there is of size 256, which a uint8_t index can't hold.

Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

tomerg-nvidia
tomerg-nvidia previously approved these changes Oct 13, 2026
Comment thread src/ucs/datastruct/static_bitmap.h Outdated
Comment on lines +377 to +379
#define UCS_STATIC_BITMAP_CHECK_INDEX_TYPE(_bit_index, _bitmap) \
((void)sizeof( \
char[UCS_STATIC_BITMAP_INDEX_FITS(_bit_index, _bitmap) ? 1 : -1]))

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.

Suggested change
#define UCS_STATIC_BITMAP_CHECK_INDEX_TYPE(_bit_index, _bitmap) \
((void)sizeof( \
char[UCS_STATIC_BITMAP_INDEX_FITS(_bit_index, _bitmap) ? 1 : -1]))
#define UCS_STATIC_BITMAP_CHECK_INDEX_TYPE(_bit_index, _bitmap) \
(sizeof(char[UCS_STATIC_BITMAP_INDEX_FITS(_bit_index, _bitmap) ? 1 : -1]) \
!= 0)

then

#define _UCS_BITMAP_BITS_FOR_EACH_BIT(_bit_index, _bits, _num_words, _cond) \
    for (_bit_index = ucs_bitmap_bits_ffs(_bits, _num_words, 0); \
         (_cond) && (_bit_index < (_num_words) * UCS_BITMAP_BITS_IN_WORD); \
         _bit_index = ucs_bitmap_bits_ffs(_bits, _num_words, _bit_index + 1))

#define UCS_BITMAP_BITS_FOR_EACH_BIT(_bit_index, _bits, _num_words) \
    _UCS_BITMAP_BITS_FOR_EACH_BIT(_bit_index, _bits, _num_words, 1)

#define UCS_STATIC_BITMAP_FOR_EACH_BIT(_bit_index, _bitmap_ptr) \
    _UCS_BITMAP_BITS_FOR_EACH_BIT( \
        _bit_index, (_bitmap_ptr)->bits, \
        UCS_STATIC_BITMAP_NUM_WORDS(*(_bitmap_ptr)), \
        UCS_STATIC_BITMAP_CHECK_INDEX_TYPE(_bit_index, *(_bitmap_ptr)))

Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/static_bitmap.h Resolved
(_src_num_words));


/* Iterate over set bits while _cond holds */

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new _cond parameter is described as a loop condition, but its only purpose (and only use) is a compile-time assertion; the name invites a runtime predicate into a shared hot iteration macro. Maybe rename _cond to _check and note it must be a compile-time constant? The current name reads like a runtime loop condition, which would silently stop the iteration early.

@brminich
brminich merged commit b3a7e8b into openucx:master Oct 19, 2026
165 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants