Ruby  3.4.0dev (2024-11-22 revision 37a72b0150ec36b4ea27175039afc28c62207b0c)
gc.c (37a72b0150ec36b4ea27175039afc28c62207b0c)
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 RUBY_ALTERNATIVE_MALLOC_HEADER
34 /* Alternative malloc header is included in ruby/missing.h */
35 # elif defined(HAVE_MALLOC_H)
36 # include <malloc.h>
37 # elif defined(HAVE_MALLOC_NP_H)
38 # include <malloc_np.h>
39 # elif defined(HAVE_MALLOC_MALLOC_H)
40 # include <malloc/malloc.h>
41 # endif
42 
43 # ifdef _WIN32
44 # define HAVE_MALLOC_USABLE_SIZE
45 # define malloc_usable_size(a) _msize(a)
46 # elif defined HAVE_MALLOC_SIZE
47 # define HAVE_MALLOC_USABLE_SIZE
48 # define malloc_usable_size(a) malloc_size(a)
49 # endif
50 #else
51 # include <malloc.h>
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 __linux__
79 #include <linux/prctl.h>
80 #include <sys/prctl.h>
81 #endif
82 
83 #undef LIST_HEAD /* ccan/list conflicts with BSD-origin sys/queue.h. */
84 
85 #include "constant.h"
86 #include "darray.h"
87 #include "debug_counter.h"
88 #include "eval_intern.h"
89 #include "gc/gc.h"
90 #include "id_table.h"
91 #include "internal.h"
92 #include "internal/class.h"
93 #include "internal/compile.h"
94 #include "internal/complex.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 "rjit.h"
113 #include "probes.h"
114 #include "regint.h"
115 #include "ruby/debug.h"
116 #include "ruby/io.h"
117 #include "ruby/re.h"
118 #include "ruby/st.h"
119 #include "ruby/thread.h"
120 #include "ruby/util.h"
121 #include "ruby/vm.h"
122 #include "ruby_assert.h"
123 #include "ruby_atomic.h"
124 #include "symbol.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 
134 unsigned int
135 rb_gc_vm_lock(void)
136 {
137  unsigned int lev;
138  RB_VM_LOCK_ENTER_LEV(&lev);
139  return lev;
140 }
141 
142 void
143 rb_gc_vm_unlock(unsigned int lev)
144 {
145  RB_VM_LOCK_LEAVE_LEV(&lev);
146 }
147 
148 unsigned int
149 rb_gc_cr_lock(void)
150 {
151  unsigned int lev;
152  RB_VM_LOCK_ENTER_CR_LEV(GET_RACTOR(), &lev);
153  return lev;
154 }
155 
156 void
157 rb_gc_cr_unlock(unsigned int lev)
158 {
159  RB_VM_LOCK_LEAVE_CR_LEV(GET_RACTOR(), &lev);
160 }
161 
162 unsigned int
163 rb_gc_vm_lock_no_barrier(void)
164 {
165  unsigned int lev = 0;
166  RB_VM_LOCK_ENTER_LEV_NB(&lev);
167  return lev;
168 }
169 
170 void
171 rb_gc_vm_unlock_no_barrier(unsigned int lev)
172 {
173  RB_VM_LOCK_LEAVE_LEV(&lev);
174 }
175 
176 void
177 rb_gc_vm_barrier(void)
178 {
179  rb_vm_barrier();
180 }
181 
182 void
183 rb_gc_event_hook(VALUE obj, rb_event_flag_t event)
184 {
185  if (LIKELY(!(ruby_vm_event_flags & event))) return;
186 
187  rb_execution_context_t *ec = GET_EC();
188  if (!ec->cfp) return;
189 
190  EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, obj);
191 }
192 
193 void *
194 rb_gc_get_objspace(void)
195 {
196  return GET_VM()->gc.objspace;
197 }
198 
199 void
200 rb_gc_ractor_newobj_cache_foreach(void (*func)(void *cache, void *data), void *data)
201 {
202  rb_ractor_t *r = NULL;
203  ccan_list_for_each(&GET_VM()->ractor.set, r, vmlr_node) {
204  func(r->newobj_cache, data);
205  }
206 }
207 
208 void
209 rb_gc_run_obj_finalizer(VALUE objid, long count, VALUE (*callback)(long i, void *data), void *data)
210 {
211  volatile struct {
212  VALUE errinfo;
213  VALUE final;
214  rb_control_frame_t *cfp;
215  VALUE *sp;
216  long finished;
217  } saved;
218 
219  rb_execution_context_t * volatile ec = GET_EC();
220 #define RESTORE_FINALIZER() (\
221  ec->cfp = saved.cfp, \
222  ec->cfp->sp = saved.sp, \
223  ec->errinfo = saved.errinfo)
224 
225  saved.errinfo = ec->errinfo;
226  saved.cfp = ec->cfp;
227  saved.sp = ec->cfp->sp;
228  saved.finished = 0;
229  saved.final = Qundef;
230 
231  EC_PUSH_TAG(ec);
232  enum ruby_tag_type state = EC_EXEC_TAG();
233  if (state != TAG_NONE) {
234  ++saved.finished; /* skip failed finalizer */
235 
236  VALUE failed_final = saved.final;
237  saved.final = Qundef;
238  if (!UNDEF_P(failed_final) && !NIL_P(ruby_verbose)) {
239  rb_warn("Exception in finalizer %+"PRIsVALUE, failed_final);
240  rb_ec_error_print(ec, ec->errinfo);
241  }
242  }
243 
244  for (long i = saved.finished; RESTORE_FINALIZER(), i < count; saved.finished = ++i) {
245  saved.final = callback(i, data);
246  rb_check_funcall(saved.final, idCall, 1, &objid);
247  }
248  EC_POP_TAG();
249 #undef RESTORE_FINALIZER
250 }
251 
252 void
253 rb_gc_set_pending_interrupt(void)
254 {
255  rb_execution_context_t *ec = GET_EC();
256  ec->interrupt_mask |= PENDING_INTERRUPT_MASK;
257 }
258 
259 void
260 rb_gc_unset_pending_interrupt(void)
261 {
262  rb_execution_context_t *ec = GET_EC();
263  ec->interrupt_mask &= ~PENDING_INTERRUPT_MASK;
264 }
265 
266 bool
267 rb_gc_multi_ractor_p(void)
268 {
269  return rb_multi_ractor_p();
270 }
271 
272 bool rb_obj_is_main_ractor(VALUE gv);
273 
274 bool
275 rb_gc_shutdown_call_finalizer_p(VALUE obj)
276 {
277  switch (BUILTIN_TYPE(obj)) {
278  case T_DATA:
279  if (!ruby_free_at_exit_p() && (!DATA_PTR(obj) || !RDATA(obj)->dfree)) return false;
280  if (rb_obj_is_thread(obj)) return false;
281  if (rb_obj_is_mutex(obj)) return false;
282  if (rb_obj_is_fiber(obj)) return false;
283  if (rb_obj_is_main_ractor(obj)) return false;
284 
285  return true;
286 
287  case T_FILE:
288  return true;
289 
290  case T_SYMBOL:
291  if (RSYMBOL(obj)->fstr &&
292  (BUILTIN_TYPE(RSYMBOL(obj)->fstr) == T_NONE ||
293  BUILTIN_TYPE(RSYMBOL(obj)->fstr) == T_ZOMBIE)) {
294  RSYMBOL(obj)->fstr = 0;
295  }
296  return true;
297 
298  case T_NONE:
299  return false;
300 
301  default:
302  return ruby_free_at_exit_p();
303  }
304 }
305 
306 uint32_t
307 rb_gc_get_shape(VALUE obj)
308 {
309  return (uint32_t)rb_shape_get_shape_id(obj);
310 }
311 
312 void
313 rb_gc_set_shape(VALUE obj, uint32_t shape_id)
314 {
315  rb_shape_set_shape_id(obj, (uint32_t)shape_id);
316 }
317 
318 uint32_t
319 rb_gc_rebuild_shape(VALUE obj, size_t heap_id)
320 {
321  rb_shape_t *orig_shape = rb_shape_get_shape(obj);
322 
323  if (rb_shape_obj_too_complex(obj)) return (uint32_t)OBJ_TOO_COMPLEX_SHAPE_ID;
324 
325  rb_shape_t *initial_shape = rb_shape_get_shape_by_id((shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID));
326  rb_shape_t *new_shape = rb_shape_traverse_from_new_root(initial_shape, orig_shape);
327 
328  if (!new_shape) return 0;
329 
330  return (uint32_t)rb_shape_id(new_shape);
331 }
332 
333 void rb_vm_update_references(void *ptr);
334 
335 #define rb_setjmp(env) RUBY_SETJMP(env)
336 #define rb_jmp_buf rb_jmpbuf_t
337 #undef rb_data_object_wrap
338 
339 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
340 #define MAP_ANONYMOUS MAP_ANON
341 #endif
342 
343 #define unless_objspace(objspace) \
344  void *objspace; \
345  rb_vm_t *unless_objspace_vm = GET_VM(); \
346  if (unless_objspace_vm) objspace = unless_objspace_vm->gc.objspace; \
347  else /* return; or objspace will be warned uninitialized */
348 
349 #define RMOVED(obj) ((struct RMoved *)(obj))
350 
351 #define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \
352  if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \
353  *(_type *)&(_thing) = (_type)rb_gc_impl_location(_objspace, (VALUE)_thing); \
354  } \
355 } while (0)
356 
357 #define UPDATE_IF_MOVED(_objspace, _thing) TYPED_UPDATE_IF_MOVED(_objspace, VALUE, _thing)
358 
359 #if RUBY_MARK_FREE_DEBUG
360 int ruby_gc_debug_indent = 0;
361 #endif
362 
363 #ifndef RGENGC_OBJ_INFO
364 # define RGENGC_OBJ_INFO RGENGC_CHECK_MODE
365 #endif
366 
367 #ifndef CALC_EXACT_MALLOC_SIZE
368 # define CALC_EXACT_MALLOC_SIZE 0
369 #endif
370 
372 
373 static size_t malloc_offset = 0;
374 #if defined(HAVE_MALLOC_USABLE_SIZE)
375 static size_t
376 gc_compute_malloc_offset(void)
377 {
378  // Different allocators use different metadata storage strategies which result in different
379  // ideal sizes.
380  // For instance malloc(64) will waste 8B with glibc, but waste 0B with jemalloc.
381  // But malloc(56) will waste 0B with glibc, but waste 8B with jemalloc.
382  // So we try allocating 64, 56 and 48 bytes and select the first offset that doesn't
383  // waste memory.
384  // This was tested on Linux with glibc 2.35 and jemalloc 5, and for both it result in
385  // no wasted memory.
386  size_t offset = 0;
387  for (offset = 0; offset <= 16; offset += 8) {
388  size_t allocated = (64 - offset);
389  void *test_ptr = malloc(allocated);
390  size_t wasted = malloc_usable_size(test_ptr) - allocated;
391  free(test_ptr);
392 
393  if (wasted == 0) {
394  return offset;
395  }
396  }
397  return 0;
398 }
399 #else
400 static size_t
401 gc_compute_malloc_offset(void)
402 {
403  // If we don't have malloc_usable_size, we use powers of 2.
404  return 0;
405 }
406 #endif
407 
408 size_t
409 rb_malloc_grow_capa(size_t current, size_t type_size)
410 {
411  size_t current_capacity = current;
412  if (current_capacity < 4) {
413  current_capacity = 4;
414  }
415  current_capacity *= type_size;
416 
417  // We double the current capacity.
418  size_t new_capacity = (current_capacity * 2);
419 
420  // And round up to the next power of 2 if it's not already one.
421  if (rb_popcount64(new_capacity) != 1) {
422  new_capacity = (size_t)(1 << (64 - nlz_int64(new_capacity)));
423  }
424 
425  new_capacity -= malloc_offset;
426  new_capacity /= type_size;
427  if (current > new_capacity) {
428  rb_bug("rb_malloc_grow_capa: current_capacity=%zu, new_capacity=%zu, malloc_offset=%zu", current, new_capacity, malloc_offset);
429  }
430  RUBY_ASSERT(new_capacity > current);
431  return new_capacity;
432 }
433 
434 static inline struct rbimpl_size_mul_overflow_tag
435 size_add_overflow(size_t x, size_t y)
436 {
437  size_t z;
438  bool p;
439 #if 0
440 
441 #elif defined(ckd_add)
442  p = ckd_add(&z, x, y);
443 
444 #elif __has_builtin(__builtin_add_overflow)
445  p = __builtin_add_overflow(x, y, &z);
446 
447 #elif defined(DSIZE_T)
448  RB_GNUC_EXTENSION DSIZE_T dx = x;
449  RB_GNUC_EXTENSION DSIZE_T dy = y;
450  RB_GNUC_EXTENSION DSIZE_T dz = dx + dy;
451  p = dz > SIZE_MAX;
452  z = (size_t)dz;
453 
454 #else
455  z = x + y;
456  p = z < y;
457 
458 #endif
459  return (struct rbimpl_size_mul_overflow_tag) { p, z, };
460 }
461 
462 static inline struct rbimpl_size_mul_overflow_tag
463 size_mul_add_overflow(size_t x, size_t y, size_t z) /* x * y + z */
464 {
465  struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
466  struct rbimpl_size_mul_overflow_tag u = size_add_overflow(t.right, z);
467  return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
468 }
469 
470 static inline struct rbimpl_size_mul_overflow_tag
471 size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
472 {
473  struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
474  struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
475  struct rbimpl_size_mul_overflow_tag v = size_add_overflow(t.right, u.right);
476  return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
477 }
478 
479 PRINTF_ARGS(NORETURN(static void gc_raise(VALUE, const char*, ...)), 2, 3);
480 
481 static inline size_t
482 size_mul_or_raise(size_t x, size_t y, VALUE exc)
483 {
484  struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
485  if (LIKELY(!t.left)) {
486  return t.right;
487  }
488  else if (rb_during_gc()) {
489  rb_memerror(); /* or...? */
490  }
491  else {
492  gc_raise(
493  exc,
494  "integer overflow: %"PRIuSIZE
495  " * %"PRIuSIZE
496  " > %"PRIuSIZE,
497  x, y, (size_t)SIZE_MAX);
498  }
499 }
500 
501 size_t
502 rb_size_mul_or_raise(size_t x, size_t y, VALUE exc)
503 {
504  return size_mul_or_raise(x, y, exc);
505 }
506 
507 static inline size_t
508 size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
509 {
510  struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
511  if (LIKELY(!t.left)) {
512  return t.right;
513  }
514  else if (rb_during_gc()) {
515  rb_memerror(); /* or...? */
516  }
517  else {
518  gc_raise(
519  exc,
520  "integer overflow: %"PRIuSIZE
521  " * %"PRIuSIZE
522  " + %"PRIuSIZE
523  " > %"PRIuSIZE,
524  x, y, z, (size_t)SIZE_MAX);
525  }
526 }
527 
528 size_t
529 rb_size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
530 {
531  return size_mul_add_or_raise(x, y, z, exc);
532 }
533 
534 static inline size_t
535 size_mul_add_mul_or_raise(size_t x, size_t y, size_t z, size_t w, VALUE exc)
536 {
537  struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
538  if (LIKELY(!t.left)) {
539  return t.right;
540  }
541  else if (rb_during_gc()) {
542  rb_memerror(); /* or...? */
543  }
544  else {
545  gc_raise(
546  exc,
547  "integer overflow: %"PRIdSIZE
548  " * %"PRIdSIZE
549  " + %"PRIdSIZE
550  " * %"PRIdSIZE
551  " > %"PRIdSIZE,
552  x, y, z, w, (size_t)SIZE_MAX);
553  }
554 }
555 
556 #if defined(HAVE_RB_GC_GUARDED_PTR_VAL) && HAVE_RB_GC_GUARDED_PTR_VAL
557 /* trick the compiler into thinking a external signal handler uses this */
558 volatile VALUE rb_gc_guarded_val;
559 volatile VALUE *
560 rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val)
561 {
562  rb_gc_guarded_val = val;
563 
564  return ptr;
565 }
566 #endif
567 
568 static const char *obj_type_name(VALUE obj);
569 #define RB_AMALGAMATED_DEFAULT_GC
570 #include "gc/default.c"
571 static int external_gc_loaded = FALSE;
572 
573 
574 #if USE_SHARED_GC && !defined(HAVE_DLOPEN)
575 # error "Shared GC requires dlopen"
576 #elif USE_SHARED_GC
577 #include <dlfcn.h>
578 
579 typedef struct gc_function_map {
580  // Bootup
581  void *(*objspace_alloc)(void);
582  void (*objspace_init)(void *objspace_ptr);
583  void (*objspace_free)(void *objspace_ptr);
584  void *(*ractor_cache_alloc)(void *objspace_ptr);
585  void (*ractor_cache_free)(void *objspace_ptr, void *cache);
586  void (*set_params)(void *objspace_ptr);
587  void (*init)(void);
588  size_t *(*heap_sizes)(void *objspace_ptr);
589  // Shutdown
590  void (*shutdown_free_objects)(void *objspace_ptr);
591  // GC
592  void (*start)(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
593  bool (*during_gc_p)(void *objspace_ptr);
594  void (*prepare_heap)(void *objspace_ptr);
595  void (*gc_enable)(void *objspace_ptr);
596  void (*gc_disable)(void *objspace_ptr, bool finish_current_gc);
597  bool (*gc_enabled_p)(void *objspace_ptr);
598  VALUE (*config_get)(void *objpace_ptr);
599  void (*config_set)(void *objspace_ptr, VALUE hash);
600  void (*stress_set)(void *objspace_ptr, VALUE flag);
601  VALUE (*stress_get)(void *objspace_ptr);
602  // Object allocation
603  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);
604  size_t (*obj_slot_size)(VALUE obj);
605  size_t (*heap_id_for_size)(void *objspace_ptr, size_t size);
606  bool (*size_allocatable_p)(size_t size);
607  // Malloc
608  void *(*malloc)(void *objspace_ptr, size_t size);
609  void *(*calloc)(void *objspace_ptr, size_t size);
610  void *(*realloc)(void *objspace_ptr, void *ptr, size_t new_size, size_t old_size);
611  void (*free)(void *objspace_ptr, void *ptr, size_t old_size);
612  void (*adjust_memory_usage)(void *objspace_ptr, ssize_t diff);
613  // Marking
614  void (*mark)(void *objspace_ptr, VALUE obj);
615  void (*mark_and_move)(void *objspace_ptr, VALUE *ptr);
616  void (*mark_and_pin)(void *objspace_ptr, VALUE obj);
617  void (*mark_maybe)(void *objspace_ptr, VALUE obj);
618  void (*mark_weak)(void *objspace_ptr, VALUE *ptr);
619  void (*remove_weak)(void *objspace_ptr, VALUE parent_obj, VALUE *ptr);
620  // Compaction
621  bool (*object_moved_p)(void *objspace_ptr, VALUE obj);
622  VALUE (*location)(void *objspace_ptr, VALUE value);
623  // Write barriers
624  void (*writebarrier)(void *objspace_ptr, VALUE a, VALUE b);
625  void (*writebarrier_unprotect)(void *objspace_ptr, VALUE obj);
626  void (*writebarrier_remember)(void *objspace_ptr, VALUE obj);
627  // Heap walking
628  void (*each_objects)(void *objspace_ptr, int (*callback)(void *, void *, size_t, void *), void *data);
629  void (*each_object)(void *objspace_ptr, void (*func)(VALUE obj, void *data), void *data);
630  // Finalizers
631  void (*make_zombie)(void *objspace_ptr, VALUE obj, void (*dfree)(void *), void *data);
632  VALUE (*define_finalizer)(void *objspace_ptr, VALUE obj, VALUE block);
633  void (*undefine_finalizer)(void *objspace_ptr, VALUE obj);
634  void (*copy_finalizer)(void *objspace_ptr, VALUE dest, VALUE obj);
635  void (*shutdown_call_finalizer)(void *objspace_ptr);
636  // Object ID
637  VALUE (*object_id)(void *objspace_ptr, VALUE obj);
638  VALUE (*object_id_to_ref)(void *objspace_ptr, VALUE object_id);
639  // Statistics
640  void (*set_measure_total_time)(void *objspace_ptr, VALUE flag);
641  bool (*get_measure_total_time)(void *objspace_ptr);
642  unsigned long long (*get_total_time)(void *objspace_ptr);
643  size_t (*gc_count)(void *objspace_ptr);
644  VALUE (*latest_gc_info)(void *objspace_ptr, VALUE key);
645  VALUE (*stat)(void *objspace_ptr, VALUE hash_or_sym);
646  VALUE (*stat_heap)(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym);
647  // Miscellaneous
648  size_t (*obj_flags)(void *objspace_ptr, VALUE obj, ID* flags, size_t max);
649  bool (*pointer_to_heap_p)(void *objspace_ptr, const void *ptr);
650  bool (*garbage_object_p)(void *objspace_ptr, VALUE obj);
651  void (*set_event_hook)(void *objspace_ptr, const rb_event_flag_t event);
652  void (*copy_attributes)(void *objspace_ptr, VALUE dest, VALUE obj);
653  // GC Identification
654  const char *(*active_gc_name)(void);
655 } rb_gc_function_map_t;
656 
657 static rb_gc_function_map_t rb_gc_functions;
658 
659 # define RUBY_GC_LIBRARY "RUBY_GC_LIBRARY"
660 
661 static void
662 ruby_external_gc_init(void)
663 {
664  // Assert that the directory path ends with a /
665  RUBY_ASSERT_ALWAYS(SHARED_GC_DIR[sizeof(SHARED_GC_DIR) - 2] == '/');
666 
667  char *gc_so_file = getenv(RUBY_GC_LIBRARY);
668 
669  char *gc_so_path = NULL;
670  void *handle = NULL;
671  if (gc_so_file) {
672  /* Check to make sure that gc_so_file matches /[\w-_]+/ so that it does
673  * not load a shared object outside of the directory. */
674  for (size_t i = 0; i < strlen(gc_so_file); i++) {
675  char c = gc_so_file[i];
676  if (isalnum(c)) continue;
677  switch (c) {
678  case '-':
679  case '_':
680  break;
681  default:
682  fprintf(stderr, "Only alphanumeric, dash, and underscore is allowed in "RUBY_GC_LIBRARY"\n");
683  exit(1);
684  }
685  }
686 
687  size_t gc_so_path_size = strlen(SHARED_GC_DIR "librubygc." SOEXT) + strlen(gc_so_file) + 1;
688  gc_so_path = alloca(gc_so_path_size);
689  {
690  size_t gc_so_path_idx = 0;
691 #define GC_SO_PATH_APPEND(str) do { \
692  gc_so_path_idx += strlcpy(gc_so_path + gc_so_path_idx, str, gc_so_path_size - gc_so_path_idx); \
693 } while (0)
694  GC_SO_PATH_APPEND(SHARED_GC_DIR);
695  GC_SO_PATH_APPEND("librubygc.");
696  GC_SO_PATH_APPEND(gc_so_file);
697  GC_SO_PATH_APPEND(SOEXT);
698  GC_ASSERT(gc_so_path_idx == gc_so_path_size - 1);
699 #undef GC_SO_PATH_APPEND
700  }
701 
702  handle = dlopen(gc_so_path, RTLD_LAZY | RTLD_GLOBAL);
703  if (!handle) {
704  fprintf(stderr, "ruby_external_gc_init: Shared library %s cannot be opened: %s\n", gc_so_path, dlerror());
705  exit(1);
706  }
707  external_gc_loaded = TRUE;
708  }
709 
710  rb_gc_function_map_t gc_functions;
711 
712 # define load_external_gc_func(name) do { \
713  if (handle) { \
714  const char *func_name = "rb_gc_impl_" #name; \
715  gc_functions.name = dlsym(handle, func_name); \
716  if (!gc_functions.name) { \
717  fprintf(stderr, "ruby_external_gc_init: %s function not exported by library %s\n", func_name, gc_so_path); \
718  exit(1); \
719  } \
720  } \
721  else { \
722  gc_functions.name = rb_gc_impl_##name; \
723  } \
724 } while (0)
725 
726  // Bootup
727  load_external_gc_func(objspace_alloc);
728  load_external_gc_func(objspace_init);
729  load_external_gc_func(objspace_free);
730  load_external_gc_func(ractor_cache_alloc);
731  load_external_gc_func(ractor_cache_free);
732  load_external_gc_func(set_params);
733  load_external_gc_func(init);
734  load_external_gc_func(heap_sizes);
735  // Shutdown
736  load_external_gc_func(shutdown_free_objects);
737  // GC
738  load_external_gc_func(start);
739  load_external_gc_func(during_gc_p);
740  load_external_gc_func(prepare_heap);
741  load_external_gc_func(gc_enable);
742  load_external_gc_func(gc_disable);
743  load_external_gc_func(gc_enabled_p);
744  load_external_gc_func(config_set);
745  load_external_gc_func(config_get);
746  load_external_gc_func(stress_set);
747  load_external_gc_func(stress_get);
748  // Object allocation
749  load_external_gc_func(new_obj);
750  load_external_gc_func(obj_slot_size);
751  load_external_gc_func(heap_id_for_size);
752  load_external_gc_func(size_allocatable_p);
753  // Malloc
754  load_external_gc_func(malloc);
755  load_external_gc_func(calloc);
756  load_external_gc_func(realloc);
757  load_external_gc_func(free);
758  load_external_gc_func(adjust_memory_usage);
759  // Marking
760  load_external_gc_func(mark);
761  load_external_gc_func(mark_and_move);
762  load_external_gc_func(mark_and_pin);
763  load_external_gc_func(mark_maybe);
764  load_external_gc_func(mark_weak);
765  load_external_gc_func(remove_weak);
766  // Compaction
767  load_external_gc_func(object_moved_p);
768  load_external_gc_func(location);
769  // Write barriers
770  load_external_gc_func(writebarrier);
771  load_external_gc_func(writebarrier_unprotect);
772  load_external_gc_func(writebarrier_remember);
773  // Heap walking
774  load_external_gc_func(each_objects);
775  load_external_gc_func(each_object);
776  // Finalizers
777  load_external_gc_func(make_zombie);
778  load_external_gc_func(define_finalizer);
779  load_external_gc_func(undefine_finalizer);
780  load_external_gc_func(copy_finalizer);
781  load_external_gc_func(shutdown_call_finalizer);
782  // Object ID
783  load_external_gc_func(object_id);
784  load_external_gc_func(object_id_to_ref);
785  // Statistics
786  load_external_gc_func(set_measure_total_time);
787  load_external_gc_func(get_measure_total_time);
788  load_external_gc_func(get_total_time);
789  load_external_gc_func(gc_count);
790  load_external_gc_func(latest_gc_info);
791  load_external_gc_func(stat);
792  load_external_gc_func(stat_heap);
793  // Miscellaneous
794  load_external_gc_func(obj_flags);
795  load_external_gc_func(pointer_to_heap_p);
796  load_external_gc_func(garbage_object_p);
797  load_external_gc_func(set_event_hook);
798  load_external_gc_func(copy_attributes);
799  //GC Identification
800  load_external_gc_func(active_gc_name);
801 
802 # undef load_external_gc_func
803 
804  rb_gc_functions = gc_functions;
805 }
806 
807 // Bootup
808 # define rb_gc_impl_objspace_alloc rb_gc_functions.objspace_alloc
809 # define rb_gc_impl_objspace_init rb_gc_functions.objspace_init
810 # define rb_gc_impl_objspace_free rb_gc_functions.objspace_free
811 # define rb_gc_impl_ractor_cache_alloc rb_gc_functions.ractor_cache_alloc
812 # define rb_gc_impl_ractor_cache_free rb_gc_functions.ractor_cache_free
813 # define rb_gc_impl_set_params rb_gc_functions.set_params
814 # define rb_gc_impl_init rb_gc_functions.init
815 # define rb_gc_impl_heap_sizes rb_gc_functions.heap_sizes
816 // Shutdown
817 # define rb_gc_impl_shutdown_free_objects rb_gc_functions.shutdown_free_objects
818 // GC
819 # define rb_gc_impl_start rb_gc_functions.start
820 # define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
821 # define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
822 # define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
823 # define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
824 # define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
825 # define rb_gc_impl_config_get rb_gc_functions.config_get
826 # define rb_gc_impl_config_set rb_gc_functions.config_set
827 # define rb_gc_impl_stress_set rb_gc_functions.stress_set
828 # define rb_gc_impl_stress_get rb_gc_functions.stress_get
829 // Object allocation
830 # define rb_gc_impl_new_obj rb_gc_functions.new_obj
831 # define rb_gc_impl_obj_slot_size rb_gc_functions.obj_slot_size
832 # define rb_gc_impl_heap_id_for_size rb_gc_functions.heap_id_for_size
833 # define rb_gc_impl_size_allocatable_p rb_gc_functions.size_allocatable_p
834 // Malloc
835 # define rb_gc_impl_malloc rb_gc_functions.malloc
836 # define rb_gc_impl_calloc rb_gc_functions.calloc
837 # define rb_gc_impl_realloc rb_gc_functions.realloc
838 # define rb_gc_impl_free rb_gc_functions.free
839 # define rb_gc_impl_adjust_memory_usage rb_gc_functions.adjust_memory_usage
840 // Marking
841 # define rb_gc_impl_mark rb_gc_functions.mark
842 # define rb_gc_impl_mark_and_move rb_gc_functions.mark_and_move
843 # define rb_gc_impl_mark_and_pin rb_gc_functions.mark_and_pin
844 # define rb_gc_impl_mark_maybe rb_gc_functions.mark_maybe
845 # define rb_gc_impl_mark_weak rb_gc_functions.mark_weak
846 # define rb_gc_impl_remove_weak rb_gc_functions.remove_weak
847 // Compaction
848 # define rb_gc_impl_object_moved_p rb_gc_functions.object_moved_p
849 # define rb_gc_impl_location rb_gc_functions.location
850 // Write barriers
851 # define rb_gc_impl_writebarrier rb_gc_functions.writebarrier
852 # define rb_gc_impl_writebarrier_unprotect rb_gc_functions.writebarrier_unprotect
853 # define rb_gc_impl_writebarrier_remember rb_gc_functions.writebarrier_remember
854 // Heap walking
855 # define rb_gc_impl_each_objects rb_gc_functions.each_objects
856 # define rb_gc_impl_each_object rb_gc_functions.each_object
857 // Finalizers
858 # define rb_gc_impl_make_zombie rb_gc_functions.make_zombie
859 # define rb_gc_impl_define_finalizer rb_gc_functions.define_finalizer
860 # define rb_gc_impl_undefine_finalizer rb_gc_functions.undefine_finalizer
861 # define rb_gc_impl_copy_finalizer rb_gc_functions.copy_finalizer
862 # define rb_gc_impl_shutdown_call_finalizer rb_gc_functions.shutdown_call_finalizer
863 // Object ID
864 # define rb_gc_impl_object_id rb_gc_functions.object_id
865 # define rb_gc_impl_object_id_to_ref rb_gc_functions.object_id_to_ref
866 // Statistics
867 # define rb_gc_impl_set_measure_total_time rb_gc_functions.set_measure_total_time
868 # define rb_gc_impl_get_measure_total_time rb_gc_functions.get_measure_total_time
869 # define rb_gc_impl_get_total_time rb_gc_functions.get_total_time
870 # define rb_gc_impl_gc_count rb_gc_functions.gc_count
871 # define rb_gc_impl_latest_gc_info rb_gc_functions.latest_gc_info
872 # define rb_gc_impl_stat rb_gc_functions.stat
873 # define rb_gc_impl_stat_heap rb_gc_functions.stat_heap
874 // Miscellaneous
875 # define rb_gc_impl_obj_flags rb_gc_functions.obj_flags
876 # define rb_gc_impl_pointer_to_heap_p rb_gc_functions.pointer_to_heap_p
877 # define rb_gc_impl_garbage_object_p rb_gc_functions.garbage_object_p
878 # define rb_gc_impl_set_event_hook rb_gc_functions.set_event_hook
879 # define rb_gc_impl_copy_attributes rb_gc_functions.copy_attributes
880 // GC Identification
881 # define rb_gc_impl_active_gc_name rb_gc_functions.active_gc_name
882 #endif
883 
884 static VALUE initial_stress = Qfalse;
885 
886 void *
887 rb_objspace_alloc(void)
888 {
889 #if USE_SHARED_GC
890  ruby_external_gc_init();
891 #endif
892 
893  void *objspace = rb_gc_impl_objspace_alloc();
894  ruby_current_vm_ptr->gc.objspace = objspace;
895 
896  rb_gc_impl_objspace_init(objspace);
897  rb_gc_impl_stress_set(objspace, initial_stress);
898 
899  return objspace;
900 }
901 
902 void
903 rb_objspace_free(void *objspace)
904 {
905  rb_gc_impl_objspace_free(objspace);
906 }
907 
908 size_t
909 rb_gc_obj_slot_size(VALUE obj)
910 {
911  return rb_gc_impl_obj_slot_size(obj);
912 }
913 
914 static inline VALUE
915 newobj_of(rb_ractor_t *cr, VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3, bool wb_protected, size_t size)
916 {
917  VALUE obj = rb_gc_impl_new_obj(rb_gc_get_objspace(), cr->newobj_cache, klass, flags, v1, v2, v3, wb_protected, size);
918 
919  if (UNLIKELY(ruby_vm_event_flags & RUBY_INTERNAL_EVENT_NEWOBJ)) {
920  unsigned int lev;
921  RB_VM_LOCK_ENTER_CR_LEV(cr, &lev);
922  {
923  memset((char *)obj + RVALUE_SIZE, 0, rb_gc_obj_slot_size(obj) - RVALUE_SIZE);
924 
925  rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_NEWOBJ);
926  }
927  RB_VM_LOCK_LEAVE_CR_LEV(cr, &lev);
928  }
929 
930  return obj;
931 }
932 
933 VALUE
934 rb_wb_unprotected_newobj_of(VALUE klass, VALUE flags, size_t size)
935 {
936  GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
937  return newobj_of(GET_RACTOR(), klass, flags, 0, 0, 0, FALSE, size);
938 }
939 
940 VALUE
941 rb_wb_protected_newobj_of(rb_execution_context_t *ec, VALUE klass, VALUE flags, size_t size)
942 {
943  GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
944  return newobj_of(rb_ec_ractor_ptr(ec), klass, flags, 0, 0, 0, TRUE, size);
945 }
946 
947 #define UNEXPECTED_NODE(func) \
948  rb_bug(#func"(): GC does not handle T_NODE 0x%x(%p) 0x%"PRIxVALUE, \
949  BUILTIN_TYPE(obj), (void*)(obj), RBASIC(obj)->flags)
950 
951 static inline void
952 rb_data_object_check(VALUE klass)
953 {
954  if (klass != rb_cObject && (rb_get_alloc_func(klass) == rb_class_allocate_instance)) {
955  rb_undef_alloc_func(klass);
956  rb_warn("undefining the allocator of T_DATA class %"PRIsVALUE, klass);
957  }
958 }
959 
960 VALUE
961 rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
962 {
963  RUBY_ASSERT_ALWAYS(dfree != (RUBY_DATA_FUNC)1);
964  if (klass) rb_data_object_check(klass);
965  return newobj_of(GET_RACTOR(), klass, T_DATA, (VALUE)dmark, (VALUE)dfree, (VALUE)datap, !dmark, sizeof(struct RTypedData));
966 }
967 
968 VALUE
969 rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
970 {
971  VALUE obj = rb_data_object_wrap(klass, 0, dmark, dfree);
972  DATA_PTR(obj) = xcalloc(1, size);
973  return obj;
974 }
975 
976 static VALUE
977 typed_data_alloc(VALUE klass, VALUE typed_flag, void *datap, const rb_data_type_t *type, size_t size)
978 {
979  RBIMPL_NONNULL_ARG(type);
980  if (klass) rb_data_object_check(klass);
981  bool wb_protected = (type->flags & RUBY_FL_WB_PROTECTED) || !type->function.dmark;
982  return newobj_of(GET_RACTOR(), klass, T_DATA, (VALUE)type, 1 | typed_flag, (VALUE)datap, wb_protected, size);
983 }
984 
985 VALUE
987 {
988  if (UNLIKELY(type->flags & RUBY_TYPED_EMBEDDABLE)) {
989  rb_raise(rb_eTypeError, "Cannot wrap an embeddable TypedData");
990  }
991 
992  return typed_data_alloc(klass, 0, datap, type, sizeof(struct RTypedData));
993 }
994 
995 VALUE
997 {
998  if (type->flags & RUBY_TYPED_EMBEDDABLE) {
999  if (!(type->flags & RUBY_TYPED_FREE_IMMEDIATELY)) {
1000  rb_raise(rb_eTypeError, "Embeddable TypedData must be freed immediately");
1001  }
1002 
1003  size_t embed_size = offsetof(struct RTypedData, data) + size;
1004  if (rb_gc_size_allocatable_p(embed_size)) {
1005  VALUE obj = typed_data_alloc(klass, TYPED_DATA_EMBEDDED, 0, type, embed_size);
1006  memset((char *)obj + offsetof(struct RTypedData, data), 0, size);
1007  return obj;
1008  }
1009  }
1010 
1011  VALUE obj = typed_data_alloc(klass, 0, NULL, type, sizeof(struct RTypedData));
1012  DATA_PTR(obj) = xcalloc(1, size);
1013  return obj;
1014 }
1015 
1016 static size_t
1017 rb_objspace_data_type_memsize(VALUE obj)
1018 {
1019  size_t size = 0;
1020  if (RTYPEDDATA_P(obj)) {
1021  const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
1022  const void *ptr = RTYPEDDATA_GET_DATA(obj);
1023 
1024  if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1025 #ifdef HAVE_MALLOC_USABLE_SIZE
1026  size += malloc_usable_size((void *)ptr);
1027 #endif
1028  }
1029 
1030  if (ptr && type->function.dsize) {
1031  size += type->function.dsize(ptr);
1032  }
1033  }
1034 
1035  return size;
1036 }
1037 
1038 const char *
1039 rb_objspace_data_type_name(VALUE obj)
1040 {
1041  if (RTYPEDDATA_P(obj)) {
1042  return RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1043  }
1044  else {
1045  return 0;
1046  }
1047 }
1048 
1049 static enum rb_id_table_iterator_result
1050 cvar_table_free_i(VALUE value, void *ctx)
1051 {
1052  xfree((void *)value);
1053  return ID_TABLE_CONTINUE;
1054 }
1055 
1056 static inline void
1057 make_io_zombie(void *objspace, VALUE obj)
1058 {
1059  rb_io_t *fptr = RFILE(obj)->fptr;
1060  rb_gc_impl_make_zombie(objspace, obj, rb_io_fptr_finalize_internal, fptr);
1061 }
1062 
1063 static bool
1064 rb_data_free(void *objspace, VALUE obj)
1065 {
1066  void *data = RTYPEDDATA_P(obj) ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
1067  if (data) {
1068  int free_immediately = false;
1069  void (*dfree)(void *);
1070 
1071  if (RTYPEDDATA_P(obj)) {
1072  free_immediately = (RTYPEDDATA(obj)->type->flags & RUBY_TYPED_FREE_IMMEDIATELY) != 0;
1073  dfree = RTYPEDDATA(obj)->type->function.dfree;
1074  }
1075  else {
1076  dfree = RDATA(obj)->dfree;
1077  }
1078 
1079  if (dfree) {
1080  if (dfree == RUBY_DEFAULT_FREE) {
1081  if (!RTYPEDDATA_P(obj) || !RTYPEDDATA_EMBEDDED_P(obj)) {
1082  xfree(data);
1083  RB_DEBUG_COUNTER_INC(obj_data_xfree);
1084  }
1085  }
1086  else if (free_immediately) {
1087  (*dfree)(data);
1088  if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_EMBEDDABLE && !RTYPEDDATA_EMBEDDED_P(obj)) {
1089  xfree(data);
1090  }
1091 
1092  RB_DEBUG_COUNTER_INC(obj_data_imm_free);
1093  }
1094  else {
1095  rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, dfree, data);
1096  RB_DEBUG_COUNTER_INC(obj_data_zombie);
1097  return FALSE;
1098  }
1099  }
1100  else {
1101  RB_DEBUG_COUNTER_INC(obj_data_empty);
1102  }
1103  }
1104 
1105  return true;
1106 }
1107 
1108 bool
1109 rb_gc_obj_free(void *objspace, VALUE obj)
1110 {
1111  RB_DEBUG_COUNTER_INC(obj_free);
1112 
1113  switch (BUILTIN_TYPE(obj)) {
1114  case T_NIL:
1115  case T_FIXNUM:
1116  case T_TRUE:
1117  case T_FALSE:
1118  rb_bug("obj_free() called for broken object");
1119  break;
1120  default:
1121  break;
1122  }
1123 
1124  if (FL_TEST(obj, FL_EXIVAR)) {
1126  FL_UNSET(obj, FL_EXIVAR);
1127  }
1128 
1129  switch (BUILTIN_TYPE(obj)) {
1130  case T_OBJECT:
1131  if (rb_shape_obj_too_complex(obj)) {
1132  RB_DEBUG_COUNTER_INC(obj_obj_too_complex);
1133  st_free_table(ROBJECT_IV_HASH(obj));
1134  }
1135  else if (RBASIC(obj)->flags & ROBJECT_EMBED) {
1136  RB_DEBUG_COUNTER_INC(obj_obj_embed);
1137  }
1138  else {
1139  xfree(ROBJECT(obj)->as.heap.ivptr);
1140  RB_DEBUG_COUNTER_INC(obj_obj_ptr);
1141  }
1142  break;
1143  case T_MODULE:
1144  case T_CLASS:
1145  rb_id_table_free(RCLASS_M_TBL(obj));
1146  rb_cc_table_free(obj);
1147  if (rb_shape_obj_too_complex(obj)) {
1148  st_free_table((st_table *)RCLASS_IVPTR(obj));
1149  }
1150  else {
1151  xfree(RCLASS_IVPTR(obj));
1152  }
1153 
1154  if (RCLASS_CONST_TBL(obj)) {
1155  rb_free_const_table(RCLASS_CONST_TBL(obj));
1156  }
1157  if (RCLASS_CVC_TBL(obj)) {
1158  rb_id_table_foreach_values(RCLASS_CVC_TBL(obj), cvar_table_free_i, NULL);
1159  rb_id_table_free(RCLASS_CVC_TBL(obj));
1160  }
1161  rb_class_remove_subclass_head(obj);
1162  rb_class_remove_from_module_subclasses(obj);
1163  rb_class_remove_from_super_subclasses(obj);
1164  if (FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1165  xfree(RCLASS_SUPERCLASSES(obj));
1166  }
1167 
1168  (void)RB_DEBUG_COUNTER_INC_IF(obj_module_ptr, BUILTIN_TYPE(obj) == T_MODULE);
1169  (void)RB_DEBUG_COUNTER_INC_IF(obj_class_ptr, BUILTIN_TYPE(obj) == T_CLASS);
1170  break;
1171  case T_STRING:
1172  rb_str_free(obj);
1173  break;
1174  case T_ARRAY:
1175  rb_ary_free(obj);
1176  break;
1177  case T_HASH:
1178 #if USE_DEBUG_COUNTER
1179  switch (RHASH_SIZE(obj)) {
1180  case 0:
1181  RB_DEBUG_COUNTER_INC(obj_hash_empty);
1182  break;
1183  case 1:
1184  RB_DEBUG_COUNTER_INC(obj_hash_1);
1185  break;
1186  case 2:
1187  RB_DEBUG_COUNTER_INC(obj_hash_2);
1188  break;
1189  case 3:
1190  RB_DEBUG_COUNTER_INC(obj_hash_3);
1191  break;
1192  case 4:
1193  RB_DEBUG_COUNTER_INC(obj_hash_4);
1194  break;
1195  case 5:
1196  case 6:
1197  case 7:
1198  case 8:
1199  RB_DEBUG_COUNTER_INC(obj_hash_5_8);
1200  break;
1201  default:
1202  GC_ASSERT(RHASH_SIZE(obj) > 8);
1203  RB_DEBUG_COUNTER_INC(obj_hash_g8);
1204  }
1205 
1206  if (RHASH_AR_TABLE_P(obj)) {
1207  if (RHASH_AR_TABLE(obj) == NULL) {
1208  RB_DEBUG_COUNTER_INC(obj_hash_null);
1209  }
1210  else {
1211  RB_DEBUG_COUNTER_INC(obj_hash_ar);
1212  }
1213  }
1214  else {
1215  RB_DEBUG_COUNTER_INC(obj_hash_st);
1216  }
1217 #endif
1218 
1219  rb_hash_free(obj);
1220  break;
1221  case T_REGEXP:
1222  if (RREGEXP(obj)->ptr) {
1223  onig_free(RREGEXP(obj)->ptr);
1224  RB_DEBUG_COUNTER_INC(obj_regexp_ptr);
1225  }
1226  break;
1227  case T_DATA:
1228  if (!rb_data_free(objspace, obj)) return false;
1229  break;
1230  case T_MATCH:
1231  {
1232  rb_matchext_t *rm = RMATCH_EXT(obj);
1233 #if USE_DEBUG_COUNTER
1234  if (rm->regs.num_regs >= 8) {
1235  RB_DEBUG_COUNTER_INC(obj_match_ge8);
1236  }
1237  else if (rm->regs.num_regs >= 4) {
1238  RB_DEBUG_COUNTER_INC(obj_match_ge4);
1239  }
1240  else if (rm->regs.num_regs >= 1) {
1241  RB_DEBUG_COUNTER_INC(obj_match_under4);
1242  }
1243 #endif
1244  onig_region_free(&rm->regs, 0);
1245  xfree(rm->char_offset);
1246 
1247  RB_DEBUG_COUNTER_INC(obj_match_ptr);
1248  }
1249  break;
1250  case T_FILE:
1251  if (RFILE(obj)->fptr) {
1252  make_io_zombie(objspace, obj);
1253  RB_DEBUG_COUNTER_INC(obj_file_ptr);
1254  return FALSE;
1255  }
1256  break;
1257  case T_RATIONAL:
1258  RB_DEBUG_COUNTER_INC(obj_rational);
1259  break;
1260  case T_COMPLEX:
1261  RB_DEBUG_COUNTER_INC(obj_complex);
1262  break;
1263  case T_MOVED:
1264  break;
1265  case T_ICLASS:
1266  /* Basically , T_ICLASS shares table with the module */
1267  if (RICLASS_OWNS_M_TBL_P(obj)) {
1268  /* Method table is not shared for origin iclasses of classes */
1269  rb_id_table_free(RCLASS_M_TBL(obj));
1270  }
1271  if (RCLASS_CALLABLE_M_TBL(obj) != NULL) {
1272  rb_id_table_free(RCLASS_CALLABLE_M_TBL(obj));
1273  }
1274  rb_class_remove_subclass_head(obj);
1275  rb_cc_table_free(obj);
1276  rb_class_remove_from_module_subclasses(obj);
1277  rb_class_remove_from_super_subclasses(obj);
1278 
1279  RB_DEBUG_COUNTER_INC(obj_iclass_ptr);
1280  break;
1281 
1282  case T_FLOAT:
1283  RB_DEBUG_COUNTER_INC(obj_float);
1284  break;
1285 
1286  case T_BIGNUM:
1287  if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
1288  xfree(BIGNUM_DIGITS(obj));
1289  RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
1290  }
1291  else {
1292  RB_DEBUG_COUNTER_INC(obj_bignum_embed);
1293  }
1294  break;
1295 
1296  case T_NODE:
1297  UNEXPECTED_NODE(obj_free);
1298  break;
1299 
1300  case T_STRUCT:
1301  if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
1302  RSTRUCT(obj)->as.heap.ptr == NULL) {
1303  RB_DEBUG_COUNTER_INC(obj_struct_embed);
1304  }
1305  else {
1306  xfree((void *)RSTRUCT(obj)->as.heap.ptr);
1307  RB_DEBUG_COUNTER_INC(obj_struct_ptr);
1308  }
1309  break;
1310 
1311  case T_SYMBOL:
1312  {
1313  rb_gc_free_dsymbol(obj);
1314  RB_DEBUG_COUNTER_INC(obj_symbol);
1315  }
1316  break;
1317 
1318  case T_IMEMO:
1319  rb_imemo_free((VALUE)obj);
1320  break;
1321 
1322  default:
1323  rb_bug("gc_sweep(): unknown data type 0x%x(%p) 0x%"PRIxVALUE,
1324  BUILTIN_TYPE(obj), (void*)obj, RBASIC(obj)->flags);
1325  }
1326 
1327  if (FL_TEST(obj, FL_FINALIZE)) {
1328  rb_gc_impl_make_zombie(rb_gc_get_objspace(), obj, 0, 0);
1329  return FALSE;
1330  }
1331  else {
1332  return TRUE;
1333  }
1334 }
1335 
1336 void
1337 rb_objspace_set_event_hook(const rb_event_flag_t event)
1338 {
1339  rb_gc_impl_set_event_hook(rb_gc_get_objspace(), event);
1340 }
1341 
1342 static int
1343 internal_object_p(VALUE obj)
1344 {
1345  void *ptr = asan_unpoison_object_temporary(obj);
1346 
1347  if (RBASIC(obj)->flags) {
1348  switch (BUILTIN_TYPE(obj)) {
1349  case T_NODE:
1350  UNEXPECTED_NODE(internal_object_p);
1351  break;
1352  case T_NONE:
1353  case T_MOVED:
1354  case T_IMEMO:
1355  case T_ICLASS:
1356  case T_ZOMBIE:
1357  break;
1358  case T_CLASS:
1359  if (!RBASIC(obj)->klass) break;
1360  if (RCLASS_SINGLETON_P(obj)) {
1361  return rb_singleton_class_internal_p(obj);
1362  }
1363  return 0;
1364  default:
1365  if (!RBASIC(obj)->klass) break;
1366  return 0;
1367  }
1368  }
1369  if (ptr || !RBASIC(obj)->flags) {
1370  asan_poison_object(obj);
1371  }
1372  return 1;
1373 }
1374 
1375 int
1376 rb_objspace_internal_object_p(VALUE obj)
1377 {
1378  return internal_object_p(obj);
1379 }
1380 
1382  size_t num;
1383  VALUE of;
1384 };
1385 
1386 static int
1387 os_obj_of_i(void *vstart, void *vend, size_t stride, void *data)
1388 {
1389  struct os_each_struct *oes = (struct os_each_struct *)data;
1390 
1391  VALUE v = (VALUE)vstart;
1392  for (; v != (VALUE)vend; v += stride) {
1393  if (!internal_object_p(v)) {
1394  if (!oes->of || rb_obj_is_kind_of(v, oes->of)) {
1395  if (!rb_multi_ractor_p() || rb_ractor_shareable_p(v)) {
1396  rb_yield(v);
1397  oes->num++;
1398  }
1399  }
1400  }
1401  }
1402 
1403  return 0;
1404 }
1405 
1406 static VALUE
1407 os_obj_of(VALUE of)
1408 {
1409  struct os_each_struct oes;
1410 
1411  oes.num = 0;
1412  oes.of = of;
1413  rb_objspace_each_objects(os_obj_of_i, &oes);
1414  return SIZET2NUM(oes.num);
1415 }
1416 
1417 /*
1418  * call-seq:
1419  * ObjectSpace.each_object([module]) {|obj| ... } -> integer
1420  * ObjectSpace.each_object([module]) -> an_enumerator
1421  *
1422  * Calls the block once for each living, nonimmediate object in this
1423  * Ruby process. If <i>module</i> is specified, calls the block
1424  * for only those classes or modules that match (or are a subclass of)
1425  * <i>module</i>. Returns the number of objects found. Immediate
1426  * objects (<code>Fixnum</code>s, <code>Symbol</code>s
1427  * <code>true</code>, <code>false</code>, and <code>nil</code>) are
1428  * never returned. In the example below, #each_object returns both
1429  * the numbers we defined and several constants defined in the Math
1430  * module.
1431  *
1432  * If no block is given, an enumerator is returned instead.
1433  *
1434  * a = 102.7
1435  * b = 95 # Won't be returned
1436  * c = 12345678987654321
1437  * count = ObjectSpace.each_object(Numeric) {|x| p x }
1438  * puts "Total count: #{count}"
1439  *
1440  * <em>produces:</em>
1441  *
1442  * 12345678987654321
1443  * 102.7
1444  * 2.71828182845905
1445  * 3.14159265358979
1446  * 2.22044604925031e-16
1447  * 1.7976931348623157e+308
1448  * 2.2250738585072e-308
1449  * Total count: 7
1450  *
1451  */
1452 
1453 static VALUE
1454 os_each_obj(int argc, VALUE *argv, VALUE os)
1455 {
1456  VALUE of;
1457 
1458  of = (!rb_check_arity(argc, 0, 1) ? 0 : argv[0]);
1459  RETURN_ENUMERATOR(os, 1, &of);
1460  return os_obj_of(of);
1461 }
1462 
1463 /*
1464  * call-seq:
1465  * ObjectSpace.undefine_finalizer(obj)
1466  *
1467  * Removes all finalizers for <i>obj</i>.
1468  *
1469  */
1470 
1471 static VALUE
1472 undefine_final(VALUE os, VALUE obj)
1473 {
1474  rb_check_frozen(obj);
1475 
1476  rb_gc_impl_undefine_finalizer(rb_gc_get_objspace(), obj);
1477 
1478  return obj;
1479 }
1480 
1481 static void
1482 should_be_callable(VALUE block)
1483 {
1484  if (!rb_obj_respond_to(block, idCall, TRUE)) {
1485  rb_raise(rb_eArgError, "wrong type argument %"PRIsVALUE" (should be callable)",
1486  rb_obj_class(block));
1487  }
1488 }
1489 
1490 static void
1491 should_be_finalizable(VALUE obj)
1492 {
1493  if (!FL_ABLE(obj)) {
1494  rb_raise(rb_eArgError, "cannot define finalizer for %s",
1495  rb_obj_classname(obj));
1496  }
1497  rb_check_frozen(obj);
1498 }
1499 
1500 void
1502 {
1503  rb_gc_impl_copy_finalizer(rb_gc_get_objspace(), dest, obj);
1504 }
1505 
1506 /*
1507  * call-seq:
1508  * ObjectSpace.define_finalizer(obj, aProc=proc())
1509  *
1510  * Adds <i>aProc</i> as a finalizer, to be called after <i>obj</i>
1511  * was destroyed. The object ID of the <i>obj</i> will be passed
1512  * as an argument to <i>aProc</i>. If <i>aProc</i> is a lambda or
1513  * method, make sure it can be called with a single argument.
1514  *
1515  * The return value is an array <code>[0, aProc]</code>.
1516  *
1517  * The two recommended patterns are to either create the finaliser proc
1518  * in a non-instance method where it can safely capture the needed state,
1519  * or to use a custom callable object that stores the needed state
1520  * explicitly as instance variables.
1521  *
1522  * class Foo
1523  * def initialize(data_needed_for_finalization)
1524  * ObjectSpace.define_finalizer(self, self.class.create_finalizer(data_needed_for_finalization))
1525  * end
1526  *
1527  * def self.create_finalizer(data_needed_for_finalization)
1528  * proc {
1529  * puts "finalizing #{data_needed_for_finalization}"
1530  * }
1531  * end
1532  * end
1533  *
1534  * class Bar
1535  * class Remover
1536  * def initialize(data_needed_for_finalization)
1537  * @data_needed_for_finalization = data_needed_for_finalization
1538  * end
1539  *
1540  * def call(id)
1541  * puts "finalizing #{@data_needed_for_finalization}"
1542  * end
1543  * end
1544  *
1545  * def initialize(data_needed_for_finalization)
1546  * ObjectSpace.define_finalizer(self, Remover.new(data_needed_for_finalization))
1547  * end
1548  * end
1549  *
1550  * Note that if your finalizer references the object to be
1551  * finalized it will never be run on GC, although it will still be
1552  * run at exit. You will get a warning if you capture the object
1553  * to be finalized as the receiver of the finalizer.
1554  *
1555  * class CapturesSelf
1556  * def initialize(name)
1557  * ObjectSpace.define_finalizer(self, proc {
1558  * # this finalizer will only be run on exit
1559  * puts "finalizing #{name}"
1560  * })
1561  * end
1562  * end
1563  *
1564  * Also note that finalization can be unpredictable and is never guaranteed
1565  * to be run except on exit.
1566  */
1567 
1568 static VALUE
1569 define_final(int argc, VALUE *argv, VALUE os)
1570 {
1571  VALUE obj, block;
1572 
1573  rb_scan_args(argc, argv, "11", &obj, &block);
1574  if (argc == 1) {
1575  block = rb_block_proc();
1576  }
1577 
1578  if (rb_callable_receiver(block) == obj) {
1579  rb_warn("finalizer references object to be finalized");
1580  }
1581 
1582  return rb_define_finalizer(obj, block);
1583 }
1584 
1585 VALUE
1587 {
1588  should_be_finalizable(obj);
1589  should_be_callable(block);
1590 
1591  block = rb_gc_impl_define_finalizer(rb_gc_get_objspace(), obj, block);
1592 
1593  block = rb_ary_new3(2, INT2FIX(0), block);
1594  OBJ_FREEZE(block);
1595  return block;
1596 }
1597 
1598 void
1599 rb_objspace_call_finalizer(void)
1600 {
1601  rb_gc_impl_shutdown_call_finalizer(rb_gc_get_objspace());
1602 }
1603 
1604 void
1605 rb_objspace_free_objects(void *objspace)
1606 {
1607  rb_gc_impl_shutdown_free_objects(objspace);
1608 }
1609 
1610 int
1611 rb_objspace_garbage_object_p(VALUE obj)
1612 {
1613  return rb_gc_impl_garbage_object_p(rb_gc_get_objspace(), obj);
1614 }
1615 
1616 /*
1617  * call-seq:
1618  * ObjectSpace._id2ref(object_id) -> an_object
1619  *
1620  * Converts an object id to a reference to the object. May not be
1621  * called on an object id passed as a parameter to a finalizer.
1622  *
1623  * s = "I am a string" #=> "I am a string"
1624  * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
1625  * r == s #=> true
1626  *
1627  * On multi-ractor mode, if the object is not shareable, it raises
1628  * RangeError.
1629  */
1630 
1631 static VALUE
1632 id2ref(VALUE objid)
1633 {
1634 #if SIZEOF_LONG == SIZEOF_VOIDP
1635 #define NUM2PTR(x) NUM2ULONG(x)
1636 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1637 #define NUM2PTR(x) NUM2ULL(x)
1638 #endif
1639  objid = rb_to_int(objid);
1640  if (FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
1641  VALUE ptr = NUM2PTR(objid);
1642  if (SPECIAL_CONST_P(ptr)) {
1643  if (ptr == Qtrue) return Qtrue;
1644  if (ptr == Qfalse) return Qfalse;
1645  if (NIL_P(ptr)) return Qnil;
1646  if (FIXNUM_P(ptr)) return ptr;
1647  if (FLONUM_P(ptr)) return ptr;
1648 
1649  if (SYMBOL_P(ptr)) {
1650  // Check that the symbol is valid
1651  if (rb_static_id_valid_p(SYM2ID(ptr))) {
1652  return ptr;
1653  }
1654  else {
1655  rb_raise(rb_eRangeError, "%p is not symbol id value", (void *)ptr);
1656  }
1657  }
1658 
1659  rb_raise(rb_eRangeError, "%+"PRIsVALUE" is not id value", rb_int2str(objid, 10));
1660  }
1661  }
1662 
1663  VALUE obj = rb_gc_impl_object_id_to_ref(rb_gc_get_objspace(), objid);
1664  if (!rb_multi_ractor_p() || rb_ractor_shareable_p(obj)) {
1665  return obj;
1666  }
1667  else {
1668  rb_raise(rb_eRangeError, "%+"PRIsVALUE" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10));
1669  }
1670 }
1671 
1672 /* :nodoc: */
1673 static VALUE
1674 os_id2ref(VALUE os, VALUE objid)
1675 {
1676  return id2ref(objid);
1677 }
1678 
1679 static VALUE
1680 rb_find_object_id(void *objspace, VALUE obj, VALUE (*get_heap_object_id)(void *, VALUE))
1681 {
1682  if (SPECIAL_CONST_P(obj)) {
1683 #if SIZEOF_LONG == SIZEOF_VOIDP
1684  return LONG2NUM((SIGNED_VALUE)obj);
1685 #else
1686  return LL2NUM((SIGNED_VALUE)obj);
1687 #endif
1688  }
1689 
1690  return get_heap_object_id(objspace, obj);
1691 }
1692 
1693 static VALUE
1694 nonspecial_obj_id(void *_objspace, VALUE obj)
1695 {
1696 #if SIZEOF_LONG == SIZEOF_VOIDP
1697  return (VALUE)((SIGNED_VALUE)(obj)|FIXNUM_FLAG);
1698 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
1699  return LL2NUM((SIGNED_VALUE)(obj) / 2);
1700 #else
1701 # error not supported
1702 #endif
1703 }
1704 
1705 VALUE
1707 {
1708  return rb_find_object_id(NULL, obj, nonspecial_obj_id);
1709 }
1710 
1711 /*
1712  * Document-method: __id__
1713  * Document-method: object_id
1714  *
1715  * call-seq:
1716  * obj.__id__ -> integer
1717  * obj.object_id -> integer
1718  *
1719  * Returns an integer identifier for +obj+.
1720  *
1721  * The same number will be returned on all calls to +object_id+ for a given
1722  * object, and no two active objects will share an id.
1723  *
1724  * Note: that some objects of builtin classes are reused for optimization.
1725  * This is the case for immediate values and frozen string literals.
1726  *
1727  * BasicObject implements +__id__+, Kernel implements +object_id+.
1728  *
1729  * Immediate values are not passed by reference but are passed by value:
1730  * +nil+, +true+, +false+, Fixnums, Symbols, and some Floats.
1731  *
1732  * Object.new.object_id == Object.new.object_id # => false
1733  * (21 * 2).object_id == (21 * 2).object_id # => true
1734  * "hello".object_id == "hello".object_id # => false
1735  * "hi".freeze.object_id == "hi".freeze.object_id # => true
1736  */
1737 
1738 VALUE
1740 {
1741  /* If obj is an immediate, the object ID is obj directly converted to a Numeric.
1742  * Otherwise, the object ID is a Numeric that is a non-zero multiple of
1743  * (RUBY_IMMEDIATE_MASK + 1) which guarantees that it does not collide with
1744  * any immediates. */
1745  return rb_find_object_id(rb_gc_get_objspace(), obj, rb_gc_impl_object_id);
1746 }
1747 
1748 static enum rb_id_table_iterator_result
1749 cc_table_memsize_i(VALUE ccs_ptr, void *data_ptr)
1750 {
1751  size_t *total_size = data_ptr;
1752  struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
1753  *total_size += sizeof(*ccs);
1754  *total_size += sizeof(ccs->entries[0]) * ccs->capa;
1755  return ID_TABLE_CONTINUE;
1756 }
1757 
1758 static size_t
1759 cc_table_memsize(struct rb_id_table *cc_table)
1760 {
1761  size_t total = rb_id_table_memsize(cc_table);
1762  rb_id_table_foreach_values(cc_table, cc_table_memsize_i, &total);
1763  return total;
1764 }
1765 
1766 size_t
1767 rb_obj_memsize_of(VALUE obj)
1768 {
1769  size_t size = 0;
1770 
1771  if (SPECIAL_CONST_P(obj)) {
1772  return 0;
1773  }
1774 
1775  if (FL_TEST(obj, FL_EXIVAR)) {
1776  size += rb_generic_ivar_memsize(obj);
1777  }
1778 
1779  switch (BUILTIN_TYPE(obj)) {
1780  case T_OBJECT:
1781  if (rb_shape_obj_too_complex(obj)) {
1782  size += rb_st_memsize(ROBJECT_IV_HASH(obj));
1783  }
1784  else if (!(RBASIC(obj)->flags & ROBJECT_EMBED)) {
1785  size += ROBJECT_IV_CAPACITY(obj) * sizeof(VALUE);
1786  }
1787  break;
1788  case T_MODULE:
1789  case T_CLASS:
1790  if (RCLASS_M_TBL(obj)) {
1791  size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1792  }
1793  // class IV sizes are allocated as powers of two
1794  size += SIZEOF_VALUE << bit_length(RCLASS_IV_COUNT(obj));
1795  if (RCLASS_CVC_TBL(obj)) {
1796  size += rb_id_table_memsize(RCLASS_CVC_TBL(obj));
1797  }
1798  if (RCLASS_EXT(obj)->const_tbl) {
1799  size += rb_id_table_memsize(RCLASS_EXT(obj)->const_tbl);
1800  }
1801  if (RCLASS_CC_TBL(obj)) {
1802  size += cc_table_memsize(RCLASS_CC_TBL(obj));
1803  }
1804  if (FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
1805  size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) * sizeof(VALUE);
1806  }
1807  break;
1808  case T_ICLASS:
1809  if (RICLASS_OWNS_M_TBL_P(obj)) {
1810  if (RCLASS_M_TBL(obj)) {
1811  size += rb_id_table_memsize(RCLASS_M_TBL(obj));
1812  }
1813  }
1814  if (RCLASS_CC_TBL(obj)) {
1815  size += cc_table_memsize(RCLASS_CC_TBL(obj));
1816  }
1817  break;
1818  case T_STRING:
1819  size += rb_str_memsize(obj);
1820  break;
1821  case T_ARRAY:
1822  size += rb_ary_memsize(obj);
1823  break;
1824  case T_HASH:
1825  if (RHASH_ST_TABLE_P(obj)) {
1826  VM_ASSERT(RHASH_ST_TABLE(obj) != NULL);
1827  /* st_table is in the slot */
1828  size += st_memsize(RHASH_ST_TABLE(obj)) - sizeof(st_table);
1829  }
1830  break;
1831  case T_REGEXP:
1832  if (RREGEXP_PTR(obj)) {
1833  size += onig_memsize(RREGEXP_PTR(obj));
1834  }
1835  break;
1836  case T_DATA:
1837  size += rb_objspace_data_type_memsize(obj);
1838  break;
1839  case T_MATCH:
1840  {
1841  rb_matchext_t *rm = RMATCH_EXT(obj);
1842  size += onig_region_memsize(&rm->regs);
1843  size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated;
1844  }
1845  break;
1846  case T_FILE:
1847  if (RFILE(obj)->fptr) {
1848  size += rb_io_memsize(RFILE(obj)->fptr);
1849  }
1850  break;
1851  case T_RATIONAL:
1852  case T_COMPLEX:
1853  break;
1854  case T_IMEMO:
1855  size += rb_imemo_memsize(obj);
1856  break;
1857 
1858  case T_FLOAT:
1859  case T_SYMBOL:
1860  break;
1861 
1862  case T_BIGNUM:
1863  if (!(RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
1864  size += BIGNUM_LEN(obj) * sizeof(BDIGIT);
1865  }
1866  break;
1867 
1868  case T_NODE:
1869  UNEXPECTED_NODE(obj_memsize_of);
1870  break;
1871 
1872  case T_STRUCT:
1873  if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
1874  RSTRUCT(obj)->as.heap.ptr) {
1875  size += sizeof(VALUE) * RSTRUCT_LEN(obj);
1876  }
1877  break;
1878 
1879  case T_ZOMBIE:
1880  case T_MOVED:
1881  break;
1882 
1883  default:
1884  rb_bug("objspace/memsize_of(): unknown data type 0x%x(%p)",
1885  BUILTIN_TYPE(obj), (void*)obj);
1886  }
1887 
1888  return size + rb_gc_obj_slot_size(obj);
1889 }
1890 
1891 static int
1892 set_zero(st_data_t key, st_data_t val, st_data_t arg)
1893 {
1894  VALUE k = (VALUE)key;
1895  VALUE hash = (VALUE)arg;
1896  rb_hash_aset(hash, k, INT2FIX(0));
1897  return ST_CONTINUE;
1898 }
1899 
1901  size_t counts[T_MASK+1];
1902  size_t freed;
1903  size_t total;
1904 };
1905 
1906 static void
1907 count_objects_i(VALUE obj, void *d)
1908 {
1909  struct count_objects_data *data = (struct count_objects_data *)d;
1910 
1911  if (RBASIC(obj)->flags) {
1912  data->counts[BUILTIN_TYPE(obj)]++;
1913  }
1914  else {
1915  data->freed++;
1916  }
1917 
1918  data->total++;
1919 }
1920 
1921 /*
1922  * call-seq:
1923  * ObjectSpace.count_objects([result_hash]) -> hash
1924  *
1925  * Counts all objects grouped by type.
1926  *
1927  * It returns a hash, such as:
1928  * {
1929  * :TOTAL=>10000,
1930  * :FREE=>3011,
1931  * :T_OBJECT=>6,
1932  * :T_CLASS=>404,
1933  * # ...
1934  * }
1935  *
1936  * The contents of the returned hash are implementation specific.
1937  * It may be changed in future.
1938  *
1939  * The keys starting with +:T_+ means live objects.
1940  * For example, +:T_ARRAY+ is the number of arrays.
1941  * +:FREE+ means object slots which is not used now.
1942  * +:TOTAL+ means sum of above.
1943  *
1944  * If the optional argument +result_hash+ is given,
1945  * it is overwritten and returned. This is intended to avoid probe effect.
1946  *
1947  * h = {}
1948  * ObjectSpace.count_objects(h)
1949  * puts h
1950  * # => { :TOTAL=>10000, :T_CLASS=>158280, :T_MODULE=>20672, :T_STRING=>527249 }
1951  *
1952  * This method is only expected to work on C Ruby.
1953  *
1954  */
1955 
1956 static VALUE
1957 count_objects(int argc, VALUE *argv, VALUE os)
1958 {
1959  struct count_objects_data data = { 0 };
1960  VALUE hash = Qnil;
1961 
1962  if (rb_check_arity(argc, 0, 1) == 1) {
1963  hash = argv[0];
1964  if (!RB_TYPE_P(hash, T_HASH))
1965  rb_raise(rb_eTypeError, "non-hash given");
1966  }
1967 
1968  rb_gc_impl_each_object(rb_gc_get_objspace(), count_objects_i, &data);
1969 
1970  if (NIL_P(hash)) {
1971  hash = rb_hash_new();
1972  }
1973  else if (!RHASH_EMPTY_P(hash)) {
1974  rb_hash_stlike_foreach(hash, set_zero, hash);
1975  }
1976  rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(data.total));
1977  rb_hash_aset(hash, ID2SYM(rb_intern("FREE")), SIZET2NUM(data.freed));
1978 
1979  for (size_t i = 0; i <= T_MASK; i++) {
1980  VALUE type = type_sym(i);
1981  if (data.counts[i])
1982  rb_hash_aset(hash, type, SIZET2NUM(data.counts[i]));
1983  }
1984 
1985  return hash;
1986 }
1987 
1988 #define SET_STACK_END SET_MACHINE_STACK_END(&ec->machine.stack_end)
1989 
1990 #define STACK_START (ec->machine.stack_start)
1991 #define STACK_END (ec->machine.stack_end)
1992 #define STACK_LEVEL_MAX (ec->machine.stack_maxsize/sizeof(VALUE))
1993 
1994 #if STACK_GROW_DIRECTION < 0
1995 # define STACK_LENGTH (size_t)(STACK_START - STACK_END)
1996 #elif STACK_GROW_DIRECTION > 0
1997 # define STACK_LENGTH (size_t)(STACK_END - STACK_START + 1)
1998 #else
1999 # define STACK_LENGTH ((STACK_END < STACK_START) ? (size_t)(STACK_START - STACK_END) \
2000  : (size_t)(STACK_END - STACK_START + 1))
2001 #endif
2002 #if !STACK_GROW_DIRECTION
2003 int ruby_stack_grow_direction;
2004 int
2005 ruby_get_stack_grow_direction(volatile VALUE *addr)
2006 {
2007  VALUE *end;
2008  SET_MACHINE_STACK_END(&end);
2009 
2010  if (end > addr) return ruby_stack_grow_direction = 1;
2011  return ruby_stack_grow_direction = -1;
2012 }
2013 #endif
2014 
2015 size_t
2017 {
2018  rb_execution_context_t *ec = GET_EC();
2019  SET_STACK_END;
2020  if (p) *p = STACK_UPPER(STACK_END, STACK_START, STACK_END);
2021  return STACK_LENGTH;
2022 }
2023 
2024 #define PREVENT_STACK_OVERFLOW 1
2025 #ifndef PREVENT_STACK_OVERFLOW
2026 #if !(defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK))
2027 # define PREVENT_STACK_OVERFLOW 1
2028 #else
2029 # define PREVENT_STACK_OVERFLOW 0
2030 #endif
2031 #endif
2032 #if PREVENT_STACK_OVERFLOW && !defined(__EMSCRIPTEN__)
2033 static int
2034 stack_check(rb_execution_context_t *ec, int water_mark)
2035 {
2036  SET_STACK_END;
2037 
2038  size_t length = STACK_LENGTH;
2039  size_t maximum_length = STACK_LEVEL_MAX - water_mark;
2040 
2041  return length > maximum_length;
2042 }
2043 #else
2044 #define stack_check(ec, water_mark) FALSE
2045 #endif
2046 
2047 #define STACKFRAME_FOR_CALL_CFUNC 2048
2048 
2049 int
2050 rb_ec_stack_check(rb_execution_context_t *ec)
2051 {
2052  return stack_check(ec, STACKFRAME_FOR_CALL_CFUNC);
2053 }
2054 
2055 int
2057 {
2058  return stack_check(GET_EC(), STACKFRAME_FOR_CALL_CFUNC);
2059 }
2060 
2061 /* ==================== Marking ==================== */
2062 
2063 #define RB_GC_MARK_OR_TRAVERSE(func, obj_or_ptr, obj, check_obj) do { \
2064  if (!RB_SPECIAL_CONST_P(obj)) { \
2065  rb_vm_t *vm = GET_VM(); \
2066  void *objspace = vm->gc.objspace; \
2067  if (LIKELY(vm->gc.mark_func_data == NULL)) { \
2068  GC_ASSERT(rb_gc_impl_during_gc_p(objspace)); \
2069  (func)(objspace, (obj_or_ptr)); \
2070  } \
2071  else if (check_obj ? \
2072  rb_gc_impl_pointer_to_heap_p(objspace, (const void *)obj) && \
2073  !rb_gc_impl_garbage_object_p(objspace, obj) : \
2074  true) { \
2075  GC_ASSERT(!rb_gc_impl_during_gc_p(objspace)); \
2076  struct gc_mark_func_data_struct *mark_func_data = vm->gc.mark_func_data; \
2077  vm->gc.mark_func_data = NULL; \
2078  mark_func_data->mark_func((obj), mark_func_data->data); \
2079  vm->gc.mark_func_data = mark_func_data; \
2080  } \
2081  } \
2082 } while (0)
2083 
2084 static inline void
2085 gc_mark_internal(VALUE obj)
2086 {
2087  RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark, obj, obj, false);
2088 }
2089 
2090 void
2092 {
2093  gc_mark_internal(obj);
2094 }
2095 
2096 void
2097 rb_gc_mark_and_move(VALUE *ptr)
2098 {
2099  RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_move, ptr, *ptr, false);
2100 }
2101 
2102 static inline void
2103 gc_mark_and_pin_internal(VALUE obj)
2104 {
2105  RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_and_pin, obj, obj, false);
2106 }
2107 
2108 void
2110 {
2111  gc_mark_and_pin_internal(obj);
2112 }
2113 
2114 static inline void
2115 gc_mark_maybe_internal(VALUE obj)
2116 {
2117  RB_GC_MARK_OR_TRAVERSE(rb_gc_impl_mark_maybe, obj, obj, true);
2118 }
2119 
2120 void
2122 {
2123  gc_mark_maybe_internal(obj);
2124 }
2125 
2126 void
2127 rb_gc_mark_weak(VALUE *ptr)
2128 {
2129  if (RB_SPECIAL_CONST_P(*ptr)) return;
2130 
2131  rb_vm_t *vm = GET_VM();
2132  void *objspace = vm->gc.objspace;
2133  if (LIKELY(vm->gc.mark_func_data == NULL)) {
2134  GC_ASSERT(rb_gc_impl_during_gc_p(objspace));
2135 
2136  rb_gc_impl_mark_weak(objspace, ptr);
2137  }
2138  else {
2139  GC_ASSERT(!rb_gc_impl_during_gc_p(objspace));
2140  }
2141 }
2142 
2143 void
2144 rb_gc_remove_weak(VALUE parent_obj, VALUE *ptr)
2145 {
2146  rb_gc_impl_remove_weak(rb_gc_get_objspace(), parent_obj, ptr);
2147 }
2148 
2149 ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS(static void each_location(register const VALUE *x, register long n, void (*cb)(VALUE, void *), void *data));
2150 static void
2151 each_location(register const VALUE *x, register long n, void (*cb)(VALUE, void *), void *data)
2152 {
2153  VALUE v;
2154  while (n--) {
2155  v = *x;
2156  cb(v, data);
2157  x++;
2158  }
2159 }
2160 
2161 static void
2162 each_location_ptr(const VALUE *start, const VALUE *end, void (*cb)(VALUE, void *), void *data)
2163 {
2164  if (end <= start) return;
2165  each_location(start, end - start, cb, data);
2166 }
2167 
2168 static void
2169 gc_mark_maybe_each_location(VALUE obj, void *data)
2170 {
2171  gc_mark_maybe_internal(obj);
2172 }
2173 
2174 void
2175 rb_gc_mark_locations(const VALUE *start, const VALUE *end)
2176 {
2177  each_location_ptr(start, end, gc_mark_maybe_each_location, NULL);
2178 }
2179 
2180 void
2181 rb_gc_mark_values(long n, const VALUE *values)
2182 {
2183  for (long i = 0; i < n; i++) {
2184  gc_mark_internal(values[i]);
2185  }
2186 }
2187 
2188 void
2189 rb_gc_mark_vm_stack_values(long n, const VALUE *values)
2190 {
2191  for (long i = 0; i < n; i++) {
2192  gc_mark_and_pin_internal(values[i]);
2193  }
2194 }
2195 
2196 static int
2197 mark_key(st_data_t key, st_data_t value, st_data_t data)
2198 {
2199  gc_mark_and_pin_internal((VALUE)key);
2200 
2201  return ST_CONTINUE;
2202 }
2203 
2204 void
2206 {
2207  if (!tbl) return;
2208 
2209  st_foreach(tbl, mark_key, (st_data_t)rb_gc_get_objspace());
2210 }
2211 
2212 static int
2213 mark_keyvalue(st_data_t key, st_data_t value, st_data_t data)
2214 {
2215  gc_mark_internal((VALUE)key);
2216  gc_mark_internal((VALUE)value);
2217 
2218  return ST_CONTINUE;
2219 }
2220 
2221 static int
2222 pin_key_pin_value(st_data_t key, st_data_t value, st_data_t data)
2223 {
2224  gc_mark_and_pin_internal((VALUE)key);
2225  gc_mark_and_pin_internal((VALUE)value);
2226 
2227  return ST_CONTINUE;
2228 }
2229 
2230 static int
2231 pin_key_mark_value(st_data_t key, st_data_t value, st_data_t data)
2232 {
2233  gc_mark_and_pin_internal((VALUE)key);
2234  gc_mark_internal((VALUE)value);
2235 
2236  return ST_CONTINUE;
2237 }
2238 
2239 static void
2240 mark_hash(VALUE hash)
2241 {
2242  if (rb_hash_compare_by_id_p(hash)) {
2243  rb_hash_stlike_foreach(hash, pin_key_mark_value, 0);
2244  }
2245  else {
2246  rb_hash_stlike_foreach(hash, mark_keyvalue, 0);
2247  }
2248 
2249  gc_mark_internal(RHASH(hash)->ifnone);
2250 }
2251 
2252 void
2254 {
2255  if (!tbl) return;
2256 
2257  st_foreach(tbl, pin_key_pin_value, 0);
2258 }
2259 
2260 static enum rb_id_table_iterator_result
2261 mark_method_entry_i(VALUE me, void *objspace)
2262 {
2263  gc_mark_internal(me);
2264 
2265  return ID_TABLE_CONTINUE;
2266 }
2267 
2268 static void
2269 mark_m_tbl(void *objspace, struct rb_id_table *tbl)
2270 {
2271  if (tbl) {
2272  rb_id_table_foreach_values(tbl, mark_method_entry_i, objspace);
2273  }
2274 }
2275 
2276 #if STACK_GROW_DIRECTION < 0
2277 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_END, (end) = STACK_START)
2278 #elif STACK_GROW_DIRECTION > 0
2279 #define GET_STACK_BOUNDS(start, end, appendix) ((start) = STACK_START, (end) = STACK_END+(appendix))
2280 #else
2281 #define GET_STACK_BOUNDS(start, end, appendix) \
2282  ((STACK_END < STACK_START) ? \
2283  ((start) = STACK_END, (end) = STACK_START) : ((start) = STACK_START, (end) = STACK_END+(appendix)))
2284 #endif
2285 
2286 static void
2287 gc_mark_machine_stack_location_maybe(VALUE obj, void *data)
2288 {
2289  gc_mark_maybe_internal(obj);
2290 
2291 #ifdef RUBY_ASAN_ENABLED
2292  const rb_execution_context_t *ec = (const rb_execution_context_t *)data;
2293  void *fake_frame_start;
2294  void *fake_frame_end;
2295  bool is_fake_frame = asan_get_fake_stack_extents(
2296  ec->machine.asan_fake_stack_handle, obj,
2297  ec->machine.stack_start, ec->machine.stack_end,
2298  &fake_frame_start, &fake_frame_end
2299  );
2300  if (is_fake_frame) {
2301  each_location_ptr(fake_frame_start, fake_frame_end, gc_mark_maybe_each_location, NULL);
2302  }
2303 #endif
2304 }
2305 
2306 #if defined(__wasm__)
2307 
2308 
2309 static VALUE *rb_stack_range_tmp[2];
2310 
2311 static void
2312 rb_mark_locations(void *begin, void *end)
2313 {
2314  rb_stack_range_tmp[0] = begin;
2315  rb_stack_range_tmp[1] = end;
2316 }
2317 
2318 # if defined(__EMSCRIPTEN__)
2319 
2320 static void
2321 mark_current_machine_context(rb_execution_context_t *ec)
2322 {
2323  emscripten_scan_stack(rb_mark_locations);
2324  each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2325 
2326  emscripten_scan_registers(rb_mark_locations);
2327  each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2328 }
2329 # else // use Asyncify version
2330 
2331 static void
2332 mark_current_machine_context(rb_execution_context_t *ec)
2333 {
2334  VALUE *stack_start, *stack_end;
2335  SET_STACK_END;
2336  GET_STACK_BOUNDS(stack_start, stack_end, 1);
2337  each_location_ptr(stack_start, stack_end, gc_mark_maybe_each_location, NULL);
2338 
2339  rb_wasm_scan_locals(rb_mark_locations);
2340  each_location_ptr(rb_stack_range_tmp[0], rb_stack_range_tmp[1], gc_mark_maybe_each_location, NULL);
2341 }
2342 
2343 # endif
2344 
2345 #else // !defined(__wasm__)
2346 
2347 static void
2348 mark_current_machine_context(rb_execution_context_t *ec)
2349 {
2350  union {
2351  rb_jmp_buf j;
2352  VALUE v[sizeof(rb_jmp_buf) / (sizeof(VALUE))];
2353  } save_regs_gc_mark;
2354  VALUE *stack_start, *stack_end;
2355 
2356  FLUSH_REGISTER_WINDOWS;
2357  memset(&save_regs_gc_mark, 0, sizeof(save_regs_gc_mark));
2358  /* This assumes that all registers are saved into the jmp_buf (and stack) */
2359  rb_setjmp(save_regs_gc_mark.j);
2360 
2361  /* SET_STACK_END must be called in this function because
2362  * the stack frame of this function may contain
2363  * callee save registers and they should be marked. */
2364  SET_STACK_END;
2365  GET_STACK_BOUNDS(stack_start, stack_end, 1);
2366 
2367  void *data =
2368 #ifdef RUBY_ASAN_ENABLED
2369  ec;
2370 #else
2371  NULL;
2372 #endif
2373 
2374  each_location(save_regs_gc_mark.v, numberof(save_regs_gc_mark.v), gc_mark_machine_stack_location_maybe, data);
2375  each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2376 }
2377 #endif
2378 
2379 void
2380 rb_gc_mark_machine_context(const rb_execution_context_t *ec)
2381 {
2382  VALUE *stack_start, *stack_end;
2383 
2384  GET_STACK_BOUNDS(stack_start, stack_end, 0);
2385  RUBY_DEBUG_LOG("ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
2386 
2387  void *data =
2388 #ifdef RUBY_ASAN_ENABLED
2389  /* gc_mark_machine_stack_location_maybe() uses data as const */
2390  (rb_execution_context_t *)ec;
2391 #else
2392  NULL;
2393 #endif
2394 
2395  each_location_ptr(stack_start, stack_end, gc_mark_machine_stack_location_maybe, data);
2396  int num_regs = sizeof(ec->machine.regs)/(sizeof(VALUE));
2397  each_location((VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe, data);
2398 }
2399 
2400 static int
2401 rb_mark_tbl_i(st_data_t key, st_data_t value, st_data_t data)
2402 {
2403  gc_mark_and_pin_internal((VALUE)value);
2404 
2405  return ST_CONTINUE;
2406 }
2407 
2408 void
2410 {
2411  if (!tbl || tbl->num_entries == 0) return;
2412 
2413  st_foreach(tbl, rb_mark_tbl_i, 0);
2414 }
2415 
2416 static void
2417 gc_mark_tbl_no_pin(st_table *tbl)
2418 {
2419  if (!tbl || tbl->num_entries == 0) return;
2420 
2421  st_foreach(tbl, gc_mark_tbl_no_pin_i, 0);
2422 }
2423 
2424 void
2426 {
2427  gc_mark_tbl_no_pin(tbl);
2428 }
2429 
2430 static enum rb_id_table_iterator_result
2431 mark_cvc_tbl_i(VALUE cvc_entry, void *objspace)
2432 {
2433  struct rb_cvar_class_tbl_entry *entry;
2434 
2435  entry = (struct rb_cvar_class_tbl_entry *)cvc_entry;
2436 
2437  RUBY_ASSERT(entry->cref == 0 || (BUILTIN_TYPE((VALUE)entry->cref) == T_IMEMO && IMEMO_TYPE_P(entry->cref, imemo_cref)));
2438  gc_mark_internal((VALUE)entry->cref);
2439 
2440  return ID_TABLE_CONTINUE;
2441 }
2442 
2443 static void
2444 mark_cvc_tbl(void *objspace, VALUE klass)
2445 {
2446  struct rb_id_table *tbl = RCLASS_CVC_TBL(klass);
2447  if (tbl) {
2448  rb_id_table_foreach_values(tbl, mark_cvc_tbl_i, objspace);
2449  }
2450 }
2451 
2452 static bool
2453 gc_declarative_marking_p(const rb_data_type_t *type)
2454 {
2455  return (type->flags & RUBY_TYPED_DECL_MARKING) != 0;
2456 }
2457 
2458 static enum rb_id_table_iterator_result
2459 mark_const_table_i(VALUE value, void *objspace)
2460 {
2461  const rb_const_entry_t *ce = (const rb_const_entry_t *)value;
2462 
2463  gc_mark_internal(ce->value);
2464  gc_mark_internal(ce->file);
2465 
2466  return ID_TABLE_CONTINUE;
2467 }
2468 
2469 void
2470 rb_gc_mark_roots(void *objspace, const char **categoryp)
2471 {
2472  rb_execution_context_t *ec = GET_EC();
2473  rb_vm_t *vm = rb_ec_vm_ptr(ec);
2474 
2475 #define MARK_CHECKPOINT(category) do { \
2476  if (categoryp) *categoryp = category; \
2477 } while (0)
2478 
2479  MARK_CHECKPOINT("vm");
2480  rb_vm_mark(vm);
2481  if (vm->self) gc_mark_internal(vm->self);
2482 
2483  MARK_CHECKPOINT("machine_context");
2484  mark_current_machine_context(ec);
2485 
2486  MARK_CHECKPOINT("end_proc");
2487  rb_mark_end_proc();
2488 
2489  MARK_CHECKPOINT("global_tbl");
2490  rb_gc_mark_global_tbl();
2491 
2492 #if USE_YJIT
2493  void rb_yjit_root_mark(void); // in Rust
2494 
2495  if (rb_yjit_enabled_p) {
2496  MARK_CHECKPOINT("YJIT");
2497  rb_yjit_root_mark();
2498  }
2499 #endif
2500 
2501  MARK_CHECKPOINT("finish");
2502 #undef MARK_CHECKPOINT
2503 }
2504 
2505 #define TYPED_DATA_REFS_OFFSET_LIST(d) (size_t *)(uintptr_t)RTYPEDDATA(d)->type->function.dmark
2506 
2507 void
2508 rb_gc_mark_children(void *objspace, VALUE obj)
2509 {
2510  if (FL_TEST(obj, FL_EXIVAR)) {
2511  rb_mark_generic_ivar(obj);
2512  }
2513 
2514  switch (BUILTIN_TYPE(obj)) {
2515  case T_FLOAT:
2516  case T_BIGNUM:
2517  case T_SYMBOL:
2518  /* Not immediates, but does not have references and singleton class.
2519  *
2520  * RSYMBOL(obj)->fstr intentionally not marked. See log for 96815f1e
2521  * ("symbol.c: remove rb_gc_mark_symbols()") */
2522  return;
2523 
2524  case T_NIL:
2525  case T_FIXNUM:
2526  rb_bug("rb_gc_mark() called for broken object");
2527  break;
2528 
2529  case T_NODE:
2530  UNEXPECTED_NODE(rb_gc_mark);
2531  break;
2532 
2533  case T_IMEMO:
2534  rb_imemo_mark_and_move(obj, false);
2535  return;
2536 
2537  default:
2538  break;
2539  }
2540 
2541  gc_mark_internal(RBASIC(obj)->klass);
2542 
2543  switch (BUILTIN_TYPE(obj)) {
2544  case T_CLASS:
2545  if (FL_TEST(obj, FL_SINGLETON)) {
2546  gc_mark_internal(RCLASS_ATTACHED_OBJECT(obj));
2547  }
2548  // Continue to the shared T_CLASS/T_MODULE
2549  case T_MODULE:
2550  if (RCLASS_SUPER(obj)) {
2551  gc_mark_internal(RCLASS_SUPER(obj));
2552  }
2553 
2554  mark_m_tbl(objspace, RCLASS_M_TBL(obj));
2555  mark_cvc_tbl(objspace, obj);
2556  rb_cc_table_mark(obj);
2557  if (rb_shape_obj_too_complex(obj)) {
2558  gc_mark_tbl_no_pin((st_table *)RCLASS_IVPTR(obj));
2559  }
2560  else {
2561  for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
2562  gc_mark_internal(RCLASS_IVPTR(obj)[i]);
2563  }
2564  }
2565 
2566  if (RCLASS_CONST_TBL(obj)) {
2567  rb_id_table_foreach_values(RCLASS_CONST_TBL(obj), mark_const_table_i, objspace);
2568  }
2569 
2570  gc_mark_internal(RCLASS_EXT(obj)->classpath);
2571  break;
2572 
2573  case T_ICLASS:
2574  if (RICLASS_OWNS_M_TBL_P(obj)) {
2575  mark_m_tbl(objspace, RCLASS_M_TBL(obj));
2576  }
2577  if (RCLASS_SUPER(obj)) {
2578  gc_mark_internal(RCLASS_SUPER(obj));
2579  }
2580 
2581  if (RCLASS_INCLUDER(obj)) {
2582  gc_mark_internal(RCLASS_INCLUDER(obj));
2583  }
2584  mark_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
2585  rb_cc_table_mark(obj);
2586  break;
2587 
2588  case T_ARRAY:
2589  if (ARY_SHARED_P(obj)) {
2590  VALUE root = ARY_SHARED_ROOT(obj);
2591  gc_mark_internal(root);
2592  }
2593  else {
2594  long len = RARRAY_LEN(obj);
2595  const VALUE *ptr = RARRAY_CONST_PTR(obj);
2596  for (long i = 0; i < len; i++) {
2597  gc_mark_internal(ptr[i]);
2598  }
2599  }
2600  break;
2601 
2602  case T_HASH:
2603  mark_hash(obj);
2604  break;
2605 
2606  case T_STRING:
2607  if (STR_SHARED_P(obj)) {
2608  if (STR_EMBED_P(RSTRING(obj)->as.heap.aux.shared)) {
2609  /* Embedded shared strings cannot be moved because this string
2610  * points into the slot of the shared string. There may be code
2611  * using the RSTRING_PTR on the stack, which would pin this
2612  * string but not pin the shared string, causing it to move. */
2613  gc_mark_and_pin_internal(RSTRING(obj)->as.heap.aux.shared);
2614  }
2615  else {
2616  gc_mark_internal(RSTRING(obj)->as.heap.aux.shared);
2617  }
2618  }
2619  break;
2620 
2621  case T_DATA: {
2622  void *const ptr = RTYPEDDATA_P(obj) ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
2623 
2624  if (ptr) {
2625  if (RTYPEDDATA_P(obj) && gc_declarative_marking_p(RTYPEDDATA(obj)->type)) {
2626  size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
2627 
2628  for (size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
2629  gc_mark_internal(*(VALUE *)((char *)ptr + offset));
2630  }
2631  }
2632  else {
2633  RUBY_DATA_FUNC mark_func = RTYPEDDATA_P(obj) ?
2634  RTYPEDDATA(obj)->type->function.dmark :
2635  RDATA(obj)->dmark;
2636  if (mark_func) (*mark_func)(ptr);
2637  }
2638  }
2639 
2640  break;
2641  }
2642 
2643  case T_OBJECT: {
2644  rb_shape_t *shape = rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj));
2645 
2646  if (rb_shape_obj_too_complex(obj)) {
2647  gc_mark_tbl_no_pin(ROBJECT_IV_HASH(obj));
2648  }
2649  else {
2650  const VALUE * const ptr = ROBJECT_IVPTR(obj);
2651 
2652  uint32_t len = ROBJECT_IV_COUNT(obj);
2653  for (uint32_t i = 0; i < len; i++) {
2654  gc_mark_internal(ptr[i]);
2655  }
2656  }
2657 
2658  if (shape) {
2659  VALUE klass = RBASIC_CLASS(obj);
2660 
2661  // Increment max_iv_count if applicable, used to determine size pool allocation
2662  attr_index_t num_of_ivs = shape->next_iv_index;
2663  if (RCLASS_EXT(klass)->max_iv_count < num_of_ivs) {
2664  RCLASS_EXT(klass)->max_iv_count = num_of_ivs;
2665  }
2666  }
2667 
2668  break;
2669  }
2670 
2671  case T_FILE:
2672  if (RFILE(obj)->fptr) {
2673  gc_mark_internal(RFILE(obj)->fptr->self);
2674  gc_mark_internal(RFILE(obj)->fptr->pathv);
2675  gc_mark_internal(RFILE(obj)->fptr->tied_io_for_writing);
2676  gc_mark_internal(RFILE(obj)->fptr->writeconv_asciicompat);
2677  gc_mark_internal(RFILE(obj)->fptr->writeconv_pre_ecopts);
2678  gc_mark_internal(RFILE(obj)->fptr->encs.ecopts);
2679  gc_mark_internal(RFILE(obj)->fptr->write_lock);
2680  gc_mark_internal(RFILE(obj)->fptr->timeout);
2681  }
2682  break;
2683 
2684  case T_REGEXP:
2685  gc_mark_internal(RREGEXP(obj)->src);
2686  break;
2687 
2688  case T_MATCH:
2689  gc_mark_internal(RMATCH(obj)->regexp);
2690  if (RMATCH(obj)->str) {
2691  gc_mark_internal(RMATCH(obj)->str);
2692  }
2693  break;
2694 
2695  case T_RATIONAL:
2696  gc_mark_internal(RRATIONAL(obj)->num);
2697  gc_mark_internal(RRATIONAL(obj)->den);
2698  break;
2699 
2700  case T_COMPLEX:
2701  gc_mark_internal(RCOMPLEX(obj)->real);
2702  gc_mark_internal(RCOMPLEX(obj)->imag);
2703  break;
2704 
2705  case T_STRUCT: {
2706  const long len = RSTRUCT_LEN(obj);
2707  const VALUE * const ptr = RSTRUCT_CONST_PTR(obj);
2708 
2709  for (long i = 0; i < len; i++) {
2710  gc_mark_internal(ptr[i]);
2711  }
2712 
2713  break;
2714  }
2715 
2716  default:
2717  if (BUILTIN_TYPE(obj) == T_MOVED) rb_bug("rb_gc_mark(): %p is T_MOVED", (void *)obj);
2718  if (BUILTIN_TYPE(obj) == T_NONE) rb_bug("rb_gc_mark(): %p is T_NONE", (void *)obj);
2719  if (BUILTIN_TYPE(obj) == T_ZOMBIE) rb_bug("rb_gc_mark(): %p is T_ZOMBIE", (void *)obj);
2720  rb_bug("rb_gc_mark(): unknown data type 0x%x(%p) %s",
2721  BUILTIN_TYPE(obj), (void *)obj,
2722  rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj) ? "corrupted object" : "non object");
2723  }
2724 }
2725 
2726 size_t
2727 rb_gc_obj_optimal_size(VALUE obj)
2728 {
2729  switch (BUILTIN_TYPE(obj)) {
2730  case T_ARRAY:
2731  return rb_ary_size_as_embedded(obj);
2732 
2733  case T_OBJECT:
2734  if (rb_shape_obj_too_complex(obj)) {
2735  return sizeof(struct RObject);
2736  }
2737  else {
2738  return rb_obj_embedded_size(ROBJECT_IV_CAPACITY(obj));
2739  }
2740 
2741  case T_STRING:
2742  return rb_str_size_as_embedded(obj);
2743 
2744  case T_HASH:
2745  return sizeof(struct RHash) + (RHASH_ST_TABLE_P(obj) ? sizeof(st_table) : sizeof(ar_table));
2746 
2747  default:
2748  return 0;
2749  }
2750 }
2751 
2752 void
2753 rb_gc_writebarrier(VALUE a, VALUE b)
2754 {
2755  rb_gc_impl_writebarrier(rb_gc_get_objspace(), a, b);
2756 }
2757 
2758 void
2760 {
2761  rb_gc_impl_writebarrier_unprotect(rb_gc_get_objspace(), obj);
2762 }
2763 
2764 /*
2765  * remember `obj' if needed.
2766  */
2767 void
2768 rb_gc_writebarrier_remember(VALUE obj)
2769 {
2770  rb_gc_impl_writebarrier_remember(rb_gc_get_objspace(), obj);
2771 }
2772 
2773 void
2774 rb_gc_copy_attributes(VALUE dest, VALUE obj)
2775 {
2776  rb_gc_impl_copy_attributes(rb_gc_get_objspace(), dest, obj);
2777 }
2778 
2779 int
2780 rb_gc_external_gc_loaded_p(void)
2781 {
2782  return external_gc_loaded;
2783 }
2784 
2785 const char *
2786 rb_gc_active_gc_name(void)
2787 {
2788  const char *gc_name = rb_gc_impl_active_gc_name();
2789  const size_t len = strlen(gc_name);
2790  if (len > RB_GC_MAX_NAME_LEN) {
2791  rb_bug("GC should have a name no more than %d chars long. Currently: %zu (%s)",
2792  RB_GC_MAX_NAME_LEN, len, gc_name);
2793  }
2794  return gc_name;
2795 
2796 }
2797 
2798 // TODO: rearchitect this function to work for a generic GC
2799 size_t
2800 rb_obj_gc_flags(VALUE obj, ID* flags, size_t max)
2801 {
2802  return rb_gc_impl_obj_flags(rb_gc_get_objspace(), obj, flags, max);
2803 }
2804 
2805 /* GC */
2806 
2807 void *
2808 rb_gc_ractor_cache_alloc(void)
2809 {
2810  return rb_gc_impl_ractor_cache_alloc(rb_gc_get_objspace());
2811 }
2812 
2813 void
2814 rb_gc_ractor_cache_free(void *cache)
2815 {
2816  rb_gc_impl_ractor_cache_free(rb_gc_get_objspace(), cache);
2817 }
2818 
2819 void
2821 {
2822  if (!rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj))
2823  return;
2824 
2825  rb_vm_register_global_object(obj);
2826 }
2827 
2828 void
2830 {
2831  rb_vm_t *vm = GET_VM();
2832 
2833  VALUE obj = *addr;
2834 
2835  struct global_object_list *tmp = ALLOC(struct global_object_list);
2836  tmp->next = vm->global_object_list;
2837  tmp->varptr = addr;
2838  vm->global_object_list = tmp;
2839 
2840  /*
2841  * Because some C extensions have assignment-then-register bugs,
2842  * we guard `obj` here so that it would not get swept defensively.
2843  */
2844  RB_GC_GUARD(obj);
2845  if (0 && !SPECIAL_CONST_P(obj)) {
2846  rb_warn("Object is assigned to registering address already: %"PRIsVALUE,
2847  rb_obj_class(obj));
2848  rb_print_backtrace(stderr);
2849  }
2850 }
2851 
2852 void
2854 {
2855  rb_vm_t *vm = GET_VM();
2856  struct global_object_list *tmp = vm->global_object_list;
2857 
2858  if (tmp->varptr == addr) {
2859  vm->global_object_list = tmp->next;
2860  xfree(tmp);
2861  return;
2862  }
2863  while (tmp->next) {
2864  if (tmp->next->varptr == addr) {
2865  struct global_object_list *t = tmp->next;
2866 
2867  tmp->next = tmp->next->next;
2868  xfree(t);
2869  break;
2870  }
2871  tmp = tmp->next;
2872  }
2873 }
2874 
2875 void
2877 {
2879 }
2880 
2881 static VALUE
2882 gc_start_internal(rb_execution_context_t *ec, VALUE self, VALUE full_mark, VALUE immediate_mark, VALUE immediate_sweep, VALUE compact)
2883 {
2884  rb_gc_impl_start(rb_gc_get_objspace(), RTEST(full_mark), RTEST(immediate_mark), RTEST(immediate_sweep), RTEST(compact));
2885 
2886  return Qnil;
2887 }
2888 
2889 /*
2890  * rb_objspace_each_objects() is special C API to walk through
2891  * Ruby object space. This C API is too difficult to use it.
2892  * To be frank, you should not use it. Or you need to read the
2893  * source code of this function and understand what this function does.
2894  *
2895  * 'callback' will be called several times (the number of heap page,
2896  * at current implementation) with:
2897  * vstart: a pointer to the first living object of the heap_page.
2898  * vend: a pointer to next to the valid heap_page area.
2899  * stride: a distance to next VALUE.
2900  *
2901  * If callback() returns non-zero, the iteration will be stopped.
2902  *
2903  * This is a sample callback code to iterate liveness objects:
2904  *
2905  * static int
2906  * sample_callback(void *vstart, void *vend, int stride, void *data)
2907  * {
2908  * VALUE v = (VALUE)vstart;
2909  * for (; v != (VALUE)vend; v += stride) {
2910  * if (!rb_objspace_internal_object_p(v)) { // liveness check
2911  * // do something with live object 'v'
2912  * }
2913  * }
2914  * return 0; // continue to iteration
2915  * }
2916  *
2917  * Note: 'vstart' is not a top of heap_page. This point the first
2918  * living object to grasp at least one object to avoid GC issue.
2919  * This means that you can not walk through all Ruby object page
2920  * including freed object page.
2921  *
2922  * Note: On this implementation, 'stride' is the same as sizeof(RVALUE).
2923  * However, there are possibilities to pass variable values with
2924  * 'stride' with some reasons. You must use stride instead of
2925  * use some constant value in the iteration.
2926  */
2927 void
2928 rb_objspace_each_objects(int (*callback)(void *, void *, size_t, void *), void *data)
2929 {
2930  rb_gc_impl_each_objects(rb_gc_get_objspace(), callback, data);
2931 }
2932 
2933 static void
2934 gc_ref_update_array(void *objspace, VALUE v)
2935 {
2936  if (ARY_SHARED_P(v)) {
2937  VALUE old_root = RARRAY(v)->as.heap.aux.shared_root;
2938 
2939  UPDATE_IF_MOVED(objspace, RARRAY(v)->as.heap.aux.shared_root);
2940 
2941  VALUE new_root = RARRAY(v)->as.heap.aux.shared_root;
2942  // If the root is embedded and its location has changed
2943  if (ARY_EMBED_P(new_root) && new_root != old_root) {
2944  size_t offset = (size_t)(RARRAY(v)->as.heap.ptr - RARRAY(old_root)->as.ary);
2945  GC_ASSERT(RARRAY(v)->as.heap.ptr >= RARRAY(old_root)->as.ary);
2946  RARRAY(v)->as.heap.ptr = RARRAY(new_root)->as.ary + offset;
2947  }
2948  }
2949  else {
2950  long len = RARRAY_LEN(v);
2951 
2952  if (len > 0) {
2953  VALUE *ptr = (VALUE *)RARRAY_CONST_PTR(v);
2954  for (long i = 0; i < len; i++) {
2955  UPDATE_IF_MOVED(objspace, ptr[i]);
2956  }
2957  }
2958 
2959  if (rb_gc_obj_slot_size(v) >= rb_ary_size_as_embedded(v)) {
2960  if (rb_ary_embeddable_p(v)) {
2961  rb_ary_make_embedded(v);
2962  }
2963  }
2964  }
2965 }
2966 
2967 static void
2968 gc_ref_update_object(void *objspace, VALUE v)
2969 {
2970  VALUE *ptr = ROBJECT_IVPTR(v);
2971 
2972  if (rb_shape_obj_too_complex(v)) {
2973  gc_ref_update_table_values_only(ROBJECT_IV_HASH(v));
2974  return;
2975  }
2976 
2977  size_t slot_size = rb_gc_obj_slot_size(v);
2978  size_t embed_size = rb_obj_embedded_size(ROBJECT_IV_CAPACITY(v));
2979  if (slot_size >= embed_size && !RB_FL_TEST_RAW(v, ROBJECT_EMBED)) {
2980  // Object can be re-embedded
2981  memcpy(ROBJECT(v)->as.ary, ptr, sizeof(VALUE) * ROBJECT_IV_COUNT(v));
2982  RB_FL_SET_RAW(v, ROBJECT_EMBED);
2983  xfree(ptr);
2984  ptr = ROBJECT(v)->as.ary;
2985  }
2986 
2987  for (uint32_t i = 0; i < ROBJECT_IV_COUNT(v); i++) {
2988  UPDATE_IF_MOVED(objspace, ptr[i]);
2989  }
2990 }
2991 
2992 void
2993 rb_gc_ref_update_table_values_only(st_table *tbl)
2994 {
2995  gc_ref_update_table_values_only(tbl);
2996 }
2997 
2998 /* Update MOVED references in a VALUE=>VALUE st_table */
2999 void
3001 {
3002  gc_update_table_refs(ptr);
3003 }
3004 
3005 static void
3006 gc_ref_update_hash(void *objspace, VALUE v)
3007 {
3008  rb_hash_stlike_foreach_with_replace(v, hash_foreach_replace, hash_replace_ref, (st_data_t)objspace);
3009 }
3010 
3011 static void
3012 gc_update_values(void *objspace, long n, VALUE *values)
3013 {
3014  for (long i = 0; i < n; i++) {
3015  UPDATE_IF_MOVED(objspace, values[i]);
3016  }
3017 }
3018 
3019 void
3020 rb_gc_update_values(long n, VALUE *values)
3021 {
3022  gc_update_values(rb_gc_get_objspace(), n, values);
3023 }
3024 
3025 static enum rb_id_table_iterator_result
3026 check_id_table_move(VALUE value, void *data)
3027 {
3028  void *objspace = (void *)data;
3029 
3030  if (rb_gc_impl_object_moved_p(objspace, (VALUE)value)) {
3031  return ID_TABLE_REPLACE;
3032  }
3033 
3034  return ID_TABLE_CONTINUE;
3035 }
3036 
3037 VALUE
3039 {
3040  return rb_gc_impl_location(rb_gc_get_objspace(), value);
3041 }
3042 
3043 void
3044 rb_gc_prepare_heap_process_object(VALUE obj)
3045 {
3046  switch (BUILTIN_TYPE(obj)) {
3047  case T_STRING:
3048  // Precompute the string coderange. This both save time for when it will be
3049  // eventually needed, and avoid mutating heap pages after a potential fork.
3050  rb_enc_str_coderange(obj);
3051  break;
3052  default:
3053  break;
3054  }
3055 }
3056 
3057 void
3058 rb_gc_prepare_heap(void)
3059 {
3060  rb_gc_impl_prepare_heap(rb_gc_get_objspace());
3061 }
3062 
3063 size_t
3064 rb_gc_heap_id_for_size(size_t size)
3065 {
3066  return rb_gc_impl_heap_id_for_size(rb_gc_get_objspace(), size);
3067 }
3068 
3069 bool
3070 rb_gc_size_allocatable_p(size_t size)
3071 {
3072  return rb_gc_impl_size_allocatable_p(size);
3073 }
3074 
3075 static enum rb_id_table_iterator_result
3076 update_id_table(VALUE *value, void *data, int existing)
3077 {
3078  void *objspace = (void *)data;
3079 
3080  if (rb_gc_impl_object_moved_p(objspace, (VALUE)*value)) {
3081  *value = rb_gc_impl_location(objspace, (VALUE)*value);
3082  }
3083 
3084  return ID_TABLE_CONTINUE;
3085 }
3086 
3087 static void
3088 update_m_tbl(void *objspace, struct rb_id_table *tbl)
3089 {
3090  if (tbl) {
3091  rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table, objspace);
3092  }
3093 }
3094 
3095 static enum rb_id_table_iterator_result
3096 update_cc_tbl_i(VALUE ccs_ptr, void *objspace)
3097 {
3098  struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
3099  VM_ASSERT(vm_ccs_p(ccs));
3100 
3101  if (rb_gc_impl_object_moved_p(objspace, (VALUE)ccs->cme)) {
3102  ccs->cme = (const rb_callable_method_entry_t *)rb_gc_impl_location(objspace, (VALUE)ccs->cme);
3103  }
3104 
3105  for (int i=0; i<ccs->len; i++) {
3106  if (rb_gc_impl_object_moved_p(objspace, (VALUE)ccs->entries[i].cc)) {
3107  ccs->entries[i].cc = (struct rb_callcache *)rb_gc_location((VALUE)ccs->entries[i].cc);
3108  }
3109  }
3110 
3111  // do not replace
3112  return ID_TABLE_CONTINUE;
3113 }
3114 
3115 static void
3116 update_cc_tbl(void *objspace, VALUE klass)
3117 {
3118  struct rb_id_table *tbl = RCLASS_CC_TBL(klass);
3119  if (tbl) {
3120  rb_id_table_foreach_values(tbl, update_cc_tbl_i, objspace);
3121  }
3122 }
3123 
3124 static enum rb_id_table_iterator_result
3125 update_cvc_tbl_i(VALUE cvc_entry, void *objspace)
3126 {
3127  struct rb_cvar_class_tbl_entry *entry;
3128 
3129  entry = (struct rb_cvar_class_tbl_entry *)cvc_entry;
3130 
3131  if (entry->cref) {
3132  TYPED_UPDATE_IF_MOVED(objspace, rb_cref_t *, entry->cref);
3133  }
3134 
3135  entry->class_value = rb_gc_impl_location(objspace, entry->class_value);
3136 
3137  return ID_TABLE_CONTINUE;
3138 }
3139 
3140 static void
3141 update_cvc_tbl(void *objspace, VALUE klass)
3142 {
3143  struct rb_id_table *tbl = RCLASS_CVC_TBL(klass);
3144  if (tbl) {
3145  rb_id_table_foreach_values(tbl, update_cvc_tbl_i, objspace);
3146  }
3147 }
3148 
3149 static enum rb_id_table_iterator_result
3150 update_const_table(VALUE value, void *objspace)
3151 {
3152  rb_const_entry_t *ce = (rb_const_entry_t *)value;
3153 
3154  if (rb_gc_impl_object_moved_p(objspace, ce->value)) {
3155  ce->value = rb_gc_impl_location(objspace, ce->value);
3156  }
3157 
3158  if (rb_gc_impl_object_moved_p(objspace, ce->file)) {
3159  ce->file = rb_gc_impl_location(objspace, ce->file);
3160  }
3161 
3162  return ID_TABLE_CONTINUE;
3163 }
3164 
3165 static void
3166 update_const_tbl(void *objspace, struct rb_id_table *tbl)
3167 {
3168  if (!tbl) return;
3169  rb_id_table_foreach_values(tbl, update_const_table, objspace);
3170 }
3171 
3172 static void
3173 update_subclass_entries(void *objspace, rb_subclass_entry_t *entry)
3174 {
3175  while (entry) {
3176  UPDATE_IF_MOVED(objspace, entry->klass);
3177  entry = entry->next;
3178  }
3179 }
3180 
3181 static void
3182 update_class_ext(void *objspace, rb_classext_t *ext)
3183 {
3184  UPDATE_IF_MOVED(objspace, ext->origin_);
3185  UPDATE_IF_MOVED(objspace, ext->includer);
3186  UPDATE_IF_MOVED(objspace, ext->refined_class);
3187  update_subclass_entries(objspace, ext->subclasses);
3188 }
3189 
3190 static void
3191 update_superclasses(void *objspace, VALUE obj)
3192 {
3193  if (FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
3194  for (size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
3195  UPDATE_IF_MOVED(objspace, RCLASS_SUPERCLASSES(obj)[i]);
3196  }
3197  }
3198 }
3199 
3200 extern rb_symbols_t ruby_global_symbols;
3201 #define global_symbols ruby_global_symbols
3202 
3203 void
3204 rb_gc_update_vm_references(void *objspace)
3205 {
3206  rb_execution_context_t *ec = GET_EC();
3207  rb_vm_t *vm = rb_ec_vm_ptr(ec);
3208 
3209  rb_vm_update_references(vm);
3210  rb_gc_update_global_tbl();
3211  global_symbols.ids = rb_gc_impl_location(objspace, global_symbols.ids);
3212  global_symbols.dsymbol_fstr_hash = rb_gc_impl_location(objspace, global_symbols.dsymbol_fstr_hash);
3213  gc_update_table_refs(global_symbols.str_sym);
3214 
3215 #if USE_YJIT
3216  void rb_yjit_root_update_references(void); // in Rust
3217 
3218  if (rb_yjit_enabled_p) {
3219  rb_yjit_root_update_references();
3220  }
3221 #endif
3222 }
3223 
3224 void
3225 rb_gc_update_object_references(void *objspace, VALUE obj)
3226 {
3227  if (FL_TEST(obj, FL_EXIVAR)) {
3228  rb_ref_update_generic_ivar(obj);
3229  }
3230 
3231  switch (BUILTIN_TYPE(obj)) {
3232  case T_CLASS:
3233  if (FL_TEST(obj, FL_SINGLETON)) {
3234  UPDATE_IF_MOVED(objspace, RCLASS_ATTACHED_OBJECT(obj));
3235  }
3236  // Continue to the shared T_CLASS/T_MODULE
3237  case T_MODULE:
3238  if (RCLASS_SUPER((VALUE)obj)) {
3239  UPDATE_IF_MOVED(objspace, RCLASS(obj)->super);
3240  }
3241  update_m_tbl(objspace, RCLASS_M_TBL(obj));
3242  update_cc_tbl(objspace, obj);
3243  update_cvc_tbl(objspace, obj);
3244  update_superclasses(objspace, obj);
3245 
3246  if (rb_shape_obj_too_complex(obj)) {
3247  gc_ref_update_table_values_only(RCLASS_IV_HASH(obj));
3248  }
3249  else {
3250  for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) {
3251  UPDATE_IF_MOVED(objspace, RCLASS_IVPTR(obj)[i]);
3252  }
3253  }
3254 
3255  update_class_ext(objspace, RCLASS_EXT(obj));
3256  update_const_tbl(objspace, RCLASS_CONST_TBL(obj));
3257 
3258  UPDATE_IF_MOVED(objspace, RCLASS_EXT(obj)->classpath);
3259  break;
3260 
3261  case T_ICLASS:
3262  if (RICLASS_OWNS_M_TBL_P(obj)) {
3263  update_m_tbl(objspace, RCLASS_M_TBL(obj));
3264  }
3265  if (RCLASS_SUPER((VALUE)obj)) {
3266  UPDATE_IF_MOVED(objspace, RCLASS(obj)->super);
3267  }
3268  update_class_ext(objspace, RCLASS_EXT(obj));
3269  update_m_tbl(objspace, RCLASS_CALLABLE_M_TBL(obj));
3270  update_cc_tbl(objspace, obj);
3271  break;
3272 
3273  case T_IMEMO:
3274  rb_imemo_mark_and_move(obj, true);
3275  return;
3276 
3277  case T_NIL:
3278  case T_FIXNUM:
3279  case T_NODE:
3280  case T_MOVED:
3281  case T_NONE:
3282  /* These can't move */
3283  return;
3284 
3285  case T_ARRAY:
3286  gc_ref_update_array(objspace, obj);
3287  break;
3288 
3289  case T_HASH:
3290  gc_ref_update_hash(objspace, obj);
3291  UPDATE_IF_MOVED(objspace, RHASH(obj)->ifnone);
3292  break;
3293 
3294  case T_STRING:
3295  {
3296  if (STR_SHARED_P(obj)) {
3297  UPDATE_IF_MOVED(objspace, RSTRING(obj)->as.heap.aux.shared);
3298  }
3299 
3300  /* If, after move the string is not embedded, and can fit in the
3301  * slot it's been placed in, then re-embed it. */
3302  if (rb_gc_obj_slot_size(obj) >= rb_str_size_as_embedded(obj)) {
3303  if (!STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
3304  rb_str_make_embedded(obj);
3305  }
3306  }
3307 
3308  break;
3309  }
3310  case T_DATA:
3311  /* Call the compaction callback, if it exists */
3312  {
3313  void *const ptr = RTYPEDDATA_P(obj) ? RTYPEDDATA_GET_DATA(obj) : DATA_PTR(obj);
3314  if (ptr) {
3315  if (RTYPEDDATA_P(obj) && gc_declarative_marking_p(RTYPEDDATA(obj)->type)) {
3316  size_t *offset_list = TYPED_DATA_REFS_OFFSET_LIST(obj);
3317 
3318  for (size_t offset = *offset_list; offset != RUBY_REF_END; offset = *offset_list++) {
3319  VALUE *ref = (VALUE *)((char *)ptr + offset);
3320  if (SPECIAL_CONST_P(*ref)) continue;
3321  *ref = rb_gc_impl_location(objspace, *ref);
3322  }
3323  }
3324  else if (RTYPEDDATA_P(obj)) {
3325  RUBY_DATA_FUNC compact_func = RTYPEDDATA(obj)->type->function.dcompact;
3326  if (compact_func) (*compact_func)(ptr);
3327  }
3328  }
3329  }
3330  break;
3331 
3332  case T_OBJECT:
3333  gc_ref_update_object(objspace, obj);
3334  break;
3335 
3336  case T_FILE:
3337  if (RFILE(obj)->fptr) {
3338  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->self);
3339  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->pathv);
3340  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->tied_io_for_writing);
3341  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->writeconv_asciicompat);
3342  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->writeconv_pre_ecopts);
3343  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->encs.ecopts);
3344  UPDATE_IF_MOVED(objspace, RFILE(obj)->fptr->write_lock);
3345  }
3346  break;
3347  case T_REGEXP:
3348  UPDATE_IF_MOVED(objspace, RREGEXP(obj)->src);
3349  break;
3350 
3351  case T_SYMBOL:
3352  UPDATE_IF_MOVED(objspace, RSYMBOL(obj)->fstr);
3353  break;
3354 
3355  case T_FLOAT:
3356  case T_BIGNUM:
3357  break;
3358 
3359  case T_MATCH:
3360  UPDATE_IF_MOVED(objspace, RMATCH(obj)->regexp);
3361 
3362  if (RMATCH(obj)->str) {
3363  UPDATE_IF_MOVED(objspace, RMATCH(obj)->str);
3364  }
3365  break;
3366 
3367  case T_RATIONAL:
3368  UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->num);
3369  UPDATE_IF_MOVED(objspace, RRATIONAL(obj)->den);
3370  break;
3371 
3372  case T_COMPLEX:
3373  UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->real);
3374  UPDATE_IF_MOVED(objspace, RCOMPLEX(obj)->imag);
3375 
3376  break;
3377 
3378  case T_STRUCT:
3379  {
3380  long i, len = RSTRUCT_LEN(obj);
3381  VALUE *ptr = (VALUE *)RSTRUCT_CONST_PTR(obj);
3382 
3383  for (i = 0; i < len; i++) {
3384  UPDATE_IF_MOVED(objspace, ptr[i]);
3385  }
3386  }
3387  break;
3388  default:
3389  rb_bug("unreachable");
3390  break;
3391  }
3392 
3393  UPDATE_IF_MOVED(objspace, RBASIC(obj)->klass);
3394 }
3395 
3396 VALUE
3398 {
3399  rb_gc();
3400  return Qnil;
3401 }
3402 
3403 void
3404 rb_gc(void)
3405 {
3406  unless_objspace(objspace) { return; }
3407 
3408  rb_gc_impl_start(objspace, true, true, true, false);
3409 }
3410 
3411 int
3413 {
3414  unless_objspace(objspace) { return FALSE; }
3415 
3416  return rb_gc_impl_during_gc_p(objspace);
3417 }
3418 
3419 size_t
3421 {
3422  return rb_gc_impl_gc_count(rb_gc_get_objspace());
3423 }
3424 
3425 static VALUE
3426 gc_count(rb_execution_context_t *ec, VALUE self)
3427 {
3428  return SIZET2NUM(rb_gc_count());
3429 }
3430 
3431 VALUE
3433 {
3434  if (!SYMBOL_P(key) && !RB_TYPE_P(key, T_HASH)) {
3435  rb_raise(rb_eTypeError, "non-hash or symbol given");
3436  }
3437 
3438  VALUE val = rb_gc_impl_latest_gc_info(rb_gc_get_objspace(), key);
3439 
3440  if (val == Qundef) {
3441  rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
3442  }
3443 
3444  return val;
3445 }
3446 
3447 static VALUE
3448 gc_stat(rb_execution_context_t *ec, VALUE self, VALUE arg) // arg is (nil || hash || symbol)
3449 {
3450  if (NIL_P(arg)) {
3451  arg = rb_hash_new();
3452  }
3453  else if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) {
3454  rb_raise(rb_eTypeError, "non-hash or symbol given");
3455  }
3456 
3457  VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3458 
3459  if (ret == Qundef) {
3460  GC_ASSERT(SYMBOL_P(arg));
3461 
3462  rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
3463  }
3464 
3465  return ret;
3466 }
3467 
3468 size_t
3470 {
3471  if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) {
3472  rb_raise(rb_eTypeError, "non-hash or symbol given");
3473  }
3474 
3475  VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg);
3476 
3477  if (ret == Qundef) {
3478  GC_ASSERT(SYMBOL_P(arg));
3479 
3480  rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
3481  }
3482 
3483  if (SYMBOL_P(arg)) {
3484  return NUM2SIZET(ret);
3485  }
3486  else {
3487  return 0;
3488  }
3489 }
3490 
3491 static VALUE
3492 gc_stat_heap(rb_execution_context_t *ec, VALUE self, VALUE heap_name, VALUE arg)
3493 {
3494  if (NIL_P(arg)) {
3495  arg = rb_hash_new();
3496  }
3497 
3498  if (NIL_P(heap_name)) {
3499  if (!RB_TYPE_P(arg, T_HASH)) {
3500  rb_raise(rb_eTypeError, "non-hash given");
3501  }
3502  }
3503  else if (FIXNUM_P(heap_name)) {
3504  if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) {
3505  rb_raise(rb_eTypeError, "non-hash or symbol given");
3506  }
3507  }
3508  else {
3509  rb_raise(rb_eTypeError, "heap_name must be nil or an Integer");
3510  }
3511 
3512  VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
3513 
3514  if (ret == Qundef) {
3515  GC_ASSERT(SYMBOL_P(arg));
3516 
3517  rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
3518  }
3519 
3520  return ret;
3521 }
3522 
3523 static VALUE
3524 gc_config_get(rb_execution_context_t *ec, VALUE self)
3525 {
3526  VALUE cfg_hash = rb_gc_impl_config_get(rb_gc_get_objspace());
3527  rb_hash_aset(cfg_hash, sym("implementation"), rb_fstring_cstr(rb_gc_impl_active_gc_name()));
3528 
3529  return cfg_hash;
3530 }
3531 
3532 static VALUE
3533 gc_config_set(rb_execution_context_t *ec, VALUE self, VALUE hash)
3534 {
3535  void *objspace = rb_gc_get_objspace();
3536 
3537  rb_gc_impl_config_set(objspace, hash);
3538 
3539  return rb_gc_impl_config_get(objspace);
3540 }
3541 
3542 static VALUE
3543 gc_stress_get(rb_execution_context_t *ec, VALUE self)
3544 {
3545  return rb_gc_impl_stress_get(rb_gc_get_objspace());
3546 }
3547 
3548 static VALUE
3549 gc_stress_set_m(rb_execution_context_t *ec, VALUE self, VALUE flag)
3550 {
3551  rb_gc_impl_stress_set(rb_gc_get_objspace(), flag);
3552 
3553  return flag;
3554 }
3555 
3556 void
3557 rb_gc_initial_stress_set(VALUE flag)
3558 {
3559  initial_stress = flag;
3560 }
3561 
3562 size_t *
3563 rb_gc_heap_sizes(void)
3564 {
3565  return rb_gc_impl_heap_sizes(rb_gc_get_objspace());
3566 }
3567 
3568 VALUE
3570 {
3571  return rb_objspace_gc_enable(rb_gc_get_objspace());
3572 }
3573 
3574 VALUE
3575 rb_objspace_gc_enable(void *objspace)
3576 {
3577  bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3578  rb_gc_impl_gc_enable(objspace);
3579  return RBOOL(disabled);
3580 }
3581 
3582 static VALUE
3583 gc_enable(rb_execution_context_t *ec, VALUE _)
3584 {
3585  return rb_gc_enable();
3586 }
3587 
3588 static VALUE
3589 gc_disable_no_rest(void *objspace)
3590 {
3591  bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3592  rb_gc_impl_gc_disable(objspace, false);
3593  return RBOOL(disabled);
3594 }
3595 
3596 VALUE
3597 rb_gc_disable_no_rest(void)
3598 {
3599  return gc_disable_no_rest(rb_gc_get_objspace());
3600 }
3601 
3602 VALUE
3604 {
3605  return rb_objspace_gc_disable(rb_gc_get_objspace());
3606 }
3607 
3608 VALUE
3609 rb_objspace_gc_disable(void *objspace)
3610 {
3611  bool disabled = !rb_gc_impl_gc_enabled_p(objspace);
3612  rb_gc_impl_gc_disable(objspace, true);
3613  return RBOOL(disabled);
3614 }
3615 
3616 static VALUE
3617 gc_disable(rb_execution_context_t *ec, VALUE _)
3618 {
3619  return rb_gc_disable();
3620 }
3621 
3622 // TODO: think about moving ruby_gc_set_params into Init_heap or Init_gc
3623 void
3624 ruby_gc_set_params(void)
3625 {
3626  rb_gc_impl_set_params(rb_gc_get_objspace());
3627 }
3628 
3629 void
3630 rb_objspace_reachable_objects_from(VALUE obj, void (func)(VALUE, void *), void *data)
3631 {
3632  RB_VM_LOCK_ENTER();
3633  {
3634  if (rb_gc_impl_during_gc_p(rb_gc_get_objspace())) rb_bug("rb_objspace_reachable_objects_from() is not supported while during GC");
3635 
3636  if (!RB_SPECIAL_CONST_P(obj)) {
3637  rb_vm_t *vm = GET_VM();
3638  struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3639  struct gc_mark_func_data_struct mfd = {
3640  .mark_func = func,
3641  .data = data,
3642  };
3643 
3644  vm->gc.mark_func_data = &mfd;
3645  rb_gc_mark_children(rb_gc_get_objspace(), obj);
3646  vm->gc.mark_func_data = prev_mfd;
3647  }
3648  }
3649  RB_VM_LOCK_LEAVE();
3650 }
3651 
3653  const char *category;
3654  void (*func)(const char *category, VALUE, void *);
3655  void *data;
3656 };
3657 
3658 static void
3659 root_objects_from(VALUE obj, void *ptr)
3660 {
3661  const struct root_objects_data *data = (struct root_objects_data *)ptr;
3662  (*data->func)(data->category, obj, data->data);
3663 }
3664 
3665 void
3666 rb_objspace_reachable_objects_from_root(void (func)(const char *category, VALUE, void *), void *passing_data)
3667 {
3668  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");
3669 
3670  rb_vm_t *vm = GET_VM();
3671 
3672  struct root_objects_data data = {
3673  .func = func,
3674  .data = passing_data,
3675  };
3676 
3677  struct gc_mark_func_data_struct *prev_mfd = vm->gc.mark_func_data;
3678  struct gc_mark_func_data_struct mfd = {
3679  .mark_func = root_objects_from,
3680  .data = &data,
3681  };
3682 
3683  vm->gc.mark_func_data = &mfd;
3684  rb_gc_mark_roots(rb_gc_get_objspace(), &data.category);
3685  vm->gc.mark_func_data = prev_mfd;
3686 }
3687 
3688 /*
3689  ------------------------------ DEBUG ------------------------------
3690 */
3691 
3692 static const char *
3693 type_name(int type, VALUE obj)
3694 {
3695  switch (type) {
3696 #define TYPE_NAME(t) case (t): return #t;
3697  TYPE_NAME(T_NONE);
3698  TYPE_NAME(T_OBJECT);
3699  TYPE_NAME(T_CLASS);
3700  TYPE_NAME(T_MODULE);
3701  TYPE_NAME(T_FLOAT);
3702  TYPE_NAME(T_STRING);
3703  TYPE_NAME(T_REGEXP);
3704  TYPE_NAME(T_ARRAY);
3705  TYPE_NAME(T_HASH);
3706  TYPE_NAME(T_STRUCT);
3707  TYPE_NAME(T_BIGNUM);
3708  TYPE_NAME(T_FILE);
3709  TYPE_NAME(T_MATCH);
3710  TYPE_NAME(T_COMPLEX);
3711  TYPE_NAME(T_RATIONAL);
3712  TYPE_NAME(T_NIL);
3713  TYPE_NAME(T_TRUE);
3714  TYPE_NAME(T_FALSE);
3715  TYPE_NAME(T_SYMBOL);
3716  TYPE_NAME(T_FIXNUM);
3717  TYPE_NAME(T_UNDEF);
3718  TYPE_NAME(T_IMEMO);
3719  TYPE_NAME(T_ICLASS);
3720  TYPE_NAME(T_MOVED);
3721  TYPE_NAME(T_ZOMBIE);
3722  case T_DATA:
3723  if (obj && rb_objspace_data_type_name(obj)) {
3724  return rb_objspace_data_type_name(obj);
3725  }
3726  return "T_DATA";
3727 #undef TYPE_NAME
3728  }
3729  return "unknown";
3730 }
3731 
3732 static const char *
3733 obj_type_name(VALUE obj)
3734 {
3735  return type_name(TYPE(obj), obj);
3736 }
3737 
3738 const char *
3739 rb_method_type_name(rb_method_type_t type)
3740 {
3741  switch (type) {
3742  case VM_METHOD_TYPE_ISEQ: return "iseq";
3743  case VM_METHOD_TYPE_ATTRSET: return "attrest";
3744  case VM_METHOD_TYPE_IVAR: return "ivar";
3745  case VM_METHOD_TYPE_BMETHOD: return "bmethod";
3746  case VM_METHOD_TYPE_ALIAS: return "alias";
3747  case VM_METHOD_TYPE_REFINED: return "refined";
3748  case VM_METHOD_TYPE_CFUNC: return "cfunc";
3749  case VM_METHOD_TYPE_ZSUPER: return "zsuper";
3750  case VM_METHOD_TYPE_MISSING: return "missing";
3751  case VM_METHOD_TYPE_OPTIMIZED: return "optimized";
3752  case VM_METHOD_TYPE_UNDEF: return "undef";
3753  case VM_METHOD_TYPE_NOTIMPLEMENTED: return "notimplemented";
3754  }
3755  rb_bug("rb_method_type_name: unreachable (type: %d)", type);
3756 }
3757 
3758 static void
3759 rb_raw_iseq_info(char *const buff, const size_t buff_size, const rb_iseq_t *iseq)
3760 {
3761  if (buff_size > 0 && ISEQ_BODY(iseq) && ISEQ_BODY(iseq)->location.label && !RB_TYPE_P(ISEQ_BODY(iseq)->location.pathobj, T_MOVED)) {
3762  VALUE path = rb_iseq_path(iseq);
3763  int n = ISEQ_BODY(iseq)->location.first_lineno;
3764  snprintf(buff, buff_size, " %s@%s:%d",
3765  RSTRING_PTR(ISEQ_BODY(iseq)->location.label),
3766  RSTRING_PTR(path), n);
3767  }
3768 }
3769 
3770 static int
3771 str_len_no_raise(VALUE str)
3772 {
3773  long len = RSTRING_LEN(str);
3774  if (len < 0) return 0;
3775  if (len > INT_MAX) return INT_MAX;
3776  return (int)len;
3777 }
3778 
3779 #define BUFF_ARGS buff + pos, buff_size - pos
3780 #define APPEND_F(...) if ((pos += snprintf(BUFF_ARGS, "" __VA_ARGS__)) >= buff_size) goto end
3781 #define APPEND_S(s) do { \
3782  if ((pos + (int)rb_strlen_lit(s)) >= buff_size) { \
3783  goto end; \
3784  } \
3785  else { \
3786  memcpy(buff + pos, (s), rb_strlen_lit(s) + 1); \
3787  } \
3788  } while (0)
3789 #define C(c, s) ((c) != 0 ? (s) : " ")
3790 
3791 static size_t
3792 rb_raw_obj_info_common(char *const buff, const size_t buff_size, const VALUE obj)
3793 {
3794  size_t pos = 0;
3795 
3796  if (SPECIAL_CONST_P(obj)) {
3797  APPEND_F("%s", obj_type_name(obj));
3798 
3799  if (FIXNUM_P(obj)) {
3800  APPEND_F(" %ld", FIX2LONG(obj));
3801  }
3802  else if (SYMBOL_P(obj)) {
3803  APPEND_F(" %s", rb_id2name(SYM2ID(obj)));
3804  }
3805  }
3806  else {
3807  // const int age = RVALUE_AGE_GET(obj);
3808 
3809  if (rb_gc_impl_pointer_to_heap_p(rb_gc_get_objspace(), (void *)obj)) {
3810  // TODO: fixme
3811  // APPEND_F("%p [%d%s%s%s%s%s%s] %s ",
3812  // (void *)obj, age,
3813  // C(RVALUE_UNCOLLECTIBLE_BITMAP(obj), "L"),
3814  // C(RVALUE_MARK_BITMAP(obj), "M"),
3815  // C(RVALUE_PIN_BITMAP(obj), "P"),
3816  // C(RVALUE_MARKING_BITMAP(obj), "R"),
3817  // C(RVALUE_WB_UNPROTECTED_BITMAP(obj), "U"),
3818  // C(rb_objspace_garbage_object_p(obj), "G"),
3819  // obj_type_name(obj));
3820  }
3821  else {
3822  /* fake */
3823  // APPEND_F("%p [%dXXXX] %s",
3824  // (void *)obj, age,
3825  // obj_type_name(obj));
3826  }
3827 
3828  if (internal_object_p(obj)) {
3829  /* ignore */
3830  }
3831  else if (RBASIC(obj)->klass == 0) {
3832  APPEND_S("(temporary internal)");
3833  }
3834  else if (RTEST(RBASIC(obj)->klass)) {
3835  VALUE class_path = rb_class_path_cached(RBASIC(obj)->klass);
3836  if (!NIL_P(class_path)) {
3837  APPEND_F("(%s)", RSTRING_PTR(class_path));
3838  }
3839  }
3840  }
3841  end:
3842 
3843  return pos;
3844 }
3845 
3846 const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
3847 
3848 static size_t
3849 rb_raw_obj_info_buitin_type(char *const buff, const size_t buff_size, const VALUE obj, size_t pos)
3850 {
3851  if (LIKELY(pos < buff_size) && !SPECIAL_CONST_P(obj)) {
3852  const enum ruby_value_type type = BUILTIN_TYPE(obj);
3853 
3854  switch (type) {
3855  case T_NODE:
3856  UNEXPECTED_NODE(rb_raw_obj_info);
3857  break;
3858  case T_ARRAY:
3859  if (ARY_SHARED_P(obj)) {
3860  APPEND_S("shared -> ");
3861  rb_raw_obj_info(BUFF_ARGS, ARY_SHARED_ROOT(obj));
3862  }
3863  else if (ARY_EMBED_P(obj)) {
3864  APPEND_F("[%s%s] len: %ld (embed)",
3865  C(ARY_EMBED_P(obj), "E"),
3866  C(ARY_SHARED_P(obj), "S"),
3867  RARRAY_LEN(obj));
3868  }
3869  else {
3870  APPEND_F("[%s%s] len: %ld, capa:%ld ptr:%p",
3871  C(ARY_EMBED_P(obj), "E"),
3872  C(ARY_SHARED_P(obj), "S"),
3873  RARRAY_LEN(obj),
3874  ARY_EMBED_P(obj) ? -1L : RARRAY(obj)->as.heap.aux.capa,
3875  (void *)RARRAY_CONST_PTR(obj));
3876  }
3877  break;
3878  case T_STRING: {
3879  if (STR_SHARED_P(obj)) {
3880  APPEND_F(" [shared] len: %ld", RSTRING_LEN(obj));
3881  }
3882  else {
3883  if (STR_EMBED_P(obj)) APPEND_S(" [embed]");
3884 
3885  APPEND_F(" len: %ld, capa: %" PRIdSIZE, RSTRING_LEN(obj), rb_str_capacity(obj));
3886  }
3887  APPEND_F(" \"%.*s\"", str_len_no_raise(obj), RSTRING_PTR(obj));
3888  break;
3889  }
3890  case T_SYMBOL: {
3891  VALUE fstr = RSYMBOL(obj)->fstr;
3892  ID id = RSYMBOL(obj)->id;
3893  if (RB_TYPE_P(fstr, T_STRING)) {
3894  APPEND_F(":%s id:%d", RSTRING_PTR(fstr), (unsigned int)id);
3895  }
3896  else {
3897  APPEND_F("(%p) id:%d", (void *)fstr, (unsigned int)id);
3898  }
3899  break;
3900  }
3901  case T_MOVED: {
3902  APPEND_F("-> %p", (void*)rb_gc_impl_location(rb_gc_get_objspace(), obj));
3903  break;
3904  }
3905  case T_HASH: {
3906  APPEND_F("[%c] %"PRIdSIZE,
3907  RHASH_AR_TABLE_P(obj) ? 'A' : 'S',
3908  RHASH_SIZE(obj));
3909  break;
3910  }
3911  case T_CLASS:
3912  case T_MODULE:
3913  {
3914  VALUE class_path = rb_class_path_cached(obj);
3915  if (!NIL_P(class_path)) {
3916  APPEND_F("%s", RSTRING_PTR(class_path));
3917  }
3918  else {
3919  APPEND_S("(anon)");
3920  }
3921  break;
3922  }
3923  case T_ICLASS:
3924  {
3925  VALUE class_path = rb_class_path_cached(RBASIC_CLASS(obj));
3926  if (!NIL_P(class_path)) {
3927  APPEND_F("src:%s", RSTRING_PTR(class_path));
3928  }
3929  break;
3930  }
3931  case T_OBJECT:
3932  {
3933  if (rb_shape_obj_too_complex(obj)) {
3934  size_t hash_len = rb_st_table_size(ROBJECT_IV_HASH(obj));
3935  APPEND_F("(too_complex) len:%zu", hash_len);
3936  }
3937  else {
3938  uint32_t len = ROBJECT_IV_CAPACITY(obj);
3939 
3940  if (RBASIC(obj)->flags & ROBJECT_EMBED) {
3941  APPEND_F("(embed) len:%d", len);
3942  }
3943  else {
3944  VALUE *ptr = ROBJECT_IVPTR(obj);
3945  APPEND_F("len:%d ptr:%p", len, (void *)ptr);
3946  }
3947  }
3948  }
3949  break;
3950  case T_DATA: {
3951  const struct rb_block *block;
3952  const rb_iseq_t *iseq;
3953  if (rb_obj_is_proc(obj) &&
3954  (block = vm_proc_block(obj)) != NULL &&
3955  (vm_block_type(block) == block_type_iseq) &&
3956  (iseq = vm_block_iseq(block)) != NULL) {
3957  rb_raw_iseq_info(BUFF_ARGS, iseq);
3958  }
3959  else if (rb_ractor_p(obj)) {
3960  rb_ractor_t *r = (void *)DATA_PTR(obj);
3961  if (r) {
3962  APPEND_F("r:%d", r->pub.id);
3963  }
3964  }
3965  else {
3966  const char * const type_name = rb_objspace_data_type_name(obj);
3967  if (type_name) {
3968  APPEND_F("%s", type_name);
3969  }
3970  }
3971  break;
3972  }
3973  case T_IMEMO: {
3974  APPEND_F("<%s> ", rb_imemo_name(imemo_type(obj)));
3975 
3976  switch (imemo_type(obj)) {
3977  case imemo_ment:
3978  {
3979  const rb_method_entry_t *me = (const rb_method_entry_t *)obj;
3980 
3981  APPEND_F(":%s (%s%s%s%s) type:%s aliased:%d owner:%p defined_class:%p",
3982  rb_id2name(me->called_id),
3983  METHOD_ENTRY_VISI(me) == METHOD_VISI_PUBLIC ? "pub" :
3984  METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE ? "pri" : "pro",
3985  METHOD_ENTRY_COMPLEMENTED(me) ? ",cmp" : "",
3986  METHOD_ENTRY_CACHED(me) ? ",cc" : "",
3987  METHOD_ENTRY_INVALIDATED(me) ? ",inv" : "",
3988  me->def ? rb_method_type_name(me->def->type) : "NULL",
3989  me->def ? me->def->aliased : -1,
3990  (void *)me->owner, // obj_info(me->owner),
3991  (void *)me->defined_class); //obj_info(me->defined_class)));
3992 
3993  if (me->def) {
3994  switch (me->def->type) {
3995  case VM_METHOD_TYPE_ISEQ:
3996  APPEND_S(" (iseq:");
3997  rb_raw_obj_info(BUFF_ARGS, (VALUE)me->def->body.iseq.iseqptr);
3998  APPEND_S(")");
3999  break;
4000  default:
4001  break;
4002  }
4003  }
4004 
4005  break;
4006  }
4007  case imemo_iseq: {
4008  const rb_iseq_t *iseq = (const rb_iseq_t *)obj;
4009  rb_raw_iseq_info(BUFF_ARGS, iseq);
4010  break;
4011  }
4012  case imemo_callinfo:
4013  {
4014  const struct rb_callinfo *ci = (const struct rb_callinfo *)obj;
4015  APPEND_F("(mid:%s, flag:%x argc:%d, kwarg:%s)",
4016  rb_id2name(vm_ci_mid(ci)),
4017  vm_ci_flag(ci),
4018  vm_ci_argc(ci),
4019  vm_ci_kwarg(ci) ? "available" : "NULL");
4020  break;
4021  }
4022  case imemo_callcache:
4023  {
4024  const struct rb_callcache *cc = (const struct rb_callcache *)obj;
4025  VALUE class_path = cc->klass ? rb_class_path_cached(cc->klass) : Qnil;
4026  const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
4027 
4028  APPEND_F("(klass:%s cme:%s%s (%p) call:%p",
4029  NIL_P(class_path) ? (cc->klass ? "??" : "<NULL>") : RSTRING_PTR(class_path),
4030  cme ? rb_id2name(cme->called_id) : "<NULL>",
4031  cme ? (METHOD_ENTRY_INVALIDATED(cme) ? " [inv]" : "") : "",
4032  (void *)cme,
4033  (void *)(uintptr_t)vm_cc_call(cc));
4034  break;
4035  }
4036  default:
4037  break;
4038  }
4039  }
4040  default:
4041  break;
4042  }
4043  }
4044  end:
4045 
4046  return pos;
4047 }
4048 
4049 #undef C
4050 
4051 #define asan_unpoisoning_object(obj) \
4052  for (void *poisoned = asan_unpoison_object_temporary(obj), \
4053  *unpoisoning = &poisoned; /* flag to loop just once */ \
4054  unpoisoning; \
4055  unpoisoning = asan_poison_object_restore(obj, poisoned))
4056 
4057 const char *
4058 rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj)
4059 {
4060  asan_unpoisoning_object(obj) {
4061  size_t pos = rb_raw_obj_info_common(buff, buff_size, obj);
4062  pos = rb_raw_obj_info_buitin_type(buff, buff_size, obj, pos);
4063  if (pos >= buff_size) {} // truncated
4064  }
4065 
4066  return buff;
4067 }
4068 
4069 #undef APPEND_S
4070 #undef APPEND_F
4071 #undef BUFF_ARGS
4072 
4073 #if RGENGC_OBJ_INFO
4074 #define OBJ_INFO_BUFFERS_NUM 10
4075 #define OBJ_INFO_BUFFERS_SIZE 0x100
4076 static rb_atomic_t obj_info_buffers_index = 0;
4077 static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE];
4078 
4079 /* Increments *var atomically and resets *var to 0 when maxval is
4080  * reached. Returns the wraparound old *var value (0...maxval). */
4081 static rb_atomic_t
4082 atomic_inc_wraparound(rb_atomic_t *var, const rb_atomic_t maxval)
4083 {
4084  rb_atomic_t oldval = RUBY_ATOMIC_FETCH_ADD(*var, 1);
4085  if (RB_UNLIKELY(oldval >= maxval - 1)) { // wraparound *var
4086  const rb_atomic_t newval = oldval + 1;
4087  RUBY_ATOMIC_CAS(*var, newval, newval % maxval);
4088  oldval %= maxval;
4089  }
4090  return oldval;
4091 }
4092 
4093 static const char *
4094 obj_info(VALUE obj)
4095 {
4096  rb_atomic_t index = atomic_inc_wraparound(&obj_info_buffers_index, OBJ_INFO_BUFFERS_NUM);
4097  char *const buff = obj_info_buffers[index];
4098  return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj);
4099 }
4100 #else
4101 static const char *
4102 obj_info(VALUE obj)
4103 {
4104  return obj_type_name(obj);
4105 }
4106 #endif
4107 
4108 /*
4109  ------------------------ Extended allocator ------------------------
4110 */
4111 
4113  VALUE exc;
4114  const char *fmt;
4115  va_list *ap;
4116 };
4117 
4118 static void *
4119 gc_vraise(void *ptr)
4120 {
4121  struct gc_raise_tag *argv = ptr;
4122  rb_vraise(argv->exc, argv->fmt, *argv->ap);
4123  UNREACHABLE_RETURN(NULL);
4124 }
4125 
4126 static void
4127 gc_raise(VALUE exc, const char *fmt, ...)
4128 {
4129  va_list ap;
4130  va_start(ap, fmt);
4131  struct gc_raise_tag argv = {
4132  exc, fmt, &ap,
4133  };
4134 
4135  if (ruby_thread_has_gvl_p()) {
4136  gc_vraise(&argv);
4137  UNREACHABLE;
4138  }
4139  else if (ruby_native_thread_p()) {
4140  rb_thread_call_with_gvl(gc_vraise, &argv);
4141  UNREACHABLE;
4142  }
4143  else {
4144  /* Not in a ruby thread */
4145  fprintf(stderr, "%s", "[FATAL] ");
4146  vfprintf(stderr, fmt, ap);
4147  }
4148 
4149  va_end(ap);
4150  abort();
4151 }
4152 
4153 NORETURN(static void negative_size_allocation_error(const char *));
4154 static void
4155 negative_size_allocation_error(const char *msg)
4156 {
4157  gc_raise(rb_eNoMemError, "%s", msg);
4158 }
4159 
4160 static void *
4161 ruby_memerror_body(void *dummy)
4162 {
4163  rb_memerror();
4164  return 0;
4165 }
4166 
4167 NORETURN(static void ruby_memerror(void));
4169 static void
4170 ruby_memerror(void)
4171 {
4172  if (ruby_thread_has_gvl_p()) {
4173  rb_memerror();
4174  }
4175  else {
4176  if (ruby_native_thread_p()) {
4177  rb_thread_call_with_gvl(ruby_memerror_body, 0);
4178  }
4179  else {
4180  /* no ruby thread */
4181  fprintf(stderr, "[FATAL] failed to allocate memory\n");
4182  }
4183  }
4184  exit(EXIT_FAILURE);
4185 }
4186 
4187 void
4189 {
4190  rb_execution_context_t *ec = GET_EC();
4191  VALUE exc = GET_VM()->special_exceptions[ruby_error_nomemory];
4192 
4193  if (!exc ||
4194  rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4195  fprintf(stderr, "[FATAL] failed to allocate memory\n");
4196  exit(EXIT_FAILURE);
4197  }
4198  if (rb_ec_raised_p(ec, RAISED_NOMEMORY)) {
4199  rb_ec_raised_clear(ec);
4200  }
4201  else {
4202  rb_ec_raised_set(ec, RAISED_NOMEMORY);
4203  exc = ruby_vm_special_exception_copy(exc);
4204  }
4205  ec->errinfo = exc;
4206  EC_JUMP_TAG(ec, TAG_RAISE);
4207 }
4208 
4209 void
4210 rb_malloc_info_show_results(void)
4211 {
4212 }
4213 
4214 void *
4215 ruby_xmalloc(size_t size)
4216 {
4217  if ((ssize_t)size < 0) {
4218  negative_size_allocation_error("too large allocation size");
4219  }
4220 
4221  return rb_gc_impl_malloc(rb_gc_get_objspace(), size);
4222 }
4223 
4224 void
4225 ruby_malloc_size_overflow(size_t count, size_t elsize)
4226 {
4228  "malloc: possible integer overflow (%"PRIuSIZE"*%"PRIuSIZE")",
4229  count, elsize);
4230 }
4231 
4232 void *
4233 ruby_xmalloc2(size_t n, size_t size)
4234 {
4235  return rb_gc_impl_malloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4236 }
4237 
4238 void *
4239 ruby_xcalloc(size_t n, size_t size)
4240 {
4241  return rb_gc_impl_calloc(rb_gc_get_objspace(), xmalloc2_size(n, size));
4242 }
4243 
4244 #ifdef ruby_sized_xrealloc
4245 #undef ruby_sized_xrealloc
4246 #endif
4247 void *
4248 ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size)
4249 {
4250  if ((ssize_t)new_size < 0) {
4251  negative_size_allocation_error("too large allocation size");
4252  }
4253 
4254  return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, new_size, old_size);
4255 }
4256 
4257 void *
4258 ruby_xrealloc(void *ptr, size_t new_size)
4259 {
4260  return ruby_sized_xrealloc(ptr, new_size, 0);
4261 }
4262 
4263 #ifdef ruby_sized_xrealloc2
4264 #undef ruby_sized_xrealloc2
4265 #endif
4266 void *
4267 ruby_sized_xrealloc2(void *ptr, size_t n, size_t size, size_t old_n)
4268 {
4269  size_t len = xmalloc2_size(n, size);
4270  return rb_gc_impl_realloc(rb_gc_get_objspace(), ptr, len, old_n * size);
4271 }
4272 
4273 void *
4274 ruby_xrealloc2(void *ptr, size_t n, size_t size)
4275 {
4276  return ruby_sized_xrealloc2(ptr, n, size, 0);
4277 }
4278 
4279 #ifdef ruby_sized_xfree
4280 #undef ruby_sized_xfree
4281 #endif
4282 void
4283 ruby_sized_xfree(void *x, size_t size)
4284 {
4285  if (LIKELY(x)) {
4286  /* It's possible for a C extension's pthread destructor function set by pthread_key_create
4287  * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in
4288  * that case. */
4289  if (LIKELY(GET_VM())) {
4290  rb_gc_impl_free(rb_gc_get_objspace(), x, size);
4291  }
4292  else {
4293  ruby_mimfree(x);
4294  }
4295  }
4296 }
4297 
4298 void
4299 ruby_xfree(void *x)
4300 {
4301  ruby_sized_xfree(x, 0);
4302 }
4303 
4304 void *
4305 rb_xmalloc_mul_add(size_t x, size_t y, size_t z) /* x * y + z */
4306 {
4307  size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4308  return ruby_xmalloc(w);
4309 }
4310 
4311 void *
4312 rb_xcalloc_mul_add(size_t x, size_t y, size_t z) /* x * y + z */
4313 {
4314  size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4315  return ruby_xcalloc(w, 1);
4316 }
4317 
4318 void *
4319 rb_xrealloc_mul_add(const void *p, size_t x, size_t y, size_t z) /* x * y + z */
4320 {
4321  size_t w = size_mul_add_or_raise(x, y, z, rb_eArgError);
4322  return ruby_xrealloc((void *)p, w);
4323 }
4324 
4325 void *
4326 rb_xmalloc_mul_add_mul(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
4327 {
4328  size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4329  return ruby_xmalloc(u);
4330 }
4331 
4332 void *
4333 rb_xcalloc_mul_add_mul(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
4334 {
4335  size_t u = size_mul_add_mul_or_raise(x, y, z, w, rb_eArgError);
4336  return ruby_xcalloc(u, 1);
4337 }
4338 
4339 /* Mimic ruby_xmalloc, but need not rb_objspace.
4340  * should return pointer suitable for ruby_xfree
4341  */
4342 void *
4343 ruby_mimmalloc(size_t size)
4344 {
4345  void *mem;
4346 #if CALC_EXACT_MALLOC_SIZE
4347  size += sizeof(struct malloc_obj_info);
4348 #endif
4349  mem = malloc(size);
4350 #if CALC_EXACT_MALLOC_SIZE
4351  if (!mem) {
4352  return NULL;
4353  }
4354  else
4355  /* set 0 for consistency of allocated_size/allocations */
4356  {
4357  struct malloc_obj_info *info = mem;
4358  info->size = 0;
4359  mem = info + 1;
4360  }
4361 #endif
4362  return mem;
4363 }
4364 
4365 void *
4366 ruby_mimcalloc(size_t num, size_t size)
4367 {
4368  void *mem;
4369 #if CALC_EXACT_MALLOC_SIZE
4370  struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
4371  if (UNLIKELY(t.left)) {
4372  return NULL;
4373  }
4374  size = t.right + sizeof(struct malloc_obj_info);
4375  mem = calloc1(size);
4376  if (!mem) {
4377  return NULL;
4378  }
4379  else
4380  /* set 0 for consistency of allocated_size/allocations */
4381  {
4382  struct malloc_obj_info *info = mem;
4383  info->size = 0;
4384  mem = info + 1;
4385  }
4386 #else
4387  mem = calloc(num, size);
4388 #endif
4389  return mem;
4390 }
4391 
4392 void
4393 ruby_mimfree(void *ptr)
4394 {
4395 #if CALC_EXACT_MALLOC_SIZE
4396  struct malloc_obj_info *info = (struct malloc_obj_info *)ptr - 1;
4397  ptr = info;
4398 #endif
4399  free(ptr);
4400 }
4401 
4402 void
4404 {
4405  unless_objspace(objspace) { return; }
4406 
4407  rb_gc_impl_adjust_memory_usage(objspace, diff);
4408 }
4409 
4410 const char *
4411 rb_obj_info(VALUE obj)
4412 {
4413  return obj_info(obj);
4414 }
4415 
4416 void
4417 rb_obj_info_dump(VALUE obj)
4418 {
4419  char buff[0x100];
4420  fprintf(stderr, "rb_obj_info_dump: %s\n", rb_raw_obj_info(buff, 0x100, obj));
4421 }
4422 
4423 void
4424 rb_obj_info_dump_loc(VALUE obj, const char *file, int line, const char *func)
4425 {
4426  char buff[0x100];
4427  fprintf(stderr, "<OBJ_INFO:%s@%s:%d> %s\n", func, file, line, rb_raw_obj_info(buff, 0x100, obj));
4428 }
4429 
4430 /*
4431  * Document-module: ObjectSpace
4432  *
4433  * The ObjectSpace module contains a number of routines
4434  * that interact with the garbage collection facility and allow you to
4435  * traverse all living objects with an iterator.
4436  *
4437  * ObjectSpace also provides support for object finalizers, procs that will be
4438  * called after a specific object was destroyed by garbage collection. See
4439  * the documentation for +ObjectSpace.define_finalizer+ for important
4440  * information on how to use this method correctly.
4441  *
4442  * a = "A"
4443  * b = "B"
4444  *
4445  * ObjectSpace.define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
4446  * ObjectSpace.define_finalizer(b, proc {|id| puts "Finalizer two on #{id}" })
4447  *
4448  * a = nil
4449  * b = nil
4450  *
4451  * _produces:_
4452  *
4453  * Finalizer two on 537763470
4454  * Finalizer one on 537763480
4455  */
4456 
4457 /* Document-class: GC::Profiler
4458  *
4459  * The GC profiler provides access to information on GC runs including time,
4460  * length and object space size.
4461  *
4462  * Example:
4463  *
4464  * GC::Profiler.enable
4465  *
4466  * require 'rdoc/rdoc'
4467  *
4468  * GC::Profiler.report
4469  *
4470  * GC::Profiler.disable
4471  *
4472  * See also GC.count, GC.malloc_allocated_size and GC.malloc_allocations
4473  */
4474 
4475 #include "gc.rbinc"
4476 
4477 void
4478 Init_GC(void)
4479 {
4480 #undef rb_intern
4481  malloc_offset = gc_compute_malloc_offset();
4482 
4483  rb_mGC = rb_define_module("GC");
4484 
4485  VALUE rb_mObjSpace = rb_define_module("ObjectSpace");
4486 
4487  rb_define_module_function(rb_mObjSpace, "each_object", os_each_obj, -1);
4488 
4489  rb_define_module_function(rb_mObjSpace, "define_finalizer", define_final, -1);
4490  rb_define_module_function(rb_mObjSpace, "undefine_finalizer", undefine_final, 1);
4491 
4492  rb_define_module_function(rb_mObjSpace, "_id2ref", os_id2ref, 1);
4493 
4494  rb_vm_register_special_exception(ruby_error_nomemory, rb_eNoMemError, "failed to allocate memory");
4495 
4497  rb_define_method(rb_mKernel, "object_id", rb_obj_id, 0);
4498 
4499  rb_define_module_function(rb_mObjSpace, "count_objects", count_objects, -1);
4500 
4501  rb_gc_impl_init();
4502 }
4503 
4504 // Set a name for the anonymous virtual memory area. `addr` is the starting
4505 // address of the area and `size` is its length in bytes. `name` is a
4506 // NUL-terminated human-readable string.
4507 //
4508 // This function is usually called after calling `mmap()`. The human-readable
4509 // annotation helps developers identify the call site of `mmap()` that created
4510 // the memory mapping.
4511 //
4512 // This function currently only works on Linux 5.17 or higher. After calling
4513 // this function, we can see annotations in the form of "[anon:...]" in
4514 // `/proc/self/maps`, where `...` is the content of `name`. This function has
4515 // no effect when called on other platforms.
4516 void
4517 ruby_annotate_mmap(const void *addr, unsigned long size, const char *name)
4518 {
4519 #if defined(__linux__) && defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
4520  // The name length cannot exceed 80 (including the '\0').
4521  RUBY_ASSERT(strlen(name) < 80);
4522  prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);
4523  // We ignore errors in prctl. prctl may set errno to EINVAL for several
4524  // reasons.
4525  // 1. The attr (PR_SET_VMA_ANON_NAME) is not a valid attribute.
4526  // 2. addr is an invalid address.
4527  // 3. The string pointed by name is too long.
4528  // The first error indicates PR_SET_VMA_ANON_NAME is not available, and may
4529  // happen if we run the compiled binary on an old kernel. In theory, all
4530  // other errors should result in a failure. But since EINVAL cannot tell
4531  // the first error from others, and this function is mainly used for
4532  // debugging, we silently ignore the error.
4533  errno = 0;
4534 #endif
4535 }
#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_CAS(var, oldval, newval)
Atomic compare-and-swap.
Definition: atomic.h:140
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:93
#define RB_UNLIKELY(x)
Asserts that the given Boolean expression likely doesn't hold.
Definition: assume.h:50
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:469
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition: fl_type.h:606
@ RUBY_FL_WB_PROTECTED
Definition: fl_type.h:199
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition: class.c:1095
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for a module.
Definition: class.c:2329
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:2635
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
Definition: class.c:2142
#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 FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
Definition: fl_type.h:66
#define ALLOC
Old name of RB_ALLOC.
Definition: memory.h:395
#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:135
#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:122
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition: fl_type.h:132
#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 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 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 FL_TEST
Old name of RB_FL_TEST.
Definition: fl_type.h:131
#define xcalloc
Old name of ruby_xcalloc.
Definition: xmalloc.h:55
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition: fl_type.h:133
#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:2016
int ruby_stack_check(void)
Checks for stack overflow.
Definition: gc.c:2056
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3635
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:1089
VALUE rb_eNoMemError
NoMemoryError exception.
Definition: error.c:1419
VALUE rb_eRangeError
RangeError exception.
Definition: error.c:1412
#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:1408
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition: error.c:466
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1409
size_t rb_obj_embedded_size(uint32_t numiv)
Internal header for Object.
Definition: object.c:98
VALUE rb_mKernel
Kernel module.
Definition: object.c:65
VALUE rb_mGC
GC module.
Definition: gc.c:371
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:247
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:64
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:865
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition: object.c:3188
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
Definition: defines.h:89
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition: string.c:900
void rb_gc_mark(VALUE obj)
Marks an object.
Definition: gc.c:2109
void rb_mark_tbl_no_pin(struct st_table *tbl)
Identical to rb_mark_tbl(), except it marks objects using rb_gc_mark_movable().
Definition: gc.c:2425
void rb_memerror(void)
Triggers out-of-memory error.
Definition: gc.c:4188
size_t rb_gc_stat(VALUE key_or_buf)
Obtains various GC related profiles.
Definition: gc.c:3469
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
Definition: gc.c:2091
VALUE rb_gc_disable(void)
Disables GC.
Definition: gc.c:3603
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
Definition: gc.c:3397
VALUE rb_gc_latest_gc_info(VALUE key_or_buf)
Obtains various info regarding the most recent GC run.
Definition: gc.c:3432
void rb_mark_tbl(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only values of the table and leave their associated keys...
Definition: gc.c:2409
VALUE rb_gc_enable(void)
(Re-) enables GC.
Definition: gc.c:3569
void rb_mark_hash(struct st_table *tbl)
Marks keys and values associated inside of the given table.
Definition: gc.c:2253
int rb_during_gc(void)
Queries if the GC is busy.
Definition: gc.c:3412
void rb_gc_register_address(VALUE *valptr)
Inform the garbage collector that the global or static variable pointed by valptr stores a live Ruby ...
Definition: gc.c:2829
void rb_gc_unregister_address(VALUE *valptr)
Inform the garbage collector that a pointer previously passed to rb_gc_register_address() no longer p...
Definition: gc.c:2853
void rb_gc_mark_maybe(VALUE obj)
Identical to rb_gc_mark(), except it allows the passed value be a non-object.
Definition: gc.c:2121
void rb_gc_writebarrier(VALUE old, VALUE young)
This is the implementation of RB_OBJ_WRITE().
Definition: gc.c:2753
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
Definition: gc.c:3038
void rb_gc_writebarrier_unprotect(VALUE obj)
This is the implementation of RB_OBJ_WB_UNPROTECT().
Definition: gc.c:2759
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Marks objects between the two pointers.
Definition: gc.c:2175
void rb_gc(void)
Triggers a GC process.
Definition: gc.c:3404
void rb_global_variable(VALUE *)
An alias for rb_gc_register_address().
Definition: gc.c:2876
void rb_gc_register_mark_object(VALUE object)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition: gc.c:2820
void rb_gc_update_tbl_refs(st_table *ptr)
Updates references inside of tables.
Definition: gc.c:3000
void rb_mark_set(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only keys of the table and leave their associated values...
Definition: gc.c:2205
VALUE rb_define_finalizer(VALUE obj, VALUE block)
Assigns a finaliser for an object.
Definition: gc.c:1586
void rb_gc_copy_finalizer(VALUE dst, VALUE src)
Copy&paste an object's finaliser to another.
Definition: gc.c:1501
void rb_gc_adjust_memory_usage(ssize_t diff)
Informs that there are external memory usages.
Definition: gc.c:4403
size_t rb_gc_count(void)
Identical to rb_gc_stat(), with "count" parameter.
Definition: gc.c:3420
Defines RBIMPL_HAS_BUILTIN.
void rb_ary_free(VALUE ary)
Destroys the given array for no reason.
Definition: array.c:875
VALUE rb_obj_is_fiber(VALUE obj)
Queries if an object is a fiber.
Definition: cont.c:1180
#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_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
Definition: hash.c:2893
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition: hash.c:1475
VALUE rb_obj_id(VALUE obj)
Finds or creates an integer primary key of the given object.
Definition: gc.c:1739
VALUE rb_memory_id(VALUE obj)
Identical to rb_obj_id(), except it hesitates from allocating a new instance of rb_cInteger.
Definition: gc.c:1706
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition: proc.c:813
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition: proc.c:119
void rb_str_free(VALUE str)
Destroys the given string for no reason.
Definition: string.c:1677
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition: string.c:954
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
Definition: variable.c:302
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
Definition: variable.c:1155
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition: vm_method.c:1286
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:668
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition: vm_method.c:1292
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:2939
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition: symbol.c:992
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:823
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition: symbol.c:970
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
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:1902
bool ruby_free_at_exit_p(void)
Returns whether the Ruby VM will free all memory at shutdown.
Definition: vm.c:4502
VALUE rb_yield(VALUE val)
Yields the block.
Definition: vm_eval.c:1354
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
Definition: maybe_unused.h:33
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:162
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
Definition: cxxanyargs.hpp:432
#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:150
#define RBASIC(obj)
Convenient casting macro.
Definition: rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition: rclass.h:44
#define RCLASS(obj)
Convenient casting macro.
Definition: rclass.h:38
VALUE rb_data_object_wrap(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
This is the primitive way to wrap an existing C struct into RData.
Definition: gc.c:961
#define DATA_PTR(obj)
Convenient getter macro.
Definition: rdata.h:67
VALUE rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
Identical to rb_data_object_wrap(), except it allocates a new data region internally instead of takin...
Definition: gc.c:969
#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_IVPTR(VALUE obj)
Queries the instance variables.
Definition: robject.h:136
#define RREGEXP(obj)
Convenient casting macro.
Definition: rregexp.h:37
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition: rregexp.h:45
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:416
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition: rstring.h:367
#define RSTRING(obj)
Convenient casting macro.
Definition: rstring.h:41
static long RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
Definition: rstruct.h:94
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition: rtypeddata.h:579
VALUE rb_data_typed_object_wrap(VALUE klass, void *datap, const rb_data_type_t *type)
This is the primitive way to wrap an existing C struct into RTypedData.
Definition: gc.c:986
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
Identical to rb_data_typed_object_wrap(), except it allocates a new data region internally instead of...
Definition: gc.c:996
#define RTYPEDDATA(obj)
Convenient casting macro.
Definition: rtypeddata.h:94
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition: rtypeddata.h:602
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition: variable.c:427
#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:5542
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:350
Definition: method.h:62
Definition: constant.h:33
CREF (Class REFerence)
Definition: method.h:44
Definition: class.h:36
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:200
const char * wrap_struct_name
Name of structs of this kind.
Definition: rtypeddata.h:207
VALUE flags
Type-specific behavioural characteristics.
Definition: rtypeddata.h:309
Ruby's IO, metadata and buffers.
Definition: io.h:143
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:54
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:135
Definition: shape.h:44
Internal header for Class.
Definition: class.h:29
Represents the region of a capture group.
Definition: rmatch.h:65
Definition: st.h:79
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
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition: value.h:69
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
void * ruby_xmalloc2(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc(), except it allocates nelems * elemsiz bytes.
Definition: gc.c:4233
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
Definition: gc.c:4215
void ruby_xfree(void *ptr)
Deallocates a storage instance.
Definition: gc.c:4299
void * ruby_xcalloc(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc2(), except it returns a zero-filled storage instance.
Definition: gc.c:4239
void * ruby_xrealloc(void *ptr, size_t newsiz)
Resize the storage instance.
Definition: gc.c:4258
void * ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems * newsiz bytes...
Definition: gc.c:4274