Linking Opus with emcc

80 views
Skip to first unread message

lukewarmtea137

unread,
Jun 17, 2020, 8:24:26 PM6/17/20
to emscripten-discuss
Hi,
I've recently been trying to compile a small propgram to decode opus frames, and so have attempted to link the opus library for this.
I managed to get the opus library configured using:
emconfigure ./configure EMCONFIGURE_JS=1 --disable-intrinsics --disable-rtcd CFAGS='-02' --disable-asm --disable-oggtest --disable-extra-programs --prefix ~/emscripten_opus

And then I simply did:
emmake make && emmake install

So I then had the include/lib/share files in the directory ~/emscripten_opus.

I then attempted to compile my small program (which at present doesn't actually do anything - just makes the decoder using opus_decoder_create), and I compiled it with:
emcc main.c -s WASM=1 -Iinclude/opus -lopus -L$HOME/emscripten_opus/lib -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap", "ccall"]'

And running this causes the following issues:
[parse exception: attempted pop from empty stack / beyond block start boundary at 398161 (at 0:398161)]
Fatal: error in parsing input
emcc: error: '/home/user/emsdk/upstream/bin/wasm-emscripten-finalize --detect-features --global-base=1024 --check-stack-overflow /tmp/emscripten_temp_5cqqc8q2/a.out.wasm -o /tmp/emscripten_temp_5cqqc8q2/a.out.wasm.o.wasm' failed (1)

Does anyone have any idea what could be going wrong?
Message has been deleted

lukewarmtea137

unread,
Jun 17, 2020, 8:27:48 PM6/17/20
to emscripten-discuss
Additionally I've discovered that removing the -s LINKABLE=1 argument fixes this issue, however immeidatelly causes the page to crash when I run it (SIGSEGV), so I assume this is not the way to go about it.

Sam Clegg

unread,
Jun 18, 2020, 12:37:34 AM6/18/20
to emscripte...@googlegroups.com
You are correct to remove LINKABL=1.   You can also remove WASM=1 (its the default).

To debug your segfault you can try building and linking with `-fsanitize=address' or one of the debugging methods.

cheers,
sam

On Wed, Jun 17, 2020 at 5:27 PM lukewarmtea137 <syed24a...@gmail.com> wrote:
Additionally I've discovered that removing the -s LINKABLE=1 argument fixes this issue, however immeidatelly causes the page to crash when I run it (SIGSEGV), so I assume this is not the way to go about it.

--
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/2a14d39d-ced8-435a-bde6-fa4a991130dbo%40googlegroups.com.

lukewarmtea137

unread,
Jun 18, 2020, 6:06:02 AM6/18/20
to emscripten-discuss
Hi thanks for the reply,
`-fsanitize=address` printed that I should use `-s ALLOW_MEMORY_GROWTH=1`, which looked like it cleared all the issues.

However when I called my function `decodeOggPage`, this is printed in the console:
Assertion failed: Cannot call unknown function decodeOggPage, make sure it is exported

So I added `-s EXPORTED_FUNCTIONS="['_decodeOggPage']"` as another option, though this caused the compilation to fail with this error:
[parse exception: attempted pop from empty stack / beyond block start boundary at 20550 (at 0:20550)]
Fatal: error in parsing input
emcc: error: '/home/user/emsdk/upstream/bin/wasm-emscripten-finalize --detect-features --global-base=33554432 --check-stack-overflow /tmp/emscripten_temp_ugi93if_/a.out.wasm -o /tmp/emscripten_temp_ugi93if_/a.out.wasm.o.wasm' failed (1)

For reference, this is the command I was using for compilation:
emcc main.c -Iinclude/opus -lopus -L$HOME/emscripten_opus/lib -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap", "ccall"]' -s ALLOW_MEMORY_GROWTH=1 -fsanitize=address -s EXPORTED_FUNCTIONS="['_decodeOggPage']"

Additionally it appeared that if I removed `-fsanitize=address`, then the second time I called the following `Module.HEAP8.set(uint8array, uint8array.length)` (in the first time the offset was 0) it appeared to cause a segfault also, is this related?

lukewarmtea137

unread,
Jun 18, 2020, 8:46:45 AM6/18/20
to emscripten-discuss
After looking into it some more, I realised the error only occurs when I call an opus specific method such as `opus_decoder_create` - am I missing an option for linking this?

Sam Clegg

unread,
Jun 18, 2020, 12:20:41 PM6/18/20
to emscripte...@googlegroups.com
I imagine there is some issue with how you are writing your data the wasm memory,  What is the `Module.HEAP8.set(uint8array, uint8array.length)` supposed to be doing?   

You need to be very careful when writing the wasm memory that you don't overwrite anything in the heap, stack or static data regions.  In general you should only write the regions that have been allocated with malloc first, and then free them when you are done.      `Module.HEAP8.set(uint8array, uint8array.length)`` looks a little suspicious to me because because the second argument I believe is that offset, which means you are writing uint8array at offset uint8array.length .. which seems like something you certainly wouldn't want to do.  But I could be misunderstanding.

Regarding the crash in wasm-emscripten-finalize,  that looks like it must be a bug, if you can create a repro case a bug would be very welcome.

--
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.

lukewarmtea137

unread,
Jun 18, 2020, 2:45:06 PM6/18/20
to emscripten-discuss
Hi, thanks again for your help, I managed to get the Moduel.HEAP8.set(...) stuff working - I initially didn't quite understand how to use it but I realise now the function returns a pointer just like in C/C++.

And I made a small script which reproduces the error which is in the link below (it sets up emsdk, generates the opus library and does the compilation in the exact way I did).

lukewarmtea137

unread,
Jun 19, 2020, 8:46:08 AM6/19/20
to emscripten-discuss
In case anyone comes across this, the error that I got above isn't present on version emsdk 1.38.45 (I just went straight to this), and the steps to setup opus are completely fine (though there are some unnecessary options such as  --disable-oggtest for the emconfigure bit, and for the emcc options, -fsanitize=address doesn't work for that version of emsdk).
Reply all
Reply to author
Forward
0 new messages