Boost C++ Libraries: Ticket #5444: boost::pool<>::ordered_malloc() crash https://svn.boost.org/trac10/ticket/5444 <p> Greetings, </p> <p> I have a segmentation fault detected in Cygwin triggered by a call to boost::pool::ordered_malloc(). The test application can be found in the Shifted Pointer library of the Sandbox: $ svn co <a class="ext-link" href="http://svn.boost.org/svn/boost/sandbox/shifted_ptr"><span class="icon">​</span>http://svn.boost.org/svn/boost/sandbox/shifted_ptr</a> $ cd shifted_ptr/libs/smart_ptr/example $ make shifted_ptr_test3 </p> <p> Gives the following backtrace: <a class="missing ticket">#0</a> 0x0040cd65 in boost::simple_segregated_storage&lt;unsigned int&gt;::try_malloc_n </p> <blockquote> <p> (start=@0x22c4c8, n=14, partition_size=4) at /usr/include/boost/pool/simple_segregated_storage.hpp:234 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/1" title="#1: Bugs: boost.build causes ftjam to segfault (closed: Wont Fix)">#1</a> 0x0040ce71 in boost::simple_segregated_storage&lt;unsigned int&gt;::malloc_n ( </p> <blockquote> <p> this=0x426020, n=15, partition_size=4) at /usr/include/boost/pool/simple_segregated_storage.hpp:256 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2" title="#2: Bugs: list::size should be const (closed: fixed)">#2</a> 0x0040d168 in boost::pool&lt;boost::default_user_allocator_new_delete&gt;::ordered_malloc (this=0x426020, n=60) at /usr/include/boost/pool/pool.hpp:511 <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3" title="#3: Bugs: automatic conversion and overload proble (closed: fixed)">#3</a> 0x0040f4a8 in boost::detail::sh::pool::allocate (this=0x426020, s=60) </p> <blockquote> <p> at ../../../boost/detail/sh_owned_base_nt.hpp:122 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4" title="#4: Bugs: any_ptr in any library documentation? (closed: Fixed)">#4</a> 0x0040f88d in boost::detail::sh::shifted&lt;node&gt;::operator new (s=60) </p> <blockquote> <p> at ../../../boost/detail/sh_owned_base_nt.hpp:262 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5" title="#5: Bugs: shared_ptr and self-owning objects (closed: Fixed)">#5</a> 0x00402607 in list::insert (this=0x22c648) at shifted_ptr_test3.cpp:58 <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6" title="#6: Bugs: tie in utility.hpp and tuple.hpp clash. (closed: Duplicate)">#6</a> 0x00401569 in test_shifted_ptr::test_method (this=0x22c6e7) </p> <blockquote> <p> at shifted_ptr_test3.cpp:96 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7" title="#7: Bugs: g++ 2.96 requires NO_STRINGSTREAM (closed: Fixed)">#7</a> 0x00401972 in test_shifted_ptr_invoker () at shifted_ptr_test3.cpp:82 <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8" title="#8: Bugs: prop in undirected graph + out_edges (closed: Works For Me)">#8</a> 0x00411181 in boost::unit_test::ut_detail::invoker&lt;boost::unit_test::ut_detail::unused&gt;::invoke&lt;void (*)()&gt; (this=0x22c727, f=@0x64d9b4) </p> <blockquote> <p> at /usr/include/boost/test/utils/callback.hpp:56 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9" title="#9: Bugs: config_info ambiguity error (closed: Invalid)">#9</a> 0x0041107f in boost::unit_test::ut_detail::callback0_impl_t&lt;boost::unit_test::ut_detail::unused, void (*)()&gt;::invoke (this=0x64d9b0) </p> <blockquote> <p> at /usr/include/boost/test/utils/callback.hpp:89 </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/10" title="#10: Bugs: allyourbase.jam file is bad. (closed: Out of Date)">#10</a> 0x638181e6 in cygboost_unit_test_framework-mt-1_43!_ZN5boost9unit_test14unit_test_mainEPFbvEiPPc () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/11" title="#11: Bugs: why not using mt19937? (closed: Fixed)">#11</a> 0x63808224 in cygboost_unit_test_framework-mt-1_43!_ZN5boost17execution_monitor13catch_signalsERKNS_9unit_test9callback0IiEE () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/12" title="#12: Bugs: Can't specify VertexPredicate (closed: Fixed)">#12</a> 0x638082a5 in cygboost_unit_test_framework-mt-1_43!_ZN5boost17execution_monitor7executeERKNS_9unit_test9callback0IiEE () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/13" title="#13: Bugs: iterator_adapter and abstract classes (closed: Out of Date)">#13</a> 0x638182fd in cygboost_unit_test_framework-mt-1_43!_ZN5boost9unit_test19unit_test_monitor_t21execute_and_translateERKNS0_9test_caseE () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/14" title="#14: Feature Requests: Support for static data members (closed: Out of Date)">#14</a> 0x63834183 in cygboost_unit_test_framework-mt-1_43!_ZN5boost9unit_test14framework_impl5visitERKNS0_9test_caseE () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/15" title="#15: Feature Requests: thread class needs join() with timeout (closed: None)">#15</a> 0x63822285 in cygboost_unit_test_framework-mt-1_43!_ZN5boost9unit_test18traverse_test_treeERKNS0_10test_suiteERNS0_17test_tree_visitorE () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/16" title="#16: Feature Requests: way to defer thread creation needed (closed: None)">#16</a> 0x6380b02e in cygboost_unit_test_framework-mt-1_43!_ZN5boost9unit_test9framework3runEmb () from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/17" title="#17: Feature Requests: Socket wrapper class (closed: fixed)">#17</a> 0x63816b79 in cygboost_unit_test_framework-mt-1_43!_ZN5boost9unit_test14unit_test_mainEPFbvEiPPc () </p> <blockquote> <p> from /usr/bin/cygboost_unit_test_framework-mt-1_43.dll </p> </blockquote> <p> <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/18" title="#18: Bugs: lexical_cast fails in some cases (closed: Fixed)">#18</a> 0x004019a6 in main (argc=1, argv=0x64a608) </p> <blockquote> <p> at /usr/include/boost/test/unit_test.hpp:59 </p> </blockquote> <p> Thanks, -Phil </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5444 Trac 1.4.3 John Maddock Thu, 14 Apr 2011 17:41:33 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5444#comment:1 https://svn.boost.org/trac10/ticket/5444#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> I can reproduce on MSVC, but this seems to be a bug in shifted_ptr: </p> <p> The order of operations on pool is: </p> <p> create pool of block size 1. </p> <p> Allocate 60 blocks </p> <p> Allocate 60 blocks </p> <p> Deallocate 60 blocks </p> <blockquote class="citation"> <blockquote class="citation"> <blockquote class="citation"> <blockquote class="citation"> <p> Corruption of pools internal free list occures here &gt;&gt;&gt;&gt;&gt; </p> </blockquote> </blockquote> </blockquote> </blockquote> <p> Deallocate 60 blocks. </p> <p> I set a memory breakpoint on the corrupted pool, and shifted pointer is changing the value 4 bytes into the already freed memory, with call stack: </p> <blockquote> <p> scrap.exe!boost::detail::sp_counted_base::release() Line 100 + 0xd bytes C++ scrap.exe!boost::detail::sh::shifted_ptr_common&lt;node&gt;::~shifted_ptr_common&lt;node&gt;() Line 67 C++ scrap.exe!boost::detail::sh::shifted_ptr_base&lt;node&gt;::~shifted_ptr_base&lt;node&gt;() + 0x2b bytes C++ scrap.exe!boost::detail::sh::shifted_ptr&lt;node&gt;::~shifted_ptr&lt;node&gt;() Line 362 + 0xf bytes C++ scrap.exe!node::~node() Line 39 + 0x1a bytes C++ scrap.exe!boost::detail::sh::shifted&lt;node&gt;::~shifted&lt;node&gt;() Line 224 + 0xf bytes C++ </p> <blockquote class="citation"> <blockquote> <p> scrap.exe!boost::detail::sh::shifted&lt;node&gt;::`scalar deleting destructor'() + 0x2b bytes C++ </p> </blockquote> </blockquote> <p> scrap.exe!boost::detail::sh::set::release() Line 99 + 0x39 bytes C++ scrap.exe!boost::detail::sh::shifted_ptr&lt;node&gt;::release(bool d) Line 375 + 0xb bytes C++ scrap.exe!boost::detail::sh::shifted_ptr&lt;node&gt;::reset() Line 357 C++ scrap.exe!list::clear() Line 53 C++ scrap.exe!test_shifted_ptr::test_method() Line 99 C++ scrap.exe!test_shifted_ptr_invoker() Line 82 + 0x26 bytes C++ scrap.exe!boost::unit_test::ut_detail::invoker&lt;boost::unit_test::ut_detail::unused&gt;::invoke&lt;void (<span class="underline">cdecl*)(void)&gt;(void (void)* &amp; f) Line 56 + 0x2c bytes C++ scrap.exe!boost::unit_test::ut_detail::callback0_impl_t&lt;boost::unit_test::ut_detail::unused,void (__cdecl*)(void)&gt;::invoke() Line 89 + 0x41 bytes C++ </span></p> </blockquote> <p> I also noted that: </p> <p> 1) If you're calling ordered_malloc then you should use ordered_free to free the memory. 2) Using pool(1) to allocate randomly sized blocks is likely to be very inefficient (worse than malloc/free), it would be much better if each shifted&lt;T&gt; had it's own pool for sizeof(T) chunks, and could therefore allocate 1 chunk at a time using the unordered pool interfaces (malloc and free) as this would be <strong>much</strong> more efficient. 3) owned_base has a static pool instance - this is not thread safe - which is to say pool is not safe for calling from mutiple threads. You could use singleton_pool&lt;&gt;::malloc/free instead of storing a static pool instance, and this would then be thread safe. </p> <p> HTH, John. </p> <p> PS closing down, please reopen if you really think it's a pool bug, but a test case involving just pool would be nice ;-) </p> Ticket