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 the delete operator #461

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
53 changes: 47 additions & 6 deletions Sources/Fuzzilli/Compiler/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -983,15 +983,56 @@ public class JavaScriptCompiler {
}

case .unaryExpression(let unaryExpression):
let argument = try compileExpression(unaryExpression.argument)

if unaryExpression.operator == "typeof" {
let argument = try compileExpression(unaryExpression.argument)
return emit(TypeOf(), withInputs: [argument]).output
} else if unaryExpression.operator == "delete" {
guard case .memberExpression(let memberExpression) = unaryExpression.argument.expression else {
throw CompilerError.invalidNodeError("delete operator must be applied to a member expression")
}

let obj = try compileExpression(memberExpression.object)
// isGuarded is true if the member expression is optional (e.g., obj?.prop)
let isGuarded = memberExpression.isOptional

if !memberExpression.name.isEmpty {
// Deleting a non-computed property (e.g., delete obj.prop)
let propertyName = memberExpression.name
let instr = emit(
DeleteProperty(propertyName: propertyName, isGuarded: isGuarded),
withInputs: [obj]
)
return instr.output
} else {
// Deleting a computed property (e.g., delete obj[expr])
let propertyExpression = memberExpression.expression
let propertyExpr = propertyExpression.expression
let property = try compileExpression(propertyExpression)

if case .numberLiteral(let numberLiteral) = propertyExpr {
// Delete an element (e.g., delete arr[42])
let index = Int64(numberLiteral.value)
let instr = emit(
DeleteElement(index: index, isGuarded: isGuarded),
withInputs: [obj]
)
return instr.output
} else {
// Use DeleteComputedProperty for other computed properties (e.g., delete obj["key"])
let instr = emit(
DeleteComputedProperty(isGuarded: isGuarded),
withInputs: [obj, property]
)
return instr.output
}
}
} else {
guard let op = UnaryOperator(rawValue: unaryExpression.operator) else {
throw CompilerError.invalidNodeError("invalid unary operator: \(unaryExpression.operator)")
}
let argument = try compileExpression(unaryExpression.argument)
return emit(UnaryOperation(op), withInputs: [argument]).output
}
guard let op = UnaryOperator(rawValue: unaryExpression.operator) else {
throw CompilerError.invalidNodeError("invalid unary operator: \(unaryExpression.operator)")
}
return emit(UnaryOperation(op), withInputs: [argument]).output

case .binaryExpression(let binaryExpression):
let lhs = try compileExpression(binaryExpression.lhs)
Expand Down
29 changes: 29 additions & 0 deletions Tests/FuzzilliTests/CompilerTests/basic_delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
if (typeof output === 'undefined') output = console.log;

const obj = { a: 1 };
console.log(delete obj.a);
console.log(obj);

const propName = 'b';
obj[propName] = 2;
console.log(delete obj[propName]);
console.log(obj);

const arr = [1, 2, 3];
console.log(delete arr[1]);
console.log(arr);

const index = 0;
console.log(delete arr[index]);
console.log(arr);

const nestedObj = { a: { b: 2 } };
console.log(delete nestedObj?.a?.b);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add a test for something like

try {
    delete null.a;
} catch(e) {
    console.log(e.message);
}
console.log(delete null?.a);

Copy link
Contributor Author

@TobiasWienand TobiasWienand Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is compilable. However, this would cause DeleteProperty to be called, even though null is not an object. In Compiler.swift, I believe it is currently not possible to check here if obj is truly an object. We could either

  • Try preventing this in parser.js, although there are possibly more edge cases than just this
  • Ignore it and add a TODO
  • Introduce a new DeleteAny JSOperation

What would you prefer?

console.log(nestedObj);

try {
delete null.a;
} catch(e) {
console.log(e.message);
}
console.log(delete null?.a);
Loading