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

Fix undefined behavior in integer advanced indexing and indexing functions #1894

Merged
merged 7 commits into from
Nov 15, 2024

Conversation

ndgrigorian
Copy link
Collaborator

@ndgrigorian ndgrigorian commented Nov 15, 2024

This PR proposes a solution to undefined behavior that could occur in some edge cases with integer advanced indexing, where indices OOB for ssize_t (aka std::ptrdiff_t) would be cast directly to ssize_t and overflow or underflow.

As ssize_t/std::ptrdiff_t is defined to be a signed type with the same size as size_t, this means that on 32-bit systems, overflow/underflow could occur for even smaller values.

This PR also re-organizes integer_advanced_indexing.hpp by reducing namespace clutter, and moves the rewritten ClipIndex and WrapIndex structs into a separate header file. This enables them to be re-used more easily in extensions.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?
  • If this PR is a work in progress, are you opening the PR as a draft?

…ent `ssize_t`

Previously, indices were directly cast to `ssize_t` before being clipped or wrapped, causing values to overflow or underflow and giving unreliable results

ClipIndex and WrapIndex remade as structs which check the bounds of `ssize_t` against the bounds of the indices type then choose to cast to the appropriate type, before performing clipping and/or wrapping

ClipIndex and WrapIndex structs have also been moved to a separate header file, `libtensor/include/utils/indexing_utils.hpp`
Moved common constexpr variables out of branches.

Replaced `static constexpr` with `constexpr`. Since these are
defined in procedure scope, `static` is not required.

Introduced typed temporary variables, so that type deduction
for `sycl::min`, `sycl::max`, `sycl::clamp` can work and removed
explicit use of their template parameter.

Added explicit static_cast on value of `projected` variable
computed as IndT type.
This is possible because ProjectorT is literal type (no state and
default constructor).
@oleksandr-pavlyk oleksandr-pavlyk mentioned this pull request Nov 15, 2024
8 tasks
Copy link

github-actions bot commented Nov 15, 2024

Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞

Copy link

Array API standard conformance tests for dpctl=0.19.0dev0=py310hdf72452_209 ran successfully.
Passed: 894
Failed: 1
Skipped: 119

@coveralls
Copy link
Collaborator

coveralls commented Nov 15, 2024

Coverage Status

coverage: 87.705%. remained the same
when pulling ba0bfd9 on fix-indexing-oob-indices
into dd2812f on master.

@ndgrigorian ndgrigorian marked this pull request as ready for review November 15, 2024 19:22
Copy link
Collaborator

@oleksandr-pavlyk oleksandr-pavlyk left a comment

Choose a reason for hiding this comment

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

LGMT! Thank you for fixing this and adding tests @ndgrigorian

Copy link

Array API standard conformance tests for dpctl=0.19.0dev0=py310hdf72452_212 ran successfully.
Passed: 894
Failed: 1
Skipped: 119

@ndgrigorian ndgrigorian merged commit 2a4714f into master Nov 15, 2024
50 of 51 checks passed
@ndgrigorian ndgrigorian deleted the fix-indexing-oob-indices branch November 15, 2024 21:30
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