aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIan Ker-Seymer <i.kerseymer@gmail.com>2023-05-04 02:48:34 -0400
committerGitHub <noreply@github.com>2023-05-04 08:48:34 +0200
commit2f9f44f077a53b14aa1fbd43111955251750d31f (patch)
treef7d0a3e153ab753702a57f3efdffc6bf3e2e8586
parente135a21a85f658cea380df47dca16f5e90c971cf (diff)
downloadruby-2f9f44f077a53b14aa1fbd43111955251750d31f.tar.gz
Ensure the VM is alive before accessing objspace in C API (Feature #19627)
[Feature #19627]
-rw-r--r--gc.c12
1 files 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);
}