{"id":969,"date":"2022-08-30T15:10:12","date_gmt":"2022-08-30T15:10:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/what-is-on-the-stack-before-my-program-starts-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:12","modified_gmt":"2022-08-30T15:10:12","slug":"what-is-on-the-stack-before-my-program-starts-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/what-is-on-the-stack-before-my-program-starts-collection-of-common-programming-errors\/","title":{"rendered":"What is on the stack before my program starts?-Collection of common programming errors"},"content":{"rendered":"<p>Before the execution of <code>main()<\/code> a bunch of other operations need to be done by the OS to properly setup the environment before control of the execution is handled to your application. So, most of what&#8217;s on the stack at this point is garbage left from previous operations.<\/p>\n<p>Right before <code>main()<\/code> is executed, you can expect to find <code>argc<\/code> and <code>argv<\/code> on the stack as well.<\/p>\n<p><strong>EDIT:<\/strong><\/p>\n<p>A comment from a user kinda challenged me to go through the process of debugging an assembly application in <strong>gdb<\/strong> and examining the stack to backup a statement I made on the original answer.<\/p>\n<p>So please consider the following assembly code written in <em>nasm<\/em>:<\/p>\n<pre><code>section .data\n\n  mymsg db \"hello, world\", 0xa  ; string with a carriage-return\n  mylen equ $-mymsg             ; string length in bytes\n\nsection .text\nglobal mystart                ; make the main function externally visible\n\nmystart:\n    ; prepare the arguments for syscall write()\n    push dword mylen          ; msg length                           \n    push dword mymsg          ; msg to write\n    push dword 1              ; file descriptor number\n\n    ; call write()\n    mov eax, 0x4              ; 0x4 identifies syscall write()\n    sub esp, 4                ; OS X (and BSD) syscalls needs \"extra space\" on stack\n    int 0x80                  ; trigger the call\n\n    ; clean up the stack\n    add esp, 16               ; 3 args * 4 bytes\/arg + 4 bytes extra space = 16 bytes\n\n    ; prepare argument for syscall exit()\n    push dword 0              ; exit status returned to the operating system\n\n    ; call exit()\n    mov eax, 0x1              ; 0x1 identifies syscall exit()\n    sub esp, 4                ; OS X (and BSD) system calls needs \"extra space\" on stack\n    int 0x80                  ; trigger the call\n<\/code><\/pre>\n<p>I compiled this on Mac OS X with:<\/p>\n<pre><code>nasm -f macho -o hello.o hello.nasm\nld -o hello -e mystart hello.o \n<\/code><\/pre>\n<p>As you can probably tell by the source code, the start of the application is defined by <code>mystart<\/code>, and it doesn&#8217;t take any parameters.<\/p>\n<p>Now, let&#8217;s make this investigation a little more exciting by opening this program in <strong>gdb<\/strong>:<\/p>\n<pre><code>gdb .\/hello\n<\/code><\/pre>\n<p>After <strong>gdb<\/strong> has loaded, it&#8217;s important for educational purposes to set a cmd line parameter for this application even though it wasn&#8217;t written to accept any.<\/p>\n<pre><code>set args deadbeef\n<\/code><\/pre>\n<p>The application is still not running at this point. We need to set a <em>breakpoint<\/em> to the beginning of the <em>main<\/em> function so can inspect the stack to see what&#8217;s going on before our application starts executing it&#8217;s own code:<\/p>\n<pre><code>break mystart\n<\/code><\/pre>\n<p>Execute the command <code>run<\/code> on gdb to start the application and break the execution. Now we can inspect the stack with:<\/p>\n<pre><code>x\/20xw $esp\n<\/code><\/pre>\n<p>outputs:<\/p>\n<pre><code>(gdb) x\/20xw $esp\n0xbffff8cc: 0x00002000  0x00000000  0x00000002  0xbffff96c\n0xbffff8dc: 0xbffff98b  0x00000000  0xbffff994  0xbffff9b0\n0xbffff8ec: 0xbffff9c1  0xbffff9d1  0xbffffa0b  0xbffffa40\n0xbffff8fc: 0xbffffa5b  0xbffffa86  0xbffffa97  0xbffffaad\n0xbffff90c: 0xbffffad8  0xbffffafa  0xbffffb06  0xbffffb28\n<\/code><\/pre>\n<p>Yes Sir, this command prints the contents of the stack. It tells <strong>gdb<\/strong> to show 20 words in hexadecimal format starting at the address stored by the <code>$esp<\/code> register.<\/p>\n<p>Let&#8217;s see, <code>$esp<\/code> actually points to <code>0xbffff8cc<\/code>, ok but examining what&#8217;s stored by this memory address reveals <em>another<\/em> address: <code>0x00002000<\/code>. To what does it points to???<\/p>\n<pre><code>(gdb) x\/20sw 0x00002000\n0x2000 :  \"hello, world\\n\"\n<\/code><\/pre>\n<p>Not a shocker, right?! So let&#8217;s take a look at what some of the other addresses of the table are pointing to:<\/p>\n<pre><code>(gdb) x\/1sw 0xbffff96c\n0xbffff96c:  \"\/Developer\/workspace\/asm\/hello\"\n<\/code><\/pre>\n<p>Wow. That&#8217;s actually the original application&#8217;s <strong>name and path<\/strong> stored right there on the stack! Awesome, let&#8217;s continue to the next interesting address of the table:<\/p>\n<pre><code>(gdb) x\/1sw 0xbffff98b\n0xbffff98b:  \"deadbeef\"\n<\/code><\/pre>\n<p>Jackpot! The cmd line argument we passed upon executing our application also got stored in the stack. So as I&#8217;ve stated before, among the garbage stored in the stack before your application executes, you can also find cmd line parameters that were used to execute the application even when the <code>main()<\/code> function of the application is <code>void<\/code> and doesn&#8217;t take any parameters.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:08:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Before the execution of main() a bunch of other operations need to be done by the OS to properly setup the environment before control of the execution is handled to your application. So, most of what&#8217;s on the stack at this point is garbage left from previous operations. Right before main() is executed, you can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}