Skip to content

fix: unaligned read for Metal buffers #882

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 1 commit into from
Jul 19, 2024
Merged

fix: unaligned read for Metal buffers #882

merged 1 commit into from
Jul 19, 2024

Conversation

Oppen
Copy link
Contributor

@Oppen Oppen commented Jul 18, 2024

fix: unaligned read on Metal implementation

After Rust 1.77 u128 starts requiring 16 bytes aligned values. This
breaks Metal, where retrieve_contents assumed it could just cast the
pointer and treat it as a slice.
Instead, we need to assume no alignment by doing the following:

  1. Read the i-th element from the buffer with
    ptr.add(i).read_unaligned();
  2. clone the read value, as the bitwise copy may cause aliasing
    otherwise;
  3. push the cloned value into the vector;
  4. mem::forget the element we read to avoid calling its drop
    implementation twice, one for the copy and one for the Buffer.

Type of change

  • Bug fix

@Oppen Oppen requested a review from a team as a code owner July 18, 2024 17:51
@codecov-commenter
Copy link

codecov-commenter commented Jul 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.03%. Comparing base (2a68c19) to head (16746a9).
Report is 12 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #882   +/-   ##
=======================================
  Coverage   72.03%   72.03%           
=======================================
  Files         149      149           
  Lines       33407    33407           
=======================================
  Hits        24066    24066           
  Misses       9341     9341           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Oppen Oppen force-pushed the fix/unaligned_read branch from 7d9b24f to 2c6ca3c Compare July 18, 2024 18:13
After Rust 1.77 `u128` starts requiring 16 bytes aligned values. This
breaks Metal, where `retrieve_contents` assumed it could just cast the
pointer and treat it as a slice.
Instead, we need to assume no alignment by doing the following:
1. Read the `i-th` element from the buffer with
   `ptr.add(i).read_unaligned()`;
2. `clone` the read value, as the bitwise copy may cause aliasing
   otherwise;
3. `push` the `clone`d value into the vector;
4. `mem::forget` the element we read to avoid calling its `drop`
   implementation twice, one for the copy and one for the `Buffer`.
@Oppen Oppen force-pushed the fix/unaligned_read branch from 2c6ca3c to 16746a9 Compare July 18, 2024 21:26
@juanbono juanbono added this pull request to the merge queue Jul 19, 2024
Merged via the queue into main with commit 07657e9 Jul 19, 2024
7 checks passed
@juanbono juanbono deleted the fix/unaligned_read branch July 19, 2024 10:46
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.

4 participants