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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
|
//! This module deals with making relevant C functions available to Rust YJIT.
//! Some C functions we use we maintain, some are public C extension APIs,
//! some are internal CRuby APIs.
//!
//! ## General notes about linking
//!
//! The YJIT crate compiles to a native static library, which for our purposes
//! we can understand as a collection of object files. On ELF platforms at least,
//! object files can refer to "external symbols" which we could take some
//! liberty and understand as assembly labels that refer to code defined in other
//! object files resolved when linking. When we are linking, say to produce miniruby,
//! the linker resolves and put concrete addresses for each usage of C function in
//! the Rust static library.
//!
//! By declaring external functions and using them, we are asserting the symbols
//! we use have definition in one of the object files we pass to the linker. Declaring
//! a function here that has no definition anywhere causes a linking error.
//!
//! There are more things going on during linking and this section makes a lot of
//! simplifications but hopefully this gives a good enough working mental model.
//!
//! ## Difference from example in the Rustonomicon
//!
//! You might be wondering about why this is different from the [FFI example]
//! in the Nomicon, an official book about Unsafe Rust.
//!
//! There is no `#[link]` attribute because we are not linking against an external
//! library, but rather implicitly asserting that we'll supply a concrete definition
//! for all C functions we call, similar to how pure C projects put functions
//! across different compilation units and link them together.
//!
//! TODO(alan): is the model different enough on Windows that this setup is unworkable?
//! Seems prudent to at least learn more about Windows binary tooling before
//! committing to a design.
//!
//! Alan recommends reading the Nomicon cover to cover as he thinks the book is
//! not very long in general and especially for something that can save hours of
//! debugging Undefined Behavior (UB) down the road.
//!
//! UBs can cause Safe Rust to crash, at which point it's hard to tell which
//! usage of `unsafe` in the codebase invokes UB. Providing safe Rust interface
//! wrapping `unsafe` Rust is a good technique, but requires practice and knowledge
//! about what's well defined and what's undefined.
//!
//! For an extremely advanced example of building safe primitives using Unsafe Rust,
//! see the [GhostCell] paper. Some parts of the paper assume less background knowledge
//! than other parts, so there should be learning opportunities in it for all experience
//! levels.
//!
//! ## Binding generation
//!
//! For the moment declarations on the Rust side are hand written. The code is boilerplate
//! and could be generated automatically with a custom tooling that depend on
//! rust-lang/rust-bindgen. The output Rust code could be checked in to version control
//! and verified on CI like `make update-deps`.
//!
//! Upsides for this design:
//! - the YJIT static lib that links with miniruby and friends will not need bindgen
//! as a dependency at all. This is an important property so Ruby end users can
//! build a YJIT enabled Ruby with no internet connection using a release tarball
//! - Less hand-typed boilerplate
//! - Helps reduce risk of C definitions and Rust declaration going out of sync since
//! CI verifies synchronicity
//!
//! Downsides and known unknowns:
//! - Using rust-bindgen this way seems unusual. We might be depending on parts
//! that the project is not committed to maintaining
//! - This setup assumes rust-bindgen gives deterministic output, which can't be taken
//! for granted
//! - YJIT contributors will need to install libclang on their system to get rust-bindgen
//! to work if they want to run the generation tool locally
//!
//! The elephant in the room is that we'll still need to use Unsafe Rust to call C functions,
//! and the binding generation can't magically save us from learning Unsafe Rust.
//!
//!
//! [FFI example]: https://doc.rust-lang.org/nomicon/ffi.html
//! [GhostCell]: http://plv.mpi-sws.org/rustbelt/ghostcell/
// CRuby types use snake_case. Allow them so we use one name across languages.
#![allow(non_camel_case_types)]
// A lot of imported CRuby globals aren't all-caps
#![allow(non_upper_case_globals)]
// Some of this code may not be used yet
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]
use std::convert::From;
use std::ffi::{CString, CStr};
use std::fmt::{Debug, Formatter};
use std::os::raw::{c_char, c_int, c_uint};
use std::panic::{catch_unwind, UnwindSafe};
// We check that we can do this with the configure script and a couple of
// static asserts. u64 and not usize to play nice with lowering to x86.
pub type size_t = u64;
/// A type alias for the redefinition flags coming from CRuby. These are just
/// shifted 1s but not explicitly an enum.
pub type RedefinitionFlag = u32;
#[allow(unsafe_op_in_unsafe_fn)]
#[allow(dead_code)]
#[allow(clippy::all)] // warning meant to help with reading; not useful for generated code
mod autogened {
use super::*;
// Textually include output from rust-bindgen as suggested by its user guide.
include!("cruby_bindings.inc.rs");
}
pub use autogened::*;
// TODO: For #defines that affect memory layout, we need to check for them
// on build and fail if they're wrong. e.g. USE_FLONUM *must* be true.
// These are functions we expose from C files, not in any header.
// Parsing it would result in a lot of duplicate definitions.
// Use bindgen for functions that are defined in headers or in yjit.c.
#[cfg_attr(test, allow(unused))] // We don't link against C code when testing
unsafe extern "C" {
pub fn rb_check_overloaded_cme(
me: *const rb_callable_method_entry_t,
ci: *const rb_callinfo,
) -> *const rb_callable_method_entry_t;
// Floats within range will be encoded without creating objects in the heap.
// (Range is 0x3000000000000001 to 0x4fffffffffffffff (1.7272337110188893E-77 to 2.3158417847463237E+77).
pub fn rb_float_new(d: f64) -> VALUE;
pub fn rb_hash_empty_p(hash: VALUE) -> VALUE;
pub fn rb_yjit_str_concat_codepoint(str: VALUE, codepoint: VALUE);
pub fn rb_str_setbyte(str: VALUE, index: VALUE, value: VALUE) -> VALUE;
pub fn rb_vm_splat_array(flag: VALUE, ary: VALUE) -> VALUE;
pub fn rb_vm_concat_array(ary1: VALUE, ary2st: VALUE) -> VALUE;
pub fn rb_vm_concat_to_array(ary1: VALUE, ary2st: VALUE) -> VALUE;
pub fn rb_vm_defined(
ec: EcPtr,
reg_cfp: CfpPtr,
op_type: rb_num_t,
obj: VALUE,
v: VALUE,
) -> bool;
pub fn rb_vm_set_ivar_id(obj: VALUE, idx: u32, val: VALUE) -> VALUE;
pub fn rb_vm_setinstancevariable(iseq: IseqPtr, obj: VALUE, id: ID, val: VALUE, ic: IVC);
pub fn rb_aliased_callable_method_entry(
me: *const rb_callable_method_entry_t,
) -> *const rb_callable_method_entry_t;
pub fn rb_vm_getclassvariable(iseq: IseqPtr, cfp: CfpPtr, id: ID, ic: ICVARC) -> VALUE;
pub fn rb_vm_setclassvariable(
iseq: IseqPtr,
cfp: CfpPtr,
id: ID,
val: VALUE,
ic: ICVARC,
) -> VALUE;
pub fn rb_vm_ic_hit_p(ic: IC, reg_ep: *const VALUE) -> bool;
pub fn rb_vm_stack_canary() -> VALUE;
pub fn rb_vm_push_cfunc_frame(cme: *const rb_callable_method_entry_t, recv_idx: c_int);
}
// Renames
pub use rb_insn_name as raw_insn_name;
pub use rb_get_ec_cfp as get_ec_cfp;
pub use rb_get_cfp_iseq as get_cfp_iseq;
pub use rb_get_cfp_pc as get_cfp_pc;
pub use rb_get_cfp_sp as get_cfp_sp;
pub use rb_get_cfp_self as get_cfp_self;
pub use rb_get_cfp_ep as get_cfp_ep;
pub use rb_get_cfp_ep_level as get_cfp_ep_level;
pub use rb_vm_base_ptr as get_cfp_bp;
pub use rb_get_cme_def_type as get_cme_def_type;
pub use rb_get_cme_def_body_attr_id as get_cme_def_body_attr_id;
pub use rb_get_cme_def_body_optimized_type as get_cme_def_body_optimized_type;
pub use rb_get_cme_def_body_optimized_index as get_cme_def_body_optimized_index;
pub use rb_get_cme_def_body_cfunc as get_cme_def_body_cfunc;
pub use rb_get_def_method_serial as get_def_method_serial;
pub use rb_get_def_original_id as get_def_original_id;
pub use rb_get_mct_argc as get_mct_argc;
pub use rb_get_mct_func as get_mct_func;
pub use rb_get_def_iseq_ptr as get_def_iseq_ptr;
pub use rb_iseq_encoded_size as get_iseq_encoded_size;
pub use rb_get_iseq_body_local_iseq as get_iseq_body_local_iseq;
pub use rb_get_iseq_body_iseq_encoded as get_iseq_body_iseq_encoded;
pub use rb_get_iseq_body_stack_max as get_iseq_body_stack_max;
pub use rb_get_iseq_body_type as get_iseq_body_type;
pub use rb_get_iseq_flags_has_lead as get_iseq_flags_has_lead;
pub use rb_get_iseq_flags_has_opt as get_iseq_flags_has_opt;
pub use rb_get_iseq_flags_has_kw as get_iseq_flags_has_kw;
pub use rb_get_iseq_flags_has_rest as get_iseq_flags_has_rest;
pub use rb_get_iseq_flags_has_post as get_iseq_flags_has_post;
pub use rb_get_iseq_flags_has_kwrest as get_iseq_flags_has_kwrest;
pub use rb_get_iseq_flags_has_block as get_iseq_flags_has_block;
pub use rb_get_iseq_flags_ambiguous_param0 as get_iseq_flags_ambiguous_param0;
pub use rb_get_iseq_flags_accepts_no_kwarg as get_iseq_flags_accepts_no_kwarg;
pub use rb_get_iseq_body_local_table_size as get_iseq_body_local_table_size;
pub use rb_get_iseq_body_param_keyword as get_iseq_body_param_keyword;
pub use rb_get_iseq_body_param_size as get_iseq_body_param_size;
pub use rb_get_iseq_body_param_lead_num as get_iseq_body_param_lead_num;
pub use rb_get_iseq_body_param_opt_num as get_iseq_body_param_opt_num;
pub use rb_get_iseq_body_param_opt_table as get_iseq_body_param_opt_table;
pub use rb_get_cikw_keyword_len as get_cikw_keyword_len;
pub use rb_get_cikw_keywords_idx as get_cikw_keywords_idx;
pub use rb_get_call_data_ci as get_call_data_ci;
pub use rb_FL_TEST as FL_TEST;
pub use rb_FL_TEST_RAW as FL_TEST_RAW;
pub use rb_RB_TYPE_P as RB_TYPE_P;
pub use rb_BASIC_OP_UNREDEFINED_P as BASIC_OP_UNREDEFINED_P;
pub use rb_RSTRUCT_LEN as RSTRUCT_LEN;
pub use rb_vm_ci_argc as vm_ci_argc;
pub use rb_vm_ci_mid as vm_ci_mid;
pub use rb_vm_ci_flag as vm_ci_flag;
pub use rb_vm_ci_kwarg as vm_ci_kwarg;
pub use rb_METHOD_ENTRY_VISI as METHOD_ENTRY_VISI;
pub use rb_RCLASS_ORIGIN as RCLASS_ORIGIN;
/// Helper so we can get a Rust string for insn_name()
pub fn insn_name(opcode: usize) -> String {
unsafe {
// Look up Ruby's NULL-terminated insn name string
let op_name = raw_insn_name(VALUE(opcode));
// Convert the op name C string to a Rust string and concat
let op_name = CStr::from_ptr(op_name).to_str().unwrap();
// Convert into an owned string
op_name.to_string()
}
}
pub fn insn_len(opcode: usize) -> u32 {
unsafe {
rb_insn_len(VALUE(opcode)).try_into().unwrap()
}
}
/// Opaque iseq type for opaque iseq pointers from vm_core.h
/// See: <https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs>
#[repr(C)]
pub struct rb_iseq_t {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
/// An object handle similar to VALUE in the C code. Our methods assume
/// that this is a handle. Sometimes the C code briefly uses VALUE as
/// an unsigned integer type and don't necessarily store valid handles but
/// thankfully those cases are rare and don't cross the FFI boundary.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[repr(transparent)] // same size and alignment as simply `usize`
pub struct VALUE(pub usize);
/// Pointer to an ISEQ
pub type IseqPtr = *const rb_iseq_t;
// Given an ISEQ pointer, convert PC to insn_idx
pub fn iseq_pc_to_insn_idx(iseq: IseqPtr, pc: *mut VALUE) -> Option<u16> {
let pc_zero = unsafe { rb_iseq_pc_at_idx(iseq, 0) };
unsafe { pc.offset_from(pc_zero) }.try_into().ok()
}
/// Given an ISEQ pointer and an instruction index, return an opcode.
pub fn iseq_opcode_at_idx(iseq: IseqPtr, insn_idx: u32) -> u32 {
let pc = unsafe { rb_iseq_pc_at_idx(iseq, insn_idx) };
unsafe { rb_iseq_opcode_at_pc(iseq, pc) as u32 }
}
/// Return a poison value to be set above the stack top to verify leafness.
#[cfg(not(test))]
pub fn vm_stack_canary() -> u64 {
unsafe { rb_vm_stack_canary() }.as_u64()
}
/// Avoid linking the C function in `cargo test`
#[cfg(test)]
pub fn vm_stack_canary() -> u64 {
0
}
/// Opaque execution-context type from vm_core.h
#[repr(C)]
pub struct rb_execution_context_struct {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
/// Alias for rb_execution_context_struct used by CRuby sometimes
pub type rb_execution_context_t = rb_execution_context_struct;
/// Pointer to an execution context (rb_execution_context_struct)
pub type EcPtr = *const rb_execution_context_struct;
// From method.h
#[repr(C)]
pub struct rb_method_definition_t {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
type rb_method_definition_struct = rb_method_definition_t;
/// Opaque cfunc type from method.h
#[repr(C)]
pub struct rb_method_cfunc_t {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
/// Opaque call-cache type from vm_callinfo.h
#[repr(C)]
pub struct rb_callcache {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
/// Opaque control_frame (CFP) struct from vm_core.h
#[repr(C)]
pub struct rb_control_frame_struct {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
/// Pointer to a control frame pointer (CFP)
pub type CfpPtr = *mut rb_control_frame_struct;
/// Opaque struct from vm_core.h
#[repr(C)]
pub struct rb_cref_t {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
#[derive(PartialEq)]
pub enum ClassRelationship {
Subclass,
Superclass,
NoRelation,
}
impl VALUE {
/// Dump info about the value to the console similarly to rp(VALUE)
pub fn dump_info(self) {
unsafe { rb_obj_info_dump(self) }
}
/// Return whether the value is truthy or falsy in Ruby -- only nil and false are falsy.
pub fn test(self) -> bool {
let VALUE(cval) = self;
let VALUE(qnilval) = Qnil;
(cval & !qnilval) != 0
}
/// Return true if the number is an immediate integer, flonum or static symbol
fn immediate_p(self) -> bool {
let VALUE(cval) = self;
let mask = RUBY_IMMEDIATE_MASK as usize;
(cval & mask) != 0
}
/// Return true if the value is a Ruby immediate integer, flonum, static symbol, nil or false
pub fn special_const_p(self) -> bool {
self.immediate_p() || !self.test()
}
/// Return true if the value is a heap object
pub fn heap_object_p(self) -> bool {
!self.special_const_p()
}
/// Return true if the value is a Ruby Fixnum (immediate-size integer)
pub fn fixnum_p(self) -> bool {
let VALUE(cval) = self;
let flag = RUBY_FIXNUM_FLAG as usize;
(cval & flag) == flag
}
/// Return true if the value is an immediate Ruby floating-point number (flonum)
pub fn flonum_p(self) -> bool {
let VALUE(cval) = self;
let mask = RUBY_FLONUM_MASK as usize;
let flag = RUBY_FLONUM_FLAG as usize;
(cval & mask) == flag
}
/// Return true if the value is a Ruby symbol (RB_SYMBOL_P)
pub fn symbol_p(self) -> bool {
self.static_sym_p() || self.dynamic_sym_p()
}
/// Return true for a static (non-heap) Ruby symbol (RB_STATIC_SYM_P)
pub fn static_sym_p(self) -> bool {
let VALUE(cval) = self;
let flag = RUBY_SYMBOL_FLAG as usize;
(cval & 0xff) == flag
}
/// Return true for a dynamic Ruby symbol (RB_DYNAMIC_SYM_P)
fn dynamic_sym_p(self) -> bool {
return if self.special_const_p() {
false
} else {
self.builtin_type() == RUBY_T_SYMBOL
}
}
/// Returns true if the value is T_HASH
pub fn hash_p(self) -> bool {
!self.special_const_p() && self.builtin_type() == RUBY_T_HASH
}
/// Returns true or false depending on whether the value is nil
pub fn nil_p(self) -> bool {
self == Qnil
}
pub fn string_p(self) -> bool {
self.class_of() == unsafe { rb_cString }
}
/// Read the flags bits from the RBasic object, then return a Ruby type enum (e.g. RUBY_T_ARRAY)
pub fn builtin_type(self) -> ruby_value_type {
(self.builtin_flags() & (RUBY_T_MASK as usize)) as ruby_value_type
}
pub fn builtin_flags(self) -> usize {
assert!(!self.special_const_p());
let VALUE(cval) = self;
let rbasic_ptr = cval as *const RBasic;
let flags_bits: usize = unsafe { (*rbasic_ptr).flags }.as_usize();
return flags_bits;
}
pub fn class_of(self) -> VALUE {
if !self.special_const_p() {
let builtin_type = self.builtin_type();
assert_ne!(builtin_type, RUBY_T_NONE, "YJIT should only see live objects");
assert_ne!(builtin_type, RUBY_T_MOVED, "YJIT should only see live objects");
}
unsafe { rb_yarv_class_of(self) }
}
/// Check if `self` is a subclass of `other`. Assumes both `self` and `other` are class
/// objects. Returns [`ClassRelationship::Subclass`] if `self <= other`,
/// [`ClassRelationship::Superclass`] if `other < self`, and [`ClassRelationship::NoRelation`]
/// otherwise.
pub fn is_subclass_of(self, other: VALUE) -> ClassRelationship {
assert!(unsafe { RB_TYPE_P(self, RUBY_T_CLASS) });
assert!(unsafe { RB_TYPE_P(other, RUBY_T_CLASS) });
match unsafe { rb_class_inherited_p(self, other) } {
Qtrue => ClassRelationship::Subclass,
Qfalse => ClassRelationship::Superclass,
Qnil => ClassRelationship::NoRelation,
// The API specifies that it will return Qnil in this case
_ => panic!("Unexpected return value from rb_class_inherited_p"),
}
}
pub fn is_frozen(self) -> bool {
unsafe { rb_obj_frozen_p(self) != VALUE(0) }
}
pub fn shape_too_complex(self) -> bool {
unsafe { rb_shape_obj_too_complex(self) }
}
pub fn shape_id_of(self) -> u32 {
unsafe { rb_shape_get_shape_id(self) }
}
pub fn shape_of(self) -> *mut rb_shape {
unsafe {
let shape = rb_shape_get_shape_by_id(self.shape_id_of());
if shape.is_null() {
panic!("Shape should not be null");
} else {
shape
}
}
}
pub fn embedded_p(self) -> bool {
unsafe {
FL_TEST_RAW(self, VALUE(ROBJECT_EMBED as usize)) != VALUE(0)
}
}
pub fn as_fixnum(self) -> i64 {
assert!(self.fixnum_p());
(self.0 >> 1) as i64
}
pub fn as_isize(self) -> isize {
let VALUE(is) = self;
is as isize
}
pub fn as_i32(self) -> i32 {
self.as_i64().try_into().unwrap()
}
pub fn as_u32(self) -> u32 {
let VALUE(i) = self;
i.try_into().unwrap()
}
pub fn as_i64(self) -> i64 {
let VALUE(i) = self;
i as i64
}
pub fn as_u64(self) -> u64 {
let VALUE(i) = self;
i.try_into().unwrap()
}
pub fn as_usize(self) -> usize {
let VALUE(us) = self;
us
}
pub fn as_ptr<T>(self) -> *const T {
let VALUE(us) = self;
us as *const T
}
pub fn as_mut_ptr<T>(self) -> *mut T {
let VALUE(us) = self;
us as *mut T
}
/// For working with opaque pointers and encoding null check.
/// Similar to [std::ptr::NonNull], but for `*const T`. `NonNull<T>`
/// is for `*mut T` while our C functions are setup to use `*const T`.
/// Casting from `NonNull<T>` to `*const T` is too noisy.
pub fn as_optional_ptr<T>(self) -> Option<*const T> {
let ptr: *const T = self.as_ptr();
if ptr.is_null() {
None
} else {
Some(ptr)
}
}
/// Assert that `self` is an iseq in debug builds
pub fn as_iseq(self) -> IseqPtr {
let ptr: IseqPtr = self.as_ptr();
#[cfg(debug_assertions)]
if !ptr.is_null() {
unsafe { rb_assert_iseq_handle(self) }
}
ptr
}
/// Assert that `self` is a method entry in debug builds
pub fn as_cme(self) -> *const rb_callable_method_entry_t {
let ptr: *const rb_callable_method_entry_t = self.as_ptr();
#[cfg(debug_assertions)]
if !ptr.is_null() {
unsafe { rb_assert_cme_handle(self) }
}
ptr
}
pub const fn fixnum_from_usize(item: usize) -> Self {
assert!(item <= (RUBY_FIXNUM_MAX as usize)); // An unsigned will always be greater than RUBY_FIXNUM_MIN
let k: usize = item.wrapping_add(item.wrapping_add(1));
VALUE(k)
}
}
impl From<IseqPtr> for VALUE {
/// For `.into()` convenience
fn from(iseq: IseqPtr) -> Self {
VALUE(iseq as usize)
}
}
impl From<*const rb_callable_method_entry_t> for VALUE {
/// For `.into()` convenience
fn from(cme: *const rb_callable_method_entry_t) -> Self {
VALUE(cme as usize)
}
}
impl From<&str> for VALUE {
fn from(value: &str) -> Self {
rust_str_to_ruby(value)
}
}
impl From<String> for VALUE {
fn from(value: String) -> Self {
rust_str_to_ruby(&value)
}
}
impl From<VALUE> for u64 {
fn from(value: VALUE) -> Self {
let VALUE(uimm) = value;
uimm as u64
}
}
impl From<VALUE> for i64 {
fn from(value: VALUE) -> Self {
let VALUE(uimm) = value;
assert!(uimm <= (i64::MAX as usize));
uimm as i64
}
}
impl From<VALUE> for i32 {
fn from(value: VALUE) -> Self {
let VALUE(uimm) = value;
assert!(uimm <= (i32::MAX as usize));
uimm.try_into().unwrap()
}
}
impl From<VALUE> for u16 {
fn from(value: VALUE) -> Self {
let VALUE(uimm) = value;
uimm.try_into().unwrap()
}
}
/// Produce a Ruby string from a Rust string slice
pub fn rust_str_to_ruby(str: &str) -> VALUE {
unsafe { rb_utf8_str_new(str.as_ptr() as *const _, str.len() as i64) }
}
/// Produce a Ruby symbol from a Rust string slice
pub fn rust_str_to_sym(str: &str) -> VALUE {
let c_str = CString::new(str).unwrap();
let c_ptr: *const c_char = c_str.as_ptr();
unsafe { rb_id2sym(rb_intern(c_ptr)) }
}
/// Produce an owned Rust String from a C char pointer
pub fn cstr_to_rust_string(c_char_ptr: *const c_char) -> Option<String> {
assert!(c_char_ptr != std::ptr::null());
let c_str: &CStr = unsafe { CStr::from_ptr(c_char_ptr) };
match c_str.to_str() {
Ok(rust_str) => Some(rust_str.to_string()),
Err(_) => None
}
}
// Location is the file defining the method, colon, method name.
// Filenames are sometimes internal strings supplied to eval,
// so be careful with them.
pub fn iseq_get_location(iseq: IseqPtr, pos: u16) -> String {
let iseq_label = unsafe { rb_iseq_label(iseq) };
let iseq_path = unsafe { rb_iseq_path(iseq) };
let iseq_lineno = unsafe { rb_iseq_line_no(iseq, pos as usize) };
let mut s = if iseq_label == Qnil {
"None".to_string()
} else {
ruby_str_to_rust(iseq_label)
};
s.push_str("@");
if iseq_path == Qnil {
s.push_str("None");
} else {
s.push_str(&ruby_str_to_rust(iseq_path));
}
s.push_str(":");
s.push_str(&iseq_lineno.to_string());
s
}
// Convert a CRuby UTF-8-encoded RSTRING into a Rust string.
// This should work fine on ASCII strings and anything else
// that is considered legal UTF-8, including embedded nulls.
fn ruby_str_to_rust(v: VALUE) -> String {
let str_ptr = unsafe { rb_RSTRING_PTR(v) } as *mut u8;
let str_len: usize = unsafe { rb_RSTRING_LEN(v) }.try_into().unwrap();
let str_slice: &[u8] = unsafe { std::slice::from_raw_parts(str_ptr, str_len) };
match String::from_utf8(str_slice.to_vec()) {
Ok(utf8) => utf8,
Err(_) => String::new(),
}
}
/// A location in Rust code for integrating with debugging facilities defined in C.
/// Use the [src_loc!] macro to crate an instance.
pub struct SourceLocation {
pub file: &'static CStr,
pub line: c_int,
}
impl Debug for SourceLocation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}:{}", self.file.to_string_lossy(), self.line))
}
}
/// Make a [SourceLocation] at the current spot.
macro_rules! src_loc {
() => {
{
// Nul-terminated string with static lifetime, make a CStr out of it safely.
let file: &'static str = concat!(file!(), '\0');
$crate::cruby::SourceLocation {
file: unsafe { std::ffi::CStr::from_ptr(file.as_ptr().cast()) },
line: line!().try_into().unwrap(),
}
}
};
}
pub(crate) use src_loc;
/// Run GC write barrier. Required after making a new edge in the object reference
/// graph from `old` to `young`.
macro_rules! obj_written {
($old: expr, $young: expr) => {
let (old, young): (VALUE, VALUE) = ($old, $young);
let src_loc = $crate::cruby::src_loc!();
unsafe { rb_yjit_obj_written(old, young, src_loc.file.as_ptr(), src_loc.line) };
};
}
pub(crate) use obj_written;
/// Acquire the VM lock, make sure all other Ruby threads are asleep then run
/// some code while holding the lock. Returns whatever `func` returns.
/// Use with [src_loc!].
///
/// Required for code patching in the presence of ractors.
pub fn with_vm_lock<F, R>(loc: SourceLocation, func: F) -> R
where
F: FnOnce() -> R + UnwindSafe,
{
let file = loc.file.as_ptr();
let line = loc.line;
let mut recursive_lock_level: c_uint = 0;
unsafe { rb_yjit_vm_lock_then_barrier(&mut recursive_lock_level, file, line) };
let ret = match catch_unwind(func) {
Ok(result) => result,
Err(_) => {
// Theoretically we can recover from some of these panics,
// but it's too late if the unwind reaches here.
let _ = catch_unwind(|| {
// IO functions can panic too.
eprintln!(
"YJIT panicked while holding VM lock acquired at {}:{}. Aborting...",
loc.file.to_string_lossy(),
line,
);
});
std::process::abort();
}
};
unsafe { rb_yjit_vm_unlock(&mut recursive_lock_level, file, line) };
ret
}
/// At the moment, we abort in all cases we panic.
/// To aid with getting diagnostics in the wild without requiring people to set
/// RUST_BACKTRACE=1, register a panic hook that crash using rb_bug() for release builds.
/// rb_bug() might not be as good at printing a call trace as Rust's stdlib, but
/// it dumps some other info that might be relevant.
///
/// In case we want to start doing fancier exception handling with panic=unwind,
/// we can revisit this later. For now, this helps to get us good bug reports.
pub fn rb_bug_panic_hook() {
use std::env;
use std::panic;
use std::io::{stderr, Write};
// Probably the default hook. We do this very early during process boot.
let previous_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
// Not using `eprintln` to avoid double panic.
let _ = stderr().write_all(b"ruby: ZJIT has panicked. More info to follow...\n");
// Always show a Rust backtrace for release builds.
// You should set RUST_BACKTRACE=1 for dev builds.
let release_build = cfg!(not(debug_assertions));
if release_build {
unsafe { env::set_var("RUST_BACKTRACE", "1"); }
}
previous_hook(panic_info);
// Dump information about the interpreter for release builds.
// You may also use ZJIT_RB_BUG=1 to trigger this on dev builds.
if release_build || env::var("ZJIT_RB_BUG").is_ok() {
// Abort with rb_bug(). It has a length limit on the message.
let panic_message = &format!("{}", panic_info)[..];
let len = std::cmp::min(0x100, panic_message.len()) as c_int;
unsafe { rb_bug(b"ZJIT: %*s\0".as_ref().as_ptr() as *const c_char, len, panic_message.as_ptr()); }
}
}));
}
// Non-idiomatic capitalization for consistency with CRuby code
#[allow(non_upper_case_globals)]
pub const Qfalse: VALUE = VALUE(RUBY_Qfalse as usize);
#[allow(non_upper_case_globals)]
pub const Qnil: VALUE = VALUE(RUBY_Qnil as usize);
#[allow(non_upper_case_globals)]
pub const Qtrue: VALUE = VALUE(RUBY_Qtrue as usize);
#[allow(non_upper_case_globals)]
pub const Qundef: VALUE = VALUE(RUBY_Qundef as usize);
#[allow(unused)]
mod manual_defs {
use super::*;
pub const SIZEOF_VALUE: usize = 8;
pub const SIZEOF_VALUE_I32: i32 = SIZEOF_VALUE as i32;
pub const VALUE_BITS: u8 = 8 * SIZEOF_VALUE as u8;
pub const RUBY_LONG_MIN: isize = std::os::raw::c_long::MIN as isize;
pub const RUBY_LONG_MAX: isize = std::os::raw::c_long::MAX as isize;
pub const RUBY_FIXNUM_MIN: isize = RUBY_LONG_MIN / 2;
pub const RUBY_FIXNUM_MAX: isize = RUBY_LONG_MAX / 2;
// From vm_callinfo.h - uses calculation that seems to confuse bindgen
pub const VM_CALL_ARGS_SIMPLE: u32 = 1 << VM_CALL_ARGS_SIMPLE_bit;
pub const VM_CALL_ARGS_SPLAT: u32 = 1 << VM_CALL_ARGS_SPLAT_bit;
pub const VM_CALL_ARGS_BLOCKARG: u32 = 1 << VM_CALL_ARGS_BLOCKARG_bit;
pub const VM_CALL_FORWARDING: u32 = 1 << VM_CALL_FORWARDING_bit;
pub const VM_CALL_FCALL: u32 = 1 << VM_CALL_FCALL_bit;
pub const VM_CALL_KWARG: u32 = 1 << VM_CALL_KWARG_bit;
pub const VM_CALL_KW_SPLAT: u32 = 1 << VM_CALL_KW_SPLAT_bit;
pub const VM_CALL_TAILCALL: u32 = 1 << VM_CALL_TAILCALL_bit;
pub const VM_CALL_ZSUPER : u32 = 1 << VM_CALL_ZSUPER_bit;
pub const VM_CALL_OPT_SEND : u32 = 1 << VM_CALL_OPT_SEND_bit;
// From internal/struct.h - in anonymous enum, so we can't easily import it
pub const RSTRUCT_EMBED_LEN_MASK: usize = (RUBY_FL_USER7 | RUBY_FL_USER6 | RUBY_FL_USER5 | RUBY_FL_USER4 | RUBY_FL_USER3 |RUBY_FL_USER2 | RUBY_FL_USER1) as usize;
// From iseq.h - via a different constant, which seems to confuse bindgen
pub const ISEQ_TRANSLATED: usize = RUBY_FL_USER7 as usize;
// We'll need to encode a lot of Ruby struct/field offsets as constants unless we want to
// redeclare all the Ruby C structs and write our own offsetof macro. For now, we use constants.
pub const RUBY_OFFSET_RBASIC_FLAGS: i32 = 0; // struct RBasic, field "flags"
pub const RUBY_OFFSET_RBASIC_KLASS: i32 = 8; // struct RBasic, field "klass"
pub const RUBY_OFFSET_RARRAY_AS_HEAP_LEN: i32 = 16; // struct RArray, subfield "as.heap.len"
pub const RUBY_OFFSET_RARRAY_AS_HEAP_PTR: i32 = 32; // struct RArray, subfield "as.heap.ptr"
pub const RUBY_OFFSET_RARRAY_AS_ARY: i32 = 16; // struct RArray, subfield "as.ary"
pub const RUBY_OFFSET_RSTRUCT_AS_HEAP_PTR: i32 = 24; // struct RStruct, subfield "as.heap.ptr"
pub const RUBY_OFFSET_RSTRUCT_AS_ARY: i32 = 16; // struct RStruct, subfield "as.ary"
pub const RUBY_OFFSET_RSTRING_AS_HEAP_PTR: i32 = 24; // struct RString, subfield "as.heap.ptr"
pub const RUBY_OFFSET_RSTRING_AS_ARY: i32 = 24; // struct RString, subfield "as.embed.ary"
// Constants from rb_control_frame_t vm_core.h
pub const RUBY_OFFSET_CFP_PC: i32 = 0;
pub const RUBY_OFFSET_CFP_SP: i32 = 8;
pub const RUBY_OFFSET_CFP_ISEQ: i32 = 16;
pub const RUBY_OFFSET_CFP_SELF: i32 = 24;
pub const RUBY_OFFSET_CFP_EP: i32 = 32;
pub const RUBY_OFFSET_CFP_BLOCK_CODE: i32 = 40;
pub const RUBY_OFFSET_CFP_JIT_RETURN: i32 = 48;
pub const RUBY_SIZEOF_CONTROL_FRAME: usize = 56;
// Constants from rb_execution_context_t vm_core.h
pub const RUBY_OFFSET_EC_CFP: i32 = 16;
pub const RUBY_OFFSET_EC_INTERRUPT_FLAG: i32 = 32; // rb_atomic_t (u32)
pub const RUBY_OFFSET_EC_INTERRUPT_MASK: i32 = 36; // rb_atomic_t (u32)
pub const RUBY_OFFSET_EC_THREAD_PTR: i32 = 48;
// Constants from rb_thread_t in vm_core.h
pub const RUBY_OFFSET_THREAD_SELF: i32 = 16;
// Constants from iseq_inline_constant_cache (IC) and iseq_inline_constant_cache_entry (ICE) in vm_core.h
pub const RUBY_OFFSET_IC_ENTRY: i32 = 0;
pub const RUBY_OFFSET_ICE_VALUE: i32 = 8;
}
pub use manual_defs::*;
#[cfg(test)]
pub mod test_utils {
use std::{ptr::null, sync::Once};
use crate::{options::init_options, state::rb_zjit_enabled_p, state::ZJITState};
use super::*;
static RUBY_VM_INIT: Once = Once::new();
/// Boot and initialize the Ruby VM for Rust testing
fn boot_rubyvm() {
// Boot the VM
unsafe {
let mut var: VALUE = Qnil;
ruby_init_stack(&mut var as *mut VALUE as *mut _);
ruby_init();
crate::cruby::ids::init(); // for ID! usages in tests
}
// Set up globals for convenience
ZJITState::init(init_options());
// Enable zjit_* instructions
unsafe { rb_zjit_enabled_p = true; }
}
/// Make sure the Ruby VM is set up and run a given callback with rb_protect()
pub fn with_rubyvm<T>(mut func: impl FnMut() -> T) -> T {
RUBY_VM_INIT.call_once(|| boot_rubyvm());
// Set up a callback wrapper to store a return value
let mut result: Option<T> = None;
let mut data: &mut dyn FnMut() = &mut || {
// Store the result externally
result.replace(func());
};
// Invoke callback through rb_protect() so exceptions don't crash the process.
// "Fun" double pointer dance to get a thin function pointer to pass through C
unsafe extern "C" fn callback_wrapper(data: VALUE) -> VALUE {
// SAFETY: shorter lifetime than the data local in the caller frame
let callback: &mut &mut dyn FnMut() = unsafe { std::mem::transmute(data) };
callback();
Qnil
}
let mut state: c_int = 0;
unsafe { super::rb_protect(Some(callback_wrapper), VALUE((&mut data) as *mut _ as usize), &mut state) };
// TODO(alan): there should be a way to print the exception instead of swallowing it
assert_eq!(0, state, "Exceptional unwind in callback. Ruby exception?");
result.expect("Callback did not set result")
}
/// Compile an ISeq via `RubyVM::InstructionSequence.compile`.
pub fn compile_to_iseq(program: &str) -> *const rb_iseq_t {
with_rubyvm(|| {
let wrapped_iseq = compile_to_wrapped_iseq(program);
unsafe { rb_iseqw_to_iseq(wrapped_iseq) }
})
}
pub fn define_class(name: &str, superclass: VALUE) -> VALUE {
let name = CString::new(name).unwrap();
unsafe { rb_define_class(name.as_ptr(), superclass) }
}
/// Evaluate a given Ruby program
pub fn eval(program: &str) -> VALUE {
with_rubyvm(|| {
let wrapped_iseq = compile_to_wrapped_iseq(&unindent(program, false));
unsafe { rb_funcallv(wrapped_iseq, ID!(eval), 0, null()) }
})
}
/// Get the ISeq of a specified method
pub fn get_method_iseq(name: &str) -> *const rb_iseq_t {
let wrapped_iseq = eval(&format!("RubyVM::InstructionSequence.of(method(:{}))", name));
unsafe { rb_iseqw_to_iseq(wrapped_iseq) }
}
/// Remove the minimum indent from every line, skipping the first and last lines if `trim_lines`.
pub fn unindent(string: &str, trim_lines: bool) -> String {
// Break up a string into multiple lines
let mut lines: Vec<String> = string.split_inclusive("\n").map(|s| s.to_string()).collect();
if trim_lines { // raw string literals come with extra lines
lines.remove(0);
lines.remove(lines.len() - 1);
}
// Count the minimum number of spaces
let spaces = lines.iter().filter_map(|line| {
for (i, ch) in line.as_bytes().iter().enumerate() {
if *ch != b' ' {
return Some(i);
}
}
None
}).min().unwrap_or(0);
// Join lines, removing spaces
let mut unindented: Vec<u8> = vec![];
for line in lines.iter() {
if line.len() > spaces {
unindented.extend_from_slice(&line.as_bytes()[spaces..]);
} else {
unindented.extend_from_slice(&line.as_bytes());
}
}
String::from_utf8(unindented).unwrap()
}
/// Compile a program into a RubyVM::InstructionSequence object
fn compile_to_wrapped_iseq(program: &str) -> VALUE {
let bytes = program.as_bytes().as_ptr() as *const c_char;
unsafe {
let program_str = rb_utf8_str_new(bytes, program.len().try_into().unwrap());
rb_funcallv(rb_cISeq, ID!(compile), 1, &program_str)
}
}
}
#[cfg(test)]
pub use test_utils::*;
/// Interned ID values for Ruby symbols and method names.
/// See [type@crate::cruby::ID] and usages outside of YJIT.
pub(crate) mod ids {
use std::sync::atomic::AtomicU64;
/// Globals to cache IDs on boot. Atomic to use with relaxed ordering
/// so reads can happen without `unsafe`. Synchronization done through
/// the VM lock.
macro_rules! def_ids {
($(name: $ident:ident content: $str:literal)*) => {
$(
#[doc = concat!("[type@crate::cruby::ID] for `", stringify!($str), "`")]
pub static $ident: AtomicU64 = AtomicU64::new(0);
)*
pub(crate) fn init() {
$(
let content = &$str;
let ptr: *const u8 = content.as_ptr();
// Lookup and cache each ID
$ident.store(
unsafe { $crate::cruby::rb_intern2(ptr.cast(), content.len() as _) },
std::sync::atomic::Ordering::Relaxed
);
)*
}
}
}
def_ids! {
name: NULL content: b""
name: respond_to_missing content: b"respond_to_missing?"
name: to_ary content: b"to_ary"
name: to_s content: b"to_s"
name: eq content: b"=="
name: include_p content: b"include?"
name: compile content: b"compile"
name: eval content: b"eval"
}
}
/// Get an CRuby `ID` to an interned string, e.g. a particular method name.
macro_rules! ID {
($id_name:ident) => {
$crate::cruby::ids::$id_name.load(std::sync::atomic::Ordering::Relaxed)
}
}
pub(crate) use ID;
|