Bug 25496 - [m68k] Compiled Code Segfaults
Summary: [m68k] Compiled Code Segfaults
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.0.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2005-12-19 21:11 UTC by Stephen R. Marenka
Modified: 2006-05-08 16:21 UTC (History)
1 user (show)

See Also:
Host: m68k-linux-gnu
Target: m68k-linux-gnu
Build: m68k-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephen R. Marenka 2005-12-19 21:11:21 UTC
gcc -O2 -falign-loops=4 -fomit-frame-pointer -funroll-loops -o buggy buggy.c

$ ./buggy
DBname = >>English.xmg<<
Segmentation fault

If you drop any single one of the parameters, it works. Dropping -O2 to
-O1 also works.

I wasn't able to find a simpler file that illustrated the problem, but
the attached are pretty short.

This code works fine on the other debian ports.


$ gcc -v
Using built-in specs.
Target: m68k-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,java,f95,objc,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr --disable-werror --enable-checking=release m68k-linux-gnu
Thread model: posix
gcc version 4.0.3 20051201 (prerelease) (Debian 4.0.2-5)


onestring:
1,"debug:\n"

buggy.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define X_MAXNUM 1832
#define X_HEADER        "CSOUND_STRINGS\n"

#define DEBUG (1)

long benlong(long lval)       /* coerce a natural long into a bigendian long */
{
    char  benchar[4];
    char *p = benchar;

    *p++ = (char)(0xFF & (lval >> 24));
    *p++ = (char)(0xFF & (lval >> 16));
    *p++ = (char)(0xFF & (lval >> 8));
    *p   = (char)(0xFF & lval);
    return(*(long *)benchar);
}

/* String file will have a header string (X_HEADER) and then 10 characters
   making a language for identification
*/

int main(int argc, char **argv)
{
    char buff[256];
    long strings[X_MAXNUM];
    long loc, baseloc;
    int j;
    int n;
    long item = 0;
    FILE *db;
    FILE *raw;
    char dbname[16];
    char lang[30] = {'E', 'n', 'g', 'l', 'i', 's', 'h', '\0'};
    int order = ('t'<<24)|('x'<<16)|('t'<<8);

    if (argc>=2) raw = fopen(argv[1], "rb");
    else raw = fopen("onestring", "rb");
    if (raw == NULL) {
      fprintf(stderr, "Failed to open input file\n");
      exit(1);
    }
    if (argc==3) {
      /* 7 is length of `English' and there are 10 maximum */
      int len = strlen(argv[2]);
      if (len>29) len = 29;
      strncpy(lang, argv[2], len);
      memset(lang+len, '\0', 30-len); /* Null rest */
    }
    strcpy(dbname, lang); strcat(dbname, ".xmg"); /* ****** */
    if (DEBUG) fprintf(stderr, "DBname = >>%s<<\n", dbname);
    db = fopen(dbname, "wb");
    if (db == NULL) {
      fprintf(stderr, "Failed to create DB file\n");
      exit(1);
    }
    fwrite(&order, sizeof(int), 1, db);
    fwrite(X_HEADER, sizeof(X_HEADER)-1, 1, db);
    fwrite(lang, sizeof(char), 30, db);
    n = X_MAXNUM;
    n = benlong(n);
    fwrite(&n, sizeof(long), 1, db);
    baseloc = ftell(db);
    for (j=0; j<X_MAXNUM; j++) strings[j] = 0L;
    fwrite(strings, sizeof(long), X_MAXNUM, db); /* Write header */
    loc = ftell(db);
    if (DEBUG) fprintf(stderr, "Baseloc=%lx Loc=%lx\n", baseloc, loc);
    for (;;) {                  /* Read the text until ended */
      long n = 0;
      long i;
      int ch = getc(raw);
      while (ch=='\r' || ch =='\n') ch = getc(raw);
      if (DEBUG) fprintf(stderr, "Read '%c'(%.2x)\n", ch, ch);
      while (isdigit(ch)) {
        if (DEBUG) fprintf(stderr, "Read '%c'(%.2x)\n", ch, ch);
        n = n*10+ch-'0';
        ch = getc(raw);
      }
      if (DEBUG) fprintf(stderr, "String# %ld\n", n);
      if (ch==EOF) break;
      if (ch!=',') {
        fprintf(stderr,
                "item %ld/%ld: Syntax error -- expecting comma got '%c'%2x\n",
                item, n, ch, ch);
        exit(1);
      }
      item = n;
      i = 0;
      while ((ch=getc(raw))!='"') ;
                                /* Now read the string */
      while ((ch = getc(raw))!='"') {
        if (ch=='\\') {
          ch=getc(raw);
          switch (ch) {
          case 'a':
            ch = '\a'; break;
          case 'b':
            ch = '\b'; break;
          case 'n':
            ch = '\n'; break;
          case 'r':
            ch = '\r'; break;
          case 't':
            ch = '\t'; break;
          default:
            break;
          }
        }
        buff[i++]=ch;
      }
      buff[i++] = '\0';
      strings[n] = loc;
      n = benlong(i);
      fwrite(&n, sizeof(long), 1, db);
      fwrite(buff, sizeof(char), i, db);
      loc = ftell(db);
      while ((ch=getc(raw))!='\n');
    }
    fseek(db, baseloc, SEEK_SET);
    for (j=0; j<X_MAXNUM; j++) strings[j] = benlong(strings[j]);
    fwrite(strings, sizeof(long), X_MAXNUM, db); /* rewrite header */
    fclose(raw);
    fclose(db);
    fprintf(stderr, "OK\n");
    return(0);
}
Comment 1 Andrew Pinski 2005-12-19 21:14:59 UTC
*(long *)benchar

You are violating C aliasing rules as you are acessing a char array as a long (yes this is an aliasing violation, the opposite is not an aliasing violation that is acessing a long as a char is not an aliasing violation).

Can you see if -fno-strict-aliasing "fixes" your problem?
Comment 2 Stephen R. Marenka 2005-12-19 21:21:54 UTC
It does not. I'll report the aliasing violation back to the csound maintainer. I wonder why it's only a problem on m68k.
Comment 3 Kazu Hirata 2006-05-08 15:26:12 UTC
Can we close this PR now?
Comment 4 Stephen R. Marenka 2006-05-08 15:39:17 UTC
Subject: Re:  [m68k] Compiled Code Segfaults

On Mon, May 08, 2006 at 03:26:13PM -0000, kazu at gcc dot gnu dot org wrote:

> Can we close this PR now?

Can't say I like the segfault much, but it doesn't seem to show up
anywhere else. So yes, it's fine with me.

Thanks,

Stephen

Comment 5 Kazu Hirata 2006-05-08 16:21:59 UTC
Closed as the submitter agrees.