Remove unnecessary args_get implementation

This commit is contained in:
刘皓 2024-12-30 13:58:38 -05:00
parent 4e15b7b2d0
commit d5de2c0c25
No known key found for this signature in database
GPG key ID: 7901753DB465B711
4 changed files with 30 additions and 43 deletions

View file

@ -44,26 +44,6 @@ extern "C" void mkxp_sandbox_trap_handler(wasm_rt_trap_t code) {
throw SandboxTrapException();
}
std::vector<const char *> Sandbox::get_args() {
std::vector<const char *> 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<std::string> 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);

View file

@ -23,7 +23,6 @@
#define MKXPZ_SANDBOX_H
#include <memory>
#include <vector>
#include "types.h"
typedef usize VALUE;
@ -41,7 +40,6 @@ struct Sandbox {
std::shared_ptr<struct w2c_ruby> ruby;
std::unique_ptr<struct w2c_wasi__snapshot__preview1> wasi;
static std::vector<const char *> get_args();
usize sandbox_malloc(usize size);
void sandbox_free(usize ptr);

View file

@ -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<struct w2c_ruby> ruby, std::vector<const char *> 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<struct w2c_ruby> 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;
}

View file

@ -226,7 +226,6 @@ struct dist_stat_info {
typedef struct w2c_wasi__snapshot__preview1 {
std::shared_ptr<struct w2c_ruby> ruby;
std::vector<const char *> args;
zip_source_t *dist_source;
zip_t *dist;
std::vector<dist_path_entry_t> 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<u32> vacant_fds;
w2c_wasi__snapshot__preview1(std::shared_ptr<struct w2c_ruby> ruby, std::vector<const char *> args);
w2c_wasi__snapshot__preview1(std::shared_ptr<struct w2c_ruby> 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);