This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c/2705: Struct arguments passed incorrectly on Origin 2000



>Number:         2705
>Category:       c
>Synopsis:       Struct arguments passed incorrectly on Origin 2000
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Tue May 01 02:56:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Dan Bonachea
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
IRIX64-6.5
Origin 2000, MIPS R10000 CPU
>Description:
The C code below is a minimal demonstration of a bug 
with the way structs are passed as function arguments 
on the SGI Origin 2000 platform (MIPS R10000 processor).

The code should simply print:
sizeof(S)=44 sizeof(S2)=56
done
and then exit normally. It does this correctly on other
platforms I've tested (e.g. Linux).

However, when compiled and run on 2 different SGI Origin 
2000 machines, the output resembles the following:

sizeof(S)=44 sizeof(S2)=56
Mismatch in S at byte 0. shouldbe=0x0a got=0x00
Mismatch in S at byte 1. shouldbe=0x0b got=0x00
Mismatch in S at byte 2. shouldbe=0x0c got=0x00
Mismatch in S at byte 3. shouldbe=0x0d got=0x00
Mismatch in S at byte 4. shouldbe=0x0e got=0x00
Mismatch in S at byte 5. shouldbe=0x0f got=0x00
Mismatch in S at byte 6. shouldbe=0x10 got=0x00
Mismatch in S at byte 7. shouldbe=0x11 got=0x01
Mismatch in S at byte 8. shouldbe=0x12 got=0x00
Mismatch in S at byte 9. shouldbe=0x13 got=0x00
Mismatch in S at byte 10. shouldbe=0x14 got=0x00
Mismatch in S at byte 11. shouldbe=0x15 got=0x00
Mismatch in S at byte 12. shouldbe=0x16 got=0x00
Mismatch in S at byte 13. shouldbe=0x17 got=0x00
Mismatch in S at byte 14. shouldbe=0x18 got=0x00
Mismatch in S at byte 15. shouldbe=0x19 got=0x01
Mismatch in S at byte 16. shouldbe=0x1a got=0x0a
Mismatch in S at byte 17. shouldbe=0x1b got=0x0b
Mismatch in S at byte 18. shouldbe=0x1c got=0x0c
Mismatch in S at byte 19. shouldbe=0x1d got=0x0d
Mismatch in S at byte 20. shouldbe=0x1e got=0x0e
Mismatch in S at byte 21. shouldbe=0x1f got=0x0f
Mismatch in S at byte 22. shouldbe=0x20 got=0x10
Mismatch in S at byte 23. shouldbe=0x21 got=0x11
Mismatch in S at byte 24. shouldbe=0x22 got=0x12
Mismatch in S at byte 25. shouldbe=0x23 got=0x13
Mismatch in S at byte 26. shouldbe=0x24 got=0x14
Mismatch in S at byte 27. shouldbe=0x25 got=0x15
Mismatch in S at byte 28. shouldbe=0x26 got=0x16
Mismatch in S at byte 29. shouldbe=0x27 got=0x17
Mismatch in S at byte 30. shouldbe=0x28 got=0x18
Mismatch in S at byte 31. shouldbe=0x29 got=0x19
Mismatch in S at byte 32. shouldbe=0x2a got=0x1a
Mismatch in S at byte 33. shouldbe=0x2b got=0x1b
Mismatch in S at byte 34. shouldbe=0x2c got=0x1c
Mismatch in S at byte 35. shouldbe=0x2d got=0x1d
Mismatch in S at byte 36. shouldbe=0x2e got=0x1e
Mismatch in S at byte 37. shouldbe=0x2f got=0x1f
Mismatch in S at byte 38. shouldbe=0x30 got=0x20
Mismatch in S at byte 39. shouldbe=0x31 got=0x21
Mismatch in S at byte 40. shouldbe=0x32 got=0x22
Mismatch in S at byte 41. shouldbe=0x33 got=0x23
Mismatch in S at byte 42. shouldbe=0x34 got=0x24
Mismatch in S at byte 43. shouldbe=0x35 got=0x25
done

Apparently the structure copy which takes place at 
function-call time is incorrectly offset by 16 bytes. 

The problem appears to be specific to the Origin 2000,
and seems to be somewhat sensitive to the sizes of the 
structs involved (tweaking their sizes will cause it to 
succeed or fail, but the results appear to be always 
consistent for a given set of sizes). The actual contents
of the structs appears to be irrelevant - all that matters
is their size. It also appears to happen at any gcc
optimization/debugging level and with -mips3 or -mips4
options. Similarly, -memcpy seems to have no effect.

>How-To-Repeat:
int fail = 0;

