Repository navigation
Expand file tree
/
Copy pathvm.c
More file actions
5326 lines (4887 loc) · 176 KB
/
Copy pathvm.c
File metadata and controls
5326 lines (4887 loc) · 176 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
/*
** vm.c - virtual machine for mruby
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/hash.h>
#include <mruby/irep.h>
#include <mruby/numeric.h>
#include <mruby/proc.h>
#include <mruby/range.h>
#include <mruby/string.h>
#include <mruby/variable.h>
#include <mruby/error.h>
#include <mruby/opcode.h>
#include "value_array.h"
#include <mruby/throw.h>
#include <mruby/dump.h>
#include <mruby/internal.h>
#ifdef MRB_NO_STDIO
#if defined(__cplusplus)
extern "C" {
#endif
void abort(void);
#if defined(__cplusplus)
} /* extern "C" */
#endif
#endif
#define STACK_INIT_SIZE 128
#define CALLINFO_INIT_SIZE 32
/* Define amount of linear stack growth. */
#ifndef MRB_STACK_GROWTH
#define MRB_STACK_GROWTH 128
#endif
/* Maximum recursive depth. Should be set lower on memory constrained systems. */
#ifdef __clang__
#if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
#define __SANITIZE_ADDRESS__
#endif
#endif
#ifndef MRB_CALL_LEVEL_MAX
#if defined(__SANITIZE_ADDRESS__)
#define MRB_CALL_LEVEL_MAX 128
#else
#define MRB_CALL_LEVEL_MAX 512
#endif
#endif
/* Maximum stack depth. Should be set lower on memory constrained systems.
The value below allows about 60000 recursive calls in the simplest case. */
#ifndef MRB_STACK_MAX
#define MRB_STACK_MAX (0x40000 - MRB_STACK_GROWTH)
#endif
#ifdef VM_DEBUG
# define DEBUG(x) (x)
#else
# define DEBUG(x)
#endif
#ifndef MRB_GC_FIXED_ARENA
/* Unwind the arena to `idx` and give back the memory it no longer needs.
The caller is the epilogue of a cfunc call, whose return value is still
only in a local of mrb_vm_exec() at this point, so the realloc below must
not run once the arena has let go of it: under MRB_GC_STRESS every
allocation is a full GC, and the value would be swept before the VM stores
it into a register. The unwind therefore comes after the realloc. */
static void
mrb_gc_arena_shrink(mrb_state *mrb, int idx)
{
mrb_gc *gc = &mrb->gc;
int capa = gc->arena_capa;
if (idx < capa / 4) {
capa >>= 2;
if (capa < MRB_GC_ARENA_SIZE) {
capa = MRB_GC_ARENA_SIZE;
}
if (capa != gc->arena_capa) {
gc->arena = (struct RBasic**)mrb_realloc(mrb, gc->arena, sizeof(struct RBasic*)*capa);
gc->arena_capa = capa;
}
}
gc->arena_idx = idx;
}
#else
#define mrb_gc_arena_shrink(mrb, idx) mrb_gc_arena_restore(mrb, idx)
#endif
#define CALL_MAXARGS 15
#define CALL_VARARGS (CALL_MAXARGS<<4 | CALL_MAXARGS)
static inline void
stack_clear(mrb_value *from, size_t count)
{
while (count-- > 0) {
SET_NIL_VALUE(*from);
from++;
}
}
static inline void
stack_copy(mrb_value *dst, const mrb_value *src, size_t size)
{
if (!src) return;
memcpy(dst, src, sizeof(mrb_value)*size);
}
static void
stack_init(mrb_state *mrb)
{
struct mrb_context *c = mrb->c;
/* mrb_assert(mrb->stack == NULL); */
c->stbase = (mrb_value*)mrb_malloc(mrb, STACK_INIT_SIZE * sizeof(mrb_value));
c->stend = c->stbase + STACK_INIT_SIZE;
stack_clear(c->stbase, STACK_INIT_SIZE);
/* mrb_assert(ci == NULL); */
static const mrb_callinfo ci_zero = { 0 };
c->cibase = (mrb_callinfo*)mrb_malloc(mrb, CALLINFO_INIT_SIZE * sizeof(mrb_callinfo));
c->ciend = c->cibase + CALLINFO_INIT_SIZE;
c->cibase[0] = ci_zero;
c->ci = c->cibase;
c->ci->u.target_class = mrb->object_class;
c->ci->stack = c->stbase;
c->ci->vis = 1; /* private (2-bit packed) */
}
static inline void
envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase)
{
mrb_callinfo *ci = mrb->c->cibase;
if (newbase == oldbase) return;
while (ci <= mrb->c->ci) {
struct REnv *e = mrb_vm_ci_env(ci);
/*
* Byte-level calculation to avoid truncation when allocator alignment is
* smaller than sizeof(mrb_value).
* eg: MRB_NO_BOXING + MRB_INT64 with MRB_32BIT => sizeof(mrb_value)=16
* And when memory allocator's alignment is 8 bytes
* Pointer subtraction on mrb_value* would truncate (8/16 -> 0).
* So, we use char* for pointer calculation to get the correct offset in
* bytes, then apply that offset to mrb_value* pointers.
*/
mrb_value *new_stack = (mrb_value *)((char *)newbase + ((char *)ci->stack - (char *)oldbase));
if (e) {
mrb_assert(e->cxt == mrb->c && MRB_ENV_ONSTACK_P(e));
mrb_assert(e->stack == ci->stack);
e->stack = new_stack;
}
ci->stack = new_stack;
ci++;
}
}
/** def rec; $deep =+ 1; if $deep > 1000; return 0; end; rec; end **/
/* Run BODY with GC disabled. Used around a stack or callinfo realloc, where the
old buffer is freed before the base pointers are updated, so an incremental
GC step triggered from inside the realloc would mark the freed buffer
(use-after-free). */
static inline void*
mrb_realloc_with_gc_disabled(mrb_state *mrb, void *p, size_t size)
{
mrb_bool gc_disabled = mrb->gc.disabled;
mrb->gc.disabled = TRUE;
p = mrb_realloc_simple(mrb, p, size);
mrb->gc.disabled = gc_disabled;
if (!p) mrb_raise_nomemory(mrb);
return p;
}
static void
stack_extend_alloc(mrb_state *mrb, mrb_int room)
{
mrb_value *oldbase = mrb->c->stbase;
size_t oldsize = mrb->c->stend - mrb->c->stbase;
size_t size = oldsize;
/* ci->stack can sit past stend when an embedder enters the VM with an
undersized stack, since cipush places the frame before the stack is
extended. The base size must cover the frame offset. */
size_t off = mrb->c->ci->stack ? mrb->c->ci->stack - oldbase : 0;
if (off > size) size = off;
#ifdef MRB_STACK_EXTEND_DOUBLING
if ((size_t)room <= size)
size *= 2;
else
size += room;
#else
/* Use 1.5x stack growth.
It is slightly slower than doubling the stack space,
but it saves memory on small devices. */
{
size_t newsize = size + (size >> 1); /* 1.5x growth */
if (newsize < size + MRB_STACK_GROWTH)
newsize = size + MRB_STACK_GROWTH;
if (newsize < size + (size_t)room)
newsize = size + room;
size = newsize;
}
#endif
mrb_value *newstack = (mrb_value*)mrb_realloc_with_gc_disabled(mrb, mrb->c->stbase, sizeof(mrb_value) * size);
stack_clear(&(newstack[oldsize]), size - oldsize);
envadjust(mrb, oldbase, newstack);
mrb->c->stbase = newstack;
mrb->c->stend = mrb->c->stbase + size;
/* Raise an exception if the new stack size will be too large,
to prevent infinite recursion. However, do this only after resizing the stack, so mrb_raise has stack space to work with. */
if (size > MRB_STACK_MAX) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
}
static inline void
stack_extend(mrb_state *mrb, mrb_int room)
{
if (mrb_unlikely(!mrb->c->ci->stack || mrb->c->ci->stack + room >= mrb->c->stend)) {
stack_extend_alloc(mrb, room);
}
}
/**
* @brief Extends the VM stack.
*
* This function extends the virtual machine stack to accommodate more values.
* If the current stack size is insufficient, it reallocates the stack
* with a larger size.
*
* @param mrb The mruby state.
* @param room The additional number of mrb_value slots required.
*/
MRB_API void
mrb_stack_extend(mrb_state *mrb, mrb_int room)
{
stack_extend(mrb, room);
}
static void
stack_extend_adjust(mrb_state *mrb, mrb_int room, const mrb_value **argp)
{
const struct mrb_context *c = mrb->c;
ptrdiff_t voff = *argp - c->stbase;
if (voff < 0 || voff >= c->stend - c->stbase) {
stack_extend(mrb, room);
}
else {
stack_extend(mrb, room);
*argp = c->stbase + voff;
}
}
static inline struct REnv*
uvenv(mrb_state *mrb, mrb_int up)
{
const struct RProc *proc = mrb->c->ci->proc;
while (up--) {
proc = proc->upper;
if (!proc) return NULL;
}
struct REnv *e = MRB_PROC_ENV(proc);
if (e) return e; /* proc has enclosed env */
return NULL;
}
static inline const struct RProc*
top_proc(mrb_state *mrb, const struct RProc *proc, const struct REnv **envp)
{
while (proc->upper) {
if (MRB_PROC_SCOPE_P(proc) || MRB_PROC_STRICT_P(proc))
return proc;
*envp = proc->e.env;
proc = proc->upper;
}
return proc;
}
#define CI_PROC_SET(ci, p) do {\
ci->proc = p;\
if (p) {\
mrb_assert(!MRB_PROC_ALIAS_P(p));\
ci->pc = (!MRB_PROC_CFUNC_P(p) && p->body.irep) ? p->body.irep->iseq : NULL;\
}\
else {\
ci->pc = NULL;\
}\
} while (0)
void
mrb_vm_ci_proc_set(mrb_callinfo *ci, const struct RProc *p)
{
CI_PROC_SET(ci, p);
}
static void ci_set_alias_owner(mrb_callinfo *ci, const struct RProc *alias);
#define MRB_PROC_RESOLVE_ALIAS(ci, p) do {\
if (MRB_PROC_ALIAS_P(p)) {\
(ci)->mid = (p)->body.mid;\
ci_set_alias_owner(ci, p);\
(p) = (p)->upper;\
}\
} while (0)
#define CI_TARGET_CLASS(ci) (((ci)->u.env && (ci)->u.env->tt == MRB_TT_ENV)? (ci)->u.env->c : (ci)->u.target_class)
struct RClass*
mrb_vm_ci_target_class(const mrb_callinfo *ci)
{
return CI_TARGET_CLASS(ci);
}
void
mrb_vm_ci_target_class_set(mrb_callinfo *ci, struct RClass *tc)
{
struct REnv *e = ci->u.env;
if (e && e->tt == MRB_TT_ENV) {
e->c = tc;
}
else {
ci->u.target_class = tc;
}
}
#define CI_ENV(ci) (((ci)->u.env && (ci)->u.env->tt == MRB_TT_ENV)? (ci)->u.env : NULL)
/* A method called through an alias runs under the class its original was
found in, so that a `super` in it goes on from there. When that is a
module, it is the module's place in the receiver's ancestry, which the
class the alias was found in starts. */
static void
ci_set_alias_owner(mrb_callinfo *ci, const struct RProc *alias)
{
struct RClass *owner = alias->e.target_class;
if (!owner) return;
/* a class prepended to since the alias was made keeps its methods in
its origin */
MRB_CLASS_ORIGIN(owner);
if (owner->tt == MRB_TT_MODULE || owner->tt == MRB_TT_ICLASS) {
struct RClass *mod = owner->tt == MRB_TT_MODULE ? owner : owner->c;
struct RClass *k = CI_TARGET_CLASS(ci);
while (k && !(k->tt == MRB_TT_ICLASS && k->c == mod)) {
k = k->super;
}
if (k) owner = k;
else if (owner->tt == MRB_TT_MODULE) return;
}
mrb_vm_ci_target_class_set(ci, owner);
}
struct REnv*
mrb_vm_ci_env(const mrb_callinfo *ci)
{
return CI_ENV(ci);
}
static inline void
ci_env_set(mrb_callinfo *ci, struct REnv *e)
{
if (ci->u.env) {
if (ci->u.env->tt == MRB_TT_ENV) {
if (e) {
e->c = ci->u.env->c;
ci->u.env = e;
}
else {
ci->u.target_class = ci->u.env->c;
}
}
else if (e) {
e->c = ci->u.target_class;
ci->u.env = e;
}
}
else {
ci->u.env = e;
}
}
void
mrb_vm_ci_env_set(mrb_callinfo *ci, struct REnv *e)
{
ci_env_set(ci, e);
}
MRB_API void
mrb_vm_ci_env_clear(mrb_state *mrb, mrb_callinfo *ci)
{
struct REnv *e = ci->u.env;
if (e && e->tt == MRB_TT_ENV) {
/* The escaping env carries the container, so a proc from the earlier
top-level chunk keeps reading the scope it was written in. The frame
keeps the env through the detach, as cipop() does, so the closing
allocation's collection finds it rooted. */
mrb_env_detach(mrb, e, mrb_ci_svar(mrb->c, ci), FALSE);
ci->u.target_class = e->c;
}
/* The frame itself starts the next chunk in a fresh scope, the way each
file `ruby -r` loads gets one of its own; a caller that wants a single
scope across its chunks (mirb's interactive lines) does not call this,
which is also how it keeps the top-level locals. */
mrb_ci_svar_set(mrb, mrb->c, ci, NULL);
}
/* A proc that is a scope of its own: one the VM made for a `def` body or a
* class body, which captured no env. `mrb_define_method_raw()` marks a
* block installed by `define_method` a scope too, but that block keeps the
* env it was made in, and its special variables belong to the scope it was
* written in, the way its locals do (MRB_PROC_LVAR_BOUNDARY_P() in
* mruby/proc.h, the same condition read for the other walk). CRuby resolves
* the same way: the block's ep is unchanged by the install, so its svar is
* still its lep's. */
static mrb_bool
svar_own_scope_p(const struct RProc *p)
{
return MRB_PROC_SCOPE_P(p) && !MRB_PROC_ENV_P(p);
}
/* The local scope of a block or lambda: following p->upper toward the
* scope proc, MRB_PROC_ENV() of each step is the env of the frame the
* proc was defined in, instance by instance, so the env in hand when the
* next proc up turns out to be a scope (or is missing) belongs to the
* method scope, the way CRuby's lep walk crosses block eps into the
* method's. NULL for a proc that captured no env. Pointer identity only:
* the env may be closed, or live on another context's stack. */
static struct REnv*
svar_scope_env(const struct RProc *p)
{
struct REnv *e = MRB_PROC_ENV(p);
if (!e) return NULL;
for (;;) {
const struct RProc *up = p->upper;
if (!up || MRB_PROC_CFUNC_P(up) || svar_own_scope_p(up)) return e;
struct REnv *upenv = MRB_PROC_ENV(up);
if (!upenv) return e;
p = up;
e = upenv;
}
}
/* A frame with no scope of its own: an irep proc that neither is a scope
* nor captured one. What reaches here is the top proc of a nested
* `mrb_top_run()`, which is `mrb_load_string()` called from a C function
* mid-execution, and an eval compiled against no Ruby scope, which is the
* same call with no Ruby frame under it. An ordinary `eval` is not one of
* these: its proc captures the calling frame's env, so it answers FALSE
* and resolves as a block does, onto the scope that called it. Holding no
* slot, a scopeless frame is as transparent to the special variables as
* the C function that pushed it, the way CRuby's `rb_eval_string()` shares
* the scope below; when its env escapes, the transparency outlives the
* frame by adoption (svar_owner_adopt()). Blocks and lambdas always
* capture, so they never answer TRUE. */
static mrb_bool
svar_scopeless_frame_p(const mrb_callinfo *ci)
{
const struct RProc *p = ci->proc;
/* `svar_scope_env()` answers NULL for exactly the procs that captured
nothing, so the walk it makes is not one this question needs. */
return p && !MRB_PROC_CFUNC_P(p) &&
!MRB_PROC_SCOPE_P(p) && !MRB_PROC_ENV_P(p);
}
/* An escaped scope's slot (MRB_ENV_SVAR_SLOT in internal.h):
* mrb_env_detach() sizes the closing allocation for it and fills it,
* through env_unshare_with_svar(), only when it actually has a container
* or forward to install; mrb_env_unshare() on its own never asks for the
* extra value. NULL where the env carries none, which only the flag can
* say: a closed env whose stack stops at its locals is the ordinary result
* of mrb_env_unshare() itself, what out-of-tree code builds by hand, and
* what error.c's fault-time rewind and the out-of-memory arm of
* mrb_env_unshare() leave behind, all three with no room past the locals.
* Reads take it for a scope holding no container; the one later write that
* cannot, a live scope's first, grows the already-closed env into the slot
* instead (svar_slot_ensure()).
*
* An env still on the stack carries no slot either, its locals being the
* VM's own stack. That test comes first because it is also what a swept
* env looks like: mrb_env_unshare() answers TRUE without touching an env
* the collection inside its allocation freed, and a freed cell's flags say
* nothing. The callers that close an env keep it rooted through that
* allocation (cipop(), mrb_vm_ci_env_clear()), so none of them reads a
* slot off a swept env; the test stands for an env closed elsewhere. */
static mrb_value*
env_svar_slot(struct REnv *e)
{
if (MRB_ENV_ONSTACK_P(e) || !MRB_ENV_SVAR_P(e)) return NULL;
mrb_assert(e->stack != NULL);
return &MRB_ENV_SVAR_SLOT(e->stack, MRB_ENV_LEN(e));
}
/* The same slot, made if the env has none yet: a closed env whose stack
* holds exactly its locals grows by one and starts carrying the flag. Only
* the first write into such a scope comes here (svar_owner_force()); reads
* answer nil off the missing slot and leave the env alone, so an env that
* never owns special variables never pays the extra mrb_value.
* The env is reachable from the roots the caller resolved it through, so a
* collection inside the resize cannot sweep it and free the stack under
* the realloc. Out of memory raises, as the container allocation just
* above it does, leaving the env exactly as it was. */
static mrb_value*
svar_slot_ensure(mrb_state *mrb, struct REnv *e)
{
mrb_value *slot = env_svar_slot(e);
if (slot) return slot;
mrb_assert(!MRB_ENV_ONSTACK_P(e));
size_t len = (size_t)MRB_ENV_LEN(e);
mrb_value *stack = (mrb_value*)mrb_realloc(mrb, e->stack, MRB_ENV_SVAR_STACK_SIZE(len));
e->stack = stack;
SET_NIL_VALUE(MRB_ENV_SVAR_SLOT(stack, len));
MRB_ENV_SET_SVAR(e);
return &MRB_ENV_SVAR_SLOT(stack, len);
}
/* The owner of the frame-scoped special variables, resolved the way CRuby
* resolves its svar: walking down from the top, a C frame has no slot of
* its own, a scope frame owns its own slot (see `svar_own_scope_p()`), a
* frame with no scope of its own is as transparent as a C frame (see
* `svar_scopeless_frame_p()`), and a block or lambda frame resolves to the
* scope it was defined in, wherever that scope now is: a live frame on
* this or on another context's stack, or the env the scope left behind. A
* block written inside a scopeless frame resolves to that frame, which
* owns no slot either, so the walk goes on below it.
* One exception, CRuby's root-lep redirect: a resolution landing on the
* very scope the running context's own root block was defined in is handed
* the context's root slot instead (`cibase`), which is how a fiber keeps
* its special variables to itself. Resolution ends in a live frame (*cip,
* its context in *ocp), in the env of a scope that escaped the stack
* (*envp, holding the container mrb_env_detach() moved off it, or the
* owner's a scopeless frame adopted), or in neither, when the scope left
* nothing behind: then a read answers nil and a write is dropped. A
* scopeless frame's escaped env may instead hold the env of the scope
* below it (what mrb_svar_frame_container() hands on where that scope
* holds no container to share): the walk follows it, one hop per nested
* load, the way CRuby's ep chain crosses an eval frame's ep into the scope
* below's.
* What resolution costs, and why it is linear: an env does not record
* which frame it belongs to, so a defining scope still on a stack is
* found by the downward scan below, one CI_ENV test per ci entry between
* the reader and the scope (eight instructions each, measured), and the
* lexical chase of svar_scope_env() adds a step per block-nesting level.
* CRuby resolves in constant time at any call depth, its svar living on
* the ep itself; the slot could live that way here only if the compiler
* reserved it in every method's locals, which no compiled irep has, so
* the scan is what keeps existing bytecode running. The term stretches
* past a handful of entries only when a proc is carried to the bottom of
* a deep call chain and reads its special variables there.
* The walk starts at `top`, which is c->ci for a read or write and a
* mid-stack frame for mrb_svar_frame_container() resolving a torn-down
* context. */
static void
svar_owner_from(struct mrb_context *c, mrb_callinfo *top, mrb_callinfo **cip, struct mrb_context **ocp, struct REnv **envp)
{
struct mrb_context *wc = c; /* the context being walked; the redirect
below stays the caller's */
mrb_callinfo *ci = top;
*cip = NULL;
*ocp = c;
*envp = NULL;
while (ci > wc->cibase) {
const struct RProc *p = ci->proc;
if (p && !MRB_PROC_CFUNC_P(p)) {
if (svar_own_scope_p(p)) {
*cip = ci;
*ocp = wc;
return;
}
struct REnv *e = svar_scope_env(p);
if (!e) {
/* A scopeless frame: reads and writes pass through to the scope
below, the way `rb_eval_string()`'s do. */
ci--;
continue;
}
for (;;) {
const struct RProc *rp = c->cibase->proc;
if (rp && !MRB_PROC_CFUNC_P(rp) && !MRB_PROC_SCOPE_P(rp) &&
svar_scope_env(rp) == e) {
*cip = c->cibase;
*ocp = c;
return;
}
if (MRB_ENV_ONSTACK_P(e)) break;
if (!e->stack) return;
mrb_value *slot = env_svar_slot(e);
if (!slot) {
/* A closed env whose stack was sized without the slot (see
internal.h): it can hold no forward, so it is the owner
itself, with no container yet. A read answers nil off it and
the first write grows it into one (svar_slot_ensure()). */
*envp = e;
return;
}
mrb_value fwd = *slot;
if (mrb_type(fwd) != MRB_TT_ENV) {
*envp = e;
return;
}
/* A scopeless frame's env, forwarded on teardown to the env of
the scope below the load: follow it there. */
e = (struct REnv*)mrb_obj_ptr(fwd);
}
struct mrb_context *oc = e->cxt;
mrb_callinfo *s = (oc == wc) ? ci - 1 : oc->ci;
/* Stop AT cibase rather than decrementing past it: forming a pointer
one element before the start of an array is undefined behavior. */
while (CI_ENV(s) != e) {
if (s == oc->cibase) return;
s--;
}
if (s > oc->cibase && svar_scopeless_frame_p(s)) {
/* The scope the block was defined in is itself scopeless: the block
was written inside a nested load, so its special variables belong
to the scope that load runs against, further down, on whichever
context that load's frame still stands. */
wc = oc;
ci = s - 1;
continue;
}
*cip = s;
*ocp = oc;
return;
}
ci--;
}
*cip = wc->cibase;
*ocp = wc;
}
static void
svar_owner(struct mrb_context *c, mrb_callinfo **cip, struct mrb_context **ocp, struct REnv **envp)
{
svar_owner_from(c, c->ci, cip, ocp, envp);
}
/* A scope's special-variable container (struct RSvar in internal.h), made
* on the first non-nil write into the scope. The object is allocated
* first and reaches the GC arena with slots == NULL, so the collector a
* slot allocation may run sees the window; gc_mark_children() guards on
* it. A raise out of the slot allocation leaves that empty shell to the
* GC and no owner ever holds it. */
static struct RSvar*
svar_new(mrb_state *mrb)
{
struct RSvar *sv = MRB_OBJ_ALLOC(mrb, MRB_TT_SVAR, NULL);
mrb_value *slots = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * MRB_SVAR_MAX);
for (int i = 0; i < MRB_SVAR_MAX; i++) {
SET_NIL_VALUE(slots[i]);
}
sv->slots = slots;
return sv;
}
/* The container of the owner svar_owner() resolved to: on a live frame
* the frame's pointer, on an escaped scope the object its env slot holds,
* NULL where the scope has never written one (or left nothing behind).
* Both places may instead hold a forward to the env of the scope below
* (mrb_svar_frame_container()), but only on a frame with no scope of its
* own, and resolution walks past those rather than ending on one, so what
* a resolved frame holds is a container or nothing. */
static struct RSvar*
svar_owner_container(const struct mrb_context *c, mrb_callinfo *ci, struct REnv *e)
{
if (ci) {
struct RBasic *sv = mrb_ci_svar(c, ci);
mrb_assert(sv == NULL || sv->tt == MRB_TT_SVAR);
return (struct RSvar*)sv;
}
if (e) {
mrb_value *slot = env_svar_slot(e);
if (slot && mrb_type(*slot) == MRB_TT_SVAR) return (struct RSvar*)mrb_obj_ptr(*slot);
}
return NULL;
}
/* Resolves the current owner and answers its container, making one on the
* spot when the scope holds none yet: the first non-nil write does this
* (mrb_vm_svar_set()), and so does a scopeless frame whose env escapes
* (svar_owner_adopt()), so the adopted slot and the scope share one
* object before either side has written. NULL when resolution found no
* scope that could hold one.
* Installing a fresh container into a frame needs no barrier of its own
* on the running or the root context, those two ci stacks being
* unbarriered roots that final_marking_phase() re-scans atomically like
* the value stack; a frame on any other context is marked through that
* context's fiber, so the fiber pays one; a task context has no fiber and
* needs none, its stack being re-scanned atomically by
* mrb_task_mark_all(), which final_marking_phase() calls for exactly
* that. An env slot is ordinary GC-owned storage and pays the env's own. */
static struct RSvar*
svar_owner_force(mrb_state *mrb)
{
mrb_callinfo *ci;
struct mrb_context *oc;
struct REnv *e;
svar_owner(mrb->c, &ci, &oc, &e);
struct RSvar *sv = svar_owner_container(oc, ci, e);
if (sv) return sv;
sv = svar_new(mrb);
/* The allocation may have run a collection, and the owner may have stood
on a stack nothing else kept alive, a suspended fiber whose last
reference was dropped: its sweep detached its envs and freed its
callinfo array, so the frame resolved above is gone. Whether one ran
cannot be read off mrb->gc.live, which counts objects rather than
cycles and comes back unchanged from a sweep that freed as many as
this allocation makes, so resolve again either way; the walk then ends
in the closed env the sweep left behind, holding whatever container
the detach carried. */
svar_owner(mrb->c, &ci, &oc, &e);
struct RSvar *moved = svar_owner_container(oc, ci, e);
if (moved) return moved;
if (ci) {
mrb_ci_svar_set(mrb, oc, ci, (struct RBasic*)sv);
if (oc != mrb->c && oc != mrb->root_c && oc->fib) {
mrb_write_barrier(mrb, (struct RBasic*)oc->fib);
}
return sv;
}
if (e) {
/* Grows a closed env sized without the slot into one (see internal.h);
the resize may raise out of memory, leaving the fresh container to
the GC the way a raise out of svar_new() does. */
mrb_value *slot = svar_slot_ensure(mrb, e);
*slot = mrb_obj_value(sv);
mrb_write_barrier(mrb, (struct RBasic*)e);
return sv;
}
return NULL;
}
/* What a frame's env carries into escape when its context is torn down
* around it or marked through it: the frame's own container, or for a
* scopeless frame the one the owner it resolved to while it ran already
* holds. Where that owner holds none, the answer is the owner's env
* itself, the forward svar_owner_from() follows one hop further, so that
* the transparency of a scopeless frame survives its context by the same
* representation an escaped scopeless env uses: neither caller may make a
* container here, gc_mark_children() (gc.c) running mid-collection and
* mrb_env_detach_all() mid-teardown, where cipop() materializes one
* instead (svar_owner_adopt()). NULL where there is nothing to carry,
* and for an owner that is the context's own root frame, whose container
* dies with the context the way the root frame's own does. */
struct RBasic*
mrb_svar_frame_container(struct mrb_context *c, mrb_callinfo *ci)
{
struct RBasic *own = mrb_ci_svar(c, ci);
if (own || !svar_scopeless_frame_p(ci)) return own;
mrb_callinfo *oci;
struct mrb_context *oc;
struct REnv *oe;
svar_owner_from(c, ci, &oci, &oc, &oe);
if (oc == c && oci == c->cibase) return NULL;
struct RSvar *sv = svar_owner_container(oc, oci, oe);
if (sv) return (struct RBasic*)sv;
struct REnv *fwd = oci ? CI_ENV(oci) : oe;
/* Nothing to forward to when the owner scope left no env behind: no proc
can be holding it, so nothing can look. A frame forwarding to its own
env would close the walk into a loop rather than a hop. */
if (fwd == NULL || fwd == CI_ENV(ci)) return NULL;
return (struct RBasic*)fwd;
}
/*
* Reads one special-variable slot of the scope owning it, on a live frame
* or in the env of a scope that returned, nil where that scope holds no
* container.
*/
mrb_value
mrb_vm_svar_get(mrb_state *mrb, enum mrb_svar_index key)
{
mrb_callinfo *ci;
struct mrb_context *oc;
struct REnv *e;
mrb_assert(key >= 0 && key < MRB_SVAR_MAX);
svar_owner(mrb->c, &ci, &oc, &e);
struct RSvar *sv = svar_owner_container(oc, ci, e);
if (!sv) return mrb_nil_value();
return sv->slots[key];
}
/*
* Writes one special-variable slot of the owning scope. The slot holds any
* mrb_value; any richer contract, like the TypeError mruby-regexp's
* virtual-global setter raises for `$~ = <not a MatchData>`, belongs to
* the caller. The scope's container is made on the first non-nil write; a
* nil write into a scope that has none is dropped, nil being what a
* missing slot already reads as, so `def foo; 1 + 2; end` never allocates
* one, and an immediate write pays no barrier, making no edge a black
* container would have to re-scan for. A heap-value write pays the
* backward barrier: a black container is
* re-grayed so the final marking re-reads its slots, and until then the
* barrier is a no-op. The forward barrier (marking the value on the spot)
* would be wrong here, not just slower: a publish-heavy loop hands the
* slot a fresh object every pass, and marking each one resurrects garbage
* into the next cycle wholesale, where a re-grayed container marks only
* what it still holds when the cycle closes.
*/
void
mrb_vm_svar_set(mrb_state *mrb, enum mrb_svar_index key, mrb_value v)
{
struct RSvar *sv;
mrb_assert(key >= 0 && key < MRB_SVAR_MAX);
if (mrb_nil_p(v)) {
mrb_callinfo *ci;
struct mrb_context *oc;
struct REnv *e;
svar_owner(mrb->c, &ci, &oc, &e);
sv = svar_owner_container(oc, ci, e);
if (!sv) return;
}
else {
sv = svar_owner_force(mrb);
if (!sv) return;
}
sv->slots[key] = v;
if (!mrb_immediate_p(v)) {
mrb_write_barrier(mrb, (struct RBasic*)sv);
}
}
#define CINFO_NONE 0 // called method from mruby VM (without C functions)
#define CINFO_SKIP 1 // ignited mruby VM from C
#define CINFO_DIRECT 2 // called method from C
#define CINFO_RESUMED 3 // resumed by `Fiber.yield` (probably the main call is `mrb_fiber_resume()`)
#define BLK_PTR(b) ((mrb_proc_p(b)) ? mrb_proc_ptr(b) : NULL)
/* See mrb_ci_svar() in internal.h for why these live beside the frames. */
void
mrb_ci_svar_set(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, struct RBasic *sv)
{
if (!c->svars) {
if (!sv) return;
mrb->svar_used = TRUE;
c->svars = (struct RBasic**)mrb_calloc(mrb, (size_t)(c->ciend - c->cibase),
sizeof(struct RBasic*));
}
c->svars[ci - c->cibase] = sv;
}
/* See internal.h. Nothing to do before the first write anywhere in the
state, which is what keeps a program that never touches one from paying
for the frames it never fills. */
void
mrb_svars_reserve(mrb_state *mrb, struct mrb_context *c)
{
if (!mrb->svar_used || c->svars) return;
c->svars = (struct RBasic**)mrb_calloc(mrb, (size_t)(c->ciend - c->cibase),
sizeof(struct RBasic*));
}
#ifdef MRB_USE_C_STACK_CHECK
/* The machine stack against the limit the host set, if it set one. Made
where C enters the VM (mrb_funcall_with_block, yield_with_attr,
mrb_vm_run): those are the calls that nest mrb_vm_exec on the machine
stack. A send from Ruby to Ruby runs in the same VM loop and grows no
machine stack, so cipush does not check. The address of a local is where
the stack is now. The stack grows downward unless MRB_C_STACK_GROWS_UP
says otherwise. The compare is on integers: two pointers into different
objects have no defined order in C. */
static inline void
c_stack_check(mrb_state *mrb)
{
if (mrb->c_stack_limit) {
char here;
#ifdef MRB_C_STACK_GROWS_UP
if ((uintptr_t)&here > (uintptr_t)mrb->c_stack_limit) {
#else
if ((uintptr_t)&here < (uintptr_t)mrb->c_stack_limit) {
#endif
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
}
}
MRB_API void
mrb_c_stack_limit_set(mrb_state *mrb, void *limit)
{
mrb->c_stack_limit = limit;
}
MRB_API void
mrb_c_stack_check(mrb_state *mrb)
{
c_stack_check(mrb);
}
#else
#define c_stack_check(mrb) ((void)0)
#endif
static inline mrb_callinfo*
cipush(mrb_state *mrb, mrb_int push_stacks, uint8_t cci, struct RClass *target_class,
const struct RProc *proc, struct RProc *blk, mrb_sym mid, uint16_t argc)
{
struct mrb_context *c = mrb->c;
mrb_callinfo *ci = c->ci + 1;
if (ci < c->ciend) {
c->ci = ci;
}
else {
ptrdiff_t size = ci - c->cibase;
if (size >= MRB_CALL_LEVEL_MAX) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
c->cibase = (mrb_callinfo*)mrb_realloc_with_gc_disabled(mrb, c->cibase, sizeof(mrb_callinfo)*size*2);
c->ci = ci = c->cibase + size;
c->ciend = c->cibase + size * 2;
if (c->svars) {
/* The frames moved and there are twice as many; the entries follow,
with the half that is new zeroed. */
c->svars = (struct RBasic**)mrb_realloc_with_gc_disabled(mrb, c->svars, sizeof(struct RBasic*)*size*2);
memset(c->svars + size, 0, sizeof(struct RBasic*)*size);
}
}
ci->mid = mid;
CI_PROC_SET(ci, proc);
ci->blk = blk;
ci->stack = ci[-1].stack + push_stacks;
ci->n = argc & 0xf;
ci->kw = (argc>>4) ? 1 : 0;
ci->cci = cci;
ci->vis = MRB_METHOD_PUBLIC_FL;
if (c->svars) c->svars[ci - c->cibase] = NULL;
ci->u.target_class = target_class;
return ci;
}
static void
fiber_terminate(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci)
{
mrb_assert(c != mrb->root_c);
struct REnv *env = CI_ENV(ci);
mrb_assert(env == NULL || MRB_ENV_LEN(env) <= c->stend - ci->stack);
c->status = MRB_FIBER_TERMINATED;
mrb_free(mrb, c->svars);
c->svars = NULL;
mrb_free(mrb, c->cibase);
c->cibase = c->ciend = c->ci = NULL;
mrb_value *stack = c->stbase;
c->stbase = c->stend = NULL;
if (!env) {
mrb_free(mrb, stack);
}
else {
size_t len = (size_t)MRB_ENV_LEN(env);
if (len == 0) {
env->stack = NULL;
MRB_ENV_CLOSE(env);
MRB_ENV_CLEAR_SVAR(env);
mrb_free(mrb, stack);
}
else {
mrb_assert(stack == env->stack);
mrb_write_barrier(mrb, (struct RBasic*)env);
// don't call MRB_ENV_CLOSE() before mrb_realloc().
// the reason is that env->stack may be freed by mrb_realloc() if MRB_DEBUG + MRB_GC_STRESS are enabled.
// realloc() on a freed heap will cause double-free.
stack = (mrb_value*)mrb_realloc(mrb, stack, sizeof(mrb_value)*len);
if (mrb_object_dead_p(mrb, (struct RBasic*)env)) {
mrb_free(mrb, stack);
}
else {