forked from Shopify/ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvm_method.c
More file actions
3569 lines (3130 loc) · 111 KB
/
vm_method.c
File metadata and controls
3569 lines (3130 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is included by vm.c
*/
#include "id_table.h"
#include "yjit.h"
#define METHOD_DEBUG 0
static int vm_redefinition_check_flag(VALUE klass);
static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass);
static inline rb_method_entry_t *lookup_method_table(VALUE klass, ID id);
#define object_id idObject_id
#define added idMethod_added
#define singleton_added idSingleton_method_added
#define removed idMethod_removed
#define singleton_removed idSingleton_method_removed
#define undefined idMethod_undefined
#define singleton_undefined idSingleton_method_undefined
#define ruby_running (GET_VM()->running)
/* int ruby_running = 0; */
static enum rb_id_table_iterator_result
mark_cc_entry_i(VALUE ccs_ptr, void *data)
{
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
VM_ASSERT(vm_ccs_p(ccs));
if (METHOD_ENTRY_INVALIDATED(ccs->cme)) {
/* Before detaching the CCs from this class, we need to invalidate the cc
* since we will no longer be marking the cme on their behalf.
*/
for (int i = 0; i < ccs->len; i++) {
const struct rb_callcache *cc = ccs->entries[i].cc;
if (cc->klass == Qundef) continue; // already invalidated
VM_ASSERT(cc->klass == Qundef || vm_cc_check_cme(cc, ccs->cme));
VM_ASSERT(!vm_cc_super_p(cc) && !vm_cc_refinement_p(cc));
vm_cc_invalidate(cc);
}
ruby_sized_xfree(ccs, vm_ccs_alloc_size(ccs->capa));
return ID_TABLE_DELETE;
}
else {
rb_gc_mark_movable((VALUE)ccs->cme);
for (int i = 0; i < ccs->len; i++) {
const struct rb_callcache *cc = ccs->entries[i].cc;
VM_ASSERT(cc->klass == Qundef || vm_cc_check_cme(cc, ccs->cme));
rb_gc_mark_movable((VALUE)cc);
}
return ID_TABLE_CONTINUE;
}
}
static void
vm_cc_table_mark(void *data)
{
struct rb_id_table *tbl = (struct rb_id_table *)data;
if (tbl) {
rb_id_table_foreach_values(tbl, mark_cc_entry_i, NULL);
}
}
static enum rb_id_table_iterator_result
cc_table_free_i(VALUE ccs_ptr, void *data)
{
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
VM_ASSERT(vm_ccs_p(ccs));
ruby_sized_xfree(ccs, vm_ccs_alloc_size(ccs->capa));
return ID_TABLE_CONTINUE;
}
static void
vm_cc_table_free(void *data)
{
struct rb_id_table *tbl = (struct rb_id_table *)data;
rb_id_table_foreach_values(tbl, cc_table_free_i, NULL);
rb_managed_id_table_type.function.dfree(data);
}
static enum rb_id_table_iterator_result
cc_table_memsize_i(VALUE ccs_ptr, void *data_ptr)
{
size_t *total_size = data_ptr;
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
*total_size += sizeof(*ccs);
*total_size += sizeof(ccs->entries[0]) * ccs->capa;
return ID_TABLE_CONTINUE;
}
static size_t
vm_cc_table_memsize(const void *data)
{
size_t memsize = rb_managed_id_table_type.function.dsize(data);
struct rb_id_table *tbl = (struct rb_id_table *)data;
rb_id_table_foreach_values(tbl, cc_table_memsize_i, &memsize);
return memsize;
}
static enum rb_id_table_iterator_result
compact_cc_entry_i(VALUE ccs_ptr, void *data)
{
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr;
ccs->cme = (const struct rb_callable_method_entry_struct *)rb_gc_location((VALUE)ccs->cme);
VM_ASSERT(vm_ccs_p(ccs));
for (int i=0; i<ccs->len; i++) {
ccs->entries[i].cc = (const struct rb_callcache *)rb_gc_location((VALUE)ccs->entries[i].cc);
}
return ID_TABLE_CONTINUE;
}
static void
vm_cc_table_compact(void *data)
{
struct rb_id_table *tbl = (struct rb_id_table *)data;
rb_id_table_foreach_values(tbl, compact_cc_entry_i, NULL);
}
static const rb_data_type_t cc_table_type = {
.wrap_struct_name = "VM/cc_table",
.function = {
.dmark = vm_cc_table_mark,
.dfree = vm_cc_table_free,
.dsize = vm_cc_table_memsize,
.dcompact = vm_cc_table_compact,
},
.parent = &rb_managed_id_table_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
};
VALUE
rb_vm_cc_table_create(size_t capa)
{
return rb_managed_id_table_create(&cc_table_type, capa);
}
static enum rb_id_table_iterator_result
vm_cc_table_dup_i(ID key, VALUE old_ccs_ptr, void *data)
{
VALUE new_table = (VALUE)data;
struct rb_class_cc_entries *old_ccs = (struct rb_class_cc_entries *)old_ccs_ptr;
if (METHOD_ENTRY_INVALIDATED(old_ccs->cme)) {
// Invalidated CME. This entry will be removed from the old table on
// the next GC mark, so it's unsafe (and undesirable) to copy
return ID_TABLE_CONTINUE;
}
size_t memsize = vm_ccs_alloc_size(old_ccs->capa);
struct rb_class_cc_entries *new_ccs = ruby_xcalloc(1, memsize);
rb_managed_id_table_insert(new_table, key, (VALUE)new_ccs);
// We hold the VM lock, so invalidation should not have happened between
// our earlier invalidation check and now.
VM_ASSERT(!METHOD_ENTRY_INVALIDATED(old_ccs->cme));
memcpy(new_ccs, old_ccs, memsize);
#if VM_CHECK_MODE > 0
new_ccs->debug_sig = ~(VALUE)new_ccs;
#endif
RB_OBJ_WRITTEN(new_table, Qundef, (VALUE)new_ccs->cme);
for (int index = 0; index < new_ccs->len; index++) {
RB_OBJ_WRITTEN(new_table, Qundef, new_ccs->entries[index].cc);
}
return ID_TABLE_CONTINUE;
}
VALUE
rb_vm_cc_table_dup(VALUE old_table)
{
ASSERT_vm_locking();
VALUE new_table = rb_vm_cc_table_create(rb_managed_id_table_size(old_table));
rb_managed_id_table_foreach(old_table, vm_cc_table_dup_i, (void *)new_table);
return new_table;
}
static void
vm_ccs_invalidate(struct rb_class_cc_entries *ccs)
{
for (int i=0; i<ccs->len; i++) {
const struct rb_callcache *cc = ccs->entries[i].cc;
VM_ASSERT(!vm_cc_super_p(cc) && !vm_cc_refinement_p(cc));
vm_cc_invalidate(cc);
}
}
static void
rb_vm_ccs_invalidate_and_free(struct rb_class_cc_entries *ccs)
{
RB_DEBUG_COUNTER_INC(ccs_free);
vm_ccs_invalidate(ccs);
ruby_sized_xfree(ccs, vm_ccs_alloc_size(ccs->capa));
}
void
rb_vm_cc_table_delete(VALUE table, ID mid)
{
VALUE ccs_obj;
if (rb_managed_id_table_lookup(table, mid, &ccs_obj)) {
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_obj;
rb_managed_id_table_delete(table, mid);
rb_vm_ccs_invalidate_and_free(ccs);
}
}
static enum rb_id_table_iterator_result
vm_ccs_dump_i(ID mid, VALUE val, void *data)
{
const struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)val;
fprintf(stderr, " | %s (len:%d) ", rb_id2name(mid), ccs->len);
rp(ccs->cme);
for (int i=0; i<ccs->len; i++) {
rp_m( " | \t", ccs->entries[i].cc);
}
return ID_TABLE_CONTINUE;
}
static void
vm_ccs_dump(VALUE klass, ID target_mid)
{
VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
if (cc_tbl) {
VALUE ccs;
if (target_mid) {
if (rb_managed_id_table_lookup(cc_tbl, target_mid, &ccs)) {
fprintf(stderr, " [CCTB] %p\n", (void *)cc_tbl);
vm_ccs_dump_i(target_mid, ccs, NULL);
}
}
else {
fprintf(stderr, " [CCTB] %p\n", (void *)cc_tbl);
rb_managed_id_table_foreach(cc_tbl, vm_ccs_dump_i, (void *)target_mid);
}
}
}
static enum rb_id_table_iterator_result
vm_cme_dump_i(ID mid, VALUE val, void *data)
{
ID target_mid = (ID)data;
if (target_mid == 0 || mid == target_mid) {
rp_m(" > ", val);
}
return ID_TABLE_CONTINUE;
}
static VALUE
vm_mtbl_dump(VALUE klass, ID target_mid)
{
fprintf(stderr, "# vm_mtbl\n");
while (klass) {
rp_m(" -> ", klass);
VALUE me;
if (RCLASS_M_TBL(klass)) {
if (target_mid != 0) {
if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, &me)) {
rp_m(" [MTBL] ", me);
}
}
else {
fprintf(stderr, " ## RCLASS_M_TBL (%p)\n", (void *)RCLASS_M_TBL(klass));
rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
}
}
else {
fprintf(stderr, " MTBL: NULL\n");
}
if (RCLASS_WRITABLE_CALLABLE_M_TBL(klass)) {
if (target_mid != 0) {
if (rb_id_table_lookup(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), target_mid, &me)) {
rp_m(" [CM**] ", me);
}
}
else {
fprintf(stderr, " ## RCLASS_CALLABLE_M_TBL\n");
rb_id_table_foreach(RCLASS_WRITABLE_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
}
}
if (RCLASS_WRITABLE_CC_TBL(klass)) {
vm_ccs_dump(klass, target_mid);
}
klass = RCLASS_SUPER(klass);
}
return Qnil;
}
void
rb_vm_mtbl_dump(const char *msg, VALUE klass, ID target_mid)
{
fprintf(stderr, "[%s] ", msg);
vm_mtbl_dump(klass, target_mid);
}
static inline void
vm_cme_invalidate(rb_callable_method_entry_t *cme)
{
VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment), "cme: %d", imemo_type((VALUE)cme));
VM_ASSERT(callable_method_entry_p(cme));
METHOD_ENTRY_INVALIDATED_SET(cme);
RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
rb_yjit_cme_invalidate(cme);
rb_zjit_cme_invalidate(cme);
}
static int
rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t arg)
{
((IC) ic)->entry = NULL;
return ST_CONTINUE;
}
void
rb_clear_constant_cache_for_id(ID id)
{
VALUE lookup_result;
rb_vm_t *vm = GET_VM();
if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) {
set_table *ics = (set_table *)lookup_result;
set_table_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
ruby_vm_constant_cache_invalidations += ics->num_entries;
}
rb_yjit_constant_state_changed(id);
rb_zjit_constant_state_changed(id);
}
static void
invalidate_negative_cache(ID mid)
{
VALUE cme;
rb_vm_t *vm = GET_VM();
if (rb_id_table_lookup(vm->negative_cme_table, mid, &cme)) {
rb_id_table_delete(vm->negative_cme_table, mid);
vm_cme_invalidate((rb_callable_method_entry_t *)cme);
RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
}
}
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *src_me);
static const rb_callable_method_entry_t *complemented_callable_method_entry(VALUE klass, ID id);
static const rb_callable_method_entry_t *lookup_overloaded_cme(const rb_callable_method_entry_t *cme);
static void
invalidate_method_cache_in_cc_table(VALUE tbl, ID mid)
{
VALUE ccs_data;
if (tbl && rb_managed_id_table_lookup(tbl, mid, &ccs_data)) {
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_data;
rb_yjit_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
rb_zjit_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
if (NIL_P(ccs->cme->owner)) invalidate_negative_cache(mid);
rb_vm_ccs_invalidate_and_free(ccs);
rb_managed_id_table_delete(tbl, mid);
RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
}
}
static void
invalidate_callable_method_entry_in_callable_m_table(struct rb_id_table *tbl, ID mid)
{
VALUE cme;
if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
rb_zjit_cme_invalidate((rb_callable_method_entry_t *)cme);
rb_id_table_delete(tbl, mid);
RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
}
}
struct invalidate_callable_method_entry_foreach_arg {
VALUE klass;
ID mid;
const rb_method_entry_t *cme;
const rb_method_entry_t *newer;
};
static void
invalidate_callable_method_entry_in_every_m_table_i(rb_classext_t *ext, bool is_prime, VALUE box_value, void *data)
{
st_data_t me;
struct invalidate_callable_method_entry_foreach_arg *arg = (struct invalidate_callable_method_entry_foreach_arg *)data;
struct rb_id_table *tbl = RCLASSEXT_M_TBL(ext);
if (rb_id_table_lookup(tbl, arg->mid, &me) && arg->cme == (const rb_method_entry_t *)me) {
rb_method_table_insert(arg->klass, tbl, arg->mid, arg->newer);
}
}
static void
invalidate_callable_method_entry_in_every_m_table(VALUE klass, ID mid, const rb_callable_method_entry_t *cme)
{
// The argument cme must be invalidated later in the caller side
const rb_method_entry_t *newer = rb_method_entry_clone((const rb_method_entry_t *)cme);
struct invalidate_callable_method_entry_foreach_arg arg = {
.klass = klass,
.mid = mid,
.cme = (const rb_method_entry_t *) cme,
.newer = newer,
};
rb_class_classext_foreach(klass, invalidate_callable_method_entry_in_every_m_table_i, (void *)&arg);
}
static void
invalidate_complemented_method_entry_in_callable_m_table(struct rb_id_table *tbl, ID mid)
{
VALUE cme;
if (tbl && rb_id_table_lookup(tbl, mid, &cme)) {
rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
rb_zjit_cme_invalidate((rb_callable_method_entry_t *)cme);
rb_id_table_delete(tbl, mid);
RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
}
}
static void
clear_method_cache_by_id_in_class(VALUE klass, ID mid)
{
VM_ASSERT_TYPE2(klass, T_CLASS, T_ICLASS);
if (rb_objspace_garbage_object_p(klass)) return;
RB_VM_LOCKING() {
rb_vm_barrier();
if (LIKELY(RCLASS_SUBCLASSES_FIRST(klass) == NULL)) {
// no subclasses
// check only current class
// invalidate CCs
VALUE cc_tbl = RCLASS_WRITABLE_CC_TBL(klass);
invalidate_method_cache_in_cc_table(cc_tbl, mid);
if (RCLASS_CC_TBL_NOT_PRIME_P(klass, cc_tbl)) {
invalidate_method_cache_in_cc_table(RCLASS_PRIME_CC_TBL(klass), mid);
}
// remove from callable_m_tbl, if exists
struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(klass);
invalidate_callable_method_entry_in_callable_m_table(cm_tbl, mid);
if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(klass, cm_tbl)) {
invalidate_callable_method_entry_in_callable_m_table(RCLASS_PRIME_CALLABLE_M_TBL(klass), mid);
}
RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
}
else {
const rb_callable_method_entry_t *cme = complemented_callable_method_entry(klass, mid);
if (cme) {
// invalidate cme if found to invalidate the inline method cache.
if (METHOD_ENTRY_CACHED(cme)) {
if (METHOD_ENTRY_COMPLEMENTED(cme)) {
// do nothing
}
else {
// invalidate cc by invalidating cc->cme
VALUE owner = cme->owner;
VM_ASSERT_TYPE(owner, T_CLASS);
VALUE klass_housing_cme;
if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
klass_housing_cme = owner;
}
else {
klass_housing_cme = RCLASS_ORIGIN(owner);
}
// replace the cme that will be invalid in the all classexts
invalidate_callable_method_entry_in_every_m_table(klass_housing_cme, mid, cme);
}
vm_cme_invalidate((rb_callable_method_entry_t *)cme);
RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
// In case of refinement ME, also invalidate the wrapped ME that
// could be cached at some callsite and is unreachable from any
// RCLASS_WRITABLE_CC_TBL.
if (cme->def->type == VM_METHOD_TYPE_REFINED && cme->def->body.refined.orig_me) {
vm_cme_invalidate((rb_callable_method_entry_t *)cme->def->body.refined.orig_me);
}
if (cme->def->iseq_overload) {
rb_callable_method_entry_t *monly_cme = (rb_callable_method_entry_t *)lookup_overloaded_cme(cme);
if (monly_cme) {
vm_cme_invalidate(monly_cme);
}
}
}
// invalidate complement tbl
if (METHOD_ENTRY_COMPLEMENTED(cme)) {
VALUE defined_class = cme->defined_class;
struct rb_id_table *cm_tbl = RCLASS_WRITABLE_CALLABLE_M_TBL(defined_class);
invalidate_complemented_method_entry_in_callable_m_table(cm_tbl, mid);
if (RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(defined_class, cm_tbl)) {
struct rb_id_table *prime_cm_table = RCLASS_PRIME_CALLABLE_M_TBL(defined_class);
invalidate_complemented_method_entry_in_callable_m_table(prime_cm_table, mid);
}
}
RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
}
else {
invalidate_negative_cache(mid);
}
}
rb_gccct_clear_table(Qnil);
}
}
static void
clear_iclass_method_cache_by_id(VALUE iclass, VALUE d)
{
VM_ASSERT_TYPE(iclass, T_ICLASS);
ID mid = (ID)d;
clear_method_cache_by_id_in_class(iclass, mid);
}
static void
clear_iclass_method_cache_by_id_for_refinements(VALUE klass, VALUE d)
{
if (RB_TYPE_P(klass, T_ICLASS)) {
ID mid = (ID)d;
clear_method_cache_by_id_in_class(klass, mid);
}
}
void
rb_clear_method_cache(VALUE klass_or_module, ID mid)
{
if (RB_TYPE_P(klass_or_module, T_MODULE)) {
VALUE module = klass_or_module; // alias
if (FL_TEST(module, RMODULE_IS_REFINEMENT)) {
VALUE refined_class = rb_refinement_module_get_refined_class(module);
rb_clear_method_cache(refined_class, mid);
rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
rb_clear_all_refinement_method_cache();
}
rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
}
else {
clear_method_cache_by_id_in_class(klass_or_module, mid);
}
}
static enum rb_id_table_iterator_result
invalidate_method_entry_in_iclass_callable_m_tbl(VALUE cme, void *data)
{
vm_cme_invalidate((rb_callable_method_entry_t *)cme);
return ID_TABLE_DELETE;
}
static enum rb_id_table_iterator_result
invalidate_ccs_in_iclass_cc_tbl(VALUE value, void *data)
{
struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)value;
vm_cme_invalidate((rb_callable_method_entry_t *)ccs->cme);
ruby_sized_xfree(ccs, vm_ccs_alloc_size(ccs->capa));
return ID_TABLE_DELETE;
}
void
rb_invalidate_method_caches(struct rb_id_table *cm_tbl, VALUE cc_tbl)
{
if (cm_tbl) {
rb_id_table_foreach_values(cm_tbl, invalidate_method_entry_in_iclass_callable_m_tbl, NULL);
}
if (cc_tbl) {
rb_managed_id_table_foreach_values(cc_tbl, invalidate_ccs_in_iclass_cc_tbl, NULL);
}
}
static st_index_t
vm_ci_hash(VALUE v)
{
const struct rb_callinfo *ci = (const struct rb_callinfo *)v;
st_index_t h;
h = rb_hash_start(ci->mid);
h = rb_hash_uint(h, ci->flag);
h = rb_hash_uint(h, ci->argc);
if (ci->kwarg) {
for (int i = 0; i < ci->kwarg->keyword_len; i++) {
h = rb_hash_uint(h, ci->kwarg->keywords[i]);
}
}
return h;
}
static int
vm_ci_hash_cmp(VALUE v1, VALUE v2)
{
const struct rb_callinfo *ci1 = (const struct rb_callinfo *)v1;
const struct rb_callinfo *ci2 = (const struct rb_callinfo *)v2;
if (ci1->mid != ci2->mid) return 1;
if (ci1->flag != ci2->flag) return 1;
if (ci1->argc != ci2->argc) return 1;
if (ci1->kwarg != NULL) {
VM_ASSERT(ci2->kwarg != NULL); // implied by matching flags
if (ci1->kwarg->keyword_len != ci2->kwarg->keyword_len)
return 1;
for (int i = 0; i < ci1->kwarg->keyword_len; i++) {
if (ci1->kwarg->keywords[i] != ci2->kwarg->keywords[i]) {
return 1;
}
}
}
else {
VM_ASSERT(ci2->kwarg == NULL); // implied by matching flags
}
return 0;
}
static const struct st_hash_type vm_ci_hashtype = {
vm_ci_hash_cmp,
vm_ci_hash
};
static int
ci_lookup_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
{
const struct rb_callinfo *ci = (const struct rb_callinfo *)*key;
st_data_t *ret = (st_data_t *)data;
if (existing) {
if (rb_objspace_garbage_object_p((VALUE)ci)) {
*ret = (st_data_t)NULL;
return ST_DELETE;
}
else {
*ret = *key;
return ST_STOP;
}
}
else {
*key = *value = *ret = (st_data_t)ci;
return ST_CONTINUE;
}
}
const struct rb_callinfo *
rb_vm_ci_lookup(ID mid, unsigned int flag, unsigned int argc, const struct rb_callinfo_kwarg *kwarg)
{
rb_vm_t *vm = GET_VM();
const struct rb_callinfo *ci = NULL;
if (kwarg) {
((struct rb_callinfo_kwarg *)kwarg)->references++;
}
struct rb_callinfo *new_ci = SHAREABLE_IMEMO_NEW(struct rb_callinfo, imemo_callinfo, (VALUE)kwarg);
new_ci->mid = mid;
new_ci->flag = flag;
new_ci->argc = argc;
RB_VM_LOCKING() {
st_table *ci_table = vm->ci_table;
VM_ASSERT(ci_table);
do {
st_update(ci_table, (st_data_t)new_ci, ci_lookup_i, (st_data_t)&ci);
} while (ci == NULL);
}
VM_ASSERT(ci);
return ci;
}
void
rb_vm_ci_free(const struct rb_callinfo *ci)
{
ASSERT_vm_locking();
rb_vm_t *vm = GET_VM();
st_data_t key = (st_data_t)ci;
st_delete(vm->ci_table, &key, NULL);
}
struct cc_refinement_entries {
VALUE *entries;
size_t len;
size_t capa;
};
static void
cc_refinement_set_free(void *ptr)
{
struct cc_refinement_entries *e = ptr;
xfree(e->entries);
}
static size_t
cc_refinement_set_memsize(const void *ptr)
{
const struct cc_refinement_entries *e = ptr;
return e->capa * sizeof(VALUE);
}
static void
cc_refinement_set_compact(void *ptr)
{
struct cc_refinement_entries *e = ptr;
for (size_t i = 0; i < e->len; i++) {
e->entries[i] = rb_gc_location(e->entries[i]);
}
}
static void
cc_refinement_set_handle_weak_references(void *ptr)
{
struct cc_refinement_entries *e = ptr;
size_t write = 0;
for (size_t read = 0; read < e->len; read++) {
if (rb_gc_handle_weak_references_alive_p(e->entries[read])) {
e->entries[write++] = e->entries[read];
}
}
e->len = write;
}
static const rb_data_type_t cc_refinement_set_type = {
"VM/cc_refinement_set",
{
NULL,
cc_refinement_set_free,
cc_refinement_set_memsize,
cc_refinement_set_compact,
cc_refinement_set_handle_weak_references,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
};
VALUE
rb_cc_refinement_set_create(void)
{
struct cc_refinement_entries *e;
VALUE obj = TypedData_Make_Struct(0, struct cc_refinement_entries, &cc_refinement_set_type, e);
e->entries = NULL;
e->len = 0;
e->capa = 0;
rb_gc_declare_weak_references(obj);
return obj;
}
void
rb_vm_insert_cc_refinement(const struct rb_callcache *cc)
{
rb_vm_t *vm = GET_VM();
RB_VM_LOCK_ENTER();
{
struct cc_refinement_entries *e = RTYPEDDATA_GET_DATA(vm->cc_refinement_set);
if (e->len == e->capa) {
size_t new_capa = e->capa == 0 ? 16 : e->capa * 2;
SIZED_REALLOC_N(e->entries, VALUE, new_capa, e->capa);
e->capa = new_capa;
}
e->entries[e->len++] = (VALUE)cc;
// We never mark the cc, but we need to issue a writebarrier so that
// the refinement set can be added to the remembered set
RB_OBJ_WRITTEN(vm->cc_refinement_set, Qundef, (VALUE)cc);
}
RB_VM_LOCK_LEAVE();
}
void
rb_clear_all_refinement_method_cache(void)
{
rb_vm_t *vm = GET_VM();
RB_VM_LOCK_ENTER();
{
struct cc_refinement_entries *e = RTYPEDDATA_GET_DATA(vm->cc_refinement_set);
for (size_t i = 0; i < e->len; i++) {
VALUE v = e->entries[i];
// All objects should be live as weak references are pruned in
// cc_refinement_set_handle_weak_references
VM_ASSERT(rb_gc_pointer_to_heap_p(v));
VM_ASSERT(!rb_objspace_garbage_object_p(v));
const struct rb_callcache *cc = (const struct rb_callcache *)v;
VM_ASSERT(vm_cc_refinement_p(cc));
if (vm_cc_valid(cc)) {
vm_cc_invalidate(cc);
}
}
e->len = 0;
}
RB_VM_LOCK_LEAVE();
rb_yjit_invalidate_all_method_lookup_assumptions();
}
void
rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me)
{
RB_VM_LOCKING() {
rb_method_table_insert0(klass, table, method_id, me, RB_TYPE_P(klass, T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass));
}
}
void
rb_method_table_insert0(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me, bool iclass_shared_mtbl)
{
VALUE table_owner = klass;
if (iclass_shared_mtbl) {
table_owner = RBASIC(table_owner)->klass;
}
VM_ASSERT_TYPE3(table_owner, T_CLASS, T_ICLASS, T_MODULE);
rb_id_table_insert(table, method_id, (VALUE)me);
RB_OBJ_WRITTEN(table_owner, Qundef, (VALUE)me);
}
// rb_f_notimplement has an extra trailing argument to distinguish it from other methods
// at compile-time to override arity to be -1. But the trailing argument introduces a
// signature mismatch between caller and callee, so rb_define_method family inserts a
// method entry with rb_f_notimplement_internal, which has canonical arity=-1 signature,
// instead of rb_f_notimplement.
NORETURN(static VALUE rb_f_notimplement_internal(int argc, const VALUE *argv, VALUE obj));
static VALUE
rb_f_notimplement_internal(int argc, const VALUE *argv, VALUE obj)
{
rb_notimplement();
UNREACHABLE_RETURN(Qnil);
}
VALUE
rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
{
rb_f_notimplement_internal(argc, argv, obj);
}
static void
rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_visibility_t visi)
{
rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, (void *)1, visi);
}
void
rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi)
{
if (argc < -2 || 15 < argc) rb_raise(rb_eArgError, "arity out of range: %d for -2..15", argc);
if (func != (VALUE(*)(ANYARGS))rb_f_notimplement) {
rb_method_cfunc_t opt;
opt.func = func;
opt.argc = argc;
rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
}
else {
rb_define_notimplement_method_id(klass, mid, visi);
}
}
void
rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type opt_type, unsigned int index, rb_method_visibility_t visi)
{
rb_method_optimized_t opt = {
.type = opt_type,
.index = index,
};
rb_add_method(klass, mid, VM_METHOD_TYPE_OPTIMIZED, &opt, visi);
}
static void
method_definition_release(rb_method_definition_t *def)
{
if (def != NULL) {
const unsigned int reference_count_was = RUBY_ATOMIC_FETCH_SUB(def->reference_count, 1);
RUBY_ASSERT_ALWAYS(reference_count_was != 0);
if (reference_count_was == 1) {
if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:1->0 (remove)\n", (void *)def,
rb_id2name(def->original_id));
SIZED_FREE(def);
}
else {
if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d (dec)\n", (void *)def, rb_id2name(def->original_id),
reference_count_was, reference_count_was - 1);
}
}
}
void
rb_method_definition_release(rb_method_definition_t *def)
{
method_definition_release(def);
}
static void delete_overloaded_cme(const rb_callable_method_entry_t *cme);
void
rb_free_method_entry_vm_weak_references(const rb_method_entry_t *me)
{
if (me->def && me->def->iseq_overload) {
delete_overloaded_cme((const rb_callable_method_entry_t *)me);
}
}
void
rb_free_method_entry(const rb_method_entry_t *me)
{
#if USE_ZJIT
if (METHOD_ENTRY_CACHED(me)) {
rb_zjit_cme_free((const rb_callable_method_entry_t *)me);
}
#endif
#if USE_YJIT
// YJIT rb_yjit_root_mark() roots CMEs in `Invariants`,
// to remove from `Invariants` here.
#endif
method_definition_release(me->def);
}
static inline rb_method_entry_t *search_method(VALUE klass, ID id, VALUE *defined_class_ptr);
extern int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2);
static VALUE
(*call_cfunc_invoker_func(int argc))(VALUE recv, int argc, const VALUE *, VALUE (*func)(ANYARGS))
{
if (!GET_THREAD()->ext_config.ractor_safe) {
switch (argc) {
case -2: return &call_cfunc_m2;
case -1: return &call_cfunc_m1;
case 0: return &call_cfunc_0;
case 1: return &call_cfunc_1;
case 2: return &call_cfunc_2;
case 3: return &call_cfunc_3;
case 4: return &call_cfunc_4;
case 5: return &call_cfunc_5;
case 6: return &call_cfunc_6;
case 7: return &call_cfunc_7;
case 8: return &call_cfunc_8;
case 9: return &call_cfunc_9;
case 10: return &call_cfunc_10;
case 11: return &call_cfunc_11;
case 12: return &call_cfunc_12;
case 13: return &call_cfunc_13;
case 14: return &call_cfunc_14;
case 15: return &call_cfunc_15;
default:
rb_bug("unsupported length: %d", argc);
}
}
else {
switch (argc) {
case -2: return &ractor_safe_call_cfunc_m2;
case -1: return &ractor_safe_call_cfunc_m1;
case 0: return &ractor_safe_call_cfunc_0;
case 1: return &ractor_safe_call_cfunc_1;
case 2: return &ractor_safe_call_cfunc_2;
case 3: return &ractor_safe_call_cfunc_3;
case 4: return &ractor_safe_call_cfunc_4;
case 5: return &ractor_safe_call_cfunc_5;
case 6: return &ractor_safe_call_cfunc_6;
case 7: return &ractor_safe_call_cfunc_7;
case 8: return &ractor_safe_call_cfunc_8;
case 9: return &ractor_safe_call_cfunc_9;
case 10: return &ractor_safe_call_cfunc_10;
case 11: return &ractor_safe_call_cfunc_11;
case 12: return &ractor_safe_call_cfunc_12;
case 13: return &ractor_safe_call_cfunc_13;
case 14: return &ractor_safe_call_cfunc_14;
case 15: return &ractor_safe_call_cfunc_15;
default:
rb_bug("unsupported length: %d", argc);
}
}
}
static void