Skip to content

Fix workspace overruns and a leak in LAPACKE high-level wrappers - #1486

Merged
langou merged 8 commits into
Reference-LAPACK:masterfrom
rmlarsen:lapacke-workspace-overruns
Nov 21, 2026
Merged

langou merged 8 commits into
Reference-LAPACK:masterfrom
rmlarsen:lapacke-workspace-overruns

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

After #1484 I audited the workspace that the LAPACKE high-level wrappers allocate against what the LAPACK routines document and use. Six wrapper families write past their buffers, or hand LAPACK a NULL workspace, one leaks memory, and one copies its row-major input with the wrong shapes:

wrappers allocated needed
LAPACKE_?pteqr, compz = 'n' WORK(1) WORK(4*N) (?LASQ1 via ?BDSQR)
LAPACKE_?sytri2x, LAPACKE_?hetri2x N+NB+1 (N+NB+1)*(NB+3)
LAPACKE_?gemqrt, side = 'l' M*NB N*NB
LAPACKE_{c,z}gejsv IWORK(MAX(3,M+2*N)) at least 4 (IWORK(1:4) is always set)
LAPACKE_?tgsna, job = 'e' WORK = NULL WORK(N)
LAPACKE_?sysvxx, LAPACKE_?hesvxx 4*N (real), 2*N (complex) 5*N (passed to ?SYTRF/?HETRF)
LAPACKE_?{sb,hb}evx_2stage, row-major leaks the copy of AB on every call
LAPACKE_?gemqrt_work, row-major copies V as ldv-by-k and T as ldt-by-nb nrows_v-by-k and nb-by-k

This PR builds on #1484 (its first commit). The new tests detect writes past the end through the guard that #1484 adds to the test allocator, and both PRs add routines to the same list in LAPACKE/testing/high_level/CMakeLists.txt. Once #1484 is merged I will rebase this PR onto master.

Description and fix

One commit per family; the commit messages give the details.

  • ?pteqr: with COMPZ = 'N', ?PTEQR computes the eigenvalues with ?LASQ1 (through ?BDSQR with NRU = 0), which uses the documented WORK(4*N). LAPACKE allocated one entry, so every call with compz = 'n' and n >= 2 overran it. The wrapper now allocates 4*N for every compz.
  • ?sytri2x/?hetri2x: the allocation MAX(1,n+nb+1)*(+1) lost the factor NB+3. The routines keep the inverted diagonal blocks in columns NB+2 and NB+3, so every call overran it, by 528 bytes past a 112-byte buffer for n = 10, nb = 3.
  • ?gemqrt: the wrapper always allocated M*NB, but ?GEMQRT uses LDWORK = N for side = 'L'.
  • {c,z}gejsv: the routines set IWORK(1:4), also in their quick return, but a 1-by-1 matrix got 3 entries.
  • ?tgsna: for job = 'e' the wrapper passed the queried lwork with WORK = NULL. ?TGSNA uses WORK for JOB = 'E', and only IWORK is unreferenced there. Every call with job = 'e' crashed.
  • ?sysvxx/?hesvxx: ?HETRF uses all 5*N entries for N > 64, so LAPACKE_{c,z}hesvxx overran WORK, by 4800 bytes past a 3200-byte buffer for n = 100. ?SYSVXX makes the same 5*N promise to ?SYTRF, which uses unblocked code at that size with the reference ILAENV. The wrappers now allocate 5*N, and ?SYSVXX documents WORK(5*N), as ?HESVXX does. These routines are XBLAS-only.
  • ?{sb,hb}evx_2stage_work: in row-major layout the query branch returned after allocating the transposed copies. The query now comes first, as in the other _work functions.
  • ?gemqrt_work: in row-major layout V and T were copied with the wrong shapes. With the natural ldv = k, ?GEMQRT rejected the copy of V, and the reference XERBLA stopped the program. With k > nb it read T past the end of its copy and returned wrong results. The copies now use leading dimensions nrows_v and nb, the leading dimension of T is checked against k, and an invalid side is rejected before anything is copied. This follows ?tpmqrt_work, which received the same fix in 2021-2022.

Regression tests

New high-level tests gejsv, gemqrt, pteqr, sbevx_2stage, sytri2x and tgsna, in all four precisions, run in the eight LAPACKE-high-level CTest entries. In the complex precisions, sytri2x also covers ?hetri2x and sbevx_2stage covers ?hbevx_2stage. Each test makes the calls that trigger the bug in both layouts, and checks the allocation failure paths, the allocation counts and an invalid matrix_layout.

The gemqrt test also applies Q with K = 3 > NB = 2 to the same matrices in both layouts and requires the same result.

