Repository navigation
Closed
Description
@bnoordhuis @saghul @bellard I will try to dig into this myself too, but if you got any idea, it would be very helpful.
Original ticket about the issue is here: andrjohns/QuickJSR#88
But the bug is in quickjs-ng itself, as it appears, not in its R port or my R build environment.
Specifically, this example fails both with 0.10.x and 0.9.0:
wget https://github.com/stan-dev/stanc3/releases/download/nightly/stanc.js
./qjs --include ./stanc.js
qjs > stanc("model_name","data{}")
0.10.x crashes already on the first command:
Starting program: /opt/local/bin/qjs --include ./stanc.js
Reading symbols for shared libraries +++. done
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x03000300
0x03000300 in ?? ()
(gdb) bt
#0 0x03000300 in ?? ()
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
#1 0x000228b8 in JS_RunGC ()
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
Cannot access memory at address 0x3000300
#2 0x00025200 in JS_NewObjectFromShape ()
Cannot access memory at address 0x3000300
#3 0x00026d5c in JS_NewArrayFrom ()
#4 0x00014b40 in JS_CallInternal ()
#5 0x0001494c in JS_CallInternal ()
#6 0x0001494c in JS_CallInternal ()
#7 0x0001494c in JS_CallInternal ()
#8 0x0001494c in JS_CallInternal ()
#9 0x0001494c in JS_CallInternal ()
#10 0x0001494c in JS_CallInternal ()
#11 0x00019660 in JS_CallFree ()
#12 0x0006d03c in __JS_EvalInternal ()
#13 0x00040224 in JS_EvalThis2 ()
#14 0x000402ac in JS_Eval ()
#15 0x00003468 in eval_buf ()
#16 0x00003500 in eval_file ()
#17 0x00079b18 in main ()
0.9.0 handles the first command, but segfaults on the second:
$ qjs --include ./stanc.js
QuickJS-ng - Type ".help" for help
qjs > stanc("model_name","data{}")
Segmentation fault
For comparison, quickjs (non-ng one, 2025.04.05) works normally:
$ qjs --include ./stanc.js
QuickJS - Type "\h" for help
qjs > stanc("model_name","data{}")
{ result: "// Code generated by stanc v2.37.0-rc2-20-g216cb27\n#include <stan/model/m...", warnings: [ ] }
qjs >
And a build from May:
---> Activating quickjs-devel @20250520_0
Sergey-Fedorovs-Mac:~ svacchanda\$ qjs --include ./stanc.js
QuickJS - Type "\h" for help
qjs > stanc("model_name","data{}")
{ result: "// Code generated by stanc v2.37.0-rc2-20-g216cb27\n#include <stan/model/model_header.hpp>\nnamespace model_name_namespace {\nusing stan::model::model_base_crtp;\nusing namespace stan::math;\nstan::math::profile_map profiles__;\nstatic constexpr std::array<const char*, 1> locations_array__ =\n {\" (found before start of program)\"};\nclass model_name final : public model_base_crtp<model_name> {\n private:\n \n public:\n ~model_name() {}\n model_name(stan::io::var_context& context__, unsigned int\n random_seed__ = 0, std::ostream* pstream__ = nullptr)\n : model_base_crtp(0) {\n int current_statement__ = 0;\n // suppress unused var warning\n (void) current_statement__;\n using local_scalar_t__ = double;\n auto base_rng__ = stan::services::util::create_rng(random_seed__, 0);\n // suppress unused var warning\n (void) base_rng__;\n static constexpr const char* function__ =\n \"model_name_namespace::model_name\";\n // suppress unused var warning\n (void) function__;"... 11809 more characters, warnings: [ ] }
qjs >
Metadata
Metadata
Assignees
Labels
No labels
Activity
barracuda156 commentedon Jul 16, 2025
The first crash is fixed in the PR. This is the second:
Any idea what causes it?
barracuda156 commentedon Jul 16, 2025
I give up on this for now, it is problematic to bisect, since the issue is old, at least 0.8.0 exhibits the same behavior, but 0.7.0 does not build at all.
It is probably issue with structs sizes, several of which differ between
quickjsvsquickjs-ng, but there is no easy way to adjust those, it seems.FWIW, rebuilding with
-mone-byte-booldoes not solve the second crash, but pc address differs in that case.past-due commentedon Jul 16, 2025
@barracuda156 Any chance these two members of
struct JSTokenare impacting things?https://github.com/quickjs-ng/quickjs/blob/0191aea691e0ae37c35bc6737e73760506876148/quickjs.c#L20401-L20402
(Try changing them to
intor similar?)barracuda156 commentedon Jul 16, 2025
@past-due Thank you, I will try it once back to the hardware.
bnoordhuis commentedon Jul 16, 2025
When I run it locally with tip of master on x86_64, I don't get a crash; I get this (coming from stanc.js itself, it catches and converts the stack overflow RangeError):
We don't have ppc-specific code but maybe it's a stack limit thing? What happens when you pass
--stack-size <n>to qjs and play around with different values ofn?nis in kilobytes.barracuda156 commentedon Jul 18, 2025
@bnoordhuis Unfortunately, stack-size does not seem to have impact here.
@past-due Changing those two bools either to int or to uint8_t breaks the binary (it crashes on launch itself, like originally).
bnoordhuis commentedon Jul 18, 2025
What happens when you revert to commit 8bcc070? That's the commit preceding the stdbool change.
barracuda156 commentedon Jul 18, 2025
@bnoordhuis I will try.
Besides, looks like this second crash is unrelated to size of bool or bitness. I have just tried on ppc64 and got the same segfault (on ppc64 bool is standard).
bnoordhuis commentedon Mar 15, 2026
@barracuda156 is this still an issue? I'm guessing 'no' since it's been almost a year but I thought I'd ask anyway.
barracuda156 commentedon Mar 16, 2026
@bnoordhuis Sorry, I will test with the current version and update soon. It got postponed since with Rstan it seems not to cause troubles so far (R package builds are done regularly). Which in itself does not guarantee issue does not exist, of course.
barracuda156 commentedon Mar 19, 2026
Unfortunately, 0.12.1 still exhibits the issue:
barracuda156 commentedon Mar 19, 2026
Looks like on the same spot:
barracuda156 commentedon Mar 19, 2026
And
quickjs(non-ng) works from the latest commit, as earlier:bnoordhuis commentedon Mar 19, 2026
Thanks for testing. Does a debug build (one with source line info) point to any particular line in lre_exec_backtrack?
edit: and this is on ppc32, not ppc64, right?
bnoordhuis commentedon Mar 19, 2026
I've been somewhat able to reproduce the crash (or at least: a crash) but I don't have a ppc32-capable gdb at hand so it's tricky to debug.
I do observe that simply entering
> stanc(no function call, just the name) is enough to trigger a segfault, but only inside the REPL.qjs --include stanc.js -e 'print(stanc)'works okay.saghul commentedon Mar 19, 2026
@bnoordhuis I had Claude take a look earlier today, but I haven't had the time to double check its output yet.
It found the same with the REPL, and apparently there might be a bug in our byteswapping logic for BE systems.
I can make a WIP PR if you like?
bnoordhuis commentedon Mar 19, 2026
Hah, I managed to track it down to byteswapping right at the moment you posted that comment. :-)
Fix big-endian crashes when loading cross-compiled bytecode
saghul commentedon Mar 19, 2026
:-) In case it helps, it found 2 bugs: #1410
Remove big endian support
bnoordhuis commentedon Mar 19, 2026
@barracuda156 Saúl and I decided to simply rip out the byte-swap logic (#1412) so that means big endian will effectively be unsupported going forward.
I think it should still be possible to get a working build but you'll have to compile on the target machine itself (cross-compiling won't work) and you're going to need to regenerate all the pre-compiled bytecode with
make codegen.Remove big endian support (#1412)
barracuda156 commentedon Mar 20, 2026
@bnoordhuis Can we not just break BE support? There are people using BE systems, which are not limited to old macOS, of course. Power CPU are bi-endian, s390x are strictly big-endian, Amiga systems and upcoming (hopefully) PowerPC Linux notebook use big-endian ppc CPUs.
There is third-party software relying on quickjs, and in some cases specifically quickjs-ng (Rstan, elinks browser).
Please, reconsider this. Byte-swapping is a fixable issue.
saghul commentedon Mar 20, 2026
From a purely utilitarian prespective, if nobody thought this was important enough to fix since July last year, maybe it's simply not important enough to fix.
Native Big Endian builds should still work, is that something you can test?
Fixing is one thing, maintaining it ad eternum is another...
barracuda156 commentedon Mar 20, 2026
@saghul Well, it was not a pressing issue because it did not seem to affect my use-cases (which is largely Rstan;
elinksswitched to quickjs-ng just recently, I have not updated the port yet). I did try to fix it a few times, but failed. Last time with Claude, which found one of the issues you fixed in your [closed, sadly] PR, but that did not fix this specific test case.I only do native builds, it is easier than cross-compiling. I do not care about cross-compilation support personally, but native builds are important. Rstan does not use external quickjs-ng (and some other ports which embed it), so broken big-endian will be a painful situation.
You know I care about this at least from the fact that I found and fixed the first bug with bool.
saghul commentedon Mar 20, 2026
If you do native builds then make codegen && make should produce a working binary.
barracuda156 commentedon Mar 20, 2026
Ok, I will look into that. What to do about third-party apps that bundle quickjs-ng or otherwise incorporate its code?
P. S. If I test your PR and it is confirmed to fix the bug here, can we consider restoring BE support plus merging your PR? It goes without saying that it does not imply any commitment to keep BE support no matter what; there may be a reason to drop it at some point, but if it is not that point yet (as a matter of fact, even with this bug unfixed, it was working just fine on BE in most cases), there is no real need to drop it right now at least. TBH I also feel bad to be a trigger for this undesirable change :(