Repository navigation
Fix workspace overruns and a leak in LAPACKE high-level wrappers - #1486
Conversation
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 Report❌ Patch coverage is Additional details and impacted files@@ 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
... and 267 files with indirect coverage changes Continue to review full report in Codecov by Harness.
|
The merge-base changed after approval.
|
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. |
The merge-base changed after approval.
|
@langou Does it help? |
The merge-base changed after approval.
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. |
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:
LAPACKE_?pteqr, compz = 'n'WORK(4*N)(?LASQ1via?BDSQR)LAPACKE_?sytri2x,LAPACKE_?hetri2x(N+NB+1)*(NB+3)LAPACKE_?gemqrt, side = 'l'M*NBN*NBLAPACKE_{c,z}gejsvIWORK(MAX(3,M+2*N))LAPACKE_?tgsna, job = 'e'LAPACKE_?sysvxx,LAPACKE_?hesvxx4*N(real),2*N(complex)5*N(passed to?SYTRF/?HETRF)LAPACKE_?{sb,hb}evx_2stage, row-majorLAPACKE_?gemqrt_work, row-majorThis 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',?PTEQRcomputes the eigenvalues with?LASQ1(through?BDSQRwith NRU = 0), which uses the documentedWORK(4*N). LAPACKE allocated one entry, so every call with compz = 'n' and n >= 2 overran it. The wrapper now allocates4*Nfor every compz.?sytri2x/?hetri2x: the allocationMAX(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 allocatedM*NB, but?GEMQRTuses 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.?TGSNAuses WORK for JOB = 'E', and only IWORK is unreferenced there. Every call with job = 'e' crashed.?sysvxx/?hesvxx:?HETRFuses all5*Nentries for N > 64, soLAPACKE_{c,z}hesvxxoverran WORK, by 4800 bytes past a 3200-byte buffer for n = 100.?SYSVXXmakes the same5*Npromise to?SYTRF, which uses unblocked code at that size with the reference ILAENV. The wrappers now allocate5*N, and?SYSVXXdocumentsWORK(5*N), as?HESVXXdoes. 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_workfunctions.?gemqrt_work: in row-major layout V and T were copied with the wrong shapes. With the naturalldv = k,?GEMQRTrejected the copy of V, and the reference XERBLA stopped the program. Withk > nbit 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,sytri2xandtgsna, in all four precisions, run in the eightLAPACKE-high-levelCTest entries. In the complex precisions,sytri2xalso covers?hetri2xandsbevx_2stagecovers?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 invalidmatrix_layout.The
gemqrttest 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,gemqrtand (complex)gejsv, and the row-majorgemqrtresults differ. The row-majorsbevx_2stageallocation counts are off, and the runner reports the leaked allocations.tgsnacrashes the runner with SIGSEGV.?sysvxx/?hesvxxare built only with XBLAS and are not tested here. I checked them with the XBLAS-only sources compiled against emptyBLAS_*_Xstubs.Test suite
The full test suite passes, configured as in CI (
CBLAS=ON,LAPACKE=ON,LAPACKE_WITH_TMG=ON) with the_64API. 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 withCMAKE_BUILD_TYPE=Release, and the tests ran on x86-64 Linux build workers.Not changed here
LAPACKE_?sbevx_2stagewith kd <= 1 still overruns WORK. The workspace query ofxSBEVX_2STAGEitself returns too little, which is fixed in Fix workspace queries and LWORK checks that allow too little workspace #1485. Thesbevx_2stagetest here uses kd = 2.Checklist
?SYSVXX.)