> On Apr 5, 2019, at 19:08, Mo Zhou <lu...@debian.org> wrote: > > Hi mentors, > > This question tightly associates with my ongoing work for Debian's > BLAS/LAPACK packages, specifically the 32-bit and 64-bit variants. > I encountered a problem that I don't fully understand so I think I > need some help at this point. > > Assume we have the following library "libfoo.c": > > #include <stddef.h> > float sasum64(size_t N, const float *X, size_t incX) > { > float asum = 0.; > for (size_t i = 0; i < N; i++) { > asum += (X[i*incX] > 0.) ? X[i*incX] : -X[i*incX]; > } > return asum; > } > float sasum32(int N, const float *X, int incX) > { > float asum = 0.; > for (int i = 0; i < N; i++) { > asum += (X[i*incX] > 0.) ? X[i*incX] : -X[i*incX]; > } > return asum; > } > > compiled as libfoo.so: gcc -shared -fPIC libfoo.c -o libfoo.so > And we have the following application "app.c" which **deliberately** > misuse the index type: > > #include <stdio.h> > #include <stddef.h> > float sasum64(int N, const float *X, int incX); > float sasum32(size_t N, const float *X, size_t incX); > > int main(void) > { > float a[] = {1., 2., -3.}; > printf("%f, %f\n", sasum32(3, a, 1), sasum64(3, a, 1)); > return 0; > } > > Then we compile and run the program: > > gcc app.c -fPIC -lfoo -L. > LD_LIBRARY_PATH=. ./a.out > 2:00:56 >>>> 6.000000, 6.000000 > > My questions are: > > 1. Why doesn't the application segfault, since it has already > misused the index (N and incX) type? > > 2. Did we avoid SIGSEGV because the arguments used to call > sasum32 or sasum64 are aligned in 64-bits? But that's still > strange due to little-endianess... > > 3. How can I make the app.c segfault? > > Thanks in advance :-) >
I do not know why this question was addressed to Debian and Gentoo as it seems to have nothing specific to do with either, but let me attempt a response. With nothing further to go on, I am taking a guess that your platform is x86-64. The 32-bit values passed to the mis-prototyped sasum64 as N and incX will be zero extended to 64-bit values as per the ABI. I know neither why nor where you expect this program to segfault, so unfortunately I can’t comment further. You might want to try Stack Overflow for something like this.