-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm_core.h
2276 lines (1883 loc) · 65.7 KB
/
vm_core.h
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
#ifndef RUBY_VM_CORE_H
#define RUBY_VM_CORE_H
/**********************************************************************
vm_core.h -
$Author$
created at: 04/01/01 19:41:38 JST
Copyright (C) 2004-2007 Koichi Sasada
**********************************************************************/
/*
* Enable check mode.
* 1: enable local assertions.
*/
#ifndef VM_CHECK_MODE
// respect RUBY_DUBUG: if given n is 0, then use RUBY_DEBUG
#define N_OR_RUBY_DEBUG(n) (((n) > 0) ? (n) : RUBY_DEBUG)
#define VM_CHECK_MODE N_OR_RUBY_DEBUG(0)
#endif
/**
* VM Debug Level
*
* debug level:
* 0: no debug output
* 1: show instruction name
* 2: show stack frame when control stack frame is changed
* 3: show stack status
* 4: show register
* 5:
* 10: gc check
*/
#ifndef VMDEBUG
#define VMDEBUG 0
#endif
#if 0
#undef VMDEBUG
#define VMDEBUG 3
#endif
#include "ruby/internal/config.h"
#include <stddef.h>
#include <signal.h>
#include <stdarg.h>
#include "ruby_assert.h"
#define RVALUE_SIZE (sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]))
#if VM_CHECK_MODE > 0
#define VM_ASSERT(expr, ...) \
RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
#define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
#define RUBY_ASSERT_CRITICAL_SECTION
#define RUBY_DEBUG_THREAD_SCHEDULE() rb_thread_schedule()
#else
#define VM_ASSERT(/*expr, */...) ((void)0)
#define VM_UNREACHABLE(func) UNREACHABLE
#define RUBY_DEBUG_THREAD_SCHEDULE()
#endif
#define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
#if defined(RUBY_ASSERT_CRITICAL_SECTION)
/*
# Critical Section Assertions
These assertions are used to ensure that context switching does not occur between two points in the code. In theory,
such code should already be protected by a mutex, but these assertions are used to ensure that the mutex is held.
The specific case where it can be useful is where a mutex is held further up the call stack, and the code in question
may not directly hold the mutex. In this case, the critical section assertions can be used to ensure that the mutex is
held by someone else.
These assertions are only enabled when RUBY_ASSERT_CRITICAL_SECTION is defined, which is only defined if VM_CHECK_MODE
is set.
## Example Usage
```c
RUBY_ASSERT_CRITICAL_SECTION_ENTER();
// ... some code which does not invoke rb_vm_check_ints() ...
RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
```
If `rb_vm_check_ints()` is called between the `RUBY_ASSERT_CRITICAL_SECTION_ENTER()` and
`RUBY_ASSERT_CRITICAL_SECTION_LEAVE()`, a failed assertion will result.
*/
extern int ruby_assert_critical_section_entered;
#define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{ruby_assert_critical_section_entered += 1;}while(false)
#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{VM_ASSERT(ruby_assert_critical_section_entered > 0);ruby_assert_critical_section_entered -= 1;}while(false)
#else
#define RUBY_ASSERT_CRITICAL_SECTION_ENTER()
#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE()
#endif
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
# include "wasm/setjmp.h"
#else
# include <setjmp.h>
#endif
#if defined(__linux__) || defined(__FreeBSD__)
# define RB_THREAD_T_HAS_NATIVE_ID
#endif
#include "ruby/internal/stdbool.h"
#include "ccan/list/list.h"
#include "id.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/basic_operators.h"
#include "internal/sanitizers.h"
#include "internal/serial.h"
#include "internal/vm.h"
#include "method.h"
#include "node.h"
#include "ruby/ruby.h"
#include "ruby/st.h"
#include "ruby_atomic.h"
#include "vm_opts.h"
#include "ruby/thread_native.h"
#if USE_MMTK
#include "internal/mmtk_support.h"
#endif
/*
* implementation selector of get_insn_info algorithm
* 0: linear search
* 1: binary search
* 2: succinct bitvector
*/
#ifndef VM_INSN_INFO_TABLE_IMPL
# define VM_INSN_INFO_TABLE_IMPL 2
#endif
#if defined(NSIG_MAX) /* POSIX issue 8 */
# undef NSIG
# define NSIG NSIG_MAX
#elif defined(_SIG_MAXSIG) /* FreeBSD */
# undef NSIG
# define NSIG _SIG_MAXSIG
#elif defined(_SIGMAX) /* QNX */
# define NSIG (_SIGMAX + 1)
#elif defined(NSIG) /* 99% of everything else */
# /* take it */
#else /* Last resort */
# define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
#endif
#define RUBY_NSIG NSIG
#if defined(SIGCLD)
# define RUBY_SIGCHLD (SIGCLD)
#elif defined(SIGCHLD)
# define RUBY_SIGCHLD (SIGCHLD)
#endif
#if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
# define USE_SIGALTSTACK
void *rb_allocate_sigaltstack(void);
void *rb_register_sigaltstack(void *);
# define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
# define RB_ALTSTACK_FREE(var) free(var)
# define RB_ALTSTACK(var) var
#else /* noop */
# define RB_ALTSTACK_INIT(var, altstack)
# define RB_ALTSTACK_FREE(var)
# define RB_ALTSTACK(var) (0)
#endif
#include THREAD_IMPL_H
#define RUBY_VM_THREAD_MODEL 2
/*****************/
/* configuration */
/*****************/
/* gcc ver. check */
#if defined(__GNUC__) && __GNUC__ >= 2
#if OPT_TOKEN_THREADED_CODE
#if OPT_DIRECT_THREADED_CODE
#undef OPT_DIRECT_THREADED_CODE
#endif
#endif
#else /* defined(__GNUC__) && __GNUC__ >= 2 */
/* disable threaded code options */
#if OPT_DIRECT_THREADED_CODE
#undef OPT_DIRECT_THREADED_CODE
#endif
#if OPT_TOKEN_THREADED_CODE
#undef OPT_TOKEN_THREADED_CODE
#endif
#endif
/* call threaded code */
#if OPT_CALL_THREADED_CODE
#if OPT_DIRECT_THREADED_CODE
#undef OPT_DIRECT_THREADED_CODE
#endif /* OPT_DIRECT_THREADED_CODE */
#endif /* OPT_CALL_THREADED_CODE */
void rb_vm_encoded_insn_data_table_init(void);
typedef unsigned long rb_num_t;
typedef signed long rb_snum_t;
enum ruby_tag_type {
RUBY_TAG_NONE = 0x0,
RUBY_TAG_RETURN = 0x1,
RUBY_TAG_BREAK = 0x2,
RUBY_TAG_NEXT = 0x3,
RUBY_TAG_RETRY = 0x4,
RUBY_TAG_REDO = 0x5,
RUBY_TAG_RAISE = 0x6,
RUBY_TAG_THROW = 0x7,
RUBY_TAG_FATAL = 0x8,
RUBY_TAG_MASK = 0xf
};
#define TAG_NONE RUBY_TAG_NONE
#define TAG_RETURN RUBY_TAG_RETURN
#define TAG_BREAK RUBY_TAG_BREAK
#define TAG_NEXT RUBY_TAG_NEXT
#define TAG_RETRY RUBY_TAG_RETRY
#define TAG_REDO RUBY_TAG_REDO
#define TAG_RAISE RUBY_TAG_RAISE
#define TAG_THROW RUBY_TAG_THROW
#define TAG_FATAL RUBY_TAG_FATAL
#define TAG_MASK RUBY_TAG_MASK
enum ruby_vm_throw_flags {
VM_THROW_NO_ESCAPE_FLAG = 0x8000,
VM_THROW_STATE_MASK = 0xff
};
/* forward declarations */
struct rb_thread_struct;
struct rb_control_frame_struct;
/* iseq data type */
typedef struct rb_compile_option_struct rb_compile_option_t;
union ic_serial_entry {
rb_serial_t raw;
VALUE data[2];
};
#define IMEMO_CONST_CACHE_SHAREABLE IMEMO_FL_USER0
// imemo_constcache
struct iseq_inline_constant_cache_entry {
VALUE flags;
VALUE value; // v0
VALUE _unused1; // v1
VALUE _unused2; // v2
const rb_cref_t *ic_cref; // v3
};
STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
(offsetof(struct iseq_inline_constant_cache_entry, ic_cref) +
sizeof(const rb_cref_t *)) <= RVALUE_SIZE);
struct iseq_inline_constant_cache {
struct iseq_inline_constant_cache_entry *entry;
/**
* A null-terminated list of ids, used to represent a constant's path
* idNULL is used to represent the :: prefix, and 0 is used to donate the end
* of the list.
*
* For example
* FOO {rb_intern("FOO"), 0}
* FOO::BAR {rb_intern("FOO"), rb_intern("BAR"), 0}
* ::FOO {idNULL, rb_intern("FOO"), 0}
* ::FOO::BAR {idNULL, rb_intern("FOO"), rb_intern("BAR"), 0}
*/
const ID *segments;
};
struct iseq_inline_iv_cache_entry {
uintptr_t value; // attr_index in lower bits, dest_shape_id in upper bits
ID iv_set_name;
};
struct iseq_inline_cvar_cache_entry {
struct rb_cvar_class_tbl_entry *entry;
};
union iseq_inline_storage_entry {
struct {
struct rb_thread_struct *running_thread;
VALUE value;
} once;
struct iseq_inline_constant_cache ic_cache;
struct iseq_inline_iv_cache_entry iv_cache;
};
struct rb_calling_info {
const struct rb_call_data *cd;
const struct rb_callcache *cc;
VALUE block_handler;
VALUE recv;
int argc;
bool kw_splat;
VALUE heap_argv;
};
#ifndef VM_ARGC_STACK_MAX
#define VM_ARGC_STACK_MAX 128
#endif
# define CALLING_ARGC(calling) ((calling)->heap_argv ? RARRAY_LENINT((calling)->heap_argv) : (calling)->argc)
struct rb_execution_context_struct;
#if 1
#define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
#else
#define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
#endif
#define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
typedef struct rb_iseq_location_struct {
VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
VALUE base_label; /* String */
VALUE label; /* String */
int first_lineno;
int node_id;
rb_code_location_t code_location;
} rb_iseq_location_t;
#define PATHOBJ_PATH 0
#define PATHOBJ_REALPATH 1
static inline VALUE
pathobj_path(VALUE pathobj)
{
if (RB_TYPE_P(pathobj, T_STRING)) {
return pathobj;
}
else {
VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
return RARRAY_AREF(pathobj, PATHOBJ_PATH);
}
}
static inline VALUE
pathobj_realpath(VALUE pathobj)
{
if (RB_TYPE_P(pathobj, T_STRING)) {
return pathobj;
}
else {
VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
}
}
/* Forward declarations */
typedef uintptr_t iseq_bits_t;
#define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
/* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
#define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
/* instruction sequence type */
enum rb_iseq_type {
ISEQ_TYPE_TOP,
ISEQ_TYPE_METHOD,
ISEQ_TYPE_BLOCK,
ISEQ_TYPE_CLASS,
ISEQ_TYPE_RESCUE,
ISEQ_TYPE_ENSURE,
ISEQ_TYPE_EVAL,
ISEQ_TYPE_MAIN,
ISEQ_TYPE_PLAIN
};
// Attributes specified by Primitive.attr!
enum rb_builtin_attr {
// The iseq does not call methods.
BUILTIN_ATTR_LEAF = 0x01,
// This iseq only contains single `opt_invokebuiltin_delegate_leave` instruction with 0 arguments.
BUILTIN_ATTR_SINGLE_NOARG_LEAF = 0x02,
// This attribute signals JIT to duplicate the iseq for each block iseq so that its `yield` will be monomorphic.
BUILTIN_ATTR_INLINE_BLOCK = 0x04,
// The iseq acts like a C method in backtraces.
BUILTIN_ATTR_C_TRACE = 0x08,
};
typedef VALUE (*rb_jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *);
struct rb_iseq_constant_body {
enum rb_iseq_type type;
unsigned int iseq_size;
VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
/**
* parameter information
*
* def m(a1, a2, ..., aM, # mandatory
* b1=(...), b2=(...), ..., bN=(...), # optional
* *c, # rest
* d1, d2, ..., dO, # post
* e1:(...), e2:(...), ..., eK:(...), # keyword
* **f, # keyword_rest
* &g) # block
* =>
*
* lead_num = M
* opt_num = N
* rest_start = M+N
* post_start = M+N+(*1)
* post_num = O
* keyword_num = K
* block_start = M+N+(*1)+O+K
* keyword_bits = M+N+(*1)+O+K+(&1)
* size = M+N+O+(*1)+K+(&1)+(**1) // parameter size.
*/
struct {
struct {
unsigned int has_lead : 1;
unsigned int has_opt : 1;
unsigned int has_rest : 1;
unsigned int has_post : 1;
unsigned int has_kw : 1;
unsigned int has_kwrest : 1;
unsigned int has_block : 1;
unsigned int ambiguous_param0 : 1; /* {|a|} */
unsigned int accepts_no_kwarg : 1;
unsigned int ruby2_keywords: 1;
unsigned int anon_rest: 1;
unsigned int anon_kwrest: 1;
unsigned int use_block: 1;
unsigned int forwardable: 1;
} flags;
unsigned int size;
int lead_num;
int opt_num;
int rest_start;
int post_start;
int post_num;
int block_start;
const VALUE *opt_table; /* (opt_num + 1) entries. */
/* opt_num and opt_table:
*
* def foo o1=e1, o2=e2, ..., oN=eN
* #=>
* # prologue code
* A1: e1
* A2: e2
* ...
* AN: eN
* AL: body
* opt_num = N
* opt_table = [A1, A2, ..., AN, AL]
*/
const struct rb_iseq_param_keyword {
int num;
int required_num;
int bits_start;
int rest_start;
const ID *table;
VALUE *default_values;
} *keyword;
} param;
rb_iseq_location_t location;
/* insn info, must be freed */
struct iseq_insn_info {
const struct iseq_insn_info_entry *body;
unsigned int *positions;
unsigned int size;
#if VM_INSN_INFO_TABLE_IMPL == 2
struct succ_index_table *succ_index_table;
#endif
} insns_info;
const ID *local_table; /* must free */
/* catch table */
struct iseq_catch_table *catch_table;
/* for child iseq */
const struct rb_iseq_struct *parent_iseq;
struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
union iseq_inline_storage_entry *is_entries; /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
struct {
rb_snum_t flip_count;
VALUE script_lines;
VALUE coverage;
VALUE pc2branchindex;
VALUE *original_iseq;
} variable;
unsigned int local_table_size;
unsigned int ic_size; // Number of IC caches
unsigned int ise_size; // Number of ISE caches
unsigned int ivc_size; // Number of IVC caches
unsigned int icvarc_size; // Number of ICVARC caches
unsigned int ci_size;
unsigned int stack_max; /* for stack overflow check */
unsigned int builtin_attrs; // Union of rb_builtin_attr
bool prism; // ISEQ was generated from prism compiler
union {
iseq_bits_t * list; /* Find references for GC */
iseq_bits_t single;
} mark_bits;
struct rb_id_table *outer_variables;
const rb_iseq_t *mandatory_only_iseq;
#if USE_YJIT
// Function pointer for JIT code on jit_exec()
rb_jit_func_t jit_entry;
// Number of calls on jit_exec()
long unsigned jit_entry_calls;
#endif
#if USE_YJIT
// Function pointer for JIT code on jit_exec_exception()
rb_jit_func_t jit_exception;
// Number of calls on jit_exec_exception()
long unsigned jit_exception_calls;
#endif
#if USE_YJIT
// YJIT stores some data on each iseq.
void *yjit_payload;
// Used to estimate how frequently this ISEQ gets called
uint64_t yjit_calls_at_interv;
#endif
};
/* T_IMEMO/iseq */
/* typedef rb_iseq_t is in method.h */
struct rb_iseq_struct {
VALUE flags; /* 1 */
VALUE wrapper; /* 2 */
struct rb_iseq_constant_body *body; /* 3 */
union { /* 4, 5 words */
struct iseq_compile_data *compile_data; /* used at compile time */
struct {
VALUE obj;
int index;
} loader;
struct {
struct rb_hook_list_struct *local_hooks;
rb_event_flag_t global_trace_events;
} exec;
} aux;
};
#define ISEQ_BODY(iseq) ((iseq)->body)
#if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
#define USE_LAZY_LOAD 0
#endif
#if !USE_LAZY_LOAD
static inline const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq) {return 0;}
#endif
const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
static inline const rb_iseq_t *
rb_iseq_check(const rb_iseq_t *iseq)
{
if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
rb_iseq_complete((rb_iseq_t *)iseq);
}
return iseq;
}
static inline bool
rb_iseq_attr_p(const rb_iseq_t *iseq, enum rb_builtin_attr attr)
{
return (ISEQ_BODY(iseq)->builtin_attrs & attr) == attr;
}
static inline const rb_iseq_t *
def_iseq_ptr(rb_method_definition_t *def)
{
//TODO: re-visit. to check the bug, enable this assertion.
#if VM_CHECK_MODE > 0
if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
#endif
return rb_iseq_check(def->body.iseq.iseqptr);
}
enum ruby_special_exceptions {
ruby_error_reenter,
ruby_error_nomemory,
ruby_error_sysstack,
ruby_error_stackfatal,
ruby_error_stream_closed,
ruby_special_error_count
};
#define GetVMPtr(obj, ptr) \
GetCoreDataFromValue((obj), rb_vm_t, (ptr))
struct rb_vm_struct;
typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
typedef struct rb_at_exit_list {
rb_vm_at_exit_func *func;
struct rb_at_exit_list *next;
} rb_at_exit_list;
void *rb_objspace_alloc(void);
void rb_objspace_free(void *objspace);
void rb_objspace_call_finalizer(void);
typedef struct rb_hook_list_struct {
struct rb_event_hook_struct *hooks;
rb_event_flag_t events;
unsigned int running;
bool need_clean;
bool is_local;
} rb_hook_list_t;
// see builtin.h for definition
typedef const struct rb_builtin_function *RB_BUILTIN;
struct global_object_list {
VALUE *varptr;
struct global_object_list *next;
};
typedef struct rb_vm_struct {
VALUE self;
struct {
struct ccan_list_head set;
unsigned int cnt;
unsigned int blocking_cnt;
struct rb_ractor_struct *main_ractor;
struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
struct {
// monitor
rb_nativethread_lock_t lock;
struct rb_ractor_struct *lock_owner;
unsigned int lock_rec;
// join at exit
rb_nativethread_cond_t terminate_cond;
bool terminate_waiting;
#ifndef RUBY_THREAD_PTHREAD_H
bool barrier_waiting;
unsigned int barrier_cnt;
rb_nativethread_cond_t barrier_cond;
#endif
} sync;
// ractor scheduling
struct {
rb_nativethread_lock_t lock;
struct rb_ractor_struct *lock_owner;
bool locked;
rb_nativethread_cond_t cond; // GRQ
unsigned int snt_cnt; // count of shared NTs
unsigned int dnt_cnt; // count of dedicated NTs
unsigned int running_cnt;
unsigned int max_cpu;
struct ccan_list_head grq; // // Global Ready Queue
unsigned int grq_cnt;
// running threads
struct ccan_list_head running_threads;
// threads which switch context by timeslice
struct ccan_list_head timeslice_threads;
struct ccan_list_head zombie_threads;
// true if timeslice timer is not enable
bool timeslice_wait_inf;
// barrier
rb_nativethread_cond_t barrier_complete_cond;
rb_nativethread_cond_t barrier_release_cond;
bool barrier_waiting;
unsigned int barrier_waiting_cnt;
unsigned int barrier_serial;
} sched;
} ractor;
#ifdef USE_SIGALTSTACK
void *main_altstack;
#endif
rb_serial_t fork_gen;
struct ccan_list_head waiting_fds; /* <=> struct waiting_fd */
/* set in single-threaded processes only: */
volatile int ubf_async_safe;
unsigned int running: 1;
unsigned int thread_abort_on_exception: 1;
unsigned int thread_report_on_exception: 1;
unsigned int thread_ignore_deadlock: 1;
/* object management */
VALUE mark_object_ary;
struct global_object_list *global_object_list;
const VALUE special_exceptions[ruby_special_error_count];
/* load */
VALUE top_self;
VALUE load_path;
VALUE load_path_snapshot;
VALUE load_path_check_cache;
VALUE expanded_load_path;
VALUE loaded_features;
VALUE loaded_features_snapshot;
VALUE loaded_features_realpaths;
VALUE loaded_features_realpath_map;
struct st_table *loaded_features_index;
struct st_table *loading_table;
// For running the init function of statically linked
// extensions when they are loaded
struct st_table *static_ext_inits;
/* signal */
struct {
VALUE cmd[RUBY_NSIG];
} trap_list;
/* postponed_job (async-signal-safe, and thread-safe) */
struct rb_postponed_job_queue *postponed_job_queue;
int src_encoding_index;
/* workqueue (thread-safe, NOT async-signal-safe) */
struct ccan_list_head workqueue; /* <=> rb_workqueue_job.jnode */
rb_nativethread_lock_t workqueue_lock;
VALUE orig_progname, progname;
VALUE coverages, me2counter;
int coverage_mode;
struct {
struct rb_objspace *objspace;
struct gc_mark_func_data_struct {
void *data;
void (*mark_func)(VALUE v, void *data);
} *mark_func_data;
} gc;
rb_at_exit_list *at_exit;
st_table *frozen_strings;
const struct rb_builtin_function *builtin_function_table;
st_table *ci_table;
struct rb_id_table *negative_cme_table;
st_table *overloaded_cme_table; // cme -> overloaded_cme
st_table *unused_block_warning_table;
// This id table contains a mapping from ID to ICs. It does this with ID
// keys and nested st_tables as values. The nested tables have ICs as keys
// and Qtrue as values. It is used when inline constant caches need to be
// invalidated or ISEQs are being freed.
struct rb_id_table *constant_cache;
ID inserting_constant_cache_id;
#ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
#define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
#endif
const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
uint32_t clock;
#endif
/* params */
struct { /* size in byte */
size_t thread_vm_stack_size;
size_t thread_machine_stack_size;
size_t fiber_vm_stack_size;
size_t fiber_machine_stack_size;
} default_params;
} rb_vm_t;
/* default values */
#define RUBY_VM_SIZE_ALIGN 4096
#define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
#define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
#define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
#define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
#define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
#if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
#else
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
#endif
#if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
/* It seems sanitizers consume A LOT of machine stacks */
#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
#define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
#endif
#ifndef VM_DEBUG_BP_CHECK
#define VM_DEBUG_BP_CHECK 0
#endif
#ifndef VM_DEBUG_VERIFY_METHOD_CACHE
#define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
#endif
struct rb_captured_block {
VALUE self;
const VALUE *ep;
union {
const rb_iseq_t *iseq;
const struct vm_ifunc *ifunc;
VALUE val;
} code;
};
enum rb_block_handler_type {
block_handler_type_iseq,
block_handler_type_ifunc,
block_handler_type_symbol,
block_handler_type_proc
};
enum rb_block_type {
block_type_iseq,
block_type_ifunc,
block_type_symbol,
block_type_proc
};
struct rb_block {
union {
struct rb_captured_block captured;
VALUE symbol;
VALUE proc;
} as;
enum rb_block_type type;
};
typedef struct rb_control_frame_struct {
const VALUE *pc; // cfp[0]
VALUE *sp; // cfp[1]
const rb_iseq_t *iseq; // cfp[2]
VALUE self; // cfp[3] / block[0]
const VALUE *ep; // cfp[4] / block[1]
const void *block_code; // cfp[5] / block[2] -- iseq, ifunc, or forwarded block handler
void *jit_return; // cfp[6] -- return address for JIT code
#if VM_DEBUG_BP_CHECK
VALUE *bp_check; // cfp[7]
#endif
} rb_control_frame_t;
extern const rb_data_type_t ruby_threadptr_data_type;
static inline struct rb_thread_struct *
rb_thread_ptr(VALUE thval)
{
return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
}
enum rb_thread_status {
THREAD_RUNNABLE,
THREAD_STOPPED,
THREAD_STOPPED_FOREVER,
THREAD_KILLED
};
#ifdef RUBY_JMP_BUF
typedef RUBY_JMP_BUF rb_jmpbuf_t;
#else
typedef void *rb_jmpbuf_t[5];
#endif
/*
`rb_vm_tag_jmpbuf_t` type represents a buffer used to
long jump to a C frame associated with `rb_vm_tag`.
Use-site of `rb_vm_tag_jmpbuf_t` is responsible for calling the
following functions:
- `rb_vm_tag_jmpbuf_init` once `rb_vm_tag_jmpbuf_t` is allocated.
- `rb_vm_tag_jmpbuf_deinit` once `rb_vm_tag_jmpbuf_t` is no longer necessary.
`RB_VM_TAG_JMPBUF_GET` transforms a `rb_vm_tag_jmpbuf_t` into a
`rb_jmpbuf_t` to be passed to `rb_setjmp/rb_longjmp`.
*/
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
/*
WebAssembly target with Asyncify-based SJLJ needs
to capture the execution context by unwind/rewind-ing
call frames into a jump buffer. The buffer space tends
to be considerably large unlike other architectures'
register-based buffers.
Therefore, we allocates the buffer on the heap on such
environments.
*/
typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
#define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
static inline void
rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
{
*jmpbuf = ruby_xmalloc(sizeof(rb_jmpbuf_t));
}
static inline void
rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
{
ruby_xfree(*jmpbuf);
}
#else
typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
#define RB_VM_TAG_JMPBUF_GET(buf) (buf)
static inline void
rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
{
// no-op
}
static inline void
rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
{
// no-op
}
#endif
/*
the members which are written in EC_PUSH_TAG() should be placed at
the beginning and the end, so that entire region is accessible.
*/
struct rb_vm_tag {
VALUE tag;
VALUE retval;
rb_vm_tag_jmpbuf_t buf;
struct rb_vm_tag *prev;
enum ruby_tag_type state;
unsigned int lock_rec;
};
STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);