wasm-ld: error: --shared-memory is disallowed by lto.tmp because it was not compiled with 'atomics' or 'bulk-memory' features.

549 views
Skip to first unread message

Ronny Nissengold

unread,
Jul 17, 2023, 9:49:27 AM7/17/23
to emscripten-discuss
Hi all!
I am trying to use multithreading in my project.
I got this error when passing -pthread both to compiler and linker.
I am using cmake.
Any idea how to overcome this?
Thanks!

Thomas Lively

unread,
Jul 17, 2023, 11:51:26 AM7/17/23
to emscripte...@googlegroups.com
This error means that some code wasn't compiled with -pthread and therefore is not safe to execute in a multithreaded environment. Normally the error message tells you which object is the culprit, but it's less helpful here because you're using LTO. Does the problem persist if you turn off LTO?

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/acbc873e-078a-4082-8761-6c0322246170n%40googlegroups.com.

Ronny Nissengold

unread,
Jul 18, 2023, 2:32:59 AM7/18/23
to emscripten-discuss
Thank you for your response. How do I turn off LTO?

ב-יום שני, 17 ביולי 2023 בשעה 18:51:26 UTC+3, tli...@google.com כתב/ה:

Thomas Lively

unread,
Jul 18, 2023, 9:06:32 AM7/18/23
to emscripte...@googlegroups.com
If you look at your CMake script, you should see that it adds -flto at some point. That’s the option you’ll want to remove. You may be able to remove it from your CMakeCache.txt file and recompile without running cmake again.

Ronny Nissengold

unread,
Jul 19, 2023, 5:45:53 AM7/19/23
to emscripten-discuss
Thank you all for your support.
I was able to successfully compile my project with pthread enabled.
Now I face a new problem, I used Module._malloc to pass an  image from a webcam into my cpp function
For that I used the following linker flags: add_link_options(-sINITIAL_MEMORY=800MB -sTOTAL_STACK=80MB -O3 -s -sEXPORTED_FUNCTIONS=['_malloc','_free'] -sASSERTIONS -pthread)

Now I get the following error in my console:

Uncaught ReferenceError: SharedArrayBuffer is not defined
    at blink_landmark_detector_sample.js:1:9999

(index):11 Uncaught TypeError: Module._malloc is not a function
    at _arrayToHeap ((index):11:24)
    at HTMLVideoElement.computeFrame ((index):40:22)

Any idea what happened?
Thanks!
Ronny
ב-יום שלישי, 18 ביולי 2023 בשעה 16:06:32 UTC+3, tli...@google.com כתב/ה:

Ronny Nissengold

unread,
Jul 19, 2023, 6:56:18 AM7/19/23
to emscripten-discuss
Just to make a few things clear:
1. I used to have a CPP thread which crashed whenever trying to create the thread. I removed it and then my WASM code worked perfectly.
2. (Not sure if relevant) What my code does is basically- Receive an image from Webcam, copy it to a JS arrayBuffer, call a callback to my CPP function, modify the image in cpp, return the image, and paint it on a canvas. With some cool AI in the process with OnnxRuntime and OpenCV compiled in ;)
3. I do want to use CPP threads, so to solve 1, I recompiled everything with -pthread, and then everything successfully recompiled.  Also some worker.js was created I am not sure what to do with...
4. After I thought I am finished, somehow the project forgot what SharedArrayBuffer is, which I guess causes all the other errors as well.
ב-יום רביעי, 19 ביולי 2023 בשעה 12:45:53 UTC+3, ‪Ronny Nissengold‬‏ כתב/ה:

Thomas Lively

unread,
Jul 19, 2023, 4:00:58 PM7/19/23
to emscripte...@googlegroups.com
Do you perhaps not have your COOP and COEP headers set? Those are required to make SharedArrayBuffer (and Wasm threads in general) available. https://web.dev/coop-coep/

Shlomi Fish

unread,
Jul 20, 2023, 2:55:27 AM7/20/23
to Ronny Nissengold, emscripte...@googlegroups.com
hi Ronny!

On Wed, 19 Jul 2023 02:45:53 -0700 (PDT)
Ronny Nissengold <ronn...@gmail.com> wrote:

