Skip to content

Commit cf13063

Browse files
committed
fix(mystrixsim): harden firmware package loading
1 parent 4a1fedf commit cf13063

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

Devices/MystrixSim/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if(EMSCRIPTEN)
7676
target_compile_options(MatrixOSHost PRIVATE -pthread)
7777

7878
target_link_options(MatrixOSHost PRIVATE
79-
"SHELL:-sEXPORTED_RUNTIME_METHODS=HEAPU8,UTF8ToString"
79+
"SHELL:-sEXPORTED_RUNTIME_METHODS=HEAPU8,UTF8ToString,PThread"
8080
"SHELL:-sEXPORTED_FUNCTIONS=[_main,_MatrixOS_Wasm_GetFrameBuffer,_MatrixOS_Wasm_GetFrameBufferByteLength,_MatrixOS_Wasm_GetWidth,_MatrixOS_Wasm_GetHeight,_MatrixOS_Wasm_KeyEvent,_MatrixOS_Wasm_FnEvent,_MatrixOS_Wasm_KeypadTick,_MatrixOS_Wasm_GetVersionString,_MatrixOS_Wasm_GetBuildIdentityString,_MatrixOS_Wasm_GetBuildTimeString,_MatrixOS_Wasm_GetBuildMetadataJson,_MatrixOS_Wasm_Reboot,_MatrixOS_Wasm_Bootloader,_MatrixOS_Wasm_GetRotation,_MatrixOS_Wasm_GetUptimeMs,_MatrixOS_Wasm_GetKeypadState,_MatrixOS_Wasm_GetKeypadStateLength,_MatrixOS_Wasm_GetFnState,_MatrixOS_Wasm_MidiInject,_MatrixOS_Wasm_RawHidInject,_MatrixOS_Wasm_SerialWrite,_MatrixOS_Wasm_NvsGetCount,_MatrixOS_Wasm_NvsGetHashes,_MatrixOS_Wasm_NvsGetSize,_MatrixOS_Wasm_NvsGetData,_MatrixOS_Wasm_NvsWrite,_MatrixOS_Wasm_NvsDelete,_MatrixOS_Wasm_NvsClear,_MatrixOS_Wasm_NvsExport,_MatrixOS_Wasm_NvsExportSize,_MatrixOS_Wasm_NvsImport,_MatrixOS_Wasm_SetUsbAvailable,_MatrixOS_Wasm_GetUsbAvailable,_MatrixOS_Wasm_TouchBarEvent]"
8181
"SHELL:-sUSE_PTHREADS=1"
8282
"SHELL:-sPTHREAD_POOL_SIZE=16"

Devices/MystrixSim/WebUI/src/stores/wasm.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ function revokeRetiredRuntimeAssetBlobUrls() {
8383
_retiredRuntimeAssetBlobUrls = []
8484
}
8585

86+
async function validateRuntimeWasmBytes(wasmBytes, label) {
87+
if (!WebAssembly.validate(wasmBytes)) {
88+
throw new Error(`Package ${label} contains an invalid MatrixOSHost.wasm.`)
89+
}
90+
91+
try {
92+
await WebAssembly.compile(wasmBytes)
93+
} catch (error) {
94+
const message = error instanceof Error ? error.message : String(error)
95+
throw new Error(`Package ${label} failed WebAssembly validation: ${message}`)
96+
}
97+
}
98+
99+
function getOptionalModuleValue(mod, key) {
100+
if (!mod) return null
101+
102+
const descriptor = Object.getOwnPropertyDescriptor(mod, key)
103+
if (!descriptor) return null
104+
if ('value' in descriptor) return descriptor.value ?? null
105+
106+
// Old Emscripten bundles install throwing getters for unexported runtime
107+
// methods. Treat accessor-only symbols as unavailable instead of touching them.
108+
return null
109+
}
110+
86111
export async function loadRuntimeAssetPair({ jsText, wasmBytes, label = 'MatrixOS.msfw' }) {
87112
const normalizedJsText = typeof jsText === 'string' ? jsText : ''
88113
const normalizedWasmBytes = wasmBytes instanceof Uint8Array ? wasmBytes : new Uint8Array(wasmBytes || [])
@@ -94,6 +119,8 @@ export async function loadRuntimeAssetPair({ jsText, wasmBytes, label = 'MatrixO
94119
throw new Error(`Package ${label} is missing MatrixOSHost.wasm`)
95120
}
96121

122+
await validateRuntimeWasmBytes(normalizedWasmBytes, label)
123+
97124
const nextSource = {
98125
kind: 'package',
99126
label,
@@ -193,12 +220,13 @@ function terminateWasmRuntime() {
193220
if (!mod) return
194221

195222
try {
196-
if (mod.PThread) {
197-
if (typeof mod.PThread.terminateAllThreads === 'function') {
198-
mod.PThread.terminateAllThreads()
223+
const pthreadRuntime = getOptionalModuleValue(mod, 'PThread')
224+
if (pthreadRuntime) {
225+
if (typeof pthreadRuntime.terminateAllThreads === 'function') {
226+
pthreadRuntime.terminateAllThreads()
199227
} else {
200-
;(mod.PThread.runningWorkers || []).forEach(w => { try { w.terminate() } catch {} })
201-
;(mod.PThread.unusedWorkers || []).forEach(w => { try { w.terminate() } catch {} })
228+
;(pthreadRuntime.runningWorkers || []).forEach(w => { try { w.terminate() } catch {} })
229+
;(pthreadRuntime.unusedWorkers || []).forEach(w => { try { w.terminate() } catch {} })
202230
}
203231
}
204232
} catch (e) {

0 commit comments

Comments
 (0)