mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-23 15:23:44 +02:00

To stop Ruby's garbage collector from freeing Ruby `VALUE`s while we're in the middle of using them in libretro builds, we need to make sure all the `VALUE`s we use are on the sandbox's stack. Also, to allow Ruby to recognize `VALUE`s on the sandbox's stack on big-endian targets, I've changed the serialization of `VALUE`s. Before, any `VALUE`s returned by a sandbox function were always converted to the target's endian, and any `VALUE`s passed to sandbox functions as argument were then converted back to WebAssembly's endianness, little-endian. Now, `VALUE`s are always little-endian; they are no longer converted to the target's endianness. That should be fine since `VALUE`s are supposed to be opaque values.
80 lines
2.5 KiB
C++
80 lines
2.5 KiB
C++
/*
|
|
** sandbox.h
|
|
**
|
|
** This file is part of mkxp.
|
|
**
|
|
** Copyright (C) 2013 - 2021 Amaryllis Kulla <ancurio@mapleshrine.eu>
|
|
**
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
** it under the terms of the GNU General Public License as published by
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
** (at your option) any later version.
|
|
**
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
** GNU General Public License for more details.
|
|
**
|
|
** You should have received a copy of the GNU General Public License
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef MKXPZ_SANDBOX_H
|
|
#define MKXPZ_SANDBOX_H
|
|
|
|
#include <memory>
|
|
#include <mkxp-sandbox-bindgen.h>
|
|
#include "types.h"
|
|
|
|
#define SANDBOX_COROUTINE(name, definition) struct name : boost::asio::coroutine { BOOST_TYPE_INDEX_REGISTER_CLASS inline name(struct mkxp_sandbox::bindings &bind) {} definition };
|
|
|
|
#define SANDBOX_AWAIT(coroutine, ...) \
|
|
do { \
|
|
{ \
|
|
mkxp_sandbox::bindings::stack_frame_guard<struct coroutine> frame = mkxp_sandbox::sandbox->bindings.bind<struct coroutine>(); \
|
|
frame()(__VA_ARGS__); \
|
|
if (frame().is_complete()) break; \
|
|
} \
|
|
yield; \
|
|
} while (1)
|
|
|
|
#define SANDBOX_AWAIT_AND_SET(variable, coroutine, ...) \
|
|
do { \
|
|
{ \
|
|
mkxp_sandbox::bindings::stack_frame_guard<struct coroutine> frame = mkxp_sandbox::sandbox->bindings.bind<struct coroutine>(); \
|
|
auto ret = frame()(__VA_ARGS__); \
|
|
if (frame().is_complete()) { \
|
|
variable = ret; \
|
|
break; \
|
|
} \
|
|
} \
|
|
yield; \
|
|
} while (1)
|
|
|
|
namespace mkxp_sandbox {
|
|
struct sandbox {
|
|
private:
|
|
std::shared_ptr<struct w2c_ruby> ruby;
|
|
std::unique_ptr<struct w2c_wasi__snapshot__preview1> wasi;
|
|
usize sandbox_malloc(usize size);
|
|
void sandbox_free(usize ptr);
|
|
|
|
public:
|
|
struct mkxp_sandbox::bindings bindings;
|
|
sandbox(const char *game_path);
|
|
~sandbox();
|
|
|
|
template <typename T> inline void run() {
|
|
T coroutine = T();
|
|
for (;;) {
|
|
coroutine();
|
|
if (coroutine.is_complete()) break;
|
|
w2c_ruby_mkxp_sandbox_yield(ruby.get());
|
|
}
|
|
}
|
|
};
|
|
|
|
extern std::unique_ptr<struct mkxp_sandbox::sandbox> sandbox;
|
|
}
|
|
|
|
#endif // MKXPZ_SANDBOX_H
|