BUG: np.linalg.qr corrupts the heap on macOS 26 arm64 with Accelerate (dgeqrf writes past its own scratch buffer)

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

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

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
12/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
c, macos, numpy, python
領域
data, devtools

調査の方向性

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.qr is 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

環境構築

はじめの一歩

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

numpy/numpy のほかの issue

numpy/numpy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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