pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Undefined behavior with a pyclass w/ >16 byte alignment

Open acshi opened this issue 1 month ago • 4 comments

Bug Description

Python apparently provides only 16-byte alignment, so any repr(align) greater than that will be UB.

The included snippet, when debug-assertions = true, panics with "misaligned pointer dereference: address must be a multiple of 0x20 but is ..."

Workaround: box any pyclass struct/field that needs greater than 16-byte alignment

Steps to Reproduce

Run with cargo +nightly -Zscript

---
[dependencies]
pyo3 = { version = "=0.29.3", features = ["auto-initialize"] }

[profile.dev]
debug-assertions = true # Will not panic when set to false
---
use pyo3::prelude::*;

#[pyclass]
#[repr(align(32))]
struct Aligned;

fn main() {
    Python::attach(|py| Py::new(py, Aligned).unwrap());
}

Backtrace

thread 'main' (9163979) panicked at /Users/acshi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.29.3/src/pyclass_init.rs:189:18:
misaligned pointer dereference: address must be a multiple of 0x20 but is 0x103707010
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/822182db4c4a1827256881d8c7aa685f0bcf2b11/library/std/src/panicking.rs:676:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/822182db4c4a1827256881d8c7aa685f0bcf2b11/library/core/src/panicking.rs:122:22
   2: core::panicking::panic_nounwind_fmt
             at /rustc/822182db4c4a1827256881d8c7aa685f0bcf2b11/library/core/src/intrinsics/mod.rs:2731:9
   3: core::panicking::panic_misaligned_pointer_dereference
             at /rustc/822182db4c4a1827256881d8c7aa685f0bcf2b11/library/core/src/panicking.rs:284:5
   4: <pyo3::pyclass_init::PyClassInitializer<pyo3_align_repro::Aligned>>::create_class_object_of_type
             at /Users/acshi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.29.3/src/pyclass_init.rs:189:18
   5: <pyo3::pyclass_init::PyClassInitializer<pyo3_align_repro::Aligned>>::create_class_object
             at /Users/acshi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.29.3/src/pyclass_init.rs:139:23
   6: <pyo3::instance::Bound<pyo3_align_repro::Aligned>>::new::<pyo3_align_repro::Aligned>
             at /Users/acshi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.29.3/src/instance.rs:102:22
   7: <pyo3::instance::Py<pyo3_align_repro::Aligned>>::new::<pyo3_align_repro::Aligned>
             at /Users/acshi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.29.3/src/instance.rs:1531:9
   8: pyo3_align_repro::main::{closure#0}
             at ./pyo3_align_repro.rs:15:25
   9: <pyo3::marker::Python>::attach::<pyo3_align_repro::main::{closure#0}, pyo3::instance::Py<pyo3_align_repro::Aligned>>
             at /Users/acshi/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.29.3/src/marker.rs:419:9
  10: pyo3_align_repro::main
             at ./pyo3_align_repro.rs:15:5
  11: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /rustc/822182db4c4a1827256881d8c7aa685f0bcf2b11/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.

Your operating system and version

macOS 26.6 (25G72)

Your Python version (python --version)

Python 3.14.8

Your Rust version (rustc --version)

rustc 1.101.0-nightly (822182db4 2026-10-13)

Your PyO3 version

0.29.3

How did you install python? Did you use a virtualenv?

brew without venv

Additional Info

No response

acshi avatar Oct 14 '26 13:10 acshi

Thanks for the clear repro, the debug-assertions abort makes the UB easy to see.

CPython only guarantees 16-byte alignment from tp_alloc and PyObject_GetTypeData, so a 32-byte-aligned PyClassObjectContents lands misaligned in create_class_object_of_type.

Would a compile-time assert in the layout's BASIC_SIZE (both PyStaticClassObject and PyVariableClassObject) that rejects alignment above 16 bytes be acceptable, with the boxing workaround in the error message and the guide?

Happy to put that together with a UI test, or to go a different way if you would rather support over-aligned classes.

devtechedge avatar Oct 14 '26 14:10 devtechedge

Duplicate of https://github.com/PyO3/pyo3/issues/5714

bschoenmaeckers avatar Oct 14 '26 15:10 bschoenmaeckers

Ahh yes, sorry I hadn't managed to find that original finding of this issue.

I agree that an appropriately-placed assert and error message would be a good solution here. Personally, I have no need for over-aligned pyclasses, when knowing to Box is enough.

acshi avatar Oct 14 '26 15:10 acshi

Thanks, no worries, it was easy to miss.

I've written up the assert approach with a branch on #5714 (https://github.com/PyO3/pyo3/issues/5714#issuecomment-6172264628) and will follow up there.

devtechedge avatar Oct 14 '26 15:10 devtechedge