With the wrappers of master, the test allocator reports the writes past the end for pteqr, sytri2x, gemqrt and (complex) gejsv, and the row-major gemqrt results differ. The row-major sbevx_2stage allocation counts are off, and the runner reports the leaked allocations. tgsna crashes the runner with SIGSEGV. ?sysvxx/?hesvxx are built only with XBLAS and are not tested here. I checked them with the XBLAS-only sources compiled against empty BLAS_*_X stubs.

Test suite

The full test suite passes, configured as in CI (CBLAS=ON, LAPACKE=ON, LAPACKE_WITH_TMG=ON) with the _64 API. All 313 CTest entries pass, with 5739011 LAPACK tests, 11973112 LAPACKE tests, 13539 BLAS tests and 645492 CBLAS tests for each API, and 0 numerical errors and 0 other errors. The builds used GNU Fortran and GCC 15.2.0 with CMAKE_BUILD_TYPE=Release, and the tests ran on x86-64 Linux build workers.

Not changed here

Checklist

  • The documentation has been updated. (WORK of ?SYSVXX.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue.)

rmlarsen and others added 8 commits November 8, 2026 22:53
LAPACKE_?pteqr allocated a WORK of one entry for compz = 'n' and
4*n-4 entries otherwise. ?PTEQR documents WORK(4*N) for every COMPZ:
with COMPZ = 'N' it calls ?BDSQR with NRU = 0, which computes the
singular values with ?LASQ1, and ?LASQ1 uses all 4*N entries. Every call
with compz = 'n' and n >= 2 therefore writes past the end of the
one-entry buffer: LAPACKE_dpteqr with n = 8 writes 248 bytes past it.
LAPACKE now allocates MAX(1,4*n) entries for every compz.

Test: a new high-level test, pteqr, calls LAPACKE_?pteqr with compz =
'N' and 'I' in both layouts and checks the allocation failure paths,
the allocation counts and an invalid matrix_layout. On the parent
commit the test allocator reports the write past the end of the
one-entry WORK in both layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LAPACKE_?sytri2x and LAPACKE_?hetri2x allocated MAX(1,n+nb+1)*(+1)
entries, that is N+NB+1; the factor NB+3 of the documented
WORK(N+NB+1,NB+3) is missing. ?SYTRI2X and ?HETRI2X keep the inverses
of the diagonal blocks in columns NB+2 and NB+3 of WORK, so every call
writes past the end of the buffer: LAPACKE_dsytri2x with n = 10 and
nb = 3 writes 528 bytes past a 112-byte buffer. LAPACKE now allocates
MAX(1,n+nb+1)*MAX(1,nb+3) entries.

Test: a new high-level test, sytri2x, inverts a diagonal matrix with
ipiv(k) = k (its own factorization by ?SYTRF and ?HETRF) with uplo = 'U'
and 'L' in both layouts, for ?sytri2x and, in the complex precisions,
?hetri2x, and checks the allocation failure paths, the allocation counts
and an invalid matrix_layout. On the parent commit every call writes
past the end of WORK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LAPACKE_?gemqrt allocated M*NB entries of WORK for both sides, but
?GEMQRT uses LDWORK = N and N*NB entries for side = 'L' (and M*NB for
side = 'R'), as documented. Every left-side call with n > m wrote past
the end of WORK: LAPACKE_dgemqrt with side = 'L', m = 4, n = 20 and
nb = 2 writes 192 bytes past a 64-byte buffer. LAPACKE now allocates
N*NB entries for side = 'l' and M*NB otherwise, like LAPACKE_?tpmqrt.

Test: a new high-level test, gemqrt, applies Q from the left to an
N-by-M C (more columns than rows) and from the right to an M-by-N C in
both layouts, and checks the allocation failure paths, the allocation
counts and an invalid matrix_layout. It uses K = NB, so that it does
not depend on the row-major copy of T (see below). On the parent commit
every left-side call writes past the end of WORK.

Not changed here: in row-major layout LAPACKE_?gemqrt_work copies V as
an ldv-by-k matrix and T as an ldt-by-nb matrix, where they are
nrows_v-by-k and nb-by-k, so with k > nb ?GEMQRT reads T past the end
of its copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CGEJSV and ZGEJSV always set IWORK(1:4), and document IWORK as having at
least four entries, but LAPACKE_{c,z}gejsv allocated MAX(3,M+2*N). For
M+2*N < 4 (a 1-by-1 matrix, or M or N = 0, where the routines set
IWORK(1:4) to zero before returning), every call writes one entry past
the end of IWORK. LAPACKE now allocates MAX(4,M+2*N). SGEJSV and DGEJSV
set only IWORK(1:3), and their wrappers allocate MAX(3,M+3*N) as
documented.

