From 2f9f44f077a53b14aa1fbd43111955251750d31f Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Thu, 4 May 2023 02:48:34 -0400 Subject: Ensure the VM is alive before accessing objspace in C API (Feature #19627) [Feature #19627] --- gc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gc.c b/gc.c index 3fcd4ef67b..db6fa9c861 100644 --- a/gc.c +++ b/gc.c @@ -1007,6 +1007,11 @@ asan_unlock_freelist(struct heap_page *page) /* Aliases */ #define rb_objspace (*rb_objspace_of(GET_VM())) #define rb_objspace_of(vm) ((vm)->objspace) +#define unless_objspace(objspace) \ + rb_objspace_t *objspace; \ + rb_vm_t *unless_objspace_vm = GET_VM(); \ + if (unless_objspace_vm) objspace = unless_objspace_vm->objspace; \ + else /* return; or objspace will be warned uninitialized */ #define ruby_initial_gc_stress gc_params.gc_stress @@ -10914,7 +10919,7 @@ rb_gc_start(void) void rb_gc(void) { - rb_objspace_t *objspace = &rb_objspace; + unless_objspace(objspace) { return; } unsigned int reason = GPR_DEFAULT_REASON; garbage_collect(objspace, reason); } @@ -10922,7 +10927,7 @@ rb_gc(void) int rb_during_gc(void) { - rb_objspace_t *objspace = &rb_objspace; + unless_objspace(objspace) { return FALSE; } return during_gc; } @@ -12740,7 +12745,8 @@ gc_malloc_allocations(VALUE self) void rb_gc_adjust_memory_usage(ssize_t diff) { - rb_objspace_t *objspace = &rb_objspace; + unless_objspace(objspace) { return; } + if (diff > 0) { objspace_malloc_increase(objspace, 0, diff, 0, MEMOP_TYPE_REALLOC); } -- cgit v1.2.3