array_t::at() returns a misaligned reference when the stride is not a multiple of alignof(T)

オープン
#6,309 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

@henryiii がすでに取り組んでいます。

2026年11月25日 から。

  • #6311 @henryiii による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
cpp, python
領域
api, data

調査の方向性

The misaligned reference comes from array_t::at() and mutable_at() in include/pybind11/numpy.h, around line 1526. Start with the element address calculation there, then run test_at_stride_not_multiple_of_itemsize under the UBSan CI job, which currently deselects it. Done means the test passes under UBSan without the deselect, and either a throw for misaligned elements or an alignment check at array creation has been chosen and implemented.

索引モデルが issue の本文から書いたものです。

説明

🤖 AI text below 🤖

Since #6241, array_t<T>::at() and mutable_at() support strides that are not a multiple of sizeof(T). They return a T & to the element. If the stride is not a multiple of alignof(T), that reference is misaligned. This is undefined behavior. It works on x86 and arm64, but it can fail on platforms that require aligned access, and UBSan reports it:

include/pybind11/numpy.h:1526: runtime error: reference binding to misaligned address 0x00000a5cf433 for type 'const short unsigned int', which requires 2 byte alignment

Reproducer (from test_at_stride_not_multiple_of_itemsize):

packed = np.zeros(3, dtype=[("a", np.uint16), ("b", np.uint8)])
m.at_t(packed["a"], 1)  # stride 3, element 1 is at an odd address

A reference cannot point to a misaligned T. Possible fixes:

  • Throw when the element address is not aligned for T.
  • Throw when the array is not aligned (NPY_ARRAY_ALIGNED), possibly as a check when the array_t is created.

The UBSan CI job deselects this test until this is fixed.

主要言語
C++
スター
18k
フォーク
2.3k
平均マージ
5日 7時間
マージ済み PR(30日)
53

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pybind/pybind11 のほかの issue

pybind/pybind11 の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。