emscripten-core / emsdk

Emscripten SDK
http://emscripten.org
Other
2.96k stars 676 forks source link

Unhandled Promise Rejection: RuntimeError: Out of bounds call_indirect (evaluating '(_main=Module["_main"]=wasmExports["ge"])(a0,a1)') on Safari #1298

Closed skhaz closed 9 months ago

skhaz commented 9 months ago

I have a project written in C++ with SDL that utilizes emsdk/3.1.44 for compiling to WebAssembly. It works seamlessly on Chrome and Firefox, but encounters issues on Safari 17. On desktop, I encounter the following error in the console.

Unhandled Promise Rejection: RuntimeError: Out of bounds call_indirect (evaluating '(_main=Module["_main"]=wasmExports["ge"])(a0,a1)')

It is possible to see here https://demo.carimbo.cloud

I don't change anything at all in the generated .js file, and this is my index.html.

<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
  <center>
    <canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
    <script type='text/javascript'>
      var Module = {
        canvas: (function () { return document.getElementById('canvas'); })()
      };
    </script>
    <script src="carimbo.js"></script>
  </center>
</body>

Is it a known issue or is it on my side?

Thank you.

sbc100 commented 9 months ago

Do you have the full backtrack available? If you build with -sASSERTIONS and--profiling-funcs that should be enough to tell you where your code the bad call_indirect happens.

Out of bounds call_indirect normally means a bad function pointer in somewhere, likely due to memory corruption. You can also try building with -fsanitize=address to help debug these types of errors.

As always, its also useful if you can share the full set of link flags that you used to build your module.

skhaz commented 9 months ago

Thank you very much for your help, now I have a better understanding of the error.

However, I don't have a function called 'apply' in my code, so I imagine it must be from a 3rd party, right?

Unhandled Promise Rejection: RuntimeError: Out of bounds call_indirect (evaluating 'f.apply(null,arguments)')

sbc100 commented 9 months ago

Indeed, but the best way to track down the failure is going to be to get a full stack trace. Do you know why you don't see one? Are you able to reproduce in devtools?

skhaz commented 9 months ago

Saddly I don't have any stack trace. Even with the flags and sanitizer that you recommended.

It's strange that it's only happening in Safari.

sbc100 commented 9 months ago

Are you able to reproduce in your browser with devtools open? What do you see in the console?

Perhaps install an Unhandled Promise Rejection handler on your page and then set a breakpoint in there?

Perhaps try adding -sRUNTIME_DEBUG to your link flags along with -sASSERTIONS=2

skhaz commented 9 months ago

I came across something interesting in the handler, 'isTrusted' equals false. Does this mean that my binary is not considered safe by Apple, similar to binaries for macOS?

[Warning] UNHANDLED PROMISE REJECTION: RuntimeError: Out of bounds call_indirect (evaluating 'f.apply(null, arguments)') (trial.carimbo.cloud, line 40) [Warning] {"isTrusted":false} (trial.carimbo.cloud, line 41)

sbc100 commented 9 months ago

Sorry, I'm afraid I don't know what that means.

Were you able to get a backtrace?

Are you example to run simple emscripten example in this environment? e.g. emcc -o out.html test/hello_world.c and then load out.html in your browser?

skhaz commented 9 months ago

I am 98% sure that the problem is actually in the 3rd party library (if you are reading this and are interested to know, my guess is the Sol2 library, a C++ wrapper for Lua), because I commented out the part that calls it and it ran normally.

Thank you very much, @sbc100, for your help. Feel free to close.

skhaz commented 4 months ago

@sbc100 FYI it is now working now, I haven't changed anything. It works on mobile and desktop Safari. Go figure out...