From 558a7f645c8452ee706e1e51432a99d823671511 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 16 May 2023 13:04:13 -0700 Subject: [PATCH 1/2] undo --- ChangeLog.md | 2 + emcc.py | 4 - src/settings.js | 19 --- test/common.py | 9 - test/test_benchmark.py | 42 ----- test/test_core.py | 42 +---- tools/wasm2c.py | 238 -------------------------- tools/wasm2c/autodebug.c | 87 ---------- tools/wasm2c/base.c | 154 ----------------- tools/wasm2c/main.c | 42 ----- tools/wasm2c/os.c | 321 ------------------------------------ tools/wasm2c/os_sandboxed.c | 22 --- tools/wasm2c/reactor.c | 14 -- 13 files changed, 7 insertions(+), 989 deletions(-) delete mode 100644 tools/wasm2c.py delete mode 100644 tools/wasm2c/autodebug.c delete mode 100644 tools/wasm2c/base.c delete mode 100644 tools/wasm2c/main.c delete mode 100644 tools/wasm2c/os.c delete mode 100644 tools/wasm2c/os_sandboxed.c delete mode 100644 tools/wasm2c/reactor.c diff --git a/ChangeLog.md b/ChangeLog.md index ccb562f546241..0f75f60afbd22 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -23,6 +23,8 @@ See docs/process.md for more on how version tagging works. - The JS `err()` function will now bind to `console.error` by default rather than `console.warning`. For debugging/tracing/logging we recommend the `dbg()` function instead. (#19326) +- The `WASM2C` options has been removed. All known users are using upstream wabt + these days anyhow. 3.1.38 - 05/10/23 ----------------- diff --git a/emcc.py b/emcc.py index da7eda09fbe9c..7d18fc470d6d0 100755 --- a/emcc.py +++ b/emcc.py @@ -51,7 +51,6 @@ import tools.line_endings from tools import feature_matrix from tools import js_manipulation -from tools import wasm2c from tools import webassembly from tools import config from tools import cache @@ -3810,9 +3809,6 @@ def preprocess_wasm2js_script(): if settings.DEBUG_LEVEL >= 3 and settings.SEPARATE_DWARF and os.path.exists(wasm_target): building.emit_debug_on_side(wasm_target) - if settings.WASM2C: - wasm2c.do_wasm2c(wasm_target) - # we have finished emitting the wasm, and so intermediate debug info will # definitely no longer be used tracking it. if debug_info: diff --git a/src/settings.js b/src/settings.js index f93140d04c4e3..cd78d210233b5 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1933,25 +1933,6 @@ var DEFAULT_TO_CXX = true; // [link] var PRINTF_LONG_DOUBLE = false; -// Run wabt's wasm2c tool on the final wasm, and combine that with a C runtime, -// resulting in a .c file that you can compile with a C compiler to get a -// native executable that works the same as the normal js+wasm. This will also -// emit the wasm2c .h file. The output filenames will be X.wasm.c, X.wasm.h -// if your output is X.js or X.wasm (note the added .wasm. we make sure to emit, -// which avoids trampling a C file). -// [link] -// [experimental] -var WASM2C = false; - -// Experimental sandboxing mode, see -// https://kripken.github.io/blog/wasm/2020/07/27/wasmboxc.html -// -// * full: Normal full wasm2c sandboxing. This uses a signal handler if it can. -// * mask: Masks loads and stores. -// * none: No sandboxing at all. -// [experimental] -var WASM2C_SANDBOXING = 'full'; - // Setting this affects the path emitted in the wasm that refers to the DWARF // file, in -gseparate-dwarf mode. This allows the debugging file to be hosted // in a custom location. diff --git a/test/common.py b/test/common.py index 67313a03499be..b23c88c4e7edc 100644 --- a/test/common.py +++ b/test/common.py @@ -1383,15 +1383,6 @@ def _build_and_run(self, filename, expected_output, args=None, output_nicerizer= if not self.wasm_engines: logger.warning('no wasm engine was found to run the standalone part of this test') engines += self.wasm_engines - if self.get_setting('WASM2C') and not EMTEST_LACKS_NATIVE_CLANG: - # compile the c file to a native executable. - c = shared.replace_suffix(js_file, '.wasm.c') - executable = shared.replace_suffix(js_file, '.exe') - cmd = [shared.CLANG_CC, c, '-o', executable] + clang_native.get_clang_native_args() - self.run_process(cmd, env=clang_native.get_clang_native_env()) - # we can now run the executable directly, without an engine, which - # we indicate with None as the engine - engines += [[None]] if len(engines) == 0: self.skipTest('No JS engine present to run this test with. Check %s and the paths therein.' % config.EM_CONFIG) for engine in engines: diff --git a/test/test_benchmark.py b/test/test_benchmark.py index 6d8618725af9c..44f7080dd73eb 100644 --- a/test/test_benchmark.py +++ b/test/test_benchmark.py @@ -244,47 +244,6 @@ def get_output_files(self): return ret -class EmscriptenWasm2CBenchmarker(EmscriptenBenchmarker): - def __init__(self, name): - super().__init__(name, 'no engine needed') - - def build(self, parent, filename, args, shared_args, emcc_args, native_args, native_exec, lib_builder, has_output_parser): - # wasm2c doesn't want minimal runtime which the normal emscripten - # benchmarker defaults to, as we don't have any JS anyhow - emcc_args = emcc_args + [ - '-sSTANDALONE_WASM', - '-sMINIMAL_RUNTIME=0', - '-sWASM2C' - ] - - global LLVM_FEATURE_FLAGS - old_flags = LLVM_FEATURE_FLAGS - try: - # wasm2c does not support anything beyond MVP - LLVM_FEATURE_FLAGS = [] - super().build(parent, filename, args, shared_args, emcc_args, native_args, native_exec, lib_builder, has_output_parser) - finally: - LLVM_FEATURE_FLAGS = old_flags - - # move the JS away so there is no chance we run it by mistake - shutil.move(self.filename, self.filename + '.old.js') - - c = shared.replace_suffix(self.filenmame, '.wasm.c') - native = shared.replace_suffix(self.filenmame, '.exe') - - run_process(['clang', c, '-o', native, OPTIMIZATIONS, '-lm', - '-DWASM_RT_MAX_CALL_STACK_DEPTH=8000']) # for havlak - - self.filename = native - - def run(self, args): - return run_process([self.filename] + args, stdout=PIPE, stderr=PIPE, check=False).stdout - - def get_output_files(self): - # return the native code. c size may also be interesting. - return [self.filename] - - CHEERP_BIN = '/opt/cheerp/bin/' @@ -375,7 +334,6 @@ def get_output_files(self): EmscriptenBenchmarker(default_v8_name, aot_v8), EmscriptenBenchmarker(default_v8_name + '-lto', aot_v8, ['-flto']), EmscriptenBenchmarker(default_v8_name + '-ctors', aot_v8, ['-sEVAL_CTORS']), - # EmscriptenWasm2CBenchmarker('wasm2c') ] if os.path.exists(CHEERP_BIN): benchmarkers += [ diff --git a/test/test_core.py b/test/test_core.py index 7d0d2226e6d7d..5ca554316e0bc 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -154,11 +154,6 @@ def metafunc(self, rawfs): return metafunc -def can_do_wasm2c(self): - # the npm version of wasm2c does not support MEMORY64 - return not self.get_setting('MEMORY64') - - def can_do_standalone(self, impure=False): # Pure standalone engines don't support MEMORY64 yet. Even with MEMORY64=2 (lowered) # the WASI APIs that take pointer values don't have 64-bit variants yet. @@ -202,7 +197,7 @@ def decorated(self): # Impure means a test that cannot run in a wasm VM yet, as it is not 100% # standalone. We can still run them with the JS code though. -def also_with_standalone_wasm(wasm2c=False, impure=False): +def also_with_standalone_wasm(impure=False): def decorated(func): def metafunc(self, standalone): if not standalone: @@ -221,11 +216,6 @@ def metafunc(self, standalone): self.wasm_engines = [] self.node_args += shared.node_bigint_flags() func(self) - if wasm2c and can_do_wasm2c(self): - print('wasm2c') - self.set_setting('WASM2C') - self.wasm_engines = [] - func(self) metafunc._parameterize = {'': (False,), 'standalone': (True,)} @@ -610,7 +600,7 @@ def test_cube2md5(self): shutil.copyfile(test_file('cube2md5.txt'), 'cube2md5.txt') self.do_run_from_file(test_file('cube2md5.cpp'), test_file('cube2md5.ok'), assert_returncode=NON_ZERO) - @also_with_standalone_wasm(wasm2c=True) + @also_with_standalone_wasm() @needs_make('make') def test_cube2hash(self): # A good test of i64 math @@ -1089,7 +1079,7 @@ def test_regex(self): self.do_core_test('test_regex.c') @crossplatform - @also_with_standalone_wasm(wasm2c=True, impure=True) + @also_with_standalone_wasm(impure=True) def test_longjmp_standalone(self): self.do_core_test('test_longjmp.c') @@ -6304,7 +6294,7 @@ def test_unistd_misc(self, fs): self.emcc_args += ['-lnodefs.js'] self.do_run_in_out_file_test('unistd/misc.c', interleaved_output=False) - @also_with_standalone_wasm(wasm2c=True) + @also_with_standalone_wasm() def test_posixtime(self): self.do_core_test('test_posixtime.c') @@ -7122,7 +7112,7 @@ def do_test(): else: do_test_openjpeg() - @also_with_standalone_wasm(wasm2c=True, impure=True) + @also_with_standalone_wasm(impure=True) @no_asan('autodebug logging interferes with asan') @with_env_modify({'EMCC_AUTODEBUG': '1'}) def test_autodebug_wasm(self): @@ -7134,28 +7124,6 @@ def test_autodebug_wasm(self): for msg in ['log_execution', 'get_i32', 'set_i32', 'load_ptr', 'load_val', 'store_ptr', 'store_val']: self.assertIn(msg, output) - @parameterized({ - 'full': ('full',), - 'mask': ('mask',), - 'none': ('none',), - }) - def test_wasm2c_sandboxing(self, mode): - if self.get_setting('WASMFS'): - # wasm2c disables JS legalization since we are building in standalone - # mode. this happens to work without wasmfs, but with wasmfs we get the - # time when we create/update a file, which uses clock_time_get that has an - # i64 param. For such an import to work we need wasm-bigint support. - self.node_args += shared.node_bigint_flags() - if not can_do_standalone(self): - return self.skipTest('standalone mode not supported') - if not can_do_wasm2c(self): - return self.skipTest('wasm2c not supported') - self.set_setting('STANDALONE_WASM') - self.set_setting('WASM2C') - self.set_setting('WASM2C_SANDBOXING', mode) - self.wasm_engines = [] - self.do_core_test('test_hello_world.c') - ### Integration tests @crossplatform diff --git a/tools/wasm2c.py b/tools/wasm2c.py deleted file mode 100644 index 0250b568c3a18..0000000000000 --- a/tools/wasm2c.py +++ /dev/null @@ -1,238 +0,0 @@ -# Copyright 2020 The Emscripten Authors. All rights reserved. -# Emscripten is available under two separate licenses, the MIT license and the -# University of Illinois/NCSA Open Source License. Both these licenses can be -# found in the LICENSE file. - -import os -import re - -from tools.shared import unsuffixed, check_call -from tools.settings import settings -from tools.utils import path_from_root, exit_with_error -from tools import config - - -# map an emscripten-style signature letter to a wasm2c C type -def s_to_c(s): - if s == 'v': - return 'void' - elif s == 'i': - return 'u32' - elif s == 'j': - return 'u64' - elif s == 'p': - if settings.MEMORY64: - return 'u64' - else: - return 'u32' - elif s == 'f': - return 'f32' - elif s == 'd': - return 'f64' - else: - exit_with_error('invalid sig element:' + str(s)) - - -# map a wasm2c C type to an emscripten-style signature letter -def c_to_s(c): - if c == 'WASM_RT_I32': - return 'i' - elif c == 'WASM_RT_I64': - return 'j' - elif c == 'WASM_RT_F32': - return 'f' - elif c == 'WASM_RT_F64': - return 'd' - else: - exit_with_error('invalid wasm2c type element:' + str(c)) - - -def get_func_types(code): - ''' - We look for this pattern: - - static void init_func_types(void) { - func_types[0] = wasm_rt_register_func_type(3, 1, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32); - func_types[1] = wasm_rt_register_func_type(1, 1, WASM_RT_I32, WASM_RT_I32); - func_types[2] = wasm_rt_register_func_type(0, 0); - } - - We return a map of signatures names to their index. - ''' - init_func_types = re.search(r'static void init_func_types\(void\) {([^}]*)}', code) - if not init_func_types: - return {} - ret = {} - for entry in re.findall(r'func_types\[(\d+)\] = wasm_rt_register_func_type\((\d+), (\d+),? ?([^)]+)?\);', init_func_types[0]): - index, params, results, types = entry - index = int(index) - params = int(params) - results = int(results) - types = types.split(', ') - sig = '' - for i in range(params): - sig += c_to_s(types[i]) - if results == 0: - sig = 'v' + sig - else: - assert results == 1, 'no multivalue support' - sig = c_to_s(types[-1]) + sig - ret[sig] = index - return ret - - -def do_wasm2c(infile): - assert settings.STANDALONE_WASM - WASM2C = config.NODE_JS + [path_from_root('node_modules/wasm2c/wasm2c.js')] - WASM2C_DIR = path_from_root('node_modules/wasm2c') - c_file = unsuffixed(infile) + '.wasm.c' - h_file = unsuffixed(infile) + '.wasm.h' - cmd = WASM2C + [infile, '-o', c_file] - check_call(cmd) - total = '''\ -/* - * This file was generated by emcc+wasm2c. To compile it, use something like - * - * $CC FILE.c -O2 -lm -DWASM_RT_MAX_CALL_STACK_DEPTH=8000 - */ -''' - SEP = '\n/* ==================================== */\n' - - def bundle_file(filename): - nonlocal total - with open(filename) as f: - total += '// ' + filename + '\n' + f.read() + SEP - - # hermeticize the C file, by bundling in the wasm2c/ includes - headers = [ - (WASM2C_DIR, 'wasm-rt.h'), - (WASM2C_DIR, 'wasm-rt-impl.h'), - ('', h_file) - ] - for header in headers: - bundle_file(os.path.join(header[0], header[1])) - # add the wasm2c output - bundle_file(c_file) - # add the wasm2c runtime - bundle_file(os.path.join(WASM2C_DIR, 'wasm-rt-impl.c')) - # add the support code - support_files = ['base.c'] - if settings.AUTODEBUG: - support_files.append('autodebug.c') - if settings.EXPECT_MAIN: - # TODO: add an option for direct OS access. For now, do that when building - # an executable with main, as opposed to a library - support_files.append('os.c') - support_files.append('main.c') - else: - support_files.append('os_sandboxed.c') - support_files.append('reactor.c') - # for a reactor, also append wasmbox_* API definitions - with open(h_file, 'a') as f: - f.write(''' -// wasmbox_* API -// TODO: optional prefixing -extern void wasmbox_init(void); -''') - for support_file in support_files: - bundle_file(path_from_root(f'tools/wasm2c/{support_file}')) - # remove #includes of the headers we bundled - for header in headers: - total = total.replace('#include "%s"\n' % header[1], '/* include of %s */\n' % header[1]) - # generate the necessary invokes - invokes = [] - for sig in re.findall(r"\/\* import\: 'env' 'invoke_(\w+)' \*\/", total): - all_func_types = get_func_types(total) - - def name(i): - return 'a' + str(i) - - wabt_sig = sig[0] + 'i' + sig[1:] - typed_args = [s_to_c(sig[i]) + ' ' + name(i) for i in range(1, len(sig))] - full_typed_args = ['u32 fptr'] + typed_args - types = [s_to_c(sig[i]) for i in range(1, len(sig))] - args = [name(i) for i in range(1, len(sig))] - c_func_type = s_to_c(sig[0]) + ' (*)(' + (', '.join(types) if types else 'void') + ')' - if sig not in all_func_types: - exit_with_error('could not find signature ' + sig + ' in function types ' + str(all_func_types)) - type_index = all_func_types[sig] - - invokes.append(r''' -IMPORT_IMPL(%(return_type)s, Z_envZ_invoke_%(sig)sZ_%(wabt_sig)s, (%(full_typed_args)s), { - VERBOSE_LOG("invoke\n"); // waka - u32 sp = WASM_RT_ADD_PREFIX(Z_stackSaveZ_iv)(); - if (next_setjmp >= MAX_SETJMP_STACK) { - abort_with_message("too many nested setjmps"); - } - u32 id = next_setjmp++; - int result = setjmp(setjmp_stack[id]); - %(declare_return)s - if (result == 0) { - %(receive)sCALL_INDIRECT(w2c___indirect_function_table, %(c_func_type)s, %(type_index)s, fptr %(args)s); - /* if we got here, no longjmp or exception happened, we returned normally */ - } else { - /* A longjmp or an exception took us here. */ - WASM_RT_ADD_PREFIX(Z_stackRestoreZ_vi)(sp); - WASM_RT_ADD_PREFIX(Z_setThrewZ_vii)(1, 0); - } - next_setjmp--; - %(return)s -}); -''' % { - 'return_type': s_to_c(sig[0]) if sig[0] != 'v' else 'void', - 'sig': sig, - 'wabt_sig': wabt_sig, - 'full_typed_args': ', '.join(full_typed_args), - 'type_index': type_index, - 'c_func_type': c_func_type, - 'args': (', ' + ', '.join(args)) if args else '', - 'declare_return': (s_to_c(sig[0]) + ' returned_value = 0;') if sig[0] != 'v' else '', - 'receive': 'returned_value = ' if sig[0] != 'v' else '', - 'return': 'return returned_value;' if sig[0] != 'v' else '' - }) - - total += '\n'.join(invokes) - - # adjust sandboxing - TRAP_OOB = 'TRAP(OOB)' - assert total.count(TRAP_OOB) == 2 - if settings.WASM2C_SANDBOXING == 'full': - pass # keep it - elif settings.WASM2C_SANDBOXING == 'none': - total = total.replace(TRAP_OOB, '{}') - elif settings.WASM2C_SANDBOXING == 'mask': - assert not settings.ALLOW_MEMORY_GROWTH - assert (settings.INITIAL_MEMORY & (settings.INITIAL_MEMORY - 1)) == 0, 'poewr of 2' - total = total.replace(TRAP_OOB, '{}') - MEM_ACCESS = '[addr]' - assert total.count(MEM_ACCESS) == 3, '2 from wasm2c, 1 from runtime' - total = total.replace(MEM_ACCESS, '[addr & %d]' % (settings.INITIAL_MEMORY - 1)) - else: - exit_with_error('bad sandboxing') - - # adjust prefixing: emit simple output that works with multiple libraries, - # each compiled into its own single .c file, by adding 'static' in some places - # TODO: decide on the proper pattern for this in an upstream discussion in - # wasm2c; another option would be to prefix all these things. - for rep in [ - 'uint32_t wasm_rt_register_func_type(', - 'void wasm_rt_trap(', - 'void wasm_rt_allocate_memory(', - 'uint32_t wasm_rt_grow_memory(', - 'void wasm_rt_allocate_table(', - 'jmp_buf g_jmp_buf', - 'uint32_t g_func_type_count', - 'FuncType* g_func_types', - 'uint32_t wasm_rt_call_stack_depth', - 'uint32_t g_saved_call_stack_depth', - ]: - # remove 'extern' from declaration - total = total.replace('extern ' + rep, rep) - # add 'static' to implementation - old = total - total = total.replace(rep, 'static ' + rep) - assert old != total, f'did not find "{rep}"' - - # write out the final file - with open(c_file, 'w') as out: - out.write(total) diff --git a/tools/wasm2c/autodebug.c b/tools/wasm2c/autodebug.c deleted file mode 100644 index 3726055c2ebbf..0000000000000 --- a/tools/wasm2c/autodebug.c +++ /dev/null @@ -1,87 +0,0 @@ -IMPORT_IMPL(void, Z_envZ_log_executionZ_vi, (u32 loc), { - printf("log_execution %d\n", loc); -}); -IMPORT_IMPL(u32, Z_envZ_get_i32Z_iiii, (u32 loc, u32 index, u32 value), { - printf("get_i32 %d,%d,%d\n", loc, index, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_get_i64Z_iiiii, (u32 loc, u32 index, u32 low, u32 high), { - printf("get_i64 %d,%d,%d,%d\n", loc, index, low, high); - w2c_setTempRet0(high); - return low; -}); -IMPORT_IMPL(f32, Z_envZ_get_f32Z_fiif, (u32 loc, u32 index, f32 value), { - printf("get_f32 %d,%d,%f\n", loc, index, value); - return value; -}); -IMPORT_IMPL(f64, Z_envZ_get_f64Z_diid, (u32 loc, u32 index, f64 value), { - printf("get_f64 %d,%d,%f\n", loc, index, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_set_i32Z_iiii, (u32 loc, u32 index, u32 value), { - printf("set_i32 %d,%d,%d\n", loc, index, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_set_i64Z_iiiii, (u32 loc, u32 index, u32 low, u32 high), { - printf("set_i64 %d,%d,%d,%d\n", loc, index, low, high); - w2c_setTempRet0(high); - return low; -}); -IMPORT_IMPL(f32, Z_envZ_set_f32Z_fiif, (u32 loc, u32 index, f32 value), { - printf("set_f32 %d,%d,%f\n", loc, index, value); - return value; -}); -IMPORT_IMPL(f64, Z_envZ_set_f64Z_diid, (u32 loc, u32 index, f64 value), { - printf("set_f64 %d,%d,%f\n", loc, index, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_load_ptrZ_iiiii, (u32 loc, u32 bytes, u32 offset, u32 ptr), { - printf("load_ptr %d,%d,%d,%d\n", loc, bytes, offset, ptr); - return ptr; -}); -IMPORT_IMPL(u32, Z_envZ_load_val_i32Z_iii, (u32 loc, u32 value), { - printf("load_val_i32 %d,%d\n", loc, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_load_val_i64Z_iiii, (u32 loc, u32 low, u32 high), { - printf("load_val_i64 %d,%d,%d\n", loc, low, high); - w2c_setTempRet0(high); - return low; -}); -IMPORT_IMPL(u64, Z_envZ_load_val_i64Z_jij, (u32 loc, u64 value), { - printf("load_val_i64 %d,%d,%d\n", loc, (u32)value, (u32)(value >> 32)); - return value; -}); -IMPORT_IMPL(f32, Z_envZ_load_val_f32Z_fif, (u32 loc, f32 value), { - printf("load_val_f32 %d,%f\n", loc, value); - return value; -}); -IMPORT_IMPL(f64, Z_envZ_load_val_f64Z_did, (u32 loc, f64 value), { - printf("load_val_f64 %d,%f\n", loc, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_store_ptrZ_iiiii, (u32 loc, u32 bytes, u32 offset, u32 ptr), { - printf("store_ptr %d,%d,%d,%d\n", loc, bytes, offset, ptr); - return ptr; -}); -IMPORT_IMPL(u32, Z_envZ_store_val_i32Z_iii, (u32 loc, u32 value), { - printf("store_val_i32 %d,%d\n", loc, value); - return value; -}); -IMPORT_IMPL(u32, Z_envZ_store_val_i64Z_iiii, (u32 loc, u32 low, u32 high), { - printf("store_val_i64 %d,%d,%d\n", loc, low, high); - w2c_setTempRet0(high); - return low; -}); -IMPORT_IMPL(u64, Z_envZ_store_val_i64Z_jij, (u32 loc, u64 value), { - printf("store_val_i64 %d,%d,%d\n", loc, (u32)value, (u32)(value >> 32)); - return value; -}); -IMPORT_IMPL(f32, Z_envZ_store_val_f32Z_fif, (u32 loc, f32 value), { - printf("store_val_f32 %d,%f\n", loc, value); - return value; -}); -IMPORT_IMPL(f64, Z_envZ_store_val_f64Z_did, (u32 loc, f64 value), { - printf("store_val_f64 %d,%f\n", loc, value); - return value; -}); diff --git a/tools/wasm2c/base.c b/tools/wasm2c/base.c deleted file mode 100644 index 37d5c0dcf8855..0000000000000 --- a/tools/wasm2c/base.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Base of all support for wasm2c code. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// ssize_t detection: usually stdint provides it, but not on windows apparently -#ifdef _WIN32 -#ifdef _MSC_VER -#include -typedef SSIZE_T ssize_t; -#else // _MSC_VER -#ifdef _WIN64 -typedef signed long long ssize_t; -#else // _WIN64 -typedef signed long ssize_t; -#endif // _WIN64 -#endif // _MSC_VER -#endif // _WIN32 - -#include "wasm-rt.h" -#include "wasm-rt-impl.h" - -#define UNLIKELY(x) __builtin_expect(!!(x), 0) -#define LIKELY(x) __builtin_expect(!!(x), 1) - -#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0) - -#define MEMACCESS(addr) ((void*)&WASM_RT_ADD_PREFIX(Z_memory)->data[addr]) - -#undef MEMCHECK -#define MEMCHECK(a, t) \ - if (UNLIKELY((a) + sizeof(t) > WASM_RT_ADD_PREFIX(Z_memory)->size)) TRAP(OOB) - -#undef DEFINE_LOAD -#define DEFINE_LOAD(name, t1, t2, t3) \ - static inline t3 name(u64 addr) { \ - MEMCHECK(addr, t1); \ - t1 result; \ - memcpy(&result, MEMACCESS(addr), sizeof(t1)); \ - return (t3)(t2)result; \ - } - -#undef DEFINE_STORE -#define DEFINE_STORE(name, t1, t2) \ - static inline void name(u64 addr, t2 value) { \ - MEMCHECK(addr, t1); \ - t1 wrapped = (t1)value; \ - memcpy(MEMACCESS(addr), &wrapped, sizeof(t1)); \ - } - -DEFINE_LOAD(wasm_i32_load, u32, u32, u32); -DEFINE_LOAD(wasm_i64_load, u64, u64, u64); -DEFINE_LOAD(wasm_f32_load, f32, f32, f32); -DEFINE_LOAD(wasm_f64_load, f64, f64, f64); -DEFINE_LOAD(wasm_i32_load8_s, s8, s32, u32); -DEFINE_LOAD(wasm_i64_load8_s, s8, s64, u64); -DEFINE_LOAD(wasm_i32_load8_u, u8, u32, u32); -DEFINE_LOAD(wasm_i64_load8_u, u8, u64, u64); -DEFINE_LOAD(wasm_i32_load16_s, s16, s32, u32); -DEFINE_LOAD(wasm_i64_load16_s, s16, s64, u64); -DEFINE_LOAD(wasm_i32_load16_u, u16, u32, u32); -DEFINE_LOAD(wasm_i64_load16_u, u16, u64, u64); -DEFINE_LOAD(wasm_i64_load32_s, s32, s64, u64); -DEFINE_LOAD(wasm_i64_load32_u, u32, u64, u64); -DEFINE_STORE(wasm_i32_store, u32, u32); -DEFINE_STORE(wasm_i64_store, u64, u64); -DEFINE_STORE(wasm_f32_store, f32, f32); -DEFINE_STORE(wasm_f64_store, f64, f64); -DEFINE_STORE(wasm_i32_store8, u8, u32); -DEFINE_STORE(wasm_i32_store16, u16, u32); -DEFINE_STORE(wasm_i64_store8, u8, u64); -DEFINE_STORE(wasm_i64_store16, u16, u64); -DEFINE_STORE(wasm_i64_store32, u32, u64); - -// Imports - -#ifdef VERBOSE_LOGGING -#define VERBOSE_LOG(...) { printf(__VA_ARGS__); } -#else -#define VERBOSE_LOG(...) -#endif - -#define IMPORT_IMPL(ret, name, params, body) \ -static ret _##name params { \ - VERBOSE_LOG("[import: " #name "]\n"); \ - body \ -} \ -ret (*WASM_RT_ADD_PREFIX(name)) params = _##name; - -#define STUB_IMPORT_IMPL(ret, name, params, returncode) IMPORT_IMPL(ret, name, params, { return returncode; }); - -// Generic abort method for a runtime error in the runtime. - -static void abort_with_message(const char* message) { - fprintf(stderr, "%s\n", message); - TRAP(UNREACHABLE); -} - -// Maintain a stack of setjmps, each jump taking us back to the last invoke. - -#define MAX_SETJMP_STACK 1024 - -static jmp_buf setjmp_stack[MAX_SETJMP_STACK]; - -static u32 next_setjmp = 0; - -IMPORT_IMPL(void, Z_envZ__emscripten_throw_longjmpZ_vv, (), { - if (next_setjmp == 0) { - abort_with_message("longjmp without setjmp"); - } - longjmp(setjmp_stack[next_setjmp - 1], 1); -}); - -IMPORT_IMPL(void, Z_envZ_emscripten_notify_memory_growthZ_vi, (u32 size), {}); - -// Shared OS support in both sandboxed and unsandboxed mode - -#define WASI_DEFAULT_ERROR 63 /* __WASI_ERRNO_PERM */ -#define WASI_EINVAL 28 - -// Syscalls return a negative error code -#define EM_EACCES -2 - -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_fdstat_getZ_iii, (u32 a, u32 b), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_syncZ_ii, (u32 a), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_envZ_dlopenZ_iii, (u32 a, u32 b), 1); -STUB_IMPORT_IMPL(u32, Z_envZ_dlcloseZ_ii, (u32 a), 1); -STUB_IMPORT_IMPL(u32, Z_envZ_dlsymZ_iii, (u32 a, u32 b), 0); -STUB_IMPORT_IMPL(u32, Z_envZ_dlerrorZ_iv, (), 0); -STUB_IMPORT_IMPL(u32, Z_envZ_signalZ_iii, (u32 a, u32 b), -1); -STUB_IMPORT_IMPL(u32, Z_envZ_systemZ_ii, (u32 a), -1); -STUB_IMPORT_IMPL(u32, Z_envZ_utimesZ_iii, (u32 a, u32 b), -1); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_rmdirZ_ii, (u32 a), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_renameZ_iii, (u32 a, u32 b), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_lstat64Z_iii, (u32 a, u32 b), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_dup3Z_iiii, (u32 a, u32 b, u32 c), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_dup2Z_iii, (u32 a, u32 b), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_getcwdZ_iii, (u32 a, u32 b), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_ftruncate64Z_iiiii, (u32 a, u32 b, u32 c, u32 d), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ_pthread_mutexattr_initZ_ii, (u32 a), 0); -STUB_IMPORT_IMPL(u32, Z_envZ_pthread_mutexattr_settypeZ_iii, (u32 a, u32 b), 0); -STUB_IMPORT_IMPL(u32, Z_envZ_pthread_mutexattr_destroyZ_ii, (u32 a), 0); -STUB_IMPORT_IMPL(u32, Z_envZ_pthread_createZ_iiiii, (u32 a, u32 b, u32 c, u32 d), -1); -STUB_IMPORT_IMPL(u32, Z_envZ_pthread_joinZ_iii, (u32 a, u32 b), -1); -STUB_IMPORT_IMPL(u32, Z_envZ___cxa_thread_atexitZ_iiii, (u32 a, u32 b, u32 c), -1); diff --git a/tools/wasm2c/main.c b/tools/wasm2c/main.c deleted file mode 100644 index ea7a535d437d2..0000000000000 --- a/tools/wasm2c/main.c +++ /dev/null @@ -1,42 +0,0 @@ -static int main_argc; -static char** main_argv; - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_args_sizes_getZ_iii, (u32 pargc, u32 pargv_buf_size), { - wasm_i32_store(pargc, main_argc); - u32 buf_size = 0; - for (u32 i = 0; i < main_argc; i++) { - buf_size += strlen(main_argv[i]) + 1; - } - wasm_i32_store(pargv_buf_size, buf_size); - return 0; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_args_getZ_iii, (u32 argv, u32 argv_buf), { - u32 buf_size = 0; - for (u32 i = 0; i < main_argc; i++) { - u32 ptr = argv_buf + buf_size; - wasm_i32_store(argv + i * 4, ptr); - char* arg = main_argv[i]; - strcpy(MEMACCESS(ptr), arg); - buf_size += strlen(arg) + 1; - } - return 0; -}); - -int main(int argc, char** argv) { - main_argc = argc; - main_argv = argv; - - init_fds(); - - init(); - - int trap_code; - if ((trap_code = setjmp(g_jmp_buf))) { - printf("[wasm trap %d, halting]\n", trap_code); - return 1; - } else { - WASM_RT_ADD_PREFIX(Z__startZ_vv)(); - } - return 0; -} diff --git a/tools/wasm2c/os.c b/tools/wasm2c/os.c deleted file mode 100644 index f5e0f0bde66ec..0000000000000 --- a/tools/wasm2c/os.c +++ /dev/null @@ -1,321 +0,0 @@ -#include -#include -#include - -#ifndef _WIN32 -#include -#else -#include -#endif - -IMPORT_IMPL(void, Z_wasi_snapshot_preview1Z_proc_exitZ_vi, (u32 x), { - exit(x); -}); - -#define MAX_FDS 1024 - -static int wasm_fd_to_native[MAX_FDS]; - -static u32 next_wasm_fd; - -#define WASM_STDIN 0 -#define WASM_STDOUT 1 -#define WASM_STDERR 2 - -static void init_fds() { -#ifndef _WIN32 - wasm_fd_to_native[WASM_STDIN] = STDIN_FILENO; - wasm_fd_to_native[WASM_STDOUT] = STDOUT_FILENO; - wasm_fd_to_native[WASM_STDERR] = STDERR_FILENO; -#else - wasm_fd_to_native[WASM_STDIN] = _fileno(stdin); - wasm_fd_to_native[WASM_STDOUT] = _fileno(stdout); - wasm_fd_to_native[WASM_STDERR] = _fileno(stderr); -#endif - next_wasm_fd = 3; -} - -static u32 get_or_allocate_wasm_fd(int nfd) { - // If the native fd is already mapped, return the same wasm fd for it. - for (int i = 0; i < next_wasm_fd; i++) { - if (wasm_fd_to_native[i] == nfd) { - return i; - } - } - if (next_wasm_fd >= MAX_FDS) { - abort_with_message("ran out of fds"); - } - u32 fd = next_wasm_fd; - wasm_fd_to_native[fd] = nfd; - next_wasm_fd++; - return fd; -} - -static int get_native_fd(u32 fd) { - if (fd >= MAX_FDS || fd >= next_wasm_fd) { - return -1; - } - return wasm_fd_to_native[fd]; -} - -IMPORT_IMPL(u32, Z_envZ___sys_openZ_iiii, (u32 path, u32 flags, u32 varargs), { - VERBOSE_LOG(" open: %s %d %d\n", MEMACCESS(path), flags, wasm_i32_load(varargs)); - int nfd = open(MEMACCESS(path), flags, wasm_i32_load(varargs)); - VERBOSE_LOG(" => native %d\n", nfd); - if (nfd >= 0) { - u32 fd = get_or_allocate_wasm_fd(nfd); - VERBOSE_LOG(" => wasm %d\n", fd); - return fd; - } - return -1; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_writeZ_iiiii, (u32 fd, u32 iov, u32 iovcnt, u32 pnum), { - int nfd = get_native_fd(fd); - VERBOSE_LOG(" fd_write wasm %d => native %d\n", fd, nfd); - if (nfd < 0) { - return WASI_DEFAULT_ERROR; - } - u32 num = 0; - for (u32 i = 0; i < iovcnt; i++) { - u32 ptr = wasm_i32_load(iov + i * 8); - u32 len = wasm_i32_load(iov + i * 8 + 4); - VERBOSE_LOG(" chunk %d %d\n", ptr, len); - ssize_t result; - // Use stdio for stdout/stderr to avoid mixing a low-level write() with - // other logging code, which can change the order from the expected. - if (fd == WASM_STDOUT) { - result = fwrite(MEMACCESS(ptr), 1, len, stdout); - } else if (fd == WASM_STDERR) { - result = fwrite(MEMACCESS(ptr), 1, len, stderr); - } else { - result = write(nfd, MEMACCESS(ptr), len); - } - if (result < 0) { - VERBOSE_LOG(" error, %d %s\n", errno, strerror(errno)); - return WASI_DEFAULT_ERROR; - } - if (result != len) { - VERBOSE_LOG(" amount error, %ld %d\n", result, len); - return WASI_DEFAULT_ERROR; - } - num += len; - } - VERBOSE_LOG(" success: %d\n", num); - wasm_i32_store(pnum, num); - return 0; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_readZ_iiiii, (u32 fd, u32 iov, u32 iovcnt, u32 pnum), { - int nfd = get_native_fd(fd); - VERBOSE_LOG(" fd_read wasm %d => native %d\n", fd, nfd); - if (nfd < 0) { - return WASI_DEFAULT_ERROR; - } - u32 num = 0; - for (u32 i = 0; i < iovcnt; i++) { - u32 ptr = wasm_i32_load(iov + i * 8); - u32 len = wasm_i32_load(iov + i * 8 + 4); - VERBOSE_LOG(" chunk %d %d\n", ptr, len); - ssize_t result = read(nfd, MEMACCESS(ptr), len); - if (result < 0) { - VERBOSE_LOG(" error, %d %s\n", errno, strerror(errno)); - return WASI_DEFAULT_ERROR; - } - num += result; - if (result != len) { - break; // nothing more to read - } - } - VERBOSE_LOG(" success: %d\n", num); - wasm_i32_store(pnum, num); - return 0; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_closeZ_ii, (u32 fd), { - // TODO full file support - int nfd = get_native_fd(fd); - VERBOSE_LOG(" close wasm %d => native %d\n", fd, nfd); - if (nfd < 0) { - return WASI_DEFAULT_ERROR; - } - close(nfd); - return 0; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_environ_sizes_getZ_iii, (u32 pcount, u32 pbuf_size), { - // TODO: connect to actual env? - wasm_i32_store(pcount, 0); - wasm_i32_store(pbuf_size, 0); - return 0; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_environ_getZ_iii, (u32 __environ, u32 environ_buf), { - // TODO: connect to actual env? - return 0; -}); - -static int whence_to_native(u32 whence) { - if (whence == 0) return SEEK_SET; - if (whence == 1) return SEEK_CUR; - if (whence == 2) return SEEK_END; - return -1; -} - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_seekZ_iijii, (u32 fd, u64 offset, u32 whence, u32 new_offset), { - int nfd = get_native_fd(fd); - int nwhence = whence_to_native(whence); - VERBOSE_LOG(" seek %d (=> native %d) %ld %d (=> %d) %d\n", fd, nfd, offset, whence, nwhence, new_offset); - if (nfd < 0) { - return WASI_DEFAULT_ERROR; - } - off_t off = lseek(nfd, offset, nwhence); - VERBOSE_LOG(" off: %ld\n", off); - if (off == (off_t)-1) { - VERBOSE_LOG(" error, %d %s\n", errno, strerror(errno)); - return WASI_DEFAULT_ERROR; - } - wasm_i64_store(new_offset, off); - return 0; -}); -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_seekZ_iiiiii, (u32 a, u32 b, u32 c, u32 d, u32 e), { - return Z_wasi_snapshot_preview1Z_fd_seekZ_iijii(a, b + (((u64)c) << 32), d, e); -}); - -// TODO: set errno in wasm for things that need it - -IMPORT_IMPL(u32, Z_envZ___sys_unlinkZ_ii, (u32 path), { - VERBOSE_LOG(" unlink %s\n", MEMACCESS(path)); - if (unlink(MEMACCESS(path))) { - VERBOSE_LOG(" error, %d %s\n", errno, strerror(errno)); - return EM_EACCES; - } - return 0; -}); - -static u32 do_stat(int nfd, u32 buf) { - struct stat nbuf; - if (fstat(nfd, &nbuf)) { - VERBOSE_LOG(" error, %d %s\n", errno, strerror(errno)); - return EM_EACCES; - } - VERBOSE_LOG(" success, size=%ld\n", nbuf.st_size); - wasm_i32_store(buf + 0, nbuf.st_dev); - wasm_i32_store(buf + 4, 0); - wasm_i32_store(buf + 8, nbuf.st_ino); - wasm_i32_store(buf + 12, nbuf.st_mode); - wasm_i32_store(buf + 16, nbuf.st_nlink); - wasm_i32_store(buf + 20, nbuf.st_uid); - wasm_i32_store(buf + 24, nbuf.st_gid); - wasm_i32_store(buf + 28, nbuf.st_rdev); - wasm_i32_store(buf + 32, 0); - wasm_i64_store(buf + 40, nbuf.st_size); -#ifdef _WIN32 - wasm_i32_store(buf + 48, 512); // fixed blocksize on windows - wasm_i32_store(buf + 52, 0); // but no reported blocks... -#else - wasm_i32_store(buf + 48, nbuf.st_blksize); - wasm_i32_store(buf + 52, nbuf.st_blocks); -#endif -#if defined(__APPLE__) || defined(__NetBSD__) - wasm_i32_store(buf + 56, nbuf.st_atimespec.tv_sec); - wasm_i32_store(buf + 60, nbuf.st_atimespec.tv_nsec); - wasm_i32_store(buf + 64, nbuf.st_mtimespec.tv_sec); - wasm_i32_store(buf + 68, nbuf.st_mtimespec.tv_nsec); - wasm_i32_store(buf + 72, nbuf.st_ctimespec.tv_sec); - wasm_i32_store(buf + 76, nbuf.st_ctimespec.tv_nsec); -#elif defined(_WIN32) - wasm_i32_store(buf + 56, gmtime(&nbuf.st_atime)->tm_sec); - wasm_i32_store(buf + 60, 0); - wasm_i32_store(buf + 64, gmtime(&nbuf.st_mtime)->tm_sec); - wasm_i32_store(buf + 68, 0); - wasm_i32_store(buf + 72, gmtime(&nbuf.st_ctime)->tm_sec); - wasm_i32_store(buf + 76, 0); -#else - wasm_i32_store(buf + 56, nbuf.st_atim.tv_sec); - wasm_i32_store(buf + 60, nbuf.st_atim.tv_nsec); - wasm_i32_store(buf + 64, nbuf.st_mtim.tv_sec); - wasm_i32_store(buf + 68, nbuf.st_mtim.tv_nsec); - wasm_i32_store(buf + 72, nbuf.st_ctim.tv_sec); - wasm_i32_store(buf + 76, nbuf.st_ctim.tv_nsec); -#endif - wasm_i64_store(buf + 80, nbuf.st_ino); - return 0; -} - -IMPORT_IMPL(u32, Z_envZ___sys_fstat64Z_iii, (u32 fd, u32 buf), { - int nfd = get_native_fd(fd); - VERBOSE_LOG(" fstat64 %d (=> %d) %d\n", fd, nfd, buf); - if (nfd < 0) { - return EM_EACCES; - } - return do_stat(nfd, buf); -}); - -IMPORT_IMPL(u32, Z_envZ___sys_stat64Z_iii, (u32 path, u32 buf), { - VERBOSE_LOG(" stat64: %s\n", MEMACCESS(path)); - int nfd = open(MEMACCESS(path), O_RDONLY); // could be O_PATH on linux... - if (nfd < 0) { - VERBOSE_LOG(" error, %d %s\n", errno, strerror(errno)); - return EM_EACCES; - } - return do_stat(nfd, buf); -}); - -IMPORT_IMPL(u32, Z_envZ___sys_readZ_iiii, (u32 fd, u32 buf, u32 count), { - int nfd = get_native_fd(fd); - VERBOSE_LOG(" read %d (=> %d) %d %d\n", fd, nfd, buf, count); - if (nfd < 0) { - VERBOSE_LOG(" bad fd\n"); - return EM_EACCES; - } - ssize_t ret = read(nfd, MEMACCESS(buf), count); - VERBOSE_LOG(" native read: %ld\n", ret); - if (ret < 0) { - VERBOSE_LOG(" read error %d %s\n", errno, strerror(errno)); - return EM_EACCES; - } - return ret; -}); - -IMPORT_IMPL(u32, Z_envZ___sys_accessZ_iii, (u32 pathname, u32 mode), { - VERBOSE_LOG(" access: %s 0x%x\n", MEMACCESS(pathname), mode); - // TODO: sandboxing, convert mode - int result = access(MEMACCESS(pathname), mode); - if (result < 0) { - VERBOSE_LOG(" access error: %d %s\n", errno, strerror(errno)); - return EM_EACCES; - } - return 0; -}); - -#define WASM_CLOCK_REALTIME 0 -#define WASM_CLOCK_MONOTONIC 1 -#define WASM_CLOCK_PROCESS_CPUTIME 2 -#define WASM_CLOCK_THREAD_CPUTIME_ID 3 - -static int check_clock(u32 clock_id) { - return clock_id == WASM_CLOCK_REALTIME || clock_id == WASM_CLOCK_MONOTONIC || - clock_id == WASM_CLOCK_PROCESS_CPUTIME || clock_id == WASM_CLOCK_THREAD_CPUTIME_ID; -} - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_clock_time_getZ_iiji, (u32 clock_id, u64 max_lag, u32 out), { - if (!check_clock(clock_id)) { - return WASI_EINVAL; - } - // TODO: handle realtime vs monotonic etc. - // wasi expects a result in nanoseconds, and we know how to convert clock() - // to seconds, so compute from there - const double NSEC_PER_SEC = 1000.0 * 1000.0 * 1000.0; - wasm_i64_store(out, (u64)(clock() / (CLOCKS_PER_SEC / NSEC_PER_SEC))); - return 0; -}); - -IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_clock_res_getZ_iii, (u32 clock_id, u32 out), { - if (!check_clock(clock_id)) { - return WASI_EINVAL; - } - // TODO: handle realtime vs monotonic etc. For now just report "milliseconds". - wasm_i64_store(out, 1000 * 1000); - return 0; -}); diff --git a/tools/wasm2c/os_sandboxed.c b/tools/wasm2c/os_sandboxed.c deleted file mode 100644 index 728d7130cb006..0000000000000 --- a/tools/wasm2c/os_sandboxed.c +++ /dev/null @@ -1,22 +0,0 @@ -// Stubs for OS functions, for a sandboxed environment. Nothing is allowed -// exit the sandbox, calls to printf will fail, etc. - -IMPORT_IMPL(void, Z_wasi_snapshot_preview1Z_proc_exitZ_vi, (u32 x), { - abort_with_message("exit() called"); -}); - -STUB_IMPORT_IMPL(u32, Z_envZ___sys_openZ_iiii, (u32 path, u32 flags, u32 varargs), -1); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_writeZ_iiiii, (u32 fd, u32 iov, u32 iovcnt, u32 pnum), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_readZ_iiiii, (u32 fd, u32 iov, u32 iovcnt, u32 pnum), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_closeZ_ii, (u32 fd), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_environ_sizes_getZ_iii, (u32 pcount, u32 pbuf_size), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_environ_getZ_iii, (u32 __environ, u32 environ_buf), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_seekZ_iijii, (u32 fd, u64 offset, u32 whence, u32 new_offset), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_fd_seekZ_iiiiii, (u32 a, u32 b, u32 c, u32 d, u32 e), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_unlinkZ_ii, (u32 path), WASI_DEFAULT_ERROR); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_fstat64Z_iii, (u32 fd, u32 buf), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_stat64Z_iii, (u32 path, u32 buf), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_readZ_iiii, (u32 fd, u32 buf, u32 count), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_envZ___sys_accessZ_iii, (u32 pathname, u32 mode), EM_EACCES); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_clock_time_getZ_iiji, (u32 clock_id, u64 max_lag, u32 out), WASI_EINVAL); -STUB_IMPORT_IMPL(u32, Z_wasi_snapshot_preview1Z_clock_res_getZ_iii, (u32 clock_id, u32 out), WASI_EINVAL); diff --git a/tools/wasm2c/reactor.c b/tools/wasm2c/reactor.c deleted file mode 100644 index 647608beadc68..0000000000000 --- a/tools/wasm2c/reactor.c +++ /dev/null @@ -1,14 +0,0 @@ -// TODO: optional prefixing -void WASM_RT_ADD_PREFIX(wasmbox_init)(void) { - // Initialize wasm2c runtime. - WASM_RT_ADD_PREFIX(init)(); - - // Set up handling for a trap - int trap_code; - if ((trap_code = setjmp(g_jmp_buf))) { - printf("[wasm trap %d, halting]\n", trap_code); - abort(); - } else { - WASM_RT_ADD_PREFIX(Z__initializeZ_vv)(); - } -} From 272363d59a0f3c8bc73fcd6782dfbd6a6c7811f2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 16 May 2023 13:06:04 -0700 Subject: [PATCH 2/2] more --- emcc.py | 9 --------- package-lock.json | 13 +------------ package.json | 3 +-- test/test_other.py | 44 -------------------------------------------- 4 files changed, 2 insertions(+), 67 deletions(-) diff --git a/emcc.py b/emcc.py index 7d18fc470d6d0..ce1d7ceabbfca 100755 --- a/emcc.py +++ b/emcc.py @@ -1897,15 +1897,6 @@ def phase_linker_setup(options, state, newargs): settings.STANDALONE_WASM = 1 settings.WASM_BIGINT = 1 - if settings.WASM2C: - # wasm2c only makes sense with standalone wasm - there will be no JS, - # just wasm and then C - settings.STANDALONE_WASM = 1 - # wasm2c doesn't need any special handling of i64, we have proper i64 - # handling on the FFI boundary, which is exactly like the case of JS with - # BigInt support - settings.WASM_BIGINT = 1 - if options.no_entry: settings.EXPECT_MAIN = 0 elif settings.STANDALONE_WASM: diff --git a/package-lock.json b/package-lock.json index 99b7251aee988..d6cc3984e50e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,8 +7,7 @@ "dependencies": { "acorn": "^8.7.1", "google-closure-compiler": "20220502.0.0", - "html-minifier-terser": "7.2.0", - "wasm2c": "1.0.0" + "html-minifier-terser": "7.2.0" }, "devDependencies": { "es-check": "^6.2.1", @@ -3174,11 +3173,6 @@ "node": ">=0.10.0" } }, - "node_modules/wasm2c": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/wasm2c/-/wasm2c-1.0.0.tgz", - "integrity": "sha512-4SIESF2JNxrry6XFa/UQcsQibn+bxPkQ/oqixiXz2o8fsMl8J4vtvhH/evgbi8vZajAlaukuihEcQTWb9tVLUA==" - }, "node_modules/watchpack": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.4.0.tgz", @@ -5984,11 +5978,6 @@ } } }, - "wasm2c": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/wasm2c/-/wasm2c-1.0.0.tgz", - "integrity": "sha512-4SIESF2JNxrry6XFa/UQcsQibn+bxPkQ/oqixiXz2o8fsMl8J4vtvhH/evgbi8vZajAlaukuihEcQTWb9tVLUA==" - }, "watchpack": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.4.0.tgz", diff --git a/package.json b/package.json index b1673d47885f2..ab701d4615772 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,7 @@ "dependencies": { "acorn": "^8.7.1", "google-closure-compiler": "20220502.0.0", - "html-minifier-terser": "7.2.0", - "wasm2c": "1.0.0" + "html-minifier-terser": "7.2.0" }, "scripts": { "lint": "eslint .", diff --git a/test/test_other.py b/test/test_other.py index 4db28ef8df22d..ae16bc3bd1e4c 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -11189,50 +11189,6 @@ def test_standalone_syscalls(self): for engine in config.WASM_ENGINES: self.assertContained(expected, self.run_js('test.wasm', engine)) - @requires_native_clang - def test_wasm2c_reactor(self): - # test compiling an unsafe library using wasm2c, then using it from a - # main program. this shows it is easy to use wasm2c as a sandboxing - # mechanism. - - # first compile the library with emcc, getting a .c and .h - self.run_process([EMCC, - test_file('other/wasm2c/unsafe-library.c'), - '-O3', '-o', 'lib.wasm', '-sWASM2C', '--no-entry']) - # compile the main program natively normally, together with the unsafe - # library - self.run_process([CLANG_CC, - test_file('other/wasm2c/my-code.c'), - 'lib.wasm.c', '-O3', '-o', 'program.exe'] + - clang_native.get_clang_native_args(), - env=clang_native.get_clang_native_env()) - output = self.run_process([os.path.abspath('program.exe')], stdout=PIPE).stdout - self.assertEqual(output, read_file(test_file('other/wasm2c/output.txt'))) - - @requires_native_clang - def test_wasm2c_multi_lib(self): - # compile two libraries to object files - for lib in ['a', 'b']: - self.run_process([EMCC, - test_file('other/wasm2c', f'unsafe-library-{lib}.c'), - '-O3', '-o', f'lib{lib}.wasm', '-sWASM2C', '--no-entry']) - # build with a different WASM_RT_MODULE_PREFIX for each library, so that - # they do not have colliding symbols - self.run_process([CLANG_CC, f'lib{lib}.wasm.c', '-O3', '-c', - f'-DWASM_RT_MODULE_PREFIX={lib}_'] + - clang_native.get_clang_native_args(), - env=clang_native.get_clang_native_env()) - - # compile the main program with the wasmboxed libraries - self.run_process([CLANG_CC, - test_file('other/wasm2c/my-code-multi.c'), - 'liba.wasm.o', 'libb.wasm.o', - '-O3', '-o', 'program.exe'] + - clang_native.get_clang_native_args(), - env=clang_native.get_clang_native_env()) - output = self.run_process([os.path.abspath('program.exe')], stdout=PIPE).stdout - self.assertEqual(output, read_file(test_file('other/wasm2c/output-multi.txt'))) - @parameterized({ 'wasm2js': (['-sWASM=0'], ''), 'modularize': (['-sMODULARIZE'], 'Module()'),