<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Safe-stack leaks stack with dynamic alloca"
   href="https://llvm.org/bugs/show_bug.cgi?id=26122">26122</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Safe-stack leaks stack with dynamic alloca
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Miscellaneous Instrumentation passes
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kuba.brecka@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Safe-stack instrumentation seems to wrongly handle some dynamic alloca cases.
Here's a test case that crashes when -fsanitize=safe-stack is used on it. We
seem to forget to reset the unsafe stack pointer when leaving the function,
causing a stack leak and eventually overrunning the unsafe stack.


#include <alloca.h>
#include <stdio.h>

void f() {
    for (int i = 0; i < 10; i++) {
        int *p = alloca(4096);
        *p = 42;
    }
}

int main(int argc, const char * argv[]) {
    printf("Hello, World!\n");

    for (int i = 0; i < 10000000; i++) {
        f();
    }

    return 0;
}


Disassembly:

f:
    0x100000ed0 <+0>:  pushq  %rbp
    0x100000ed1 <+1>:  movq   %rsp, %rbp
    0x100000ed4 <+4>:  movl   $0x0, -0x4(%rbp)
    0x100000edb <+11>: cmpl   $0xa, -0x4(%rbp)
    0x100000edf <+15>: jge    0x100000f1d               ; <+77> at
safe-stack-alloca-crash.c:9
    0x100000ee5 <+21>: movq   %gs:0x80, %rax
    0x100000eee <+30>: addq   $-0x1000, %rax
    0x100000ef4 <+36>: andq   $-0x10, %rax
    0x100000ef8 <+40>: movq   %rax, %gs:0x80
    0x100000f01 <+49>: movq   %rax, -0x10(%rbp)
    0x100000f05 <+53>: movq   -0x10(%rbp), %rax
    0x100000f09 <+57>: movl   $0x2a, (%rax)
    0x100000f0f <+63>: movl   -0x4(%rbp), %eax
    0x100000f12 <+66>: addl   $0x1, %eax
    0x100000f15 <+69>: movl   %eax, -0x4(%rbp)
    0x100000f18 <+72>: jmp    0x100000edb               ; <+11> at
safe-stack-alloca-crash.c:5
    0x100000f1d <+77>: popq   %rbp
    0x100000f1e <+78>: retq</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>