Skip to content

Commit eabdfa6

Browse files
authored
wasm2c: Add experimental sandboxing modes (emscripten-core#13719)
1 parent 964bcf2 commit eabdfa6

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/settings.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,14 @@ var PRINTF_LONG_DOUBLE = 0;
18641864
// [link]
18651865
var WASM2C = 0;
18661866

1867+
// Experimental sandboxing mode, see
1868+
// https://kripken.github.io/blog/wasm/2020/07/27/wasmboxc.html
1869+
//
1870+
// * full: Normal full wasm2c sandboxing. This uses a signal handler if it can.
1871+
// * mask: Masks loads and stores.
1872+
// * none: No sandboxing at all.
1873+
var WASM2C_SANDBOXING = 'full';
1874+
18671875
// Setting this affects the path emitted in the wasm that refers to the DWARF
18681876
// file, in -gseparate-dwarf mode. This allows the debugging file to be hosted
18691877
// in a custom location.

tests/test_core.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6307,6 +6307,20 @@ def check(out, err):
63076307
self.do_runf(test_file('core', 'test_autodebug.c'),
63086308
'success', output_nicerizer=check)
63096309

6310+
@parameterized({
6311+
'full': ('full',),
6312+
'mask': ('mask',),
6313+
'none': ('none',),
6314+
})
6315+
def test_wasm2c_sandboxing(self, mode):
6316+
if not can_do_standalone(self):
6317+
return self.skipTest('standalone mode not supported')
6318+
self.set_setting('STANDALONE_WASM')
6319+
self.set_setting('WASM2C')
6320+
self.set_setting('WASM2C_SANDBOXING', mode)
6321+
self.wasm_engines = []
6322+
self.do_core_test('test_hello_world.c')
6323+
63106324
### Integration tests
63116325

63126326
@sync

tools/wasm2c.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ def name(i):
186186
})
187187

188188
total += '\n'.join(invokes)
189+
190+
# adjust sandboxing
191+
TRAP_OOB = 'TRAP(OOB)'
192+
assert total.count(TRAP_OOB) == 2
193+
if Settings.WASM2C_SANDBOXING == 'full':
194+
pass # keep it
195+
elif Settings.WASM2C_SANDBOXING == 'none':
196+
total = total.replace(TRAP_OOB, '{}')
197+
elif Settings.WASM2C_SANDBOXING == 'mask':
198+
assert not Settings.ALLOW_MEMORY_GROWTH
199+
assert (Settings.INITIAL_MEMORY & (Settings.INITIAL_MEMORY - 1)) == 0, 'poewr of 2'
200+
total = total.replace(TRAP_OOB, '{}')
201+
MEM_ACCESS = '[addr]'
202+
assert total.count(MEM_ACCESS) == 3, '2 from wasm2c, 1 from runtime'
203+
total = total.replace(MEM_ACCESS, '[addr & %d]' % (Settings.INITIAL_MEMORY - 1))
204+
else:
205+
exit_with_error('bad sandboxing')
206+
189207
# write out the final file
190208
with open(c_file, 'w') as out:
191209
out.write(total)

0 commit comments

Comments
 (0)