Skip to content
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

Check for overflow during backend initialization (Cuda, HIP, SYCL) #6159

Merged
merged 11 commits into from
Jul 26, 2023

Conversation

cz4rs
Copy link
Contributor

@cz4rs cz4rs commented May 24, 2023

fixes #3805


  • refactor the code to avoid potential overflow when possible
  • check for overflow when initializing scratch flags / scratch space
    • note: the code will Kokkos::abort when an overflow is detected (which should be fine in this case since we cannot allocate that much memory anyways)

@cz4rs cz4rs changed the base branch from master to develop May 24, 2023 13:41
Copy link
Contributor

@masterleinad masterleinad left a comment

Choose a reason for hiding this comment

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

Please also fix the other backends that have the same code.

@fnrizzi
Copy link
Contributor

fnrizzi commented May 24, 2023

please make the description more descriptive

@cz4rs cz4rs marked this pull request as draft May 24, 2023 19:36
@cz4rs cz4rs marked this pull request as ready for review May 25, 2023 11:41
@cz4rs cz4rs requested a review from masterleinad May 25, 2023 11:41
Copy link
Contributor

@masterleinad masterleinad left a comment

Choose a reason for hiding this comment

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

Looks OK to me.

@cz4rs cz4rs force-pushed the fix-overflow branch 2 times, most recently from 425f765 to 9267043 Compare May 27, 2023 13:29
@cz4rs cz4rs requested review from dalg24 and masterleinad May 27, 2023 16:28
@masterleinad
Copy link
Contributor

Please update the title and description to describe the full scope of this pull request.

@cz4rs cz4rs changed the title Fix potential arithmetic overflow in Cuda backend Check for overflow during backend initialization (Cuda, HIP, SYCL) May 31, 2023
@cz4rs
Copy link
Contributor Author

cz4rs commented May 31, 2023

Please update the title and description to describe the full scope of this pull request.

Done 👌

@cz4rs cz4rs marked this pull request as draft May 31, 2023 19:29
@cz4rs cz4rs marked this pull request as ready for review June 23, 2023 15:11
@cz4rs cz4rs requested a review from dalg24 June 26, 2023 11:38
@cz4rs
Copy link
Contributor Author

cz4rs commented Jun 26, 2023

Looks like HIP failures are unrelated.

@Rombur
Copy link
Member

Rombur commented Jun 27, 2023

Retest this please

@cz4rs cz4rs marked this pull request as draft June 29, 2023 11:52
@cz4rs cz4rs marked this pull request as ready for review July 5, 2023 19:54
@cz4rs cz4rs requested review from dalg24 and masterleinad July 5, 2023 19:54
std::size_t alloc_size;
if (multiply_overflow(m_scratchFlagsCount, sizeScratchGrain, alloc_size)) {
Kokkos::abort("Arithmetic overflow detected.");
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't find this code intuitive. What about we do sth like

std::size_t alloc_size = multiply_overflow(m_scratchFlagsCount, sizeScratchGrain);

and we put the Kokkos::abort inside multiply_overflow. The code is easier to read and we don't need to repeat the Kokkos::abort everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See ffe1120.

@cz4rs cz4rs requested a review from Rombur July 22, 2023 21:47
} else {
return true;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Small follow up for someone: there are builtins we could use here: https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html

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.

arithmetic overflow potential
6 participants