PyO3 / PyO3/pyo3

Class constructor trips `misaligned pointer dereference`

Open
#5,714 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

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/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _Py_DumpStack+0x44 [0x10103c1fc]
  Binary file "/opt/homebrew/Cellar/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at faulthandler_dump_c_stack+0x58 [0x10104ec9c]
  Binary file "/opt/homebrew/Cellar/python@3.14/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/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at type_call+0x64 [0x100f0eba0]
  Binary file "/opt/homebrew/Cellar/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyObject_MakeTpCall+0x78 [0x100e75400]
  Binary file "/opt/homebrew/Cellar/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyEval_EvalFrameDefault+0x5b58 [0x100fac700]
  Binary file "/opt/homebrew/Cellar/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyEval_Vector+0x10c [0x100fa68a4]
  Binary file "/opt/homebrew/Cellar/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at _PyObject_VectorcallDictTstate+0xc8 [0x100e752a4]
  Binary file "/opt/homebrew/Cellar/python@3.14/3.14.2/Frameworks/Python.framework/Versions/3.14/Python", at call_method+0xcc [0x100f1848c]
  Binary file "/opt/homebrew/Cellar/python@3.14/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the linked pyhyperminhash branch with maturin in development mode, then run tests/test_pyhyperminhash.py or import pyhyperminhash and construct Entry(). Read pyo3-0.27.2/src/pyclass_init.rs around line 185 and compare the constructor backtrace with the failing debug build. Done means the Entry constructor no longer aborts with a misaligned pointer dereference across the reported Python versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.