Skip to content

[SYCL] Prevent unintended sign extensions #15230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 5, 2024

Conversation

ianayl
Copy link
Contributor

@ianayl ianayl commented Aug 29, 2024

This is to resolve a Coverity hit, pointing out that a potentially unintended sign extension is possible:

  • SectionHeaderNum and SectionHeaderSize are unsigned i16.
  • By C++ promotion rules, SectionHeaderNum * SectionHeaderSize results in a signed i32 value.
  • However, SectionHeaderOffset is i64, so the i32 product is promoted to i64 via sign extension
  • If SectionHeaderNum * SectionHeaderSize results in a number greater than 0x7FFFFF (greatest signed i32 number), then the upper bits get set to 1, producing the wrong result.

This PR adds a cast to ensure the promotions do not result in unexpected sign extensions: While I recognize that the odds of SectionHeaderNum and SectionHeaderSize getting as big as sqrt(0x7FFFFFF) is rather unlikely, this change does not hurt or slow down the code, and is probably the safer thing to do anyway. If it is decided that this change is not necessarily, feel free to close the PR, and I will fix the Coverity triage to reflect the decision.

@ianayl ianayl requested a review from a team as a code owner August 29, 2024 18:26
@ianayl ianayl marked this pull request as draft August 31, 2024 04:06
@ianayl ianayl marked this pull request as ready for review August 31, 2024 20:39
@ianayl
Copy link
Contributor Author

ianayl commented Sep 3, 2024

@intel/unified-runtime-reviewers Friendly ping, thanks in advance!

@ianayl
Copy link
Contributor Author

ianayl commented Sep 5, 2024

Thanks Aaron for the review!

@intel/llvm-gatekeepers This is ready for merging, thanks!

@martygrant martygrant merged commit 1345ae0 into intel:sycl Sep 5, 2024
13 checks passed
AlexeySachkov pushed a commit to AlexeySachkov/llvm that referenced this pull request Nov 26, 2024
This is to resolve a Coverity hit, pointing out that a potentially
unintended sign extension is possible:

- `SectionHeaderNum` and `SectionHeaderSize` are unsigned i16.
- By C++ promotion rules, `SectionHeaderNum * SectionHeaderSize` results
in a signed i32 value.
- However, `SectionHeaderOffset` is i64, so the i32 product is promoted
to i64 via sign extension
- If `SectionHeaderNum * SectionHeaderSize` results in a number greater
than 0x7FFFFF (greatest signed i32 number), then the upper bits get set
to 1, producing the wrong result.

This PR adds a cast to ensure the promotions do not result in unexpected
sign extensions: While I recognize that the odds of `SectionHeaderNum`
and `SectionHeaderSize` getting as big as sqrt(0x7FFFFFF) is rather
unlikely, this change does not hurt or slow down the code, and is
probably the safer thing to do anyway. If it is decided that this change
is not necessarily, feel free to close the PR, and I will fix the
Coverity triage to reflect the decision.
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