<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/75995/determining-stack-overflow-by-writing-to-a-stack-in-a-startup-file</link><description>I&amp;#39;m trying to detect stack overflow. One approach that I found online was to prefill the memory with a constant value before the program runs, and see how much of the stack has been overwritten. 
 So now that I&amp;#39;ve been looking into it: 
 
 is ses_startup_common</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 08 Jun 2021 23:17:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/75995/determining-stack-overflow-by-writing-to-a-stack-in-a-startup-file" /><item><title>RE: Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/thread/314300?ContentTypeID=1</link><pubDate>Tue, 08 Jun 2021 23:17:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f86590d-d031-41ab-baed-89fb5777e1fb</guid><dc:creator>morpho</dc:creator><description>&lt;p&gt;Assembly and the startup file because you would want to write to the stack before the main is called, and track how much is being used by looking at the bits in memory&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/thread/314101?ContentTypeID=1</link><pubDate>Tue, 08 Jun 2021 07:58:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:37a915dc-16d5-463a-9e8c-ce15918f002d</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="morpho"]Where are&amp;nbsp;__StackLimit and&amp;nbsp;__StackTop defined? Segger isn&amp;#39;t helping me navigate and it gets annoying at times.[/quote]
&lt;p&gt;I recommend just searching through all code in a normal editor then, or grepping or what you prefer. That way you can typically find these things as there are not that many references. Specifically,&amp;nbsp;__StackTop and __StackLimit comes from &amp;lt;SDK&amp;gt;\modules\nrfx\mdk\nrf52_common.ld and you can see how it is done there.&lt;/p&gt;
[quote user="morpho"]I see. Can you confirm if `STACK_INIT_VAL` equals `0x20040000` which marks the end of the RAM i.e what MSP points to at the start of main? I found its value in the `.map` file.&amp;nbsp;[/quote]
&lt;p&gt;&amp;nbsp;Yes.&lt;/p&gt;
[quote user="morpho"]So if the RAM is between `0x2000 0000 - 0x2004 0000`, how much of that is taken up by the stack?[/quote]
&lt;p&gt;That is entirely up to you. Most example projects use 8192 byte stacks, but you can change that. For SES, you can do it from here:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/change_5F00_stack.PNG" /&gt;&lt;/p&gt;
[quote user="morpho"]Sorry, I&amp;#39;m not great at ASM.&amp;nbsp;[/quote]
&lt;p&gt;Neither am I, sorry&amp;nbsp;&amp;nbsp;&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&amp;nbsp;But why do you need to do this in assembly, then? And why in the startup file if all you want is a stack guard? You are free to play with this as you like to experiment but I do not have much to add.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/thread/314046?ContentTypeID=1</link><pubDate>Mon, 07 Jun 2021 23:13:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5c8e4cd0-ab22-4c17-9167-dc2f0837aece</guid><dc:creator>morpho</dc:creator><description>&lt;p&gt;Thanks.&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/75995/determining-stack-overflow-by-writing-to-a-stack-in-a-startup-file/313866#313866"]Yes, that is entirely correct. STACK_TOP is the end of the stack (i.e. the next address after the highest address of the stack), which is typically what you initialize the stack pointer with (unless you want to waste some stack that will never be used).&amp;nbsp;STACK_BASE is the end/lowest address of the stack[/quote]
&lt;p&gt;Where are&amp;nbsp;__StackLimit and&amp;nbsp;__StackTop defined? Segger isn&amp;#39;t helping me navigate and it gets annoying at times.&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/75995/determining-stack-overflow-by-writing-to-a-stack-in-a-startup-file/313866#313866"]So for instance if you are using the nRF52840 which has 256 kB RAM and as will all nRF52 devices the RAM is mapped to start at 0x20000000, the STACK_INIT_VAL will be 0x20040000.[/quote]
&lt;p&gt;I see. Can you confirm if `STACK_INIT_VAL` equals `0x20040000` which marks the end of the RAM i.e what MSP points to at the start of main? I found its value in the `.map` file.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So if the RAM is between `0x2000 0000 - 0x2004 0000`, how much of that is taken up by the stack?&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/75995/determining-stack-overflow-by-writing-to-a-stack-in-a-startup-file/313866#313866"]You can make an assembly loop and write to any part of the RAM in the startup file yes, including the stack and memory immediately after the stack. [/quote]
&lt;p&gt;Sorry, I&amp;#39;m not great at ASM.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For instance, if I want to write `5`, I&amp;#39;d perhaps do something like the following?&lt;/p&gt;
&lt;p class="p1"&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;#160; ldr r2, =&amp;#160;STACK_INIT_VAL

&amp;#160; movs&amp;#160; r1, #5

