public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "alexander.grund@tu-dresden.de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/100799] New: Stackoverflow in optimized code on PPC
Date: Thu, 27 May 2021 11:20:38 +0000	[thread overview]
Message-ID: <bug-100799-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100799

            Bug ID: 100799
           Summary: Stackoverflow in optimized code on PPC
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexander.grund@tu-dresden.de
  Target Milestone: ---

Created attachment 50879
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50879&action=edit
Disassembly of dbgebal_ in debug and release modes

Quick summary of the use case: When using FlexiBLAS with OpenBLAS I noticed
corruption of the parameters passed to OpenBLAS functions. FlexiBLAS basically
provides a BLAS interface where each function is a stub that forwards the
arguments to a real BLAS lib, like OpenBLAS

Example:
void FC_GLOBAL(dgebal,DGEBAL)(char* job, blasint* n, double* a, blasint* lda,
blasint* ilo, blasint* ihi, double* scale, blasint* info)
{
        void (*fn) (void* job, void* n, void* a, void* lda, void* ilo, void*
ihi, void* scale, void* info);

        fn = current_backend->lapack.dgebal.f77_blas_function; 

                fn((void*) job, (void*) n, (void*) a, (void*) lda, (void*) ilo,
(void*) ihi, (void*) scale, (void*) info); 

        return;
}
void dgebal(char* job, blasint* n, double* a, blasint* lda, blasint* ilo,
blasint* ihi, double* scale, blasint* info)
__attribute__((alias(MTS(FC_GLOBAL(dgebal,DGEBAL)))));

Due to the alias and the real BLAS lib being loader after FlexiBLAS also the
calls from an OpenBLAS function to another OpenBLAS function get routed through
FlexiBLAS.

Now I noticed that the parameter "N" at
https://github.com/xianyi/OpenBLAS/blob/v0.3.15/lapack-netlib/SRC/dgeev.f#L369
gets messed up during the call at
https://github.com/xianyi/OpenBLAS/blob/v0.3.15/lapack-netlib/SRC/dgeev.f#L363
which I traced to FlexiBLAS pushing the register that holds it, calling the
OpenBLAS DGEBAL and restoring it afterwards but the stack entry where it came
from gets changed by DGEBAL

So the actual Bug here is that GCC generates code for DGEBAL which uses a write
outside of the allocated stack.

The dissassembly of the dgebal_ function shows "stdu    r1,-368(r1)" in the
prologue and "std     r25,440(r1)" later, which is the instruction that
overwrites the saved register from the calling function.
As far as I can tell an offset of 440 onto r1, which is bigger than the 368
"allocated" by the stdu is invalid.
The line reported by GDB for the overwriting instruction is
https://github.com/xianyi/OpenBLAS/blob/v0.3.15/lapack-netlib/SRC/dgebal.f#L328

The command used to compile the file is: gfortran -fno-math-errno -Wall
-frecursive -fno-optimize-sibling-calls -m64 -fopenmp -fPIC -O2 -fno-fast-math
-mcpu=power9 -mtune=power9  -DUSE_OPENMP -fopenmp -fno-optimize-sibling-calls
-g  -c -o dgebal.o dgebal.f

Replacing the "O2" by "Og" changes the prologue to "stdu    r1,-336(r1)" and
the max offset used for std on r1 is 328. Using this works with FlexiBLAS,
hence I suspect an optimization issue which leads to more spills but doesn't
update the stack size.

Reproduced with GCC 10.2.0, 10.3.0, 11.1.0

             reply	other threads:[~2021-05-27 11:20 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-27 11:20 alexander.grund@tu-dresden.de [this message]
2021-05-28 16:42 ` [Bug target/100799] " alexander.grund@tu-dresden.de
2021-06-01 19:08 ` bergner at gcc dot gnu.org
2021-06-01 21:09 ` segher at gcc dot gnu.org
2021-06-02  0:31 ` amodra at gmail dot com
2021-10-05 22:45 ` bergner at gcc dot gnu.org
2022-01-09 11:13 ` kenneth.hoste at ugent dot be
2022-07-08 10:53 ` alexander.grund@tu-dresden.de
2022-07-08 16:38 ` bergner at gcc dot gnu.org
2022-07-14 20:10 ` bergner at gcc dot gnu.org
2022-07-20 11:45 ` alexander.grund@tu-dresden.de
2022-07-20 14:14 ` alexander.grund@tu-dresden.de
2022-07-20 17:42 ` segher at gcc dot gnu.org
2022-07-20 17:59 ` segher at gcc dot gnu.org
2022-09-13 19:29 ` segher at gcc dot gnu.org
2022-09-19  5:46 ` jskumari at gcc dot gnu.org
2022-09-20 22:45 ` segher at gcc dot gnu.org
2022-10-17  8:17 ` jskumari at gcc dot gnu.org
2022-10-17  9:42 ` jskumari at gcc dot gnu.org
2022-10-17 17:10 ` jskumari at gcc dot gnu.org
2022-10-31  3:00 ` linkw at gcc dot gnu.org
2022-11-09 16:43 ` jskumari at gcc dot gnu.org
2023-06-19 20:25 ` bergner at gcc dot gnu.org
2024-02-21  7:38 ` jakub at gcc dot gnu.org
2024-02-22  2:51 ` bergner at gcc dot gnu.org
2024-02-22 14:44 ` bergner at gcc dot gnu.org
2024-02-22 14:59 ` jakub at gcc dot gnu.org
2024-02-25  0:39 ` bergner at gcc dot gnu.org
2024-02-26  9:58 ` jakub at gcc dot gnu.org
2024-02-27  0:45 ` bergner at gcc dot gnu.org
2024-02-27  7:26 ` jakub at gcc dot gnu.org
2024-02-27 15:30 ` bergner at gcc dot gnu.org
2024-03-01 15:25 ` bergner at gcc dot gnu.org
2024-03-22  7:44 ` aagarwa at gcc dot gnu.org
2024-03-22  7:45 ` aagarwa at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-100799-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).