-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir_builder_javap.rb
177 lines (158 loc) · 5.64 KB
/
ir_builder_javap.rb
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
# frozen_string_literal: true
require_relative 'ir/class'
require_relative 'ir/ssa_builder'
require_relative 'bytecode'
require 'open3'
require 'tmpdir'
# This class builds IR from the javap disasm files.
# It uses straightforward parser, based on regexps, therefore it is quite fragile and should not be used in
# the production code.
class IrBuilderJavap
TYPE_MAP = {Z: :T_INT, V: :T_VOID, C: :T_CHAR, B: :T_BYTE, I: :T_INT, J: :T_LONG, F: :T_FLOAT, D: :T_DOUBLE,
L: :T_OBJECT, '['.to_sym => :T_OBJECT}
def initialize
@class = nil
end
def build_from_file(filename)
basename = File.basename(filename, '.*')
classname = "#{Dir.tmpdir}/test/#{basename}.class"
command("javac -d #{Dir.tmpdir} #{filename}")
disasm = command("javap -p -l -c #{classname}")
build_from_lines(disasm.split("\n"), filename)
end
def build_from_lines(lines, filename)
@class = nil
method = nil
instructions = nil
linetable = nil
state = nil
lines.each do |line|
if line.empty? || line == '}'
method.instructions = instructions if instructions
method.set_linetable(linetable) if linetable
method = nil
state = nil
linetable = nil
instructions = nil
next
end
unless @class
class_name = /(public |private )?class ([^ ]*).*{/.match(line)
@class = JavaClass.new(class_name[2], filename) if class_name
next
end
case
when line =~ /^ (public|private|protected|static|final)/ || line == " #{@class.name}();"
field = parse_declaration(line)
if field.is_a? JavaMethod
method = field
@class.methods << method
else
@class.fields[field.name] = field
end
when line == ' Code:'
state = :CODE
instructions = []
when line == ' LineNumberTable:'
state = :LINETABLE
method.instructions = instructions
instructions = nil
linetable = []
else
if state == :CODE
instructions << build_instruction(line)
elsif state == :LINETABLE
data = line.strip.split
linetable << [data[2], data[1][0..-2]].map(&:to_i)
else
raise "Unexpected line: #{line}"
end
end
end
raise "No class found in '#{filename}'" if @class.nil?
@class.methods.each(&:construct_cfg)
@class.methods.each { |method| SsaBuilder.new(method).construct }
@class
end
def parse_declaration(line)
if line == ' static {};'
return JavaMethod.new('__StaticBlock__', [], @class, nil, ['static', 'static_initializer'])
end
return parse_method_decl(line) if line.include?('(')
data = line.strip[0..-1].split
name = data[-1].chomp(';')
modifiers = data[0..-2]
JavaField.new(name, @class, modifiers[-1], modifiers)
end
def parse_method_decl(line)
m = /((.*) )?(.+)\((.*)\);/.match(line.strip)
raise "Invalid method declaration" if m.nil?
modifiers = m[2] ? m[2].split : []
name = m[3]
args = m[4].split(', ')
JavaMethod.new(name, args, @class, nil, modifiers)
end
def parse_comment(comment, opcode)
comment.strip!
if comment.start_with? 'Method'
comment = comment[7..-1] # Remove 'Method '
m = /(.*):\((.*)\)(.+)/.match(comment)
raise "Method signature parse failed: #{comment}" unless m
name = m[1]
params = m[2]&.split(';')
return_type = TYPE_MAP[m[3][0].to_sym]
raise "Unsupported type: #{m[3]}" unless return_type
name_tkn = name.split('.')
if name_tkn.size == 2
cls = name_tkn[0]
name = name_tkn[1].delete('"')
else
raise "Invalid method name token" unless name_tkn.size == 1
cls = @class.name
name = name_tkn[0].delete('"')
end
params.prepend(cls) if opcode != :invokestatic
return MethodSignature.new(return_type, cls, name, params, [])
elsif comment.start_with? 'Field'
comment = comment[6..-1]
split = comment.chomp(';').split(':')
name = split[0]
field = @class.fields[name]
if field.nil?
field = JavaField.new(name, nil, split[1], [])
@class.fields[name] = field
end
field
end
end
def build_invoke(comment, pc, opcode, immediates, descr)
sign = parse_comment(comment, opcode)
raise "Invalid comment for invoke instruction: #{comment}" unless sign.is_a? MethodSignature
InvokeInstruction.new(pc, opcode, descr, immediates, sign)
end
def build_field(comment, pc, opcode, immediates, descr)
field = parse_comment(comment, opcode)
raise "Invalid comment for field instruction: #{comment}" unless field.is_a? JavaField
FieldInstruction.new(pc, opcode, descr, immediates, field)
end
def build_instruction(line)
main_tkn, comment_tkn = line.split('//')
tokens = main_tkn.strip.split
pc = tokens[0][0...-1].to_i
opcode = tokens[1].to_sym
immediates = tokens[2..-1].map { |x| x.chomp(',').delete_prefix('#') }.map(&:to_i)
descr = Bytecode::INFO[opcode.to_sym]
raise "Description not found for opcode: #{opcode}" unless descr
return build_invoke(comment_tkn, pc, opcode, immediates, descr) if opcode.start_with? 'invoke'
return build_field(comment_tkn, pc, opcode, immediates, descr) if opcode == :getfield || opcode == :putfield
return build_field(comment_tkn, pc, opcode, immediates, descr) if opcode == :getstatic || opcode == :putstatic
Instruction.new(pc, opcode, immediates, descr)
end
def command(cmd)
output, status = Open3.capture2e(cmd.to_s)
if status.signaled? || status.exitstatus != 0
raise "Command failed '#{cmd}':\n#{output}"
end
output
end
end