target/6899: construction of C++ classes with less than 5 bytes of storage can cause stack misalignment
thousel@usa.net
thousel@usa.net
Sat Jun 1 13:56:00 GMT 2002
>Number: 6899
>Category: target
>Synopsis: construction of C++ classes with less than 5 bytes of storage can cause stack misalignment
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Sat Jun 01 13:56:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: thousel@usa.net
>Release: gcc compiler 3.1 m68hc1x-20020517
>Organization:
>Environment:
Cygwin/Win2k
>Description:
Under certain scenarios, it appears that a loop of construction of one class followed by a construction of another class with less than 5 bytes of storage will cause stack misalignment.
>How-To-Repeat:
see comments in attachment
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="test.txt"
Content-Disposition: inline; filename="test.txt"
> m6812-elf-gcc -v
Reading specs from /cygdrive/c/m6812-elf-tools/lib/gcc-lib/m6812-elf/3.1/specs
Configured with: ./configure --target=m6812-elf --program-prefix=m6812-elf- --prefix=/cygdrive/c/m6812-elf-tools --exec-prefix=/cygdrive/c/m6812-elf-tools --enable-languages=c,c++
Thread model: single
gcc version 3.1 m68hc1x-20020517
Suppose you have the following code called test.cpp:
class bar {
public :
bar(int index);
};
class foo
{
private:
unsigned short attr1;
unsigned short attr2;
unsigned char attr3;
};
class glerg {
public :
static void Init();
private :
static foo* pFoo;
};
void glerg::Init() {
int i;
bar* pBar[4];
for (i = 0; i < 4; i++)
pBar[i] = new bar(i);
pFoo = new foo();
}
Compile it with:
> m6812-elf-gcc -m68hc12 -mshort -Os -mauto-incdec -fomit-frame-pointer -fno-rtti -fno-exceptions -S test.cpp
This will create the following test.s:
;;;-----------------------------------------
;;; Start MC68HC11 gcc assembly output
;;; gcc compiler 3.1 m68hc1x-20020517
;;; Command: /cygdrive/c/m6812-elf-tools/lib/gcc-lib/m6812-elf/3.1/cc1plus.exe -D__GNUC__=3 -D__GNUC_MINOR__=1 -D__GNUC_PATCHLEVEL__=0 -Dmc68hc1x -D__mc68hc1x__ -D__mc68hc1x -D__OPTIMIZE_SIZE__ -D__OPTIMIZE__ -D__STDC_HOSTED__=1 -D__HAVE_SHORT_INT__ -D__INT__=16 -D__INT_MAX__=32767 -Dmc6812 -DMC6812 -Dmc68hc12 test.cpp -D__GNUG__=3 -D__DEPRECATED -D__GXX_ABI_VERSION=100 -quiet -dumpbase test.cpp -m68hc12 -mshort -mauto-incdec -Os -fomit-frame-pointer -fno-rtti -fno-exceptions -o test.s
;;; Compiled: Sat Jun 1 15:42:11 2002
;;; (META)compiled by GNU C version 2.95.3-5 (cygwin special).
;;;-----------------------------------------
.file "test.cpp"
; extern _Znwt
; extern _ZN3barC1Ei
; extern _ZN5glerg4pFooE
; extern memset
.sect .text
.globl _ZN5glerg4InitEv
.type _ZN5glerg4InitEv,@function
_ZN5glerg4InitEv:
leas -16,sp
clr 9,sp
clr 8,sp
sts 14,sp
.L6:
ldd #1
bsr _Znwt
std 10,sp
*** movw 8,sp,2,-sp
ldd 12,sp
bsr _ZN3barC1Ei
ldx 16,sp
movw 12,sp,2,x+
stx 16,sp
*** pulx
ldx 8,sp
inx
stx 8,sp
cpx #3
ble .L6
ldd #5
bsr _Znwt
std 12,sp
movw #5,2,-sp
clr 1,-sp
clr 1,-sp
ldd 16,sp
bsr memset
ldx 16,sp
stx _ZN5glerg4pFooE
leas 20,sp
rts
.Lfe1:
.size _ZN5glerg4InitEv,.Lfe1-_ZN5glerg4InitEv
.ident "GCC: (GNU) 3.1 m68hc1x-20020517"
Note the two lines that begin with "***". The first line autodecrements the
stack by two, and the second uses "pulx" to realign it.
Now we comment out one of the attributes in class foo:
class bar {
public :
bar(int index);
};
class foo
{
private:
unsigned short attr1;
unsigned short attr2;
// unsigned char attr3;
};
class glerg {
public :
static void Init();
private :
static foo* pFoo;
};
void glerg::Init() {
int i;
bar* pBar[4];
for (i = 0; i < 4; i++)
pBar[i] = new bar(i);
pFoo = new foo();
}
Compile it again with:
> m6812-elf-gcc -m68hc12 -mshort -Os -mauto-incdec -fomit-frame-pointer -fno-rtti -fno-exceptions -S test.cpp
This will create the following test.s:
;;;-----------------------------------------
;;; Start MC68HC11 gcc assembly output
;;; gcc compiler 3.1 m68hc1x-20020517
;;; Command: /cygdrive/c/m6812-elf-tools/lib/gcc-lib/m6812-elf/3.1/cc1plus.exe -D__GNUC__=3 -D__GNUC_MINOR__=1 -D__GNUC_PATCHLEVEL__=0 -Dmc68hc1x -D__mc68hc1x__ -D__mc68hc1x -D__OPTIMIZE_SIZE__ -D__OPTIMIZE__ -D__STDC_HOSTED__=1 -D__HAVE_SHORT_INT__ -D__INT__=16 -D__INT_MAX__=32767 -Dmc6812 -DMC6812 -Dmc68hc12 test.cpp -D__GNUG__=3 -D__DEPRECATED -D__GXX_ABI_VERSION=100 -quiet -dumpbase test.cpp -m68hc12 -mshort -mauto-incdec -Os -fomit-frame-pointer -fno-rtti -fno-exceptions -o test.s
;;; Compiled: Sat Jun 1 15:44:24 2002
;;; (META)compiled by GNU C version 2.95.3-5 (cygwin special).
;;;-----------------------------------------
.file "test.cpp"
; extern _Znwt
; extern _ZN3barC1Ei
; extern _ZN5glerg4pFooE
.sect .text
.globl _ZN5glerg4InitEv
.type _ZN5glerg4InitEv,@function
_ZN5glerg4InitEv:
leas -14,sp
clr 9,sp
clr 8,sp
sts 12,sp
.L6:
ldd #1
bsr _Znwt
std 10,sp
*** movw 8,sp,2,-sp
ldd 12,sp
bsr _ZN3barC1Ei
ldx 14,sp
movw 12,sp,2,x+
stx 14,sp
??? ldx 8,sp
inx
stx 8,sp
cpx #3
ble .L6
ldd #4
bsr _Znwt
tfr d,x
clr 3,x
clr 2,x
clr 1,x
clr 0,x
std _ZN5glerg4pFooE
leas 14,sp
rts
.Lfe1:
.size _ZN5glerg4InitEv,.Lfe1-_ZN5glerg4InitEv
.ident "GCC: (GNU) 3.1 m68hc1x-20020517"
The generated code is fairly similar, but there is no pulx above the ??? line
this time and the stack therefore becomes misaligned.
More information about the Gcc-bugs
mailing list