[Boost-bugs] [Boost C++ Libraries] #12215: Boost.context: call stack corrupted on Windows using default fixedsize_stack

Subject: [Boost-bugs] [Boost C++ Libraries] #12215: Boost.context: call stack corrupted on Windows using default fixedsize_stack
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-05-18 18:47:28


#12215: Boost.context: call stack corrupted on Windows using default
fixedsize_stack
----------------------------------+---------------------
 Reporter: runningwithscythes@… | Owner: olli
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: context
  Version: Boost 1.61.0 | Severity: Problem
 Keywords: |
----------------------------------+---------------------
 There is an issue in basic_fixedsize_stack since at least Boost 1.59 on
 Windows using MSVC2013 or MSVC2015 in debug builds only, causing wired
 crashes of seemingly totally unrelated Windows API calls and the like. The
 following simple unit test fails on any Windows machine I tested so far:


 {{{
 #define BOOST_COROUTINES_UNIDRECT
 #define BOOST_COROUTINES_V2
 #include <boost/coroutine2/coroutine.hpp>
 // ...

 using coro_t = boost::coroutines2::coroutine<int>;

 BOOST_AUTO_TEST_CASE(test_windows_boost_bug)
 {
   bool result = false;

   auto coro_function = [&](coro_t::push_type& sink) {
 #if defined(PLATFORM_WINDOWS)
     char buffer[MAX_PATH];
     // The following simple Windows API call crashes when using MSVC
     // on Windows in debug build only.
     GetModuleFileName(nullptr, buffer, MAX_PATH);
     // Exception thrown at 0x00007FF939A21D58 (ntdll.dll) in
     // test.shift.task.x86_64.vc140.exe: 0xC0000005:
     // Access violation reading location 0xFFFFFFFFFFFFFFFF.

     result = true; // code not reached.
 #endif
   };

   coro_t::pull_type{coro_function};
   BOOST_CHECK(result);
 }
 }}}

 I stumbled across this bug several times but didn't try to fix it until I
 realized that it is still present in the recently released Boost 1.61.

 Once the code crashes the full stack trace looks like this:

 {{{
 ntdll.dll!LdrGetDllFullName Unknown
 KernelBase.dll!GetModuleFileNameW Unknown
 KernelBase.dll!GetModuleFileNameA Unknown
>
 test.shift.task.x86_64.vc140.exe!test_windows_boost_bug::test_method::__l2::<lambda>
 C++
 test.shift.task.x86_64.vc140.exe!boost::coroutines2::detail::pull_coroutine<int>::control_block::<lambda>
 C++
 test.shift.task.x86_64.vc140.exe!std::_Invoker_functor::_Call<boost::context::execution_context<int
 *> <lambda>(boost::context::execution_context<int *>, int
 *),boost::context::execution_context<int * __ptr64>,int * __ptr64>
 C++
 test.shift.task.x86_64.vc140.exe!std::invoke<boost::context::execution_context<int
 *> <lambda>(boost::context::execution_context<int *>, int
 *),boost::context::execution_context<int * __ptr64>,int * __ptr64> C++
 test.shift.task.x86_64.vc140.exe!boost::context::detail::apply_impl<boost::context::execution_context<int
 *> <lambda>(boost::context::execution_context<int *>, int
 *),std::tuple<boost::context::execution_context<int * __ptr64> &&
 __ptr64,int * __ptr64>,0,1> C++
 test.shift.task.x86_64.vc140.exe!boost::context::detail::apply<boost::context::execution_context<int
 *> <lambda>(boost::context::execution_context<int *>, int
 *),std::tuple<boost::context::execution_context<int * __ptr64> &&
 __ptr64,int * __ptr64> > C++
 test.shift.task.x86_64.vc140.exe!boost::context::detail::record<boost::context::execution_context<int
 *
 __ptr64>,boost::context::basic_fixedsize_stack<boost::context::stack_traits>,boost::context::execution_context<int
 *> <lambda>(boost::context::execution_context<int *>, int *) >::run C++
 test.shift.task.x86_64.vc140.exe!boost::context::detail::context_entry<boost::context::detail::record<boost::context::execution_context<int
 *
 __ptr64>,boost::context::basic_fixedsize_stack<boost::context::stack_traits>,boost::context::execution_context<int
 *> <lambda>(boost::context::execution_context<int *>, int *) > > C++
 test.shift.task.x86_64.vc140.exe!make_fcontext Unknown
 0000015ef8773e60 Unknown
 cdcdcdcdcdcdcdcd Unknown
 cdcdcdcdcdcdcdcd Unknown
 cdcdcdcdcdcdcdcd Unknown
 00000018dad1d500 Unknown
 0000015ef8773e80 Unknown
 cdcdcdcdcdcdcdcd Unknown
 cdcdcdcdcdcdcdcd Unknown
 0000000000010000 Unknown
 0000000000010000 Unknown
 0000015ef8773f20 Unknown
 0000015ef8773ec0 Unknown
 00000018dad1d984 Unknown
 cdcdcdcdcdcdcdcd Unknown
 cdcdcdcdcdcdcdcd Unknown
 cdcdcdcdcdcdcdcd Unknown
 }}}

 It took me a while to figure out what went wrong with the call stack as I
 initially thought about a bug in the context switching code. However, the
 solution turned out to be rather simple: The stack memory allocated using
 the basic_fixedsize_stack class simply isn't initialized. A simple call to
 memset fully resolves the issue for me.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12215>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC