-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrubyval.rb
More file actions
217 lines (192 loc) · 4.23 KB
/
Copy pathrubyval.rb
File metadata and controls
217 lines (192 loc) · 4.23 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
require_relative 'config'
# Class that wraps a Ruby VALUE.
#
class RubyVal
# Constants
#
MASK_32BIT = 2**32 - 1
MASK_64BIT = 2**64 - 1
Qfalse = 0
Qtrue = 20
Qnil = 8
Qundef = 52
IMMEDIATE_MASK = 7
FIXNUM_FLAG = 1
FLONUM_MASK = 3
FLONUM_FLAG = 2
SYMBOL_FLAG = 12
SPECIAL_SHIFT = 8
T_MASK = 0x1F
FL_USHIFT = 12
# Based on FL_USERx in ruby.h
#
def self.fl_user_mask(n)
1 << (FL_USHIFT + n)
end
def initialize(val)
@val = val
end
def immediate?
@val & IMMEDIATE_MASK != 0
end
def fixnum?
# Is the low bit set?
#
@val & 1 != 0
end
def flonum?
@val & FLONUM_MASK == FLONUM_FLAG
end
def static_sym?
symbol_mask = ~(MASK_64BIT << SPECIAL_SHIFT) & MASK_64BIT
@val & symbol_mask == SYMBOL_FLAG
end
def test_true?
@val & ~Qnil != 0
end
def builtin_type
rbasic = DbgScript.create_typed_object("#{RUBYMOD}!RBasic", @val)
type = rbasic.flags.value & T_MASK
return DbgScript.resolve_enum('ruby_value_type', type), rbasic
end
# See 'rb_type' core function.
#
def type
if immediate?
return Fixnum if fixnum?
return Float if flonum?
return TrueClass if @val == Qtrue
return Symbol if static_sym?
return "undef" if @val == Qundef
elsif !test_true?
return NilClass if @val == Qnil
return FalseClass if @val == Qfalse
end
builtin_type
end
def value
t, rbasic = type
# Can't use 'case' statement because that uses '===' which doesn't help for
# comparing classes.
#
if t == Fixnum
@val >> 1
elsif t == NilClass
nil
elsif t == FalseClass or t == 'RUBY_T_FALSE'
false
elsif t == TrueClass or t == 'RUBY_T_TRUE'
true
elsif t == 'RUBY_T_ARRAY'
RArray.new(rbasic)
elsif t == 'RUBY_T_STRING'
RString.new(rbasic)
elsif t == 'RUBY_T_IMEMO'
Memo.new(@val)
else
"not implemented"
end
end
# Transform to a native Ruby value.
#
def to_native
val = value
if val.class == RArray
val.to_a
elsif val.class == RString
val.to_s
else
val
end
end
end
class Memo
def initialize(val)
@memo = DbgScript.create_typed_object(
"#{RUBYMOD}!MEMO", val)
end
def inspect
"#{DbgScript.get_nearest_sym(@memo.u3.func.value)}"
end
end
class RString
def initialize(rbasic)
@rbasic = rbasic
@str = DbgScript.create_typed_object(
"#{RUBYMOD}!RString", @rbasic.address)
end
def embedded?
no_embed_flag = RubyVal.fl_user_mask(1)
@rbasic.flags.value & no_embed_flag == 0
end
def size
embed_len_mask =
RubyVal.fl_user_mask(2) |
RubyVal.fl_user_mask(3) |
RubyVal.fl_user_mask(4) |
RubyVal.fl_user_mask(5) |
RubyVal.fl_user_mask(6)
embed_len_shift = RubyVal::FL_USHIFT + 2
flags = @rbasic.flags.value
if embedded?
(flags & embed_len_mask) >> embed_len_shift
else
@str.as.heap.len.value
end
end
# Obtain a ptr to the array.
#
def ptr
if embedded?
@str.as.ary
else
@str.as.heap.ptr.deref
end
end
def to_s
ptr.read_bytes(size)
end
end
class RArray
def initialize(rbasic)
@rbasic = rbasic
@arr = DbgScript.create_typed_object(
"#{RUBYMOD}!RArray", @rbasic.address)
end
def embedded?
embed_flag = RubyVal.fl_user_mask(1)
@rbasic.flags.value & embed_flag != 0
end
def size
embed_len_mask = RubyVal.fl_user_mask(4) | RubyVal.fl_user_mask(3)
embed_len_shift = RubyVal::FL_USHIFT + 3
flags = @rbasic.flags.value
if embedded?
(flags & embed_len_mask) >> embed_len_shift
else
@arr.as.heap.len.value
end
end
# Obtain a ptr to the array.
#
def ptr
if embedded?
@arr.as.ary
else
@arr.as.heap.ptr
end
end
# Generate a Ruby Array by cracking traversing the RArray.
#
def to_a
p = ptr
ary = []
for i in 0...size
# Each element is itself a Ruby VALUE.
#
val = RubyVal.new(p[i].value).to_native
ary << val
end
ary
end
end