From b9e12b5fd50c1558f2ccd3a53d8aa1478e6df025 Mon Sep 17 00:00:00 2001 From: ChrisLundquist <181256+ChrisLundquist@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:24:59 -0800 Subject: [PATCH 1/2] Fix work buffer overflow in single-threaded lower POTRF for large N 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 (146beeda5) 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 --- lapack/potrf/potrf_L_single.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lapack/potrf/potrf_L_single.c b/lapack/potrf/potrf_L_single.c index b781e048db..5d7129a531 100644 --- a/lapack/potrf/potrf_L_single.c +++ b/lapack/potrf/potrf_L_single.c @@ -178,7 +178,12 @@ blasint CNAME(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *sa, a + (is + j * lda) * COMPSIZE, lda, 0); if (is < j + bk + min_j) { - GEMM_OTCOPY(bk, min_i, a + (is + j * lda) * COMPSIZE, lda, sb2 + bk * (is - j - bk) * COMPSIZE); + /* Pack only the rows of the first tile: sb2 has room for min_j of + them, and the SYRK kernel below reads no more than that. */ + BLASLONG min_ii = j + bk + min_j - is; + if (min_ii > min_i) min_ii = min_i; + + GEMM_OTCOPY(bk, min_ii, a + (is + j * lda) * COMPSIZE, lda, sb2 + bk * (is - j - bk) * COMPSIZE); } SYRK_KERNEL_L(min_i, min_j, bk, dm1, From d5f9bb245f706fc3347979a757d5f93651993ef8 Mon Sep 17 00:00:00 2001 From: ChrisLundquist <181256+ChrisLundquist@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:24:59 -0800 Subject: [PATCH 2/2] utest: check that single-threaded lower POTRF stays inside its work area 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 --- utest/CMakeLists.txt | 4 + utest/Makefile | 2 +- utest/test_potrf_workarea.c | 193 ++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 1 deletion(-) create mode 100755 utest/test_potrf_workarea.c diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 8db20ae5f0..d34457bd60 100644 --- a/utest/CMakeLists.txt +++ b/utest/CMakeLists.txt @@ -137,6 +137,10 @@ set(OpenBLAS_utest_src ${OpenBLAS_utest_src} test_potrs.c ) +# test_potrf_workarea.c calls the single-threaded POTRF driver directly, which a shared libopenblas does not export +if (BUILD_STATIC_LIBS) + set(OpenBLAS_utest_src ${OpenBLAS_utest_src} test_potrf_workarea.c) +endif () set(OpenBLAS_utest_ext_src ${OpenBLAS_utest_ext_src} ${DIR_EXT}/test_cspmv.c diff --git a/utest/Makefile b/utest/Makefile index 7916c3636f..cbd518c26f 100644 --- a/utest/Makefile +++ b/utest/Makefile @@ -33,7 +33,7 @@ OBJS_EXT+=$(DIR_EXT)/test_zgemm.o $(DIR_EXT)/test_cgemm.o $(DIR_EXT)/test_zgbmv. OBJS_EXT+=$(DIR_EXT)/test_bgemm.o ifneq ($(NO_LAPACK), 1) -OBJS += test_potrs.o +OBJS += test_potrs.o test_potrf_workarea.o OBJS_EXT += $(DIR_EXT)/test_zspmv.o $(DIR_EXT)/test_cspmv.o $(DIR_EXT)/test_zsbmv.o $(DIR_EXT)/test_csbmv.o ifneq ($(NO_CBLAS), 1) ifneq ($(NO_LAPACKE), 1) diff --git a/utest/test_potrf_workarea.c b/utest/test_potrf_workarea.c new file mode 100755 index 0000000000..270f2ec8b4 --- /dev/null +++ b/utest/test_potrf_workarea.c @@ -0,0 +1,193 @@ +/***************************************************************************** +Copyright (c) 2026, The OpenBLAS Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name of the OpenBLAS project nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**********************************************************************************/ + +/* The single-threaded lower-triangular POTRF driver + * (lapack/potrf/potrf_L_single.c) packs the rows of the "first tile" of the + * trailing matrix behind sb2 in its work area. sb2 has room for REAL_GEMM_R + * rows of GEMM_Q elements, but the rows were packed GEMM_P at a time without + * stopping at the end of the tile, so up to GEMM_P - 1 rows too many were + * written: past the end of the BUFFER_SIZE work area once n - GEMM_Q exceeds + * REAL_GEMM_R. + * + * This test calls the driver directly with a work area of its own, laid out + * like the one interface/lapack/potrf.c gets from blas_memory_alloc(): + * BUFFER_SIZE bytes, page aligned (but not aligned to GEMM_ALIGN + 1, which + * blas_memory_alloc() does not promise either), followed by PW_TAIL bytes + * that must stay zero. The matrix is + * + * [ I E^T ] I : GEMM_Q x GEMM_Q identity + * [ E 0 ] E : all ones + * + * so the driver factors the first diagonal block, updates the whole + * trailing matrix once (which is where the first tile is packed) and stops + * at the next pivot, which is -GEMM_Q: info = GEMM_Q + 1. That costs about + * n * n * GEMM_Q / 2 multiply-adds and 8 * n * n bytes of zero-initialized + * memory, of which about half is touched. n grows with BUFFER_SIZE (about + * 10^4 for 32 MB work areas, 4 * 10^4 for the 128 MB default of x86_64), so + * the test is skipped where it would need more than PW_MAX_BYTES or more + * than OPENBLAS_UTEST_TIME_LIMIT seconds (default 10, 0 = no limits). + * + * It needs the internal driver symbol, i.e. a static libopenblas + * (utest/CMakeLists.txt adds it only with BUILD_STATIC_LIBS). */ + +#include "openblas_utest.h" +#include +#include +#include + +#if defined(BUILD_DOUBLE) + +#if !defined(_WIN32) || defined(__CYGWIN__) +#include +#endif + +#define PW_TAIL (4 << 20) /* zero memory behind the work area that is checked */ +#define PW_ALIGN (64 * 1024) +#define PW_PAGE 4096 +#define PW_MAX_BYTES ((double)(1 << 30)) /* largest matrix allocated unless the limits are off */ + +static double pw_now(void) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + LARGE_INTEGER f, c; + QueryPerformanceFrequency(&f); QueryPerformanceCounter(&c); + return (double)c.QuadPart / (double)f.QuadPart; +#else + struct timeval t; + gettimeofday(&t, NULL); + return (double)t.tv_sec + 1e-6 * (double)t.tv_usec; +#endif +} + +/* One call of the driver on the matrix described above; returns info, or -1 without memory. */ +static blasint pw_run(BLASLONG n, BLASLONG q, char *area, BLASLONG sb_off, double *secs) +{ + blas_arg_t args; + blasint info; + BLASLONG i, j; + double t0, *a = (double *)calloc((size_t)n * (size_t)n, sizeof(double)); + if (!a) return -1; + for (j = 0; j < q; j++) { + a[j + j * n] = 1.0; + for (i = q; i < n; i++) a[i + j * n] = 1.0; /* no zeros in what gets packed */ + } + memset(&args, 0, sizeof(args)); + args.n = n; args.a = a; args.lda = n; + args.nthreads = 1; + t0 = pw_now(); + info = dpotrf_L_single(&args, NULL, NULL, (double *)(area + GEMM_OFFSET_A), (double *)(area + sb_off), 0); + *secs = pw_now() - t0; + free(a); + return info; +} + +CTEST(potrf, single_work_area_bound) +{ + BLASLONG p = DGEMM_P, q = DGEMM_Q, r = DGEMM_R, pq, real_r, sb_off, n, n0; + BLASULONG sb2; + double limit = 10.0, secs = 0.0, est, bytes; + blasint info; + long past = -1; + size_t o; + char *raw, *area, *env; + + if (p < 1 || p > 65536 || q < 1 || q > 65536 || r < 1) { + CTEST_LOG("skipped: implausible GEMM parameters (built with other flags than the library?)"); + return; + } + pq = p > q ? p : q; + real_r = (r > pq + 96 ? r : pq + 96) - pq; /* REAL_GEMM_R of the driver */ + sb_off = (BLASLONG)GEMM_OFFSET_A + (BLASLONG)GEMM_OFFSET_B + + (BLASLONG)(((size_t)p * q * sizeof(double) + GEMM_ALIGN) & ~(size_t)GEMM_ALIGN); + n = q + real_r + p; /* first tile of real_r rows, then at least one more block of rows */ + n0 = 4 * q + 8; /* smallest size with the same blocking, for the time estimate */ + if (n < n0) { + CTEST_LOG("skipped: GEMM_R=%ld is too small for this test", (long)r); + return; + } + + env = getenv("OPENBLAS_UTEST_TIME_LIMIT"); + if (env) limit = atof(env); + bytes = (double)n * (double)n * sizeof(double); + if (limit > 0 && bytes > PW_MAX_BYTES) { + CTEST_LOG("skipped: n=%ld needs a matrix of %.1f GB", (long)n, bytes / 1e9); + return; + } + + raw = (char *)calloc(1, (size_t)BUFFER_SIZE + PW_TAIL + PW_ALIGN + PW_PAGE); + if (!raw) { + CTEST_LOG("skipped: not enough memory"); + return; + } + area = (char *)(((BLASULONG)raw + PW_ALIGN - 1) & ~(BLASULONG)(PW_ALIGN - 1)) + PW_PAGE; + + /* where the driver puts sb2, and whether real_r rows fit behind it at all */ + sb2 = (((BLASULONG)(area + sb_off) + (BLASULONG)pq * q * sizeof(double) + GEMM_ALIGN) & ~(BLASULONG)GEMM_ALIGN) + GEMM_OFFSET_B; + if ((double)(sb2 - (BLASULONG)area) + (double)q * (double)real_r * sizeof(double) > (double)BUFFER_SIZE) { + free(raw); + CTEST_LOG("skipped: GEMM_R=%ld leaves no room for GEMM_ALIGN in this work area", (long)r); + return; + } + + info = pw_run(n0, q, area, sb_off, &secs); + if (info < 0) { + free(raw); + CTEST_LOG("skipped: not enough memory"); + return; + } + ASSERT_EQUAL(q + 1, info); + est = secs * ((double)n / (double)n0) * ((double)n / (double)n0); + if (limit > 0 && est > limit) { + free(raw); + CTEST_LOG("skipped: n=%ld would take about %.0f s", (long)n, est); + return; + } + + info = pw_run(n, q, area, sb_off, &secs); + if (info >= 0) + for (o = 0; o < PW_TAIL; o++) + if (area[(size_t)BUFFER_SIZE + o]) { past = (long)o; break; } + free(raw); + if (info < 0) { + CTEST_LOG("skipped: not enough memory for n=%ld", (long)n); + return; + } + + CTEST_LOG("dpotrf_L_single n=%ld, GEMM_P=%ld GEMM_Q=%ld GEMM_R=%ld, BUFFER_SIZE=%lu: %.2f s", + (long)n, (long)p, (long)q, (long)r, (unsigned long)BUFFER_SIZE, secs); + if (past >= 0) + CTEST_LOG("the driver wrote past the end of its work area (first changed byte at +%ld)", past); + ASSERT_EQUAL(-1, past); /* nothing written behind the BUFFER_SIZE work area */ + ASSERT_EQUAL(q + 1, info); /* and the driver went through the trailing update */ +} + +#endif