trealla: GSL: Bugs

This bundle is mathematically error free:

matrix_calloc
permutation_alloc
LU_decomp
LU_det

Exercisers:

$ cat x.c
#include <stdio.h>
#include <gsl/gsl_version.h>
#include <gsl/gsl_linalg.h>

int main (int argc, char *argv[]) {
  int sig, stat;
  double det;
  int rows = atoi(argv[1]);
  int cols = atoi(argv[2]);
  printf("v%s %dx%d\n",GSL_VERSION,rows,cols);
  gsl_matrix *mat = gsl_matrix_calloc(rows, cols);
  gsl_permutation *perm = gsl_permutation_alloc(rows);
  stat = gsl_linalg_LU_decomp(mat, perm, &sig);
   det = gsl_linalg_LU_det(mat, sig);
  printf("%lf\n",det);
  gsl_permutation_free(perm);
  gsl_matrix_free(mat);
  return 0;
}

$ cat x.pl
:- use_module(library(gsl)).
tst(Rows,Cols)  :-
              gsl_matrix_calloc(Rows, Cols, M),
              gsl_permutation_alloc(Rows, P),
              gsl_linalg_LU_decomp(M, P, Sig, Stat),
              gsl_linalg_LU_det(M, Sig, Det),
              write(Det),nl,
              gsl_permutation_free(P),
              gsl_matrix_free(M).
RESULTS:

GSL 2.7.1
$ ./a.out 127 127
v2.7.1 127x127
-nan  #  Libgsl: known bug in v2.7
$ 
$ tpl x.pl
?- tst(127,127).
-.0nan   % Trealla morphs -nan into -.0nan
   true.
?- halt.
$

GSL 2.8
$ ./a.out 127 127
v2.8 127x127
: 
# printf in function gsl_linalg_LU_decomp() of gsl-git/linalg/lu.c
# convert ipiv array to permutation
# unsigned int pivi = gsl_vector_uint_get(ipiv, i);
:  
FF pivi 13 i 13  
FF pivi 14 i 14
FF pivi 15 i 15
FF pivi 0 i 16  #  Trealla Crash here
FF pivi 0 i 17
FF pivi 0 i 18
: 
FF pivi 0 i 124
FF pivi 0 i 125
FF pivi 0 i 126
-0.000000   # expected, v2.7 bug '-nan' has been fixed
$

$ tpl x.pl
?- tst(127,127).  % First Call
: 
FF pivi 13 i 13
FF pivi 14 i 14
FF pivi 15 i 15
FF pivi 210299952 i 16  % looks like Trealla puts an address somewhere in an array
Segmentation fault (core dumped)
$

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Happening I guess in next release…

[Fri 27 Dec 2024 12:07:28 PM UTC, comment #4:](https://savannah.gnu.org/bugs/?66026#comment4) 
Attached is a patch that fixes the subtle bug discovered here. I've implemented the suggested change (executing the mentioned for-loop only when the status == 0) in both lu.c and luc.c.

([file #56718](https://file.savannah.gnu.org/file/bug_66026.patch?file_id=56718))

[Christian Krueger <mangrove84>](https://savannah.gnu.org/users/mangrove84)
fyi
More info for if those gsl guys start a discussion:

1.) ./a.out 128 128 or 256 256 or 512 512 or 1024 1024 does not crash,
    so you don't always get the fault.

2.) The problem might be the status of a singular matrix:

status = LU_decomp_L3 (&AL.matrix, ipiv);

If not singular then status = 0 and all fields of vector ipiv are valid(have been processed).

if singular then status = number of fields which have been processed.

For example: singular 127x127 status = 16
means that this is wrong:
for (i = 0; i < minMN; ++i) {  # the segfault area

Better is this:
size_t damage = minMN;
if (status) damage = status; 
for (i = 0; i < damage; ++i) {
}  

Even better (perhaps)
if (status)  { ; /* singular, do nothing */
} else { 
   for (i = 0; i < minMN; ++i) { 
   : 
}
gsl_vector_uint_free(ipiv);
return status;

Performance back: no calloc and in case of singular dismiss signum as valueless.
Buggy gsl 2.7 does not crash because it handles a singular matrix as if it is non-singular:
status = LU_decomp_L3 (&AL.matrix, ipiv);
status is always 0 (vector fields are 'filled' according to alloc 127 for example).

Buggy gsl 2.8 stops when it is clear that its a singular matrix and returns a value >= 1.
status = LU_decomp_L3 (&AL.matrix, ipiv);
status == 16 (for example), fields(-index) > 16 stay untouched hence the crash(see above).
Here we are

$ cat x.c
: 
  gsl_matrix *mat = gsl_matrix_calloc(rows, cols);
  gsl_permutation *perm = gsl_permutation_alloc(rows);

{
int i, j;
gsl_matrix *m = gsl_matrix_alloc(rows, cols);
for (i = 0; i < rows; i++)
    for (j = 0; j < cols; j++)
        gsl_matrix_set(m, i, j, 2000000000);
gsl_matrix_free(m);
}

  stat = gsl_linalg_LU_decomp(mat, perm, &sig);
   det = gsl_linalg_LU_det(mat, sig);
: 

$ gcc ...
$ ./a.out 127 127
v2.8 127x127
: 
FF pivi 15 i 15
FF pivi 0 i 16
FF pivi 1105055077 i 17
Segmentation fault (core dumped)
$