Skip to content

Fix work buffer overflow in single-threaded lower POTRF for large N - #6295

Open
ChrisLundquist wants to merge 2 commits into
OpenMathLib:developfrom
ChrisLundquist:potrf-l-single-overflow
Open

ChrisLundquist wants to merge 2 commits into
OpenMathLib:developfrom
ChrisLundquist:potrf-l-single-overflow

Conversation

@ChrisLundquist

Copy link
Copy Markdown

With one thread, ?potrf with uplo = 'L' can write past the end of its work area once n - GEMM_Q > REAL_GEMM_R. In lapack/potrf/potrf_L_single.c the "first tile" loop walks the remaining rows GEMM_P at a time and copies the block that starts inside the tile whole, so up to GEMM_P - 1 rows more than sb2 has room for are packed. Since #5988 (REAL_GEMM_R = GEMM_R - GEMM_PQ) there is no slack left behind sb2 for them. potrf_U_single.c packs only what fits and is not affected.

It is easy to reach with BUFFERSIZE=20 builds such as the scipy-openblas wheels: with the Zen 4/5 AVX-512 parameters (P = Q = 512, R = 7664) from about n = 7680, and with the Intel SKYLAKEX parameters (192, 384, 10704) from about n = 10730. The symptom is a SIGSEGV in dgemm_otcopy_SKYLAKEX called from dpotrf_L_single, heap corruption, or silent corruption, depending on what is mapped behind the work area; it does not depend on the matrix. Seen with numpy.linalg.cholesky on an 8008 x 8008 matrix with OPENBLAS_NUM_THREADS=1 on a Zen 5 CPU. The v0.3.34 release is not affected; develop is. (The threaded path had a separate overflow, fixed in #6235.)

Fix: pack only the rows of the tile, min(min_i, j + bk + min_j - is). The SYRK kernel that follows never reads the others.

Test: utest/test_potrf_workarea.c calls the single-threaded driver with its own work area followed by a zero tail and checks that the tail stays untouched and the factor is right. It needs 0.5 to 1 GB on BUFFERSIZE=20 builds and skips itself where the sizes would need more than 8 GB (default builds).

Tested (DYNAMIC_ARCH=1 INTERFACE64=1 BUFFERSIZE=20 NOFORTRAN=1, Zen 5): on develop edd6ae0 plus these two commits, make tests passes (utest 178/178 including the new potrf:single_work_area_bound, utest_ext 1563/1563, and the BLAS/CBLAS tests). A standalone reproducer passes for n = 8008 and 11440 with 1 and 2 threads, lower and upper, and with the Intel block sizes (forced with OPENBLAS_L2_SIZE=2048) at n = 11000. Before the fix the reproducer faults with one thread at n = 8008 (Zen 4/5 sizes) and n = 11000 (Intel sizes), and the new test fails.

Not tested: the Fortran LAPACK test suite (no Fortran compiler on the test host) and the CMake build of the new test (the CMake lines were written by analogy with test_syrk_thread_workarea.c).

This fix and test were prepared with AI assistance (Claude); both commits carry a Co-Authored-By trailer. A self-contained C reproducer (synthetic matrix, an mmap interposer that puts a guard region behind each work area so the overflow faults deterministically) is available if useful.

🤖 Generated with Claude Code

ChrisLundquist and others added 2 commits November 30, 2026 22:24
potrf_L_single packs the rows of the first tile of the trailing panel
behind sb2 while it walks over all remaining rows GEMM_P at a time. The
block of rows that starts inside the tile was copied whole, so up to
GEMM_P - 1 rows more than the REAL_GEMM_R that sb2 has room for were
written. Since REAL_GEMM_R is GEMM_R - GEMM_PQ instead of
GEMM_R - 2 * GEMM_PQ (146beed) there is no room left for them, and
for n - GEMM_Q > REAL_GEMM_R the driver wrote past the end of the
BUFFER_SIZE work area: by 64 kB with the SKYLAKEX parameters
(192, 384) and BUFFERSIZE=20, and by up to 12 kB with the Zen 4/5
parameters (512, 512), where the rows fit exactly and only the
rounding of sb2 to GEMM_ALIGN is past the end.

Pack only the rows of the tile. The SYRK kernel never read the others.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Calls dpotrf_L_single with a work area of its own, followed by memory
that must stay zero, at the smallest size that packs a full first tile.
The matrix is chosen so that the driver stops after one update of the
trailing matrix. Skipped where that size needs more than 1 GB or more
than OPENBLAS_UTEST_TIME_LIMIT seconds (the size grows with BUFFER_SIZE;
with BUFFERSIZE=20 it is about 10^4 and the test takes under a second).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant