Skip to content

Allocate enough IWORK and RWORK in LAPACKE_?gesvdmr3 - #1484

Merged
langou merged 1 commit into
Reference-LAPACK:masterfrom
rmlarsen:gesvdmr3-lapacke-workspace
Nov 20, 2026
Merged

langou merged 1 commit into
Reference-LAPACK:masterfrom
rmlarsen:gesvdmr3-lapacke-workspace

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

LAPACKE_?gesvdmr3 (#1361) was modelled on LAPACKE_?gesvdx and kept its workspace sizes, which are too small for xGESVDMR3. Every call with singular vectors writes past the end of the IWORK that LAPACKE allocates, and the complex calls also overrun RWORK when MIN(M,N) is 2 or 3. This PR allocates the sizes that xGESVDMR3 documents, corrects two workspace descriptions of xBDSVDMR3 that disagree with its argument checks, and makes the LAPACKE high-level tests catch a workspace overrun.

buffer allocated by LAPACKE_?gesvdmr3 needed by xGESVDMR3 (documented)
IWORK 12*MIN(M,N) 24*MIN(M,N)
RWORK (c, z) 17*MIN(M,N)**2 7*MIN(M,N)**2 + 41*MIN(M,N)

Description

  • IWORK: xGESVDMR3 passes LIWORK = 24*MIN(M,N) on to xBDSVDMR3, so no argument check catches a smaller array. With singular vectors, xBDSVDMR3_WORK hands DLARRV_TGK the IWORK from position 8*N+1 on, and DLARRV_TGK always writes IWORK(12*N+1:12*N+2), the start of its cluster lists; the lists and the workspace of DLAR1V_TGK lie further out. So every call with singular vectors and MIN(M,N) >= 2 writes past the buffer.
  • RWORK (c, z): CGESVDMR3 and ZGESVDMR3 keep the bidiagonal and the TGK eigenvectors in RWORK (2*MIN(M,N)**2 + 3*MIN(M,N) entries) and pass the 5*MIN(M,N)**2 + 37*MIN(M,N) entries after them to xBDSVDMR3. The 17*MIN(M,N)**2 of xGESVDX is too small for MIN(M,N) <= 4. With singular vectors, ZGESVDMR3 writes 104 entries of RWORK for MIN(M,N) = 2 (68 allocated) and 174 for MIN(M,N) = 3 (153 allocated).

Fix

  • lapacke_{s,d,c,z}gesvdmr3.c: allocate IWORK with 24*MIN(m,n) entries. superb keeps the 12*MIN(m,n)-1 entries of LAPACKE_?gesvdx, so callers are unaffected.
  • lapacke_{c,z}gesvdmr3.c: allocate RWORK with 7*MIN(m,n)**2 + 41*MIN(m,n) entries.
  • {s,d}bdsvdmr3.f, {s,d}bdsvdmr3_work.f: the descriptions asked for LIWORK >= 20*N, but both routines check LIWORK >= 24*N, so a caller that followed the documentation stopped in XERBLA. The LWORK of xBDSVDMR3 for JOBZ = 'N' was documented as 2*N*N + 37*N, two less than the check requires. The descriptions now give the sizes that the code checks.

Regression tests

  • LAPACKE/testing/high_level/lapacke_test_alloc.c: the test allocator, which the LAPACKE high-level tests install for LAPACKE's internal buffers, now follows each allocation with a guard region of 1024 known bytes. When the allocation is freed, a changed byte fails the test. The existing tests pass with it.
  • LAPACKE/testing/high_level/{s,d,c,z}gesvdmr3.c: a new gesvdmr3 test calls LAPACKE_?gesvdmr3 with singular vectors for a 4-by-3 and a 3-by-4 matrix in both layouts and checks NS, and, like the other tests, checks the allocation failure paths, the allocation counts and an invalid matrix_layout.

With the LAPACKE_?gesvdmr3 of master, every call that computes singular vectors fails the guard check, for IWORK in all precisions and also for RWORK in complex (6 failures in s/d and 12 in c/z):

FAIL write past the end of a LAPACKE allocation of 144 bytes (first changed byte: 0 past the end)
FAIL write past the end of a LAPACKE allocation of 1224 bytes (first changed byte: 24 past the end)

As an independent check, a standalone program calling LAPACKE_?gesvdmr3 with LAPACKE's default allocator runs clean under valgrind with this PR. With master, valgrind reports writes 0 and 4 bytes past the 144-byte IWORK from slarrv_tgk_, and the complex RWORK overrun then corrupts the heap until valgrind itself aborts.

The NaN check of LAPACKE_?gesvdmr3 is not swept here: #1483 changes the INFO it returns from -6 to -7 (a is its seventh argument), so the NaN sweep can join this test once both PRs are in. The two PRs touch the same four LAPACKE files on different lines and merge cleanly in either order.

Test suite

The full test suite passes on this branch, configured as in CI (CBLAS=ON, LAPACKE=ON, LAPACKE_WITH_TMG=ON) with the _64 API: all 313 CTest entries, including the eight LAPACKE-high-level runners, with 5739011 LAPACK tests, 11973112 LAPACKE tests, 319677 BLAS tests and 645492 CBLAS tests for each API, 0 numerical errors and 0 other errors (13th Gen Intel(R) Core(TM) i7-13700HX, Linux under WSL2, GNU Fortran and GCC 15.2.0, CMAKE_BUILD_TYPE=Release).

Not changed here

LAPACKE_?gesvdmr3 fills superb from IWORK(2:12*MIN(M,N)), as LAPACKE_?gesvdx does, where those entries hold the indices of eigenvectors that failed to converge. xGESVDMR3 leaves other data there (IWORK(2) is the path that xBDSVDMR3 took), and the IWORK description of xGESVDMR3, also inherited from xGESVDX, does not match the routine. That interface question is left alone.

Checklist

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

LAPACKE_?gesvdmr3 was modelled on LAPACKE_?gesvdx and kept its
workspace sizes, which are too small for xGESVDMR3:

- It allocates IWORK with 12*MIN(M,N) entries, the size xGESVDX
  needs.  xGESVDMR3 documents 24*MIN(M,N) and passes
  LIWORK = 24*MIN(M,N) on to xBDSVDMR3, so no argument check catches
  the shortfall.  With singular vectors, xBDSVDMR3_WORK hands
  DLARRV_TGK the IWORK from position 8*N+1 on, and DLARRV_TGK always
  writes IWORK(12*N+1:12*N+2), the start of its cluster lists; the
  lists and the workspace of DLAR1V_TGK lie further out.  So every
  call with singular vectors and MIN(M,N) >= 2 writes past the end of
  the buffer.
- CGESVDMR3 and ZGESVDMR3 also need an RWORK of 7*MIN(M,N)**2 +
  41*MIN(M,N) entries, but LAPACKE_cgesvdmr3 and LAPACKE_zgesvdmr3
  allocate 17*MIN(M,N)**2, the size xGESVDX needs.  That is too small
  for MIN(M,N) <= 4: with singular vectors, ZGESVDMR3 writes 104
  entries of RWORK for MIN(M,N) = 2 (68 allocated) and 174 for
  MIN(M,N) = 3 (153 allocated).

Both buffers now have the documented sizes.  superb keeps the
12*MIN(M,N)-1 entries of LAPACKE_?gesvdx, so callers are unaffected.

The workspace descriptions of xBDSVDMR3 and xBDSVDMR3_WORK asked for
LIWORK >= 20*N, but both routines check LIWORK >= 24*N, the size that
xGESVDMR3 documents and passes, so a caller that followed the
documentation stopped in XERBLA.  The LWORK of xBDSVDMR3 for
JOBZ = 'N' was documented as 2*N*N + 37*N, two less than the check
requires.  The descriptions now give the sizes that the code checks.

The test allocator of the LAPACKE high-level tests now follows every
allocation with a guard region of 1024 known bytes and fails the test
when the region has changed by the time the allocation is freed.  A
new gesvdmr3 test, in all precisions, calls LAPACKE_?gesvdmr3 with
singular vectors for a 4-by-3 and a 3-by-4 matrix in both layouts,
and checks its allocation failure paths, its allocation counts and an
invalid matrix_layout like the other tests.  With the parent's
LAPACKE_?gesvdmr3, every call that computes singular vectors fails
the guard check: for IWORK in all precisions, and also for RWORK in
complex.  Under valgrind, with LAPACKE's default allocator, a
standalone reproducer shows the writes past IWORK as invalid on the
parent, after which the RWORK overrun corrupts the heap and valgrind
aborts; with this change valgrind reports no errors.

The full test suite passes, with and without the _64 API.

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

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.16%. Comparing base (c9967aa) to head (07d6288).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1484      +/-   ##
==========================================
+ Coverage   70.10%   70.16%   +0.06%     
==========================================
  Files        6213     6213              
  Lines      498005   497965      -40     
  Branches    23446    23446              
==========================================
+ Hits       349106   349386     +280     
+ Misses     148462   148083     -379     
- Partials      437      496      +59     
Components Coverage Δ
BLAS 99.73% <ø> (+0.02%) ⬆️
CBLAS 97.10% <ø> (ø)
LAPACK 83.51% <ø> (+<0.01%) ⬆️
LAPACKE 2.49% <100.00%> (+0.31%) ⬆️
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.77% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.33% <ø> (ø)
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
LAPACKE/src/lapacke_cgesvdmr3.c 87.50% <100.00%> (+87.50%) ⬆️
LAPACKE/src/lapacke_dgesvdmr3.c 84.37% <ø> (+84.37%) ⬆️
LAPACKE/src/lapacke_sgesvdmr3.c 84.37% <ø> (+84.37%) ⬆️
LAPACKE/src/lapacke_zgesvdmr3.c 87.50% <100.00%> (+87.50%) ⬆️
SRC/dbdsvdmr3.f 73.26% <ø> (ø)
SRC/dbdsvdmr3_work.f 87.67% <ø> (ø)
SRC/sbdsvdmr3.f 73.26% <ø> (ø)
SRC/sbdsvdmr3_work.f 87.67% <ø> (ø)

... and 10 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 c9967aa...07d6288. Read the comment docs.

@langou
langou merged commit 0d0ed9d into Reference-LAPACK:master Nov 20, 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