jcmvbkbc / gcc-xtensa

gcc for xtensa
GNU General Public License v2.0
133 stars 58 forks source link

Bad prologue generated for call0 ABI functions #13

Closed jcmvbkbc closed 5 years ago

jcmvbkbc commented 5 years ago

call0 ABI functions that require 129 to 1024 bytes of stack frame and don't save any incoming registers on the stack don't get stack pointer adjustment prologue. This results in stack frame corruption in the calling functions and wrong stack pointer upon return from the function. And inevitable crash as a result.

Reproducer:

#include <stdarg.h>
long x;
void
f3 (int i, ...)
{ 
  va_list aps[10];
  va_start (aps[4], i);
  x = va_arg (aps[4], long);
  va_end (aps[4]);
}

Generated code:

0040069c <f3>:
  40069c:       216132          s32i    a3, a1, 132
  40069f:       226142          s32i    a4, a1, 136
  4006a2:       236152          s32i    a5, a1, 140
  4006a5:       246162          s32i    a6, a1, 144
  4006a8:       256172          s32i    a7, a1, 148
  4006ab:       80a022          movi    a2, 128
  4006ae:       212a            add.n   a2, a1, a2
  4006b0:       d129            s32i.n  a2, a1, 52
  4006b2:       c129            s32i.n  a2, a1, 48
  4006b4:       830c            movi.n  a3, 8
  4006b6:       e139            s32i.n  a3, a1, 56
  4006b8:       223a            add.n   a2, a2, a3
  4006ba:       fcc222          addi    a2, a2, -4
  4006bd:       0238            l32i.n  a3, a2, 0
  4006bf:       ffdf21          l32r    a2, 40063c <bar+0xa8>
  4006c2:       0239            s32i.n  a3, a2, 0
  4006c4:       a0a092          movi    a9, 160
  4006c7:       119a            add.n   a1, a1, a9
  4006c9:       f00d            ret.n

All gcc versions with call0 ABI support and the current gcc trunk are affected.

jcmvbkbc commented 5 years ago

Fixed in the mainline by the commit 6c552ff765c1b02d3ec9094f92c1ce58f8cda14b