From 12102d101af258d7a3e9695b736a189cd3658df1 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Wed, 6 Sep 2023 14:20:23 -0400 Subject: Fix crash in WeakMap during compaction WeakMap can crash during compaction because the st_insert could allocate memory. --- internal/gc.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'internal') diff --git a/internal/gc.h b/internal/gc.h index 28b82f4196..f8f88a41cb 100644 --- a/internal/gc.h +++ b/internal/gc.h @@ -189,6 +189,17 @@ struct rb_objspace; /* in vm_core.h */ # define SIZE_POOL_COUNT 5 #endif +/* Used in places that could malloc during, which can cause the GC to run. We + * need to temporarily disable the GC to allow the malloc to happen. + * Allocating memory during GC is a bad idea, so use this only when absolutely + * necessary. */ +#define DURING_GC_COULD_MALLOC_REGION_START() \ + assert(rb_during_gc()); \ + VALUE _already_disabled = rb_gc_disable_no_rest() + +#define DURING_GC_COULD_MALLOC_REGION_END() \ + if (_already_disabled == Qfalse) rb_gc_enable() + typedef struct ractor_newobj_size_pool_cache { struct RVALUE *freelist; struct heap_page *using_page; -- cgit v1.2.3