typedef
struct {
 char n[44];
} S;

typedef
struct {
 char n[56];
} S2;

void check(S s, S2 s2) {
 /* check s1 */
 char *p = (char *)&s;
 int i;
 for (i=0; i < sizeof(S); i++) {
  if (*(p+i) != 10 + i) {
    printf("Mismatch in S at byte %i. shouldbe=0x%02x got=0x%02x\n",
    i,(10 + i),(int)*(p+i));
    fail = 1;
  }
 } 
 /* check s2 */
 p = (char *)&s2;
 for (i=0; i < sizeof(S2); i++) {
  if (*(p+i) != 30 + i) {
    printf("Mismatch in S2 at byte %i. shouldbe=0x%02x got=0x%02x\n",
    i,(30 + i),(int)*(p+i));
    fail = 1;
  }  
 } 
}

int main() {
 S s;
 S2 s2;
 int i;
 char *p = (char *)&s;

 printf("sizeof(S)=%i sizeof(S2)=%i\n",sizeof(S),sizeof(S2)); 
 /* setup s1 */
 for (i=0; i < sizeof(S); i++) {
  *(p+i) = (char)(10 + i);
 } 
 /* setup s2 */
 p = (char *)&s2;
 for (i=0; i < sizeof(S2); i++) {
  *(p+i) = (char)(30 + i);
 } 

 check(s,s2);

 printf("done\n");
 return fail;
}
>Fix:

The only known workaround is to pad the structures with 
some extra bytes until the problem goes away.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/x-unknown-content-type-cfile; name="gccO2Kbug.c"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="gccO2Kbug.c"

aW50IGZhaWwgPSAwOw0KDQp0eXBlZGVmDQpzdHJ1Y3Qgew0KIGNoYXIgbls0NF07DQp9IFM7DQoN
CnR5cGVkZWYNCnN0cnVjdCB7DQogY2hhciBuWzU2XTsNCn0gUzI7DQoNCnZvaWQgY2hlY2soUyBz
LCBTMiBzMikgew0KIC8qIGNoZWNrIHMxICovDQogY2hhciAqcCA9IChjaGFyICopJnM7DQogaW50
IGk7DQogZm9yIChpPTA7IGkgPCBzaXplb2YoUyk7IGkrKykgew0KICBpZiAoKihwK2kpICE9IDEw
ICsgaSkgew0KICAgIHByaW50ZigiTWlzbWF0Y2ggaW4gUyBhdCBieXRlICVpLiBzaG91bGRiZT0w
eCUwMnggZ290PTB4JTAyeFxuIiwNCiAgICBpLCgxMCArIGkpLChpbnQpKihwK2kpKTsNCiAgICBm
YWlsID0gMTsNCiAgfQ0KIH0gDQogLyogY2hlY2sgczIgKi8NCiBwID0gKGNoYXIgKikmczI7DQog
Zm9yIChpPTA7IGkgPCBzaXplb2YoUzIpOyBpKyspIHsNCiAgaWYgKCoocCtpKSAhPSAzMCArIGkp
IHsNCiAgICBwcmludGYoIk1pc21hdGNoIGluIFMyIGF0IGJ5dGUgJWkuIHNob3VsZGJlPTB4JTAy
eCBnb3Q9MHglMDJ4XG4iLA0KICAgIGksKDMwICsgaSksKGludCkqKHAraSkpOw0KICAgIGZhaWwg
PSAxOw0KICB9ICANCiB9IA0KfQ0KDQppbnQgbWFpbigpIHsNCiBTIHM7DQogUzIgczI7DQogaW50
IGk7DQogY2hhciAqcCA9IChjaGFyICopJnM7DQoNCiBwcmludGYoInNpemVvZihTKT0laSBzaXpl
b2YoUzIpPSVpXG4iLHNpemVvZihTKSxzaXplb2YoUzIpKTsgDQogLyogc2V0dXAgczEgKi8NCiBm
b3IgKGk9MDsgaSA8IHNpemVvZihTKTsgaSsrKSB7DQogICoocCtpKSA9IChjaGFyKSgxMCArIGkp
Ow0KIH0gDQogLyogc2V0dXAgczIgKi8NCiBwID0gKGNoYXIgKikmczI7DQogZm9yIChpPTA7IGkg
PCBzaXplb2YoUzIpOyBpKyspIHsNCiAgKihwK2kpID0gKGNoYXIpKDMwICsgaSk7DQogfSANCg0K
IGNoZWNrKHMsczIpOw0KDQogcHJpbnRmKCJkb25lXG4iKTsNCiByZXR1cm4gZmFpbDsNCn0NCg==


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]