Bug 938 - C++ global constructors are running out of order
Summary: C++ global constructors are running out of order
Status: RESOLVED FIXED
Alias: None
Product: OE-Core
Classification: Build System, Metadata & Runtime
Component: devtools / tool chain (show other bugs)
Version: 1.0
Hardware: Other arm
: High critical
Target Milestone: 1.0 PointRelease
Assignee: Nitin Kamble
QA Contact:
URL:
Whiteboard:
Depends on:
Blocks: 827
  Show dependency tree
 
Reported: 2011-03-29 09:41 UTC by Mark Hatle
Modified: 2011-05-13 10:53 UTC (History)
4 users (show)

See Also:
OS type for building Yocto: ---
Type of Regression: ---
Verified:
Documentation change: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Hatle 2011-03-29 09:41:52 UTC
See bug 827 for details on Zypper where this problem was found.  (Specifically in libzypp)

Within libzypp there is a code in zypp/Arch.cc that looks like:


namespace zypp
{ /////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////
  namespace
  { /////////////////////////////////////////////////////////////////
#define DEF_BUILTIN(A) const IdString  _##A( #A );
    DEF_BUILTIN( all );
    DEF_BUILTIN( any );
    DEF_BUILTIN( noarch );
...
#undef DEF_BUILTIN
...
    struct ArchCompatSet : private base::NonCopyable
    {
...
    private:
      /** Singleton ctor. */
      ArchCompatSet()
      {
        _compatSet.insert( Arch::CompatEntry( _all, 0 ) );
        _compatSet.insert( Arch::CompatEntry( _any, 0 ) );
        _compatSet.insert( Arch::CompatEntry( _noarch, 0 ) );
...

---

Note, for debugging I inserted a debug message in the IdString class constructor, and the routines that ArchCompatSet() is calling to setup the table of compatible entries.

When this code is executed on x86 the order of execution is:


IdString: all(28)
IdString: any(27)
IdString: noarch(26)
IdString: i586(164)
IdString: qemux86(165)
addCompat 0: 'i586'(164) 'all'(28)
addCompat 1: 'i586'(164) 'any'(27)
addCompat 2: 'i586'(164) 'noarch'(26)
addCompat 3: 'i586'(164) ''(1)
addCompat 0: 'qemux86'(165) 'all'(28)
addCompat 1: 'qemux86'(165) 'any'(27)
addCompat 2: 'qemux86'(165) 'noarch'(26)
addCompat 3: 'qemux86'(165) 'i586'(164)
addCompat 4: 'qemux86'(165) ''(1)

However on ARM, the code running is:

...
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(1)
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(0)
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(1)
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(0)
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(0)
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(0)
addCompat 0: ''(0) ''(0)
addCompat 1: ''(0) ''(0)
...
IdString: all(28)
IdString: any(27)
IdString: noarch(26)
IdString: arm(187)
IdString: armv4(188)
IdString: armv4t(189)
IdString: armv5te(190)
IdString: qemuarm(191)


As indicated above, the values of the IdString's are being used BEFORE they have been constructed!
Comment 1 Qing He 2011-04-01 19:12:02 UTC
The reason that ArchCompatSet is constructed before IdString is that it's invoked by another const constructor in some other file.

The exact spot is at zypp/Repository.cc:32, following is the run path
that actually initializes it:

    const Repository Repository::noRepository;
->  zypp::Repository::Repository();
->  zypp::RepoInfo::RepoInfo();
->  zypp::RepoInfo::Impl::Impl();
->  zypp::repo::RepoVariablesUrlReplacer::RepoVariablesUrlReplacer();
->  zypp::repo::RepoVariablesStringReplacer::RepoVariablesStringReplacer();

The definition of the final node on the chain is at
zypp/repo/RepoVariables.cc:26
---
  RepoVariablesStringReplacer::RepoVariablesStringReplacer()
  {
    sysarch = Arch_empty;
    basearch = Arch_empty;
  }
--

Where Arch_empty is defined in zypp/Arch.cc:
--
  const Arch Arch_empty ( IdString::Empty );
--

At this point, the initialization order of zypp::Repository::noRepository
and zypp::Arch_empty is undefined (which is also referred to as the
"static initialization order fiasco"), since they belong to different
compilation units (read, .cc file).

On x86, we are fortunate that Arch_empty is init'ed before noRepository, but
on arm the order is reversed. When the constructor of
RepoVariablesStringReplacer
runs, Arch_empty is not constructed yet, and it immediately leads to
undefined behavior. On g++ 4.5.0, this will result in default
constructor of Arch, i.e. Arch::Arch() being called, and then
ArchCompatSet::instance(), which leads to the pre-mature construction of
the compatibility table. That's why zypper on arm fails.
Comment 2 Qing He 2011-04-02 01:55:40 UTC
(In reply to comment #1)
> The reason that ArchCompatSet is constructed before IdString is that it's
> invoked by another const constructor in some other file.


i.e. this bug is not related to toolchain or arm, but fully inside libzypp.

The initialization order of two global variables in two .cc file doesn't have a fixed order, the compiler and linker is not possible to determine the right order. So this "out of order" is caused by the ill-written constructors, the behavior of g++ is ok.

It's suggested that the bug change to zypper bug and have the priority lowered.