forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScriptLifter.swift
453 lines (368 loc) · 18.6 KB
/
JavaScriptLifter.swift
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Lifts a FuzzIL program to JavaScript.
public class JavaScriptLifter: ComponentBase, Lifter {
/// Prefix and suffix to surround the emitted code in
private let prefix: String
private let suffix: String
/// The inlining policy to follow. This influences the look of the emitted code.
let policy: InliningPolicy
/// The identifier to refer to the global object.
///
/// For node.js this is "global", otherwise probably just "this".
let globalObjectIdentifier: String
/// Supported versions of the ECMA standard.
enum ECMAScriptVersion {
case es5
case es6
}
/// The version of the ECMAScript standard that this lifter generates code for.
let version: ECMAScriptVersion
public init(prefix: String = "", suffix: String = "", inliningPolicy: InliningPolicy, globalObjectIdentifier: String = "this") {
self.prefix = prefix
self.suffix = suffix
self.policy = inliningPolicy
self.globalObjectIdentifier = globalObjectIdentifier
self.version = .es6
super.init(name: "JavaScriptLifter")
}
public func lift(_ program: Program, withOptions options: LiftingOptions) -> String {
var w = ScriptWriter()
// Analyze the program to determine the uses of a variable
var analyzer = DefUseAnalyzer(for: program)
// Need an abstract interpreter if we are dumping type information as well
var interpreter = AbstractInterpreter(for: fuzzer)
// Associates variables with the expressions that produce them
var expressions = VariableMap<Expression>()
func expr(for v: Variable) -> Expression {
return expressions[v] ?? Identifier.new(v.identifier)
}
w.emitBlock(prefix)
let varDecl = version == .es6 ? "let" : "var"
let constDecl = version == .es6 ? "const" : "var"
for instr in program {
// Convenience access to inputs
func input(_ idx: Int) -> Expression {
return expr(for: instr.input(idx))
}
func value(_ idx: Int) -> Any? {
switch analyzer.definition(of: instr.input(idx)).operation {
case let op as LoadInteger:
return op.value
case let op as LoadFloat:
return op.value
case let op as LoadString:
return op.value
default:
return nil
}
}
// Interpret to compute type information if requested
if options.contains(.dumpTypes) {
interpreter.execute(instr)
}
var output: Expression? = nil
switch instr.operation {
case is Nop:
break
case let op as LoadInteger:
output = NumberLiteral.new(String(op.value))
case let op as LoadFloat:
if op.value.isNaN {
output = Identifier.new("NaN")
} else if op.value.isEqual(to: -Double.infinity) {
output = UnaryExpression.new("-Infinity")
} else if op.value.isEqual(to: Double.infinity) {
output = Identifier.new("Infinity")
} else {
output = NumberLiteral.new(String(op.value))
}
case let op as LoadString:
output = Literal.new() <> "\"" <> op.value <> "\""
case let op as LoadBoolean:
output = Literal.new(op.value ? "true" : "false")
case is LoadUndefined:
output = Identifier.new("undefined")
case is LoadNull:
output = Literal.new("null")
case let op as CreateObject:
var properties = [String]()
for (index, propertyName) in op.propertyNames.enumerated() {
properties.append(propertyName + ":" + input(index))
}
output = ObjectLiteral.new("{" + properties.joined(separator: ",") + "}")
case is CreateArray:
let elems = instr.inputs.map({ expr(for: $0).text }).joined(separator: ",")
output = ArrayLiteral.new("[" + elems + "]")
case let op as CreateObjectWithSpread:
var properties = [String]()
for (index, propertyName) in op.propertyNames.enumerated() {
properties.append(propertyName + ":" + input(index))
}
// Remaining ones are spread.
for v in instr.inputs.dropFirst(properties.count) {
properties.append("..." + expr(for: v).text)
}
output = ObjectLiteral.new("{" + properties.joined(separator: ",") + "}")
case let op as CreateArrayWithSpread:
var elems = [String]()
for (i, v) in instr.inputs.enumerated() {
if op.spreads[i] {
elems.append("..." + expr(for: v).text)
} else {
elems.append(expr(for: v).text)
}
}
output = ArrayLiteral.new("[" + elems.joined(separator: ",") + "]")
case let op as LoadBuiltin:
output = Identifier.new(op.builtinName)
case let op as LoadProperty:
output = MemberExpression.new() <> input(0) <> "." <> op.propertyName
case let op as StoreProperty:
let dest = MemberExpression.new() <> input(0) <> "." <> op.propertyName
let expr = AssignmentExpression.new() <> dest <> " = " <> input(1)
w.emit(expr)
case let op as DeleteProperty:
let target = MemberExpression.new() <> input(0) <> "." <> op.propertyName
let expr = UnaryExpression.new() <> "delete " <> target
w.emit(expr)
case let op as LoadElement:
output = MemberExpression.new() <> input(0) <> "[" <> op.index <> "]"
case let op as StoreElement:
let dest = MemberExpression.new() <> input(0) <> "[" <> op.index <> "]"
let expr = AssignmentExpression.new() <> dest <> " = " <> input(1)
w.emit(expr)
case let op as DeleteElement:
let target = MemberExpression.new() <> input(0) <> "[" <> op.index <> "]"
let expr = UnaryExpression.new() <> "delete " <> target
w.emit(expr)
case is LoadComputedProperty:
output = MemberExpression.new() <> input(0) <> "[" <> input(1).text <> "]"
case is StoreComputedProperty:
let dest = MemberExpression.new() <> input(0) <> "[" <> input(1).text <> "]"
let expr = AssignmentExpression.new() <> dest <> " = " <> input(2)
w.emit(expr)
case is DeleteComputedProperty:
let target = MemberExpression.new() <> input(0) <> "[" <> input(1).text <> "]"
let expr = UnaryExpression.new() <> "delete " <> target
w.emit(expr)
case is TypeOf:
output = UnaryExpression.new() <> "typeof " <> input(0)
case is InstanceOf:
output = BinaryExpression.new() <> input(0) <> " instanceof " <> input(1)
case is In:
output = BinaryExpression.new() <> input(0) <> " in " <> input(1)
case let op as BeginFunctionDefinition:
var identifiers = instr.innerOutputs.map({ $0.identifier })
if op.hasRestParam, let last = instr.innerOutputs.last {
identifiers[identifiers.endIndex - 1] = "..." + last.identifier
}
let params = identifiers.joined(separator: ",")
w.emit("function \(instr.output)(\(params)) {")
w.increaseIndentionLevel()
if (op.isJSStrictMode) {
w.emit("'use strict'")
}
case is Return:
w.emit("return \(input(0));")
case is EndFunctionDefinition:
w.decreaseIndentionLevel()
w.emit("}")
case is CallFunction:
let arguments = instr.inputs.dropFirst().map({ expr(for: $0).text })
output = CallExpression.new() <> input(0) <> "(" <> arguments.joined(separator: ",") <> ")"
case let op as CallMethod:
let arguments = instr.inputs.dropFirst().map({ expr(for: $0).text })
let method = MemberExpression.new() <> input(0) <> "." <> op.methodName
output = CallExpression.new() <> method <> "(" <> arguments.joined(separator: ",") <> ")"
case is Construct:
let arguments = instr.inputs.dropFirst().map({ expr(for: $0).text })
output = NewExpression.new() <> "new " <> input(0) <> "(" <> arguments.joined(separator: ",") <> ")"
case let op as CallFunctionWithSpread:
var arguments = [String]()
for (i, v) in instr.inputs.dropFirst().enumerated() {
if op.spreads[i] {
arguments.append("..." + expr(for: v).text)
} else {
arguments.append(expr(for: v).text)
}
}
output = CallExpression.new() <> input(0) <> "(" <> arguments.joined(separator: ",") <> ")"
case let op as UnaryOperation:
if op.op == .Inc {
output = BinaryExpression.new() <> input(0) <> " + 1"
} else if op.op == .Dec {
output = BinaryExpression.new() <> input(0) <> " - 1"
} else {
output = UnaryExpression.new() <> op.op.token <> input(0)
}
case let op as BinaryOperation:
output = BinaryExpression.new() <> input(0) <> " " <> op.op.token <> " " <> input(1)
case is Phi:
w.emit("\(varDecl) \(instr.output) = \(input(0));")
case is Copy:
w.emit("\(instr.input(0)) = \(input(1));")
case let op as Compare:
output = BinaryExpression.new() <> input(0) <> " " <> op.comparator.token <> " " <> input(1)
case let op as Eval:
// Woraround until Strings implement the CVarArg protocol in the linux Foundation library...
// TODO can make this permanent, but then use different placeholder pattern
var string = op.string
for v in instr.inputs {
let range = string.range(of: "%@")!
string.replaceSubrange(range, with: expr(for: v).text)
}
w.emit(string)
case is BeginWith:
w.emit("with (\(input(0))) {")
w.increaseIndentionLevel()
case is EndWith:
w.decreaseIndentionLevel()
w.emit("}")
case let op as LoadFromScope:
output = Identifier.new(op.id)
case let op as StoreToScope:
w.emit("\(op.id) = \(input(0));")
case is BeginIf:
w.emit("if (\(input(0))) {")
w.increaseIndentionLevel()
case is BeginElse:
w.decreaseIndentionLevel()
w.emit("} else {")
w.increaseIndentionLevel()
case is EndIf:
w.decreaseIndentionLevel()
w.emit("}")
case let op as BeginWhile:
let cond = BinaryExpression.new() <> input(0) <> " " <> op.comparator.token <> " " <> input(1)
w.emit("while (\(cond)) {")
w.increaseIndentionLevel()
case is EndWhile:
w.decreaseIndentionLevel()
w.emit("}")
case is BeginDoWhile:
w.emit("do {")
w.increaseIndentionLevel()
case let op as EndDoWhile:
w.decreaseIndentionLevel()
let cond = BinaryExpression.new() <> input(0) <> " " <> op.comparator.token <> " " <> input(1)
w.emit("} while (\(cond));")
case let op as BeginFor:
let loopVar = Identifier.new(instr.innerOutput.identifier)
let cond = BinaryExpression.new() <> loopVar <> " " <> op.comparator.token <> " " <> input(1)
var expr: Expression
if let i = value(2) as? Int, i == 1 && op.op == .Add {
expr = PostfixExpression.new() <> loopVar <> "++"
} else if let i = value(2) as? Int, i == 1 && op.op == .Sub {
expr = PostfixExpression.new() <> loopVar <> "--"
} else {
let newValue = BinaryExpression.new() <> loopVar <> " " <> op.op.token <> " " <> input(2)
expr = AssignmentExpression.new() <> loopVar <> " = " <> newValue
}
w.emit("for (\(varDecl) \(loopVar) = \(input(0)); \(cond); \(expr)) {")
w.increaseIndentionLevel()
case is EndFor:
w.decreaseIndentionLevel()
w.emit("}")
case is BeginForIn:
w.emit("for (\(constDecl) \(instr.innerOutput) in \(input(0))) {")
w.increaseIndentionLevel()
case is EndForIn:
w.decreaseIndentionLevel()
w.emit("}")
case is BeginForOf:
w.emit("for (\(constDecl) \(instr.innerOutput) of \(input(0))) {")
w.increaseIndentionLevel()
case is EndForOf:
w.decreaseIndentionLevel()
w.emit("}")
case is Break:
w.emit("break;")
case is Continue:
w.emit("continue;")
case is BeginTry:
w.emit("try {")
w.increaseIndentionLevel()
case is BeginCatch:
w.decreaseIndentionLevel()
w.emit("} catch(\(instr.innerOutput)) {")
w.increaseIndentionLevel()
case is EndTryCatch:
w.decreaseIndentionLevel()
w.emit("}")
case is ThrowException:
w.emit("throw \(input(0));")
case is Print:
w.emit("fuzzilli('FUZZILLI_PRINT', \(input(0)));")
case is InspectType:
w.emitBlock(
"""
{
try {
var proto = (\(input(0))).__proto__;
var typename = proto == null ? "Object" : proto.constructor.name;
fuzzilli('FUZZILLI_PRINT', typename);
} catch (e) {
fuzzilli('FUZZILLI_PRINT', "");
}
}
""")
case is InspectValue:
w.emitBlock(
"""
{
var properties = [];
var methods = [];
var obj = \(input(0));
while (obj != null) {
for (p of Object.getOwnPropertyNames(obj)) {
var prop;
try { prop = obj[p]; } catch (e) { continue; }
if (typeof(prop) === 'function') {
methods.push(p);
}
// Every method is also a property!
properties.push(p);
}
obj = obj.__proto__;
}
fuzzilli('FUZZILLI_PRINT', JSON.stringify({properties: properties, methods: methods}));
}
""")
case is EnumerateBuiltins:
w.emitBlock("""
{
var globals = Object.getOwnPropertyNames(\(globalObjectIdentifier));
fuzzilli('FUZZILLI_PRINT', (JSON.stringify({globals: globals}));
}
""")
default:
logger.fatal("Unhandled Operation: \(type(of: instr.operation))")
}
if let expression = output {
let v = instr.output
if policy.shouldInline(expression) && expression.canInline(instr, analyzer.usesIndices(of: v)) {
expressions[v] = expression
} else {
w.emit("\(constDecl) \(v) = \(expression);")
if options.contains(.dumpTypes) {
w.emitComment("\(v) = \(interpreter.type(of: v))")
}
}
}
}
w.emitBlock(suffix)
return w.code
}
}