libstdc++/16: ios initialization bug

Gibbons, Chad Chad_Gibbons@bmc.com
Thu Dec 2 12:17:00 GMT 1999


The following reply was made to PR libstdc++/16; it has been noted by GNATS.

From: "Gibbons, Chad" <Chad_Gibbons@bmc.com>
To: "'Benjamin Kosnik'" <bkoz@cygnus.com>,
        "Gibbons, Chad"
	 <Chad_Gibbons@ec01-hou.bmc.com>
Cc: libstdc++-gnats@sourceware.cygnus.com
Subject: RE: libstdc++/16: ios initialization bug
Date: Thu, 2 Dec 1999 14:15:00 -0600 

 Unfortunately, I don't have a small test program that can break the ios
 initialization code. We only see the behavior in an application that has an
 extremely large number of symbols (which causes g++/gld to place the ctors
 for some objects before the creation of the iostream objects). Our original
 problem was with code that was including <iostream> and even though they
 didn't use it early on, it caused global constructors to be called for that
 module.
 
 If this line:
     static ios_base::Init __ioinit;
 is left in bits/std_iostream.h, then it is possible (on compiler/linker
 whims) for it to be called before the cout/cerr/etc. constructors are
 called. Once the constructors for those global objects are called, the file
 buffers that were created by ios_base::Init are replaced with null pointers.
 
 By moving the line above to src/stdstreams.cc, I was forcing the
 initialization of the file buffers to occur after the iostream objects are
 actually created. Since the initialization of the buffers is tied to the
 iostream's creation, you're still safe if a user tries to use ios early on
 (they can't use them any earlier than they've been constructed...).
 
 Thanks,
 Chad Gibbons
 
 -----Original Message-----
 From: Benjamin Kosnik [ mailto:bkoz@cygnus.com ]
 Sent: Thursday, December 02, 1999 12:59 PM
 To: Gibbons, Chad
 Cc: libstdc++-gnats@sourceware.cygnus.com
 Subject: RE: libstdc++/16: ios initialization bug
 
 
 
 hmmm. If this static init object is in the iostream header, then the
 initialization of the iostream std objects will take place before people
 can use things like cout/cin/cerr (as they'd need the declarations in the
 iostream header). 
 
 If this is put into a source file, no such guarantees are in place. I
 don't believe you sent me a small, reproducable test case for this (but
 maybe I'm wrong) -- can you either correct me (I admit to being
 particularly flaky about this bug report, sorry) or construct one and send
 it to me?
 
 -benjamin
  
 
 On Thu, 2 Dec 1999, Gibbons, Chad wrote:
 
 > Benjamin,
 > 
 > Here's the patch I'm using. It seems to work fine on our code, but I
 wasn't
 > able to run the testsuite at all (even before the patch!) to try it out...
 > 
 > Index: bits/std_iostream.h
 > ===================================================================
 > RCS file: /cvs/libstdc++/libstdc++/bits/std_iostream.h,v
 > retrieving revision 1.11
 > diff -c -3 -p -d -r1.11 std_iostream.h
 > *** std_iostream.h      1999/06/25 03:55:41     1.11
 > --- std_iostream.h      1999/12/02 18:11:01
 > *************** namespace std {
 > *** 50,58 ****
 >     extern wostream wcerr;
 >     extern wostream wclog;
 >   #endif
 > - 
 > -   // For construction of filebuffers for cout, cin, cerr, clog et. al.
 > -   static ios_base::Init __ioinit;
 >   } // namespace std
 >   
 >   #endif        /* _CPP_IOSTREAM */
 > --- 50,55 ----
 > Index: src/stdstreams.cc
 > ===================================================================
 > RCS file: /cvs/libstdc++/libstdc++/src/stdstreams.cc,v
 > retrieving revision 1.11
 > diff -c -3 -p -d -r1.11 stdstreams.cc
 > *** stdstreams.cc       1999/06/25 03:56:04     1.11
 > --- stdstreams.cc       1999/12/02 18:11:02
 > *************** namespace std {
 > *** 45,48 ****
 > --- 45,51 ----
 >     wostream wclog(&__wfileinit);
 >   #endif
 >    
 > +    // For construction of filebuffers for cout, cin, cerr, clog et. al.
 > +    static ios_base::Init __ioinit;
 > + 
 >   } // std
 > 
 > 
 > 
 > -----Original Message-----
 > From: Benjamin Kosnik [ mailto:bkoz@cygnus.com ]
 > Sent: Wednesday, December 01, 1999 11:53 PM
 > To: Gibbons, Chad
 > Subject: RE: libstdc++/16: ios initialization bug
 > 
 > 
 > 
 > aggh. Sorry. I just got back. Can you resend your patch please? I got a 
 > bit behind.
 > 
 > -benjamin
 > 
 > On Wed, 1 Dec 1999, Gibbons, Chad wrote:
 > 
 > > Hiya Benjamin,
 > > 
 > > I sent you a message back last week or so with a pseudo-patch for this
 > > problem. Would it be best for me to submit a patch in the way described
 at
 > > http://sourceware.cygnus.com/libstdc++/17_intro/contribute.html ?
 > > 
 > > By the way, the latest CVS sources still exhibit the behavior.
 > > 
 > > Thanks again,
 > > Chad Gibbons
 > > 
 > > -----Original Message-----
 > > From: Benjamin Kosnik [ mailto:bkoz@cygnus.com ]
 > > Sent: Monday, November 22, 1999 1:06 PM
 > > To: Chad_Gibbons@bmc.com
 > > Cc: libstdc++-gnats@sourceware.cygnus.com
 > > Subject: Re: libstdc++/16: ios initialization bug
 > > 
 > > 
 > > 
 > > Chad. 
 > > 
 > > Thanks for your bug report. Unfortunately, the snapshot you are using is
 
 > > a bit dated now, and some of the changes that have happened in the three
 
 > > months since we last released affected the ios_base::init member. I 
 > > strongly urge you to check out the CVS sources and give it a whirl, as
 it 
 > > also includes such things as floating point output, and lots of 
 > > stringstream fixes. 
 > > 
 > > 
 > > > The constructor for this object creates the file buffers for the
 > standard
 > > I/O stream objects and sets their references via rdbuf. If, however, the
 > > constructors for those standard I/O stream objects occurs after the
 first
 > > > call to ios_base::Init::Init(), then the pointers to these buffers are
 > > overridden and all stream operations via those objects are disabled.
 > > > 
 > > > In practice, I have only seen this in extremely large projects (on the
 > > order of 50,000 symbols). For whatever Reason, the compiler/linker
 selects
 > > several modules that are initialized before the libstdc++ modules. These
 > > modules
 > > > have no static data, no references to static objects, and are
 themselves
 > > not referenced statically -- they just happen to include <iostream>.
 > > > 
 > > > In any event, it seems the library is relying on the compiler to
 > construct
 > > cout/cin/etc. before ios_base::Init is constructed, and that is
 something
 > > the language doesn't guarantee (I believe).
 > > 
 > > 27.3 iostream objects goes over this requirement. I belive CVS sources 
 > > now get this behavior correct, but know that the release you are using 
 > > has problems. 
 > > 
 > > -benjamin
 > > 
 > 


More information about the Libstdc++-prs mailing list