Skip to content

Commit 684ecd4

Browse files
committed
Attempt to disable JIT on Android with JSC_useJIT=false to avoid crash in JSValueUnprotect
1 parent 0428853 commit 684ecd4

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Sources/SkipScript/JSContext.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@ let JavaScriptCore: JavaScriptCoreLibrary = JavaScriptCoreLibrary.instance
1616

1717
public typealias ExceptionPtr = UnsafeMutablePointer<JSValueRef?>
1818

19+
/// JIT needs to be disabled on Android to avoid random crashes in `JSValueUnprotect`
20+
/// See: https://github.com/WebKit/WebKit/blob/main/Tools/Scripts/run-jsc-benchmarks
21+
private func disableJIT() {
22+
// JSC_useWasmIPInt=0 JSC_useWasmLLInt=1 JSC_useWebAssembly=1 JSC_useJIT=0
23+
setenv("JSC_useJIT", "false", 1)
24+
}
25+
1926
/// A context for evaluating JavaScipt.
2027
public class JSContext {
2128
public let context: JSContextRef
2229
public private(set) var exception: JSValue? = nil
2330
private var tryingRecursionGuard = false
2431

2532
public init(jsGlobalContextRef context: JSContextRef) {
33+
disableJIT()
2634
self.context = context
2735
JavaScriptCore.JSGlobalContextRetain(context)
2836
}
2937

3038
public init() {
39+
disableJIT()
3140
self.context = JavaScriptCore.JSGlobalContextCreate(nil)
3241
}
3342

@@ -544,8 +553,24 @@ public class JSValue {
544553
}
545554

546555
deinit {
547-
// this has been seen to raise an exception on the Android emulator:
556+
// this has been seen to raise an exception on the Android emulator when not setting `JSC_useJIT`:
548557
// java.util.concurrent.TimeoutException: skip.script.JSValue.finalize() timed out after 10 seconds
558+
// it has also led to crashes:
559+
/*
560+
02-06 14:33:12.996 2016 2016 F DEBUG : #00 pc 000000000028ff90 /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk!libjsc.so (offset 0x2aa8000) (JSValueUnprotect+16) (BuildId: ca8f87b98242c913dfdaa146cce2a24b070804a2)
561+
02-06 14:33:12.996 2016 2016 F DEBUG : #01 pc 0000000000012051 /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk (offset 0x2a88000) (BuildId: 93b2f9545d27a84372ca7fba3b2b473c2f9c6edd)
562+
02-06 14:33:12.996 2016 2016 F DEBUG : #02 pc 0000000000011032 /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk (offset 0x2a88000) (BuildId: 93b2f9545d27a84372ca7fba3b2b473c2f9c6edd)
563+
02-06 14:33:12.996 2016 2016 F DEBUG : #03 pc 000000000001174b /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk (offset 0x2a88000) (ffi_call+219) (BuildId: 93b2f9545d27a84372ca7fba3b2b473c2f9c6edd)
564+
02-06 14:33:12.996 2016 2016 F DEBUG : #04 pc 0000000000007264 /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk (offset 0x2a88000) (BuildId: 93b2f9545d27a84372ca7fba3b2b473c2f9c6edd)
565+
02-06 14:33:12.996 2016 2016 F DEBUG : #05 pc 0000000000011a17 /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk (offset 0x2a88000) (BuildId: 93b2f9545d27a84372ca7fba3b2b473c2f9c6edd)
566+
02-06 14:33:12.997 2016 2016 F DEBUG : #06 pc 00000000000121e7 /data/app/~~HElSLsP99NS9XFzsCODJmA==/skip.script.test-v4AHg-MWsLEjjHmmm5YC7w==/base.apk (offset 0x2a88000) (BuildId: 93b2f9545d27a84372ca7fba3b2b473c2f9c6edd)
567+
02-06 14:33:12.997 2016 2016 F DEBUG : #07 pc 000000000200d1de /memfd:jit-cache (deleted) (offset 0x2000000) (art_jni_trampoline+222)
568+
02-06 14:33:12.997 2016 2016 F DEBUG : #08 pc 000000000202ee59 /memfd:jit-cache (deleted) (offset 0x2000000) (skip.script.JSValue.finalize+121)
569+
02-06 14:33:12.997 2016 2016 F DEBUG : #09 pc 000000000202f070 /memfd:jit-cache (deleted) (offset 0x2000000) (java.lang.Daemons$FinalizerDaemon.doFinalize+112)
570+
02-06 14:33:12.997 2016 2016 F DEBUG : #10 pc 000000000202b42f /memfd:jit-cache (deleted) (offset 0x2000000) (java.lang.Daemons$FinalizerDaemon.runInternal+511)
571+
02-06 14:33:12.997 2016 2016 F DEBUG : #11 pc 0000000000185e6b /apex/com.android.art/lib64/libart.so (art_quick_osr_stub+27) (BuildId: 1dfb27162fe62a7ac7a10ea361233369)
572+
02-06 14:33:12.997 2016 2016 F DEBUG : #12 pc 00000000003d27ba /apex/com.android.art/lib64/libart.so (art::jit::Jit::MaybeDoOnStackReplacement(art::Thread*, art::ArtMethod*, unsigned int, int, art::JValue*)+410) (BuildId: 1dfb27162fe62a7ac7a10ea361233369)
573+
*/
549574
JavaScriptCore.JSValueUnprotect(context.context, value)
550575
}
551576
}

0 commit comments

Comments
 (0)