Skip to content

pyclass: reject unsupported alignment - #6571

Open
JMak-Security wants to merge 6 commits into
PyO3:mainfrom
JMak-Security:fix/reject-overaligned-pyclasses-noreply
Open

JMak-Security wants to merge 6 commits into
PyO3:mainfrom
JMak-Security:fix/reject-overaligned-pyclasses-noreply

Conversation

@JMak-Security

Copy link
Copy Markdown
Contributor

Summary

  • Reject #[pyclass] layouts whose alignment exceeds the guarantee of Python's object allocator (16 bytes on 64-bit, 8 bytes on 32-bit) for both static and variable layouts.
  • Add a compile-fail example, a test for the maximum supported alignment, and guide documentation recommending Box for over-aligned data.

This prevents undefined behavior when Rust references an object Python allocated at an insufficiently aligned address. Addresses #5714 and duplicate report #6526.

Validation

  • cargo fmt --all -- --check — passed
  • git diff --check — passed
  • Standalone rustc const-evaluation check — 16-byte alignment accepted and 32-byte alignment rejected on 64-bit
  • The targeted Cargo test could not run here because the Windows environment lacks the MSVC linker (link.exe) and Windows SDK libraries.

JMak-Security and others added 6 commits November 8, 2026 21:31
Python's object allocator does not guarantee alignment beyond 16 bytes on 64-bit platforms or 8 bytes on 32-bit platforms. Reject unsupported layouts at compile time and document boxing over-aligned data.

Co-authored-by: Copilot <[email protected]>
Reflow the alignment guidance to satisfy rumdl and add the required fixed changelog fragment.

Co-authored-by: Copilot <[email protected]>
Use the maximum alignment guaranteed by Python's allocator in the disjoint-base test fixture, and lower its base field alignment so the padding assertion remains meaningful.

Co-authored-by: Copilot <[email protected]>
Exercise the allocator-alignment guard's rejecting branch at runtime so the new safety check is covered by the test suite.

Co-authored-by: Copilot <[email protected]>
Exercise the allocator-alignment check's passing branch in the unit test so the new guard is fully covered.

Co-authored-by: Copilot <[email protected]>
Avoid relying on the runner's system Python version, which changed with ubuntu-latest and broke the FFI macro checks.

Co-authored-by: Copilot <[email protected]>

This branch has not been deployed

No deployments
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.

1 participant