Skip to content

Commit

Permalink
Handle invalid property names gracefully in ProbingMutator
Browse files Browse the repository at this point in the history
Instead of just assert()ing that the name is valid, explicitly check for
that and log an error if an invalid property name is observed.

Drive-by: exclude Protobuf/gen_programproto.py in Package.swift to
fix compiler warnings.
  • Loading branch information
Samuel Groß committed Aug 15, 2023
1 parent 2e95ff9 commit 49449fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ let package = Package(
"libreprl",
"libcoverage"],
exclude: [
"Protobuf/sync.proto",
"Protobuf/gen_programproto.py",
"Protobuf/operations.proto",
"Protobuf/program.proto",
"Protobuf/sync.proto",
"Protobuf/README.md"],
resources: [
// The ast.proto file is required by the node.js parser
Expand Down
18 changes: 14 additions & 4 deletions Sources/Fuzzilli/Mutators/ProbingMutator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public class ProbingMutator: RuntimeAssistedMutator {
let propertyIsStored = result.stores.keys.contains(propertyName)

// Install the property, either as regular property or as a property accessor.
let property = parsePropertyName(propertyName)
guard let property = parsePropertyName(propertyName) else { return }
if probability(0.8) {
installRegularProperty(property, on: obj, using: b)
} else {
Expand All @@ -208,7 +208,7 @@ public class ProbingMutator: RuntimeAssistedMutator {

switch property {
case .regular(let name):
assert(name.rangeOfCharacter(from: .whitespacesAndNewlines) == nil)
assert(isValidPropertyName(name))
b.setProperty(name, of: obj, to: value)
case .element(let index):
b.setElement(index, of: obj, to: value)
Expand Down Expand Up @@ -253,7 +253,7 @@ public class ProbingMutator: RuntimeAssistedMutator {

switch property {
case .regular(let name):
assert(name.rangeOfCharacter(from: .whitespacesAndNewlines) == nil)
assert(isValidPropertyName(name))
b.configureProperty(name, of: obj, usingFlags: PropertyFlags.random(), as: config)
case .element(let index):
b.configureElement(index, of: obj, usingFlags: PropertyFlags.random(), as: config)
Expand Down Expand Up @@ -308,7 +308,12 @@ public class ProbingMutator: RuntimeAssistedMutator {
}
}

private func parsePropertyName(_ propertyName: String) -> Property {
private func isValidPropertyName(_ name: String) -> Bool {
// Currently only property names containing whitespaces or newlines are invalid.
return name.rangeOfCharacter(from: .whitespacesAndNewlines) == nil
}

private func parsePropertyName(_ propertyName: String) -> Property? {
// Anything that parses as an Int64 is an element index.
if let index = Int64(propertyName) {
return .element(index)
Expand All @@ -323,6 +328,11 @@ public class ProbingMutator: RuntimeAssistedMutator {
}

// Everything else is a regular property name.
guard isValidPropertyName(propertyName) else {
// Invalid property names should have been filtered out on the JavaScript side, so receiving them here is an error.
logger.error("Received invalid property name: \(propertyName)")
return nil
}
return .regular(propertyName)
}

Expand Down

0 comments on commit 49449fb

Please sign in to comment.