pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Class constructor trips `misaligned pointer dereference`

Open lukaslueg opened this issue 11 months ago • 15 comments

Bug Description

Using 0.27.2, the constructor of a #[pyclass] immediately trips a rustc-injected check at pyo3-0.27.2/src/pyclass_init.rs:185:17: when compiled in debug mode. The problem can be reproduced locally (3.14) and on CI (3.9, 3.13, 3.14).

misaligned pointer dereference: address must be a multiple of 0x40 but is 0x105219430

I'm somewhat lost what to do here, since the constructor-call that is causing the problem is entirely pyo3-derived. I've pushed a branch that demonstrates the problem.

Steps to Reproduce

  1. Checkout https://github.com/lukaslueg/pyhyperminhash/tree/6f8774e373b86928433154f701f0acfac4d3f5b4
  2. Build using maturin in development mode
  3. Observe pytest -v or import pyhyperminhash; pyhyperminhash.Entry()

Backtrace

> pytest -v

tests/test_pyhyperminhash.py::test_from_iter PASSED                                                                                                                                                                    [ 73%]
tests/test_pyhyperminhash.py::test_entry_unhashable Fatal Python error: Aborted

Current thread 0x000000020683e080 (most recent call first):
  File "/Users/llueg/dev/pyhyperminhash/tests/test_pyhyperminhash.py", line 148 in test_entry_unhashable
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/python.py", line 166 in pytest_pyfunc_call
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/python.py", line 1720 in runtest
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 179 in pytest_runtest_call
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 245 in <lambda>
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 353 in from_call
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 244 in call_and_report
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 137 in runtestprotocol
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 118 in pytest_runtest_protocol
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/main.py", line 396 in pytest_runtestloop
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/main.py", line 372 in _main
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/main.py", line 318 in wrap_session
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/main.py", line 365 in pytest_cmdline_main
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 121 in _multicall
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pluggy/_hooks.py", line 512 in __call__
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/config/__init__.py", line 199 in main
  File "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/_pytest/config/__init__.py", line 223 in console_main
  File "/Users/llueg/dev/pyhyperminhash/.venv/bin/pytest", line 7 in <module>

Current thread's C stack trace (most recent call first):
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _Py_DumpStack+0x44 [0x10103c1fc]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at faulthandler_dump_c_stack+0x58 [0x10104ec9c]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at faulthandler_fatal_error+0x160 [0x10104eb60]
  Binary file "/usr/lib/system/libsystem_platform.dylib", at _sigtramp+0x38 [0x199e7b744]
  Binary file "/usr/lib/system/libsystem_pthread.dylib", at pthread_kill+0x128 [0x199e71888]
  Binary file "/usr/lib/system/libsystem_c.dylib", at abort+0x7c [0x199d76850]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std3sys3pal4unix4sync5mutex5Mutex4lock4fail17hfd38f3b0af37e16dE+0x0 [0x106e46e18]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std9panicking11panic_count17is_zero_slow_path17h0e49bc980a448302E+0x0 [0x106e476ec]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std9panicking15panic_with_hook17h93c775fc227522ddE+0x310 [0x106e20180]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std9panicking13panic_handler28_$u7b$$u7b$closure$u7d$$u7d$17h8561e58c9391724aE+0x90 [0x106e1fd78]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std3sys9backtrace4lock17hbd44f6947d8c1202E+0x0 [0x106e1e74c]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _RNvCsiGVaDesi5rv_7___rustc18___rdl_alloc_zeroed+0x0 [0x106e15900]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4core9panicking18panic_nounwind_fmt17hbccf1478dcdd0d51E+0x34 [0x106e4b4f4]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4core9panicking5panic17h4a3c64c65dc98033E+0x0 [0x106e4b6fc]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4pyo312pyclass_init27PyClassInitializer$LT$T$GT$27create_class_object_of_type17h5926d8e37bfd3ec2E+0x198 [0x106dcd724]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4pyo35impl_9pymethods11tp_new_impl17h98257976ae80db68E+0x24 [0x106dbbd10]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN14pyhyperminhash5Entry20__pymethod___new____17hcea4db44adc7e8cdE+0x178 [0x106dbe4cc]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4pyo35impl_10trampoline7newfunc28_$u7b$$u7b$closure$u7d$$u7d$17h3889011bc89d48b8E+0x34 [0x106dc59b8]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4pyo35impl_10trampoline10trampoline28_$u7b$$u7b$closure$u7d$$u7d$17h4526ddc3c5e82b96E+0x28 [0x106dc52c4]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std9panicking12catch_unwind7do_call17hd7f47029c2ca2a12E+0x44 [0x106dc95c0]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at __rust_try+0x20 [0x106dca42c]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN3std5panic12catch_unwind17he952f5781ccd8351E+0x54 [0x106dca098]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4pyo35impl_10trampoline10trampoline17hee2fb9e98ad73a59E+0xf0 [0x106dc4f44]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN4pyo35impl_10trampoline7newfunc17h535810fe2a1cc4c5E+0x44 [0x106db98d8]
  Binary file "/Users/llueg/dev/pyhyperminhash/.venv/lib/python3.14/site-packages/pyhyperminhash/pyhyperminhash.abi3.so", at _ZN14pyhyperminhash162_$LT$impl$u20$pyo3..impl_..pyclass..PyMethods$LT$pyhyperminhash..Entry$GT$$u20$for$u20$pyo3..impl_..pyclass..PyClassImplCollector$LT$pyhyperminhash..Entry$GT$$GT$10py_methods5ITEMS10trampoline17h1db992949ce13519E+0x30 [0x106dbd4e8]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at type_call+0x64 [0x100f0eba0]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyObject_MakeTpCall+0x78 [0x100e75400]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyEval_EvalFrameDefault+0x5b58 [0x100fac700]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyEval_Vector+0x10c [0x100fa68a4]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyObject_VectorcallDictTstate+0xc8 [0x100e752a4]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at call_method+0xcc [0x100f1848c]
  Binary file "/opt/homebrew/Cellar/[email protected]/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyObject_MakeTpCall+0x78 [0x100e75400]
  <truncated rest of calls>
