Open conradsnicta opened 5 years ago
Can you submit a PR for this?
I got my hands full fixing the same issue in Armadillo :)
Ha, ok, thanks for the heads up :)
Are there known cases where this results in incorrect runtime behavior?
The above first looked like a compiler problem.
A while back I've also had a few reports of strange crashes involving code compiled by non-gcc compilers. I couldn't reproduce them on my setup, but I now strongly suspect that the "hidden" args were the culprit.
The prototypes for Fortran LAPACK functions appear to be incomplete in Dlib. This can lead to undefined behaviour with recent releases of gcc due to more aggressive compiler optimisations.
According to gfortran passing conventions (followed by other fortran compilers as well), every
char*
argument also has an associated "hidden" argument which specifies the number of characters. The type of the "hidden" argument may vary across compilers and/or compiler versions. The position of each "hidden" argument is also compiler dependent, though seems to be typically tacked onto the end of the function definition. https://gcc.gnu.org/onlinedocs/gfortran/Argument-passing-conventions.htmlAs an example in Dlib, the current prototype for
dsyev
in https://github.com/davisking/dlib/blob/master/dlib/matrix/lapack/syev.h is defined as:void DLIB_FORTRAN_ID(dsyev) (const char *jobz, const char *uplo, const integer *n, double *a, const integer *lda, double *w, double *work, const integer *lwork, integer *info);
But it probably should be:
void DLIB_FORTRAN_ID(dsyev) (const char *jobz, const char *uplo, const integer *n, double *a, const integer *lda, double *w, double *work, const integer *lwork, integer *info, blas_len jobz_len, blas_len uplo_len);
where
blas_len
is typically a 32 bit unsigned int on 32 bit platforms, and 64 bit unsigned int on 64 bit platforms. (This does not apply to gcc versions <= 7, where it is alwaysint
).Avoiding the use of the blas_len arguments appeared to work, but recently gcc has started to optimise more heavily, leading to stack overwrites when these arguments are not used.
The same bug affects Armadillo, R, and a lot of other software in C and C++ that uses LAPACK:
CC: @dodomorandi