Boost logo

Boost :

From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2006-03-24 13:36:29


Hello Anand,

This is a bug in the gcc version of atomic_increment found in boost/
detail/sp_counted_base_gcc_ppc.hpp. It looks like:

inline void atomic_increment( int * pw )
{
     // ++*pw;

     int tmp;

     __asm__
     (
         "0:\n\t"
         "lwarx %1, 0, %2\n\t"
         "addi %1, %1, 1\n\t"
         "stwcx. %1, 0, %2\n\t"
         "bne- 0b":

         "=m"( *pw ), "=&b"( tmp ):
         "r"( pw ):
         "cc"
     );
}

Should be changed to:

inline void atomic_increment( int * pw )
{
     // ++*pw;

     int tmp;

     __asm__
     (
         "0:\n\t"
         "lwarx %1, 0, %2\n\t"
         "addi %1, %1, 1\n\t"
         "stwcx. %1, 0, %2\n\t"
         "bne- 0b":

         "=m"( *pw ), "=&b"( tmp ):
         "m"( *pw ), "r"( pw ):
         "cc"
     );
}

The difference is the '"m"( *pw)' bit near the bottom.

atomic_decrement and atomic_conditional_increment in the same file
should be treated in the same way. The latest boost sources have
been updated with this fix so that when you update, you will not
loose this fix.

-Howard

On Mar 24, 2006, at 12:53 PM, Ananda Tallur wrote:

> Hello,
>
> I have further investigated on this issue, by analysing what happens
> in the unit_test_framework.
> I could come out with a simple test program, only using shared
> pointer, which fails on Mac OS X, when compiled with gcc 4.0.1
> (coming with latest XCode 2.2.1), or gcc 4.1 from darwinports, when
> optimization is enabled (-O, -O1, or -O2).
> I am using boost 1.33.1, but is it the same with 1.33.0 and 1.32.0.
>
> Below the new test program :
>
> --- macosx_gcc4problem.cpp ---
> #include <iostream>
> #include <boost/shared_ptr.hpp>
> using namespace std;
> using namespace boost;
>
> struct A {
> int value;
> ~A ()
> {
> std::cout << "SampleTest destructor" << std::endl <<
> std::flush;
> }
> };
>
> struct B
> {
> B (shared_ptr <A> val)
> : a (val) {}
>
> shared_ptr <A> a;
> };
>
> struct C {
> C (B val)
> : b (val) {}
>
> B b;
> };
>
> struct D
> {
> D (C val)
> : c (val)
> {}
> C c;
> };
>
> D create ()
> {
> boost::shared_ptr <A> a (new A);
> a->value = 12345;
> return D (C (B (a)));
> }
>
> int main (int argc, char * argv [])
> {
> D d = create ();
> std::cout << "value = " << d.c.b.a->value << std::endl <<
> std::flush;
> return 0;
> }
> --- macosx_gcc4problem.cpp ---
>
> To compile it :
> g++-4.0 -O2 -I <path-to-boost-includes> macosx_gcc4problem.cpp -o
> macosx_gcc4problem
>
> Output :
> When run, the value displayed is 13691, instead of 12345.
>
> Analysis :
> -> there is a problem in shared_ptr reference counting, in line :
> return D (C (B (a)));
> Just after D creation, the reference counter is a 1, instead of
> 2 as it should.
> And when leaving create () function, the automatic variable a is
> detroyed, which changes the reference counter to 0 and detroys the A
> object.
>
> In main function after call to create, the reference counter
> should be of 1, and the object A should be available on the heap.
> But it has already been destroyed, leading to an incorrect
> result being displayed.
> In worst cases, a segmentation fault may happen.
>
> Remark : if changing the constructor of B, so that a const reference
> to the shared_ptr is passed, instead of the shared_ptr itself, it
> works :
> ---
> struct B
> {
> B (const shared_ptr <A> &val)
> : a (val) {}
> ---
>
> To further investigate the problem, it may be needed to analyse the
> assembly code generated by the gcc 4 compiler.
> The problem is clearly in the line : return D (C (B (a)));
> There may be a problem with management of temporary variables,
> combined with optimisations.
>
> If there is experienced developpers working on Mac OS X, and having a
> litlle time to investigate, it would be very helpful, although it is
> likely the problem may come from the gcc 4 compiler on Mac OS X.
>
> Thanks
>
> Anand
>
>
>
>
> On 7 mars 06, at 10:39, Ananda Tallur wrote:
>
>> Hello Gennadiy,
>>
>> Thank you very much for you answer, and sorry for having forgotten
>> something a important as mentionning the Boost version used !
>>
>> In fact, I have tried with three Boost versions : 1.33.1, 1.33.0,
>> and 1.32.0, compiled with Apple gcc 4.0.1, and the same is exactly
>> the same with these three versions.
>>
>> I have just tried the test again, with the included version of
>> unit_test_framework :
>> - including <boost/test/included/unit_test_framework.hpp> instead
>> of <boost/test/unit_test.hpp>
>> - not linking the test program against boost_unit_test static library
>>
>> The test result is exactly the same as when using the static
>> library version :
>> ----
>> Running 1 test case...a : 13691
>> boost_unit_test_gcc4_pb_sample.cpp(18): error in "
>> SampleTest::sampleTest": check a == 12345 failed [13691 != 12345]
>>
>> *** 1 failure detected in test suite "Sample test"
>>
>> So it really seems to show that there is a problem with the
>> compiler from Apple.
>> I am currently using build Apple gcc 4.0.1 build 5250, but I have
>> also tried with previous version (build 5247), and the problem is
>> the same.
>>
>> But it works with gcc 3.3 from Apple, and with gcc 4.0.1 without
>> optimizations.
>>
>> I have already issued a bug report to Apple.
>>
>> Thank you again for your answer, do you think of some other test I
>> could do to precise the problem ?
>> And also, it would be great if another Mac OS X user could try the
>> test program I have sent in my previous e-mail, and confirm the
>> problem.
>>
>> Best regards,
>>
>> Anand
>>
>>
>> On 6 mars 06, at 18:00, boost-request_at_[hidden] wrote:
>>> Date: Mon, 6 Mar 2006 11:44:43 -0500
>>> From: "Gennadiy Rozental" <gennadiy.rozental_at_[hidden]>
>>> Subject: Re: [boost] problem with boost_unit_test :
>>> BOOST_CLASS_TEST_CASE seemsno the access instance at the right
>>> address, on Mac OS X, with gcc 4.0.1/2 with optimization level > 0
>>> To: boost_at_[hidden]
>>> Message-ID: <duhotr$dmg$1_at_[hidden]>
>>>
>>> Hi, Ananda
>>>
>>> I did not reply to your post in users list, since I do not have
>>> any idea
>>> what could be casuing this. One question: which version of boost
>>> you are
>>> using. One recommendation: try to use included version of UTF
>>> <boost/test/included/unit_test_framework.hpp> and let us know the
>>> results.
>>>
>>> Regards,
>>>
>>> Gennadiy
>>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/
> listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk