mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-26 16:53:45 +02:00
Make sure stack_index
is initialized in binding-sandbox/binding-base.h
This field needs to be initialized when `struct frame` is constructed, or its value may be random. I also changed the type of `stack_index` to `wasm_size_t` so that it's the same size on every platform.
This commit is contained in:
parent
ecfaa4eebf
commit
b8fb59e558
2 changed files with 9 additions and 4 deletions
|
@ -20,7 +20,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "binding-base.h"
|
#include "binding-base.h"
|
||||||
#include "mkxp-polyfill.h"
|
|
||||||
|
|
||||||
using namespace mkxp_sandbox;
|
using namespace mkxp_sandbox;
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#include <boost/asio/coroutine.hpp>
|
#include <boost/asio/coroutine.hpp>
|
||||||
#include <mkxp-sandbox-ruby.h>
|
#include <mkxp-sandbox-ruby.h>
|
||||||
#include "wasm-types.h"
|
#include "wasm-types.h"
|
||||||
|
#include "mkxp-polyfill.h"
|
||||||
|
|
||||||
// LLVM uses a stack alignment of 16 on WebAssembly targets
|
// LLVM uses a stack alignment of 16 on WebAssembly targets
|
||||||
#define WASMSTACKALIGN 16
|
#define WASMSTACKALIGN 16
|
||||||
|
@ -221,8 +222,8 @@ namespace mkxp_sandbox {
|
||||||
|
|
||||||
struct fiber {
|
struct fiber {
|
||||||
key_t key;
|
key_t key;
|
||||||
|
wasm_size_t stack_index;
|
||||||
std::vector<struct stack_frame> stack;
|
std::vector<struct stack_frame> stack;
|
||||||
size_t stack_index;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct object {
|
struct object {
|
||||||
|
@ -326,7 +327,7 @@ namespace mkxp_sandbox {
|
||||||
bind.ref<wasm_ptr_t>(bind.instance().w2c_mkxp_sandbox_fiber_arg1),
|
bind.ref<wasm_ptr_t>(bind.instance().w2c_mkxp_sandbox_fiber_arg1),
|
||||||
};
|
};
|
||||||
if (bind.fibers.count(key) == 0) {
|
if (bind.fibers.count(key) == 0) {
|
||||||
bind.fibers[key] = (struct fiber){.key = key};
|
bind.fibers[key] = (struct fiber){.key = key, .stack_index = 0};
|
||||||
}
|
}
|
||||||
return bind.fibers[key];
|
return bind.fibers[key];
|
||||||
}
|
}
|
||||||
|
@ -352,6 +353,9 @@ namespace mkxp_sandbox {
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
struct stack_frame &frame = fiber->stack[fiber->stack_index++];
|
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;
|
b.stack_ptr = frame.stack_ptr;
|
||||||
coroutine = (T *)frame.coroutine;
|
coroutine = (T *)frame.coroutine;
|
||||||
return;
|
return;
|
||||||
|
@ -363,7 +367,9 @@ namespace mkxp_sandbox {
|
||||||
bind->stack_ptr = fiber->stack.back().stack_ptr;
|
bind->stack_ptr = fiber->stack.back().stack_ptr;
|
||||||
fiber->stack.pop_back();
|
fiber->stack.pop_back();
|
||||||
}
|
}
|
||||||
++fiber->stack_index;
|
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<T>::value);
|
b.stack_ptr = w2c_ruby_rb_wasm_get_stack_pointer(&b.instance()) - CEIL_WASMSTACKALIGN(declared_slots_size<T>::value);
|
||||||
assert(b.stack_ptr % sizeof(VALUE) == 0);
|
assert(b.stack_ptr % sizeof(VALUE) == 0);
|
||||||
assert(b.stack_ptr % WASMSTACKALIGN == 0);
|
assert(b.stack_ptr % WASMSTACKALIGN == 0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue