🐛 Restore the WASM stack pointer after a failed call (#12978)

A failed render-wasm call traps without popping its shadow-stack frames.
Each failure leaked stack until it overwrote static data, and from then on
every call trapped as wasm-critical. h/call now saves the stack pointer
before the call and restores it on failure.

Closes #12977
Related to #12906

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Belén Albeza 2026-11-13 09:39:43 +01:00 committed by GitHub
parent 69c33c4877
commit a7b6ef0d41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 90 additions and 18 deletions

View File

@ -5,34 +5,42 @@
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns app.common.render-wasm.helpers
#?(:cljs (:require-macros [app.common.render-wasm.helpers]))
(:require [app.common.data :as d]))
#?(:cljs (:require-macros [app.common.render-wasm.helpers])))
(def error-code
"WASM error code constants (must match render-wasm/src/error.rs and mem.rs)."
{0x01 :non-blocking
0x02 :panic})
#?(:cljs
(defn on-call-error
"Restores the stack pointer after a failed WASM call and throws the
classified error."
[module fn-name stack cause]
(when (some? stack)
((unchecked-get module "stackRestore") stack))
(let [read-code-fn (unchecked-get module "_read_error_code")
code-num (when read-code-fn (read-code-fn))
code (get error-code code-num :wasm-critical)
hint (str "WASM Error (" (name code) ")")]
(throw (ex-info hint {:type :wasm-error :code code :hint hint :fn fn-name} cause)))))
(defmacro call
"A helper for calling a wasm function.
Catches any exception thrown by the WASM function, reads the error code from
WASM when available, and routes it based on the error type:
- :wasm-non-blocking: call app.main.errors/on-error (eventually, shows a toast and logs the error)
- :wasm-critical or unknown: throws an exception to be handled by the global error handler (eventually, shows the internal error page)"
- :wasm-critical or unknown: throws an exception to be handled by the global error handler (eventually, shows the internal error page)
Restores the stack pointer on failure, since a trap leaves its shadow-stack frames behind."
[module name & params]
(let [fn-sym (with-meta (gensym "fn-") {:tag 'function})
cause-sym (gensym "cause")]
`(let [~fn-sym (cljs.core/unchecked-get ~module ~name)]
(let [fn-sym (with-meta (gensym "fn-") {:tag 'function})
module-sym (gensym "module")
stack-sym (gensym "stack")]
`(let [~module-sym ~module
~fn-sym (cljs.core/unchecked-get ~module-sym ~name)
save-fn# (cljs.core/unchecked-get ~module-sym "stackSave")
~stack-sym (when save-fn# (save-fn#))]
(try
(~fn-sym ~@params)
(catch :default ~cause-sym
(let [read-code-fn# (cljs.core/unchecked-get ~module "_read_error_code")
code-num# (when read-code-fn# (read-code-fn#))
code# (get error-code code-num# :wasm-critical)
hint# (str "WASM Error (" (d/name code#) ")")
context# {:type :wasm-error
:code code#
:hint hint#
:fn ~name}
cause# (ex-info hint# context# ~cause-sym)]
(throw cause#)))))))
(catch :default cause#
(on-call-error ~module-sym ~name ~stack-sym cause#))))))

View File

@ -0,0 +1,62 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.render-wasm.call-test
"Unit tests for the `app.common.render-wasm.helpers/call` macro."
(:require
[app.common.render-wasm.helpers :as h]
[cljs.test :as t :include-macros true]))
(defn- fake-module
"Module whose `_fail` export throws like a WASM trap after moving the
shadow stack pointer down 64 bytes, leaking the frame as a trap does."
[error-code]
(let [sp (atom 1000)
restored (atom [])]
{:sp sp
:restored restored
:module #js {"stackSave" (fn [] @sp)
"stackRestore" (fn [v]
(swap! restored conj v)
(reset! sp v))
"_read_error_code" (fn [] error-code)
"_ok" (fn [x] (* 2 x))
"_fail" (fn []
(swap! sp - 64)
(throw (js/Error. "unreachable")))}}))
(t/deftest failed-call-restores-stack-pointer
(let [{:keys [module sp restored]} (fake-module 0x02)]
(dotimes [_ 3]
(t/is (thrown? js/Error (h/call module "_fail"))))
(t/is (= 1000 @sp))
(t/is (= [1000 1000 1000] @restored))))
(t/deftest failed-call-keeps-error-classification
(let [{:keys [module]} (fake-module 0x01)
data (try
(h/call module "_fail")
nil
(catch :default cause
(ex-data cause)))]
(t/is (= :wasm-error (:type data)))
(t/is (= :non-blocking (:code data)))
(t/is (= "_fail" (:fn data)))))
(t/deftest successful-call-does-not-restore
(let [{:keys [module restored]} (fake-module 0)]
(t/is (= 42 (h/call module "_ok" 21)))
(t/is (empty? @restored))))
(t/deftest module-without-stack-methods-still-reports-errors
(let [module #js {"_read_error_code" (fn [] 0x02)
"_fail" (fn [] (throw (js/Error. "unreachable")))}
data (try
(h/call module "_fail")
nil
(catch :default cause
(ex-data cause)))]
(t/is (= :panic (:code data)))))

View File

@ -111,6 +111,7 @@
[frontend-tests.plugins.utils-test]
[frontend-tests.plugins.value-objects-test]
[frontend-tests.render-dimensions-test]
[frontend-tests.render-wasm.call-test]
[frontend-tests.render-wasm.process-objects-test]
[frontend-tests.render-wasm.serialization-test]
[frontend-tests.render-wasm.shape-proxy-test]
@ -320,6 +321,7 @@
'frontend-tests.plugins.user-test
'frontend-tests.plugins.utils-test
'frontend-tests.plugins.value-objects-test
'frontend-tests.render-wasm.call-test
'frontend-tests.render-wasm.process-objects-test
'frontend-tests.render-wasm.serialization-test
'frontend-tests.render-wasm.shape-proxy-test

View File

@ -67,7 +67,7 @@ export EMCC_CFLAGS="--no-entry \
-sERROR_ON_UNDEFINED_SYMBOLS=0 \
-sMAX_WEBGL_VERSION=2 \
-sEXPORT_NAME=createRustSkiaModule \
-sEXPORTED_RUNTIME_METHODS=GL,UTF8ToString,stringToUTF8,HEAPU8,HEAP32,HEAPU32,HEAPF32 \
-sEXPORTED_RUNTIME_METHODS=GL,UTF8ToString,stringToUTF8,HEAPU8,HEAP32,HEAPU32,HEAPF32,stackSave,stackRestore \
-sENVIRONMENT=web,worker,node \
-sMODULARIZE=1 \
-sFILESYSTEM=0 \