From 53b23ed71a2a680a21fb136091116dcfcf4bf9cc Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 17:01:45 +0000 Subject: [PATCH] Fix 64-bit index corruption when L and U use a user-supplied work array With XSDK_INDEX_SIZE=64 and lwork > 0, xLUMemInit carved the int_t column pointer arrays xlsub, xlusup and xusub out of work[] with (n+1)*sizeof(int) bytes each, half of what they need. xusub overran into the lusup/ucol storage placed right after it and the numeric values overwrote the U column pointers, so xgssvx crashed in xPivotGrowth (or free() aborted) on the first factorization. The heap path (lwork == 0) was unaffected because it uses intMalloc. Size the three int_t arrays, the matching xuser_free in the shrink loop, and the lwork == -1 estimate with sizeof(int_t); xsup and supno remain int. Also fix the byte offsets in the (currently unused) xStackCompress, which computed the positions of the int_t lsub/usub blocks with sizeof(int) and the end of ucol with the integer size instead of the value size. Reproducer: build with -DXSDK_INDEX_SIZE=64 and run TESTING/s_test -t "LA" -l 1000000 -n 9 which segfaulted and now passes; the full ctest suite passes with both 32- and 64-bit indices. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VjnVBdsXEM3c41ssQ3hFAM --- SRC/cmemory.c | 31 +++++++++++++++++-------------- SRC/dmemory.c | 31 +++++++++++++++++-------------- SRC/smemory.c | 31 +++++++++++++++++-------------- SRC/zmemory.c | 31 +++++++++++++++++-------------- 4 files changed, 68 insertions(+), 56 deletions(-) diff --git a/SRC/cmemory.c b/SRC/cmemory.c index 4b4ac325..4fb662f0 100644 --- a/SRC/cmemory.c +++ b/SRC/cmemory.c @@ -191,7 +191,7 @@ cLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int panel_size, float fill_ratio, SuperMatrix *L, SuperMatrix *U, GlobalLU_t *Glu, int **iwork, singlecomplex **dwork) { - int info, iword, dword; + int info, iword, liword, dword; SCformat *Lstore; NCformat *Ustore; int *xsup, *supno; @@ -203,6 +203,7 @@ cLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int_t nzlmax, nzumax, nzlumax; iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(singlecomplex); Glu->n = n; Glu->num_expansions = 0; @@ -217,8 +218,9 @@ cLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, //nzlmax = SUPERLU_MAX(1, fill_ratio/4.) * annz; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else { cSetupSpace(work, lwork, Glu); } @@ -239,9 +241,9 @@ cLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, } else { xsup = (int *)cuser_malloc((n+1) * iword, HEAD, Glu); supno = (int *)cuser_malloc((n+1) * iword, HEAD, Glu); - xlsub = cuser_malloc((n+1) * iword, HEAD, Glu); - xlusup = cuser_malloc((n+1) * iword, HEAD, Glu); - xusub = cuser_malloc((n+1) * iword, HEAD, Glu); + xlsub = cuser_malloc((n+1) * liword, HEAD, Glu); + xlusup = cuser_malloc((n+1) * liword, HEAD, Glu); + xusub = cuser_malloc((n+1) * liword, HEAD, Glu); } lusup = (singlecomplex *) cexpand( &nzlumax, LUSUP, 0, 0, Glu ); @@ -256,7 +258,7 @@ cLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, SUPERLU_FREE(lsub); SUPERLU_FREE(usub); } else { - cuser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*iword, + cuser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*liword, HEAD, Glu); } nzlumax /= 2; @@ -291,8 +293,9 @@ cLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, nzlumax = Glu->nzlumax; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else if ( lwork == 0 ) { Glu->MemModel = SYSTEM; } else { @@ -624,14 +627,14 @@ void void cStackCompress(GlobalLU_t *Glu) { - register int iword, dword, ndim; + register int liword, dword, ndim; char *last, *fragment; int_t *ifrom, *ito; singlecomplex *dfrom, *dto; int_t *xlsub, *lsub, *xusub, *usub, *xlusup; singlecomplex *ucol, *lusup; - iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(singlecomplex); ndim = Glu->n; @@ -649,16 +652,16 @@ cStackCompress(GlobalLU_t *Glu) ucol = dto; ifrom = lsub; - ito = (int_t *) ((char*)ucol + xusub[ndim] * iword); + ito = (int_t *) ((char*)ucol + xusub[ndim] * dword); copy_mem_int(xlsub[ndim], ifrom, ito); lsub = ito; ifrom = usub; - ito = (int_t *) ((char*)lsub + xlsub[ndim] * iword); + ito = (int_t *) ((char*)lsub + xlsub[ndim] * liword); copy_mem_int(xusub[ndim], ifrom, ito); usub = ito; - last = (char*)usub + xusub[ndim] * iword; + last = (char*)usub + xusub[ndim] * liword; fragment = (char*) (((char*)Glu->stack.array + Glu->stack.top1) - last); Glu->stack.used -= (int_t)(intptr_t) fragment; Glu->stack.top1 -= (int_t)(intptr_t) fragment; diff --git a/SRC/dmemory.c b/SRC/dmemory.c index 190769c8..7331c0dd 100644 --- a/SRC/dmemory.c +++ b/SRC/dmemory.c @@ -191,7 +191,7 @@ dLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int panel_size, double fill_ratio, SuperMatrix *L, SuperMatrix *U, GlobalLU_t *Glu, int **iwork, double **dwork) { - int info, iword, dword; + int info, iword, liword, dword; SCformat *Lstore; NCformat *Ustore; int *xsup, *supno; @@ -203,6 +203,7 @@ dLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int_t nzlmax, nzumax, nzlumax; iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(double); Glu->n = n; Glu->num_expansions = 0; @@ -217,8 +218,9 @@ dLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, //nzlmax = SUPERLU_MAX(1, fill_ratio/4.) * annz; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else { dSetupSpace(work, lwork, Glu); } @@ -239,9 +241,9 @@ dLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, } else { xsup = (int *)duser_malloc((n+1) * iword, HEAD, Glu); supno = (int *)duser_malloc((n+1) * iword, HEAD, Glu); - xlsub = duser_malloc((n+1) * iword, HEAD, Glu); - xlusup = duser_malloc((n+1) * iword, HEAD, Glu); - xusub = duser_malloc((n+1) * iword, HEAD, Glu); + xlsub = duser_malloc((n+1) * liword, HEAD, Glu); + xlusup = duser_malloc((n+1) * liword, HEAD, Glu); + xusub = duser_malloc((n+1) * liword, HEAD, Glu); } lusup = (double *) dexpand( &nzlumax, LUSUP, 0, 0, Glu ); @@ -256,7 +258,7 @@ dLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, SUPERLU_FREE(lsub); SUPERLU_FREE(usub); } else { - duser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*iword, + duser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*liword, HEAD, Glu); } nzlumax /= 2; @@ -291,8 +293,9 @@ dLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, nzlumax = Glu->nzlumax; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else if ( lwork == 0 ) { Glu->MemModel = SYSTEM; } else { @@ -624,14 +627,14 @@ void void dStackCompress(GlobalLU_t *Glu) { - register int iword, dword, ndim; + register int liword, dword, ndim; char *last, *fragment; int_t *ifrom, *ito; double *dfrom, *dto; int_t *xlsub, *lsub, *xusub, *usub, *xlusup; double *ucol, *lusup; - iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(double); ndim = Glu->n; @@ -649,16 +652,16 @@ dStackCompress(GlobalLU_t *Glu) ucol = dto; ifrom = lsub; - ito = (int_t *) ((char*)ucol + xusub[ndim] * iword); + ito = (int_t *) ((char*)ucol + xusub[ndim] * dword); copy_mem_int(xlsub[ndim], ifrom, ito); lsub = ito; ifrom = usub; - ito = (int_t *) ((char*)lsub + xlsub[ndim] * iword); + ito = (int_t *) ((char*)lsub + xlsub[ndim] * liword); copy_mem_int(xusub[ndim], ifrom, ito); usub = ito; - last = (char*)usub + xusub[ndim] * iword; + last = (char*)usub + xusub[ndim] * liword; fragment = (char*) (((char*)Glu->stack.array + Glu->stack.top1) - last); Glu->stack.used -= (int_t)(intptr_t) fragment; Glu->stack.top1 -= (int_t)(intptr_t) fragment; diff --git a/SRC/smemory.c b/SRC/smemory.c index 7e535e0d..128db89f 100644 --- a/SRC/smemory.c +++ b/SRC/smemory.c @@ -191,7 +191,7 @@ sLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int panel_size, float fill_ratio, SuperMatrix *L, SuperMatrix *U, GlobalLU_t *Glu, int **iwork, float **dwork) { - int info, iword, dword; + int info, iword, liword, dword; SCformat *Lstore; NCformat *Ustore; int *xsup, *supno; @@ -203,6 +203,7 @@ sLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int_t nzlmax, nzumax, nzlumax; iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(float); Glu->n = n; Glu->num_expansions = 0; @@ -217,8 +218,9 @@ sLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, //nzlmax = SUPERLU_MAX(1, fill_ratio/4.) * annz; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else { sSetupSpace(work, lwork, Glu); } @@ -239,9 +241,9 @@ sLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, } else { xsup = (int *)suser_malloc((n+1) * iword, HEAD, Glu); supno = (int *)suser_malloc((n+1) * iword, HEAD, Glu); - xlsub = suser_malloc((n+1) * iword, HEAD, Glu); - xlusup = suser_malloc((n+1) * iword, HEAD, Glu); - xusub = suser_malloc((n+1) * iword, HEAD, Glu); + xlsub = suser_malloc((n+1) * liword, HEAD, Glu); + xlusup = suser_malloc((n+1) * liword, HEAD, Glu); + xusub = suser_malloc((n+1) * liword, HEAD, Glu); } lusup = (float *) sexpand( &nzlumax, LUSUP, 0, 0, Glu ); @@ -256,7 +258,7 @@ sLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, SUPERLU_FREE(lsub); SUPERLU_FREE(usub); } else { - suser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*iword, + suser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*liword, HEAD, Glu); } nzlumax /= 2; @@ -291,8 +293,9 @@ sLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, nzlumax = Glu->nzlumax; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else if ( lwork == 0 ) { Glu->MemModel = SYSTEM; } else { @@ -624,14 +627,14 @@ void void sStackCompress(GlobalLU_t *Glu) { - register int iword, dword, ndim; + register int liword, dword, ndim; char *last, *fragment; int_t *ifrom, *ito; float *dfrom, *dto; int_t *xlsub, *lsub, *xusub, *usub, *xlusup; float *ucol, *lusup; - iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(float); ndim = Glu->n; @@ -649,16 +652,16 @@ sStackCompress(GlobalLU_t *Glu) ucol = dto; ifrom = lsub; - ito = (int_t *) ((char*)ucol + xusub[ndim] * iword); + ito = (int_t *) ((char*)ucol + xusub[ndim] * dword); copy_mem_int(xlsub[ndim], ifrom, ito); lsub = ito; ifrom = usub; - ito = (int_t *) ((char*)lsub + xlsub[ndim] * iword); + ito = (int_t *) ((char*)lsub + xlsub[ndim] * liword); copy_mem_int(xusub[ndim], ifrom, ito); usub = ito; - last = (char*)usub + xusub[ndim] * iword; + last = (char*)usub + xusub[ndim] * liword; fragment = (char*) (((char*)Glu->stack.array + Glu->stack.top1) - last); Glu->stack.used -= (int_t)(intptr_t) fragment; Glu->stack.top1 -= (int_t)(intptr_t) fragment; diff --git a/SRC/zmemory.c b/SRC/zmemory.c index b61a8692..b527be4d 100644 --- a/SRC/zmemory.c +++ b/SRC/zmemory.c @@ -191,7 +191,7 @@ zLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int panel_size, double fill_ratio, SuperMatrix *L, SuperMatrix *U, GlobalLU_t *Glu, int **iwork, doublecomplex **dwork) { - int info, iword, dword; + int info, iword, liword, dword; SCformat *Lstore; NCformat *Ustore; int *xsup, *supno; @@ -203,6 +203,7 @@ zLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, int_t nzlmax, nzumax, nzlumax; iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(doublecomplex); Glu->n = n; Glu->num_expansions = 0; @@ -217,8 +218,9 @@ zLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, //nzlmax = SUPERLU_MAX(1, fill_ratio/4.) * annz; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else { zSetupSpace(work, lwork, Glu); } @@ -239,9 +241,9 @@ zLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, } else { xsup = (int *)zuser_malloc((n+1) * iword, HEAD, Glu); supno = (int *)zuser_malloc((n+1) * iword, HEAD, Glu); - xlsub = zuser_malloc((n+1) * iword, HEAD, Glu); - xlusup = zuser_malloc((n+1) * iword, HEAD, Glu); - xusub = zuser_malloc((n+1) * iword, HEAD, Glu); + xlsub = zuser_malloc((n+1) * liword, HEAD, Glu); + xlusup = zuser_malloc((n+1) * liword, HEAD, Glu); + xusub = zuser_malloc((n+1) * liword, HEAD, Glu); } lusup = (doublecomplex *) zexpand( &nzlumax, LUSUP, 0, 0, Glu ); @@ -256,7 +258,7 @@ zLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, SUPERLU_FREE(lsub); SUPERLU_FREE(usub); } else { - zuser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*iword, + zuser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*liword, HEAD, Glu); } nzlumax /= 2; @@ -291,8 +293,9 @@ zLUMemInit(fact_t fact, void *work, int_t lwork, int m, int n, int_t annz, nzlumax = Glu->nzlumax; if ( lwork == -1 ) { - return ( GluIntArray(n) * iword + TempSpace(m, panel_size) - + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n ); + return ( 2 * (n+1) * iword + 3 * (n+1) * liword + + TempSpace(m, panel_size) + + (nzlmax+nzumax)*liword + (nzlumax+nzumax)*dword + n ); } else if ( lwork == 0 ) { Glu->MemModel = SYSTEM; } else { @@ -624,14 +627,14 @@ void void zStackCompress(GlobalLU_t *Glu) { - register int iword, dword, ndim; + register int liword, dword, ndim; char *last, *fragment; int_t *ifrom, *ito; doublecomplex *dfrom, *dto; int_t *xlsub, *lsub, *xusub, *usub, *xlusup; doublecomplex *ucol, *lusup; - iword = sizeof(int); + liword = sizeof(int_t); dword = sizeof(doublecomplex); ndim = Glu->n; @@ -649,16 +652,16 @@ zStackCompress(GlobalLU_t *Glu) ucol = dto; ifrom = lsub; - ito = (int_t *) ((char*)ucol + xusub[ndim] * iword); + ito = (int_t *) ((char*)ucol + xusub[ndim] * dword); copy_mem_int(xlsub[ndim], ifrom, ito); lsub = ito; ifrom = usub; - ito = (int_t *) ((char*)lsub + xlsub[ndim] * iword); + ito = (int_t *) ((char*)lsub + xlsub[ndim] * liword); copy_mem_int(xusub[ndim], ifrom, ito); usub = ito; - last = (char*)usub + xusub[ndim] * iword; + last = (char*)usub + xusub[ndim] * liword; fragment = (char*) (((char*)Glu->stack.array + Glu->stack.top1) - last); Glu->stack.used -= (int_t)(intptr_t) fragment; Glu->stack.top1 -= (int_t)(intptr_t) fragment;