Ruby 3.5.0dev (2025-08-16 revision 11c8bad64b31c125b903547bb3eed0ede8f0f8e2)
gc.c (11c8bad64b31c125b903547bb3eed0ede8f0f8e2)
1/**********************************************************************
2
3 gc.c -
4
5 $Author$
6 created at: Tue Oct 5 09:44:46 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#define rb_data_object_alloc rb_data_object_alloc
15#define rb_data_typed_object_alloc rb_data_typed_object_alloc
16
17#include "ruby/internal/config.h"
18#ifdef _WIN32
19# include "ruby/ruby.h"
20#endif
21
22#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
23# include "wasm/setjmp.h"
24# include "wasm/machine.h"
25#else
26# include <setjmp.h>
27#endif
28#include <stdarg.h>
29#include <stdio.h>
30
31/* MALLOC_HEADERS_BEGIN */
32#ifndef HAVE_MALLOC_USABLE_SIZE
33# ifdef _WIN32
34# define HAVE_MALLOC_USABLE_SIZE
35# define malloc_usable_size(a) _msize(a)
36# elif defined HAVE_MALLOC_SIZE
37# define HAVE_MALLOC_USABLE_SIZE
38# define malloc_usable_size(a) malloc_size(a)
39# endif
40#endif
41
42#ifdef HAVE_MALLOC_USABLE_SIZE
43# ifdef RUBY_ALTERNATIVE_MALLOC_HEADER
44/* Alternative malloc header is included in ruby/missing.h */
45# elif defined(HAVE_MALLOC_H)
46# include <malloc.h>
47# elif defined(HAVE_MALLOC_NP_H)
48# include <malloc_np.h>
49# elif defined(HAVE_MALLOC_MALLOC_H)
50# include <malloc/malloc.h>
51# endif
52#endif
53
54/* MALLOC_HEADERS_END */
55
56#ifdef HAVE_SYS_TIME_H
57# include <sys/time.h>
58#endif
59
60#ifdef HAVE_SYS_RESOURCE_H
61# include <sys/resource.h>
62#endif
63
64#if defined _WIN32 || defined __CYGWIN__
65# include <windows.h>
66#elif defined(HAVE_POSIX_MEMALIGN)
67#elif defined(HAVE_MEMALIGN)
68# include <malloc.h>
69#endif
70
71#include <sys/types.h>
72
73#ifdef __EMSCRIPTEN__
74#include <emscripten.h>
75#endif
76
77/* For ruby_annotate_mmap */
78#ifdef HAVE_SYS_PRCTL_H
79#include <sys/prctl.h>
80#endif
81
82#undef LIST_HEAD /* ccan/list conflicts with BSD-origin sys/queue.h. */
83
84#include "constant.h"
85#include "darray.h"
86#include "debug_counter.h"
87#include "eval_intern.h"
88#include "gc/gc.h"
89#include "id_table.h"
90#include "internal.h"
91#include "internal/class.h"
92#include "internal/compile.h"
93#include "internal/complex.h"
94#include "internal/concurrent_set.h"
95#include "internal/cont.h"
96#include "internal/error.h"
97#include "internal/eval.h"
98#include "internal/gc.h"
99#include "internal/hash.h"
100#include "internal/imemo.h"
101#include "internal/io.h"
102#include "internal/numeric.h"
103#include "internal/object.h"
104#include "internal/proc.h"
105#include "internal/rational.h"
106#include "internal/sanitizers.h"
107#include "internal/struct.h"
108#include "internal/symbol.h"
109#include "internal/thread.h"
110#include "internal/variable.h"
111#include "internal/warnings.h"
112#include "probes.h"
113#include "regint.h"
114#include "ruby/debug.h"
115#include "ruby/io.h"
116#include "ruby/re.h"
117#include "ruby/st.h"
118#include "ruby/thread.h"
119#include "ruby/util.h"
120#include "ruby/vm.h"
121#include "ruby_assert.h"
122#include "ruby_atomic.h"
123#include "symbol.h"
124#include "variable.h"
125#include "vm_core.h"
126#include "vm_sync.h"
127#include "vm_callinfo.h"
128#include "ractor_core.h"
129#include "yjit.h"
130
131#include "builtin.h"
132#include "shape.h"
133
134unsigned int
135rb_gc_vm_lock(const char *file, int line)
136{
137 unsigned int lev = 0;
138 rb_vm_lock_enter(&lev, file, line);
139 return lev;
140}
141
142void
143rb_gc_vm_unlock(unsigned int lev, const char *file, int line)
144{
145 rb_vm_lock_leave(&lev, file, line);
146}
147
148unsigned int
149rb_gc_cr_lock(const char *file, int line)
150{
151 unsigned int lev;
152 rb_vm_lock_enter_cr(GET_RACTOR(), &lev, file, line);
153 return lev;
154}
155
156void
157rb_gc_cr_unlock(unsigned int lev, const char *file, int line)
158{
159 rb_vm_lock_leave_cr(GET_RACTOR(), &lev, file, line);
160}
161
162unsigned int
163rb_gc_vm_lock_no_barrier(const char *file, int line)
164{
165 unsigned int lev = 0;
166 rb_vm_lock_enter_nb(&lev, file, line);
167 return lev;
168}
169
170void
171rb_gc_vm_unlock_no_barrier(unsigned int lev, const char *file, int line)
172{
173 rb_vm_lock_leave_nb(&lev, file, line);
174}
175
176void
177rb_gc_vm_barrier(void)
178{
179 rb_vm_barrier();
180}
181
182#if USE_MODULAR_GC
183void *
184rb_gc_get_ractor_newobj_cache(void)
185{
186 return GET_RACTOR()->newobj_cache;
187}
188
189void
190rb_gc_initialize_vm_context(struct rb_gc_vm_context *context)
191{
192 rb_native_mutex_initialize(&context->lock);
193 context->ec = GET_EC();
194}
195
196void
197rb_gc_worker_thread_set_vm_context(struct rb_gc_vm_context *context)
198{
199 rb_native_mutex_lock(&context->lock);
200
201 GC_ASSERT(rb_current_execution_context(false) == NULL);
202
203#ifdef RB_THREAD_LOCAL_SPECIFIER
204 rb_current_ec_set(context->ec);
205#else
206 native_tls_set(ruby_current_ec_key, context->ec);
207#endif
208}
209
210void
211rb_gc_worker_thread_unset_vm_context(struct rb_gc_vm_context *context)
212{
213 rb_native_mutex_unlock(&context->lock);
214
215 GC_ASSERT(rb_current_execution_context(true) == context->ec);
216
217#ifdef RB_THREAD_LOCAL_SPECIFIER
218 rb_current_ec_set(NULL);
219#else
220 native_tls_set(ruby_current_ec_key, NULL);
221#endif
222}
223#endif
224
225bool
226rb_gc_event_hook_required_p(rb_event_flag_t event)
227{
228 return ruby_vm_event_flags & event;
229}
230
231void
232rb_gc_event_hook(VALUE obj, rb_event_flag_t event)
233{
234 if (LIKELY(!rb_gc_event_hook_required_p(event))) return;
235
236 rb_execution_context_t *ec = GET_EC();
237 if (!ec->cfp) return;
238
239 EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
240}
241
242void *
243rb_gc_get_objspace(void)
244{
245 return GET_VM()->gc.objspace;
246}
247
248
249void
250rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data)
251{
252 rb_ractor_t *r = NULL;
253 if (RB_LIKELY(ruby_single_main_ractor)) {
254 GC_ASSERT(
255 ccan_list_empty(&GET_VM()->ractor.set) ||
256 (ccan_list_top(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor &&
257 ccan_list_tail(&GET_VM()->ractor.set, rb_ractor_t, vmlr_node) == ruby_single_main_ractor)
258 );
259
260 func(ruby_single_main_ractor->newobj_cache, data);
261 }
262 else {
263 ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
264 func(r->newobj_cache, data);
265 }
266 }
267}
268
269void
270rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void *data), void *data)
271{
272 volatile struct {
273 VALUE errinfo;
274 VALUE final;
276 VALUE *sp;
277 long finished;
278 } saved;
279
280 rb_execution_context_t * volatile ec = GET_EC();
281#define RESTORE_FINALIZER() (\
282 ec->cfp = saved.cfp, \
283 ec->cfp->sp = saved.sp, \
284 ec->errinfo = saved.errinfo)
285
286 saved.errinfo = ec->errinfo;
287 saved.cfp = ec->cfp;
288 saved.sp = ec->cfp->sp;
289 saved.finished = 0;
290 saved.final = Qundef;
291
292 rb_ractor_ignore_belonging(true);
293 EC_PUSH_TAG(ec);
294 enum ruby_tag_type state = EC_EXEC_TAG();
295 if (state != TAG_NONE) {
296 ++saved.finished; /* skip failed finalizer */
297
298 VALUE failed_final = saved.final;
299 saved.final = Qundef;
300 if (!UNDEF_P(failed_final) && !NIL_P(ruby_verbose)) {
301 rb_warn("Exception in finalizer %+"PRIsVALUE, failed_final);
302 rb_ec_error_print(ec, ec->errinfo);
303 }
304 }
305
306 for (long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
307 saved.final = callback(i, data);
308 rb_check_funcall(saved.final, idCall, 1, &objid);
309 }
310 EC_POP_TAG();
311 rb_ractor_ignore_belonging(false);
312#undef RESTORE_FINALIZER
313}
314
315void
316rb_gc_set_pending_interrupt(void)
317{
318 rb_execution_context_t *ec = GET_EC();
319 ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
320}
321
322void
323rb_gc_unset_pending_interrupt(void)
324{
325 rb_execution_context_t *ec = GET_EC();
326 ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
327}
328
329bool
330rb_gc_multi_ractor_p(void)
331{
332 return rb_multi_ractor_p();
333}
334
335bool rb_obj_is_main_ractor(VALUE gv);
336
337bool
338rb_gc_shutdown_call_finalizer_p(VALUE obj)
339{
340 switch (BUILTIN_TYPE(obj)) {
341 case T_DATA:
342 if (!ruby_free_at_exit_p() && (!DATA_PTR(obj) || !RDATA(obj)->dfree)) return false;
343 if (rb_obj_is_thread(obj)) return false;
344 if (rb_obj_is_mutex(obj)) return false;
345 if (rb_obj_is_fiber(obj)) return false;
346 if (rb_ractor_p(obj)) return false;
347 if (rb_obj_is_fstring_table(obj)) return false;
348 if (rb_obj_is_symbol_table(obj)) return false;
349
350 return true;
351
352 case T_FILE:
353 return true;
354
355 case T_SYMBOL:
356 return true;
357
358 case T_NONE:
359 return false;
360
361 default:
362 return ruby_free_at_exit_p();
363 }
364}
365
366uint32_t
367rb_gc_get_shape(VALUE obj)
368{
369 return (uint32_t)rb_obj_shape_id(obj);
370}
371
372void
373rb_gc_set_shape(VALUE obj, uint32_t shape_id)
374{
375 rb_obj_set_shape_id(obj, (uint32_t)shape_id);
376}
377
378uint32_t
379rb_gc_rebuild_shape(VALUE obj, size_t heap_id)
380{
382
383 return (uint32_t)rb_shape_transition_heap(obj, heap_id);
384}
385
386void rb_vm_update_references(void *ptr);
387
388#define rb_setjmp(env) RUBY_SETJMP(env)
389#define rb_jmp_buf rb_jmpbuf_t
390#undef rb_data_object_wrap
391
392#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
393#define MAP_ANONYMOUS MAP_ANON
394#endif
395
396#define unless_objspace(objspace) \
397 void *objspace; \
398 rb_vm_t *unless_objspace_vm = GET_VM(); \
399 if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
400 else /* return; or objspace will be warned uninitialized */
401
402#define RMOVED(obj) ((struct RMoved *)(obj))
403
404#define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
405 if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
406 *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \
407 } \
408} while (0)
409
410#define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
411
412#if RUBY_MARK_FREE_DEBUG
413int ruby_gc_debug_indent = 0;
414#endif
415
416#ifndef RGENGC_OBJ_INFO
417# define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
418#endif
419
420#ifndef CALC_EXACT_MALLOC_SIZE
421# define CALC_EXACT_MALLOC_SIZE 0
422#endif
423
425
426static size_t malloc_offset = 0;
427#if defined(HAVE_MALLOC_USABLE_SIZE)
428static size_t
429gc_compute_malloc_offset(void)
430{
431 // Different allocators use different metadata storage strategies which result in different
432 // ideal sizes.
433 // For instance malloc(64) will waste 8B with glibc, but waste 0B with jemalloc.
434 // But malloc(56) will waste 0B with glibc, but waste 8B with jemalloc.
435 // So we try allocating 64, 56 and 48 bytes and select the first offset that doesn't
436 // waste memory.
437 // This was tested on Linux with glibc 2.35 and jemalloc 5, and for both it result in
438 // no wasted memory.
439 size_t offset = 0;
440 for (offset = 0; offset <= 16; offset += 8) {
441 size_t allocated = (64 - offset);
442 void *test_ptr = malloc(allocated);
443 size_t wasted = malloc_usable_size(test_ptr) - allocated;
444 free(test_ptr);
445
446 if (wasted == 0) {
447 return offset;
448 }
449 }
450 return 0;
451}
452#else
453static size_t
454gc_compute_malloc_offset(void)
455{
456 // If we don't have malloc_usable_size, we use powers of 2.
457 return 0;
458}
459#endif
460
461size_t
462rb_malloc_grow_capa(size_t current, size_t type_size)
463{
464 size_t current_capacity = current;
465 if (current_capacity < 4) {
466 current_capacity = 4;
467 }
468 current_capacity *= type_size;
469
470 // We double the current capacity.
471 size_t new_capacity = (current_capacity * 2);
472
473 // And round up to the next power of 2 if it's not already one.
474 if (rb_popcount64(new_capacity) != 1) {
475 new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
476 }
477
478 new_capacity -= malloc_offset;
479 new_capacity /= type_size;
480 if (current > new_capacity) {
481 rb_bug("rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
482 }
483 RUBY_ASSERT(new_capacity > current);
484 return new_capacity;
485}
486
487static inline struct rbimpl_size_mul_overflow_tag
488size_mul_add_overflow(size_t x, size_t y, size_t z) /* x * y + z */
489{
490 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
491 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_add_overflow(t.right, z);
492 return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
493}
494
495static inline struct rbimpl_size_mul_overflow_tag
496size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
497{
498 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
499 struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
500 struct rbimpl_size_mul_overflow_tag v = rbimpl_size_add_overflow(t.right, u.right);
501 return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
502}
503
504PRINTF_ARGS(NORETURN(static void gc_raise(VALUE, const char*, ...)), 2, 3);
505
506static inline size_t
507size_mul_or_raise(size_t x, size_t y, VALUE exc)
508{
509 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510 if (LIKELY(!t.left)) {
511 return t.right;
512 }
513 else if (rb_during_gc()) {
514 rb_memerror(); /* or...? */
515 }
516 else {
517 gc_raise(
518 exc,
519 "integer overflow: %"PRIuSIZE
520 " * %"PRIuSIZE
521 " > %"PRIuSIZE,
522 x, y, (size_t)SIZE_MAX);
523 }
524}
525
526size_t
527rb_size_mul_or_raise(size_t x, size_t y, VALUE exc)
528{
529 return size_mul_or_raise(x, y, exc);
530}
531
532static inline size_t
533size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
534{
535 struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
536 if (LIKELY(!t.left)) {
537 return t.right;
538 }
539 else if (rb_during_gc()) {
540 rb_memerror(); /* or...? */
541 }
542 else {
543 gc_raise(
544 exc,
545 "integer overflow: %"PRIuSIZE
546 " * %"PRIuSIZE
547 " + %"PRIuSIZE
548 " > %"PRIuSIZE,
549 x, y, z, (size_t)SIZE_MAX);
550 }
551}
552
553size_t
554rb_size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
555{
556 return size_mul_add_or_raise(x, y, z, exc);
557}
558
559static inline size_t
560size_mul_add_mul_or_raise(size_t x, size_t y, size_t z, size_t w, VALUE exc)
561{
562 struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
563 if (LIKELY(!t.left)) {
564 return t.right;
565 }
566 else if (rb_during_gc()) {
567 rb_memerror(); /* or...? */
568 }
569 else {
570 gc_raise(
571 exc,
572 "integer overflow: %"PRIdSIZE
573 " * %"PRIdSIZE
574 " + %"PRIdSIZE
575 " * %"PRIdSIZE
576 " > %"PRIdSIZE,
577 x, y, z, w, (size_t)SIZE_MAX);
578 }
579}
580
581#if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
582/* trick the compiler into thinking a external signal handler uses this */
583volatile VALUE rb_gc_guarded_val;
584volatile VALUE *
585rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val)
586{
587 rb_gc_guarded_val = val;
588
589 return ptr;
590}
591#endif
592
593static const char *obj_type_name(VALUE obj);
594#include "gc/default/default.c"
595
596#if USE_MODULAR_GC && !defined(HAVE_DLOPEN)
597# error "Modular GC requires dlopen"
598#elif USE_MODULAR_GC
599#include <dlfcn.h>
600
601typedef struct gc_function_map {
602 // Bootup
603 void *(*objspace_alloc)(void);
604 void (*objspace_init)(void *objspace_ptr);
605 void *(*ractor_cache_alloc)(void *objspace_ptr, void *ractor);
606 void (*set_params)(void *objspace_ptr);
607 void (*init)(void);
608 size_t *(*heap_sizes)(void *objspace_ptr);
609 // Shutdown
610 void (*shutdown_free_objects)(void *objspace_ptr);
611 void (*objspace_free)(void *objspace_ptr);
612 void (*ractor_cache_free)(void *objspace_ptr, void *cache);
613 // GC
614 void (*start)(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
615 bool (*during_gc_p)(void *objspace_ptr);
616 void (*prepare_heap)(void *objspace_ptr);
617 void (*gc_enable)(void *objspace_ptr);
618 void (*gc_disable)(void *objspace_ptr, bool finish_current_gc);
619 bool (*gc_enabled_p)(void *objspace_ptr);
620 VALUE (*config_get)(void *objpace_ptr);
621 void (*config_set)(void *objspace_ptr, VALUE hash);
622 void (*stress_set)(void *objspace_ptr, VALUE flag);
623 VALUE (*stress_get)(void *objspace_ptr);
624 // Object allocation
625 VALUE (*new_obj)(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, bool wb_protected, size_t alloc_size);
626 size_t (*obj_slot_size)(VALUE obj);
627 size_t (*heap_id_for_size)(void *objspace_ptr, size_t size);
628 bool (*size_allocatable_p)(size_t size);
629 // Malloc
630 void *(*malloc)(void *objspace_ptr, size_t size);
631 void *(*calloc)(void *objspace_ptr, size_t size);
632 void *(*realloc)(void *objspace_ptr, void *ptr, size_t new_size, size_t old_size);
633 void (*free)(void *objspace_ptr, void *ptr, size_t old_size);
634 void (*adjust_memory_usage)(void *objspace_ptr, ssize_t diff);
635 // Marking
636 void (*mark)(void *objspace_ptr, VALUE obj);
637 void (*mark_and_move)(void *objspace_ptr, VALUE *ptr);
638 void (*mark_and_pin)(void *objspace_ptr, VALUE obj);
639 void (*mark_maybe)(void *objspace_ptr, VALUE obj);
640 void (*mark_weak)(void *objspace_ptr, VALUE *ptr);
641 void (*remove_weak)(void *objspace_ptr, VALUE parent_obj, VALUE *ptr);
642 // Compaction
643 bool (*object_moved_p)(void *objspace_ptr, VALUE obj);
644 VALUE (*location)(void *objspace_ptr, VALUE value);
645 // Write barriers
646 void (*writebarrier)(void *objspace_ptr, VALUE a, VALUE b);
647 void (*writebarrier_unprotect)(void *objspace_ptr, VALUE obj);
648 void (*writebarrier_remember)(void *objspace_ptr, VALUE obj);
649 // Heap walking
650 void (*each_objects)(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data);
651 void (*each_object)(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data);
652 // Finalizers
653 void (*make_zombie)(void *objspace_ptr, VALUE obj, void (*dfree)(void *), void *data);
654 VALUE (*define_finalizer)(void *objspace_ptr, VALUE obj, VALUE block);
655 void (*undefine_finalizer)(void *objspace_ptr, VALUE obj);
656 void (*copy_finalizer)(void *objspace_ptr, VALUE dest, VALUE obj);
657 void (*shutdown_call_finalizer)(void *objspace_ptr);
658 // Forking
659 void (*before_fork)(void *objspace_ptr);
660 void (*after_fork)(void *objspace_ptr, rb_pid_t pid);
661 // Statistics
662 void (*set_measure_total_time)(void *objspace_ptr, VALUE flag);
663 bool (*get_measure_total_time)(void *objspace_ptr);
664 unsigned long long (*get_total_time)(void *objspace_ptr);
665 size_t (*gc_count)(void *objspace_ptr);
666 VALUE (*latest_gc_info)(void *objspace_ptr, VALUE key);
667 VALUE (*stat)(void *objspace_ptr, VALUE hash_or_sym);
668 VALUE (*stat_heap)(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym);
669 const char *(*active_gc_name)(void);
670 // Miscellaneous
671 struct rb_gc_object_metadata_entry *(*object_metadata)(void *objspace_ptr, VALUE obj);
672 bool (*pointer_to_heap_p)(void *objspace_ptr, const void *ptr);
673 bool (*garbage_object_p)(void *objspace_ptr, VALUE obj);
674 void (*set_event_hook)(void *objspace_ptr, const rb_event_flag_t event);
675 void (*copy_attributes)(void *objspace_ptr, VALUE dest, VALUE obj);
676
677 bool modular_gc_loaded_p;
678} rb_gc_function_map_t;
679
680static rb_gc_function_map_t rb_gc_functions;
681
682# define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
683# define MODULAR_GC_DIR STRINGIZE(modular_gc_dir)
684
685static void
686ruby_modular_gc_init(void)
687{
688 // Assert that the directory path ends with a /
689 RUBY_ASSERT_ALWAYS(MODULAR_GC_DIR[sizeof(MODULAR_GC_DIR) - 2] == '/');
690
691 const char *gc_so_file = getenv(RUBY_GC_LIBRARY);
692
693 rb_gc_function_map_t gc_functions = { 0 };
694
695 char *gc_so_path = NULL;
696 void *handle = NULL;
697 if (gc_so_file) {
698 /* Check to make sure that gc_so_file matches /[\w-_]+/ so that it does
699 * not load a shared object outside of the directory. */
700 for (size_t i = 0; i < strlen(gc_so_file); i++) {
701 char c = gc_so_file[i];
702 if (isalnum(c)) continue;
703 switch (c) {
704 case '-':
705 case '_':
706 break;
707 default:
708 fprintf(stderr, "Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY"\n");
709 exit(1);
710 }
711 }
712
713 size_t gc_so_path_size = strlen(MODULAR_GC_DIR "librubygc." DLEXT) + strlen(gc_so_file) + 1;
714#ifdef LOAD_RELATIVE
715 Dl_info dli;
716 size_t prefix_len = 0;
717 if (dladdr((void *)(uintptr_t)ruby_modular_gc_init, &dli)) {
718 const char *base = strrchr(dli.dli_fname, '/');
719 if (base) {
720 size_t tail = 0;
721# define end_with_p(lit) \
722 (prefix_len >= (tail = rb_strlen_lit(lit)) && \
723 memcmp(base - tail, lit, tail) == 0)
724
725 prefix_len = base - dli.dli_fname;
726 if (end_with_p("/bin") || end_with_p("/lib")) {
727 prefix_len -= tail;
728 }
729 prefix_len += MODULAR_GC_DIR[0] != '/';
730 gc_so_path_size += prefix_len;
731 }
732 }
733#endif
734 gc_so_path = alloca(gc_so_path_size);
735 {
736 size_t gc_so_path_idx = 0;
737#define GC_SO_PATH_APPEND(str) do { \
738 gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
739} while (0)
740#ifdef LOAD_RELATIVE
741 if (prefix_len > 0) {
742 memcpy(gc_so_path, dli.dli_fname, prefix_len);
743 gc_so_path_idx = prefix_len;
744 }
745#endif
746 GC_SO_PATH_APPEND(MODULAR_GC_DIR "librubygc.");
747 GC_SO_PATH_APPEND(gc_so_file);
748 GC_SO_PATH_APPEND(DLEXT);
749 GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
750#undef GC_SO_PATH_APPEND
751 }
752
753 handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
754 if (!handle) {
755 fprintf(stderr, "ruby_modular_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
756 exit(1);
757 }
758
759 gc_functions.modular_gc_loaded_p = true;
760 }
761
762# define load_modular_gc_func(name) do { \
763 if (handle) { \
764 const char *func_name = "rb_gc_impl_" #name; \
765 gc_functions.name = dlsym(handle, func_name); \
766 if (!gc_functions.name) { \
767 fprintf(stderr, "ruby_modular_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
768 exit(1); \
769 } \
770 } \
771 else { \
772 gc_functions.name = rb_gc_impl_##name; \
773 } \
774} while (0)
775
776 // Bootup
777 load_modular_gc_func(objspace_alloc);
778 load_modular_gc_func(objspace_init);
779 load_modular_gc_func(ractor_cache_alloc);
780 load_modular_gc_func(set_params);
781 load_modular_gc_func(init);
782 load_modular_gc_func(heap_sizes);
783 // Shutdown
784 load_modular_gc_func(shutdown_free_objects);
785 load_modular_gc_func(objspace_free);
786 load_modular_gc_func(ractor_cache_free);
787 // GC
788 load_modular_gc_func(start);
789 load_modular_gc_func(during_gc_p);
790 load_modular_gc_func(prepare_heap);
791 load_modular_gc_func(gc_enable);
792 load_modular_gc_func(gc_disable);
793 load_modular_gc_func(gc_enabled_p);
794 load_modular_gc_func(config_set);
795 load_modular_gc_func(config_get);
796 load_modular_gc_func(stress_set);
797 load_modular_gc_func(stress_get);
798 // Object allocation
799 load_modular_gc_func(new_obj);
800 load_modular_gc_func(obj_slot_size);
801 load_modular_gc_func(heap_id_for_size);
802 load_modular_gc_func(size_allocatable_p);
803 // Malloc
804 load_modular_gc_func(malloc);
805 load_modular_gc_func(calloc);
806 load_modular_gc_func(realloc);
807 load_modular_gc_func(free);
808 load_modular_gc_func(adjust_memory_usage);
809 // Marking
810 load_modular_gc_func(mark);
811 load_modular_gc_func(mark_and_move);
812 load_modular_gc_func(mark_and_pin);
813 load_modular_gc_func(mark_maybe);
814 load_modular_gc_func(mark_weak);
815 load_modular_gc_func(remove_weak);
816 // Compaction
817 load_modular_gc_func(object_moved_p);
818 load_modular_gc_func(location);
819 // Write barriers
820 load_modular_gc_func(writebarrier);
821 load_modular_gc_func(writebarrier_unprotect);
822 load_modular_gc_func(writebarrier_remember);
823 // Heap walking
824 load_modular_gc_func(each_objects);
825 load_modular_gc_func(each_object);
826 // Finalizers
827 load_modular_gc_func(make_zombie);
828 load_modular_gc_func(define_finalizer);
829 load_modular_gc_func(undefine_finalizer);
830 load_modular_gc_func(copy_finalizer);
831 load_modular_gc_func(shutdown_call_finalizer);
832 // Forking
833 load_modular_gc_func(before_fork);
834 load_modular_gc_func(after_fork);
835 // Statistics
836 load_modular_gc_func(set_measure_total_time);
837 load_modular_gc_func(get_measure_total_time);
838 load_modular_gc_func(get_total_time);
839 load_modular_gc_func(gc_count);
840 load_modular_gc_func(latest_gc_info);
841 load_modular_gc_func(stat);
842 load_modular_gc_func(stat_heap);
843 load_modular_gc_func(active_gc_name);
844 // Miscellaneous
845 load_modular_gc_func(object_metadata);
846 load_modular_gc_func(pointer_to_heap_p);
847 load_modular_gc_func(garbage_object_p);
848 load_modular_gc_func(set_event_hook);
849 load_modular_gc_func(copy_attributes);
850
851# undef load_modular_gc_func
852
853 rb_gc_functions = gc_functions;
854}
855
856// Bootup
857# define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
858# define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
859# define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
860# define rb_gc_impl_set_params rb_gc_functions.set_params
861# define rb_gc_impl_init rb_gc_functions.init
862# define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
863// Shutdown
864# define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
865# define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
866# define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
867// GC
868# define rb_gc_impl_start rb_gc_functions.start
869# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
870# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
871# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
872# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
873# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
874# define rb_gc_impl_config_get rb_gc_functions.config_get
875# define rb_gc_impl_config_set rb_gc_functions.config_set
876# define rb_gc_impl_stress_set rb_gc_functions.stress_set
877# define rb_gc_impl_stress_get rb_gc_functions.stress_get
878// Object allocation
879# define rb_gc_impl_new_obj rb_gc_functions.new_obj
880# define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
881# define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
882# define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
883// Malloc
884# define rb_gc_impl_malloc rb_gc_functions.malloc
885# define rb_gc_impl_calloc rb_gc_functions.calloc
886# define rb_gc_impl_realloc rb_gc_functions.realloc
887# define rb_gc_impl_free rb_gc_functions.free
888# define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
889// Marking
890# define rb_gc_impl_mark rb_gc_functions.mark
891# define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
892# define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
893# define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
894# define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
895# define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
896// Compaction
897# define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
898# define rb_gc_impl_location rb_gc_functions.location
899// Write barriers
900# define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
901# define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
902# define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
903// Heap walking
904# define rb_gc_impl_each_objects rb_gc_functions.each_objects
905# define rb_gc_impl_each_object rb_gc_functions.each_object
906// Finalizers
907# define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
908# define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
909# define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
910# define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
911# define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
912// Forking
913# define rb_gc_impl_before_fork rb_gc_functions.before_fork
914# define rb_gc_impl_after_fork rb_gc_functions.after_fork
915// Statistics
916# define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
917# define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
918# define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
919# define rb_gc_impl_gc_count rb_gc_functions.gc_count
920# define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
921# define rb_gc_impl_stat rb_gc_functions.stat
922# define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
923# define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
924// Miscellaneous
925# define rb_gc_impl_object_metadata rb_gc_functions.object_metadata
926# define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
927# define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
928# define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
929# define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
930#endif
931
932#ifdef RUBY_ASAN_ENABLED
933static void
934asan_death_callback(void)
935{
936 if (GET_VM()) {
937 rb_bug_without_die("ASAN error");
938 }
939}
940#endif
941
942static VALUE initial_stress = Qfalse;
943
944void *
945rb_objspace_alloc(void)
946{
947#if USE_MODULAR_GC
948 ruby_modular_gc_init();
949#endif
950
951 void *objspace = rb_gc_impl_objspace_alloc();
952 ruby_current_vm_ptr->gc.objspace = objspace;
953 rb_gc_impl_objspace_init(objspace);
954 rb_gc_impl_stress_set(objspace, initial_stress);
955
956#ifdef RUBY_ASAN_ENABLED
957 __sanitizer_set_death_callback(asan_death_callback);
958#endif
959
960 return objspace;
961}
962
963void
964rb_objspace_free(void *objspace)
965{
966 rb_gc_impl_objspace_free(objspace);
967}
968
969size_t
970rb_gc_obj_slot_size(VALUE obj)
971{
972 return rb_gc_impl_obj_slot_size(obj);
973}
974
975static inline void
976gc_validate_pc(void)
977{
978#if RUBY_DEBUG
979 rb_execution_context_t *ec = GET_EC();
980 const rb_control_frame_t *cfp = ec->cfp;
981 if (cfp && VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc) {
982 RUBY_ASSERT(cfp->pc >= ISEQ_BODY(cfp->iseq)->iseq_encoded);
983 RUBY_ASSERT(cfp->pc <= ISEQ_BODY(cfp->iseq)->iseq_encoded + ISEQ_BODY(cfp->iseq)->iseq_size);
984 }
985#endif
986}
987
988static inline VALUE
989newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, bool wb_protected, size_t size)
990{
991 VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
992
993 gc_validate_pc();
994
995 if (UNLIKELY(rb_gc_event_hook_required_p(RUBY_INTERNAL_EVENT_NEWOBJ))) {
996 unsigned int lev;
997 RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
998 {
999 memset((char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
1000
1001 /* We must disable GC here because the callback could call xmalloc
1002 * which could potentially trigger a GC, and a lot of code is unsafe
1003 * to trigger a GC right after an object has been allocated because
1004 * they perform initialization for the object and assume that the
1005 * GC does not trigger before then. */
1006 bool gc_disabled = RTEST(rb_gc_disable_no_rest());
1007 {
1008 rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_NEWOBJ);
1009 }
1010 if (!gc_disabled) rb_gc_enable();
1011 }
1012 RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
1013 }
1014
1015 return obj;
1016}
1017
1018VALUE
1019rb_wb_unprotected_newobj_of(VALUE klass, VALUE flags, size_t size)
1020{
1021 GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
1022 return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
1023}
1024
1025VALUE
1026rb_wb_protected_newobj_of(rb_execution_context_t *ec, VALUE klass, VALUE flags, size_t size)
1027{
1028 GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
1029 return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
1030}
1031
1032#define UNEXPECTED_NODE(func) \
1033 rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
1034 BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
1035
1036static inline void
1037rb_data_object_check(VALUE klass)
1038{
1039 if (klass != rb_cObject && (rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
1040 rb_undef_alloc_func(klass);
1041 rb_warn("undefining the allocator of T_DATA class %"PRIsVALUE, klass);
1042 }
1043}
1044
1045VALUE
1046rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
1047{
1049 if (klass) rb_data_object_check(klass);
1050 return newobj_of(GET_RACTOR(), klass, T_DATA, (VALUE)dmark, (VALUE)dfree, (VALUE)datap, !dmark, sizeof(struct RTypedData));
1051}
1052
1053VALUE
1054rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
1055{
1056 VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
1057 DATA_PTR(obj) = xcalloc(1, size);
1058 return obj;
1059}
1060
1061static VALUE
1062typed_data_alloc(VALUE klass, VALUE typed_flag, void *datap, const rb_data_type_t *type, size_t size)
1063{
1064 RBIMPL_NONNULL_ARG(type);
1065 if (klass) rb_data_object_check(klass);
1066 bool wb_protected = (type->flags & RUBY_FL_WB_PROTECTED) || !type->function.dmark;
1067 return newobj_of(GET_RACTOR(), klass, T_DATA, 0, ((VALUE)type) | IS_TYPED_DATA | typed_flag, (VALUE)datap, wb_protected, size);
1068}
1069
1070VALUE
1071rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
1072{
1073 if (UNLIKELY(type->flags & RUBY_TYPED_EMBEDDABLE)) {
1074 rb_raise(rb_eTypeError, "Cannot wrap an embeddable TypedData");
1075 }
1076
1077 return typed_data_alloc(klass, 0, datap, type, sizeof(struct RTypedData));
1078}
1079
1080VALUE
1081rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
1082{
1083 if (type->flags & RUBY_TYPED_EMBEDDABLE) {
1084 if (!(type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1085 rb_raise(rb_eTypeError, "Embeddable TypedData must be freed immediately");
1086 }
1087
1088 size_t embed_size = offsetof(struct RTypedData, data) + size;
1089 if (rb_gc_size_allocatable_p(embed_size)) {
1090 VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0, type, embed_size);
1091 memset((char *)obj + offsetof(struct RTypedData, data), 0, size);
1092 return obj;
1093 }
1094 }
1095
1096 VALUE obj = typed_data_alloc(klass, 0, NULL, type, sizeof(struct RTypedData));
1097 DATA_PTR(obj) = xcalloc(1, size);
1098 return obj;
1099}
1100
1101static size_t
1102rb_objspace_data_type_memsize(VALUE obj)
1103{
1104 size_t size = 0;
1105 if (RTYPEDDATA_P(obj)) {
1106 const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
1107 const void *ptr = RTYPEDDATA_GET_DATA(obj);
1108
1109 if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1110#ifdef HAVE_MALLOC_USABLE_SIZE
1111 size += malloc_usable_size((void *)ptr);
1112#endif
1113 }
1114
1115 if (ptr && type->function.dsize) {
1116 size += type->function.dsize(ptr);
1117 }
1118 }
1119
1120 return size;
1121}
1122
1123const char *
1124rb_objspace_data_type_name(VALUE obj)
1125{
1126 if (RTYPEDDATA_P(obj)) {
1127 return RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1128 }
1129 else {
1130 return 0;
1131 }
1132}
1133
1134static enum rb_id_table_iterator_result
1135cvar_table_free_i(VALUE value, void *ctx)
1136{
1137 xfree((void *)value);
1138 return ID_TABLE_CONTINUE;
1139}
1140
1141static void
1142io_fptr_finalize(void *fptr)
1143{
1144 rb_io_fptr_finalize((struct rb_io *)fptr);
1145}
1146
1147static inline void
1148make_io_zombie(void *objspace, VALUE obj)
1149{
1150 rb_io_t *fptr = RFILE(obj)->fptr;
1151 rb_gc_impl_make_zombie(objspace, obj, io_fptr_finalize, fptr);
1152}
1153
1154static bool
1155rb_data_free(void *objspace, VALUE obj)
1156{
1157 void *data = RTYPEDDATA_P(obj) ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
1158 if (data) {
1159 int free_immediately = false;
1160 void (*dfree)(void *);
1161
1162 if (RTYPEDDATA_P(obj)) {
1163 free_immediately = (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1164 dfree = RTYPEDDATA_TYPE(obj)->function.dfree;
1165 }
1166 else {
1167 dfree = RDATA(obj)->dfree;
1168 }
1169
1170 if (dfree) {
1171 if (dfree == RUBY_DEFAULT_FREE) {
1172 if (!RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1173 xfree(data);
1174 RB_DEBUG_COUNTER_INC(obj_data_xfree);
1175 }
1176 }
1177 else if (free_immediately) {
1178 (*dfree)(data);
1179 if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1180 xfree(data);
1181 }
1182
1183 RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1184 }
1185 else {
1186 rb_gc_impl_make_zombie(objspace, obj, dfree, data);
1187 RB_DEBUG_COUNTER_INC(obj_data_zombie);
1188 return FALSE;
1189 }
1190 }
1191 else {
1192 RB_DEBUG_COUNTER_INC(obj_data_empty);
1193 }
1194 }
1195
1196 return true;
1197}
1198
1200 VALUE klass;
1201 rb_objspace_t *objspace; // used for update_*
1202};
1203
1204static void
1205classext_free(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)
1206{
1207 struct rb_id_table *tbl;
1208 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
1209
1210 rb_id_table_free(RCLASSEXT_M_TBL(ext));
1211
1212 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
1213 rb_free_const_table(tbl);
1214 }
1215 if ((tbl = RCLASSEXT_CVC_TBL(ext)) != NULL) {
1216 rb_id_table_foreach_values(tbl, cvar_table_free_i, NULL);
1217 rb_id_table_free(tbl);
1218 }
1219 rb_class_classext_free_subclasses(ext, args->klass);
1220 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
1221 RUBY_ASSERT(is_prime); // superclasses should only be used on prime
1222 xfree(RCLASSEXT_SUPERCLASSES(ext));
1223 }
1224 if (!is_prime) { // the prime classext will be freed with RClass
1225 xfree(ext);
1226 }
1227}
1228
1229static void
1230classext_iclass_free(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)
1231{
1232 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
1233
1234 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
1235 /* Method table is not shared for origin iclasses of classes */
1236 rb_id_table_free(RCLASSEXT_M_TBL(ext));
1237 }
1238 if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
1239 rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
1240 }
1241
1242 rb_class_classext_free_subclasses(ext, args->klass);
1243
1244 if (!is_prime) { // the prime classext will be freed with RClass
1245 xfree(ext);
1246 }
1247}
1248
1249bool
1250rb_gc_obj_free(void *objspace, VALUE obj)
1251{
1252 struct classext_foreach_args args;
1253
1254 RB_DEBUG_COUNTER_INC(obj_free);
1255
1256 switch (BUILTIN_TYPE(obj)) {
1257 case T_NIL:
1258 case T_FIXNUM:
1259 case T_TRUE:
1260 case T_FALSE:
1261 rb_bug("obj_free() called for broken object");
1262 break;
1263 default:
1264 break;
1265 }
1266
1267 switch (BUILTIN_TYPE(obj)) {
1268 case T_OBJECT:
1269 if (rb_shape_obj_too_complex_p(obj)) {
1270 RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1271 st_free_table(ROBJECT_FIELDS_HASH(obj));
1272 }
1273 else if (RBASIC(obj)->flags & ROBJECT_EMBED) {
1274 RB_DEBUG_COUNTER_INC(obj_obj_embed);
1275 }
1276 else {
1277 xfree(ROBJECT(obj)->as.heap.fields);
1278 RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1279 }
1280 break;
1281 case T_MODULE:
1282 case T_CLASS:
1283 args.klass = obj;
1284 rb_class_classext_foreach(obj, classext_free, (void *)&args);
1285 if (RCLASS_CLASSEXT_TBL(obj)) {
1286 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1287 }
1288 (void)RB_DEBUG_COUNTER_INC_IF(obj_module_ptr, BUILTIN_TYPE(obj) == T_MODULE);
1289 (void)RB_DEBUG_COUNTER_INC_IF(obj_class_ptr, BUILTIN_TYPE(obj) == T_CLASS);
1290 break;
1291 case T_STRING:
1292 rb_str_free(obj);
1293 break;
1294 case T_ARRAY:
1295 rb_ary_free(obj);
1296 break;
1297 case T_HASH:
1298#if USE_DEBUG_COUNTER
1299 switch (RHASH_SIZE(obj)) {
1300 case 0:
1301 RB_DEBUG_COUNTER_INC(obj_hash_empty);
1302 break;
1303 case 1:
1304 RB_DEBUG_COUNTER_INC(obj_hash_1);
1305 break;
1306 case 2:
1307 RB_DEBUG_COUNTER_INC(obj_hash_2);
1308 break;
1309 case 3:
1310 RB_DEBUG_COUNTER_INC(obj_hash_3);
1311 break;
1312 case 4:
1313 RB_DEBUG_COUNTER_INC(obj_hash_4);
1314 break;
1315 case 5:
1316 case 6:
1317 case 7:
1318 case 8:
1319 RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1320 break;
1321 default:
1322 GC_ASSERT(RHASH_SIZE(obj) > 8);
1323 RB_DEBUG_COUNTER_INC(obj_hash_g8);
1324 }
1325
1326 if (RHASH_AR_TABLE_P(obj)) {
1327 if (RHASH_AR_TABLE(obj) == NULL) {
1328 RB_DEBUG_COUNTER_INC(obj_hash_null);
1329 }
1330 else {
1331 RB_DEBUG_COUNTER_INC(obj_hash_ar);
1332 }
1333 }
1334 else {
1335 RB_DEBUG_COUNTER_INC(obj_hash_st);
1336 }
1337#endif
1338
1339 rb_hash_free(obj);
1340 break;
1341 case T_REGEXP:
1342 if (RREGEXP(obj)->ptr) {
1343 onig_free(RREGEXP(obj)->ptr);
1344 RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1345 }
1346 break;
1347 case T_DATA:
1348 if (!rb_data_free(objspace, obj)) return false;
1349 break;
1350 case T_MATCH:
1351 {
1352 rb_matchext_t *rm = RMATCH_EXT(obj);
1353#if USE_DEBUG_COUNTER
1354 if (rm->regs.num_regs >= 8) {
1355 RB_DEBUG_COUNTER_INC(obj_match_ge8);
1356 }
1357 else if (rm->regs.num_regs >= 4) {
1358 RB_DEBUG_COUNTER_INC(obj_match_ge4);
1359 }
1360 else if (rm->regs.num_regs >= 1) {
1361 RB_DEBUG_COUNTER_INC(obj_match_under4);
1362 }
1363#endif
1364 onig_region_free(&rm->regs, 0);
1365 xfree(rm->char_offset);
1366
1367 RB_DEBUG_COUNTER_INC(obj_match_ptr);
1368 }
1369 break;
1370 case T_FILE:
1371 if (RFILE(obj)->fptr) {
1372 make_io_zombie(objspace, obj);
1373 RB_DEBUG_COUNTER_INC(obj_file_ptr);
1374 return FALSE;
1375 }
1376 break;
1377 case T_RATIONAL:
1378 RB_DEBUG_COUNTER_INC(obj_rational);
1379 break;
1380 case T_COMPLEX:
1381 RB_DEBUG_COUNTER_INC(obj_complex);
1382 break;
1383 case T_MOVED:
1384 break;
1385 case T_ICLASS:
1386 args.klass = obj;
1387
1388 rb_class_classext_foreach(obj, classext_iclass_free, (void *)&args);
1389 if (RCLASS_CLASSEXT_TBL(obj)) {
1390 st_free_table(RCLASS_CLASSEXT_TBL(obj));
1391 }
1392
1393 RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1394 break;
1395
1396 case T_FLOAT:
1397 RB_DEBUG_COUNTER_INC(obj_float);
1398 break;
1399
1400 case T_BIGNUM:
1401 if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1402 xfree(BIGNUM_DIGITS(obj));
1403 RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1404 }
1405 else {
1406 RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1407 }
1408 break;
1409
1410 case T_NODE:
1411 UNEXPECTED_NODE(obj_free);
1412 break;
1413
1414 case T_STRUCT:
1415 if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1416 RSTRUCT(obj)->as.heap.ptr == NULL) {
1417 RB_DEBUG_COUNTER_INC(obj_struct_embed);
1418 }
1419 else {
1420 xfree((void *)RSTRUCT(obj)->as.heap.ptr);
1421 RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1422 }
1423 break;
1424
1425 case T_SYMBOL:
1426 RB_DEBUG_COUNTER_INC(obj_symbol);
1427 break;
1428
1429 case T_IMEMO:
1430 rb_imemo_free((VALUE)obj);
1431 break;
1432
1433 default:
1434 rb_bug("gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1435 BUILTIN_TYPE(obj), (void*)obj, RBASIC(obj)->flags);
1436 }
1437
1438 if (FL_TEST_RAW(obj, FL_FINALIZE)) {
1439 rb_gc_impl_make_zombie(objspace, obj, 0, 0);
1440 return FALSE;
1441 }
1442 else {
1443 return TRUE;
1444 }
1445}
1446
1447void
1448rb_objspace_set_event_hook(const rb_event_flag_t event)
1449{
1450 rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1451}
1452
1453static int
1454internal_object_p(VALUE obj)
1455{
1456 void *ptr = asan_unpoison_object_temporary(obj);
1457
1458 if (RBASIC(obj)->flags) {
1459 switch (BUILTIN_TYPE(obj)) {
1460 case T_NODE:
1461 UNEXPECTED_NODE(internal_object_p);
1462 break;
1463 case T_NONE:
1464 case T_MOVED:
1465 case T_IMEMO:
1466 case T_ICLASS:
1467 case T_ZOMBIE:
1468 break;
1469 case T_CLASS:
1470 if (obj == rb_mRubyVMFrozenCore)
1471 return 1;
1472
1473 if (!RBASIC_CLASS(obj)) break;
1474 if (RCLASS_SINGLETON_P(obj)) {
1475 return rb_singleton_class_internal_p(obj);
1476 }
1477 return 0;
1478 default:
1479 if (!RBASIC(obj)->klass) break;
1480 return 0;
1481 }
1482 }
1483 if (ptr || !RBASIC(obj)->flags) {
1484 rb_asan_poison_object(obj);
1485 }
1486 return 1;
1487}
1488
1489int
1490rb_objspace_internal_object_p(VALUE obj)
1491{
1492 return internal_object_p(obj);
1493}
1494
1496 size_t num;
1497 VALUE of;
1498};
1499
1500static int
1501os_obj_of_i(void *vstart, void *vend, size_t stride, void *data)
1502{
1503 struct os_each_struct *oes = (struct os_each_struct *)data;
1504
1505 VALUE v = (VALUE)vstart;
1506 for (; v != (VALUE)vend; v += stride) {
1507 if (!internal_object_p(v)) {
1508 if (!oes->of || rb_obj_is_kind_of(v, oes->of)) {
1509 if (!rb_multi_ractor_p() || rb_ractor_shareable_p(v)) {
1510 rb_yield(v);
1511 oes->num++;
1512 }
1513 }
1514 }
1515 }
1516
1517 return 0;
1518}
1519
1520static VALUE
1521os_obj_of(VALUE of)
1522{
1523 struct os_each_struct oes;
1524
1525 oes.num = 0;
1526 oes.of = of;
1527 rb_objspace_each_objects(os_obj_of_i, &oes);
1528 return SIZET2NUM(oes.num);
1529}
1530
1531/*
1532 * call-seq:
1533 * ObjectSpace.each_object([module]) {|obj| ... } -> integer
1534 * ObjectSpace.each_object([module]) -> an_enumerator
1535 *
1536 * Calls the block once for each living, nonimmediate object in this
1537 * Ruby process. If <i>module</i> is specified, calls the block
1538 * for only those classes or modules that match (or are a subclass of)
1539 * <i>module</i>. Returns the number of objects found. Immediate
1540 * objects (<code>Fixnum</code>s, <code>Symbol</code>s
1541 * <code>true</code>, <code>false</code>, and <code>nil</code>) are
1542 * never returned. In the example below, #each_object returns both
1543 * the numbers we defined and several constants defined in the Math
1544 * module.
1545 *
1546 * If no block is given, an enumerator is returned instead.
1547 *
1548 * a = 102.7
1549 * b = 95 # Won't be returned
1550 * c = 12345678987654321
1551 * count = ObjectSpace.each_object(Numeric) {|x| p x }
1552 * puts "Total count: #{count}"
1553 *
1554 * <em>produces:</em>
1555 *
1556 * 12345678987654321
1557 * 102.7
1558 * 2.71828182845905
1559 * 3.14159265358979
1560 * 2.22044604925031e-16
1561 * 1.7976931348623157e+308
1562 * 2.2250738585072e-308
1563 * Total count: 7
1564 *
1565 * Due to a current known Ractor implementation issue, this method will not yield
1566 * Ractor-unshareable objects in multi-Ractor mode (when
1567 * <code>Ractor.new</code> has been called within the process at least once).
1568 * See https://bugs.ruby-lang.org/issues/19387 for more information.
1569 *
1570 * a = 12345678987654321 # shareable
1571 * b = [].freeze # shareable
1572 * c = {} # not shareable
1573 * ObjectSpace.each_object {|x| x } # yields a, b, and c
1574 * Ractor.new {} # enter multi-Ractor mode
1575 * ObjectSpace.each_object {|x| x } # does not yield c
1576 *
1577 */
1578
1579static VALUE
1580os_each_obj(int argc, VALUE *argv, VALUE os)
1581{
1582 VALUE of;
1583
1584 of = (!rb_check_arity(argc, 0, 1) ? 0 : argv[0]);
1585 RETURN_ENUMERATOR(os, 1, &of);
1586 return os_obj_of(of);
1587}
1588
1589/*
1590 * call-seq:
1591 * ObjectSpace.undefine_finalizer(obj)
1592 *
1593 * Removes all finalizers for <i>obj</i>.
1594 *
1595 */
1596
1597static VALUE
1598undefine_final(VALUE os, VALUE obj)
1599{
1600 return rb_undefine_finalizer(obj);
1601}
1602
1603VALUE
1604rb_undefine_finalizer(VALUE obj)
1605{
1606 rb_check_frozen(obj);
1607
1608 rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1609
1610 return obj;
1611}
1612
1613static void
1614should_be_callable(VALUE block)
1615{
1616 if (!rb_obj_respond_to(block, idCall, TRUE)) {
1617 rb_raise(rb_eArgError, "wrong type argument %"PRIsVALUE" (should be callable)",
1618 rb_obj_class(block));
1619 }
1620}
1621
1622static void
1623should_be_finalizable(VALUE obj)
1624{
1625 if (!FL_ABLE(obj)) {
1626 rb_raise(rb_eArgError, "cannot define finalizer for %s",
1627 rb_obj_classname(obj));
1628 }
1629 rb_check_frozen(obj);
1630}
1631
1632void
1633rb_gc_copy_finalizer(VALUE dest, VALUE obj)
1634{
1635 rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1636}
1637
1638/*
1639 * call-seq:
1640 * ObjectSpace.define_finalizer(obj, aProc=proc())
1641 *
1642 * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
1643 * was destroyed. The object ID of the <i>obj</i> will be passed
1644 * as an argument to <i>aProc</i>. If <i>aProc</i> is a lambda or
1645 * method, make sure it can be called with a single argument.
1646 *
1647 * The return value is an array <code>[0, aProc]</code>.
1648 *
1649 * The two recommended patterns are to either create the finaliser proc
1650 * in a non-instance method where it can safely capture the needed state,
1651 * or to use a custom callable object that stores the needed state
1652 * explicitly as instance variables.
1653 *
1654 * class Foo
1655 * def initialize(data_needed_for_finalization)
1656 * ObjectSpace.define_finalizer(self, self.class.create_finalizer(data_needed_for_finalization))
1657 * end
1658 *
1659 * def self.create_finalizer(data_needed_for_finalization)
1660 * proc {
1661 * puts "finalizing #{data_needed_for_finalization}"
1662 * }
1663 * end
1664 * end
1665 *
1666 * class Bar
1667 * class Remover
1668 * def initialize(data_needed_for_finalization)
1669 * @data_needed_for_finalization = data_needed_for_finalization
1670 * end
1671 *
1672 * def call(id)
1673 * puts "finalizing #{@data_needed_for_finalization}"
1674 * end
1675 * end
1676 *
1677 * def initialize(data_needed_for_finalization)
1678 * ObjectSpace.define_finalizer(self, Remover.new(data_needed_for_finalization))
1679 * end
1680 * end
1681 *
1682 * Note that if your finalizer references the object to be
1683 * finalized it will never be run on GC, although it will still be
1684 * run at exit. You will get a warning if you capture the object
1685 * to be finalized as the receiver of the finalizer.
1686 *
1687 * class CapturesSelf
1688 * def initialize(name)
1689 * ObjectSpace.define_finalizer(self, proc {
1690 * # this finalizer will only be run on exit
1691 * puts "finalizing #{name}"
1692 * })
1693 * end
1694 * end
1695 *
1696 * Also note that finalization can be unpredictable and is never guaranteed
1697 * to be run except on exit.
1698 */
1699
1700static VALUE
1701define_final(int argc, VALUE *argv, VALUE os)
1702{
1703 VALUE obj, block;
1704
1705 rb_scan_args(argc, argv, "11", &obj, &block);
1706 if (argc == 1) {
1707 block = rb_block_proc();
1708 }
1709
1710 if (rb_callable_receiver(block) == obj) {
1711 rb_warn("finalizer references object to be finalized");
1712 }
1713
1714 return rb_define_finalizer(obj, block);
1715}
1716
1717VALUE
1718rb_define_finalizer(VALUE obj, VALUE block)
1719{
1720 should_be_finalizable(obj);
1721 should_be_callable(block);
1722
1723 block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1724
1725 block = rb_ary_new3(2, INT2FIX(0), block);
1726 OBJ_FREEZE(block);
1727 return block;
1728}
1729
1730void
1731rb_objspace_call_finalizer(void)
1732{
1733 rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1734}
1735
1736void
1737rb_objspace_free_objects(void *objspace)
1738{
1739 rb_gc_impl_shutdown_free_objects(objspace);
1740}
1741
1742int
1743rb_objspace_garbage_object_p(VALUE obj)
1744{
1745 return !SPECIAL_CONST_P(obj) && rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1746}
1747
1748bool
1749rb_gc_pointer_to_heap_p(VALUE obj)
1750{
1751 return rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj);
1752}
1753
1754#define OBJ_ID_INCREMENT (RUBY_IMMEDIATE_MASK + 1)
1755#define LAST_OBJECT_ID() (object_id_counter * OBJ_ID_INCREMENT)
1756static VALUE id2ref_value = 0;
1757static st_table *id2ref_tbl = NULL;
1758
1759#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1760static size_t object_id_counter = 1;
1761#else
1762static unsigned long long object_id_counter = 1;
1763#endif
1764
1765static inline VALUE
1766generate_next_object_id(void)
1767{
1768#if SIZEOF_SIZE_T == SIZEOF_LONG_LONG
1769 // 64bit atomics are available
1770 return SIZET2NUM(RUBY_ATOMIC_SIZE_FETCH_ADD(object_id_counter, 1) * OBJ_ID_INCREMENT);
1771#else
1772 unsigned int lock_lev = RB_GC_VM_LOCK();
1773 VALUE id = ULL2NUM(++object_id_counter * OBJ_ID_INCREMENT);
1774 RB_GC_VM_UNLOCK(lock_lev);
1775 return id;
1776#endif
1777}
1778
1779void
1780rb_gc_obj_id_moved(VALUE obj)
1781{
1782 if (UNLIKELY(id2ref_tbl)) {
1783 st_insert(id2ref_tbl, (st_data_t)rb_obj_id(obj), (st_data_t)obj);
1784 }
1785}
1786
1787static int
1788object_id_cmp(st_data_t x, st_data_t y)
1789{
1790 if (RB_TYPE_P(x, T_BIGNUM)) {
1791 return !rb_big_eql(x, y);
1792 }
1793 else {
1794 return x != y;
1795 }
1796}
1797
1798static st_index_t
1799object_id_hash(st_data_t n)
1800{
1801 return FIX2LONG(rb_hash((VALUE)n));
1802}
1803
1804static const struct st_hash_type object_id_hash_type = {
1805 object_id_cmp,
1806 object_id_hash,
1807};
1808
1809static void gc_mark_tbl_no_pin(st_table *table);
1810
1811static void
1812id2ref_tbl_mark(void *data)
1813{
1814 st_table *table = (st_table *)data;
1815 if (UNLIKELY(!RB_POSFIXABLE(LAST_OBJECT_ID()))) {
1816 // It's very unlikely, but if enough object ids were generated, keys may be T_BIGNUM
1817 rb_mark_set(table);
1818 }
1819 // We purposedly don't mark values, as they are weak references.
1820 // rb_gc_obj_free_vm_weak_references takes care of cleaning them up.
1821}
1822
1823static size_t
1824id2ref_tbl_memsize(const void *data)
1825{
1826 return rb_st_memsize(data);
1827}
1828
1829static void
1830id2ref_tbl_free(void *data)
1831{
1832 id2ref_tbl = NULL; // clear global ref
1833 st_table *table = (st_table *)data;
1834 st_free_table(table);
1835}
1836
1837static const rb_data_type_t id2ref_tbl_type = {
1838 .wrap_struct_name = "VM/_id2ref_table",
1839 .function = {
1840 .dmark = id2ref_tbl_mark,
1841 .dfree = id2ref_tbl_free,
1842 .dsize = id2ref_tbl_memsize,
1843 // dcompact function not required because the table is reference updated
1844 // in rb_gc_vm_weak_table_foreach
1845 },
1846 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
1847};
1848
1849static VALUE
1850class_object_id(VALUE klass)
1851{
1852 VALUE id = RUBY_ATOMIC_VALUE_LOAD(RCLASS(klass)->object_id);
1853 if (!id) {
1854 unsigned int lock_lev = RB_GC_VM_LOCK();
1855 id = generate_next_object_id();
1856 VALUE existing_id = RUBY_ATOMIC_VALUE_CAS(RCLASS(klass)->object_id, 0, id);
1857 if (existing_id) {
1858 id = existing_id;
1859 }
1860 else if (RB_UNLIKELY(id2ref_tbl)) {
1861 st_insert(id2ref_tbl, id, klass);
1862 }
1863 RB_GC_VM_UNLOCK(lock_lev);
1864 }
1865 return id;
1866}
1867
1868static inline VALUE
1869object_id_get(VALUE obj, shape_id_t shape_id)
1870{
1871 VALUE id;
1872 if (rb_shape_too_complex_p(shape_id)) {
1873 id = rb_obj_field_get(obj, ROOT_TOO_COMPLEX_WITH_OBJ_ID);
1874 }
1875 else {
1876 id = rb_obj_field_get(obj, rb_shape_object_id(shape_id));
1877 }
1878
1879#if RUBY_DEBUG
1880 if (!(FIXNUM_P(id) || RB_TYPE_P(id, T_BIGNUM))) {
1881 rb_p(obj);
1882 rb_bug("Object's shape includes object_id, but it's missing %s", rb_obj_info(obj));
1883 }
1884#endif
1885
1886 return id;
1887}
1888
1889static VALUE
1890object_id0(VALUE obj)
1891{
1892 VALUE id = Qfalse;
1893 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1894
1895 if (rb_shape_has_object_id(shape_id)) {
1896 return object_id_get(obj, shape_id);
1897 }
1898
1899 shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
1900
1901 id = generate_next_object_id();
1902 rb_obj_field_set(obj, object_id_shape_id, 0, id);
1903
1904 RUBY_ASSERT(RBASIC_SHAPE_ID(obj) == object_id_shape_id);
1905 RUBY_ASSERT(rb_shape_obj_has_id(obj));
1906
1907 if (RB_UNLIKELY(id2ref_tbl)) {
1908 st_insert(id2ref_tbl, (st_data_t)id, (st_data_t)obj);
1909 }
1910 return id;
1911}
1912
1913static VALUE
1914object_id(VALUE obj)
1915{
1916 switch (BUILTIN_TYPE(obj)) {
1917 case T_CLASS:
1918 case T_MODULE:
1919 // With namespaces, classes and modules have different fields
1920 // in different namespaces, so we cannot store the object id
1921 // in fields.
1922 return class_object_id(obj);
1923 case T_IMEMO:
1924 RUBY_ASSERT(IMEMO_TYPE_P(obj, imemo_fields));
1925 break;
1926 default:
1927 break;
1928 }
1929
1930 if (UNLIKELY(rb_gc_multi_ractor_p() && rb_ractor_shareable_p(obj))) {
1931 unsigned int lock_lev = RB_GC_VM_LOCK();
1932 VALUE id = object_id0(obj);
1933 RB_GC_VM_UNLOCK(lock_lev);
1934 return id;
1935 }
1936
1937 return object_id0(obj);
1938}
1939
1940static void
1941build_id2ref_i(VALUE obj, void *data)
1942{
1943 st_table *id2ref_tbl = (st_table *)data;
1944
1945 switch (BUILTIN_TYPE(obj)) {
1946 case T_CLASS:
1947 case T_MODULE:
1948 RUBY_ASSERT(!rb_objspace_garbage_object_p(obj));
1949 if (RCLASS(obj)->object_id) {
1950 st_insert(id2ref_tbl, RCLASS(obj)->object_id, obj);
1951 }
1952 break;
1953 case T_IMEMO:
1954 RUBY_ASSERT(!rb_objspace_garbage_object_p(obj));
1955 if (IMEMO_TYPE_P(obj, imemo_fields) && rb_shape_obj_has_id(obj)) {
1956 st_insert(id2ref_tbl, rb_obj_id(obj), rb_imemo_fields_owner(obj));
1957 }
1958 break;
1959 case T_OBJECT:
1960 RUBY_ASSERT(!rb_objspace_garbage_object_p(obj));
1961 if (rb_shape_obj_has_id(obj)) {
1962 st_insert(id2ref_tbl, rb_obj_id(obj), obj);
1963 }
1964 break;
1965 default:
1966 // For generic_fields, the T_IMEMO/fields is responsible for populating the entry.
1967 break;
1968 }
1969}
1970
1971static VALUE
1972object_id_to_ref(void *objspace_ptr, VALUE object_id)
1973{
1974 rb_objspace_t *objspace = objspace_ptr;
1975
1976 unsigned int lev = RB_GC_VM_LOCK();
1977
1978 if (!id2ref_tbl) {
1979 rb_gc_vm_barrier(); // stop other ractors
1980
1981 // GC Must not trigger while we build the table, otherwise if we end
1982 // up freeing an object that had an ID, we might try to delete it from
1983 // the table even though it wasn't inserted yet.
1984 st_table *tmp_id2ref_tbl = st_init_table(&object_id_hash_type);
1985 VALUE tmp_id2ref_value = TypedData_Wrap_Struct(0, &id2ref_tbl_type, tmp_id2ref_tbl);
1986
1987 // build_id2ref_i will most certainly malloc, which could trigger GC and sweep
1988 // objects we just added to the table.
1989 // By calling rb_gc_disable() we also save having to handle potentially garbage objects.
1990 bool gc_disabled = RTEST(rb_gc_disable());
1991 {
1992 id2ref_tbl = tmp_id2ref_tbl;
1993 id2ref_value = tmp_id2ref_value;
1994
1995 rb_gc_impl_each_object(objspace, build_id2ref_i, (void *)id2ref_tbl);
1996 }
1997 if (!gc_disabled) rb_gc_enable();
1998 }
1999
2000 VALUE obj;
2001 bool found = st_lookup(id2ref_tbl, object_id, &obj) && !rb_gc_impl_garbage_object_p(objspace, obj);
2002
2003 RB_GC_VM_UNLOCK(lev);
2004
2005 if (found) {
2006 return obj;
2007 }
2008
2009 if (rb_funcall(object_id, rb_intern(">="), 1, ULL2NUM(LAST_OBJECT_ID()))) {
2010 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is not an id value", rb_funcall(object_id, rb_intern("to_s"), 1, INT2FIX(10)));
2011 }
2012 else {
2013 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is a recycled object", rb_funcall(object_id, rb_intern("to_s"), 1, INT2FIX(10)));
2014 }
2015}
2016
2017static inline void
2018obj_free_object_id(VALUE obj)
2019{
2020 VALUE obj_id = 0;
2021 if (RB_UNLIKELY(id2ref_tbl)) {
2022 switch (BUILTIN_TYPE(obj)) {
2023 case T_CLASS:
2024 case T_MODULE:
2025 obj_id = RCLASS(obj)->object_id;
2026 break;
2027 case T_IMEMO:
2028 if (!IMEMO_TYPE_P(obj, imemo_fields)) {
2029 return;
2030 }
2031 // fallthrough
2032 case T_OBJECT:
2033 {
2034 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2035 if (rb_shape_has_object_id(shape_id)) {
2036 obj_id = object_id_get(obj, shape_id);
2037 }
2038 break;
2039 }
2040 default:
2041 // For generic_fields, the T_IMEMO/fields is responsible for freeing the id.
2042 return;
2043 }
2044
2045 if (RB_UNLIKELY(obj_id)) {
2046 RUBY_ASSERT(FIXNUM_P(obj_id) || RB_TYPE_P(obj_id, T_BIGNUM));
2047
2048 if (!st_delete(id2ref_tbl, (st_data_t *)&obj_id, NULL)) {
2049 // The the object is a T_IMEMO/fields, then it's possible the actual object
2050 // has been garbage collected already.
2051 if (!RB_TYPE_P(obj, T_IMEMO)) {
2052 rb_bug("Object ID seen, but not in _id2ref table: object_id=%llu object=%s", NUM2ULL(obj_id), rb_obj_info(obj));
2053 }
2054 }
2055 }
2056 }
2057}
2058
2059void
2060rb_gc_obj_free_vm_weak_references(VALUE obj)
2061{
2062 obj_free_object_id(obj);
2063
2064 if (rb_obj_exivar_p(obj)) {
2066 }
2067
2068 switch (BUILTIN_TYPE(obj)) {
2069 case T_STRING:
2070 if (FL_TEST_RAW(obj, RSTRING_FSTR)) {
2071 rb_gc_free_fstring(obj);
2072 }
2073 break;
2074 case T_SYMBOL:
2075 rb_gc_free_dsymbol(obj);
2076 break;
2077 case T_IMEMO:
2078 switch (imemo_type(obj)) {
2079 case imemo_callcache: {
2080 const struct rb_callcache *cc = (const struct rb_callcache *)obj;
2081
2082 if (vm_cc_refinement_p(cc)) {
2083 rb_vm_delete_cc_refinement(cc);
2084 }
2085
2086 break;
2087 }
2088 case imemo_callinfo:
2089 rb_vm_ci_free((const struct rb_callinfo *)obj);
2090 break;
2091 case imemo_ment:
2092 rb_free_method_entry_vm_weak_references((const rb_method_entry_t *)obj);
2093 break;
2094 default:
2095 break;
2096 }
2097 break;
2098 default:
2099 break;
2100 }
2101}
2102
2103/*
2104 * call-seq:
2105 * ObjectSpace._id2ref(object_id) -> an_object
2106 *
2107 * Converts an object id to a reference to the object. May not be
2108 * called on an object id passed as a parameter to a finalizer.
2109 *
2110 * s = "I am a string" #=> "I am a string"
2111 * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
2112 * r == s #=> true
2113 *
2114 * On multi-ractor mode, if the object is not shareable, it raises
2115 * RangeError.
2116 *
2117 * This method is deprecated and should no longer be used.
2118 */
2119
2120static VALUE
2121id2ref(VALUE objid)
2122{
2123#if SIZEOF_LONG == SIZEOF_VOIDP
2124#define NUM2PTR(x) NUM2ULONG(x)
2125#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2126#define NUM2PTR(x) NUM2ULL(x)
2127#endif
2128 objid = rb_to_int(objid);
2129 if (FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
2130 VALUE ptr = NUM2PTR(objid);
2131 if (SPECIAL_CONST_P(ptr)) {
2132 if (ptr == Qtrue) return Qtrue;
2133 if (ptr == Qfalse) return Qfalse;
2134 if (NIL_P(ptr)) return Qnil;
2135 if (FIXNUM_P(ptr)) return ptr;
2136 if (FLONUM_P(ptr)) return ptr;
2137
2138 if (SYMBOL_P(ptr)) {
2139 // Check that the symbol is valid
2140 if (rb_static_id_valid_p(SYM2ID(ptr))) {
2141 return ptr;
2142 }
2143 else {
2144 rb_raise(rb_eRangeError, "%p is not a symbol id value", (void *)ptr);
2145 }
2146 }
2147
2148 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is not an id value", rb_int2str(objid, 10));
2149 }
2150 }
2151
2152 VALUE obj = object_id_to_ref(rb_gc_get_objspace(), objid);
2153 if (!rb_multi_ractor_p() || rb_ractor_shareable_p(obj)) {
2154 return obj;
2155 }
2156 else {
2157 rb_raise(rb_eRangeError, "%+"PRIsVALUE" is the id of an unshareable object on multi-ractor", rb_int2str(objid, 10));
2158 }
2159}
2160
2161/* :nodoc: */
2162static VALUE
2163os_id2ref(VALUE os, VALUE objid)
2164{
2165 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "ObjectSpace._id2ref is deprecated");
2166 return id2ref(objid);
2167}
2168
2169static VALUE
2170rb_find_object_id(void *objspace, VALUE obj, VALUE (*get_heap_object_id)(VALUE))
2171{
2172 if (SPECIAL_CONST_P(obj)) {
2173#if SIZEOF_LONG == SIZEOF_VOIDP
2174 return LONG2NUM((SIGNED_VALUE)obj);
2175#else
2176 return LL2NUM((SIGNED_VALUE)obj);
2177#endif
2178 }
2179
2180 return get_heap_object_id(obj);
2181}
2182
2183static VALUE
2184nonspecial_obj_id(VALUE obj)
2185{
2186#if SIZEOF_LONG == SIZEOF_VOIDP
2187 return (VALUE)((SIGNED_VALUE)(obj)|FIXNUM_FLAG);
2188#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
2189 return LL2NUM((SIGNED_VALUE)(obj) / 2);
2190#else
2191# error not supported
2192#endif
2193}
2194
2195VALUE
2196rb_memory_id(VALUE obj)
2197{
2198 return rb_find_object_id(NULL, obj, nonspecial_obj_id);
2199}
2200
2201/*
2202 * Document-method: __id__
2203 * Document-method: object_id
2204 *
2205 * call-seq:
2206 * obj.__id__ -> integer
2207 * obj.object_id -> integer
2208 *
2209 * Returns an integer identifier for +obj+.
2210 *
2211 * The same number will be returned on all calls to +object_id+ for a given
2212 * object, and no two active objects will share an id.
2213 *
2214 * Note: that some objects of builtin classes are reused for optimization.
2215 * This is the case for immediate values and frozen string literals.
2216 *
2217 * BasicObject implements +__id__+, Kernel implements +object_id+.
2218 *
2219 * Immediate values are not passed by reference but are passed by value:
2220 * +nil+, +true+, +false+, Fixnums, Symbols, and some Floats.
2221 *
2222 * Object.new.object_id == Object.new.object_id # => false
2223 * (21 * 2).object_id == (21 * 2).object_id # => true
2224 * "hello".object_id == "hello".object_id # => false
2225 * "hi".freeze.object_id == "hi".freeze.object_id # => true
2226 */
2227
2228VALUE
2229rb_obj_id(VALUE obj)
2230{
2231 /* If obj is an immediate, the object ID is obj directly converted to a Numeric.
2232 * Otherwise, the object ID is a Numeric that is a non-zero multiple of
2233 * (RUBY_IMMEDIATE_MASK + 1) which guarantees that it does not collide with
2234 * any immediates. */
2235 return rb_find_object_id(rb_gc_get_objspace(), obj, object_id);
2236}
2237
2238bool
2239rb_obj_id_p(VALUE obj)
2240{
2241 return !RB_TYPE_P(obj, T_IMEMO) && rb_shape_obj_has_id(obj);
2242}
2243
2244/*
2245 * GC implementations should call this function before the GC phase that updates references
2246 * embedded in the machine code generated by JIT compilers. JIT compilers usually enforce the
2247 * "W^X" policy and protect the code memory from being modified during execution. This function
2248 * makes the code memory writeable.
2249 */
2250void
2251rb_gc_before_updating_jit_code(void)
2252{
2253#if USE_YJIT
2254 rb_yjit_mark_all_writeable();
2255#endif
2256}
2257
2258/*
2259 * GC implementations should call this function before the GC phase that updates references
2260 * embedded in the machine code generated by JIT compilers. This function makes the code memory
2261 * executable again.
2262 */
2263void
2264rb_gc_after_updating_jit_code(void)
2265{
2266#if USE_YJIT
2267 rb_yjit_mark_all_executable();
2268#endif
2269}
2270
2271static void
2272classext_memsize(rb_classext_t *ext, bool prime, VALUE namespace, void *arg)
2273{
2274 size_t *size = (size_t *)arg;
2275 size_t s = 0;
2276
2277 if (RCLASSEXT_M_TBL(ext)) {
2278 s += rb_id_table_memsize(RCLASSEXT_M_TBL(ext));
2279 }
2280 if (RCLASSEXT_CVC_TBL(ext)) {
2281 s += rb_id_table_memsize(RCLASSEXT_CVC_TBL(ext));
2282 }
2283 if (RCLASSEXT_CONST_TBL(ext)) {
2284 s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
2285 }
2286 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2287 s += (RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1) * sizeof(VALUE);
2288 }
2289 if (!prime) {
2290 s += sizeof(rb_classext_t);
2291 }
2292 *size += s;
2293}
2294
2295static void
2296classext_superclasses_memsize(rb_classext_t *ext, bool prime, VALUE namespace, void *arg)
2297{
2298 size_t *size = (size_t *)arg;
2299 size_t array_size;
2300 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
2301 RUBY_ASSERT(prime);
2302 array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
2303 *size += array_size * sizeof(VALUE);
2304 }
2305}
2306
2307size_t
2308rb_obj_memsize_of(VALUE obj)
2309{
2310 size_t size = 0;
2311
2312 if (SPECIAL_CONST_P(obj)) {
2313 return 0;
2314 }
2315
2316 switch (BUILTIN_TYPE(obj)) {
2317 case T_OBJECT:
2318 if (rb_shape_obj_too_complex_p(obj)) {
2319 size += rb_st_memsize(ROBJECT_FIELDS_HASH(obj));
2320 }
2321 else if (!(RBASIC(obj)->flags & ROBJECT_EMBED)) {
2322 size += ROBJECT_FIELDS_CAPACITY(obj) * sizeof(VALUE);
2323 }
2324 break;
2325 case T_MODULE:
2326 case T_CLASS:
2327 rb_class_classext_foreach(obj, classext_memsize, (void *)&size);
2328 rb_class_classext_foreach(obj, classext_superclasses_memsize, (void *)&size);
2329 break;
2330 case T_ICLASS:
2331 if (RICLASS_OWNS_M_TBL_P(obj)) {
2332 if (RCLASS_M_TBL(obj)) {
2333 size += rb_id_table_memsize(RCLASS_M_TBL(obj));
2334 }
2335 }
2336 break;
2337 case T_STRING:
2338 size += rb_str_memsize(obj);
2339 break;
2340 case T_ARRAY:
2341 size += rb_ary_memsize(obj);
2342 break;
2343 case T_HASH:
2344 if (RHASH_ST_TABLE_P(obj)) {
2345 VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
2346 /* st_table is in the slot */
2347 size += st_memsize(RHASH_ST_TABLE(obj)) - sizeof(st_table);
2348 }
2349 break;
2350 case T_REGEXP:
2351 if (RREGEXP_PTR(obj)) {
2352 size += onig_memsize(RREGEXP_PTR(obj));
2353 }
2354 break;
2355 case T_DATA:
2356 size += rb_objspace_data_type_memsize(obj);
2357 break;
2358 case T_MATCH:
2359 {
2360 rb_matchext_t *rm = RMATCH_EXT(obj);
2361 size += onig_region_memsize(&rm->regs);
2362 size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated;
2363 }
2364 break;
2365 case T_FILE:
2366 if (RFILE(obj)->fptr) {
2367 size += rb_io_memsize(RFILE(obj)->fptr);
2368 }
2369 break;
2370 case T_RATIONAL:
2371 case T_COMPLEX:
2372 break;
2373 case T_IMEMO:
2374 size += rb_imemo_memsize(obj);
2375 break;
2376
2377 case T_FLOAT:
2378 case T_SYMBOL:
2379 break;
2380
2381 case T_BIGNUM:
2382 if (!(RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
2383 size += BIGNUM_LEN(obj) * sizeof(BDIGIT);
2384 }
2385 break;
2386
2387 case T_NODE:
2388 UNEXPECTED_NODE(obj_memsize_of);
2389 break;
2390
2391 case T_STRUCT:
2392 if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
2393 RSTRUCT(obj)->as.heap.ptr) {
2394 size += sizeof(VALUE) * RSTRUCT_LEN(obj);
2395 }
2396 break;
2397
2398 case T_ZOMBIE:
2399 case T_MOVED:
2400 break;
2401
2402 default:
2403 rb_bug("objspace/memsize_of(): unknown data type 0x%x(%p)",
2404 BUILTIN_TYPE(obj), (void*)obj);
2405 }
2406
2407 return size + rb_gc_obj_slot_size(obj);
2408}
2409
2410static int
2411set_zero(st_data_t key, st_data_t val, st_data_t arg)
2412{
2413 VALUE k = (VALUE)key;
2414 VALUE hash = (VALUE)arg;
2415 rb_hash_aset(hash, k, INT2FIX(0));
2416 return ST_CONTINUE;
2417}
2418
2420 size_t counts[T_MASK+1];
2421 size_t freed;
2422 size_t total;
2423};
2424
2425static void
2426count_objects_i(VALUE obj, void *d)
2427{
2428 struct count_objects_data *data = (struct count_objects_data *)d;
2429
2430 if (RBASIC(obj)->flags) {
2431 data->counts[BUILTIN_TYPE(obj)]++;
2432 }
2433 else {
2434 data->freed++;
2435 }
2436
2437 data->total++;
2438}
2439
2440/*
2441 * call-seq:
2442 * ObjectSpace.count_objects([result_hash]) -> hash
2443 *
2444 * Counts all objects grouped by type.
2445 *
2446 * It returns a hash, such as:
2447 * {
2448 * :TOTAL=>10000,
2449 * :FREE=>3011,
2450 * :T_OBJECT=>6,
2451 * :T_CLASS=>404,
2452 * # ...
2453 * }
2454 *
2455 * The contents of the returned hash are implementation specific.
2456 * It may be changed in future.
2457 *
2458 * The keys starting with +:T_+ means live objects.
2459 * For example, +:T_ARRAY+ is the number of arrays.
2460 * +:FREE+ means object slots which is not used now.
2461 * +:TOTAL+ means sum of above.
2462 *
2463 * If the optional argument +result_hash+ is given,
2464 * it is overwritten and returned. This is intended to avoid probe effect.
2465 *
2466 * h = {}
2467 * ObjectSpace.count_objects(h)
2468 * puts h
2469 * # => { :TOTAL=>10000, :T_CLASS=>158280, :T_MODULE=>20672, :T_STRING=>527249 }
2470 *
2471 * This method is only expected to work on C Ruby.
2472 *
2473 */
2474
2475static VALUE
2476count_objects(int argc, VALUE *argv, VALUE os)
2477{
2478 struct count_objects_data data = { 0 };
2479 VALUE hash = Qnil;
2480 VALUE types[T_MASK + 1];
2481
2482 if (rb_check_arity(argc, 0, 1) == 1) {
2483 hash = argv[0];
2484 if (!RB_TYPE_P(hash, T_HASH))
2485 rb_raise(rb_eTypeError, "non-hash given");
2486 }
2487
2488 for (size_t i = 0; i <= T_MASK; i++) {
2489 // type_sym can allocate an object,
2490 // so we need to create all key symbols in advance
2491 // not to disturb the result
2492 types[i] = type_sym(i);
2493 }
2494
2495 rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
2496
2497 if (NIL_P(hash)) {
2498 hash = rb_hash_new();
2499 }
2500 else if (!RHASH_EMPTY_P(hash)) {
2501 rb_hash_stlike_foreach(hash, set_zero, hash);
2502 }
2503 rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(data.total));
2504 rb_hash_aset(hash, ID2SYM(rb_intern("FREE")), SIZET2NUM(data.freed));
2505
2506 for (size_t i = 0; i <= T_MASK; i++) {
2507 if (data.counts[i]) {
2508 rb_hash_aset(hash, types[i], SIZET2NUM(data.counts[i]));
2509 }
2510 }
2511
2512 return hash;
2513}
2514
2515#define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
2516
2517#define STACK_START (ec->machine.stack_start)
2518#define STACK_END (ec->machine.stack_end)
2519#define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
2520
2521#if STACK_GROW_DIRECTION < 0
2522# define STACK_LENGTH (size_t)(STACK_START - STACK_END)
2523#elif STACK_GROW_DIRECTION > 0
2524# define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
2525#else
2526# define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2527 : (size_t)(STACK_END - STACK_START + 1))
2528#endif
2529#if !STACK_GROW_DIRECTION
2530int ruby_stack_grow_direction;
2531int
2532ruby_get_stack_grow_direction(volatile VALUE *addr)
2533{
2534 VALUE *end;
2535 SET_MACHINE_STACK_END(&end);
2536
2537 if (end > addr) return ruby_stack_grow_direction = 1;
2538 return ruby_stack_grow_direction = -1;
2539}
2540#endif
2541
2542size_t
2544{
2545 rb_execution_context_t *ec = GET_EC();
2546 SET_STACK_END;
2547 if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2548 return STACK_LENGTH;
2549}
2550
2551#define PREVENT_STACK_OVERFLOW 1
2552#ifndef PREVENT_STACK_OVERFLOW
2553#if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2554# define PREVENT_STACK_OVERFLOW 1
2555#else
2556# define PREVENT_STACK_OVERFLOW 0
2557#endif
2558#endif
2559#if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2560static int
2561stack_check(rb_execution_context_t *ec, int water_mark)
2562{
2563 SET_STACK_END;
2564
2565 size_t length = STACK_LENGTH;
2566 size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2567
2568 return length > maximum_length;
2569}
2570#else
2571#define stack_check(ec, water_mark) FALSE
2572#endif
2573
2574#define STACKFRAME_FOR_CALL_CFUNC 2048
2575
2576int
2577rb_ec_stack_check(rb_execution_context_t *ec)
2578{
2579 return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2580}
2581
2582int
2584{
2585 return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2586}
2587
2588/* ==================== Marking ==================== */
2589
2590#define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2591 if (!RB_SPECIAL_CONST_P(obj)) { \
2592 rb_vm_t *vm = GET_VM(); \
2593 void *objspace = vm->gc.objspace; \
2594 if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2595 GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2596 (func)(objspace, (obj_or_ptr)); \
2597 } \
2598 else if (check_obj ? \
2599 rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2600 !rb_gc_impl_garbage_object_p(objspace, obj) : \
2601 true) { \
2602 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2603 struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2604 vm->gc.mark_func_data = NULL; \
2605 mark_func_data->mark_func((obj), mark_func_data->data); \
2606 vm->gc.mark_func_data = mark_func_data; \
2607 } \
2608 } \
2609} while (0)
2610
2611static inline void
2612gc_mark_internal(VALUE obj)
2613{
2614 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj, false);
2615}
2616
2617void
2618rb_gc_mark_movable(VALUE obj)
2619{
2620 gc_mark_internal(obj);
2621}
2622
2623void
2624rb_gc_mark_and_move(VALUE *ptr)
2625{
2626 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr, false);
2627}
2628
2629static inline void
2630gc_mark_and_pin_internal(VALUE obj)
2631{
2632 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj, false);
2633}
2634
2635void
2636rb_gc_mark(VALUE obj)
2637{
2638 gc_mark_and_pin_internal(obj);
2639}
2640
2641static inline void
2642gc_mark_maybe_internal(VALUE obj)
2643{
2644 RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj, true);
2645}
2646
2647void
2648rb_gc_mark_maybe(VALUE obj)
2649{
2650 gc_mark_maybe_internal(obj);
2651}
2652
2653void
2654rb_gc_mark_weak(VALUE *ptr)
2655{
2656 if (RB_SPECIAL_CONST_P(*ptr)) return;
2657
2658 rb_vm_t *vm = GET_VM();
2659 void *objspace = vm->gc.objspace;
2660 if (LIKELY(vm->gc.mark_func_data == NULL)) {
2661 GC_ASSERT(rb_gc_impl_during_gc_p(objspace));
2662
2663 rb_gc_impl_mark_weak(objspace, ptr);
2664 }
2665 else {
2666 GC_ASSERT(!rb_gc_impl_during_gc_p(objspace));
2667 }
2668}
2669
2670void
2671rb_gc_remove_weak(VALUE parent_obj, VALUE *ptr)
2672{
2673 rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2674}
2675
2676ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static void each_location(register const VALUE *x, register long n, void (*cb)(VALUE, void *), void *data));
2677static void
2678each_location(register const VALUE *x, register long n, void (*cb)(VALUE, void *), void *data)
2679{
2680 VALUE v;
2681 while (n--) {
2682 v = *x;
2683 cb(v, data);
2684 x++;
2685 }
2686}
2687
2688static void
2689each_location_ptr(const VALUE *start, const VALUE *end, void (*cb)(VALUE, void *), void *data)
2690{
2691 if (end <= start) return;
2692 each_location(start, end - start, cb, data);
2693}
2694
2695static void
2696gc_mark_maybe_each_location(VALUE obj, void *data)
2697{
2698 gc_mark_maybe_internal(obj);
2699}
2700
2701void
2702rb_gc_mark_locations(const VALUE *start, const VALUE *end)
2703{
2704 each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2705}
2706
2707void
2708rb_gc_mark_values(long n, const VALUE *values)
2709{
2710 for (long i = 0; i < n; i++) {
2711 gc_mark_internal(values[i]);
2712 }
2713}
2714
2715void
2716rb_gc_mark_vm_stack_values(long n, const VALUE *values)
2717{
2718 for (long i = 0; i < n; i++) {
2719 gc_mark_and_pin_internal(values[i]);
2720 }
2721}
2722
2723static int
2724mark_key(st_data_t key, st_data_t value, st_data_t data)
2725{
2726 gc_mark_and_pin_internal((VALUE)key);
2727
2728 return ST_CONTINUE;
2729}
2730
2731void
2732rb_mark_set(st_table *tbl)
2733{
2734 if (!tbl) return;
2735
2736 st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2737}
2738
2739static int
2740mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2741{
2742 gc_mark_internal((VALUE)key);
2743 gc_mark_internal((VALUE)value);
2744
2745 return ST_CONTINUE;
2746}
2747
2748static int
2749pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2750{
2751 gc_mark_and_pin_internal((VALUE)key);
2752 gc_mark_and_pin_internal((VALUE)value);
2753
2754 return ST_CONTINUE;
2755}
2756
2757static int
2758pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2759{
2760 gc_mark_and_pin_internal((VALUE)key);
2761 gc_mark_internal((VALUE)value);
2762
2763 return ST_CONTINUE;
2764}
2765
2766static void
2767mark_hash(VALUE hash)
2768{
2769 if (rb_hash_compare_by_id_p(hash)) {
2770 rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2771 }
2772 else {
2773 rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2774 }
2775
2776 gc_mark_internal(RHASH(hash)->ifnone);
2777}
2778
2779void
2780rb_mark_hash(st_table *tbl)
2781{
2782 if (!tbl) return;
2783
2784 st_foreach(tbl, pin_key_pin_value, 0);
2785}
2786
2787static enum rb_id_table_iterator_result
2788mark_method_entry_i(VALUE me, void *objspace)
2789{
2790 gc_mark_internal(me);
2791
2792 return ID_TABLE_CONTINUE;
2793}
2794
2795static void
2796mark_m_tbl(void *objspace, struct rb_id_table *tbl)
2797{
2798 if (tbl) {
2799 rb_id_table_foreach_values(tbl, mark_method_entry_i, objspace);
2800 }
2801}
2802
2803static enum rb_id_table_iterator_result
2804mark_const_entry_i(VALUE value, void *objspace)
2805{
2806 const rb_const_entry_t *ce = (const rb_const_entry_t *)value;
2807
2808 gc_mark_internal(ce->value);
2809 gc_mark_internal(ce->file);
2810 return ID_TABLE_CONTINUE;
2811}
2812
2813static void
2814mark_const_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
2815{
2816 if (!tbl) return;
2817 rb_id_table_foreach_values(tbl, mark_const_entry_i, objspace);
2818}
2819
2820static enum rb_id_table_iterator_result
2821mark_cvc_tbl_i(VALUE cvc_entry, void *objspace)
2822{
2823 struct rb_cvar_class_tbl_entry *entry;
2824
2825 entry = (struct rb_cvar_class_tbl_entry *)cvc_entry;
2826
2827 RUBY_ASSERT(entry->cref == 0 || (BUILTIN_TYPE((VALUE)entry->cref) == T_IMEMO && IMEMO_TYPE_P(entry->cref, imemo_cref)));
2828 gc_mark_internal((VALUE)entry->cref);
2829
2830 return ID_TABLE_CONTINUE;
2831}
2832
2833static void
2834mark_cvc_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
2835{
2836 if (!tbl) return;
2837 rb_id_table_foreach_values(tbl, mark_cvc_tbl_i, objspace);
2838}
2839
2840#if STACK_GROW_DIRECTION < 0
2841#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2842#elif STACK_GROW_DIRECTION > 0
2843#define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2844#else
2845#define GET_STACK_BOUNDS(start, end, appendix) \
2846 ((STACK_END < STACK_START) ? \
2847 ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2848#endif
2849
2850static void
2851gc_mark_machine_stack_location_maybe(VALUE obj, void *data)
2852{
2853 gc_mark_maybe_internal(obj);
2854
2855#ifdef RUBY_ASAN_ENABLED
2856 const rb_execution_context_t *ec = (const rb_execution_context_t *)data;
2857 void *fake_frame_start;
2858 void *fake_frame_end;
2859 bool is_fake_frame = asan_get_fake_stack_extents(
2860 ec->machine.asan_fake_stack_handle, obj,
2861 ec->machine.stack_start, ec->machine.stack_end,
2862 &fake_frame_start, &fake_frame_end
2863 );
2864 if (is_fake_frame) {
2865 each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2866 }
2867#endif
2868}
2869
2870static VALUE
2871gc_location_internal(void *objspace, VALUE value)
2872{
2873 if (SPECIAL_CONST_P(value)) {
2874 return value;
2875 }
2876
2877 GC_ASSERT(rb_gc_impl_pointer_to_heap_p(objspace, (void *)value));
2878
2879 return rb_gc_impl_location(objspace, value);
2880}
2881
2882VALUE
2883rb_gc_location(VALUE value)
2884{
2885 return gc_location_internal(rb_gc_get_objspace(), value);
2886}
2887
2888#if defined(__wasm__)
2889
2890
2891static VALUE *rb_stack_range_tmp[2];
2892
2893static void
2894rb_mark_locations(void *begin, void *end)
2895{
2896 rb_stack_range_tmp[0] = begin;
2897 rb_stack_range_tmp[1] = end;
2898}
2899
2900void
2901rb_gc_save_machine_context(void)
2902{
2903 // no-op
2904}
2905
2906# if defined(__EMSCRIPTEN__)
2907
2908static void
2909mark_current_machine_context(const rb_execution_context_t *ec)
2910{
2911 emscripten_scan_stack(rb_mark_locations);
2912 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2913
2914 emscripten_scan_registers(rb_mark_locations);
2915 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2916}
2917# else // use Asyncify version
2918
2919static void
2920mark_current_machine_context(rb_execution_context_t *ec)
2921{
2922 VALUE *stack_start, *stack_end;
2923 SET_STACK_END;
2924 GET_STACK_BOUNDS(stack_start, stack_end, 1);
2925 each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2926
2927 rb_wasm_scan_locals(rb_mark_locations);
2928 each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2929}
2930
2931# endif
2932
2933#else // !defined(__wasm__)
2934
2935void
2936rb_gc_save_machine_context(void)
2937{
2938 rb_thread_t *thread = GET_THREAD();
2939
2940 RB_VM_SAVE_MACHINE_CONTEXT(thread);
2941}
2942
2943
2944static void
2945mark_current_machine_context(const rb_execution_context_t *ec)
2946{
2947 rb_gc_mark_machine_context(ec);
2948}
2949#endif
2950
2951void
2952rb_gc_mark_machine_context(const rb_execution_context_t *ec)
2953{
2954 VALUE *stack_start, *stack_end;
2955
2956 GET_STACK_BOUNDS(stack_start, stack_end, 0);
2957 RUBY_DEBUG_LOG("ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2958
2959 void *data =
2960#ifdef RUBY_ASAN_ENABLED
2961 /* gc_mark_machine_stack_location_maybe() uses data as const */
2963#else
2964 NULL;
2965#endif
2966
2967 each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2968 int num_regs = sizeof(ec->machine.regs)/(sizeof(VALUE));
2969 each_location((VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2970}
2971
2972static int
2973rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2974{
2975 gc_mark_and_pin_internal((VALUE)value);
2976
2977 return ST_CONTINUE;
2978}
2979
2980void
2981rb_mark_tbl(st_table *tbl)
2982{
2983 if (!tbl || tbl->num_entries == 0) return;
2984
2985 st_foreach(tbl, rb_mark_tbl_i, 0);
2986}
2987
2988static void
2989gc_mark_tbl_no_pin(st_table *tbl)
2990{
2991 if (!tbl || tbl->num_entries == 0) return;
2992
2993 st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
2994}
2995
2996void
2997rb_mark_tbl_no_pin(st_table *tbl)
2998{
2999 gc_mark_tbl_no_pin(tbl);
3000}
3001
3002static bool
3003gc_declarative_marking_p(const rb_data_type_t *type)
3004{
3005 return (type->flags & RUBY_TYPED_DECL_MARKING) != 0;
3006}
3007
3008void
3009rb_gc_mark_roots(void *objspace, const char **categoryp)
3010{
3011 rb_execution_context_t *ec = GET_EC();
3012 rb_vm_t *vm = rb_ec_vm_ptr(ec);
3013
3014#define MARK_CHECKPOINT(category) do { \
3015 if (categoryp) *categoryp = category; \
3016} while (0)
3017
3018 MARK_CHECKPOINT("vm");
3019 rb_vm_mark(vm);
3020
3021 MARK_CHECKPOINT("end_proc");
3022 rb_mark_end_proc();
3023
3024 MARK_CHECKPOINT("global_tbl");
3025 rb_gc_mark_global_tbl();
3026
3027#if USE_YJIT
3028 void rb_yjit_root_mark(void); // in Rust
3029
3030 if (rb_yjit_enabled_p) {
3031 MARK_CHECKPOINT("YJIT");
3032 rb_yjit_root_mark();
3033 }
3034#endif
3035
3036 MARK_CHECKPOINT("machine_context");
3037 mark_current_machine_context(ec);
3038
3039 MARK_CHECKPOINT("global_symbols");
3040 rb_sym_global_symbols_mark_and_move();
3041
3042 MARK_CHECKPOINT("finish");
3043
3044#undef MARK_CHECKPOINT
3045}
3046
3051
3052static void
3053gc_mark_classext_module(rb_classext_t *ext, bool prime, VALUE namespace, void *arg)
3054{
3056 rb_objspace_t *objspace = foreach_arg->objspace;
3057
3058 if (RCLASSEXT_SUPER(ext)) {
3059 gc_mark_internal(RCLASSEXT_SUPER(ext));
3060 }
3061 mark_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3062 gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
3063 if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
3064 mark_const_tbl(objspace, RCLASSEXT_CONST_TBL(ext));
3065 }
3066 mark_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3067 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3068 mark_cvc_tbl(objspace, RCLASSEXT_CVC_TBL(ext));
3069 gc_mark_internal(RCLASSEXT_CLASSPATH(ext));
3070}
3071
3072static void
3073gc_mark_classext_iclass(rb_classext_t *ext, bool prime, VALUE namespace, void *arg)
3074{
3076 rb_objspace_t *objspace = foreach_arg->objspace;
3077
3078 if (RCLASSEXT_SUPER(ext)) {
3079 gc_mark_internal(RCLASSEXT_SUPER(ext));
3080 }
3081 if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
3082 mark_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3083 }
3084 if (RCLASSEXT_INCLUDER(ext)) {
3085 gc_mark_internal(RCLASSEXT_INCLUDER(ext));
3086 }
3087 mark_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3088 gc_mark_internal(RCLASSEXT_CC_TBL(ext));
3089}
3090
3091#define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA_TYPE(d)->function.dmark
3092
3093void
3094rb_gc_mark_children(void *objspace, VALUE obj)
3095{
3096 struct gc_mark_classext_foreach_arg foreach_args;
3097
3098 if (rb_obj_exivar_p(obj)) {
3099 rb_mark_generic_ivar(obj);
3100 }
3101
3102 switch (BUILTIN_TYPE(obj)) {
3103 case T_FLOAT:
3104 case T_BIGNUM:
3105 return;
3106
3107 case T_NIL:
3108 case T_FIXNUM:
3109 rb_bug("rb_gc_mark() called for broken object");
3110 break;
3111
3112 case T_NODE:
3113 UNEXPECTED_NODE(rb_gc_mark);
3114 break;
3115
3116 case T_IMEMO:
3117 rb_imemo_mark_and_move(obj, false);
3118 return;
3119
3120 default:
3121 break;
3122 }
3123
3124 gc_mark_internal(RBASIC(obj)->klass);
3125
3126 switch (BUILTIN_TYPE(obj)) {
3127 case T_CLASS:
3128 if (FL_TEST_RAW(obj, FL_SINGLETON)) {
3129 gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
3130 }
3131 // Continue to the shared T_CLASS/T_MODULE
3132 case T_MODULE:
3133 foreach_args.objspace = objspace;
3134 foreach_args.obj = obj;
3135 rb_class_classext_foreach(obj, gc_mark_classext_module, (void *)&foreach_args);
3136 break;
3137
3138 case T_ICLASS:
3139 foreach_args.objspace = objspace;
3140 foreach_args.obj = obj;
3141 rb_class_classext_foreach(obj, gc_mark_classext_iclass, (void *)&foreach_args);
3142 break;
3143
3144 case T_ARRAY:
3145 if (ARY_SHARED_P(obj)) {
3146 VALUE root = ARY_SHARED_ROOT(obj);
3147 gc_mark_internal(root);
3148 }
3149 else {
3150 long len = RARRAY_LEN(obj);
3151 const VALUE *ptr = RARRAY_CONST_PTR(obj);
3152 for (long i = 0; i < len; i++) {
3153 gc_mark_internal(ptr[i]);
3154 }
3155 }
3156 break;
3157
3158 case T_HASH:
3159 mark_hash(obj);
3160 break;
3161
3162 case T_SYMBOL:
3163 gc_mark_internal(RSYMBOL(obj)->fstr);
3164 break;
3165
3166 case T_STRING:
3167 if (STR_SHARED_P(obj)) {
3168 if (STR_EMBED_P(RSTRING(obj)->as.heap.aux.shared)) {
3169 /* Embedded shared strings cannot be moved because this string
3170 * points into the slot of the shared string. There may be code
3171 * using the RSTRING_PTR on the stack, which would pin this
3172 * string but not pin the shared string, causing it to move. */
3173 gc_mark_and_pin_internal(RSTRING(obj)->as.heap.aux.shared);
3174 }
3175 else {
3176 gc_mark_internal(RSTRING(obj)->as.heap.aux.shared);
3177 }
3178 }
3179 break;
3180
3181 case T_DATA: {
3182 bool typed_data = RTYPEDDATA_P(obj);
3183 void *const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
3184
3185 if (typed_data) {
3186 gc_mark_internal(RTYPEDDATA(obj)->fields_obj);
3187 }
3188
3189 if (ptr) {
3190 if (typed_data && gc_declarative_marking_p(RTYPEDDATA_TYPE(obj))) {
3191 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3192
3193 for (size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3194 gc_mark_internal(*(VALUE *)((char *)ptr + offset));
3195 }
3196 }
3197 else {
3198 RUBY_DATA_FUNC mark_func = typed_data ?
3200 RDATA(obj)->dmark;
3201 if (mark_func) (*mark_func)(ptr);
3202 }
3203 }
3204
3205 break;
3206 }
3207
3208 case T_OBJECT: {
3209 if (rb_shape_obj_too_complex_p(obj)) {
3210 gc_mark_tbl_no_pin(ROBJECT_FIELDS_HASH(obj));
3211 }
3212 else {
3213 const VALUE * const ptr = ROBJECT_FIELDS(obj);
3214
3215 uint32_t len = ROBJECT_FIELDS_COUNT(obj);
3216 for (uint32_t i = 0; i < len; i++) {
3217 gc_mark_internal(ptr[i]);
3218 }
3219 }
3220
3221 attr_index_t fields_count = ROBJECT_FIELDS_COUNT(obj);
3222 if (fields_count) {
3223 VALUE klass = RBASIC_CLASS(obj);
3224
3225 // Increment max_iv_count if applicable, used to determine size pool allocation
3226 if (RCLASS_MAX_IV_COUNT(klass) < fields_count) {
3227 RCLASS_SET_MAX_IV_COUNT(klass, fields_count);
3228 }
3229 }
3230
3231 break;
3232 }
3233
3234 case T_FILE:
3235 if (RFILE(obj)->fptr) {
3236 gc_mark_internal(RFILE(obj)->fptr->self);
3237 gc_mark_internal(RFILE(obj)->fptr->pathv);
3238 gc_mark_internal(RFILE(obj)->fptr->tied_io_for_writing);
3239 gc_mark_internal(RFILE(obj)->fptr->writeconv_asciicompat);
3240 gc_mark_internal(RFILE(obj)->fptr->writeconv_pre_ecopts);
3241 gc_mark_internal(RFILE(obj)->fptr->encs.ecopts);
3242 gc_mark_internal(RFILE(obj)->fptr->write_lock);
3243 gc_mark_internal(RFILE(obj)->fptr->timeout);
3244 gc_mark_internal(RFILE(obj)->fptr->wakeup_mutex);
3245 }
3246 break;
3247
3248 case T_REGEXP:
3249 gc_mark_internal(RREGEXP(obj)->src);
3250 break;
3251
3252 case T_MATCH:
3253 gc_mark_internal(RMATCH(obj)->regexp);
3254 if (RMATCH(obj)->str) {
3255 gc_mark_internal(RMATCH(obj)->str);
3256 }
3257 break;
3258
3259 case T_RATIONAL:
3260 gc_mark_internal(RRATIONAL(obj)->num);
3261 gc_mark_internal(RRATIONAL(obj)->den);
3262 break;
3263
3264 case T_COMPLEX:
3265 gc_mark_internal(RCOMPLEX(obj)->real);
3266 gc_mark_internal(RCOMPLEX(obj)->imag);
3267 break;
3268
3269 case T_STRUCT: {
3270 const long len = RSTRUCT_LEN(obj);
3271 const VALUE * const ptr = RSTRUCT_CONST_PTR(obj);
3272
3273 for (long i = 0; i < len; i++) {
3274 gc_mark_internal(ptr[i]);
3275 }
3276
3277 if (!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
3278 gc_mark_internal(RSTRUCT_FIELDS_OBJ(obj));
3279 }
3280
3281 break;
3282 }
3283
3284 default:
3285 if (BUILTIN_TYPE(obj) == T_MOVED) rb_bug("rb_gc_mark(): %p is T_MOVED", (void *)obj);
3286 if (BUILTIN_TYPE(obj) == T_NONE) rb_bug("rb_gc_mark(): %p is T_NONE", (void *)obj);
3287 if (BUILTIN_TYPE(obj) == T_ZOMBIE) rb_bug("rb_gc_mark(): %p is T_ZOMBIE", (void *)obj);
3288 rb_bug("rb_gc_mark(): unknown data type 0x%x(%p) %s",
3289 BUILTIN_TYPE(obj), (void *)obj,
3290 rb_gc_impl_pointer_to_heap_p(objspace, (void *)obj) ? "corrupted object" : "non object");
3291 }
3292}
3293
3294size_t
3295rb_gc_obj_optimal_size(VALUE obj)
3296{
3297 switch (BUILTIN_TYPE(obj)) {
3298 case T_ARRAY:
3299 return rb_ary_size_as_embedded(obj);
3300
3301 case T_OBJECT:
3302 if (rb_shape_obj_too_complex_p(obj)) {
3303 return sizeof(struct RObject);
3304 }
3305 else {
3306 return rb_obj_embedded_size(ROBJECT_FIELDS_CAPACITY(obj));
3307 }
3308
3309 case T_STRING:
3310 return rb_str_size_as_embedded(obj);
3311
3312 case T_HASH:
3313 return sizeof(struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
3314
3315 default:
3316 return 0;
3317 }
3318}
3319
3320void
3321rb_gc_writebarrier(VALUE a, VALUE b)
3322{
3323 rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
3324}
3325
3326void
3327rb_gc_writebarrier_unprotect(VALUE obj)
3328{
3329 rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
3330}
3331
3332/*
3333 * remember `obj' if needed.
3334 */
3335void
3336rb_gc_writebarrier_remember(VALUE obj)
3337{
3338 rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
3339}
3340
3341void
3342rb_gc_copy_attributes(VALUE dest, VALUE obj)
3343{
3344 rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
3345}
3346
3347int
3348rb_gc_modular_gc_loaded_p(void)
3349{
3350#if USE_MODULAR_GC
3351 return rb_gc_functions.modular_gc_loaded_p;
3352#else
3353 return false;
3354#endif
3355}
3356
3357const char *
3358rb_gc_active_gc_name(void)
3359{
3360 const char *gc_name = rb_gc_impl_active_gc_name();
3361
3362 const size_t len = strlen(gc_name);
3363 if (len > RB_GC_MAX_NAME_LEN) {
3364 rb_bug("GC should have a name no more than %d chars long. Currently: %zu (%s)",
3365 RB_GC_MAX_NAME_LEN, len, gc_name);
3366 }
3367
3368 return gc_name;
3369}
3370
3372rb_gc_object_metadata(VALUE obj)
3373{
3374 return rb_gc_impl_object_metadata(rb_gc_get_objspace(), obj);
3375}
3376
3377/* GC */
3378
3379void *
3380rb_gc_ractor_cache_alloc(rb_ractor_t *ractor)
3381{
3382 return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace(), ractor);
3383}
3384
3385void
3386rb_gc_ractor_cache_free(void *cache)
3387{
3388 rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
3389}
3390
3391void
3392rb_gc_register_mark_object(VALUE obj)
3393{
3394 if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj))
3395 return;
3396
3397 rb_vm_register_global_object(obj);
3398}
3399
3400void
3401rb_gc_register_address(VALUE *addr)
3402{
3403 rb_vm_t *vm = GET_VM();
3404
3405 VALUE obj = *addr;
3406
3407 struct global_object_list *tmp = ALLOC(struct global_object_list);
3408 tmp->next = vm->global_object_list;
3409 tmp->varptr = addr;
3410 vm->global_object_list = tmp;
3411
3412 /*
3413 * Because some C extensions have assignment-then-register bugs,
3414 * we guard `obj` here so that it would not get swept defensively.
3415 */
3416 RB_GC_GUARD(obj);
3417 if (0 && !SPECIAL_CONST_P(obj)) {
3418 rb_warn("Object is assigned to registering address already: %"PRIsVALUE,
3419 rb_obj_class(obj));
3420 rb_print_backtrace(stderr);
3421 }
3422}
3423
3424void
3425rb_gc_unregister_address(VALUE *addr)
3426{
3427 rb_vm_t *vm = GET_VM();
3428 struct global_object_list *tmp = vm->global_object_list;
3429
3430 if (tmp->varptr == addr) {
3431 vm->global_object_list = tmp->next;
3432 xfree(tmp);
3433 return;
3434 }
3435 while (tmp->next) {
3436 if (tmp->next->varptr == addr) {
3437 struct global_object_list *t = tmp->next;
3438
3439 tmp->next = tmp->next->next;
3440 xfree(t);
3441 break;
3442 }
3443 tmp = tmp->next;
3444 }
3445}
3446
3447void
3449{
3450 rb_gc_register_address(var);
3451}
3452
3453static VALUE
3454gc_start_internal(rb_execution_context_t *ec, VALUE self, VALUE full_mark, VALUE immediate_mark, VALUE immediate_sweep, VALUE compact)
3455{
3456 rb_gc_impl_start(rb_gc_get_objspace(), RTEST(full_mark), RTEST(immediate_mark), RTEST(immediate_sweep), RTEST(compact));
3457
3458 return Qnil;
3459}
3460
3461/*
3462 * rb_objspace_each_objects() is special C API to walk through
3463 * Ruby object space. This C API is too difficult to use it.
3464 * To be frank, you should not use it. Or you need to read the
3465 * source code of this function and understand what this function does.
3466 *
3467 * 'callback' will be called several times (the number of heap page,
3468 * at current implementation) with:
3469 * vstart: a pointer to the first living object of the heap_page.
3470 * vend: a pointer to next to the valid heap_page area.
3471 * stride: a distance to next VALUE.
3472 *
3473 * If callback() returns non-zero, the iteration will be stopped.
3474 *
3475 * This is a sample callback code to iterate liveness objects:
3476 *
3477 * static int
3478 * sample_callback(void *vstart, void *vend, int stride, void *data)
3479 * {
3480 * VALUE v = (VALUE)vstart;
3481 * for (; v != (VALUE)vend; v += stride) {
3482 * if (!rb_objspace_internal_object_p(v)) { // liveness check
3483 * // do something with live object 'v'
3484 * }
3485 * }
3486 * return 0; // continue to iteration
3487 * }
3488 *
3489 * Note: 'vstart' is not a top of heap_page. This point the first
3490 * living object to grasp at least one object to avoid GC issue.
3491 * This means that you can not walk through all Ruby object page
3492 * including freed object page.
3493 *
3494 * Note: On this implementation, 'stride' is the same as sizeof(RVALUE).
3495 * However, there are possibilities to pass variable values with
3496 * 'stride' with some reasons. You must use stride instead of
3497 * use some constant value in the iteration.
3498 */
3499void
3500rb_objspace_each_objects(int (*callback)(void *, void *, size_t, void *), void *data)
3501{
3502 rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
3503}
3504
3505static void
3506gc_ref_update_array(void *objspace, VALUE v)
3507{
3508 if (ARY_SHARED_P(v)) {
3509 VALUE old_root = RARRAY(v)->as.heap.aux.shared_root;
3510
3511 UPDATE_IF_MOVED(objspace, RARRAY(v)->as.heap.aux.shared_root);
3512
3513 VALUE new_root = RARRAY(v)->as.heap.aux.shared_root;
3514 // If the root is embedded and its location has changed
3515 if (ARY_EMBED_P(new_root) && new_root != old_root) {
3516 size_t offset = (size_t)(RARRAY(v)->as.heap.ptr - RARRAY(old_root)->as.ary);
3517 GC_ASSERT(RARRAY(v)->as.heap.ptr >= RARRAY(old_root)->as.ary);
3518 RARRAY(v)->as.heap.ptr = RARRAY(new_root)->as.ary + offset;
3519 }
3520 }
3521 else {
3522 long len = RARRAY_LEN(v);
3523
3524 if (len > 0) {
3525 VALUE *ptr = (VALUE *)RARRAY_CONST_PTR(v);
3526 for (long i = 0; i < len; i++) {
3527 UPDATE_IF_MOVED(objspace, ptr[i]);
3528 }
3529 }
3530
3531 if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
3532 if (rb_ary_embeddable_p(v)) {
3533 rb_ary_make_embedded(v);
3534 }
3535 }
3536 }
3537}
3538
3539static void
3540gc_ref_update_object(void *objspace, VALUE v)
3541{
3542 VALUE *ptr = ROBJECT_FIELDS(v);
3543
3544 if (rb_shape_obj_too_complex_p(v)) {
3545 gc_ref_update_table_values_only(ROBJECT_FIELDS_HASH(v));
3546 return;
3547 }
3548
3549 size_t slot_size = rb_gc_obj_slot_size(v);
3550 size_t embed_size = rb_obj_embedded_size(ROBJECT_FIELDS_CAPACITY(v));
3551 if (slot_size >= embed_size && !RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
3552 // Object can be re-embedded
3553 memcpy(ROBJECT(v)->as.ary, ptr, sizeof(VALUE) * ROBJECT_FIELDS_COUNT(v));
3554 RB_FL_SET_RAW(v, ROBJECT_EMBED);
3555 xfree(ptr);
3556 ptr = ROBJECT(v)->as.ary;
3557 }
3558
3559 for (uint32_t i = 0; i < ROBJECT_FIELDS_COUNT(v); i++) {
3560 UPDATE_IF_MOVED(objspace, ptr[i]);
3561 }
3562}
3563
3564void
3565rb_gc_ref_update_table_values_only(st_table *tbl)
3566{
3567 gc_ref_update_table_values_only(tbl);
3568}
3569
3570/* Update MOVED references in a VALUE=>VALUE st_table */
3571void
3572rb_gc_update_tbl_refs(st_table *ptr)
3573{
3574 gc_update_table_refs(ptr);
3575}
3576
3577static void
3578gc_ref_update_hash(void *objspace, VALUE v)
3579{
3580 rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)objspace);
3581}
3582
3583static void
3584gc_update_values(void *objspace, long n, VALUE *values)
3585{
3586 for (long i = 0; i < n; i++) {
3587 UPDATE_IF_MOVED(objspace, values[i]);
3588 }
3589}
3590
3591void
3592rb_gc_update_values(long n, VALUE *values)
3593{
3594 gc_update_values(rb_gc_get_objspace(), n, values);
3595}
3596
3597static enum rb_id_table_iterator_result
3598check_id_table_move(VALUE value, void *data)
3599{
3600 void *objspace = (void *)data;
3601
3602 if (rb_gc_impl_object_moved_p(objspace, (VALUE)value)) {
3603 return ID_TABLE_REPLACE;
3604 }
3605
3606 return ID_TABLE_CONTINUE;
3607}
3608
3609void
3610rb_gc_prepare_heap_process_object(VALUE obj)
3611{
3612 switch (BUILTIN_TYPE(obj)) {
3613 case T_STRING:
3614 // Precompute the string coderange. This both save time for when it will be
3615 // eventually needed, and avoid mutating heap pages after a potential fork.
3617 break;
3618 default:
3619 break;
3620 }
3621}
3622
3623void
3624rb_gc_prepare_heap(void)
3625{
3626 rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3627}
3628
3629size_t
3630rb_gc_heap_id_for_size(size_t size)
3631{
3632 return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3633}
3634
3635bool
3636rb_gc_size_allocatable_p(size_t size)
3637{
3638 return rb_gc_impl_size_allocatable_p(size);
3639}
3640
3641static enum rb_id_table_iterator_result
3642update_id_table(VALUE *value, void *data, int existing)
3643{
3644 void *objspace = (void *)data;
3645
3646 if (rb_gc_impl_object_moved_p(objspace, (VALUE)*value)) {
3647 *value = gc_location_internal(objspace, (VALUE)*value);
3648 }
3649
3650 return ID_TABLE_CONTINUE;
3651}
3652
3653static void
3654update_m_tbl(void *objspace, struct rb_id_table *tbl)
3655{
3656 if (tbl) {
3657 rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table, objspace);
3658 }
3659}
3660
3661static enum rb_id_table_iterator_result
3662update_cvc_tbl_i(VALUE cvc_entry, void *objspace)
3663{
3664 struct rb_cvar_class_tbl_entry *entry;
3665
3666 entry = (struct rb_cvar_class_tbl_entry *)cvc_entry;
3667
3668 if (entry->cref) {
3669 TYPED_UPDATE_IF_MOVED(objspace, rb_cref_t *, entry->cref);
3670 }
3671
3672 entry->class_value = gc_location_internal(objspace, entry->class_value);
3673
3674 return ID_TABLE_CONTINUE;
3675}
3676
3677static void
3678update_cvc_tbl(void *objspace, struct rb_id_table *tbl)
3679{
3680 if (!tbl) return;
3681 rb_id_table_foreach_values(tbl, update_cvc_tbl_i, objspace);
3682}
3683
3684static enum rb_id_table_iterator_result
3685update_const_tbl_i(VALUE value, void *objspace)
3686{
3687 rb_const_entry_t *ce = (rb_const_entry_t *)value;
3688
3689 if (rb_gc_impl_object_moved_p(objspace, ce->value)) {
3690 ce->value = gc_location_internal(objspace, ce->value);
3691 }
3692
3693 if (rb_gc_impl_object_moved_p(objspace, ce->file)) {
3694 ce->file = gc_location_internal(objspace, ce->file);
3695 }
3696
3697 return ID_TABLE_CONTINUE;
3698}
3699
3700static void
3701update_const_tbl(void *objspace, struct rb_id_table *tbl)
3702{
3703 if (!tbl) return;
3704 rb_id_table_foreach_values(tbl, update_const_tbl_i, objspace);
3705}
3706
3707static void
3708update_subclasses(void *objspace, rb_classext_t *ext)
3709{
3710 rb_subclass_entry_t *entry;
3711 rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
3712 if (!anchor) return;
3713 entry = anchor->head;
3714 while (entry) {
3715 if (entry->klass)
3716 UPDATE_IF_MOVED(objspace, entry->klass);
3717 entry = entry->next;
3718 }
3719}
3720
3721static void
3722update_superclasses(rb_objspace_t *objspace, rb_classext_t *ext)
3723{
3724 if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
3725 size_t array_size = RCLASSEXT_SUPERCLASS_DEPTH(ext) + 1;
3726 for (size_t i = 0; i < array_size; i++) {
3727 UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPERCLASSES(ext)[i]);
3728 }
3729 }
3730}
3731
3732static void
3733update_classext_values(rb_objspace_t *objspace, rb_classext_t *ext, bool is_iclass)
3734{
3735 UPDATE_IF_MOVED(objspace, RCLASSEXT_ORIGIN(ext));
3736 UPDATE_IF_MOVED(objspace, RCLASSEXT_REFINED_CLASS(ext));
3737 UPDATE_IF_MOVED(objspace, RCLASSEXT_CLASSPATH(ext));
3738 if (is_iclass) {
3739 UPDATE_IF_MOVED(objspace, RCLASSEXT_INCLUDER(ext));
3740 }
3741}
3742
3743static void
3744update_classext(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)
3745{
3746 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
3747 rb_objspace_t *objspace = args->objspace;
3748
3749 if (RCLASSEXT_SUPER(ext)) {
3750 UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPER(ext));
3751 }
3752
3753 update_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3754
3755 UPDATE_IF_MOVED(objspace, ext->fields_obj);
3756 if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
3757 update_const_tbl(objspace, RCLASSEXT_CONST_TBL(ext));
3758 }
3759 UPDATE_IF_MOVED(objspace, RCLASSEXT_CC_TBL(ext));
3760 update_cvc_tbl(objspace, RCLASSEXT_CVC_TBL(ext));
3761 update_superclasses(objspace, ext);
3762 update_subclasses(objspace, ext);
3763
3764 update_classext_values(objspace, ext, false);
3765}
3766
3767static void
3768update_iclass_classext(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)
3769{
3770 struct classext_foreach_args *args = (struct classext_foreach_args *)arg;
3771 rb_objspace_t *objspace = args->objspace;
3772
3773 if (RCLASSEXT_SUPER(ext)) {
3774 UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPER(ext));
3775 }
3776 update_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
3777 update_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
3778 UPDATE_IF_MOVED(objspace, RCLASSEXT_CC_TBL(ext));
3779 update_subclasses(objspace, ext);
3780
3781 update_classext_values(objspace, ext, true);
3782}
3783
3785 vm_table_foreach_callback_func callback;
3786 vm_table_update_callback_func update_callback;
3787 void *data;
3788 bool weak_only;
3789};
3790
3791static int
3792vm_weak_table_foreach_weak_key(st_data_t key, st_data_t value, st_data_t data, int error)
3793{
3794 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3795
3796 int ret = iter_data->callback((VALUE)key, iter_data->data);
3797
3798 if (!iter_data->weak_only) {
3799 if (ret != ST_CONTINUE) return ret;
3800
3801 ret = iter_data->callback((VALUE)value, iter_data->data);
3802 }
3803
3804 return ret;
3805}
3806
3807static int
3808vm_weak_table_foreach_update_weak_key(st_data_t *key, st_data_t *value, st_data_t data, int existing)
3809{
3810 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3811
3812 int ret = iter_data->update_callback((VALUE *)key, iter_data->data);
3813
3814 if (!iter_data->weak_only) {
3815 if (ret != ST_CONTINUE) return ret;
3816
3817 ret = iter_data->update_callback((VALUE *)value, iter_data->data);
3818 }
3819
3820 return ret;
3821}
3822
3823static int
3824vm_weak_table_cc_refinement_foreach(st_data_t key, st_data_t data, int error)
3825{
3826 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3827
3828 return iter_data->callback((VALUE)key, iter_data->data);
3829}
3830
3831static int
3832vm_weak_table_cc_refinement_foreach_update_update(st_data_t *key, st_data_t data, int existing)
3833{
3834 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3835
3836 return iter_data->update_callback((VALUE *)key, iter_data->data);
3837}
3838
3839
3840static int
3841vm_weak_table_sym_set_foreach(VALUE *sym_ptr, void *data)
3842{
3843 VALUE sym = *sym_ptr;
3844 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3845
3846 if (RB_SPECIAL_CONST_P(sym)) return ST_CONTINUE;
3847
3848 int ret = iter_data->callback(sym, iter_data->data);
3849
3850 if (ret == ST_REPLACE) {
3851 ret = iter_data->update_callback(sym_ptr, iter_data->data);
3852 }
3853
3854 return ret;
3855}
3856
3857struct st_table *rb_generic_fields_tbl_get(void);
3858
3859static int
3860vm_weak_table_id2ref_foreach(st_data_t key, st_data_t value, st_data_t data, int error)
3861{
3862 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3863
3864 if (!iter_data->weak_only && !FIXNUM_P((VALUE)key)) {
3865 int ret = iter_data->callback((VALUE)key, iter_data->data);
3866 if (ret != ST_CONTINUE) return ret;
3867 }
3868
3869 return iter_data->callback((VALUE)value, iter_data->data);
3870}
3871
3872static int
3873vm_weak_table_id2ref_foreach_update(st_data_t *key, st_data_t *value, st_data_t data, int existing)
3874{
3875 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3876
3877 iter_data->update_callback((VALUE *)value, iter_data->data);
3878
3879 if (!iter_data->weak_only && !FIXNUM_P((VALUE)*key)) {
3880 iter_data->update_callback((VALUE *)key, iter_data->data);
3881 }
3882
3883 return ST_CONTINUE;
3884}
3885
3886static int
3887vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
3888{
3889 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3890
3891 int ret = iter_data->callback((VALUE)key, iter_data->data);
3892
3893 VALUE new_value = (VALUE)value;
3894 VALUE new_key = (VALUE)key;
3895
3896 switch (ret) {
3897 case ST_CONTINUE:
3898 break;
3899
3900 case ST_DELETE:
3901 RBASIC_SET_SHAPE_ID((VALUE)key, ROOT_SHAPE_ID);
3902 return ST_DELETE;
3903
3904 case ST_REPLACE: {
3905 ret = iter_data->update_callback(&new_key, iter_data->data);
3906 if (key != new_key) {
3907 ret = ST_DELETE;
3908 }
3909 break;
3910 }
3911
3912 default:
3913 rb_bug("vm_weak_table_gen_fields_foreach: return value %d not supported", ret);
3914 }
3915
3916 if (!iter_data->weak_only) {
3917 int ivar_ret = iter_data->callback(new_value, iter_data->data);
3918 switch (ivar_ret) {
3919 case ST_CONTINUE:
3920 break;
3921
3922 case ST_REPLACE:
3923 iter_data->update_callback(&new_value, iter_data->data);
3924 break;
3925
3926 default:
3927 rb_bug("vm_weak_table_gen_fields_foreach: return value %d not supported", ivar_ret);
3928 }
3929 }
3930
3931 if (key != new_key || value != new_value) {
3932 DURING_GC_COULD_MALLOC_REGION_START();
3933 {
3934 st_insert(rb_generic_fields_tbl_get(), (st_data_t)new_key, new_value);
3935 }
3936 DURING_GC_COULD_MALLOC_REGION_END();
3937 }
3938
3939 return ret;
3940}
3941
3942static int
3943vm_weak_table_frozen_strings_foreach(VALUE *str, void *data)
3944{
3945 // int retval = vm_weak_table_foreach_weak_key(key, value, data, error);
3946 struct global_vm_table_foreach_data *iter_data = (struct global_vm_table_foreach_data *)data;
3947 int retval = iter_data->callback(*str, iter_data->data);
3948
3949 if (retval == ST_REPLACE) {
3950 retval = iter_data->update_callback(str, iter_data->data);
3951 }
3952
3953 if (retval == ST_DELETE) {
3954 FL_UNSET(*str, RSTRING_FSTR);
3955 }
3956
3957 return retval;
3958}
3959
3960void rb_fstring_foreach_with_replace(int (*callback)(VALUE *str, void *data), void *data);
3961void
3962rb_gc_vm_weak_table_foreach(vm_table_foreach_callback_func callback,
3963 vm_table_update_callback_func update_callback,
3964 void *data,
3965 bool weak_only,
3966 enum rb_gc_vm_weak_tables table)
3967{
3968 rb_vm_t *vm = GET_VM();
3969
3970 struct global_vm_table_foreach_data foreach_data = {
3971 .callback = callback,
3972 .update_callback = update_callback,
3973 .data = data,
3974 .weak_only = weak_only,
3975 };
3976
3977 switch (table) {
3978 case RB_GC_VM_CI_TABLE: {
3979 if (vm->ci_table) {
3980 st_foreach_with_replace(
3981 vm->ci_table,
3982 vm_weak_table_foreach_weak_key,
3983 vm_weak_table_foreach_update_weak_key,
3984 (st_data_t)&foreach_data
3985 );
3986 }
3987 break;
3988 }
3989 case RB_GC_VM_OVERLOADED_CME_TABLE: {
3990 if (vm->overloaded_cme_table) {
3991 st_foreach_with_replace(
3992 vm->overloaded_cme_table,
3993 vm_weak_table_foreach_weak_key,
3994 vm_weak_table_foreach_update_weak_key,
3995 (st_data_t)&foreach_data
3996 );
3997 }
3998 break;
3999 }
4000 case RB_GC_VM_GLOBAL_SYMBOLS_TABLE: {
4001 rb_sym_global_symbol_table_foreach_weak_reference(
4002 vm_weak_table_sym_set_foreach,
4003 &foreach_data
4004 );
4005 break;
4006 }
4007 case RB_GC_VM_ID2REF_TABLE: {
4008 if (id2ref_tbl) {
4009 st_foreach_with_replace(
4010 id2ref_tbl,
4011 vm_weak_table_id2ref_foreach,
4012 vm_weak_table_id2ref_foreach_update,
4013 (st_data_t)&foreach_data
4014 );
4015 }
4016 break;
4017 }
4018 case RB_GC_VM_GENERIC_FIELDS_TABLE: {
4019 st_table *generic_fields_tbl = rb_generic_fields_tbl_get();
4020 if (generic_fields_tbl) {
4021 st_foreach(
4022 generic_fields_tbl,
4023 vm_weak_table_gen_fields_foreach,
4024 (st_data_t)&foreach_data
4025 );
4026 }
4027 break;
4028 }
4029 case RB_GC_VM_FROZEN_STRINGS_TABLE: {
4030 rb_fstring_foreach_with_replace(
4031 vm_weak_table_frozen_strings_foreach,
4032 &foreach_data
4033 );
4034 break;
4035 }
4036 case RB_GC_VM_CC_REFINEMENT_TABLE: {
4037 if (vm->cc_refinement_table) {
4038 set_foreach_with_replace(
4039 vm->cc_refinement_table,
4040 vm_weak_table_cc_refinement_foreach,
4041 vm_weak_table_cc_refinement_foreach_update_update,
4042 (st_data_t)&foreach_data
4043 );
4044 }
4045 break;
4046 }
4047 case RB_GC_VM_WEAK_TABLE_COUNT:
4048 rb_bug("Unreachable");
4049 default:
4050 rb_bug("rb_gc_vm_weak_table_foreach: unknown table %d", table);
4051 }
4052}
4053
4054void
4055rb_gc_update_vm_references(void *objspace)
4056{
4057 rb_execution_context_t *ec = GET_EC();
4058 rb_vm_t *vm = rb_ec_vm_ptr(ec);
4059
4060 rb_vm_update_references(vm);
4061 rb_gc_update_global_tbl();
4062 rb_sym_global_symbols_mark_and_move();
4063
4064#if USE_YJIT
4065 void rb_yjit_root_update_references(void); // in Rust
4066
4067 if (rb_yjit_enabled_p) {
4068 rb_yjit_root_update_references();
4069 }
4070#endif
4071}
4072
4073void
4074rb_gc_update_object_references(void *objspace, VALUE obj)
4075{
4076 struct classext_foreach_args args;
4077
4078 switch (BUILTIN_TYPE(obj)) {
4079 case T_CLASS:
4080 if (FL_TEST_RAW(obj, FL_SINGLETON)) {
4081 UPDATE_IF_MOVED(objspace, RCLASS_ATTACHED_OBJECT(obj));
4082 }
4083 // Continue to the shared T_CLASS/T_MODULE
4084 case T_MODULE:
4085 args.klass = obj;
4086 args.objspace = objspace;
4087 rb_class_classext_foreach(obj, update_classext, (void *)&args);
4088 break;
4089
4090 case T_ICLASS:
4091 args.objspace = objspace;
4092 rb_class_classext_foreach(obj, update_iclass_classext, (void *)&args);
4093 break;
4094
4095 case T_IMEMO:
4096 rb_imemo_mark_and_move(obj, true);
4097 return;
4098
4099 case T_NIL:
4100 case T_FIXNUM:
4101 case T_NODE:
4102 case T_MOVED:
4103 case T_NONE:
4104 /* These can't move */
4105 return;
4106
4107 case T_ARRAY:
4108 gc_ref_update_array(objspace, obj);
4109 break;
4110
4111 case T_HASH:
4112 gc_ref_update_hash(objspace, obj);
4113 UPDATE_IF_MOVED(objspace, RHASH(obj)->ifnone);
4114 break;
4115
4116 case T_STRING:
4117 {
4118 if (STR_SHARED_P(obj)) {
4119 UPDATE_IF_MOVED(objspace, RSTRING(obj)->as.heap.aux.shared);
4120 }
4121
4122 /* If, after move the string is not embedded, and can fit in the
4123 * slot it's been placed in, then re-embed it. */
4124 if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
4125 if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
4126 rb_str_make_embedded(obj);
4127 }
4128 }
4129
4130 break;
4131 }
4132 case T_DATA:
4133 /* Call the compaction callback, if it exists */
4134 {
4135 bool typed_data = RTYPEDDATA_P(obj);
4136 void *const ptr = typed_data ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
4137
4138 if (typed_data) {
4139 UPDATE_IF_MOVED(objspace, RTYPEDDATA(obj)->fields_obj);
4140 }
4141
4142 if (ptr) {
4143 if (typed_data && gc_declarative_marking_p(RTYPEDDATA_TYPE(obj))) {
4144 size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
4145
4146 for (size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
4147 VALUE *ref = (VALUE *)((char *)ptr + offset);
4148 *ref = gc_location_internal(objspace, *ref);
4149 }
4150 }
4151 else if (typed_data) {
4152 RUBY_DATA_FUNC compact_func = RTYPEDDATA_TYPE(obj)->function.dcompact;
4153 if (compact_func) (*compact_func)(ptr);
4154 }
4155 }
4156 }
4157 break;
4158
4159 case T_OBJECT:
4160 gc_ref_update_object(objspace, obj);
4161 break;
4162
4163 case T_FILE:
4164 if (RFILE(obj)->fptr) {
4165 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->self);
4166 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->pathv);
4167 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->tied_io_for_writing);
4168 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->writeconv_asciicompat);
4169 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->writeconv_pre_ecopts);
4170 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->encs.ecopts);
4171 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->write_lock);
4172 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->timeout);
4173 UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->wakeup_mutex);
4174 }
4175 break;
4176 case T_REGEXP:
4177 UPDATE_IF_MOVED(objspace, RREGEXP(obj)->src);
4178 break;
4179
4180 case T_SYMBOL:
4181 UPDATE_IF_MOVED(objspace, RSYMBOL(obj)->fstr);
4182 break;
4183
4184 case T_FLOAT:
4185 case T_BIGNUM:
4186 break;
4187
4188 case T_MATCH:
4189 UPDATE_IF_MOVED(objspace, RMATCH(obj)->regexp);
4190
4191 if (RMATCH(obj)->str) {
4192 UPDATE_IF_MOVED(objspace, RMATCH(obj)->str);
4193 }
4194 break;
4195
4196 case T_RATIONAL:
4197 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->num);
4198 UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->den);
4199 break;
4200
4201 case T_COMPLEX:
4202 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->real);
4203 UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->imag);
4204
4205 break;
4206
4207 case T_STRUCT:
4208 {
4209 long i, len = RSTRUCT_LEN(obj);
4210 VALUE *ptr = (VALUE *)RSTRUCT_CONST_PTR(obj);
4211
4212 for (i = 0; i < len; i++) {
4213 UPDATE_IF_MOVED(objspace, ptr[i]);
4214 }
4215
4216 if (RSTRUCT_EMBED_LEN(obj)) {
4217 if (!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) {
4218 UPDATE_IF_MOVED(objspace, ptr[len]);
4219 }
4220 }
4221 else {
4222 UPDATE_IF_MOVED(objspace, RSTRUCT(obj)->as.heap.fields_obj);
4223 }
4224 }
4225 break;
4226 default:
4227 rb_bug("unreachable");
4228 break;
4229 }
4230
4231 UPDATE_IF_MOVED(objspace, RBASIC(obj)->klass);
4232}
4233
4234VALUE
4235rb_gc_start(void)
4236{
4237 rb_gc();
4238 return Qnil;
4239}
4240
4241void
4242rb_gc(void)
4243{
4244 unless_objspace(objspace) { return; }
4245
4246 rb_gc_impl_start(objspace, true, true, true, false);
4247}
4248
4249int
4250rb_during_gc(void)
4251{
4252 unless_objspace(objspace) { return FALSE; }
4253
4254 return rb_gc_impl_during_gc_p(objspace);
4255}
4256
4257size_t
4258rb_gc_count(void)
4259{
4260 return rb_gc_impl_gc_count(rb_gc_get_objspace());
4261}
4262
4263static VALUE
4264gc_count(rb_execution_context_t *ec, VALUE self)
4265{
4266 return SIZET2NUM(rb_gc_count());
4267}
4268
4269VALUE
4270rb_gc_latest_gc_info(VALUE key)
4271{
4272 if (!SYMBOL_P(key) && !RB_TYPE_P(key, T_HASH)) {
4273 rb_raise(rb_eTypeError, "non-hash or symbol given");
4274 }
4275
4276 VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
4277
4278 if (val == Qundef) {
4279 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
4280 }
4281
4282 return val;
4283}
4284
4285static VALUE
4286gc_stat(rb_execution_context_t *ec, VALUE self, VALUE arg) // arg is (nil || hash || symbol)
4287{
4288 if (NIL_P(arg)) {
4289 arg = rb_hash_new();
4290 }
4291 else if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) {
4292 rb_raise(rb_eTypeError, "non-hash or symbol given");
4293 }
4294
4295 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4296
4297 if (ret == Qundef) {
4298 GC_ASSERT(SYMBOL_P(arg));
4299
4300 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
4301 }
4302
4303 return ret;
4304}
4305
4306size_t
4307rb_gc_stat(VALUE arg)
4308{
4309 if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) {
4310 rb_raise(rb_eTypeError, "non-hash or symbol given");
4311 }
4312
4313 VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
4314
4315 if (ret == Qundef) {
4316 GC_ASSERT(SYMBOL_P(arg));
4317
4318 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
4319 }
4320
4321 if (SYMBOL_P(arg)) {
4322 return NUM2SIZET(ret);
4323 }
4324 else {
4325 return 0;
4326 }
4327}
4328
4329static VALUE
4330gc_stat_heap(rb_execution_context_t *ec, VALUE self, VALUE heap_name, VALUE arg)
4331{
4332 if (NIL_P(arg)) {
4333 arg = rb_hash_new();
4334 }
4335
4336 if (NIL_P(heap_name)) {
4337 if (!RB_TYPE_P(arg, T_HASH)) {
4338 rb_raise(rb_eTypeError, "non-hash given");
4339 }
4340 }
4341 else if (FIXNUM_P(heap_name)) {
4342 if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) {
4343 rb_raise(rb_eTypeError, "non-hash or symbol given");
4344 }
4345 }
4346 else {
4347 rb_raise(rb_eTypeError, "heap_name must be nil or an Integer");
4348 }
4349
4350 VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
4351
4352 if (ret == Qundef) {
4353 GC_ASSERT(SYMBOL_P(arg));
4354
4355 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
4356 }
4357
4358 return ret;
4359}
4360
4361static VALUE
4362gc_config_get(rb_execution_context_t *ec, VALUE self)
4363{
4364 VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
4365 rb_hash_aset(cfg_hash, sym("implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
4366
4367 return cfg_hash;
4368}
4369
4370static VALUE
4371gc_config_set(rb_execution_context_t *ec, VALUE self, VALUE hash)
4372{
4373 void *objspace = rb_gc_get_objspace();
4374
4375 rb_gc_impl_config_set(objspace, hash);
4376
4377 return Qnil;
4378}
4379
4380static VALUE
4381gc_stress_get(rb_execution_context_t *ec, VALUE self)
4382{
4383 return rb_gc_impl_stress_get(rb_gc_get_objspace());
4384}
4385
4386static VALUE
4387gc_stress_set_m(rb_execution_context_t *ec, VALUE self, VALUE flag)
4388{
4389 rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
4390
4391 return flag;
4392}
4393
4394void
4395rb_gc_initial_stress_set(VALUE flag)
4396{
4397 initial_stress = flag;
4398}
4399
4400size_t *
4401rb_gc_heap_sizes(void)
4402{
4403 return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
4404}
4405
4406VALUE
4407rb_gc_enable(void)
4408{
4409 return rb_objspace_gc_enable(rb_gc_get_objspace());
4410}
4411
4412VALUE
4413rb_objspace_gc_enable(void *objspace)
4414{
4415 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
4416 rb_gc_impl_gc_enable(objspace);
4417 return RBOOL(disabled);
4418}
4419
4420static VALUE
4421gc_enable(rb_execution_context_t *ec, VALUE _)
4422{
4423 return rb_gc_enable();
4424}
4425
4426static VALUE
4427gc_disable_no_rest(void *objspace)
4428{
4429 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
4430 rb_gc_impl_gc_disable(objspace, false);
4431 return RBOOL(disabled);
4432}
4433
4434VALUE
4435rb_gc_disable_no_rest(void)
4436{
4437 return gc_disable_no_rest(rb_gc_get_objspace());
4438}
4439
4440VALUE
4441rb_gc_disable(void)
4442{
4443 return rb_objspace_gc_disable(rb_gc_get_objspace());
4444}
4445
4446VALUE
4447rb_objspace_gc_disable(void *objspace)
4448{
4449 bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
4450 rb_gc_impl_gc_disable(objspace, true);
4451 return RBOOL(disabled);
4452}
4453
4454static VALUE
4455gc_disable(rb_execution_context_t *ec, VALUE _)
4456{
4457 return rb_gc_disable();
4458}
4459
4460// TODO: think about moving ruby_gc_set_params into Init_heap or Init_gc
4461void
4462ruby_gc_set_params(void)
4463{
4464 rb_gc_impl_set_params(rb_gc_get_objspace());
4465}
4466
4467void
4468rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data)
4469{
4470 RB_VM_LOCKING() {
4471 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug("rb_objspace_reachable_objects_from() is not supported while during GC");
4472
4473 if (!RB_SPECIAL_CONST_P(obj)) {
4474 rb_vm_t *vm = GET_VM();
4475 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4476 struct gc_mark_func_data_struct mfd = {
4477 .mark_func = func,
4478 .data = data,
4479 };
4480
4481 vm->gc.mark_func_data = &mfd;
4482 rb_gc_mark_children(rb_gc_get_objspace(), obj);
4483 vm->gc.mark_func_data = prev_mfd;
4484 }
4485 }
4486}
4487
4489 const char *category;
4490 void (*func)(const char *category, VALUE, void *);
4491 void *data;
4492};
4493
4494static void
4495root_objects_from(VALUE obj, void *ptr)
4496{
4497 const struct root_objects_data *data = (struct root_objects_data *)ptr;
4498 (*data->func)(data->category, obj, data->data);
4499}
4500
4501void
4502rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *passing_data)
4503{
4504 if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug("rb_gc_impl_objspace_reachable_objects_from_root() is not supported while during GC");
4505
4506 rb_vm_t *vm = GET_VM();
4507
4508 struct root_objects_data data = {
4509 .func = func,
4510 .data = passing_data,
4511 };
4512
4513 struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
4514 struct gc_mark_func_data_struct mfd = {
4515 .mark_func = root_objects_from,
4516 .data = &data,
4517 };
4518
4519 vm->gc.mark_func_data = &mfd;
4520 rb_gc_save_machine_context();
4521 rb_gc_mark_roots(vm->gc.objspace, &data.category);
4522 vm->gc.mark_func_data = prev_mfd;
4523}
4524
4525/*
4526 ------------------------------ DEBUG ------------------------------
4527*/
4528
4529static const char *
4530type_name(int type, VALUE obj)
4531{
4532 switch (type) {
4533#define TYPE_NAME(t) case (t): return #t;
4534 TYPE_NAME(T_NONE);
4535 TYPE_NAME(T_OBJECT);
4536 TYPE_NAME(T_CLASS);
4537 TYPE_NAME(T_MODULE);
4538 TYPE_NAME(T_FLOAT);
4539 TYPE_NAME(T_STRING);
4540 TYPE_NAME(T_REGEXP);
4541 TYPE_NAME(T_ARRAY);
4542 TYPE_NAME(T_HASH);
4543 TYPE_NAME(T_STRUCT);
4544 TYPE_NAME(T_BIGNUM);
4545 TYPE_NAME(T_FILE);
4546 TYPE_NAME(T_MATCH);
4547 TYPE_NAME(T_COMPLEX);
4548 TYPE_NAME(T_RATIONAL);
4549 TYPE_NAME(T_NIL);
4550 TYPE_NAME(T_TRUE);
4551 TYPE_NAME(T_FALSE);
4552 TYPE_NAME(T_SYMBOL);
4553 TYPE_NAME(T_FIXNUM);
4554 TYPE_NAME(T_UNDEF);
4555 TYPE_NAME(T_IMEMO);
4556 TYPE_NAME(T_ICLASS);
4557 TYPE_NAME(T_MOVED);
4558 TYPE_NAME(T_ZOMBIE);
4559 case T_DATA:
4560 if (obj && rb_objspace_data_type_name(obj)) {
4561 return rb_objspace_data_type_name(obj);
4562 }
4563 return "T_DATA";
4564#undef TYPE_NAME
4565 }
4566 return "unknown";
4567}
4568
4569static const char *
4570obj_type_name(VALUE obj)
4571{
4572 return type_name(TYPE(obj), obj);
4573}
4574
4575const char *
4576rb_method_type_name(rb_method_type_t type)
4577{
4578 switch (type) {
4579 case VM_METHOD_TYPE_ISEQ: return "iseq";
4580 case VM_METHOD_TYPE_ATTRSET: return "attrest";
4581 case VM_METHOD_TYPE_IVAR: return "ivar";
4582 case VM_METHOD_TYPE_BMETHOD: return "bmethod";
4583 case VM_METHOD_TYPE_ALIAS: return "alias";
4584 case VM_METHOD_TYPE_REFINED: return "refined";
4585 case VM_METHOD_TYPE_CFUNC: return "cfunc";
4586 case VM_METHOD_TYPE_ZSUPER: return "zsuper";
4587 case VM_METHOD_TYPE_MISSING: return "missing";
4588 case VM_METHOD_TYPE_OPTIMIZED: return "optimized";
4589 case VM_METHOD_TYPE_UNDEF: return "undef";
4590 case VM_METHOD_TYPE_NOTIMPLEMENTED: return "notimplemented";
4591 }
4592 rb_bug("rb_method_type_name: unreachable (type: %d)", type);
4593}
4594
4595static void
4596rb_raw_iseq_info(char *const buff, const size_t buff_size, const rb_iseq_t *iseq)
4597{
4598 if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj, T_MOVED)) {
4599 VALUE path = rb_iseq_path(iseq);
4600 int n = ISEQ_BODY(iseq)->location.first_lineno;
4601 snprintf(buff, buff_size, " %s@%s:%d",
4602 RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
4603 RSTRING_PTR(path), n);
4604 }
4605}
4606
4607static int
4608str_len_no_raise(VALUE str)
4609{
4610 long len = RSTRING_LEN(str);
4611 if (len < 0) return 0;
4612 if (len > INT_MAX) return INT_MAX;
4613 return (int)len;
4614}
4615
4616#define BUFF_ARGS buff + pos, buff_size - pos
4617#define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
4618#define APPEND_S(s) do { \
4619 if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
4620 goto end; \
4621 } \
4622 else { \
4623 memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
4624 } \
4625 } while (0)
4626#define C(c, s) ((c) != 0 ? (s) : " ")
4627
4628static size_t
4629rb_raw_obj_info_common(char *const buff, const size_t buff_size, const VALUE obj)
4630{
4631 size_t pos = 0;
4632
4633 if (SPECIAL_CONST_P(obj)) {
4634 APPEND_F("%s", obj_type_name(obj));
4635
4636 if (FIXNUM_P(obj)) {
4637 APPEND_F(" %ld", FIX2LONG(obj));
4638 }
4639 else if (SYMBOL_P(obj)) {
4640 APPEND_F(" %s", rb_id2name(SYM2ID(obj)));
4641 }
4642 }
4643 else {
4644 // const int age = RVALUE_AGE_GET(obj);
4645
4646 if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj)) {
4647 APPEND_F("%p %s/", (void *)obj, obj_type_name(obj));
4648 // TODO: fixme
4649 // APPEND_F("%p [%d%s%s%s%s%s%s] %s ",
4650 // (void *)obj, age,
4651 // C(RVALUE_UNCOLLECTIBLE_BITMAP(obj), "L"),
4652 // C(RVALUE_MARK_BITMAP(obj), "M"),
4653 // C(RVALUE_PIN_BITMAP(obj), "P"),
4654 // C(RVALUE_MARKING_BITMAP(obj), "R"),
4655 // C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
4656 // C(rb_objspace_garbage_object_p(obj), "G"),
4657 // obj_type_name(obj));
4658 }
4659 else {
4660 /* fake */
4661 // APPEND_F("%p [%dXXXX] %s",
4662 // (void *)obj, age,
4663 // obj_type_name(obj));
4664 }
4665
4666 if (internal_object_p(obj)) {
4667 /* ignore */
4668 }
4669 else if (RBASIC(obj)->klass == 0) {
4670 APPEND_S("(temporary internal)");
4671 }
4672 else if (RTEST(RBASIC(obj)->klass)) {
4673 VALUE class_path = rb_class_path_cached(RBASIC(obj)->klass);
4674 if (!NIL_P(class_path)) {
4675 APPEND_F("%s ", RSTRING_PTR(class_path));
4676 }
4677 }
4678 }
4679 end:
4680
4681 return pos;
4682}
4683
4684const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
4685
4686static size_t
4687rb_raw_obj_info_buitin_type(char *const buff, const size_t buff_size, const VALUE obj, size_t pos)
4688{
4689 if (LIKELY(pos < buff_size) && !SPECIAL_CONST_P(obj)) {
4690 const enum ruby_value_type type = BUILTIN_TYPE(obj);
4691
4692 switch (type) {
4693 case T_NODE:
4694 UNEXPECTED_NODE(rb_raw_obj_info);
4695 break;
4696 case T_ARRAY:
4697 if (ARY_SHARED_P(obj)) {
4698 APPEND_S("shared -> ");
4699 rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
4700 }
4701 else if (ARY_EMBED_P(obj)) {
4702 APPEND_F("[%s%s] len: %ld (embed)",
4703 C(ARY_EMBED_P(obj), "E"),
4704 C(ARY_SHARED_P(obj), "S"),
4705 RARRAY_LEN(obj));
4706 }
4707 else {
4708 APPEND_F("[%s%s] len: %ld, capa:%ld ptr:%p",
4709 C(ARY_EMBED_P(obj), "E"),
4710 C(ARY_SHARED_P(obj), "S"),
4711 RARRAY_LEN(obj),
4712 ARY_EMBED_P(obj) ? -1L : RARRAY(obj)->as.heap.aux.capa,
4713 (void *)RARRAY_CONST_PTR(obj));
4714 }
4715 break;
4716 case T_STRING: {
4717 if (STR_SHARED_P(obj)) {
4718 APPEND_F(" [shared] len: %ld", RSTRING_LEN(obj));
4719 }
4720 else {
4721 if (STR_EMBED_P(obj)) APPEND_S(" [embed]");
4722
4723 APPEND_F(" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj), rb_str_capacity(obj));
4724 }
4725 APPEND_F(" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
4726 break;
4727 }
4728 case T_SYMBOL: {
4729 VALUE fstr = RSYMBOL(obj)->fstr;
4730 ID id = RSYMBOL(obj)->id;
4731 if (RB_TYPE_P(fstr, T_STRING)) {
4732 APPEND_F(":%s id:%d", RSTRING_PTR(fstr), (unsigned int)id);
4733 }
4734 else {
4735 APPEND_F("(%p) id:%d", (void *)fstr, (unsigned int)id);
4736 }
4737 break;
4738 }
4739 case T_MOVED: {
4740 APPEND_F("-> %p", (void*)gc_location_internal(rb_gc_get_objspace(), obj));
4741 break;
4742 }
4743 case T_HASH: {
4744 APPEND_F("[%c] %"PRIdSIZE,
4745 RHASH_AR_TABLE_P(obj) ? 'A' : 'S',
4746 RHASH_SIZE(obj));
4747 break;
4748 }
4749 case T_CLASS:
4750 case T_MODULE:
4751 {
4752 VALUE class_path = rb_class_path_cached(obj);
4753 if (!NIL_P(class_path)) {
4754 APPEND_F("%s", RSTRING_PTR(class_path));
4755 }
4756 else {
4757 APPEND_S("(anon)");
4758 }
4759 break;
4760 }
4761 case T_ICLASS:
4762 {
4763 VALUE class_path = rb_class_path_cached(RBASIC_CLASS(obj));
4764 if (!NIL_P(class_path)) {
4765 APPEND_F("src:%s", RSTRING_PTR(class_path));
4766 }
4767 break;
4768 }
4769 case T_OBJECT:
4770 {
4771 if (rb_shape_obj_too_complex_p(obj)) {
4772 size_t hash_len = rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
4773 APPEND_F("(too_complex) len:%zu", hash_len);
4774 }
4775 else {
4776 uint32_t len = ROBJECT_FIELDS_CAPACITY(obj);
4777
4778 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
4779 APPEND_F("(embed) len:%d", len);
4780 }
4781 else {
4782 VALUE *ptr = ROBJECT_FIELDS(obj);
4783 APPEND_F("len:%d ptr:%p", len, (void *)ptr);
4784 }
4785 }
4786 }
4787 break;
4788 case T_DATA: {
4789 const struct rb_block *block;
4790 const rb_iseq_t *iseq;
4791 if (rb_obj_is_proc(obj) &&
4792 (block = vm_proc_block(obj)) != NULL &&
4793 (vm_block_type(block) == block_type_iseq) &&
4794 (iseq = vm_block_iseq(block)) != NULL) {
4795 rb_raw_iseq_info(BUFF_ARGS, iseq);
4796 }
4797 else if (rb_ractor_p(obj)) {
4798 rb_ractor_t *r = (void *)DATA_PTR(obj);
4799 if (r) {
4800 APPEND_F("r:%d", r->pub.id);
4801 }
4802 }
4803 else {
4804 const char * const type_name = rb_objspace_data_type_name(obj);
4805 if (type_name) {
4806 APPEND_F("%s", type_name);
4807 }
4808 }
4809 break;
4810 }
4811 case T_IMEMO: {
4812 APPEND_F("<%s> ", rb_imemo_name(imemo_type(obj)));
4813
4814 switch (imemo_type(obj)) {
4815 case imemo_ment:
4816 {
4817 const rb_method_entry_t *me = (const rb_method_entry_t *)obj;
4818
4819 APPEND_F(":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
4820 rb_id2name(me->called_id),
4821 METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ? "pub" :
4822 METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ? "pri" : "pro",
4823 METHOD_ENTRY_COMPLEMENTED(me) ? ",cmp" : "",
4824 METHOD_ENTRY_CACHED(me) ? ",cc" : "",
4825 METHOD_ENTRY_INVALIDATED(me) ? ",inv" : "",
4826 me->def ? rb_method_type_name(me->def->type) : "NULL",
4827 me->def ? me->def->aliased : -1,
4828 (void *)me->owner, // obj_info(me->owner),
4829 (void *)me->defined_class); //obj_info(me->defined_class)));
4830
4831 if (me->def) {
4832 switch (me->def->type) {
4833 case VM_METHOD_TYPE_ISEQ:
4834 APPEND_S(" (iseq:");
4835 rb_raw_obj_info(BUFF_ARGS, (VALUE)me->def->body.iseq.iseqptr);
4836 APPEND_S(")");
4837 break;
4838 default:
4839 break;
4840 }
4841 }
4842
4843 break;
4844 }
4845 case imemo_iseq: {
4846 const rb_iseq_t *iseq = (const rb_iseq_t *)obj;
4847 rb_raw_iseq_info(BUFF_ARGS, iseq);
4848 break;
4849 }
4850 case imemo_callinfo:
4851 {
4852 const struct rb_callinfo *ci = (const struct rb_callinfo *)obj;
4853 APPEND_F("(mid:%s, flag:%x argc:%d, kwarg:%s)",
4854 rb_id2name(vm_ci_mid(ci)),
4855 vm_ci_flag(ci),
4856 vm_ci_argc(ci),
4857 vm_ci_kwarg(ci) ? "available" : "NULL");
4858 break;
4859 }
4860 case imemo_callcache:
4861 {
4862 const struct rb_callcache *cc = (const struct rb_callcache *)obj;
4863 VALUE class_path = vm_cc_valid(cc) ? rb_class_path_cached(cc->klass) : Qnil;
4864 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4865
4866 APPEND_F("(klass:%s cme:%s%s (%p) call:%p",
4867 NIL_P(class_path) ? (vm_cc_valid(cc) ? "??" : "<NULL>") : RSTRING_PTR(class_path),
4868 cme ? rb_id2name(cme->called_id) : "<NULL>",
4869 cme ? (METHOD_ENTRY_INVALIDATED(cme) ? " [inv]" : "") : "",
4870 (void *)cme,
4871 (void *)(uintptr_t)vm_cc_call(cc));
4872 break;
4873 }
4874 default:
4875 break;
4876 }
4877 }
4878 default:
4879 break;
4880 }
4881 }
4882 end:
4883
4884 return pos;
4885}
4886
4887#undef C
4888
4889#ifdef RUBY_ASAN_ENABLED
4890void
4891rb_asan_poison_object(VALUE obj)
4892{
4893 MAYBE_UNUSED(struct RVALUE *) ptr = (void *)obj;
4894 asan_poison_memory_region(ptr, rb_gc_obj_slot_size(obj));
4895}
4896
4897void
4898rb_asan_unpoison_object(VALUE obj, bool newobj_p)
4899{
4900 MAYBE_UNUSED(struct RVALUE *) ptr = (void *)obj;
4901 asan_unpoison_memory_region(ptr, rb_gc_obj_slot_size(obj), newobj_p);
4902}
4903
4904void *
4905rb_asan_poisoned_object_p(VALUE obj)
4906{
4907 MAYBE_UNUSED(struct RVALUE *) ptr = (void *)obj;
4908 return __asan_region_is_poisoned(ptr, rb_gc_obj_slot_size(obj));
4909}
4910#endif
4911
4912static void
4913raw_obj_info(char *const buff, const size_t buff_size, VALUE obj)
4914{
4915 size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4916 pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4917 if (pos >= buff_size) {} // truncated
4918}
4919
4920const char *
4921rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj)
4922{
4923 void *objspace = rb_gc_get_objspace();
4924
4925 if (SPECIAL_CONST_P(obj)) {
4926 raw_obj_info(buff, buff_size, obj);
4927 }
4928 else if (!rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj)) {
4929 snprintf(buff, buff_size, "out-of-heap:%p", (void *)obj);
4930 }
4931#if 0 // maybe no need to check it?
4932 else if (0 && rb_gc_impl_garbage_object_p(objspace, obj)) {
4933 snprintf(buff, buff_size, "garbage:%p", (void *)obj);
4934 }
4935#endif
4936 else {
4937 asan_unpoisoning_object(obj) {
4938 raw_obj_info(buff, buff_size, obj);
4939 }
4940 }
4941 return buff;
4942}
4943
4944#undef APPEND_S
4945#undef APPEND_F
4946#undef BUFF_ARGS
4947
4948#if RGENGC_OBJ_INFO
4949#define OBJ_INFO_BUFFERS_NUM 10
4950#define OBJ_INFO_BUFFERS_SIZE 0x100
4951static rb_atomic_t obj_info_buffers_index = 0;
4952static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4953
4954/* Increments *var atomically and resets *var to 0 when maxval is
4955 * reached. Returns the wraparound old *var value (0...maxval). */
4956static rb_atomic_t
4957atomic_inc_wraparound(rb_atomic_t *var, const rb_atomic_t maxval)
4958{
4959 rb_atomic_t oldval = RUBY_ATOMIC_FETCH_ADD(*var, 1);
4960 if (RB_UNLIKELY(oldval >= maxval - 1)) { // wraparound *var
4961 const rb_atomic_t newval = oldval + 1;
4962 RUBY_ATOMIC_CAS(*var, newval, newval % maxval);
4963 oldval %= maxval;
4964 }
4965 return oldval;
4966}
4967
4968static const char *
4969obj_info(VALUE obj)
4970{
4971 rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4972 char *const buff = obj_info_buffers[index];
4973 return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4974}
4975#else
4976static const char *
4977obj_info(VALUE obj)
4978{
4979 return obj_type_name(obj);
4980}
4981#endif
4982
4983/*
4984 ------------------------ Extended allocator ------------------------
4985*/
4986
4988 VALUE exc;
4989 const char *fmt;
4990 va_list *ap;
4991};
4992
4993static void *
4994gc_vraise(void *ptr)
4995{
4996 struct gc_raise_tag *argv = ptr;
4997 rb_vraise(argv->exc, argv->fmt, *argv->ap);
4998 UNREACHABLE_RETURN(NULL);
4999}
5000
5001static void
5002gc_raise(VALUE exc, const char *fmt, ...)
5003{
5004 va_list ap;
5005 va_start(ap, fmt);
5006 struct gc_raise_tag argv = {
5007 exc, fmt, &ap,
5008 };
5009
5010 if (ruby_thread_has_gvl_p()) {
5011 gc_vraise(&argv);
5013 }
5014 else if (ruby_native_thread_p()) {
5015 rb_thread_call_with_gvl(gc_vraise, &argv);
5017 }
5018 else {
5019 /* Not in a ruby thread */
5020 fprintf(stderr, "%s", "[FATAL] ");
5021 vfprintf(stderr, fmt, ap);
5022 }
5023
5024 va_end(ap);
5025 abort();
5026}
5027
5028NORETURN(static void negative_size_allocation_error(const char *));
5029static void
5030negative_size_allocation_error(const char *msg)
5031{
5032 gc_raise(rb_eNoMemError, "%s", msg);
5033}
5034
5035static void *
5036ruby_memerror_body(void *dummy)
5037{
5038 rb_memerror();
5039 return 0;
5040}
5041
5042NORETURN(static void ruby_memerror(void));
5044static void
5045ruby_memerror(void)
5046{
5047 if (ruby_thread_has_gvl_p()) {
5048 rb_memerror();
5049 }
5050 else {
5051 if (ruby_native_thread_p()) {
5052 rb_thread_call_with_gvl(ruby_memerror_body, 0);
5053 }
5054 else {
5055 /* no ruby thread */
5056 fprintf(stderr, "[FATAL] failed to allocate memory\n");
5057 }
5058 }
5059
5060 /* We have discussions whether we should die here; */
5061 /* We might rethink about it later. */
5062 exit(EXIT_FAILURE);
5063}
5064
5065void
5066rb_memerror(void)
5067{
5068 /* the `GET_VM()->special_exceptions` below assumes that
5069 * the VM is reachable from the current thread. We should
5070 * definitely make sure of that. */
5071 RUBY_ASSERT_ALWAYS(ruby_thread_has_gvl_p());
5072
5073 rb_execution_context_t *ec = GET_EC();
5074 VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
5075
5076 if (!exc ||
5077 rb_ec_raised_p(ec, RAISED_NOMEMORY) ||
5078 rb_ec_vm_lock_rec(ec) != ec->tag->lock_rec) {
5079 fprintf(stderr, "[FATAL] failed to allocate memory\n");
5080 exit(EXIT_FAILURE);
5081 }
5082 if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
5083 rb_ec_raised_clear(ec);
5084 }
5085 else {
5086 rb_ec_raised_set(ec, RAISED_NOMEMORY);
5087 exc = ruby_vm_special_exception_copy(exc);
5088 }
5089 ec->errinfo = exc;
5090 EC_JUMP_TAG(ec, TAG_RAISE);
5091}
5092
5093bool
5094rb_memerror_reentered(void)
5095{
5096 rb_execution_context_t *ec = GET_EC();
5097 return (ec && rb_ec_raised_p(ec, RAISED_NOMEMORY));
5098}
5099
5100static void *
5101handle_malloc_failure(void *ptr)
5102{
5103 if (LIKELY(ptr)) {
5104 return ptr;
5105 }
5106 else {
5107 ruby_memerror();
5108 UNREACHABLE_RETURN(ptr);
5109 }
5110}
5111
5112static void *ruby_xmalloc_body(size_t size);
5113
5114void *
5115ruby_xmalloc(size_t size)
5116{
5117 return handle_malloc_failure(ruby_xmalloc_body(size));
5118}
5119
5120static void *
5121ruby_xmalloc_body(size_t size)
5122{
5123 if ((ssize_t)size < 0) {
5124 negative_size_allocation_error("too large allocation size");
5125 }
5126
5127 return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
5128}
5129
5130void
5131ruby_malloc_size_overflow(size_t count, size_t elsize)
5132{
5133 rb_raise(rb_eArgError,
5134 "malloc: possible integer overflow (%"PRIuSIZE"*%"PRIuSIZE")",
5135 count, elsize);
5136}
5137
5138void
5139ruby_malloc_add_size_overflow(size_t x, size_t y)
5140{
5141 rb_raise(rb_eArgError,
5142 "malloc: possible integer overflow (%"PRIuSIZE"+%"PRIuSIZE")",
5143 x, y);
5144}
5145
5146static void *ruby_xmalloc2_body(size_t n, size_t size);
5147
5148void *
5149ruby_xmalloc2(size_t n, size_t size)
5150{
5151 return handle_malloc_failure(ruby_xmalloc2_body(n, size));
5152}
5153
5154static void *
5155ruby_xmalloc2_body(size_t n, size_t size)
5156{
5157 return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
5158}
5159
5160static void *ruby_xcalloc_body(size_t n, size_t size);
5161
5162void *
5163ruby_xcalloc(size_t n, size_t size)
5164{
5165 return handle_malloc_failure(ruby_xcalloc_body(n, size));
5166}
5167
5168static void *
5169ruby_xcalloc_body(size_t n, size_t size)
5170{
5171 return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
5172}
5173
5174static void *ruby_sized_xrealloc_body(void *ptr, size_t new_size, size_t old_size);
5175
5176#ifdef ruby_sized_xrealloc
5177#undef ruby_sized_xrealloc
5178#endif
5179void *
5180ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size)
5181{
5182 return handle_malloc_failure(ruby_sized_xrealloc_body(ptr, new_size, old_size));
5183}
5184
5185static void *
5186ruby_sized_xrealloc_body(void *ptr, size_t new_size, size_t old_size)
5187{
5188 if ((ssize_t)new_size < 0) {
5189 negative_size_allocation_error("too large allocation size");
5190 }
5191
5192 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size);
5193}
5194
5195void *
5196ruby_xrealloc(void *ptr, size_t new_size)
5197{
5198 return ruby_sized_xrealloc(ptr, new_size, 0);
5199}
5200
5201static void *ruby_sized_xrealloc2_body(void *ptr, size_t n, size_t size, size_t old_n);
5202
5203#ifdef ruby_sized_xrealloc2
5204#undef ruby_sized_xrealloc2
5205#endif
5206void *
5207ruby_sized_xrealloc2(void *ptr, size_t n, size_t size, size_t old_n)
5208{
5209 return handle_malloc_failure(ruby_sized_xrealloc2_body(ptr, n, size, old_n));
5210}
5211
5212static void *
5213ruby_sized_xrealloc2_body(void *ptr, size_t n, size_t size, size_t old_n)
5214{
5215 size_t len = xmalloc2_size(n, size);
5216 return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, len, old_n * size);
5217}
5218
5219void *
5220ruby_xrealloc2(void *ptr, size_t n, size_t size)
5221{
5222 return ruby_sized_xrealloc2(ptr, n, size, 0);
5223}
5224
5225#ifdef ruby_sized_xfree
5226#undef ruby_sized_xfree
5227#endif
5228void
5229ruby_sized_xfree(void *x, size_t size)
5230{
5231 if (LIKELY(x)) {
5232 /* It's possible for a C extension's pthread destructor function set by pthread_key_create
5233 * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in
5234 * that case. */
5235 if (LIKELY(GET_VM())) {
5236 rb_gc_impl_free(rb_gc_get_objspace(), x, size);
5237 }
5238 else {
5239 ruby_mimfree(x);
5240 }
5241 }
5242}
5243
5244void
5245ruby_xfree(void *x)
5246{
5247 ruby_sized_xfree(x, 0);
5248}
5249
5250void *
5251rb_xmalloc_mul_add(size_t x, size_t y, size_t z) /* x * y + z */
5252{
5253 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5254 return ruby_xmalloc(w);
5255}
5256
5257void *
5258rb_xcalloc_mul_add(size_t x, size_t y, size_t z) /* x * y + z */
5259{
5260 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5261 return ruby_xcalloc(w, 1);
5262}
5263
5264void *
5265rb_xrealloc_mul_add(const void *p, size_t x, size_t y, size_t z) /* x * y + z */
5266{
5267 size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
5268 return ruby_xrealloc((void *)p, w);
5269}
5270
5271void *
5272rb_xmalloc_mul_add_mul(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
5273{
5274 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5275 return ruby_xmalloc(u);
5276}
5277
5278void *
5279rb_xcalloc_mul_add_mul(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
5280{
5281 size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
5282 return ruby_xcalloc(u, 1);
5283}
5284
5285/* Mimic ruby_xmalloc, but need not rb_objspace.
5286 * should return pointer suitable for ruby_xfree
5287 */
5288void *
5289ruby_mimmalloc(size_t size)
5290{
5291 void *mem;
5292#if CALC_EXACT_MALLOC_SIZE
5293 size += sizeof(struct malloc_obj_info);
5294#endif
5295 mem = malloc(size);
5296#if CALC_EXACT_MALLOC_SIZE
5297 if (!mem) {
5298 return NULL;
5299 }
5300 else
5301 /* set 0 for consistency of allocated_size/allocations */
5302 {
5303 struct malloc_obj_info *info = mem;
5304 info->size = 0;
5305 mem = info + 1;
5306 }
5307#endif
5308 return mem;
5309}
5310
5311void *
5312ruby_mimcalloc(size_t num, size_t size)
5313{
5314 void *mem;
5315#if CALC_EXACT_MALLOC_SIZE
5316 struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5317 if (UNLIKELY(t.left)) {
5318 return NULL;
5319 }
5320 size = t.right + sizeof(struct malloc_obj_info);
5321 mem = calloc1(size);
5322 if (!mem) {
5323 return NULL;
5324 }
5325 else
5326 /* set 0 for consistency of allocated_size/allocations */
5327 {
5328 struct malloc_obj_info *info = mem;
5329 info->size = 0;
5330 mem = info + 1;
5331 }
5332#else
5333 mem = calloc(num, size);
5334#endif
5335 return mem;
5336}
5337
5338void
5339ruby_mimfree(void *ptr)
5340{
5341#if CALC_EXACT_MALLOC_SIZE
5342 struct malloc_obj_info *info = (struct malloc_obj_info *)ptr - 1;
5343 ptr = info;
5344#endif
5345 free(ptr);
5346}
5347
5348void
5349rb_gc_adjust_memory_usage(ssize_t diff)
5350{
5351 unless_objspace(objspace) { return; }
5352
5353 rb_gc_impl_adjust_memory_usage(objspace, diff);
5354}
5355
5356const char *
5357rb_obj_info(VALUE obj)
5358{
5359 return obj_info(obj);
5360}
5361
5362void
5363rb_obj_info_dump(VALUE obj)
5364{
5365 char buff[0x100];
5366 fprintf(stderr, "rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
5367}
5368
5369void
5370rb_obj_info_dump_loc(VALUE obj, const char *file, int line, const char *func)
5371{
5372 char buff[0x100];
5373 fprintf(stderr, "<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
5374}
5375
5376void
5377rb_gc_before_fork(void)
5378{
5379 rb_gc_impl_before_fork(rb_gc_get_objspace());
5380}
5381
5382void
5383rb_gc_after_fork(rb_pid_t pid)
5384{
5385 rb_gc_impl_after_fork(rb_gc_get_objspace(), pid);
5386}
5387
5388/*
5389 * Document-module: ObjectSpace
5390 *
5391 * The ObjectSpace module contains a number of routines
5392 * that interact with the garbage collection facility and allow you to
5393 * traverse all living objects with an iterator.
5394 *
5395 * ObjectSpace also provides support for object finalizers, procs that will be
5396 * called after a specific object was destroyed by garbage collection. See
5397 * the documentation for +ObjectSpace.define_finalizer+ for important
5398 * information on how to use this method correctly.
5399 *
5400 * a = "A"
5401 * b = "B"
5402 *
5403 * ObjectSpace.define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
5404 * ObjectSpace.define_finalizer(b, proc {|id| puts "Finalizer two on #{id}" })
5405 *
5406 * a = nil
5407 * b = nil
5408 *
5409 * _produces:_
5410 *
5411 * Finalizer two on 537763470
5412 * Finalizer one on 537763480
5413 */
5414
5415/* Document-class: GC::Profiler
5416 *
5417 * The GC profiler provides access to information on GC runs including time,
5418 * length and object space size.
5419 *
5420 * Example:
5421 *
5422 * GC::Profiler.enable
5423 *
5424 * require 'rdoc/rdoc'
5425 *
5426 * GC::Profiler.report
5427 *
5428 * GC::Profiler.disable
5429 *
5430 * See also GC.count, GC.malloc_allocated_size and GC.malloc_allocations
5431 */
5432
5433#include "gc.rbinc"
5434
5435void
5436Init_GC(void)
5437{
5438#undef rb_intern
5439 rb_gc_register_address(&id2ref_value);
5440
5441 malloc_offset = gc_compute_malloc_offset();
5442
5443 rb_mGC = rb_define_module("GC");
5444
5445 VALUE rb_mObjSpace = rb_define_module("ObjectSpace");
5446
5447 rb_define_module_function(rb_mObjSpace, "each_object", os_each_obj, -1);
5448
5449 rb_define_module_function(rb_mObjSpace, "define_finalizer", define_final, -1);
5450 rb_define_module_function(rb_mObjSpace, "undefine_finalizer", undefine_final, 1);
5451
5452 rb_define_module_function(rb_mObjSpace, "_id2ref", os_id2ref, 1);
5453
5454 rb_vm_register_special_exception(ruby_error_nomemory, rb_eNoMemError, "failed to allocate memory");
5455
5456 rb_define_method(rb_cBasicObject, "__id__", rb_obj_id, 0);
5457 rb_define_method(rb_mKernel, "object_id", rb_obj_id, 0);
5458
5459 rb_define_module_function(rb_mObjSpace, "count_objects", count_objects, -1);
5460
5461 rb_gc_impl_init();
5462}
5463
5464// Set a name for the anonymous virtual memory area. `addr` is the starting
5465// address of the area and `size` is its length in bytes. `name` is a
5466// NUL-terminated human-readable string.
5467//
5468// This function is usually called after calling `mmap()`. The human-readable
5469// annotation helps developers identify the call site of `mmap()` that created
5470// the memory mapping.
5471//
5472// This function currently only works on Linux 5.17 or higher. After calling
5473// this function, we can see annotations in the form of "[anon:...]" in
5474// `/proc/self/maps`, where `...` is the content of `name`. This function has
5475// no effect when called on other platforms.
5476void
5477ruby_annotate_mmap(const void *addr, unsigned long size, const char *name)
5478{
5479#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
5480 // The name length cannot exceed 80 (including the '\0').
5481 RUBY_ASSERT(strlen(name) < 80);
5482 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);
5483 // We ignore errors in prctl. prctl may set errno to EINVAL for several
5484 // reasons.
5485 // 1. The attr (PR_SET_VMA_ANON_NAME) is not a valid attribute.
5486 // 2. addr is an invalid address.
5487 // 3. The string pointed by name is too long.
5488 // The first error indicates PR_SET_VMA_ANON_NAME is not available, and may
5489 // happen if we run the compiled binary on an old kernel. In theory, all
5490 // other errors should result in a failure. But since EINVAL cannot tell
5491 // the first error from others, and this function is mainly used for
5492 // debugging, we silently ignore the error.
5493 errno = 0;
5494#endif
5495}
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
Definition atomic.h:406
#define RUBY_ATOMIC_SIZE_FETCH_ADD(var, val)
Identical to RUBY_ATOMIC_FETCH_ADD, except it expects its arguments to be size_t.
Definition atomic.h:235
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
Definition atomic.h:165
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
Definition atomic.h:118
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
Definition event.h:93
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
Definition fl_type.h:463
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:600
@ RUBY_FL_WB_PROTECTED
Definition fl_type.h:198
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1592
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3133
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define LL2NUM
Old name of RB_LL2NUM.
Definition long_long.h:30
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define T_NODE
Old name of RUBY_T_NODE.
Definition value_type.h:73
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
Definition fl_type.h:61
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define FL_ABLE
Old name of RB_FL_ABLE.
Definition fl_type.h:121
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define T_UNDEF
Old name of RUBY_T_UNDEF.
Definition value_type.h:82
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
Definition value_type.h:83
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
Definition long_long.h:35
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_MATCH
Old name of RUBY_T_MATCH.
Definition value_type.h:69
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define T_MOVED
Old name of RUBY_T_MOVED.
Definition value_type.h:71
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition fl_type.h:132
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
Definition size_t.h:61
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
Definition gc.c:2543
int ruby_stack_check(void)
Checks for stack overflow.
Definition gc.c:2583
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
VALUE rb_eNoMemError
NoMemoryError exception.
Definition error.c:1441
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1434
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:475
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_mKernel
Kernel module.
Definition object.c:61
VALUE rb_mGC
GC module.
Definition gc.c:424
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:243
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:60
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:878
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
Definition object.c:95
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3221
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
Definition fixnum.h:43
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:932
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
Defines RBIMPL_HAS_BUILTIN.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:239
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:847
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:120
void rb_str_free(VALUE str)
Destroys the given string for no reason.
Definition string.c:1717
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:986
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
Definition variable.c:388
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
Definition variable.c:1269
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1602
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:686
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition vm_method.c:1608
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:3293
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1005
int capa
Designed capacity of the buffer.
Definition io.h:11
int rb_io_fptr_finalize(rb_io_t *fptr)
Destroys the given IO.
Definition io.c:5675
int len
Length of the buffer.
Definition io.h:8
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
Definition thread.c:2037
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1372
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:44
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:163
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS(obj)
Convenient casting macro.
Definition rclass.h:38
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
#define RDATA(obj)
Convenient casting macro.
Definition rdata.h:59
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
Definition rdata.h:78
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
Definition rdata.h:104
#define RFILE(obj)
Convenient casting macro.
Definition rfile.h:50
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
#define RMATCH(obj)
Convenient casting macro.
Definition rmatch.h:37
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
Definition robject.h:126
#define RREGEXP(obj)
Convenient casting macro.
Definition rregexp.h:37
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition rregexp.h:45
#define RSTRING(obj)
Convenient casting macro.
Definition rstring.h:41
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:586
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:458
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition rtypeddata.h:95
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:609
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:513
void rb_p(VALUE obj)
Inspects an object.
Definition io.c:9067
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
Definition thread.c:5759
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
Defines old _.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
Definition hash.h:53
Ruby's ordinal objects.
Definition robject.h:83
"Typed" user data.
Definition rtypeddata.h:354
Definition method.h:63
Definition constant.h:33
CREF (Class REFerence)
Definition method.h:45
Definition class.h:72
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:204
RUBY_DATA_FUNC dfree
This function is called when the object is no longer used.
Definition rtypeddata.h:234
RUBY_DATA_FUNC dcompact
This function is called when the object is relocated.
Definition rtypeddata.h:255
struct rb_data_type_struct::@54 function
Function pointers.
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:211
RUBY_DATA_FUNC dmark
This function is called when the object is experiencing GC marks.
Definition rtypeddata.h:225
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:313
Definition gc_impl.h:15
Ruby's IO, metadata and buffers.
Definition io.h:295
Represents a match.
Definition rmatch.h:71
struct rmatch_offset * char_offset
Capture group offsets, in C array.
Definition rmatch.h:79
int char_offset_num_allocated
Number of rmatch_offset that ::rmatch::char_offset holds.
Definition rmatch.h:82
struct re_registers regs
"Registers" of a match.
Definition rmatch.h:76
Definition method.h:55
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:136
Definition class.h:65
Represents the region of a capture group.
Definition rmatch.h:65
Definition st.h:79
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
ruby_value_type
C-level type of an object.
Definition value_type.h:113