<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64526>64526</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [wasm] Feature Request: Please add a warning/error diagnostic option specifically for Wasm ABI function pointer casts
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          juj
      </td>
    </tr>
</table>

<pre>
    When running code compiled to WebAssembly, invoking a function pointer via an incompatible signature will cause a trap, e.g.:

```
RuntimeError: null function or function signature mismatch
```

LLVM supports two command line diagnostics to capture warning/error messages with respect to unsafe function pointer signature casts: `-Wbad-function-cast` and `-Wcast-function-type` to help detect such errors at compile-time.

However, these diagnostic flags are not designed to be accurate with respect to WebAssembly function dispatch signature rules. For example, the following program

```
void foo(int x) { }

int main()
{
  reinterpret_cast<void(*)(unsigned int)>(foo)(0); // (A)
  reinterpret_cast<void(*)(float)>(foo)(0.f); // (B)
  return 0;
}
```
will crash on line B when run in WebAssembly.

However, if the program is built with `-Wbad-function-cast`, no diagnostic will be given. If the program is built with `-Wcast-function-type`, a diagnostic will be given of both lines A and B, which can be counterproductive and overwhelming.

In WebAssembly, the function pointer call signature checks are performed with respect to only the primitive types that Wasm recognizes: `i32`, `i64`, `f32`, `f64` and `v128`. Hence under those rules, line A above is ok, but line B above is not.

In our scenario, when working on the Unity3D engine codebase, on the order of ~millions of code lines, enabling `-Wcast-function-type` will produce thousands of lines of diagnostics, of which most are false positives. Whereas enabling `-Wbad-function-cast` does not accurately catch the issues that occur with WebAssembly.

It would be extremely helpful to have a command line flag, which would only diagnose those function signature casts that precisely will violate the WebAssembly signature check rules, i.e. casts between the Wasm primitive types, and nothing else.

With such a flag, it would be more feasible to audit through our millions of lines of code, to focus on call sites that will be known at compile time to run into trouble. Would it be possible to add such a `-Wbad-wasm-function-cast` flag for example?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVl1v4zoO_TXOC1HDsZM0echDcnuDKTALLAbY7eNClmhbU1n06iOZ7sP-9gVlp3G_Zi9QpLYlUjyHh6SE97q1iPtsfczWDwsRQ0du_zP-XNSkXvZPHVpw0VptW5CkECT1gzaoIBA8YX3wHvvavGTlH6DtmZ55o4AmWhk0WRhI24AOzlqAsKAt24uga4PAR4sQHcJFGwNSRI8gIDgxsDvM2zyrDlnxkBXX300x_aXXH9EG3eOfzpHLqgPYaMztaHK359tRvfa9CLL71OH4-_37P_8GPg4DueAhXIhB98IqMNoiKC1aSz5o6ZkEKYYRg3DMUlaekOOBHr0XLXq46NCBQz-gDGwQrRcNfqToFqMUPngGlG2Ku6daqLvr5jteyjYFcDRplT_clsPLgLwcCDo0AygMfKqPsoMUlgcRrjm8Y_LyOfJvdMEzOmY_dOjnWKExovUgHIKlAAqTcJIMagQhZXQi4AewM4ncECvtB87BDLKLBn0OJ3KAv0Q_GJyCgIaMoQvLanDUOtH_RhBn0goaoqzcahvgV1buILs_Qnb_MLfitV5om5XbrNxNK_fH8QHAYUrI4DD8K_Fd_cGO0-4DG5TbaCf42gb-Uv2Zldt0MK8W6dMRsvKUlSfIyu3h9Zy_5L4xJD7zmzcfPB_feA7RWSiy6ngF9fApT2O9OeE7IDuq-giXqdZB23navhKIblJ6pqSA9lBHbcKogC-Fy5aW5rpKsdQIrT6jzeHx_7r9VPHsV3zpFqiBmkKXoHo4pPI5ss2l07IDKSzvlRTHzJCKMugzpn10Rnfp0PTatm_IeLTvO2DS6_u6lsKYeXF3KJ_HQhrQNeR6VB_Khqx5mXjQvU6hMFAPoRMBnoTvwaGk1ur_4LVT6KqciOCXzer20sxXmrRybSDnZbnNNkUO39BKhGgVOggd-akk2Sjp4wCipjNyQuiZv9YxXJXzumIpvGeIogMv0QqnaSQcLVzIpUFBNoH8h9XhpXoAtC075DlTC586wLSDHMdFDfy318Zosp5f0kBKKU3zworasNff9MWk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