Skip to content

Fix workspace queries and LWORK checks that allow too little workspace - #1485

Merged
langou merged 5 commits into
Reference-LAPACK:masterfrom
rmlarsen:workspace-query-fixes
Nov 15, 2026
Merged

langou merged 5 commits into
Reference-LAPACK:masterfrom
rmlarsen:workspace-query-fixes

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

Six routine families return a workspace query or accept an LWORK that is smaller than what they then use. A caller that allocates what the query returns, as LAPACKE does, or that follows the documented minimum gets its workspace written past the end. One family stops in an argument check on the size its own query returned. Each commit fixes one family and adds a test that fails on the parent commit.

routine query or check used
xSBEVX_2STAGE, KD = 0 or 1 2*N+LHTRD+LWTRD 2*N+LHTRD+MAX(4*N,LWTRD) (xSTEBZ)
xGEESX, SENSE = 'V' or 'B', N = 1 LIWORK = (N*N)/4 = 0 rejects LIWORK < 1
xGGSVP3, xGGSVD3 documents and checks LWORK >= 1 MAX(M,3*N+1), and P with JOBV = 'V'; N more in xGGSVD3
xGEMQR, xLAMTSQR, SIDE = 'R' MB*NB M*NB (xGEMQRT, xTPMQRT)
xGELQ, xGELQT branch MB*N MB*M (xGELQT)
xSBGVD, N = 1, JOBZ = 'V' 1 3 (xSTEDC, xGEMM)

Description

  • xSBEVX_2STAGE keeps D, E and the Householder vectors (LHTRD) at the start of WORK and gives the rest to xSYTRD_SB2ST and then to xSTEBZ, which uses 4*N entries. For KD <= 1, LWTRD < 4*N. The documented MAX(1, 7*N, (2*KD+1)*N+KD*NTHREADS+2*N) was too small as well, because it leaves out the Householder vectors.
  • xGEESX returns (N*N)/4 from its LIWORK query, which is 0 for N = 1. LAPACKE_dgeesx(..., sense = 'V', n = 1, ...) therefore stops with "On entry to DGEESX parameter number 18 had an illegal value".
  • xGGSVP3 and xGGSVD3 documented and checked only LWORK >= 1. Below the real minimum, an internal routine reports the error and the routines go on regardless. With LWORK = N-1, xGGSVD3 passes LWORK-N = -1 to xGGSVP3. xGGSVP3 takes that as a workspace query and computes nothing, and xGGSVD3 then writes past WORK and returns INFO = 0. CGGSVP3 and ZGGSVP3 also reported a too small LWORK as argument 24, but LWORK is their argument 25.
  • xGEMQR (all precisions) and SLAMTSQR/DLAMTSQR size WORK as MB*NB for SIDE = 'R', but xGEMQRT and xTPMQRT use M*NB. CLAMTSQR and ZLAMTSQR already compute M*NB, but document MB*NB. With M = 50, the query returns 3 and DGEMQR writes 50 entries. CGEMQR also rejected the LWORK = 1 that its query returns when MIN(M,N,K) = 0.
  • xGELQ sizes WORK as MB*N for its xGELQT branch, but xGELQT uses MB*M. With M = 50 and N = 2, the query returns 2 and DGELQ writes 49 entries.
  • SSBGVD and DSBGVD require LWORK = 1 for N <= 1 but use 3 entries for N = 1 with JOBZ = 'V'. With the LWORK = 1 that their query returns, they also return a wrong eigenvector with INFO = 0: xSTEDC gets LWORK = -1, takes it as a workspace query and leaves its eigenvector unset. CHBGVD and ZHBGVD have required 1+N since 2011, but still documented 1.

Fix

Each routine now checks, and returns from its workspace query, the size it uses, and documents it. See the commit messages for the formulas. Nothing changes where the old sizes were already enough: KD >= 2 for xSBEVX_2STAGE, N >= 2 for xGEESX, N >= 2 or JOBZ = 'N' for xSBGVD, and SIDE = 'L' for xGEMQR. The one exception is xGELQ with N = 0, which now needs LWORK >= M instead of 1, as xGEQR has always needed LWORK >= N for M = 0. xGGSVP3 and xGGSVD3 count P only when they form V.

