llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[powerpc] powerpc-linux-gnu accesses memory below the stack

Open markus-oberhumer opened this issue 11 years ago • 9 comments

Bugzilla Link 22819
Version 3.6
OS Linux
CC @chmeeedalf,@hfinkel

Extended Description

powerpc-linux-gnu accesses memory below the stack - please see "-4(1)" below.

This is a nasty one as it only shows up under rare conditions - it was quite hard to find the real cause of the problem.

$ cat test.c

int foo(int *);

int test(int *a) { return foo(a) + 1; }

// EOF

$ clang-3.6.0 -target powerpc-linux-gnu -O2 -Wall -save-temps -c test.c

$ cat test.s

    .text
    .file   "test.bc"
    .globl  test
    .align  2
    .type   test,@function

test: # @​test

BB#0: # %entry

    mflr 0
    stw 31, -4(1)
    stw 0, 4(1)
    stwu 1, -16(1)
    mr 31, 1
    bl foo
    addi 3, 3, 1
    addi 1, 1, 16
    lwz 0, 4(1)
    lwz 31, -4(1)
    mtlr 0
    blr

.Ltmp0: .size test, .Ltmp0-test .ident "clang version 3.6.0 (tags/RELEASE_360/final)" .section ".note.GNU-stack","",@progbits

markus-oberhumer avatar Mar 06 '15 14:03 markus-oberhumer

It seems that extra prologue/epilogue spilling of r30 ("<<<<<") is actually not needed at all because R30 is included in CalleeSavedRegs.

So would it be safe to simply disable the test "FI->usesPICBase()" in PPCFrameLowering.cpp lines 711 and 1089 ?

Thanks, Markus

Do you mean to remove just the check, and unconditionally save/restore? Or do you mean to remove the whole block in each?

chmeeedalf avatar May 13 '15 07:05 chmeeedalf

I added in the explicit save/restore of R30 in the prologue/epilogue because during my testing there were cases where not doing so would not save $r30, and I could not determine why, so punted. Unfortunately, I don't have time right now to test if this has changed.

chmeeedalf avatar Mar 17 '15 01:03 chmeeedalf

...

It seems that extra prologue/epilogue spilling of r30 ("<<<<<") is actually not needed at all because R30 is included in CalleeSavedRegs.

So would it be safe to simply disable the test "FI->usesPICBase()" in PPCFrameLowering.cpp lines 711 and 1089 ?

[+Justin who implemented the PIC support]

Thanks, Markus

hfinkel avatar Mar 16 '15 19:03 hfinkel

It seems that in our scenario we can work around spilling r31 by using -fomit-frame-pointer, so I have a question about r30.

Consider this code:

$ cat test2.c

extern int foo; int *test(void) { return &foo; }

// EOF

$ clang-3.6.0 -target powerpc-linux-gnu -fPIC -O2 -fomit-frame-pointer -Wall -save-temps -c test2.c

$ cat test2.s

[...]

test:

BB#0: # %entry

    mflr 0
    stw 30, -8(1)        <<<<<
    stw 0, 4(1)
    stwu 1, -16(1)
    stw 30, 8(1)                    # 4-byte Folded Spill
    bl .L0$pb

.L0$pb: mflr 30 lwz 3, .L0$poff-.L0$pb(30) add 30, 3, 30 lwz 3, .LC1-.LTOC(30) lwz 30, 8(1) # 4-byte Folded Reload addi 1, 1, 16 lwz 0, 4(1) lwz 30, -8(1) <<<<< mtlr 0 blr

[...]

It seems that extra prologue/epilogue spilling of r30 ("<<<<<") is actually not needed at all because R30 is included in CalleeSavedRegs.

So would it be safe to simply disable the test "FI->usesPICBase()" in PPCFrameLowering.cpp lines 711 and 1089 ?

Thanks, Markus

markus-oberhumer avatar Mar 13 '15 11:03 markus-oberhumer

Please note that I do consider this a critical bug rendering llvm ppc32-elf completely unusable for production code.

In the meantime I've also looked into this issue a little bit deeper and have found several "FIXME" in PPCFrameLowering.cpp, including one from SVN rev 157133 ;-)

Unfortunately I personally don't know enough about PPC to be able to fix this.

markus-oberhumer avatar Mar 09 '15 10:03 markus-oberhumer

I've just noticed that there is an option -disable-red-zone (but -mno-red-zone is not an allowed option??), and in fact

clang-3.6.0 -target powerpc-apple-darwin -Xclang -disable-red-zone

also accesses memory below the stack.

Is there a specific reason why the stack pointer is not adjusted first?

No, and it really should be adjusted first for ppc32. Patches welcome.

hfinkel avatar Mar 06 '15 20:03 hfinkel

I've just noticed that there is an option -disable-red-zone (but -mno-red-zone is not an allowed option??), and in fact

clang-3.6.0 -target powerpc-apple-darwin -Xclang -disable-red-zone

also accesses memory below the stack.

Is there a specific reason why the stack pointer is not adjusted first?

markus-oberhumer avatar Mar 06 '15 17:03 markus-oberhumer

Indeed; this is a known issue. The frame-lowering code assumes the availability of a red zone on all targets. However, this is actually only legal on PPC64/Linux and on Darwin. It is not legal on PPC/Linux.

hfinkel avatar Mar 06 '15 16:03 hfinkel

https://godbolt.org/z/c5fT4o1cW

the trunk should generate expected instructions.

chenzheng1030 avatar Jan 07 '24 03:01 chenzheng1030