[1]    3627 abort      RUST_BACKTRACE=full pytest -v
> python3 -c "import pyhyperminhash; pyhyperminhash.Entry()"
thread '<unnamed>' (8810098) panicked at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/pyclass_init.rs:185:17:
misaligned pointer dereference: address must be a multiple of 0x40 but is 0x105219430
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:698:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/panicking.rs:122:22
   2: core::panicking::panic_nounwind_fmt
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/intrinsics/mod.rs:2435:9
   3: core::fmt::rt::<impl core::fmt::Arguments>::new_v1_formatted
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/fmt/rt.rs:236:9
   4: core::panicking::panic_misaligned_pointer_dereference
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/panicking.rs:290:9
   5: pyo3::pyclass_init::PyClassInitializer<T>::create_class_object_of_type
             at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/pyclass_init.rs:185:17
   6: pyo3::impl_::pymethods::tp_new_impl
             at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/impl_/pymethods.rs:716:14
   7: pyhyperminhash::Entry::__pymethod___new____
             at ./src/lib.rs:46:1
   8: pyo3::impl_::trampoline::newfunc::{{closure}}
             at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/impl_/trampoline.rs:44:37
   9: pyo3::impl_::trampoline::trampoline::{{closure}}
             at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/impl_/trampoline.rs:190:54
  10: std::panicking::catch_unwind::do_call
             at /Users/llueg/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:590:40
  11: ___rust_try
  12: std::panicking::catch_unwind
             at /Users/llueg/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:553:19
  13: std::panic::catch_unwind
             at /Users/llueg/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  14: pyo3::impl_::trampoline::trampoline
             at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/impl_/trampoline.rs:190:9
  15: pyo3::impl_::trampoline::newfunc
             at /Users/llueg/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.27.2/src/impl_/trampoline.rs:44:21
  16: pyhyperminhash::<impl pyo3::impl_::pyclass::PyMethods<pyhyperminhash::Entry> for pyo3::impl_::pyclass::PyClassImplCollector<pyhyperminhash::Entry>>::py_methods::ITEMS::trampoline
             at ./src/lib.rs:46:1
  17: _type_call
  18: __PyObject_MakeTpCall
  19: __PyEval_EvalFrameDefault
  20: _PyEval_EvalCode
  21: _run_mod
  22: __PyRun_StringFlagsWithName
  23: __PyRun_SimpleStringFlagsWithName
  24: _Py_RunMain
  25: _pymain_main
  26: _Py_BytesMain
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
[1]    3419 abort      RUST_BACKTRACE=1 python3 -c "import pyhyperminhash; pyhyperminhash.Entry()"

Your operating system and version

macOS Tahoe

Your Python version (python --version)

3.14.2

Your Rust version (rustc --version)

1.92

Your PyO3 version

0.27.2

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

brew, gh-actions

Additional Info

No response

lukaslueg avatar Dec 24 '25 18:12 lukaslueg

