{"id":8838,"date":"2016-08-25T16:54:36","date_gmt":"2016-08-25T15:54:36","guid":{"rendered":"https:\/\/sysprogs.com\/w\/?p=8838"},"modified":"2016-08-25T16:54:36","modified_gmt":"2016-08-25T15:54:36","slug":"introducing-dynamic-stack-verifier","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/w\/introducing-dynamic-stack-verifier\/","title":{"rendered":"Introducing Dynamic Stack Verifier"},"content":{"rendered":"<p>One of the features most wanted from VisualGDB before version 5.2 was\u00a0to\u00a0detect stack overflows on embedded devices. On a typical ARM Cortex\u00a0microcontroller\u00a0where every byte counts,\u00a0running out of stack space is not something\u00a0out of the ordinary, and it can have catastrophic consequences and be hard to debug. So VisualGDB 5.2 Preview 2 comes with a feature that\u00a0makes the stack usage analysis completely automatic\u00a0and\u00a0tracks the overflow event when it happens:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8839\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break.png\" alt=\"break\" width=\"545\" height=\"311\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break.png 545w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break-300x171.png 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/a><!--more--><\/p>\n<h2>Enabling Stack Verifier<\/h2>\n<p>All you need to do to\u00a0get your stack usage automatically checked is enable this feature on the Dynamic Analysis page of VisualGDB Project Properties (Custom edition and higher):<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/enableverifier.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8840\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/enableverifier.png\" alt=\"enableverifier\" width=\"885\" height=\"708\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/enableverifier.png 885w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/enableverifier-300x240.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/enableverifier-768x614.png 768w\" sizes=\"(max-width: 885px) 100vw, 885px\" \/><\/a>VisualGDB will suggest doing 2 things: referencing the profiler framework and adding a GCC flag\u00a0that dumps the stack usage by each function into\u00a0text files. You\u00a0will also need to specify the stack\u00a0size that you want to enforce (unless you are already using the\u00a0fixed stack\/heap feature\u00a0that comes with the new BSPs) and you are good to go.<\/p>\n<p>Now if any of\u00a0the functions in your program ends up using stack space below the stack limit,\u00a0VisualGDB will\u00a0stop your program and show you\u00a0detailed information about the offending function:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8839\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break.png\" alt=\"break\" width=\"545\" height=\"311\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break.png 545w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/break-300x171.png 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/a><\/p>\n<h2>How This Works<\/h2>\n<p>The Stack\u00a0Verifier\u00a0uses the same mechanism as the embedded profiler: it hooks every function in your program and\u00a0redirects it to a small piece of code that compares the current stack pointer (plus the expected stack usage inside the function) to the configured stack limit. If the stack pointer gets too low &#8211; kaboom! The\u00a0code responsible for checking the stack is fairly small, so it does not\u00a0introduce a considerable slowdown to your program and it&#8217;s done after linking, so the rest of your program is exactly the same as it was before instrumentation.<\/p>\n<p><a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/stub.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8841\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/stub.png\" alt=\"stub\" width=\"915\" height=\"687\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/stub.png 915w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/stub-300x225.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2016\/08\/stub-768x577.png 768w\" sizes=\"(max-width: 915px) 100vw, 915px\" \/><\/a><\/p>\n<h2>Limitations<\/h2>\n<p>There are a few cases when the verifier won&#8217;t catch the problem immediately. First is the dynamic stack allocation. If your functions call alloca() to allocate stack space dynamically, the stack usage cannot be determined at the\u00a0compile time,\u00a0so\u00a0the stack\u00a0verifier will\u00a0not see the problem\u00a0at the function entry. If the function calls another\u00a0one while the stack was already allocated beyond the limit, it will still get\u00a0caught.<\/p>\n<p>The other limitation is the library functions.\u00a0GCC does not provide stack usage information for those, so again the\u00a0stack verifier will\u00a0trigger if the stack pointer was already below the limit, but won&#8217;t detect\u00a0it if the\u00a0function itself\u00a0used some space\u00a0in the forbidden zone.<\/p>\n<h2>RTOS support<\/h2>\n<p>Yes, yes, yes. You can use it with FreeRTOS. Other RTOSes as well, but that would require a very small modification to the code.<\/p>\n<p>The logic\u00a0behind\u00a0RTOS support is simple &#8211; each time a thread switch happens, the profiler\u00a0framework updates the\u00a0<strong>SysprogsStackVerifier::StackLimit<\/strong> variable to point to the\u00a0thread&#8217;s stack limit. VisualGDB recognized FreeRTOS automatically and hooks the thread switching function to do that\u00a0transparently, but if you want to use the stack verifier with another RTOS, you will need to do it yourself by calling <strong>SysprogsProfiler_RTOSThreadSwitched()<\/strong>.<\/p>\n<h2>Download<\/h2>\n<p>You can download VisualGDB 5.2 Preview 2 with the\u00a0new stack verifier from the <a href=\"http:\/\/visualgdb.com\/download\/\">download page<\/a>.\u00a0All\u00a0feedback on it is welcome via our <a href=\"http:\/\/sysprogs.com\/contact\/\">contact form<\/a> or support emails.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the features most wanted from VisualGDB before version 5.2 was\u00a0to\u00a0detect stack overflows on embedded devices. On a typical ARM Cortex\u00a0microcontroller\u00a0where every byte counts,\u00a0running out of stack space is not something\u00a0out of the ordinary, and it can have catastrophic consequences and be hard to debug. So VisualGDB 5.2 Preview 2 comes with a feature &hellip; <a href=\"https:\/\/sysprogs.com\/w\/introducing-dynamic-stack-verifier\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Introducing Dynamic Stack Verifier<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[216],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/8838"}],"collection":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/comments?post=8838"}],"version-history":[{"count":2,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/8838\/revisions"}],"predecessor-version":[{"id":8919,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/8838\/revisions\/8919"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/media?parent=8838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/categories?post=8838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/tags?post=8838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}