/* ** binding-base.h ** ** This file is part of mkxp. ** ** Copyright (C) 2013 - 2021 Amaryllis Kulla ** ** 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 . */ #ifndef MKXPZ_SANDBOX_BINDING_BASE_H #define MKXPZ_SANDBOX_BINDING_BASE_H #include #include #include #include #include #include #include #include #include #include #include #include #include "wasm-types.h" #include "mkxp-polyfill.h" // LLVM uses a stack alignment of 16 on WebAssembly targets #define WASMSTACKALIGN 16 // Rounds a number up to the nearest multiple of the WebAssembly stack alignment #define CEIL_WASMSTACKALIGN(x) (((wasm_size_t)(x) + (wasm_size_t)(WASMSTACKALIGN - 1)) & ~(wasm_size_t)(WASMSTACKALIGN - 1)) namespace mkxp_sandbox { template struct decl_slots {}; template struct get_num_slots; template <> struct get_num_slots> { static constexpr wasm_size_t value = 0; }; template struct get_num_slots> { static constexpr wasm_size_t value = 1 + get_num_slots>::value; }; // typename concat_slots, decl_slots>::type -> decl_slots template struct concat_slots; template struct concat_slots, struct decl_slots> { using type = decl_slots; }; // typename get_last_slot>::type -> xn template struct get_last_slot; template struct get_last_slot> { using type = Tail; }; template struct get_last_slot> { using type = typename get_last_slot>::type; }; // typename pop_last_slot>::type -> decl_slots template struct pop_last_slot; template struct pop_last_slot> { using type = decl_slots<>; }; template struct pop_last_slot> { using type = typename concat_slots, typename pop_last_slot>::type>::type; }; // `slot_type::type` is the type of the `i`th slot. // For example: // typedef decl_slots slots; // slot_type<0, slots>::type var0; // this variable should be of type `uint64_t` // slot_type<1, slots>::type var1; // this variable should be of type `uint32_t` // slot_type<2, slots>::type var2; // this variable should be of type `uint16_t` // slot_type<3, slots>::type var3; // this variable should be of type `uint8_t` template struct slot_type; template struct slot_type<0, struct decl_slots> { static_assert(std::is_integral::value || std::is_floating_point::value, "slots must have numeric types"); typedef Head type; }; template struct slot_type> : slot_type> {}; // `slots_size::value` is the total number of bytes required to store all the slots, including padding bytes between the slots but not including padding bytes after the last slot. // For example: // typedef decl_slots slots; // constexpr wasm_size_t size = slots_size::value; // should be 15 template struct slots_size; template <> struct slots_size> { static constexpr wasm_size_t value = 0; }; template struct slots_size> { static_assert(std::is_integral>::type>::value || std::is_floating_point>::type>::value, "slots must have numeric types"); private: static constexpr wasm_size_t last_size = sizeof(typename get_last_slot>::type); static constexpr wasm_size_t rest_size = slots_size>::type>::value; static constexpr wasm_size_t rest_size_aligned_to_last_size = (rest_size - 1 + last_size) / last_size * last_size; public: static constexpr wasm_size_t value = rest_size_aligned_to_last_size + last_size; }; template struct slot_offset_nothrow; template struct slot_offset_nothrow> { static constexpr wasm_size_t value = 0; }; template struct slot_offset_nothrow> { static constexpr wasm_size_t value = get_num_slots>::value <= Index ? slots_size>::value : slot_offset_nothrow>::type>::value; }; // `slot_offset::value` is the byte offset of the `i`th slot. // For example: // typedef decl_slots slots; // constexpr wasm_size_t slot0_offset = slot_offset<0, slots>::value; // should be 0 // constexpr wasm_size_t slot1_offset = slot_offset<1, slots>::value; // should be 8 // constexpr wasm_size_t slot2_offset = slot_offset<2, slots>::value; // should be 12 // constexpr wasm_size_t slot3_offset = slot_offset<3, slots>::value; // should be 14 template struct slot_offset; template struct slot_offset> { static_assert(Index < get_num_slots>::value, "index out of range"); static constexpr wasm_size_t value = slot_offset_nothrow>::value; }; // If the type `T::slots` exists, // then `declared_slots_size::value` is equal to `slots_size::value` (i.e. the total size of the slots used by `T`). // Otherwise, it's equal to 0. template struct declared_slots_size; template using slots_declaration = typename T::slots; template struct declared_slots_size::value>::type> { static constexpr wasm_size_t value = slots_size::value; }; template struct declared_slots_size::value>::type> { static constexpr wasm_size_t value = 0; }; // Gets a pointer to the given address in sandbox memory. void *sandbox_ptr(struct w2c_ruby &instance, wasm_ptr_t address) noexcept; // Gets a reference to the value stored at a given address in sandbox memory. template T &sandbox_ref(struct w2c_ruby &instance, wasm_ptr_t address) noexcept { static_assert(std::is_integral::value || std::is_floating_point::value, "can only get references to numeric values in the sandbox"); #ifdef MKXPZ_BIG_ENDIAN return *(T *)((uint8_t *)sandbox_ptr(instance, address) - sizeof(T)); #else return *(T *)sandbox_ptr(instance, address); #endif // MKXPZ_BIG_ENDIAN } // Gets a reference to the value stored at the given index in the array at a given address in sandbox memory. template T &sandbox_ref(struct w2c_ruby &instance, wasm_ptr_t array_address, wasm_size_t array_index) noexcept { return sandbox_ref(array_address + array_index * sizeof(T)); } // Gets the length of a string stored at a given address in sandbox memory. wasm_size_t sandbox_strlen(struct w2c_ruby &instance, wasm_ptr_t address) noexcept; // Gets a string stored at a given address in sandbox memory. // The returned string doesn't need to be freed but only lives until the next call to this function, // so you need to store the returned string in a buffer somewhere if you need to get more than one. const char *sandbox_str(struct w2c_ruby &instance, wasm_ptr_t address) noexcept; // Copies a string into a sandbox memory address. void sandbox_strcpy(struct w2c_ruby &instance, wasm_ptr_t dst_address, const char *src) noexcept; // Copies a string into a sandbox memory address. void sandbox_strncpy(struct w2c_ruby &instance, wasm_ptr_t dst_address, const char *src, wasm_size_t max_size) noexcept; // Copies an array of length `num_elements` into a sandbox memory address. template void sandbox_arycpy(struct w2c_ruby &instance, wasm_ptr_t dst_address, const T *src, wasm_size_t num_elements) noexcept { #ifdef MKXPZ_BIG_ENDIAN T *dst = (T *)sandbox_ptr(instance, dst_address); while (num_elements > 0) { if ((uint8_t *)dst - instance.w2c_memory.data < sizeof(T)) { std::abort(); } *--dst = *src++; --num_elements; } #else if (instance.w2c_memory.size - dst_address < num_elements * sizeof(T)) { std::abort(); } T *dst = (T *)sandbox_ptr(instance, dst_address); std::memcpy(dst, src, num_elements * sizeof(T)); #endif } struct typenum_table_entry { void (*destructor)(void *); }; extern const struct typenum_table_entry typenum_table[]; extern const wasm_size_t typenum_table_size; struct binding_base { private: typedef std::tuple key_t; struct stack_frame { void *coroutine; void (*destructor)(void *coroutine); wasm_ptr_t stack_ptr; stack_frame(void *coroutine, void (*destructor)(void *coroutine), wasm_ptr_t stack_ptr); stack_frame(const struct stack_frame &frame) = delete; stack_frame(struct stack_frame &&frame) noexcept; struct stack_frame &operator=(const struct stack_frame &frame) = delete; struct stack_frame &operator=(struct stack_frame &&frame) noexcept; ~stack_frame(); }; struct fiber { key_t key; wasm_size_t stack_index; std::vector stack; }; struct object { // If this is a free object, this is 0. // Otherwise, this is a number corresponding to the type of the object. wasm_size_t typenum; // If this is a free object, the `next` field is the key of the next free object, or 0 if this is the last free object. // Otherwise, the `ptr` field is a pointer to the actual object. union inner { wasm_size_t next; void *ptr; } inner; object(wasm_size_t typenum, void *ptr); object(const struct object &object) = delete; object(struct object &&object) noexcept; struct object &operator=(const struct object &object) = delete; struct object &operator=(struct object &&object) noexcept; ~object(); }; std::shared_ptr _instance; std::unordered_map> fibers; std::vector objects; wasm_objkey_t next_free_objkey; wasm_ptr_t stack_ptr; public: binding_base(std::shared_ptr m); ~binding_base(); struct w2c_ruby &instance() const noexcept; wasm_ptr_t sandbox_malloc(wasm_size_t); void sandbox_free(wasm_ptr_t ptr); wasm_ptr_t rtypeddata_data(VALUE obj) const noexcept; void rtypeddata_dmark(wasm_ptr_t data, wasm_ptr_t ptr); void rtypeddata_dfree(wasm_ptr_t data, wasm_ptr_t ptr); wasm_size_t rtypeddata_dsize(wasm_ptr_t data, wasm_ptr_t ptr); void rtypeddata_dcompact(wasm_ptr_t data, wasm_ptr_t ptr); // Gets a pointer to the given address in sandbox memory. void *ptr(wasm_ptr_t address) const noexcept; // Gets a reference to the value stored at a given address in sandbox memory. template T &ref(wasm_ptr_t address) const noexcept { return sandbox_ref(instance(), address); } // Gets a reference to the value stored at the given index in the array at a given address in sandbox memory. template T &ref(wasm_ptr_t array_address, wasm_size_t array_index) const noexcept { return ref(array_address + array_index * sizeof(T)); } // Gets the length of a string stored at a given address in sandbox memory. wasm_size_t strlen(wasm_ptr_t address) const noexcept; // Gets a string stored at a given address in sandbox memory. // The returned string doesn't need to be freed but only lives until the next call to this function, // so you need to store the returned string in a buffer somewhere if you need to get more than one. const char *str(wasm_ptr_t address) const noexcept; // Copies a string into a sandbox memory address. void strcpy(wasm_ptr_t dst_address, const char *src) const noexcept; // Copies a string into a sandbox memory address. void strncpy(wasm_ptr_t dst_address, const char *src, wasm_size_t max_size) const noexcept; // Copies an array of length `num_elements` into a sandbox memory address. template void arycpy(wasm_ptr_t dst_address, const T *src, wasm_size_t num_elements) const noexcept { return sandbox_arycpy(instance(), dst_address, src, num_elements); } // Creates a new object and returns its key. wasm_objkey_t create_object(wasm_size_t typenum, void *ptr); // Gets the object with the given key. void *get_object(wasm_objkey_t key); // Returns true if the typenum of the object with the given key matches the given typenum, otherwise false. bool check_object_type(wasm_objkey_t key, wasm_size_t typenum); // Destroys the object with the given key, calling its destructor and freeing its key for use by future objects. void destroy_object(wasm_objkey_t key); template struct stack_frame_guard { static_assert(std::is_base_of::value, "`T` must be a subclass of `boost::asio::coroutine`"); friend struct binding_base; private: T *coroutine; struct binding_base *bind; struct fiber *fiber; static void coroutine_destructor(void *coroutine) { ((T *)coroutine)->~T(); } static struct fiber &init_fiber(struct binding_base &bind) { key_t key = { bind.ref(bind.instance().w2c_mkxp_sandbox_fiber_entry_point), bind.ref(bind.instance().w2c_mkxp_sandbox_fiber_arg0), bind.ref(bind.instance().w2c_mkxp_sandbox_fiber_arg1), }; if (bind.fibers.count(key) == 0) { bind.fibers[key] = (struct fiber){.key = key, .stack_index = 0}; } return bind.fibers[key]; } template static typename std::enable_if::value, U *>::type construct_frame(struct binding_base &bind) { return new U(bind); } template static typename std::enable_if::value, U *>::type construct_frame(struct binding_base &bind) { return new U; } stack_frame_guard(struct binding_base &b) : bind(&b), fiber(&init_fiber(b)) { uint32_t state = w2c_ruby_asyncify_get_state(&b.instance()); if (fiber->stack_index > fiber->stack.size()) { std::abort(); } // If Asyncify is rewinding, restore the stack frame from before Asyncify started unwinding if (state == 2) { if (fiber->stack_index == fiber->stack.size()) { std::abort(); } struct stack_frame &frame = fiber->stack[fiber->stack_index++]; if (fiber->stack_index == 0) { MKXPZ_THROW(std::bad_alloc()); } b.stack_ptr = frame.stack_ptr; coroutine = (T *)frame.coroutine; return; } // Otherwise, create a new stack frame assert(state == 0); while (fiber->stack.size() > fiber->stack_index) { bind->stack_ptr = fiber->stack.back().stack_ptr; fiber->stack.pop_back(); } if (++fiber->stack_index == 0) { MKXPZ_THROW(std::bad_alloc()); } b.stack_ptr = w2c_ruby_rb_wasm_get_stack_pointer(&b.instance()) - CEIL_WASMSTACKALIGN(declared_slots_size::value); assert(b.stack_ptr % sizeof(VALUE) == 0); assert(b.stack_ptr % WASMSTACKALIGN == 0); if (declared_slots_size::value != 0) { w2c_ruby_rb_wasm_set_stack_pointer(&b.instance(), b.stack_ptr); } coroutine = construct_frame(b); fiber->stack.emplace_back( coroutine, coroutine_destructor, b.stack_ptr ); } public: stack_frame_guard(const stack_frame_guard &frame) = delete; stack_frame_guard(stack_frame_guard &&frame) noexcept : coroutine(std::exchange(frame.coroutine, nullptr)), bind(std::exchange(frame.bind, nullptr)), fiber(std::exchange(frame.fiber, nullptr)) {} struct stack_frame_guard &operator=(const stack_frame_guard &frame) = delete; struct stack_frame_guard &operator=(stack_frame_guard &&frame) noexcept { coroutine = std::exchange(frame.coroutine, nullptr); bind = std::exchange(frame.bind, nullptr); fiber = std::exchange(frame.fiber, nullptr); return *this; } ~stack_frame_guard() { if (fiber == nullptr) { return; } assert(fiber->stack_index > 0); assert(fiber->stack_index - 1 < fiber->stack.size()); if (get()->is_complete()) { while (fiber->stack.size() > fiber->stack_index) { bind->stack_ptr = fiber->stack.back().stack_ptr; fiber->stack.pop_back(); } assert(fiber->stack.size() == fiber->stack_index); w2c_ruby_rb_wasm_set_stack_pointer(&bind->instance(), fiber->stack.back().stack_ptr + CEIL_WASMSTACKALIGN(declared_slots_size::value)); bind->stack_ptr = fiber->stack.back().stack_ptr; fiber->stack.pop_back(); } if (--fiber->stack_index > 0) { bind->stack_ptr = fiber->stack[fiber->stack_index - 1].stack_ptr; } if (fiber->stack.empty()) { bind->fibers.erase(fiber->key); } } inline T *get() const noexcept { return coroutine; } inline T &operator()() const noexcept { return *get(); } }; template struct stack_frame_guard bind() { return *this; } wasm_ptr_t stack_pointer() const noexcept { return stack_ptr; } }; } #endif // MKXPZ_SANDBOX_BINDING_BASE