Repository navigation
Fix plugin calls crashing on iOS: depend on WasmKit with wasmkit_tc_start in C - #25
Merged
Merged
Conversation
…ls into plugins don't crash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every call into a plugin crashes in optimized builds on iOS. This moves the app to a WasmKit fork commit that fixes it.
The crash
WasmKit runs Wasm with direct-threaded handlers that use the
swiftasynccalling convention. Those handlers overwrite its context register (x22on arm64) and don't preserve it. The handlers are started bywasmkit_tc_start, astatic inlineC function in_CWasmKit.h. Once that function was inlined intoExecution.runDirectThreaded, the call was treated as if it keptx22. After the handlers returned, the code read the execution's trap through the stale register. It took a value off the Wasm stack (ani32result such as42) as the error and crashed retaining it inswift_errorRetain.That path runs only when memory bounds are checked in software, which is the default on iOS. macOS uses mprotect guards and goes through
wasmkit_trap_guard_run, a real C function that saves the registers. So the crash didn't show on the Mac.The fix
WasmKit fork, branch
fix/direct-threaded-start(80990b7):wasmkit_tc_startis declared in the header and defined in_CWasmKit.c. As an ordinary C function it saves and restores its callee-saved registers around the handlers.This PR only bumps the pin in
Package.swiftandPackage.resolved.Testing
A minimal module with
(func (result i32) i32.const 42)and(func unreachable), run in a release build:memoryBoundsChecking: .software: crashes before the fix; returns42and reportsTrap: unreachableafter it.