&amp;#160; str&amp;#160; r1, [r2]&lt;/pre&gt;&lt;/p&gt;
&lt;p class="p1"&gt;&lt;/p&gt;
&lt;p class="p1"&gt;Regarding stack guard, sure sounds like something I could use but I also want to write something from scratch to understand how one could detect a stack overflow by writing a simple program. Though, is it just the&amp;nbsp;nrf_stack_guard_init function that&amp;#39;s used?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/thread/313866?ContentTypeID=1</link><pubDate>Mon, 07 Jun 2021 09:47:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7d3e24e4-d3e1-47c3-9ddc-42689c1a3088</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="morpho"]- In&amp;nbsp;&lt;span&gt;&amp;lt;nRF5 SDK&amp;gt;\components\libraries\util\app_util.h, I see there&amp;#39;s a bunch of definitions for STACK_TOP and STACK_BASE. Which one is possibly used?&lt;/span&gt;[/quote]
&lt;p&gt;These are for different toolchains. When you use SES the GNUC part is relevant, so it would be this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;extern uint32_t __StackTop;
extern uint32_t __StackLimit;
#define STACK_BASE    &amp;amp;__StackLimit
#define STACK_TOP     &amp;amp;__StackTop&lt;/pre&gt;&lt;/p&gt;
[quote user="morpho"]-&amp;nbsp;I reckon STACK_TOP refers to the starting address of the stack i.e what MSP points to when the program starts in main?&amp;nbsp;[/quote]
&lt;p&gt;Yes, that is entirely correct. STACK_TOP is the end of the stack (i.e. the next address after the highest address of the stack), which is typically what you initialize the stack pointer with (unless you want to waste some stack that will never be used).&amp;nbsp;STACK_BASE is the end/lowest address of the stack.&lt;/p&gt;
[quote user="morpho"]&lt;span&gt;what does&amp;nbsp;&lt;/span&gt;&lt;strong&gt;STACK_INIT_VAL&lt;/strong&gt; store?[/quote]
&lt;p&gt;This is the address of the end of the RAM. You can see that from how it is defined in modules\nrfx\mdk\ses_startup_nrf_common.s:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#ifndef STACK_INIT_VAL
#define STACK_INIT_VAL __RAM1_segment_end__
#endif&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So for instance if you are using the nRF52840 which has 256 kB RAM and as will all nRF52 devices the RAM is mapped to start at 0x20000000, the STACK_INIT_VAL will be 0x20040000.&lt;/p&gt;
[quote user="morpho"]Is it perhaps using &lt;strong&gt;str&lt;/strong&gt;&amp;nbsp;asm command in a startup file to write at address 0x2000 0000 where the DATA RAM starts in nRF52?[/quote]
&lt;p&gt;You can make an assembly loop and write to any part of the RAM in the startup file yes, including the stack and memory immediately after the stack. Typically for a stack guard you would write a known pattern immediately below the stack and check that regularly. (If you want to measure the stack usage you write the pattern within the stack, though.)&lt;/p&gt;
&lt;p&gt;Note that if all you want is a stack guard to detect a stack overflow, as the topic of your original post suggests, then you can simply use what we provide for you in with the &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/group__nrf__stack__guard.html"&gt;stack guard library&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/thread/313742?ContentTypeID=1</link><pubDate>Fri, 04 Jun 2021 17:07:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b718901-b626-4a47-a948-31280b707a70</guid><dc:creator>morpho</dc:creator><description>&lt;p&gt;Thanks for the response.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;- In&amp;nbsp;&lt;span&gt;&amp;lt;nRF5 SDK&amp;gt;\components\libraries\util\app_util.h, I see there&amp;#39;s a bunch of definitions for STACK_TOP and STACK_BASE. Which one is possibly used?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-&amp;nbsp;I reckon STACK_TOP refers to the starting address of the stack i.e what MSP points to when the program starts in main?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;- none of this is used inside&amp;nbsp;&lt;strong&gt;ses_startup_common.s&lt;/strong&gt;. In fact, the following seems to be stored in a stack pointer, hence my question about what does&amp;nbsp;&lt;/span&gt;&lt;strong&gt;STACK_INIT_VAL&lt;/strong&gt; store?&lt;/p&gt;
&lt;pre&gt;ldr r0, =STACK_INIT_VAL
ldr r1, =0x7
bics r0, r1
mov sp, r0&lt;br /&gt;&lt;br /&gt;- how would you fill in the stack memory though? does what I mentioned in my previous comment make sense?&lt;/pre&gt;
&lt;p&gt;Is it perhaps using &lt;strong&gt;str&lt;/strong&gt;&amp;nbsp;asm command in a startup file to write at address 0x2000 0000 where the DATA RAM starts in nRF52?&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fmemory.html"&gt;infocenter.nordicsemi.com/index.jsp&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Determining stack overflow by writing to a stack in a startup file</title><link>https://devzone.nordicsemi.com/thread/313692?ContentTypeID=1</link><pubDate>Fri, 04 Jun 2021 13:17:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c3a77d44-1a35-4bbb-bccf-3140ecadc165</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can see how to find the start and end of the stack for different toolchains by searching for&amp;nbsp;&lt;code&gt;STACK_BASE&lt;/code&gt; and&amp;nbsp;&lt;code&gt;STACK_TOP&lt;/code&gt; in &amp;lt;nRF5 SDK&amp;gt;\components\libraries\util\app_util.h. Note that if you just want a stack guard (and not a way to measure stack usage) and use the nRF5 SDK, then you can use the &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/group__nrf__stack__guard.html"&gt;Stack guard librar&lt;/a&gt;y.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>