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

            Bug ID: 91197
           Summary: [8,9 regression] alignment test program used in perl
                    does not work with -O2 anymore
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eb at emlix dot com
  Target Milestone: ---

The story is in https://bugs.gentoo.org/676062 and especially
https://rt.perl.org/Public/Bug/Display.html?id=133495

This test program, taken from the above perl bug, incorrectly returns 0 when
built with -O2 on gcc 8.3 and 9.1, but is fine with 7.3 and probably everything
before. Building with -g only makes things work:

#include <stdio.h>
#define I_STDLIB
#ifdef I_STDLIB
#include <stdlib.h>
#endif
#define U32 unsigned long
#define BYTEORDER 0x4321
#define U8 unsigned char
#include <signal.h>
#ifdef SIGBUS
void bletch(int s) { exit(4); }
#endif
int main() {
#if BYTEORDER == 0x1234 || BYTEORDER == 0x4321
    volatile U8 buf[8];
    volatile U32 *up;
    int i;

    if (sizeof(U32) != 4) {
        printf("sizeof(U32) is not 4, but %d\n", sizeof(U32));
        exit(1);
    }

    fflush(stdout);

#ifdef SIGBUS
    signal(SIGBUS, bletch);
#endif

    buf[0] = 0;
    buf[1] = 0;
    buf[2] = 0;
    buf[3] = 1;
    buf[4] = 0;
    buf[5] = 0;
    buf[6] = 0;
    buf[7] = 1;

    for (i = 0; i < 4; i++) {
        up = (U32*)(buf + i);
        if (! ((*up == 1 << (8*i)) ||   /* big-endian */
               (*up == 1 << (8*(3-i)))  /* little-endian */
              )
           )
        {
            printf("read failed (%x)\n", *up);
            exit(2);
        }
    }

    /* write test */
    for (i = 0; i < 4; i++) {
        up = (U32*)(buf + i);
        *up = 0xBeef;
        if (*up != 0xBeef) {
            printf("write failed (%x)\n", *up);
            exit(3);
        }
    }

    exit(0);
#else
    printf("1\n");
    exit(1);
#endif
    return 0;
}

Reply via email to