Boost C++ Libraries: Ticket #12340: Combining boost::context and SEH doesn't work https://svn.boost.org/trac10/ticket/12340 <p> This piece of code should print "hi!", but it does not. Compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64 </p> <p> How to compile it: cl test.cpp /MD /EHsc /I F:\repos\boost\include\boost-1_61 F:\repos\boost\lib\libboost_context-vc140-mt-1_61.lib </p> <p> Boost was built using: .\b2 address-model=64 asmflags=\safeseh --prefix=F:\repos\boost install </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12340 Trac 1.4.3 anonymous Thu, 21 Jul 2016 17:33:08 GMT attachment set https://svn.boost.org/trac10/ticket/12340 https://svn.boost.org/trac10/ticket/12340 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.cpp</span> </li> </ul> <p> test case. </p> Ticket anonymous Thu, 21 Jul 2016 22:35:17 GMT summary changed https://svn.boost.org/trac10/ticket/12340#comment:1 https://svn.boost.org/trac10/ticket/12340#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">Combining boot::context, SEH and C++ exceptions doesn't work</span> → <span class="trac-field-new">Combining boost::context and SEH doesn't work</span> </li> </ul> Ticket anonymous Thu, 21 Jul 2016 22:36:43 GMT attachment set https://svn.boost.org/trac10/ticket/12340 https://svn.boost.org/trac10/ticket/12340 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_correct.cpp</span> </li> </ul> <p> ignore previous test please. </p> Ticket olli Sun, 31 Jul 2016 17:47:39 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/12340#comment:2 https://svn.boost.org/trac10/ticket/12340#comment:2 <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">worksforme</span> </li> </ul> <p> SEH works for me on 32bit/64bit Windows - see added unit-test (branch develop) </p> Ticket anonymous Sun, 31 Jul 2016 18:00:43 GMT <link>https://svn.boost.org/trac10/ticket/12340#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:3</guid> <description> <p> Does test_correct.cpp work for you? I know SEH works when there are not threads involved. But as soon as I add threads things start failing. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>olli</dc:creator> <pubDate>Sun, 31 Jul 2016 20:58:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12340#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/12340#comment:3" title="Comment 3">anonymous</a>: </p> <blockquote class="citation"> <p> Does test_correct.cpp work for you? I know SEH works when there are not threads involved. But as soon as I add threads things start failing. </p> </blockquote> <p> I've changed the unit-test - now execution_context is executed in a std::thread. Works for me. I did not use your example - but it is equivalent. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 01 Aug 2016 18:55:43 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12340#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:5</guid> <description> <p> The difference is that my test produces a stack overflow that is not caught. If I replace rec() with a null ptr dereference or a división by zero everything works alright. </p> <p> I am running Windows 10 with the lastest updates, just in case it matters. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>olli</dc:creator> <pubDate>Tue, 02 Aug 2016 06:56:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12340#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/12340#comment:5" title="Comment 5">anonymous</a>: </p> <blockquote class="citation"> <p> The difference is that my test produces a stack overflow that is not caught. </p> </blockquote> <p> Maybe you should use protected_fixedsize stack-allocator: <a href="http://www.boost.org/doc/libs/1_61_0/libs/context/doc/html/context/stack/protected_fixedsize.html">http://www.boost.org/doc/libs/1_61_0/libs/context/doc/html/context/stack/protected_fixedsize.html</a> The stack-allocator adds an protected page (virtual memory) at the end of each stack. Please report if it works for you. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 02 Aug 2016 15:49:15 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12340#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:7</guid> <description> <p> It does not work. I am using SEH to implement a stack allocator that enlarges the stack after an access violation. I was able to reduce my code to the small test in test_correct.cpp I am thinking that since SEH only fails when the exception is caused by an illegal access to the bottom of the stack it may be a code generation bug. Especially since divisions by zero are correcty caught by the SEH handler. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>olli</dc:creator> <pubDate>Tue, 02 Aug 2016 16:26:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12340#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/12340#comment:7" title="Comment 7">anonymous</a>: </p> <blockquote class="citation"> <p> I am using SEH to implement a stack allocator that enlarges the stack after an access violation. </p> </blockquote> <p> A Micorosoft principal told me that Windows requires/forces a contiguous stack - extending the stack after an access violation might not work in principle. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 02 Aug 2016 18:00:59 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12340#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12340#comment:9</guid> <description> <p> Mmmmh, that's good to know. </p> <p> It seems to contradict what this example shows: </p> <p> <a class="ext-link" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa366803(v=vs.85).aspx"><span class="icon">​</span>https://msdn.microsoft.com/en-us/library/windows/desktop/aa366803(v=vs.85).aspx</a> </p> <blockquote class="citation"> <p> This sample does not show how to use the guard page fault to "grow" a dynamic array, such as a stack. </p> </blockquote> <p> It implies that it is possible to enlarge a stack after a guard page fault. </p> <p> <a class="ext-link" href="https://blogs.msdn.microsoft.com/oldnewthing/20060927-07/?p=29563/"><span class="icon">​</span>https://blogs.msdn.microsoft.com/oldnewthing/20060927-07/?p=29563/</a> </p> <p> Explains that normal stack growth is implemented using guard pages, so it must be legal to extend a stack. </p> <p> Also note that the test does not enlarge the stack at all. It simply should detect the page fault and leave the function. </p> <p> There is something in boost::context that prevents the SEH code from catching access violations to the bottom of the stack. </p> <p> I made discovery: if at the bottom of the stack there are <strong>at least 3 guard pages</strong> Windows will automatically extend the stack without ever letting the exception reach the exception handler, but when it runs out of guard pages it will throw a STATUS_STACK_OVERFLOW exception. If at the bottom of the stack there is a page marked PAGE_NOACCESS or if there are fewer than 3 guard pages a STATUS_ACCESS_VIOLATION exception will be thrown which won't pass through the exception filter (this is what IMHO is a bug). </p> <p> I changed the parameters of the <a class="missing wiki">VirtualProtect</a> call in protected_fixedsize_stack.hpp </p> <pre class="wiki">VirtualProtect(vp, traits_type::page_size() '''* 3''', PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, &amp; old_options); </pre> </description> <category>Ticket</category> </item> </channel> </rss>