<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - WASM - linking functions with missing arguments results in wrong args"
   href="https://bugs.llvm.org/show_bug.cgi?id=36145">36145</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>WASM - linking functions with missing arguments results in wrong args
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>All Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>cjd@cjdns.fr
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I'm unsure of whether to report this here or elsewhere, it might be clang, it
might be an ABI issue, but in any case I assume emscripten doesn't have this
problem because it seems to stop most software from running.

To reproduce:

## missing_args.c
```c
#include <stdio.h>
int callme_maybe(int arg1, int arg2, int arg3);
int main() {
    printf("3 = %d", callme_maybe(1, 2, 3));
}
```

## missing_args2.c
```c
int callme_maybe(int arg1, int arg2) {
    return arg1 + arg2;
}
```

```bash
/Users/user/wrk/play/clang-wasm-toolchain/build/clang/bin/clang
./test/missing_args.c ./test/missing_args2.c -target
wasm32-unknown-unknown-wasm -mthread-model single
-B/Users/user/wrk/play/clang-wasm-toolchain/build/bin/ -idirafter
/Users/user/wrk/play/clang-wasm-toolchain/projects/kernel-headers/generic/include
-I /Users/user/wrk/play/clang-wasm-toolchain/include_asm -D__linux=1
-D__linux__=1 -D__gnu_linux__=1 -Dlinux=1 --sysroot
/Users/user/wrk/play/clang-wasm-toolchain/build/musl -idirafter
/Users/user/wrk/play/clang-wasm-toolchain/build/musl/include
-Wl,--allow-undefined-file=/Users/user/wrk/play/clang-wasm-toolchain/wasm.syms
-rtlib=compiler-rt -resource-dir
/Users/user/wrk/play/clang-wasm-toolchain/build/compiler-rt -o a.out.wasm

<called by clang> /Users/user/wrk/play/clang-wasm-toolchain/build/lld/bin/lld
-flavor wasm -L/Users/user/wrk/play/clang-wasm-toolchain/build/musl/lib
/Users/user/wrk/play/clang-wasm-toolchain/build/musl/lib/crt1.o
/var/folders/z5/1x_ykg5d3bn162n3sm9ph4q00000gn/T/missing_args-707012.o
/var/folders/z5/1x_ykg5d3bn162n3sm9ph4q00000gn/T/missing_args2-83ae6c.o
--allow-undefined-file=/Users/user/wrk/play/clang-wasm-toolchain/wasm.syms -lc
/Users/user/wrk/play/clang-wasm-toolchain/build/compiler-rt/lib/libclang_rt.builtins-wasm32.a
-o a.out.wasm -z relro -z now

node runner.js a.out.wasm
3 = 5
```

Seems that the linker is ordering args in reverse order so if there is a
function definition with missing args, it will pass the wrong args.

You can reproduce this more easily using
<a href="https://github.com/cjdelisle/clang-wasm-toolchain">https://github.com/cjdelisle/clang-wasm-toolchain</a> where you only need to do
./build/bin/wasm32-unknown-unknown-wasm-cc ./test/missing_args.c
./test/missing_args2.c and then run the resulting a.out.wasm</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>