Re: Crash with mozjs-76 on Linux in _dl_runtime_resolve() / dl_fixup() as soon as program starts

Miles <[email protected]> Thu, 21 May 2020 07:38:44 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
On Tuesday, 19 May 2020 17:24:52 UTC+1, Miles  wrote:
> Hi,
> 
> I'm trying to upgrade to mozjs-76 and have slowly being sorting out the compile errors so I get a program that can link. I've now done that but when I run the executable it crashes instantly on startup. If I look at where it is crashing in gdb I see (for a release build)
> 
> #0  0x000000393100e02c in _dl_fixup () from /lib64/ld-linux-x86-64.so.2
> #1  0x0000003931014725 in _dl_runtime_resolve () from /lib64/ld-linux-x86-64.so.2
> #2  0x00007ffff6e2f170 in ?? () from /spidermonkey/js76_release/lib/libmozjs-76.so
> #3  0x00007ffff706e9b2 in ?? () from /spidermonkey/js76_release/lib/libmozjs-76.so
> 
> If I use a debug build I get
> 
> #0  0x000000393100e02c in _dl_fixup () from /lib64/ld-linux-x86-64.so.2
> #1  0x0000003931014725 in _dl_runtime_resolve () from /lib64/ld-linux-x86-64.so.2
> #2  0x00007ffff570465e in js::MutexImpl::MutexImpl() ()
>     at /spidermonkey/tmp/mozjs-76.0.0/js/src/threading/Mutex.h:39
> #3  0x00007ffff57046ec in js::Mutex::Mutex(js::MutexId const&) ()
>     at /spidermonkey/tmp/mozjs-76.0.0/js/src/threading/Mutex.h:62
> #4  0x00007ffff670237f in _ZN2js13ExclusiveDataI12ReadLockFlagEC2IJEEERKNS_7MutexIdEDpOT_ ()
>     at /spidermonkey/tmp/mozjs-76.0.0/js/src/threading/ExclusiveData.h:110
> #5  0x00007ffff670ea11 in __static_initialization_and_destruction_0 ()
>     at /spidermonkey/tmp/mozjs-76.0.0/js/src/wasm/WasmProcess.cpp:336
> #6  0x00007ffff670eb10 in _GLOBAL__sub_I_Unified_cpp_js_src_wasm2.cpp ()
>     at /spidermonkey/tmp/mozjs-76.0.0/js/src/wasm/WasmStubs.cpp:2823
> #7  0x00007ffff6997a42 in __do_global_ctors_aux () from /spidermonkey/js76_debug/lib/libmozjs-76.so
> #8  0x00007fffffffd9a8 in ?? ()
> #9  0x0000000000000001 in ?? ()
> #10 0x00007fffffffd9a8 in ?? ()
> #11 0x00007ffff567f5bb in _init () from /spidermonkey/js76_debug/lib/libmozjs-76.so
> 
> 
> I presumed I was doing something wrong so I went back to trying to run the simplest program I could think of using the JS API
> 
> #include "jsapi.h"
> #include "js/Initialization.h"
> 
> int main(int argc, const char *argv[])
> {
>     JS_Init();
>     JS_ShutDown();
>     exit(0);
> }
> 
> and I'm compiling that with something like
> 
> g++ -std=gnu++17 -include=/spidermonkey/js76_release/include/mozjs-76/js/RequiredDefines.h -I/spidermonkey/js76_release/include/mozjs-76 -O -pthread test.cpp -o test -rdynamic -lm -ldl -lpthread -lrt -lz -L/spidermonkey/js76_release/lib -lmozjs-76
> 
> and that still crashes in exactly the same way.
> 
> So I then commented out the JS_Init() and JS_ShutDown() calls (but still link the executable with -lmozjs-76)
> 
> and it *still* crashes.
> 
> If I remove  -lmozjs-76 from the linker the program runs (but obviously then doesn't do anything!)
> 
> I'm compiling this on an old CentOS 6 machine so the installed g++ compilers are ancient. I compiled g++ 9.3 from source and that's what I used to build.
> 
> I successfully managed to build mozjs-76 and the JS shell and the test suite all ran OK and passed so the JS shell clearly works OK using the compilers I used.
> 
> I've spent the last few days trying to understand why the shell works OK but my executable doesn't.
> One difference was that the shell linked to libjs_static.a and libjsrust.a instead of libmozjs-76.so so I tried linking statically instead of using the shared library but I still get the same crash.
> 
> I presume I'm somehow getting some sort of incompatibility when the library is being initialised but I'm completely at a loss as to why the JS shell works but it crashes for me. There must be *something* that I'm doing wrong...
> I hope I'm just being really stupid and missing something.
> Can anybody help?
> 
> Many thanks
> 
> Miles

Many thanks to everyone for their replies and pointing me in the correct direction of looking at mozglue.
The problem was indeed the fact that mozglue isn't included in libmozjs-76.so.
I fixed this my looking at the input to the linker when building the JS shell and the input when building libmozjs-76.so.
For the shell it has the following inputs to g++ when linking

INPUT("Unified_cpp_js_src_shell0.o")
INPUT("../../../memory/build/Unified_cpp_memory_build0.o")
INPUT("../../../memory/mozalloc/cxxalloc.o")
INPUT("../../../memory/mozalloc/mozalloc_abort.o")
INPUT("../../../memory/mozalloc/Unified_cpp_memory_mozalloc0.o")
INPUT("../../../mozglue/misc/AutoProfilerLabel.o")
INPUT("../../../mozglue/misc/ConditionVariable_posix.o")
INPUT("../../../mozglue/misc/Mutex_posix.o")
INPUT("../../../mozglue/misc/Printf.o")
INPUT("../../../mozglue/misc/StackWalk.o")
INPUT("../../../mozglue/misc/TimeStamp.o")
INPUT("../../../mozglue/misc/TimeStamp_posix.o")
INPUT("../../../mozglue/misc/Decimal.o")
INPUT("../../../mfbt/lz4.o")
INPUT("../../../mfbt/lz4frame.o")
INPUT("../../../mfbt/lz4hc.o")
INPUT("../../../mfbt/xxhash.o")
INPUT("../../../mfbt/Compression.o")
INPUT("../../../mfbt/Unified_cpp_mfbt0.o")
INPUT("../../../mfbt/Unified_cpp_mfbt1.o")
INPUT("../editline/Unified_c_js_src_editline0.o")

I added all of the memory, mozglue and mfbt inputs to the file libmozjs-76.so.list and relinked libmozjs-76.so and my test program now works! :-)

@Steve Fink, I did the above hack before I read your reply about using --disable-jemalloc. Does adding that 'properly' do what I did above manually or is that something else/another issue that I might face?

Thanks again for the help. Now back to upgrading my embedding... :-)

Miles