Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements compilation of new parameter types #449

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions Sources/Fuzzilli/Compiler/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,14 @@ public class JavaScriptCompiler {
try enterNewScope {
let beginCatch = emit(BeginCatch())
if tryStatement.catch.hasParameter {
map(tryStatement.catch.parameter.name, to: beginCatch.innerOutput)
let parameter = tryStatement.catch.parameter
switch parameter.parameter {
TobiasWienand marked this conversation as resolved.
Show resolved Hide resolved
case .identifierParameter(let identifier):
map(identifier.name, to: beginCatch.innerOutput)

default:
throw CompilerError.unsupportedFeatureError("Unsupported parameter type")
TobiasWienand marked this conversation as resolved.
Show resolved Hide resolved
}
}
for statement in tryStatement.catch.body {
try compileStatement(statement)
Expand Down Expand Up @@ -1071,14 +1078,69 @@ public class JavaScriptCompiler {
}

private func mapParameters(_ parameters: [Compiler_Protobuf_Parameter], to variables: ArraySlice<Variable>) {
saelo marked this conversation as resolved.
Show resolved Hide resolved
assert(parameters.count == variables.count)
for (param, v) in zip(parameters, variables) {
map(param.name, to: v)
var flatParameters: [String] = []
var expectedVariableCount = 0
for param in parameters {
switch param.parameter {
case .identifierParameter(let identifier):
flatParameters.append(identifier.name)
expectedVariableCount += 1
case .objectParameter(let object):
for subParam in object.parameters {
flatParameters.append(subParam.name)
expectedVariableCount += 1
}
case .arrayParameter(let array):
for element in array.elements {
flatParameters.append(element.name)
expectedVariableCount += 1
}
default:
TobiasWienand marked this conversation as resolved.
Show resolved Hide resolved
break
saelo marked this conversation as resolved.
Show resolved Hide resolved
}
}
assert(expectedVariableCount == variables.count, "The number of variables does not match the number of parameters.")
for (name, v) in zip(flatParameters, variables) {
map(name, to: v)
}
}

private func convertParameters(_ parameters: [Compiler_Protobuf_Parameter]) -> Parameters {
return Parameters(count: parameters.count)
var totalParameterCount = 0
var parameterTypes = [Parameters.ParameterType]()
var objectPropertyNames = [[String]]()

for param in parameters {
switch param.parameter {
case .identifierParameter(_):
totalParameterCount += 1
parameterTypes.append(.identifier)
case .objectParameter(let object):
let objectCount = object.parameters.count
totalParameterCount += objectCount
if (objectCount == 1) {
parameterTypes.append(.standaloneObject)
} else {
parameterTypes.append(.objectStart)
parameterTypes.append(contentsOf: Array(repeating: .objectMiddle, count: max(0, objectCount - 2)))
parameterTypes.append(.objectEnd)
}
objectPropertyNames.append(object.parameters.map { $0.name })
case .arrayParameter(let array):
let arrayCount = array.elements.count
totalParameterCount += arrayCount
if arrayCount == 1 {
parameterTypes.append(.standaloneArray)
} else {
parameterTypes.append(.arrayStart)
parameterTypes.append(contentsOf: Array(repeating: .arrayMiddle, count: max(0, arrayCount - 2)))
parameterTypes.append(.arrayEnd)
}
default:
break
}
}
return Parameters(count: totalParameterCount, parameterTypes: parameterTypes, objectPropertyNames: objectPropertyNames)
}

/// Convenience accessor for the currently active scope.
Expand Down
24 changes: 21 additions & 3 deletions Sources/Fuzzilli/Compiler/Parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,27 @@ function parse(script, proto) {
}

function visitParameter(param) {
assert(param.type == 'Identifier');
return make('Parameter', { name: param.name });
}
assert(param.type == 'Identifier' || param.type == 'ObjectPattern' || param.type == 'ArrayPattern');
if (param.type === 'Identifier') {
return make('IdentifierParameter', { identifierParameter: { name: param.name } });
} else if (param.type === 'ObjectPattern') {
const parameters = param.properties.map(property => {
assert(property.type === 'ObjectProperty');
assert(property.computed === false);
assert(property.extra && property.extra.shorthand === true);
assert(property.method === false);
assert(property.key.type === 'Identifier');
return { name: property.key.name };
});
return make('ObjectParameter', { objectParameter: { parameters } });
} else if (param.type === 'ArrayPattern') {
const elements = param.elements.map(element => {
assert(element.type === 'Identifier');
return { name: element.name };
});
return make('ArrayParameter', { arrayParameter: { elements } });
}
}

function visitVariableDeclaration(node) {
let kind;
Expand Down
18 changes: 17 additions & 1 deletion Sources/Fuzzilli/FuzzIL/JsOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,25 @@ public struct Parameters {
return Int(numParameters)
}

init(count: Int, hasRestParameter: Bool = false) {
enum ParameterType {
saelo marked this conversation as resolved.
Show resolved Hide resolved
TobiasWienand marked this conversation as resolved.
Show resolved Hide resolved
case identifier
case objectStart
case objectEnd
case objectMiddle
case standaloneObject
case arrayStart
case arrayEnd
case arrayMiddle
case standaloneArray
saelo marked this conversation as resolved.
Show resolved Hide resolved
}

var parameterTypes: [ParameterType]
TobiasWienand marked this conversation as resolved.
Show resolved Hide resolved
var objectPropertyNames: [[String]]
saelo marked this conversation as resolved.
Show resolved Hide resolved
init(count: Int, hasRestParameter: Bool = false, parameterTypes: [ParameterType] = [], objectPropertyNames: [[String]] = []) {
self.numParameters = UInt32(count)
self.hasRestParameter = hasRestParameter
self.parameterTypes = parameterTypes
saelo marked this conversation as resolved.
Show resolved Hide resolved
self.objectPropertyNames = objectPropertyNames
}
}

Expand Down
28 changes: 24 additions & 4 deletions Sources/Fuzzilli/Lifting/JavaScriptLifter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1356,11 +1356,31 @@ public class JavaScriptLifter: Lifter {
private func liftParameters(_ parameters: Parameters, as variables: [String]) -> String {
saelo marked this conversation as resolved.
Show resolved Hide resolved
assert(parameters.count == variables.count)
var paramList = [String]()
for v in variables {
if parameters.hasRestParameter && v == variables.last {
paramList.append("..." + v)
} else {
var objectPropertyIndex = 0
for (index, v) in variables.enumerated() {
let type = parameters.parameterTypes[index]
switch type {
case .identifier, .standaloneObject, .standaloneArray:
paramList.append(v)
case .objectStart:
let propertyNames = parameters.objectPropertyNames[objectPropertyIndex]
let firstProperty = propertyNames.first!
paramList.append("{ \(firstProperty): \(v)")
case .objectMiddle:
let propertyNames = parameters.objectPropertyNames[objectPropertyIndex]
let middleProperty = propertyNames[index % propertyNames.count]
paramList.append("\(middleProperty): \(v)")
case .objectEnd:
let propertyNames = parameters.objectPropertyNames[objectPropertyIndex]
let lastProperty = propertyNames.last!
paramList.append("\(lastProperty): \(v) }")
objectPropertyIndex += 1
case .arrayStart:
paramList.append("[\(v)")
case .arrayMiddle:
paramList.append("\(v)")
case .arrayEnd:
paramList.append("\(v)]")
}
}
return paramList.joined(separator: ", ")
Expand Down
Loading
Loading