QuickJSR 1.8.0 has broken Rstan
@andrjohns Hi, Andrew!
Upgrading to QuickJSR 1.8.0 has broken Rstan for me. Examples:
---> Building R-YPPE
xinstall: mkdir /opt/local/var/macports/build/_opt_local_var_macports_sources_macos-powerpc.org_macos-powerpc_R_ports_R_R-YPPE/R-YPPE/work/build
Executing: cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_macos-powerpc.org_macos-powerpc_R_ports_R_R-YPPE/R-YPPE/work/YPPE" && /opt/local/bin/R CMD INSTALL . --library=/opt/local/var/macports/build/_opt_local_var_macports_sources_macos-powerpc.org_macos-powerpc_R_ports_R_R-YPPE/R-YPPE/work/build --install-tests
* installing *source* package ‘YPPE’ ...
** this is package ‘YPPE’ version ‘1.0.1’
** package ‘YPPE’ successfully unpacked and MD5 sums checked
** using staged installation
*** caught segfault ***
address 0xc1ab005, cause 'memory not mapped'
Traceback:
1: qjs_source(ContextList$runtime_context_ptr, input = normalizePath(file), is_file = TRUE)
2: stanc_ctx$source(stanc_js)
3: fun(libname, pkgname)
4: doTryCatch(return(expr), name, parentenv, handler)
5: tryCatchOne(expr, names, parentenv, handlers[[1L]])
6: tryCatchList(expr, classes, parentenv, handlers)
7: tryCatch(fun(libname, pkgname), error = identity)
8: runHook(".onLoad", env, package.lib, package)
9: loadNamespace(x)
10: FUN(X[[i]], ...)
11: lapply(X = X, FUN = FUN, ...)
12: sapply(stan_files, .make_cc, pkgdir = pkgdir)
13: rstantools::rstan_config()
An irrecoverable exception occurred. R is aborting now ...
./configure: line 4: 69501 Segmentation fault "${R_HOME}/bin/Rscript" -e "rstantools::rstan_config()"
ERROR: configuration failed for package ‘YPPE’
* removing ‘/opt/local/var/macports/build/_opt_local_var_macports_sources_macos-powerpc.org_macos-powerpc_R_ports_R_R-YPPE/R-YPPE/work/build/YPPE’
Command failed: cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_macos-powerpc.org_macos-powerpc_R_ports_R_R-YPPE/R-YPPE/work/YPPE" && /opt/local/bin/R CMD INSTALL . --library=/opt/local/var/macports/build/_opt_local_var_macports_sources_macos-powerpc.org_macos-powerpc_R_ports_R_R-YPPE/R-YPPE/work/build --install-tests
Exit code: 1
Error: Failed to build R-YPPE: command execution failed
---> Building R-bayesforecast
xinstall: mkdir /opt/local/var/macports/build/_opt_local_ppcports_R_R-bayesforecast/R-bayesforecast/work/build
Executing: cd "/opt/local/var/macports/build/_opt_local_ppcports_R_R-bayesforecast/R-bayesforecast/work/bayesforecast" && /opt/local/bin/R CMD INSTALL . --library=/opt/local/var/macports/build/_opt_local_ppcports_R_R-bayesforecast/R-bayesforecast/work/build --install-tests
* installing *source* package ‘bayesforecast’ ...
** this is package ‘bayesforecast’ version ‘1.0.5’
** package ‘bayesforecast’ successfully unpacked and MD5 sums checked
** using staged installation
*** caught illegal operation ***
address 0x301f7a8, cause 'illegal opcode'
Traceback:
1: qjs_source(ContextList$runtime_context_ptr, input = normalizePath(file), is_file = TRUE)
2: stanc_ctx$source(stanc_js)
3: fun(libname, pkgname)
4: doTryCatch(return(expr), name, parentenv, handler)
5: tryCatchOne(expr, names, parentenv, handlers[[1L]])
6: tryCatchList(expr, classes, parentenv, handlers)
7: tryCatch(fun(libname, pkgname), error = identity)
8: runHook(".onLoad", env, package.lib, package)
9: loadNamespace(x)
10: FUN(X[[i]], ...)
11: lapply(X = X, FUN = FUN, ...)
12: sapply(stan_files, .make_cc, pkgdir = pkgdir)
13: rstantools::rstan_config()
An irrecoverable exception occurred. R is aborting now ...
./configure: line 5: 68298 Illegal instruction "${R_HOME}/bin/Rscript" -e "rstantools::rstan_config()"
ERROR: configuration failed for package ‘bayesforecast’
* removing ‘/opt/local/var/macports/build/_opt_local_ppcports_R_R-bayesforecast/R-bayesforecast/work/build/bayesforecast’
Command failed: cd "/opt/local/var/macports/build/_opt_local_ppcports_R_R-bayesforecast/R-bayesforecast/work/bayesforecast" && /opt/local/bin/R CMD INSTALL . --library=/opt/local/var/macports/build/_opt_local_ppcports_R_R-bayesforecast/R-bayesforecast/work/build --install-tests
Exit code: 1
Error: Failed to build R-bayesforecast: command execution failed
Reverting to 1.7.x fixes the issue, everything else remaining the same.
I see that 1.8.0 switched to quickjs-ng from quickjs. Does this change require rebuilding Rstan? If yes, then perhaps it should be updated in CRAN. If no, then how could I debug the cause of the breakage?
Update. I cannot rebuild Rstan against the new QuickJSR, since it also segfaults when trying to load itself from temporary location.
Let me actually rebuild both QuickJSR 1.7.0 and Rstan to make sure that it is not something else introducing the breakage.
Update. Okay, looks like it is indeed QuickJSR 1.8.0. At least I can successfully rebuild QuickJSR 1.7.0 and then rebuild Rstan against it, no segfaults.
I can build both YPPE and bayesforecast from source with rstan and QuickJSR 1.8.0. Those packages have also been re-building on CRAN's CI with no issues.
I'm guessing this is happening on a MacOS PPC installation? If so, it may be up to you identify what changed in quickjs-ng between 0.10.0 and 0.9.0 that could have broken things for PPC
@bnoordhuis @saghul Could you suggest what may be causing this? Tracebacks seem to point to qjs_source(ContextList$runtime_context_ptr, input = normalizePath(file).
That function call is just QuickJSRs entrypoint, it doesn't tell us anything about what in the engine is segfaulting.
Your best bet would be to try and create a minimal reproducible example using only quickjs-ng, otherwise you're the only one that can debug this
@andrjohns By the way, it is possible to allow an external quickjs? That would have probably made things easier. Optionally, of course. Like, say, it is done for TBB.
I'd rather not unless it's absolutely necessary.
Do you have a modified version of current quickjs that works on ppc Mac without crashing, or are you just wanting to use an older quickjs with current QuickJSR?
Do you have a modified version of current quickjs that works on ppc Mac without crashing
I have just now compiled 0.10.1, and it looks fine. (I do not guarantee that all tests will pass, but it is certainly not just broken.)
Zero patches applied. Only linking to legacysupport and libatomic (via flags, not hacking).
Yes but your issue was a crash when evaluating JS code, not compiling.
Try using stanc:
wget https://github.com/stan-dev/stanc3/releases/download/nightly/stanc.js
./qjs --include ./stanc.js
qjs > stanc("model_name","data{}")
@andrjohns Thank you for suggestion, it was helpful.
Indeed, QuickJS is broken:
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 ()
Should not be horribly difficult to bisect, I hope. I will try.
quickjs-ng 0.9.0 does fine with ./qjs --include ./stanc.js but segfaults on stanc("model_name","data{}"). Does not look good :(
quickjs (2025.04.05) works fine though. Looks like regression is specific to -ng.
@andrjohns I have fixed the immediate issue, there is no crash on launch now, but stanc("model_name","data{}") does not work, it segfaults on that for a different reason. This issue is not new, I have built versions back to 0.8.0, and they all fail the same way. (Upstream, non-ng quickjs works normally, including the latest commit from the master.)
When did QuickJSR switch to -ng and at which version of the latter? It appears that 1.7.x works fine – I ran tests for some dependent of Rstan (which involved compiling and evaluating models), and they passed. However, given that the issue in -ng might have existed for a long time, I am a bit concerned now.