diff --git a/common/src/app/common/render_wasm/helpers.cljc b/common/src/app/common/render_wasm/helpers.cljc index d96cc9f990..7a73d6f94e 100644 --- a/common/src/app/common/render_wasm/helpers.cljc +++ b/common/src/app/common/render_wasm/helpers.cljc @@ -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#)))))) diff --git a/frontend/test/frontend_tests/render_wasm/call_test.cljs b/frontend/test/frontend_tests/render_wasm/call_test.cljs new file mode 100644 index 0000000000..17d4331519 --- /dev/null +++ b/frontend/test/frontend_tests/render_wasm/call_test.cljs @@ -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))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 6abd1aebfc..e7062d785a 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -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 diff --git a/render-wasm/_build_env b/render-wasm/_build_env index 5c804469d0..83759da674 100644 --- a/render-wasm/_build_env +++ b/render-wasm/_build_env @@ -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 \