Skip to content

Commit 60b68c5

Browse files
authored
Implement LoadThis and LoadArguments operations (googleprojectzero#262)
1 parent 590b6b0 commit 60b68c5

14 files changed

+194
-6
lines changed

Sources/Fuzzilli/Core/CodeGenerators.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public let CodeGenerators: [CodeGenerator] = [
5050
b.loadNull()
5151
},
5252

53+
CodeGenerator("ThisGenerator") { b in
54+
b.loadThis()
55+
},
56+
57+
CodeGenerator("ArgumentsGenerator", inContext: .function) { b in
58+
assert(b.context.contains(.function))
59+
b.loadArguments()
60+
},
61+
5362
CodeGenerator("ObjectGenerator") { b in
5463
var initialProperties = [String: Variable]()
5564
for _ in 0..<Int.random(in: 0...10) {

Sources/Fuzzilli/Core/JavaScriptEnvironment.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ public class JavaScriptEnvironment: ComponentBase, Environment {
169169
registerBuiltin("NaN", ofType: .jsNaN)
170170
registerBuiltin("Infinity", ofType: .jsInfinity)
171171

172-
// Register pseudo builtins
173-
registerBuiltin("this", ofType: .object())
174-
registerBuiltin("arguments", ofType: .object())
175-
176172
for (builtin, type) in additionalBuiltins {
177173
registerBuiltin(builtin, ofType: type)
178174
}

Sources/Fuzzilli/Core/ProgramBuilder.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,16 @@ public class ProgramBuilder {
10091009
return perform(LoadNull()).output
10101010
}
10111011

1012+
@discardableResult
1013+
public func loadThis() -> Variable {
1014+
return perform(LoadThis()).output
1015+
}
1016+
1017+
@discardableResult
1018+
public func loadArguments() -> Variable {
1019+
return perform(LoadArguments()).output
1020+
}
1021+
10121022
@discardableResult
10131023
public func loadRegExp(_ value: String, _ flags: RegExpFlags) -> Variable {
10141024
return perform(LoadRegExp(value: value, flags: flags)).output

Sources/Fuzzilli/FuzzIL/AbstractInterpreter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ public struct AbstractInterpreter {
319319
case is LoadNull:
320320
set(instr.output, .undefined)
321321

322+
case is LoadThis:
323+
set(instr.output, .object())
324+
325+
case is LoadArguments:
326+
set(instr.output, .iterable)
327+
322328
case is LoadRegExp:
323329
set(instr.output, environment.regExpType)
324330

Sources/Fuzzilli/FuzzIL/Instruction.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ extension Instruction: ProtobufConvertible {
280280
$0.loadUndefined = Fuzzilli_Protobuf_LoadUndefined()
281281
case is LoadNull:
282282
$0.loadNull = Fuzzilli_Protobuf_LoadNull()
283+
case is LoadThis:
284+
$0.loadThis = Fuzzilli_Protobuf_LoadThis()
285+
case is LoadArguments:
286+
$0.loadArguments = Fuzzilli_Protobuf_LoadArguments()
283287
case let op as LoadRegExp:
284288
$0.loadRegExp = Fuzzilli_Protobuf_LoadRegExp.with { $0.value = op.value; $0.flags = op.flags.rawValue }
285289
case let op as CreateObject:
@@ -514,6 +518,10 @@ extension Instruction: ProtobufConvertible {
514518
op = LoadUndefined()
515519
case .loadNull(_):
516520
op = LoadNull()
521+
case .loadThis(_):
522+
op = LoadThis()
523+
case .loadArguments(_):
524+
op = LoadArguments()
517525
case .loadRegExp(let p):
518526
op = LoadRegExp(value: p.value, flags: RegExpFlags(rawValue: p.flags))
519527
case .createObject(let p):

Sources/Fuzzilli/FuzzIL/Operations.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ class LoadNull: Operation {
137137
}
138138
}
139139

140+
class LoadThis: Operation {
141+
init() {
142+
super.init(numInputs: 0, numOutputs: 1, attributes: [.isPure])
143+
}
144+
}
145+
146+
class LoadArguments: Operation {
147+
init() {
148+
super.init(numInputs: 0, numOutputs: 1, attributes: [.isPure])
149+
}
150+
}
151+
140152
public struct RegExpFlags: OptionSet, Hashable {
141153
public let rawValue: UInt32
142154

Sources/Fuzzilli/FuzzIL/Semantics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ extension Instruction {
111111
canFold = true
112112
case (is LoadNull, is LoadNull):
113113
canFold = true
114+
case (is LoadThis, is LoadThis):
115+
canFold = false
116+
case (is LoadArguments, is LoadArguments):
117+
canFold = false
114118
case (let op1 as LoadRegExp, let op2 as LoadRegExp):
115119
canFold = op1.value == op2.value && op1.flags == op2.flags
116120
case (let op1 as LoadBuiltin, let op2 as LoadBuiltin):

Sources/Fuzzilli/Lifting/FuzzILLifter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public class FuzzILLifter: Lifter {
4949
case is LoadNull:
5050
w.emit("\(instr.output) <- LoadNull")
5151

52+
case is LoadThis:
53+
w.emit("\(instr.output) <- LoadThis")
54+
55+
case is LoadArguments:
56+
w.emit("\(instr.output) <- LoadArguments")
57+
5258
case let op as CreateObject:
5359
var properties = [String]()
5460
for (index, propertyName) in op.propertyNames.enumerated() {

Sources/Fuzzilli/Lifting/JavaScriptLifter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ public class JavaScriptLifter: Lifter {
182182
case is LoadNull:
183183
output = Literal.new("null")
184184

185+
case is LoadThis:
186+
output = Literal.new("this")
187+
188+
case is LoadArguments:
189+
output = Literal.new("arguments")
190+
185191
case let op as CreateObject:
186192
var properties = [String]()
187193
for (index, propertyName) in op.propertyNames.enumerated() {

Sources/Fuzzilli/Protobuf/operations.pb.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ public struct Fuzzilli_Protobuf_LoadNull {
330330
public init() {}
331331
}
332332

333+
public struct Fuzzilli_Protobuf_LoadThis {
334+
// SwiftProtobuf.Message conformance is added in an extension below. See the
335+
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
336+
// methods supported on all messages.
337+
338+
public var unknownFields = SwiftProtobuf.UnknownStorage()
339+
340+
public init() {}
341+
}
342+
343+
public struct Fuzzilli_Protobuf_LoadArguments {
344+
// SwiftProtobuf.Message conformance is added in an extension below. See the
345+
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
346+
// methods supported on all messages.
347+
348+
public var unknownFields = SwiftProtobuf.UnknownStorage()
349+
350+
public init() {}
351+
}
352+
333353
public struct Fuzzilli_Protobuf_LoadRegExp {
334354
// SwiftProtobuf.Message conformance is added in an extension below. See the
335355
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
@@ -1593,6 +1613,44 @@ extension Fuzzilli_Protobuf_LoadNull: SwiftProtobuf.Message, SwiftProtobuf._Mess
15931613
}
15941614
}
15951615

1616+
extension Fuzzilli_Protobuf_LoadThis: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1617+
public static let protoMessageName: String = _protobuf_package + ".LoadThis"
1618+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
1619+
1620+
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1621+
while let _ = try decoder.nextFieldNumber() {
1622+
}
1623+
}
1624+
1625+
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
1626+
try unknownFields.traverse(visitor: &visitor)
1627+
}
1628+
1629+
public static func ==(lhs: Fuzzilli_Protobuf_LoadThis, rhs: Fuzzilli_Protobuf_LoadThis) -> Bool {
1630+
if lhs.unknownFields != rhs.unknownFields {return false}
1631+
return true
1632+
}
1633+
}
1634+
1635+
extension Fuzzilli_Protobuf_LoadArguments: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
1636+
public static let protoMessageName: String = _protobuf_package + ".LoadArguments"
1637+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap()
1638+
1639+
public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
1640+
while let _ = try decoder.nextFieldNumber() {
1641+
}
1642+
}
1643+
1644+
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
1645+
try unknownFields.traverse(visitor: &visitor)
1646+
}
1647+
1648+
public static func ==(lhs: Fuzzilli_Protobuf_LoadArguments, rhs: Fuzzilli_Protobuf_LoadArguments) -> Bool {
1649+
if lhs.unknownFields != rhs.unknownFields {return false}
1650+
return true
1651+
}
1652+
}
1653+
15961654
extension Fuzzilli_Protobuf_LoadRegExp: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
15971655
public static let protoMessageName: String = _protobuf_package + ".LoadRegExp"
15981656
public static let _protobuf_nameMap: SwiftProtobuf._NameMap = [

0 commit comments

Comments
 (0)