Test: a new high-level test, gejsv, computes the singular values of a
1-by-1 and an M-by-N matrix in both layouts, and checks the allocation
failure paths, the allocation counts and an invalid matrix_layout. On
the parent commit the 1-by-1 calls of the complex precisions write past
the end of IWORK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LAPACKE_?tgsna allocated WORK only for job = 'b' or 'v', but passed the
queried lwork (N) to ?TGSNA with WORK = NULL for job = 'e'. ?TGSNA uses
WORK(1:N) for the products with the eigenvectors that form the
eigenvalue condition numbers for JOB = 'E' and 'B'; only IWORK is not
referenced for JOB = 'E'. Every call with job = 'e' and n >= 1 crashes:
LAPACKE_dtgsna and LAPACKE_ztgsna with job = 'e' and n = 3 die with
SIGSEGV. LAPACKE now allocates WORK for every job.

Test: a new high-level test, tgsna, calls LAPACKE_?tgsna with job =
'E', 'V' and 'B' for an upper triangular pair (A,B), which is in
generalized Schur form, in both layouts, and checks the allocation
failure paths, the allocation counts and an invalid matrix_layout. On
the parent commit the test program crashes at the first job = 'E' call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
?SYSVXX and ?HESVXX factor A with ?SYTRF or ?HETRF and give them
LWORK = 5*MAX(1,N). ?HETRF's NBMIN is 2, so for N > 64 (the default
block size) it uses the blocked code with NB = 5, and all 5*N entries.
LAPACKE_{c,z}hesvxx allocated 2*N entries, so every call with n > 64
writes past the end of WORK: LAPACKE_zhesvxx with fact = 'N' and n = 100
writes 4800 bytes past a 3200-byte buffer.

?SYTRF's NBMIN is 8 with the reference ILAENV, so ?SYSVXX does not use
the blocked code with 5*N entries today. Still, it promises ?SYTRF 5*N
entries while it documented WORK(4*N) (real) or WORK(2*N) (complex),
and LAPACKE allocated those sizes; with an ILAENV that returns NBMIN <= 5
for xSYTRF it would write past the end too.

LAPACKE now allocates 5*N entries for all six wrappers, and ?SYSVXX
documents WORK(5*N), as ?HESVXX already does.

These routines are built only with XBLAS, so the test suite does not
run them. The overrun and the fix were checked with the XBLAS-only
sources compiled against empty BLAS_*_X stubs and a guarded allocator:
with this change LAPACKE_{c,z}hesvxx with n = 100 stay within WORK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In row-major layout, LAPACKE_?sbevx_2stage_work and
LAPACKE_?hbevx_2stage_work allocated the transposed copy of AB (and of
Q and Z when jobz = 'v') before they handled the workspace query, and
returned from the query without freeing them. Each row-major call of
LAPACKE_?sbevx_2stage and LAPACKE_?hbevx_2stage, which queries first,
therefore leaked one copy of AB. The query is now handled before the
allocations, as in the other _work functions.

Test: a new high-level test, sbevx_2stage (LAPACKE_?hbevx_2stage in the
complex precisions), calls the routine with range = 'A', 'I' and 'V' in
both layouts and checks the allocation failure paths, the allocation
counts and an invalid matrix_layout. It uses kd = 2: for kd <= 1 the
workspace query of xSBEVX_2STAGE itself returns too little, which is
fixed separately. On the parent commit the row-major allocation counts
are off and the test runner reports the leaked allocations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In row-major layout LAPACKE_?gemqrt_work copied V as an ldv-by-k matrix
and T as an ldt-by-nb matrix into column-major arrays with leading
dimensions ldv and ldt. V is nrows_v-by-k (nrows_v = m for side = 'L',
n for side = 'R') and T is nb-by-k, as LAPACKE_?gemqrt checks them.
Hence:

- with the natural leading dimension ldv = k < nrows_v, ?GEMQRT
  rejected the copy of V (LDV < M), and the reference XERBLA stopped the
  program;
- with k > nb, ?GEMQRT read the copy of T (allocated ldt*nb) past its
  end, and returned wrong results;
- the leading dimension of T was checked against nb instead of k.

The copies now have leading dimensions nrows_v and nb, T is checked
against k, and an invalid side is rejected before anything is copied,
as LAPACKE_?tpmqrt_work does since it was fixed in 2021-2022.

