BUG: np.linalg.qr corrupts the heap on macOS 26 arm64 with Accelerate (dgeqrf writes past its own scratch buffer)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 12/100
調査の方向性
The bug originates in Apple's Accelerate dgeqrf on macOS 26 arm64, not in numpy's own code, so there is no obvious numpy file to edit. Start from the reproducer: run the C program dgeqrf_overrun.c under libgmalloc at 512 and 768 to confirm the crash. Done would mean a documented guard or a size-based workaround in numpy's Accelerate path, which needs maintainer agreement on the approach.
索引モデルが issue の本文から書いたものです。
説明
Describe the issue:
On macOS 26 (arm64), np.linalg.qr with the Accelerate backend corrupts the heap for some matrix sizes. The bug is in Accelerate's dgeqrf, not in numpy: a plain C call to dgeqrf_ crashes the same way with no Python involved. I'm filing here as well because np.linalg.qr is the most common way people will hit it, and the symptom is random crashes far from the call.
What we saw: test workers that built a 768 x 768 random rotation with np.linalg.qr crashed at random later in unrelated code: SIGSEGV on garbage pointers, or the allocator trapping on a corrupted freelist (EXC_BREAKPOINT in libsystem_malloc). Under libgmalloc the fault is inside dgeqrf. With lldb we traced it to a scratch buffer that dgeqrf allocates itself: for 768 x 768 it asks for 942,080 bytes and writes 163,840 bytes past the end, on every call and with one thread. The lwork passed in is the size the workspace query returned, so this isn't a caller workspace problem.
Sizes, as LAPACK m x n, checked under libgmalloc with the C reproducer below:
- Square: bad from 544 to 927, except 896. 543, 896 and 928 are clean.
- Rectangular: 768x300, 768x600, 600x768 and 768x1536 are bad; 300x768 and 1536x768 are clean.
- float32 input to
np.linalg.qris affected too, since numpy computes it in float64.
Without libgmalloc the call returns info=0 and a correct-looking result, so the corruption is silent until something else touches that memory.
Our workaround was a Householder QR written in numpy, which avoids dgeqrf entirely. I'm also reporting this to Apple through Feedback Assistant.
Reproduce the code example:
# Run with: DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib python repro.py
import numpy as np
np.linalg.qr(np.random.default_rng(0).standard_normal((512, 512)))
print("512 ok")
np.linalg.qr(np.random.default_rng(0).standard_normal((768, 768))) # segfaults under libgmalloc
print("768 ok")
The same bug in C, with no numpy:
/* clang -O1 -o dgeqrf_overrun dgeqrf_overrun.c -framework Accelerate
DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib ./dgeqrf_overrun 768 */
#include <stdio.h>
#include <stdlib.h>
extern void dgeqrf_(int *m, int *n, double *a, int *lda, double *tau,
double *work, int *lwork, int *info);
int main(int argc, char **argv) {
int m = argc > 1 ? atoi(argv[1]) : 768;
int n = argc > 2 ? atoi(argv[2]) : m;
int lda = m, info = 0, lwork = -1;
double query = 0.0;
double *a = malloc(sizeof(double) * (size_t)m * (size_t)n);
double *tau = malloc(sizeof(double) * (size_t)(m < n ? m : n));
srand(1);
for (size_t i = 0; i < (size_t)m * (size_t)n; i++)
a[i] = (double)rand() / RAND_MAX - 0.5;
dgeqrf_(&m, &n, a, &lda, tau, &query, &lwork, &info); /* workspace query */
lwork = (int)query;
double *work = malloc(sizeof(double) * (size_t)lwork);
dgeqrf_(&m, &n, a, &lda, tau, work, &lwork, &info);
printf("m=%d n=%d lwork=%d info=%d\n", m, n, lwork, info);
free(work); free(tau); free(a);
return info != 0;
}
Error message:
$ DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib ./dgeqrf_overrun 512
m=512 n=512 lwork=16384 info=0
$ DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib ./dgeqrf_overrun 768
zsh: segmentation fault (exit 139)
$ ./dgeqrf_overrun 768
m=768 n=768 lwork=24576 info=0
Python and NumPy Versions:
numpy 2.4.6, Python 3.12.12 (also seen on 3.11.14), BLAS and LAPACK: accelerate (numpy.show_config).
Runtime Environment:
macOS 26.6.2 (25G83), arm64.
Context for the issue:
Silent heap corruption in a common linear algebra call. Anyone building random orthogonal matrices or doing QR at these sizes on current macOS can get crashes that point somewhere else entirely. A note in the docs or a check in numpy's Accelerate path might save people a lot of debugging until Apple ships a fix.
- 主要言語
- Python
- スター
- 33.5k
- フォーク
- 13.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 143
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
numpy/numpy のほかの issue
-
00 - Bug component: numpy.linalg
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
00 - Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
00 - Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
numpy/numpy#33422 · コメント 6 件 ·
メンテナーはふだん 1 日以内に返信
-
BUG: f2py treats a backslash as an escape inside character literals対応中かも @HaoZeke が 17 日前に担当しました。 オープン00 - Bug component: numpy.f2py
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
numpy/numpy#33220 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
DOC: use CITATION.cff file format for citation information対応中かも @JaroCamphuijsen が 58 日前に担当しました。 オープン04 - Documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
numpy/numpy#32817 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
521xueweihan/HelloGitHub#4543 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
emeryberger/CSrankings#15700 ·
メンテナーはふだん 2 日以内に返信
-
Bug | 工作不正常 Triage | 待检查
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
OneDragon-Anything/ZenlessZoneZero-OneDragon#3084 ·
メンテナーはふだん 1 日以内に返信
-
Task select patterns with uppercase letters never match対応中かも @manobhisriram が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信