<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugzilla.yoctoproject.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugzilla.yoctoproject.org/"
          
          maintainer="it-coreprojects-helpdesk@linuxfoundation.org"
>

    <bug>
          <bug_id>938</bug_id>
          
          <creation_ts>2011-03-29 09:41:52 +0000</creation_ts>
          <short_desc>C++ global constructors are running out of order</short_desc>
          <delta_ts>2011-05-13 10:53:06 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>7</classification_id>
          <classification>Build System, Metadata &amp; Runtime</classification>
          <product>OE-Core</product>
          <component>devtools / tool chain</component>
          <version>1.0</version>
          <rep_platform>Other</rep_platform>
          <op_sys>arm</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>High</priority>
          <bug_severity>critical</bug_severity>
          <target_milestone>1.0 PointRelease</target_milestone>
          
          <blocked>827</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Mark Hatle">mark.hatle</reporter>
          <assigned_to name="Nitin Kamble">nitin.a.kamble</assigned_to>
          <cc>meta.mr.watcher</cc>
    
    <cc>meta.watcher</cc>
    
    <cc>qing.he</cc>
    
    <cc>sgw</cc>
          
          
          <cf_os>---</cf_os>
          <cf_regression_type>---</cf_regression_type>
          
          <cf_docchange>---</cf_docchange>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>13034</commentid>
    <comment_count>0</comment_count>
    <who name="Mark Hatle">mark.hatle</who>
    <bug_when>2011-03-29 09:41:52 +0000</bug_when>
    <thetext>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: &apos;i586&apos;(164) &apos;all&apos;(28)
addCompat 1: &apos;i586&apos;(164) &apos;any&apos;(27)
addCompat 2: &apos;i586&apos;(164) &apos;noarch&apos;(26)
addCompat 3: &apos;i586&apos;(164) &apos;&apos;(1)
addCompat 0: &apos;qemux86&apos;(165) &apos;all&apos;(28)
addCompat 1: &apos;qemux86&apos;(165) &apos;any&apos;(27)
addCompat 2: &apos;qemux86&apos;(165) &apos;noarch&apos;(26)
addCompat 3: &apos;qemux86&apos;(165) &apos;i586&apos;(164)
addCompat 4: &apos;qemux86&apos;(165) &apos;&apos;(1)

However on ARM, the code running is:

...
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(1)
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(0)
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(1)
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(0)
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(0)
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(0)
addCompat 0: &apos;&apos;(0) &apos;&apos;(0)
addCompat 1: &apos;&apos;(0) &apos;&apos;(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&apos;s are being used BEFORE they have been constructed!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>13097</commentid>
    <comment_count>1</comment_count>
    <who name="Qing He">qing.he</who>
    <bug_when>2011-04-01 19:12:02 +0000</bug_when>
    <thetext>The reason that ArchCompatSet is constructed before IdString is that it&apos;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;
-&gt;  zypp::Repository::Repository();
-&gt;  zypp::RepoInfo::RepoInfo();
-&gt;  zypp::RepoInfo::Impl::Impl();
-&gt;  zypp::repo::RepoVariablesUrlReplacer::RepoVariablesUrlReplacer();
-&gt;  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
&quot;static initialization order fiasco&quot;), since they belong to different
compilation units (read, .cc file).

On x86, we are fortunate that Arch_empty is init&apos;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&apos;s why zypper on arm fails.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>13108</commentid>
    <comment_count>2</comment_count>
    <who name="Qing He">qing.he</who>
    <bug_when>2011-04-02 01:55:40 +0000</bug_when>
    <thetext>(In reply to comment #1)
&gt; The reason that ArchCompatSet is constructed before IdString is that it&apos;s
&gt; 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&apos;t have a fixed order, the compiler and linker is not possible to determine the right order. So this &quot;out of order&quot; is caused by the ill-written constructors, the behavior of g++ is ok.

It&apos;s suggested that the bug change to zypper bug and have the priority lowered.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>13117</commentid>
    <comment_count>3</comment_count>
    <who name="Saul Wold">sgw</who>
    <bug_when>2011-04-04 09:44:23 +0000</bug_when>
    <thetext>
http://git.pokylinux.org/cgit/cgit.cgi/poky/commit/?id=37f3ef2cfd385f4481b250e00fa74ca9104c1da4</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>13798</commentid>
    <comment_count>4</comment_count>
    <who name="Saul Wold">sgw</who>
    <bug_when>2011-05-13 10:53:06 +0000</bug_when>
    <thetext>Bernard

http://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=23a0019b1f0973003e43ef863d87356162a555b7</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>