<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 --- - WebAssembly alias stack pointer not initialized"
   href="https://llvm.org/bugs/show_bug.cgi?id=30862">30862</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>WebAssembly alias stack pointer not initialized
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Backend: WebAssembly
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>timo.savola@iki.fi
          </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>Created <span class=""><a href="attachment.cgi?id=17529" name="attach_17529" title="Small demo program">attachment 17529</a> <a href="attachment.cgi?id=17529&action=edit" title="Small demo program">[details]</a></span>
Small demo program

LLVM seems to use the 32-bit value at WebAssembly linear memory offset 4 as
__stack_pointer, which is used to access the secondary stack within the linear
memory.  However, while it's correctly maintained by function prologue and
epilogue, it's not initialized at the start of the program.

If the size of the WebAssembly memory would be constant, and indexing the
linear memory would wrap according to the memory size, things would work by
default.  But neither of those is the case.

Perhaps the memory location is supposed to be initialized by some WebAssembly C
runtime library that needs to be linked to the program?  Or is the WebAssembly
runtime environment expected to set that up somehow (seems wrong)?

I've managed to run LLVM-compiled WebAssembly programs (in a custom runtime) by
initializing the stack pointer with the size of the initial memory limit (and
thus assuming that the stack is located at the end of the initial linear
memory).

The attached test.c can be compiled to a valid WebAssembly program using
LLVM/clang compiled with the experimental WebAssembly target and the tools from
<a href="https://github.com/WebAssembly/binaryen">https://github.com/WebAssembly/binaryen</a> and
<a href="https://github.com/WebAssembly/wabt">https://github.com/WebAssembly/wabt</a>:

    $ clang --target=wasm32 -Os -S -o test.s test.c
    $ s2wasm --start=main test.s > test.wast
    $ wast2wasm -o test.wasm test.wast

I located the stack management code in
lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp, but could't find a place
which attempts to initialize the __stack_pointer.</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>