Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6874b28

Browse files
white-axekateinoigakukun
authored andcommitted
Free jump buffers leaked by cont_restore_thread in WASI builds
1 parent 45e814d commit 6874b28

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

cont.c

+45
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,51 @@ cont_restore_thread(rb_context_t *cont)
15111511
rb_raise(rb_eRuntimeError, "can't call across trace_func");
15121512
}
15131513

1514+
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
1515+
if (th->ec->tag != sec->tag) {
1516+
/* find the lowest common ancestor tag of the current EC and the saved EC */
1517+
1518+
struct rb_vm_tag *lowest_common_ancestor = NULL;
1519+
size_t num_tags = 0;
1520+
size_t num_saved_tags = 0;
1521+
for (struct rb_vm_tag *tag = th->ec->tag; tag != NULL; tag = tag->prev) {
1522+
++num_tags;
1523+
}
1524+
for (struct rb_vm_tag *tag = sec->tag; tag != NULL; tag = tag->prev) {
1525+
++num_saved_tags;
1526+
}
1527+
1528+
size_t min_tags = num_tags <= num_saved_tags ? num_tags : num_saved_tags;
1529+
1530+
struct rb_vm_tag *tag = th->ec->tag;
1531+
while (num_tags > min_tags) {
1532+
tag = tag->prev;
1533+
--num_tags;
1534+
}
1535+
1536+
struct rb_vm_tag *saved_tag = sec->tag;
1537+
while (num_saved_tags > min_tags) {
1538+
saved_tag = saved_tag->prev;
1539+
--num_saved_tags;
1540+
}
1541+
1542+
while (min_tags > 0) {
1543+
if (tag == saved_tag) {
1544+
lowest_common_ancestor = tag;
1545+
break;
1546+
}
1547+
tag = tag->prev;
1548+
saved_tag = saved_tag->prev;
1549+
--min_tags;
1550+
}
1551+
1552+
/* free all the jump buffers between the current EC's tag and the lowest common ancestor tag */
1553+
for (struct rb_vm_tag *tag = th->ec->tag; tag != lowest_common_ancestor; tag = tag->prev) {
1554+
rb_vm_tag_jmpbuf_deinit(&tag->buf);
1555+
}
1556+
}
1557+
#endif
1558+
15141559
/* copy vm stack */
15151560
#ifdef CAPTURE_JUST_VALID_VM_STACK
15161561
MEMCPY(th->ec->vm_stack,

0 commit comments

Comments
 (0)