> Thank you all for your support.
> I was able to successfully compile my project with pthread enabled.
> Now I face a new problem, I used Module._malloc to pass an image from a
> webcam into my cpp function
> For that I used the following linker
> flags: add_link_options(-sINITIAL_MEMORY=800MB -sTOTAL_STACK=80MB -O3 -s
> -sEXPORTED_FUNCTIONS=['_malloc','_free'] -sASSERTIONS -pthread)
>
> Now I get the following error in my console:
>
> Uncaught ReferenceError: SharedArrayBuffer is not defined
> at blink_landmark_detector_sample.js:1:9999
>
> (index):11 Uncaught TypeError: Module._malloc is not a function
> at _arrayToHeap ((index):11:24)
> at HTMLVideoElement.computeFrame ((index):40:22)
>
> Any idea what happened?

try using 'Module.malloc' [without the underscore] or 'cwrap'.

HTH

> Thanks!
> Ronny



--

Shlomi Fish https://www.shlomifish.org/
https://github.com/shlomif/validate-your-html - Validate Your HTML

Chuck Norris knows what the gender of Great A’Tuin, the Discworld world turtle,
is.
https://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - https://shlom.in/reply .

Ronny Nissengold

unread,
Jul 20, 2023, 3:47:07 AM7/20/23
to emscripten-discuss
Using malloc didn't work.
By setting the coop policy, using requestly, I now get new errors:
Refused to execute script from 'http://localhost:8000/blink_landmark_detector_sample.js' because its MIME type ('text/plain') is not executable.
At least now SharedArrayBuffer is recognised.
Looks like I am close but still no cigar.
Any ideas?
Thanks.

ב-יום חמישי, 20 ביולי 2023 בשעה 09:55:27 UTC+3, Shlomi Fish כתב/ה:

Ronny Nissengold

unread,
Jul 20, 2023, 3:54:53 AM7/20/23
to emscripten-discuss
I noticed the error appears when trying to call the worker script:
blink_landmark_detector_sample.worker.js:1 Refused to execute script from 'http://localhost:8000/blink_landmark_detector_sample.js' because its MIME type ('text/plain') is not executable.
handleMessage @ blink_landmark_detector_sample.worker.js:1

ב-יום חמישי, 20 ביולי 2023 בשעה 10:47:07 UTC+3, ‪Ronny Nissengold‬‏ כתב/ה:

Ronny Nissengold

unread,
Jul 20, 2023, 4:01:27 AM7/20/23
to emscripten-discuss
Sorry for spamming- 1 last question.
Do I even need the -pthread flag to use CPP threads, or is it only necessary for some JS threads I am yet to undestand?
Thanks,
Ronny

ב-יום חמישי, 20 ביולי 2023 בשעה 10:54:53 UTC+3, ‪Ronny Nissengold‬‏ כתב/ה:

Ronny Nissengold

unread,
Jul 20, 2023, 6:47:08 AM7/20/23
to emscripten-discuss
Sorry but one more thing. I am using std::future in my cpp code and it crashes. Maybe that is the curlpit?
To simplify all my qeuestions, how can I make this code work with WASM?
It is called deep within an exported Extern C function.
If I do not use a future, and call initialize explicitly, my application runs perfectly.

m_init_future = std::async(std::launch::async, [&]
{
initialize(m_gl_context, m_gl_display);
});
ב-יום חמישי, 20 ביולי 2023 בשעה 11:01:27 UTC+3, ‪Ronny Nissengold‬‏ כתב/ה:

Shlomi Fish

unread,
Jul 21, 2023, 2:05:13 AM7/21/23
to Ronny Nissengold, emscripte...@googlegroups.com
hi,

On Thu, 20 Jul 2023 00:47:07 -0700 (PDT)
Ronny Nissengold <ronn...@gmail.com> wrote:

> Using malloc didn't work.

did using cwrap() work? see:

https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html
https://www.shlomifish.org/humour/bits/New-versions-of-the-GPL/

Yawn! Too TL;DR! Dude, do you even tweet?
https://twitter.com/shlomif/status/504931822757638144

Thomas Lively

unread,
Jul 21, 2023, 11:13:07 AM7/21/23
to emscripte...@googlegroups.com, Ronny Nissengold
> Do I even need the -pthread flag to use CPP threads, or is it only necessary for some JS threads I am yet to undestand?

Yes you do.

> Refused to execute script from 'http://localhost:8000/blink_landmark_detector_sample.js' because its MIME type ('text/plain') is not executable.

This is probably also a server configuration problem. The MIME type for JS should be text/javascript.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Ronny Nissengold

unread,
Jul 30, 2023, 5:08:36 AM7/30/23
to emscripten-discuss
Thanks a lot

ב-יום שישי, 21 ביולי 2023 בשעה 18:13:07 UTC+3, tli...@google.com כתב/ה:
Reply all
Reply to author
Forward
0 new messages