Boost C++ Libraries: Ticket #12592: Wrongly aligned bottom stack address https://svn.boost.org/trac10/ticket/12592 <p> I'm facing an incompatibility of Boost context based coroutines with the compiler generated function <code>__chkstk()</code> (on Windows using MSVC2015), where apparently the bottom stack address is not aligned correctly to page boundaries. </p> <p> My application uses Qt within a Boost coroutine (v2) and unfortunately runs out of stack space, which is a different issue and out of scope of this problem here. However, symptoms are that deep within the call stack some QT function implicitly calls the compiler generated function <code>__chkstk()</code> and crashes with a memory access violation. The function's code looks like this: </p> <pre class="wiki">void QQmlNotifier::emitNotify(QQmlNotifierEndpoint *endpoint, void **a) { ; auto generated code at the beginning of the function 00000000730D9C20 48 89 54 24 10 mov qword ptr [rsp+10h],rdx 00000000730D9C25 48 89 4C 24 08 mov qword ptr [rsp+8],rcx ; This QT function needs 0x1878 bytes on the stack, which is more than one page of memory, ; thus call __chkstk to make sure that all pages are mapped to actual memory 00000000730D9C2A B8 78 18 00 00 mov eax,1878h 00000000730D9C2F E8 39 CE B6 FF call __chkstk (072C46A6Dh) Qt5Qmld.dll!__chkstk(): __chkstk: ; simply jump to actual function below 0000000072C46A6D E9 0E 23 58 00 jmp __chkstk (0731C8D80h) ; do some stuff... 00000000731C8D80 48 83 EC 10 sub rsp,10h 00000000731C8D84 4C 89 14 24 mov qword ptr [rsp],r10 00000000731C8D88 4C 89 5C 24 08 mov qword ptr [rsp+8],r11 00000000731C8D8D 4D 33 DB xor r11,r11 00000000731C8D90 4C 8D 54 24 18 lea r10,[rsp+18h] 00000000731C8D95 4C 2B D0 sub r10,rax 00000000731C8D98 4D 0F 42 D3 cmovb r10,r11 ; At this point r11 gets the bottom address of context stack assigned 00000000731C8D9C 65 4C 8B 1C 25 10 00 00 00 mov r11,qword ptr gs:[10h] ; check if the actually allocated size of stack is big enough 00000000731C8DA5 4D 3B D3 cmp r10,r11 ; if this is false and if we don't jump to cs20, trouble begins 00000000731C8DA8 F2 73 17 bnd jae cs20 (0731C8DC2h) 00000000731C8DAB 66 41 81 E2 00 F0 and r10w,0F000h cs10: ; walk down the stack in 4096 byte steps 00000000731C8DB1 4D 8D 9B 00 F0 FF FF lea r11,[r11-1000h] ; trigger guard page (by writing a value) to allow dynamic resize of stack or mapping of additional pages 00000000731C8DB8 41 C6 03 00 mov byte ptr [r11],0 ; r10 and r11 will never be equal because the value of r11 is not aligned to 0x1000. 00000000731C8DBC 4D 3B D3 cmp r10,r11 00000000731C8DBF F2 75 EF bnd jne cs10 (0731C8DB1h) cs20: ; irrelevant end of function 00000000731C8DC2 4C 8B 14 24 mov r10,qword ptr [rsp] 00000000731C8DC6 4C 8B 5C 24 08 mov r11,qword ptr [rsp+8] 00000000731C8DCB 48 83 C4 10 add rsp,10h 00000000731C8DCF F2 C3 bnd ret </pre><p> The value of <a class="changeset" href="https://svn.boost.org/trac10/changeset/11" title="Initial content for next-gen Boost website. ">r11</a> in above code is the same as is assigned in boost_1_62_0\libs\context\src\asm\make_x86_64_ms_pe_masm.asm during context creation: </p> <pre class="wiki">; ... ; save bottom address of context stack as 'limit' 00007FF7C0A05A8C 48 89 48 10 mov qword ptr [rax+10h],rcx ; ... (see source file for full code) </pre><p> When using QT without the surrounding coroutine/context and walking into <code>__chkstk</code>, the bottom stack limit (<a class="changeset" href="https://svn.boost.org/trac10/changeset/11" title="Initial content for next-gen Boost website. ">r11</a>) is always a value aligned to 0x1000. Due to the fact that context does not align this value to 0x1000 the loop above writes random zeros into the applications stack address space until an access violation/seg fault occurs, destroying vital data on its way down and possibly interfering with other threads before crashing. </p> <p> I don't know yet if this only affects MSVC and the default basic_fixedsize_stack used on Windows, or if other stack allocators are broken as well. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12592 Trac 1.4.3 olli Mon, 07 Nov 2016 19:14:52 GMT <link>https://svn.boost.org/trac10/ticket/12592#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:1</guid> <description> <p> As a quick fix (before I get time to fix this issue) you could disable basic runtime checks: </p> <p> #pragma check_stack(off) or set "Basic Runtime Checks" to default under "C/C++"/"Code Generation" </p> </description> <category>Ticket</category> </item> <item> <author>Christian Maaser <runningwithscythes@…></author> <pubDate>Mon, 07 Nov 2016 19:34:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12592#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:2</guid> <description> <p> I don't think that this function is related to the Basic Runtime Checks setting, but is generally used in functions that use more than one page of stack memory (using large local variables). See <a class="ext-link" href="http://stackoverflow.com/questions/8400118/what-is-the-purpose-of-the-chkstk-function"><span class="icon">​</span>http://stackoverflow.com/questions/8400118/what-is-the-purpose-of-the-chkstk-function</a>. </p> <p> I'm currently using the prebuilt version of QT, so I can't simply test whether this setting actually removes the call to <code>_chkstk</code>. </p> <p> However, I do have a different workaround for this issue which works fine for me, so it isn't blocking my work. </p> </description> <category>Ticket</category> </item> <item> <author>Christian Maaser <runningwithscythes@…></author> <pubDate>Mon, 07 Nov 2016 19:48:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12592#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:3</guid> <description> <p> Regarding <code>_chkstk</code> see this (stupid) simple code: </p> <pre class="wiki">#include &lt;array&gt; #include &lt;cstring&gt; int main(int argc, char* argv[]) { std::array&lt;char, 8192&gt; bar; strcpy(bar.data(), argv[0]); return 0; } </pre><p> results in </p> <pre class="wiki">int main(int argc, char* argv[]) { 00007FF6323C2C10 B8 18 20 00 00 mov eax,2018h 00007FF6323C2C15 E8 76 EE 01 00 call __chkstk (07FF6323E1A90h) ... </pre><p> The compiler does not generate this call when using a smaller array (like 1024 bytes). Thus it should be easy to reproduce the issue using an array large enough. I can provide you a minimal test case by tomorrow if you like. </p> </description> <category>Ticket</category> </item> <item> <author>Christian Maaser <runningwithscythes@…></author> <pubDate>Tue, 08 Nov 2016 12:39:14 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/12592 https://svn.boost.org/trac10/ticket/12592 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">wrong_stack_alignment.cpp</span> </li> </ul> <p> minimal test case </p> Ticket Christian Maaser <runningwithscythes@…> Tue, 08 Nov 2016 12:52:19 GMT <link>https://svn.boost.org/trac10/ticket/12592#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:4</guid> <description> <p> I can confirm that MSVC adds <code>__chkstk</code> to any function requesting more than one page of memory regardless of "Basic Runtime Checks", "Disable Security Checks", or other compiler flags, even in fully optimized release builds. It is a Windows NT platform requirement to do these checks. See <a class="ext-link" href="https://support.microsoft.com/en-us/kb/100775"><span class="icon">​</span>https://support.microsoft.com/en-us/kb/100775</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>olli</dc:creator> <pubDate>Sun, 22 Jan 2017 18:21:23 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12592#comment:5 https://svn.boost.org/trac10/ticket/12592#comment:5 <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> I've had time to look at this bug in more detail: </p> <p> The bottom stack is correctly aligned (16-byte) - the problem is that </p> <blockquote> <p> execution_context&lt;int&gt; source([](execution_context&lt;int&gt; sink, int) </p> </blockquote> <p> uses the 'fixedsize_stack' with a default size of 64kB. That means that in best case you get an segmentation fault if you excceed the stack size (allocating 7 arrays on the stack). </p> <p> Windows does not provide segemented (on demand growing) stacks. </p> <p> If you use the 'protected_fixedsize_stack' you get an guard page appended at the stack. If you access memory from that page an 'stack overflow' exception is raised: </p> <blockquote> <p> execution_context&lt;int&gt; source( </p> <blockquote> <p> std::allocator_arg, protected_fixedsize_stack( 64*1024), </p> </blockquote> <p> [](execution_context&lt;int&gt; sink, int) </p> </blockquote> <p> In your case you used the fixedsize_stack with a stack size of 64kb, which seams too small for your use case. You should try a larger stack size. </p> <p> So, I close this bug as 'works for me'. </p> <p> BTW, to disable optimization for a function you could use '#pragma optimize("",on|off)' </p> Ticket Christian Maaser Mon, 23 Jan 2017 08:37:38 GMT <link>https://svn.boost.org/trac10/ticket/12592#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:6</guid> <description> <p> The bottom stack address is required to be aligned at a page boundary (4KB) on 64bit Windows. protected_fixedsize_stack uses </p> <pre class="wiki">void * vp = ::VirtualAlloc( 0, size__, MEM_COMMIT, PAGE_READWRITE); </pre><p> to allocate its memory, which does have this guarantee (the function allocates on a memory page level). fixedsize_stack however simply uses </p> <pre class="wiki">void * vp = std::malloc( size_); </pre><p> which has no such guarantee. </p> <p> Of course I agree with you that when running out of stack there is no way (other than dynamically resizing the stack) to recover and without a guard page the application will crash at some point due to accessing an unmapped page. However, MSVC's generated _chkstk assumes that </p> <blockquote> <p> a) there is a guard page (as setup by protected_fixedsize_stack), and </p> </blockquote> <blockquote> <p> b) the bottom stack address is properly aligned. </p> </blockquote> <p> In any case _chkstk will attempt to overwrite several bytes in attempt to trigger the dynamic stack resizing mechanism (assumed to be present), but only when the stack is page aligned _chkstk will be limited to the number of additional pages required by the user's function. When the stack is not properly aligned _chkstk will potentially overwrite many more bytes until it crashes because due to assumptions above it fails to find the end of stack and happily loops through process memory (e.g. other fixedsize_stacks from other contexts allocated right next to each other) overwriting several bytes until it reaches unmapped addresses and only then generating an access violation. </p> <p> Actually, due to the assumptions of _chkstk/Windows use of fixedsize_stack should be discouraged and shouldn't be default on Windows, but that is a different issue. </p> <p> Thanks for showing how to change the stack allocator. I missed that part and manually patched my copy of Boost to use protected_fixedsize_stack by default. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>olli</dc:creator> <pubDate>Mon, 23 Jan 2017 09:58:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12592#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:7</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/12592#comment:6" title="Comment 6">Christian Maaser</a>: </p> <blockquote class="citation"> <p> Actually, due to the assumptions of _chkstk/Windows use of fixedsize_stack should be discouraged and shouldn't be default on Windows, but that is a different issue. </p> </blockquote> <p> A solution for fixedsize_stack could be to use </p> <pre class="wiki">VirtualAlloc() </pre><p> as in protected_fixedsize_stack but without adding a guard page at the end. On Windows the minimal stack size must be of page size. So fixedsize_stack would allocate a stack of mulitple pages size. </p> </description> <category>Ticket</category> </item> <item> <author>Christian Maaser <runningwithscythes@…></author> <pubDate>Mon, 23 Jan 2017 14:56:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12592#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:8</guid> <description> <p> C++17 will probably introduce <a class="ext-link" href="http://en.cppreference.com/w/cpp/memory/c/aligned_alloc"><span class="icon">​</span>http://en.cppreference.com/w/cpp/memory/c/aligned_alloc</a>, but until then this sounds like a good solution to me. Generally I think the benefits of fixedsize_stack over protected_fixedsize_stack are virtually non-existent, because the additional guard page does not require any actual memory allocation but only an address space allocation and reliably protects against stack overflows (utilizing _chkstk). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>olli</dc:creator> <pubDate>Mon, 29 May 2017 06:02:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12592#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12592#comment:9</guid> <description> <p> The next release of boost.context supports Windows-Fibers - of course Windows-Fibers are slower than fcontext. </p> </description> <category>Ticket</category> </item> </channel> </rss>