Regression tests

Five of the six tests make the existing test drivers pass a routine exactly the LWORK that its workspace query returns, followed by a guard region that the call must leave unchanged. An overwritten guard fails the test. On the parent commit they fail as follows:

  • xDRVST2STG (xSBEVX_2STAGE): 58 to 64 of the 13464 DST/SST driver tests per run.
  • xGET24 (xGEESX): takes LIWORK from the query instead of N*N. 44 of 2888 DSX/SSX tests fail, from INFO = -18 at N = 1.
  • xTSQR01 (xGEMQR, xGELQ): 1060 of 10800 TS tests in each precision.
  • xDRVSG2STG (xSBGVD): 4 to 8 DSG/SSG tests per run.

The sixth, xERRGG, checks that LWORK below the minimum is an argument error in xGGSVP3 and xGGSVD3. xGSVTS3 now checks that the minimum is enough: it calls xGGSVD3 with exactly the documented minimum for JOBU = JOBV = JOBQ = 'N' and then for 'U','V','Q', each followed by a guard.

For xGGSVP3 and xGGSVD3, a standalone program calls DGGSVP3, DGGSVD3, ZGGSVP3 and ZGGSVD3 with exactly the new minimum and a guard. It covers M, P, N in {0,1,2,3,5,8} with JOBU, JOBV, JOBQ = 'N','N','N', 'U','V','Q', 'U','N','Q' and 'N','V','N', 3456 cases, and none writes past the end or reports an error. With one less, all 2840 cases with LWORK >= 1 stop with the argument error.

How these were found

Each LAPACK subroutine with a workspace-length argument and a workspace query (546 of them) was wrapped by a generated shim. The shim asks the routine's own query for the sizes, then calls it with buffers of exactly MIN(length given, size queried) entries, each followed by a guard. LAPACK, tmglib, LAPACKE and the test programs all call through the shims. Running the whole test suite this way reported xSBEVX_2STAGE, xGEMQR, xGELQ and xSBGVD, plus one more, listed under "Not changed here". With this PR, that one is the only report left.

The other two were found by reading the code, while auditing LAPACKE's workspace sizes after #1484. The test suite never passes xGEESX the LIWORK from its query, and never passes xGGSVP3 or xGGSVD3 an LWORK below their real minimum.

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

DBDSVDX with RANGE = 'V' and VL = 0 writes 2*N columns of Z for a bidiagonal with zero singular values, although it documents NS+1 columns. RANGE = 'A' stays within NS+1. For example, DGESVDX on a 30-by-30 zero matrix with JOBU = JOBVT = 'V' and RANGE = 'V' uses 3720 entries of WORK, while its query returns 2910 and its minimum is 2370. The complex xGESVDX keep Z in RWORK. #1488 fixes it.

Checklist

  • The documentation has been updated. (The LWORK and LIWORK descriptions of the routines above.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue.)

rmlarsen and others added 2 commits November 8, 2026 22:49
xSBEVX_2STAGE keeps D, E and the Householder vectors of the band
reduction (LHTRD entries) at the start of WORK, and gives the rest, from
INDWRK = 2*N+LHTRD+1 on, first to xSYTRD_SB2ST (LWTRD entries) and then
to the tridiagonal eigensolver: xSTEBZ uses 4*N entries there, and the
xSTERF path copies E to WORK(INDWRK+2*N). The minimum LWORK that the
routine checks, and returns from a workspace query, was
2*N+LHTRD+LWTRD, which does not cover the 4*N of xSTEBZ when
LWTRD = (2*KD+1)*N+KD*NTHREADS is smaller, that is for KD = 0 and
KD = 1. A caller that allocates what the query returns, as
LAPACKE_?sbevx_2stage does, gets WORK written past its end: by up to
3*N entries for KD = 0 and up to N-NTHREADS for KD = 1. With
LAPACKE_dsbevx_2stage and N = 10, KD = 1, RANGE = 'I', the 728-byte
buffer is written 56 bytes past its end.

The documented size, MAX(1, 7*N, (2*KD+1)*N+KD*NTHREADS+2*N), is too
small as well: it leaves out the Householder vectors.

The minimum is now 2*N+LHTRD+MAX(4*N,LWTRD), and the documentation
gives it for JOBZ = 'N' (the only job available, with LHTRD = 4*N):
6*N+MAX(4*N, (2*KD+1)*N+KD*NTHREADS). Nothing changes for KD >= 2. The
complex xHBEVX_2STAGE keeps its real workspace in RWORK(7*N) and is not
affected.

Test: xDRVST2STG now gives each of its three xSBEVX_2STAGE calls
exactly the LWORK that a workspace query returns, followed by a guard
of 4*N entries that the call must leave unchanged. On the parent
commit, 58 to 64 of the 13464 DST and SST driver tests of each run fail
with "DSBEVX_2STAGE(N,I,L) wrote past the workspace size that its
workspace query returned." (the driver uses ABSTOL > 0, so every call
with KD <= 1 reaches xSTEBZ); with this change they pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For SENSE = 'V' or 'B', xGEESX returns (N*N)/4 in IWORK(1) from a
workspace query, which is 0 for N = 1, but it rejects LIWORK < 1 with
INFO = -18. A caller that sizes IWORK from the query, as
LAPACKE_?geesx does, therefore fails for every 1-by-1 matrix with these
jobs; with the reference XERBLA, LAPACKE_dgeesx(..., sense = 'V',
n = 1, ...) stops the program with "On entry to DGEESX parameter number
18 had an illegal value".

The query now returns MAX(1, (N*N)/4). SGEESX also returns
MAX(1, SDIM*(N-SDIM)) in IWORK(1) on exit, as DGEESX already does. The
complex routines have no IWORK.

Test: xGET24 took LIWORK = N*N for all its xGEESX calls. It now takes
the LIWORK that a workspace query of xGEESX returns for SENSE = 'B',
which also checks that the queried size is enough for every test
matrix ((N*N)/4 >= SDIM*(N-SDIM) for every SDIM). On the parent commit
the DSX and SSX tests then fail 44 of 2888 tests each, the test XERBLA
reporting INFO = 18 from xGEESX for N = 1; with this change all 3508
pass.

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 87.30159% with 48 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.11%. Comparing base (c9967aa) to head (0d7d846).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
TESTING/EIG/ddrvst2stg.f 69.23% 12 Missing ⚠️
TESTING/EIG/sdrvst2stg.f 69.23% 12 Missing ⚠️
TESTING/EIG/cgsvts3.f 84.61% 4 Missing ⚠️
TESTING/EIG/dgsvts3.f 84.61% 4 Missing ⚠️
TESTING/EIG/sgsvts3.f 84.61% 4 Missing ⚠️
TESTING/EIG/zgsvts3.f 84.61% 4 Missing ⚠️
TESTING/EIG/ddrvsg2stg.f 75.00% 3 Missing ⚠️
TESTING/EIG/sdrvsg2stg.f 75.00% 3 Missing ⚠️
SRC/dsbgvd.f 66.66% 1 Missing ⚠️
SRC/ssbgvd.f 66.66% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1485      +/-   ##
==========================================
+ Coverage   70.10%   70.11%   +0.01%     
==========================================
  Files        6213     6213              
  Lines      498005   498285     +280     
  Branches    23446    23446              
==========================================
+ Hits       349106   349355     +249     
- Misses     148462   148494      +32     
+ Partials      437      436       -1     
Components Coverage Δ
BLAS 99.73% <ø> (+0.02%) ⬆️
CBLAS 97.10% <ø> (ø)
LAPACK 83.51% <97.72%> (+<0.01%) ⬆️
LAPACKE 2.17% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.77% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.33% <84.13%> (-0.01%) ⬇️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/cgelq.f 87.50% <100.00%> (-0.91%) ⬇️
SRC/cgemlq.f 98.21% <ø> (ø)
SRC/cgemqr.f 98.21% <100.00%> (ø)
SRC/cggsvd3.f 98.66% <100.00%> (+1.52%) ⬆️
SRC/cggsvp3.f 100.00% <100.00%> (+0.75%) ⬆️
SRC/chbgvd.f 71.23% <ø> (ø)
SRC/clamtsqr.f 82.35% <ø> (ø)
SRC/dgeesx.f 84.70% <100.00%> (+0.58%) ⬆️
SRC/dgelq.f 87.50% <100.00%> (-0.91%) ⬇️
SRC/dgemlq.f 98.21% <ø> (ø)
... and 40 more

... and 1 file 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 c9967aa...0d7d846. Read the comment docs.

rmlarsen and others added 3 commits November 9, 2026 00:26
xGGSVP3 and xGGSVD3 documented LWORK >= 1 and checked only that, but
they need much more:

- xGGSVP3 calls xGEQP3 on B and on A, which needs 3*N+1 entries (N+1 in
  the complex routines), the unblocked QR, RQ and
  orthogonal-transformation routines, which need M or N, and, to form
  V, xORG2R (xUNG2R), which needs P.
- xGGSVD3 keeps the TAU of xGGSVP3 in WORK(1:N) and gives it
  WORK(N+1:LWORK) as its workspace; xTGSJA uses WORK(1:2*N).

A caller that follows the documentation therefore gets an argument
error from an internal routine (DGEQP3 parameter 8, or DGGSVP3
parameter 24 from DGGSVD3), and the routines go on after it: with an
XERBLA that returns, DGGSVD3 with LWORK = 1 and N = 3 writes WORK(2:3).
Worse, xGGSVD3 with LWORK = N-1 passes LWORK-N = -1 to xGGSVP3, which
takes it as a workspace query and returns without computing anything;
xGGSVD3 then goes on with whatever K and L held on entry, writes past
the end of WORK, and returns INFO = 0.

The routines now check the minimum they need and document it:

- xGGSVP3: if N > 0 and MAX(M,P) > 0, LWORK >= MAX(M,3*N+1)
  (MAX(M,N+1) in complex), and also LWORK >= P if JOBV = 'V';
  otherwise LWORK >= 1, as no routine uses WORK.
- xGGSVD3: N more than xGGSVP3 if N > 0 and MAX(M,P) > 0, and
  LWORK >= MAX(1,2*N) otherwise.

P counts only when V is formed, as it already did in the optimal size
that the workspace query returns.

The workspace query returns at least this minimum. CGGSVP3 and ZGGSVP3
reported a too small LWORK as INFO = -24, but LWORK is their argument
25 (RWORK precedes TAU); they now return -25.

A standalone program calls DGGSVP3, DGGSVD3, ZGGSVP3 and ZGGSVD3 with
exactly this LWORK, followed by a guard, for M, P, N in {0,1,2,3,5,8}
and JOBU, JOBV, JOBQ = 'N','N','N', 'U','V','Q', 'U','N','Q' and
'N','V','N'. All 3456 cases run without a write past the end, an
argument error or INFO != 0. With one less, all 2840 cases with
LWORK >= 1 stop with the argument error.

Tests: xERRGG checks that LWORK below the minimum is an error in
argument 22 of xGGSVD3 and in argument 24 (real) or 25 (complex) of
xGGSVP3.

