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

Ensure pointer is 8-byte aligned in stack_alloc::alloc #3014

Merged
merged 4 commits into from
Jan 27, 2024

Conversation

WardBrian
Copy link
Member

Summary

This was originally found using UBSAN on the csr_matrix_times_vector function: stan-dev/rstan#1111

The issue there was that that function places u, which is std::vector<int>, in our memory arena. If u contained an odd number of elements (e.g., 3 in the above issue), this resulted in a call to alloc with a len of 12.

The next call to alloc would then return a pointer which was not 8-byte aligned, which is undefined behavior. On x86 this is relatively benign, but on other platforms this could be either very slow or cause processor exceptions.

Tests

I have updated stack_alloc_test

Side Effects

This will add a few unused bytes (always less than 8) to allocations of types that sizeof(T) % 8 !=0. Additionally, a bit of extra math is needed in the alloc function, but @SteveBronder and I checked that this gets compiled down to just a and, sub, and cmov

Release notes

Fixed the stack_allocator being able to return non-8-byte aligned pointers

Checklist

  • Copyright holder: (fill in copyright holder information)

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

Copy link
Collaborator

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

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

At some point I think we should come back and rewrite some of the memory things here so that we can request arbitrary alignments, but I think good!

@syclik syclik merged commit 9b6bc3d into develop Jan 27, 2024
8 checks passed
@syclik syclik deleted the fix/stack-alloc-unaligned-pointer branch January 27, 2024 19:26
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