mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-23 07:13:44 +02:00
86 lines
3.2 KiB
Diff
86 lines
3.2 KiB
Diff
# Fixes a memory leak in WASI builds of Ruby where VM jump buffers are sometimes not freed.
|
|
|
|
--- a/cont.c
|
|
+++ b/cont.c
|
|
@@ -1518,6 +1518,51 @@ cont_restore_thread(rb_context_t *cont)
|
|
rb_raise(rb_eRuntimeError, "can't call across trace_func");
|
|
}
|
|
|
|
+#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
|
|
+ if (th->ec->tag != sec->tag) {
|
|
+ /* find the lowest common ancestor tag of the current EC and the saved EC */
|
|
+
|
|
+ struct rb_vm_tag *lowest_common_ancestor = NULL;
|
|
+ size_t num_tags = 0;
|
|
+ size_t num_saved_tags = 0;
|
|
+ for (struct rb_vm_tag *tag = th->ec->tag; tag != NULL; tag = tag->prev) {
|
|
+ ++num_tags;
|
|
+ }
|
|
+ for (struct rb_vm_tag *tag = sec->tag; tag != NULL; tag = tag->prev) {
|
|
+ ++num_saved_tags;
|
|
+ }
|
|
+
|
|
+ size_t min_tags = num_tags <= num_saved_tags ? num_tags : num_saved_tags;
|
|
+
|
|
+ struct rb_vm_tag *tag = th->ec->tag;
|
|
+ while (num_tags > min_tags) {
|
|
+ tag = tag->prev;
|
|
+ --num_tags;
|
|
+ }
|
|
+
|
|
+ struct rb_vm_tag *saved_tag = sec->tag;
|
|
+ while (num_saved_tags > min_tags) {
|
|
+ saved_tag = saved_tag->prev;
|
|
+ --num_saved_tags;
|
|
+ }
|
|
+
|
|
+ while (min_tags > 0) {
|
|
+ if (tag == saved_tag) {
|
|
+ lowest_common_ancestor = tag;
|
|
+ break;
|
|
+ }
|
|
+ tag = tag->prev;
|
|
+ saved_tag = saved_tag->prev;
|
|
+ --min_tags;
|
|
+ }
|
|
+
|
|
+ /* free all the jump buffers between the current EC's tag and the lowest common ancestor tag */
|
|
+ for (struct rb_vm_tag *tag = th->ec->tag; tag != lowest_common_ancestor; tag = tag->prev) {
|
|
+ rb_vm_tag_jmpbuf_deinit(&tag->buf);
|
|
+ }
|
|
+ }
|
|
+#endif
|
|
+
|
|
/* copy vm stack */
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
|
MEMCPY(th->ec->vm_stack,
|
|
--- a/signal.c
|
|
+++ b/signal.c
|
|
@@ -849,6 +849,7 @@ check_stack_overflow(int sig, const uintptr_t addr, const ucontext_t *ctx)
|
|
* otherwise it can cause stack overflow again at the same
|
|
* place. */
|
|
if ((crit = (!ec->tag->prev || !--uplevel)) != FALSE) break;
|
|
+ rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
|
|
ec->tag = ec->tag->prev;
|
|
}
|
|
reset_sigmask(sig);
|
|
--- a/vm.c
|
|
+++ b/vm.c
|
|
@@ -2729,6 +2729,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state, V
|
|
if (VM_FRAME_FINISHED_P(ec->cfp)) {
|
|
rb_vm_pop_frame(ec);
|
|
ec->errinfo = (VALUE)err;
|
|
+ rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
|
|
ec->tag = ec->tag->prev;
|
|
EC_JUMP_TAG(ec, state);
|
|
}
|
|
--- a/vm_trace.c
|
|
+++ b/vm_trace.c
|
|
@@ -455,6 +455,7 @@ rb_exec_event_hooks(rb_trace_arg_t *trace_arg, rb_hook_list_t *hooks, int pop_p)
|
|
if (state) {
|
|
if (pop_p) {
|
|
if (VM_FRAME_FINISHED_P(ec->cfp)) {
|
|
+ rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
|
|
ec->tag = ec->tag->prev;
|
|
}
|
|
rb_vm_pop_frame(ec);
|