xGSVTS3 now calls xGGSVD3 twice, with JOBU = JOBV = JOBQ = 'N' and
then with 'U', 'V', 'Q' for its residual tests, each time with exactly
the minimum LWORK documented for those jobs followed by a guard. A
rejected LWORK or an overwritten guard makes test ratio 1 equal to
1/ULP. The GSV path runs with block size 1, so xGEQP3 takes the same
unblocked path as with the larger LWORK it got before. These tests
check that the minimum is enough; xERRGG checks that less is an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xGEMQR applies the Q of xGEQR with xGEMQRT or xLAMTSQR. For SIDE = 'R'
both of these use M*NB entries of WORK (xGEMQRT and xTPMQRT with
LDWORK = M), but xGEMQR checked and returned MB*NB, and so did SLAMTSQR
and DLAMTSQR (CLAMTSQR and ZLAMTSQR already compute M*NB, but document
MB*NB). A caller that allocates what the query returns gets WORK
written past its end whenever M > MB: in xTSQR01, DGEMQR('R', ..., M =
50, N = 3) gets 3 entries from its query and writes 50.

xGELQ computes the LQ factorization with xGELQT or xLASWLQ, which both
use MB*M entries of WORK. For the xGELQT branch (N <= M, or NB outside
(M,N)), xGELQ checked and returned MB*N and a minimum of N. DGELQ with
M = 50, N = 2 gets 2 entries from its query and writes 49.

Both are found by running the test suite with every workspace sized
exactly by the routine's own workspace query, followed by a guard.

The routines now check and return M*NB (xGEMQR, xLAMTSQR) and MB*M
(xGELQ). For N = 0, xGELQ therefore needs LWORK >= M instead of 1, as
xGEQR has always needed LWORK >= N for M = 0. CGEMQR also checked
LWORK against MAX(1,LW) instead of LWMIN, so for MIN(M,N,K) = 0 it
rejected the LWORK = 1 that its own query returns; it now checks
LWMIN, as SGEMQR, DGEMQR and ZGEMQR do. The LWORK descriptions of
xGEMQR and xGEMLQ, which said only LWORK >= 1, and of xGELQ give the
minimum that the routines check.

Test: xTSQR01 sized WORK by the largest of all its queries, so each
call got at least the workspace of the largest one. Its xGELQ call and
its two right-side xGEMQR calls now get exactly the LWORK that their
own workspace query returns, followed by a guard that must stay
unchanged; an overwritten guard makes the test ratio 1/EPS. On the
parent commit the TS tests then fail 1060 of 10800 tests in each
precision; with this change they all pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For N <= 1, SSBGVD and DSBGVD required, and returned from a workspace
query, LWORK = 1. For N = 1 the routines do not return early: they
keep E in WORK(1), the eigenvectors of the tridiagonal matrix (from
xSTEDC) in WORK(2), and the product with Z (from xGEMM) in WORK(3). A
caller that allocates what the query returns gets WORK written two
entries past its end for JOBZ = 'V', and a wrong eigenvector with
INFO = 0: xSTEDC gets LWORK-2 = -1, takes it as a workspace query and
returns without setting WORK(2) = 1, and xGEMM multiplies Z by
whatever WORK(2) holds.

For JOBZ = 'V' the minimum for N <= 1 is now 1+2*N. For JOBZ = 'N' it
stays 1: WORK(3) is used only for eigenvectors, and with N = 1 nothing
is written past WORK(1) (checked for KA, KB <= 2). CHBGVD and ZHBGVD
already require 1+N for N <= 1 in WORK and RWORK (since 2011), but still
documented 1; their documentation now says 1+N. The documentation of
SSBGVD also gave 3*N instead of 2*N for JOBZ = 'N' and N > 1.

Test: xDRVSG2STG gives its xSBGVD call exactly the LWORK that a
workspace query returns, followed by a guard that the call must leave
unchanged. (xDRVSG is no longer run by xCHKEE.) On the parent commit
the DSG and SSG tests report 4 to 8 failures per run, all for N = 1;
with this change they pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmlarsen
rmlarsen force-pushed the workspace-query-fixes branch from 442cdbf to 0d7d846 Compare November 9, 2026 08:29
@langou
langou merged commit 7c4f2a6 into Reference-LAPACK:master Nov 15, 2026
49 checks passed
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.

2 participants