Skip to content

Conversation

AlexJones0
Copy link

This PR fixes the comparator logic for sorting address ranges as used by the vmapper device.

The current logic uses the difference between the start and end addresses of each range, which are stored as uint32_t values (which is correct, as virtual address ranges can cover any valid 32 bit address). By subtracting the addresses in unsigned arithmetic we will underflow in the case that the first address is smaller than the second.

Furthermore, the conversion to a signed gint (int) means that for large address differences (e.g. consider a region starts at 0x00000000 and another at 0x80000001 or higher) the result will when cast overflow the 32-bit signed domain to become a negative value.

Replace this logic with comparisons that return simple unit sign values. This now performs the intended sorting without risking overflow.

This commit fixes the comparator logic for sorting address ranges as
used by the vmapper device.

The current logic uses the difference between the `start` and `end`
addresses of each range, which are stored as `uint32_t` values (which is
correct, as virtual address ranges can cover any valid 32 bit address).
By subtracting the addresses in unsigned arithmetic we will underflow in
the case that the first address is smaller than the second.

Furthermore, the conversion to a signed `gint` (`int`) means that for
large address differences (e.g. consider a region starts at `0x00000000`
and another at 0x80000001 or higher) the result will when cast
overflow the 32-bit signed domain to become a negative value.

Replace this logic with comparisons that return simple unit sign
values. This now performs the intended sorting without risking overflow.

Signed-off-by: Alex Jones <[email protected]>
Copy link

@rivos-eblot rivos-eblot left a comment

Choose a reason for hiding this comment

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

LGTM

@AlexJones0 AlexJones0 merged commit b0c3eab into lowRISC:ot-9.2.0 Oct 3, 2025
10 of 11 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.

3 participants