Notice that hyperminhash::Entry has an alignment of 64. When boxing the inner value, the crash goes away. When manually #[repr(align(...))] back above ptr-size, the crash reappears.

lukaslueg avatar Dec 24 '25 19:12 lukaslueg

Hmm, are you able to build a minimal reproducer?

alex avatar Dec 24 '25 19:12 alex

Actually I guess your last message answers it: anytime the alignment requirement for T is sufficiently large, I think that might trigger it.

The question is: is using ptr::write_unaligned() sufficient here, or do we need something more?

alex avatar Dec 24 '25 19:12 alex

Here is a minimal reproducer:

use pyo3::prelude::*;

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

#[pymethods]
impl Aligned {
    #[new]
    fn new() -> Self {
        Self
    }
}

#[pymodule]
mod pyo3align {
    #[pymodule_export]
    use super::Aligned;
}

The above will crash when calling the constructor on 0.27.2

lukaslueg avatar Dec 24 '25 19:12 lukaslueg

Actually I guess your last message answers it: anytime the alignment requirement for T is sufficiently large, I think that might trigger it.

The question is: is using ptr::write_unaligned() sufficient here, or do we need something more?

@alex Afaics the problematic part is not the ptr::write() but the field-projection in (*part_init).contents. A assert!(part_init.is_aligned()); before the ptr::write fails. The access to .contents is UB.

lukaslueg avatar Dec 24 '25 19:12 lukaslueg

Yeah, that's right. The ptr::write() part is also wrong though :-)

alex avatar Dec 24 '25 20:12 alex

I guess the underlying problem is that CPython's allocator does not care about alignment beyond ptr-sized. This is fine for almost all native types and combinations thereof, but not fine for any Rust-type that has alignment requirements above and beyond ptr-sized, e.g. because it contains such a type.

lukaslueg avatar Dec 28 '25 10:12 lukaslueg

Yeah, that appears right.

It's a bit unclear what the right solution is though.

alex avatar Dec 28 '25 13:12 alex

Since we will hand out Rust-references to pyclass-objects, even if we snuck by pyclass_init, the moment a method gets called on a pyclass, we're back to UB.

Either pyo3 uses the Rust runtime // the global allocator (which takes care of business) to allocate memory for pyclass-objects, or the inner objects tp_basicsize get padded to the required size, and the Rust-object manually offset within that padded size on every access.

lukaslueg avatar Dec 28 '25 14:12 lukaslueg

I have been struggling to reproduce the panic. When I build, I seem to be getting Python objects which calculate a size which includes the alignment.

davidhewitt avatar Jan 18 '26 13:01 davidhewitt

@davidhewitt, the problem as far as I can see is not the size but the fact that the underlying allocator only guarantees alignment for fundamental C-types, which is either 8 or 4. In pyo3align, the type requires a 32-byte alignment, which the allocator does not care about. If you are lucky to get an allocation that happens to be aligned by 32, nothing happens.

lukaslueg avatar Jan 18 '26 16:01 lukaslueg

Indeed, though for some reason the objects I am getting seem to have an appropriate alignment for their size. I will continue testing, as it seems that we do have a problem here that needs a concrete solution.

davidhewitt avatar Jan 19 '26 10:01 davidhewitt

@davidhewitt #[repr(align(4096))] struct Aligned; should raise your chances :-)

Somewhat related

lukaslueg avatar Jan 19 '26 14:01 lukaslueg

One thing we could easily do is simply refuse to work with structs that have a large alignment. Right now they simply cannot be correct.

The other option is to round up the size of the struct we give CPython, and then read at an offset into there based on the address. Second one has a few instructions of overhead, but should work.

@davidhewitt do you have a view? (I just almost hit this with some code I was writing)

alex avatar Jun 15 '26 01:06 alex

I hit this via #6526 and have a branch that takes the "refuse large alignment" route alex suggested above: https://github.com/devtechedge/pyo3/tree/pyclass-overalign-reject

It adds a const assert to BASIC_SIZE for both PyStaticClassObject and PyVariableClassObject, rejecting alignment above 16 bytes (8 on 32-bit, matching pymalloc's ALIGNMENT), with an error message suggesting boxing the over-aligned field.

It comes with a UI test, a positive align(16) test, and a short note in the class guide.

One caveat is that it is a post-monomorphization error, so cargo check will not show it; emitting the assert from the #[pyclass] macro instead would fix that if you prefer.

Would you take a PR along these lines, or would you rather support over-aligned classes by offsetting into a larger allocation?

devtechedge avatar Oct 14 '26 15:10 devtechedge