[LLVMbugs] [Bug 18979] New: Global char* assigned to other global not initialized in statically-executed code in PCH

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Feb 26 13:56:05 PST 2014


http://llvm.org/bugs/show_bug.cgi?id=18979

            Bug ID: 18979
           Summary: Global char* assigned to other global not initialized
                    in statically-executed code in PCH
           Product: clang
           Version: 3.3
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: akluck at barracuda.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 12155
  --> http://llvm.org/bugs/attachment.cgi?id=12155&action=edit
Simple header that will cause static execution that references a global
variable

A global const char pointer assigned to another const char pointer may not be
properly assigned if referenced in statically executing code, such as the
constructor of some global class object, if all these objects exist in a
precompiled header.


Consider the pasted header below. If compiled without PCH using an empty main()
function, the output of the statically-allocated ScopedPrinter object, as
expected, is:

"constructing from header ; ints: 7 7 7 ; strs: foo-bar foo-bar foo-bar"

However, if compiled with a PCH, g_str2 and g_str3, which are assigned to be
equal to g_str1, are NULL. The output of the statically-allocated object is:

"constructing from header ; ints: 7 7 7 ; strs: foo-bar (null) (null)"

In any code not executing statically, there is no issue, PCH or not. There also
doesn't seem to be a problem with the global int in the example, just the char
pointer.

We haven't run into any similar issue with precompiled headers using gcc or
MSVC in Linux or Windows, respectively.

My clang --version output is:
"Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)"

To compile *without* PCH, I ran:
clang++ -include test.h testMain.cpp -o test

To compile *with* PCH, I ran:
clang++ -x c++-header test.h -o test.h.pch
clang++ -include test.h testMain.cpp -o test

~~~~~~ test.h ~~~~~~

#ifndef _test_h_
#define _test_h_

#include <stdio.h>

const unsigned int g_int1 = 7;
const unsigned int g_int2 = g_int1;
const unsigned int g_int3 = g_int2;
const char *const g_str1 = "foo-bar";
const char *const g_str2 = g_str1;
const char *const g_str3 = g_str2;

struct ScopedPrinter
{
    ScopedPrinter(const char *input)
    {
        printf("constructing from %s ; ints: %u %u %u ; strs: %s %s %s\n",
            input, g_int1, g_int2, g_int3, g_str1, g_str2, g_str3);
    }
};

static ScopedPrinter g_headerGlob("header");

#endif

~~~~~~ testMain.cpp ~~~~~~

int main(int argc, char **argv)
{
    return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140226/f53baa5b/attachment.html>


More information about the llvm-bugs mailing list