public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/58066] New: GCC mis-compiles access to TLS variable with -fPIC on x86_64
@ 2013-08-02 23:12 ppluzhnikov at google dot com
  2013-08-02 23:57 ` [Bug target/58066] " pinskia at gcc dot gnu.org
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: ppluzhnikov at google dot com @ 2013-08-02 23:12 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 58066
           Summary: GCC mis-compiles access to TLS variable with -fPIC on
                    x86_64
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppluzhnikov at google dot com

Google ref: b/10151411

Reproduced with current trunk, but is broken since at least gcc-4.3.1.

On Linux/x86_64, libstdc++.so.6 __cxa_get_globals looks like so:

Dump of assembler code for function __cxa_get_globals:
   0x00000000000cb430 <+0>:     lea    0x233131(%rip),%rdi
   0x00000000000cb437 <+7>:     callq  0x4f570 <__tls_get_addr@plt>
   0x00000000000cb43c <+12>:    add    $0x0,%rax
   0x00000000000cb442 <+18>:    retq   

This calls external function __tls_get_addr with mis-aligned stack.
__tls_get_addr may itself call malloc, and malloc is user-replaceable,
and may assume that stack is properly aligned (and crash when it isn't).

Trivial test case:


static __thread char ccc;
extern "C" void* __cxa_get_globals() throw()
{
 return &ccc;
}

  g++ -fPIC -S -O2 t.cc

results in:

__cxa_get_globals:
       leaq    _ZL3ccc@tlsld(%rip), %rdi
       call    __tls_get_addr@PLT
       addq    $_ZL3ccc@dtpoff, %rax
       ret



Ian Lance Taylor says:

  There is code in the i386 backend that is designed to avoid this.
  However, it appears to have only been fully implemented for the GNU2 TLS
  descriptor style ...

  I suspect that the right fix is to add the line

     ix86_tls_descriptor_calls_expanded_in_cfun = true;

  to tls_global_dynamic_64_<mode> and tls_local_dynamic_base_64_<mode>
  in gcc/config/i386/i386.md.


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

end of thread, other threads:[~2015-07-30  9:00 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-02 23:12 [Bug c/58066] New: GCC mis-compiles access to TLS variable with -fPIC on x86_64 ppluzhnikov at google dot com
2013-08-02 23:57 ` [Bug target/58066] " pinskia at gcc dot gnu.org
2013-08-06 23:14 ` ppluzhnikov at google dot com
2014-03-12 21:05 ` hjl.tools at gmail dot com
2014-03-12 22:22 ` hjl.tools at gmail dot com
2014-05-19  5:26 ` wmi at gcc dot gnu.org
2014-12-18 17:54 ` dvyukov at google dot com
2015-07-11 21:03 ` [Bug target/58066] __tls_get_addr is called with misaligned stack on x86-64 hjl.tools at gmail dot com
2015-07-13  3:58 ` hjl.tools at gmail dot com
2015-07-13  6:41 ` ubizjak at gmail dot com
2015-07-13  9:16 ` [Bug rtl-optimization/58066] " ubizjak at gmail dot com
2015-07-13 11:08 ` ubizjak at gmail dot com
2015-07-13 12:08 ` hjl.tools at gmail dot com
2015-07-15  7:40 ` uros at gcc dot gnu.org
2015-07-15  7:41 ` [Bug target/58066] " ubizjak at gmail dot com
2015-07-15 13:42 ` uros at gcc dot gnu.org
2015-07-23 18:52 ` uros at gcc dot gnu.org
2015-07-30  8:54 ` uros at gcc dot gnu.org
2015-07-30  9: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).