public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/57264] New: cld not emitted when string instructions used, and '-mcld' on command line
@ 2013-05-13 13:32 thutt at vmware dot com
  2013-05-13 15:41 ` [Bug target/57264] " ubizjak at gmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: thutt at vmware dot com @ 2013-05-13 13:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57264

            Bug ID: 57264
           Summary: cld not emitted when string instructions used, and
                    '-mcld' on command line
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thutt at vmware dot com

A defect has been found in several versions of gcc with respect to the
'-mcld' option.

  -mcld

     This option instructs GCC to emit a "cld" instruction in the
     prologue of functions that use string instructions.  String
     instructions depend on the DF flag to select between
     autoincrement or autodecrement mode.  While the ABI specifies the
     DF flag to be cleared on function entry, some operating systems
     violate this specification by not clearing the DF flag in their
     exception dispatchers.  The exception handler can be invoked with
     the DF flag set which leads to wrong direction mode, when string
     instructions are used.  This option can be enabled by default on
     32-bit x86 targets by configuring GCC with the --enable-cld
     configure option.  Generation of "cld" instructions can be
     suppressed with the -mno-cld compiler option in this case.


Consider the following distillation of the issue (cld.c):

  void test(int x, int **pp)
  {
      while (x) {
          int *ip = *pp;
          int *op = *pp;
          while (*ip) {
             int v = *ip++;
             *op++ = v + 1;
          }
       }
  }

To demonstrate the issue:

  gcc -c -mcld -O1 -m64 -o cld.o cld.c
  objdump --disassemble --reloc cld.o

An example failure case:

  gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
  Copyright (C) 2011 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


  0000000000000000 <test>:
     0:  85 ff                  test   %edi,%edi
     2:  74 15                  je     19 <test+0x19>
     4:  48 8b 3e               mov    (%rsi),%rdi
     7:  8b 07                  mov    (%rdi),%eax
     9:  85 c0                  test   %eax,%eax
     b:  74 f7                  je     4 <test+0x4>
     d:  83 c0 01               add    $0x1,%eax
    10:  ab                     stos   %eax,%es:(%rdi)
    11:  8b 07                  mov    (%rdi),%eax
    13:  85 c0                  test   %eax,%eax
    15:  74 ed                  je     4 <test+0x4>
    17:  eb f4                  jmp    d <test+0xd>
    19:  f3 c3                  repz retq

For the above sample code, local experimentation has shown than 4.5,
4.6 & 4.7 emit a string instruction without a 'cld' in the prologue.
Versions 4.4 & 4.8 do not emit a string instruction.

Most perturbations to the above sample code will change the output so
that no string instruction is emitted when using the defective
versions.

It is expected that a 'cld' instruction will be generated in the
function prolong whenever any string instruction is emitted by gcc when
the '-mcld' option has been provided.


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-05-14 18:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-13 13:32 [Bug c/57264] New: cld not emitted when string instructions used, and '-mcld' on command line thutt at vmware dot com
2013-05-13 15:41 ` [Bug target/57264] " ubizjak at gmail dot com
2013-05-13 15:56 ` ubizjak at gmail dot com
2013-05-13 17:26 ` ubizjak at gmail dot com
2013-05-13 17:28 ` ubizjak at gmail dot com
2013-05-13 17:56 ` thutt at vmware dot com
2013-05-13 18:54 ` ubizjak at gmail dot com
2013-05-14 13:41 ` thutt at vmware dot com
2013-05-14 14:01 ` ubizjak at gmail dot com
2013-05-14 17:07 ` thutt at vmware dot com
2013-05-14 18:00 ` ubizjak at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).