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 :-)

Reply via email to