Test: the gemqrt high-level test applies Q with K = 3 > NB = 2 from both
sides, with and without transposition, to the same matrices in both
layouts and requires the same result. On the parent commit the
row-major results differ in all four cases and precisions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.48718% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.35%. Comparing base (07d6288) to head (2da9f82).
⚠️ Report is 76 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
LAPACKE/src/lapacke_cgemqrt_work.c 63.63% 3 Missing and 1 partial ⚠️
LAPACKE/src/lapacke_dgemqrt_work.c 63.63% 3 Missing and 1 partial ⚠️
LAPACKE/src/lapacke_sgemqrt_work.c 63.63% 3 Missing and 1 partial ⚠️
LAPACKE/src/lapacke_zgemqrt_work.c 63.63% 3 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1486      +/-   ##
==========================================
+ Coverage   70.16%   71.35%   +1.19%     
==========================================
  Files        6213     6243      +30     
  Lines      497965   517259   +19294     
  Branches    23446    23517      +71     
==========================================
+ Hits       349386   369083   +19697     
+ Misses     148083   147312     -771     
- Partials      496      864     +368     
Components Coverage Δ
BLAS 99.73% <ø> (+<0.01%) ⬆️
CBLAS 96.95% <ø> (-0.16%) ⬇️
LAPACK 83.95% <ø> (+0.44%) ⬆️
LAPACKE 4.18% <79.48%> (+1.69%) ⬆️
TMGLIB 55.69% <ø> (ø)
BLAS testing 90.11% <ø> (+1.33%) ⬆️
CBLAS testing 89.64% <ø> (-0.01%) ⬇️
LAPACK testing 82.83% <ø> (+0.49%) ⬆️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
LAPACKE/src/lapacke_cgejsv.c 55.26% <100.00%> (+55.26%) ⬆️
LAPACKE/src/lapacke_cgemqrt.c 70.83% <100.00%> (+70.83%) ⬆️
LAPACKE/src/lapacke_chbevx_2stage_work.c 42.85% <100.00%> (+42.85%) ⬆️
LAPACKE/src/lapacke_chetri2x.c 83.33% <ø> (+83.33%) ⬆️
LAPACKE/src/lapacke_cpteqr.c 66.66% <ø> (+66.66%) ⬆️
LAPACKE/src/lapacke_csytri2x.c 83.33% <ø> (+83.33%) ⬆️
LAPACKE/src/lapacke_ctgsna.c 65.00% <100.00%> (+65.00%) ⬆️
LAPACKE/src/lapacke_dgemqrt.c 70.83% <100.00%> (+70.83%) ⬆️
LAPACKE/src/lapacke_dpteqr.c 66.66% <ø> (+66.66%) ⬆️
LAPACKE/src/lapacke_dsbevx_2stage_work.c 42.85% <100.00%> (+42.85%) ⬆️
... and 18 more

... and 267 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 07d6288...2da9f82. Read the comment docs.

langou
langou previously approved these changes Nov 21, 2026
@rmlarsen
rmlarsen dismissed langou’s stale review November 21, 2026 03:57

The merge-base changed after approval.

langou
langou previously approved these changes Nov 21, 2026
@langou

langou commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Ping to the approvers @ACSimon33, @martin-frbg, @amontoison here. If someone can review and consider approving, that'd be great. For some reasons, I reviewed and approved and it is not enough to allow a merge. So if someone can review and consider approving, that'd be great.

@rmlarsen
rmlarsen dismissed langou’s stale review November 21, 2026 04:40

The merge-base changed after approval.

amontoison
amontoison previously approved these changes Nov 21, 2026
@amontoison

amontoison commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

@langou Does it help?
I didn't reviewed the PR but I approved it such that we can fix if it fixes the issue.

@rmlarsen
rmlarsen dismissed amontoison’s stale review November 21, 2026 04:57

The merge-base changed after approval.

@langou

langou commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

@langou Does it help? I didn't reviewed the PR but I approved it such that we can fix if it fixes the issue.

Thanks Alexis. No it does not help. Our reviews (yours and mine) are dismissed. Let me close and reopen the PR. Thanks for helping with the troubleshooting.

@langou
langou self-requested a review November 21, 2026 04:59
langou
langou previously approved these changes Nov 21, 2026
@langou langou closed this Nov 21, 2026
@langou langou reopened this Nov 21, 2026
@langou
langou dismissed their stale review November 21, 2026 05:00

The merge-base changed after approval.

@langou
langou merged commit ef55c9b into Reference-LAPACK:master Nov 21, 2026
133 of 134 checks passed
@langou

langou commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

@langou Does it help? I didn't reviewed the PR but I approved it such that we can fix if it fixes the issue.

Thanks Alexis. No it does not help. Our reviews (yours and mine) are dismissed. Let me close and reopen the PR. Thanks for helping with the troubleshooting.

OKey doc. I closed the PR, reopened it, approved and merged. Weird. Thanks Alexis.
And thanks @rmlarsen !

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.

3 participants