From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 22361 invoked from network); 12 Jul 1999 18:15:28 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 12 Jul 1999 18:15:28 -0000 Received: from paralleldata.com (smtp.paralleldata.com [38.153.145.2]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id OAA14953 for ; Mon, 12 Jul 1999 14:09:32 -0400 Received: by paralleldata.com from localhost (router,SLMail V2.5); Mon, 12 Jul 1999 13:13:59 -0500 Received: by paralleldata.com from paralleldata.com (207.136.51.253::mail daemon; unverified,SLMail V2.5); Mon, 12 Jul 1999 13:13:56 -0500 Sender: "Wsb" Message-ID: <378A21C6.1E094CB4@paralleldata.com> Date: Mon, 12 Jul 1999 12:11:34 -0500 From: "Wes Bauske" Organization: PDS Inc. X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.5-15 i686) X-Accept-Language: en MIME-Version: 1.0 To: axp-list@redhat.com Subject: Re: an evil memcpy() optimizer bug! (egcs-1.1.2, alphaev6, linux) References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Try modifying your code to pass the address of a double rather than the double itself and see if you still can create the problem. I've seen problems passing floats/doubles to functions rather than their addresses. I just pass the address instead. Wes "Alexander L. Belikoff" wrote: > > Hello everybody, > > While porting our code to Alpha dp264 (ev6), I've found the following > bug, that bit us quite painfully. It maybe the case that somebody > reported it before, but then it is unclear why nobody bothered to > issue an emergency bugfix release (1.1.3?). This one really looks like > a major showstopper... > > I. The environment: > > Alpha dp264 (dual-CPU), running RedHat Linux 5.2 with kernel 2.2.10 > (w/ d-ansn-2). > > Bothe "native" compiler (egcs 1.0.3) and egcs 1.1.2 installed (the > latter built manually and installed in /usr/local/egcs). The latter > shows: > > $ gcc -v > Reading specs from /usr/local/egcs/lib/gcc-lib/alphaev6-unknown-linux-gnu/egcs-2.91.66/specs > gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release) > > II. Short description of the bug: > > When optimized, memcpy() function IN SOME CASES doesn't properly copy > the data. > > III. The program (it is as minimal as it could be - the main bulk of > the source is the hexdump function): > > ============================================================================ > #include > #include > #include > #include > #include > #include > > void > hexdump( > FILE* fp, /* in - output stream */ > char* name, /* in - dump name (or NULL) */ > char* buf, /* in - buffer to dump */ > const size_t len /* in - buffer length */ > ) > { > int i, j; > > assert(fp != NULL); > assert(buf != NULL); > assert(len > 0); > > #define LMAX 16 /* number of bytes per line */ > > fprintf(fp, "\n=== %s dump at %p (%d bytes) ====================\n", > (name ? name : ""), buf, (int) len); > > for (i = 0; i < len; i += LMAX) { > unsigned short* ibuf = (unsigned short*) &buf[i]; > fprintf(fp, "%04d-%04d ", i, > (int) ((i + LMAX - 1 < len) ? i + LMAX - 1 : len)); > > for (j = 0; j < LMAX / 2; j++) { > if (i + j * 2 >= len) { > fprintf(fp, "%*s", (LMAX / 2 - j) * 5, ""); > break; > } > > fprintf(fp, "%04X ", ibuf[j]); > } > > fprintf(fp, " "); > > for (j = 0; j < LMAX; j++) { > char c; > > if (i + j >= len) > break; > > c = isprint(buf[i + j]) ? buf[i + j] : '.'; > fprintf(fp, "%c", c); > } > > fprintf(fp, "\n"); > } > > fprintf(fp, "=========================================================" > "==================\n\n"); > } > > void > put_f8( > double v, /* in - value */ > char* buf /* in - destination address */ > ) > { > v = 99.875; > hexdump(stdout, "pack_f8: v", (char*) &v, 8); > memcpy(buf, &v, 8); > hexdump(stdout, "pack_f8: buf", (char*) buf, 8); > } > > int > main() > { > double foovar = 0.05; > double dbuf[3]; > > memset(dbuf, '\0', sizeof(dbuf)); > put_f8(foovar, (char*) &dbuf); > exit(1); > } > > /* > Local Variables: > compile-command: "gcc -O2 -g packbug.c -o packbug" > End: > */ > > ============================================================================= > > (the corresponding .i file is attached below) > > IV. A bug demo session. > > The program tries to write a double prec. value into a buffer, using > memcpy(). The value hexdump is printed before memcpy(), and the buffer > hexdump is printed after it - they don't match in the environment > above, IF the compiler is egcs 1.1.2, AND -O2 is used. There may be > other combinations, however, showing the same bug. > > a. a "buggy" session: > > $ gcc -v > Reading specs from /usr/local/egcs/lib/gcc-lib/alphaev6-unknown-linux-gnu/egcs-2.91.66/specs > gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release) > $ gcc -Wall -O2 -g packbug.c -o packbug > $ ./packbug > > === pack_f8: v dump at 0x11ffff6e0 (8 bytes) ==================== > 0000-0008 0000 0000 F800 4058 ......X@ > =========================================================================== > > === pack_f8: buf dump at 0x11ffff700 (8 bytes) ==================== > 0000-0008 0000 0000 0000 0000 ........ > =========================================================================== > > b. no optimization > > $ gcc -v > Reading specs from /usr/local/egcs/lib/gcc-lib/alphaev6-unknown-linux-gnu/egcs-2.91.66/specs > gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release) > $ gcc -Wall -g packbug.c -o packbug > $ ./packbug > > === pack_f8: v dump at 0x11ffff6e0 (8 bytes) ==================== > 0000-0008 0000 0000 F800 4058 ......X@ > =========================================================================== > > === pack_f8: buf dump at 0x11ffff708 (8 bytes) ==================== > 0000-0008 0000 0000 F800 4058 ......X@ > =========================================================================== > > c. egcs 1.0.3 > > $ /usr/bin/gcc -v > Reading specs from /usr/lib/gcc-lib/alpha-redhat-linux/egcs-2.90.29/specs > gcc version egcs-2.90.29 980515 (egcs-1.0.3 release) > $ /usr/bin/gcc -Wall -O2 -g packbug.c -o packbug > $ ./packbug > > === pack_f8: v dump at 0x11ffff6e0 (8 bytes) ==================== > 0000-0008 0000 0000 F800 4058 ......X@ > =========================================================================== > > === pack_f8: buf dump at 0x11ffff700 (8 bytes) ==================== > 0000-0008 0000 0000 F800 4058 ......X@ > =========================================================================== > > Regards, > > PS. Here is a cpp'ed file: > > ---------------------------------------------------------------------------------------------------- > Name: packbug.i.gz > packbug.i.gz Type: GNU gzip (application/gz) > Encoding: x-uuencode > > ---------------------------------------------------------------------------------------------------- > > -- > Alexander L. Belikoff > Bloomberg L.P. / BFM Financial Research Ltd. > abel@vallinor4.com, abel@bfr.co.il