From d5de2c0c25fcd20cbf6027a9efeaebcbfc65a10f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Mon, 30 Dec 2024 13:58:38 -0500 Subject: [PATCH] Remove unnecessary `args_get` implementation --- src/sandbox/sandbox.cpp | 54 ++++++++++++++++++++--------------------- src/sandbox/sandbox.h | 2 -- src/sandbox/wasi.cpp | 14 +++-------- src/sandbox/wasi.h | 3 +-- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/sandbox/sandbox.cpp b/src/sandbox/sandbox.cpp index 3560dcf0..3b0a34a0 100644 --- a/src/sandbox/sandbox.cpp +++ b/src/sandbox/sandbox.cpp @@ -44,26 +44,6 @@ extern "C" void mkxp_sandbox_trap_handler(wasm_rt_trap_t code) { throw SandboxTrapException(); } -std::vector Sandbox::get_args() { - std::vector args{"mkxp-z"}; - args.push_back("-e "); - if (MJIT_ENABLED) { - std::string verboseLevel("--mjit-verbose="); - std::string maxCache("--mjit-max-cache="); - std::string minCalls("--mjit-min-calls="); - args.push_back("--mjit"); - verboseLevel += std::to_string(MJIT_VERBOSE); - maxCache += std::to_string(MJIT_MAX_CACHE); - minCalls += std::to_string(MJIT_MIN_CALLS); - args.push_back(verboseLevel.c_str()); - args.push_back(maxCache.c_str()); - args.push_back(minCalls.c_str()); - } else if (YJIT_ENABLED) { - args.push_back("--yjit"); - } - return args; -} - usize Sandbox::sandbox_malloc(usize size) { usize buf = w2c_ruby_mkxp_sandbox_malloc(RB, size); @@ -80,22 +60,40 @@ void Sandbox::sandbox_free(usize ptr) { w2c_ruby_mkxp_sandbox_free(RB, ptr); } -Sandbox::Sandbox() : ruby(new struct w2c_ruby), wasi(new wasi_t(ruby, get_args())) { +Sandbox::Sandbox() : ruby(new struct w2c_ruby), wasi(new wasi_t(ruby)) { try { // Initialize the sandbox wasm_rt_init(); wasm2c_ruby_instantiate(RB, wasi.get()); w2c_ruby_mkxp_sandbox_init(RB); + // Determine Ruby command-line arguments + std::vector args{"mkxp-z"}; + args.push_back("-e "); + if (MJIT_ENABLED) { + std::string verboseLevel("--mjit-verbose="); + std::string maxCache("--mjit-max-cache="); + std::string minCalls("--mjit-min-calls="); + args.push_back("--mjit"); + verboseLevel += std::to_string(MJIT_VERBOSE); + maxCache += std::to_string(MJIT_MAX_CACHE); + minCalls += std::to_string(MJIT_MIN_CALLS); + args.push_back(verboseLevel.c_str()); + args.push_back(maxCache.c_str()); + args.push_back(minCalls.c_str()); + } else if (YJIT_ENABLED) { + args.push_back("--yjit"); + } + // Copy all the command-line arguments into the sandbox (sandboxed code can't access memory that's outside the sandbox!) - usize argv_buf = sandbox_malloc(wasi->args.size() * sizeof(usize)); - for (usize i = 0; i < wasi->args.size(); ++i) { - usize arg_buf = sandbox_malloc(std::strlen(wasi->args[i]) + 1); - std::strcpy((char *)WASM_MEM(arg_buf), wasi->args[i]); + usize argv_buf = sandbox_malloc(args.size() * sizeof(usize)); + for (usize i = 0; i < args.size(); ++i) { + usize arg_buf = sandbox_malloc(args[i].length() + 1); + std::strcpy((char *)WASM_MEM(arg_buf), args[i].c_str()); WASM_SET(usize, argv_buf + i * sizeof(usize), arg_buf); } usize sysinit_buf = sandbox_malloc(sizeof(usize) + sizeof(u32)); - WASM_SET(u32, sysinit_buf + sizeof(usize), wasi->args.size()); + WASM_SET(u32, sysinit_buf + sizeof(usize), args.size()); WASM_SET(usize, sysinit_buf, argv_buf); // Pass the command-line arguments to Ruby @@ -103,7 +101,7 @@ Sandbox::Sandbox() : ruby(new struct w2c_ruby), wasi(new wasi_t(ruby, get_args() AWAIT(w2c_ruby_ruby_init_stack(RB, ruby->w2c_0x5F_stack_pointer)); AWAIT(w2c_ruby_ruby_init(RB)); usize node; - AWAIT(node = w2c_ruby_ruby_options(RB, wasi->args.size(), argv_buf)); + AWAIT(node = w2c_ruby_ruby_options(RB, args.size(), argv_buf)); // Start up Ruby executable node bool valid; @@ -113,7 +111,7 @@ Sandbox::Sandbox() : ruby(new struct w2c_ruby), wasi(new wasi_t(ruby, get_args() if (valid) { AWAIT(state = w2c_ruby_ruby_exec_node(RB, WASM_GET(u32, state_buf))); } - if (state || !valid) { + if (!valid || state) { throw SandboxNodeException(); } sandbox_free(state_buf); diff --git a/src/sandbox/sandbox.h b/src/sandbox/sandbox.h index f4389193..ba7b028c 100644 --- a/src/sandbox/sandbox.h +++ b/src/sandbox/sandbox.h @@ -23,7 +23,6 @@ #define MKXPZ_SANDBOX_H #include -#include #include "types.h" typedef usize VALUE; @@ -41,7 +40,6 @@ struct Sandbox { std::shared_ptr ruby; std::unique_ptr wasi; - static std::vector get_args(); usize sandbox_malloc(usize size); void sandbox_free(usize ptr); diff --git a/src/sandbox/wasi.cpp b/src/sandbox/wasi.cpp index f9b5bf5b..8223eab2 100644 --- a/src/sandbox/wasi.cpp +++ b/src/sandbox/wasi.cpp @@ -48,9 +48,7 @@ namespace mkxp_retro { #define WASM_MEM(address) ((void *)&wasi->ruby->w2c_memory.data[address]) -wasi_t::w2c_wasi__snapshot__preview1(std::shared_ptr ruby, std::vector args) : ruby(ruby), args(args), dist_source(NULL), dist(NULL), argv_buf_size(0) { - for (unsigned int i = 0; i < args.size(); ++i) this->argv_buf_size += std::strlen(args[i]) + 1; - +wasi_t::w2c_wasi__snapshot__preview1(std::shared_ptr ruby) : ruby(ruby), dist_source(NULL), dist(NULL) { // Open the zip file for /mkxp-retro-dist dist_source = zip_source_buffer_create(mkxp_retro_dist_zip, mkxp_retro_dist_zip_len, 0, NULL); if (dist_source == NULL) { @@ -235,19 +233,13 @@ void wasi_t::deallocate_file_descriptor(u32 fd) { u32 w2c_wasi__snapshot__preview1_args_get(wasi_t *wasi, usize argv, usize argv_buf) { WASI_DEBUG("args_get()\n"); - for (unsigned int i = 0; i < wasi->args.size(); ++i) { - std::strcpy((char *)WASM_MEM(argv_buf), wasi->args[i]); - WASM_SET(usize, argv, argv_buf); - argv += sizeof(usize); - argv_buf += std::strlen(wasi->args[i]) + 1; - } return WASI_ESUCCESS; } u32 w2c_wasi__snapshot__preview1_args_sizes_get(wasi_t *wasi, usize argc, usize argv_buf_size) { WASI_DEBUG("args_sizes_get(0x%08x, 0x%08x)\n", argc, argv_buf_size); - WASM_SET(u32, argc, wasi->args.size()); - WASM_SET(u32, argv_buf_size, wasi->argv_buf_size); + WASM_SET(u32, argc, 0); + WASM_SET(u32, argv_buf_size, 0); return WASI_ESUCCESS; } diff --git a/src/sandbox/wasi.h b/src/sandbox/wasi.h index 8c3d1ba3..25578538 100644 --- a/src/sandbox/wasi.h +++ b/src/sandbox/wasi.h @@ -226,7 +226,6 @@ struct dist_stat_info { typedef struct w2c_wasi__snapshot__preview1 { std::shared_ptr ruby; - std::vector args; zip_source_t *dist_source; zip_t *dist; std::vector dist_path_cache; @@ -238,7 +237,7 @@ typedef struct w2c_wasi__snapshot__preview1 { // List of vacant WASI file descriptors so that we can reallocate vacant WASI file descriptors in O(1) amortized time. std::vector vacant_fds; - w2c_wasi__snapshot__preview1(std::shared_ptr ruby, std::vector args); + w2c_wasi__snapshot__preview1(std::shared_ptr ruby); ~w2c_wasi__snapshot__preview1(); struct dist_stat_info dist_stat(const char *path, u32 path_len); struct dist_stat_info dist_stat_entry(struct file_entry &entry);