Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit f7be3d9

Browse files
committed
Add stack cleanup helper
1 parent 0eb7f7b commit f7be3d9

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

wasm/generateInterface.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ function checkOverflowStackItem256 (spOffset) {
222222
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3}))))`
223223
}
224224

225+
// assumes the stack contains 128 bits of value and clears the rest
226+
function cleanupStackItem128 (spOffset) {
227+
return `
228+
;; zero out mem
229+
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})) (i64.const 0))
230+
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})) (i64.const 0))`
231+
}
232+
233+
// assumes the stack contains 64 bits of value and clears the rest
234+
function cleanupStackItem64 (spOffset) {
235+
return `
236+
;; zero out mem
237+
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})) (i64.const 0))
238+
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})) (i64.const 0))
239+
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8})) (i64.const 0))`
240+
}
241+
225242
function generateManifest (interfaceManifest, opts) {
226243
const useAsyncAPI = opts.useAsyncAPI
227244
const json = {}
@@ -356,10 +373,8 @@ function generateManifest (interfaceManifest, opts) {
356373
call += '(get_local $callback)'
357374
}
358375

359-
call += `)
360-
;; zero out mem
361-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})) (i64.const 0))
362-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})) (i64.const 0))`
376+
call += ')'
377+
call += cleanupStackItem128(spOffset)
363378
} else if (output === 'address') {
364379
call =
365380
`${call} (i32.add (get_global $sp) (i32.const ${spOffset * 32}))`
@@ -368,6 +383,7 @@ function generateManifest (interfaceManifest, opts) {
368383
call += '(get_local $callback)'
369384
}
370385

386+
// FIXME: implement support in cleanupStackItem160
371387
call += `)
372388
;; zero out mem
373389
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})) (i64.const 0))
@@ -405,22 +421,15 @@ function generateManifest (interfaceManifest, opts) {
405421
${call})))`
406422
}
407423

408-
call += `
409-
;; zero out mem
410-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})) (i64.const 0))
411-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})) (i64.const 0))
412-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8})) (i64.const 0))`
424+
call += cleanupStackItem64(spOffset)
413425
} else if (output === 'i64') {
414426
if (useAsyncAPI && op.async) {
415427
call += '(get_local $callback)'
416428
}
417429
call =
418-
`(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32})) ${call}))
430+
`(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32})) ${call}))`
419431

420-
;; zero out mem
421-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})) (i64.const 0))
422-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})) (i64.const 0))
423-
(i64.store (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8})) (i64.const 0))`
432+
call += cleanupStackItem64(spOffset)
424433
} else if (!output) {
425434
if (useAsyncAPI && op.async) {
426435
call += '(get_local $callback)'

0 commit comments

Comments
 (0)