Skip to content

Commit fe1d0e5

Browse files
authored
Support feature/nullish coalesce (#471)
1 parent 5798638 commit fe1d0e5

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

Sources/Fuzzilli/FuzzIL/JSTyper.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ public struct JSTyper: Analyzer {
427427
.UnRShift:
428428
return maybeBigIntOr(.integer)
429429
case .LogicAnd,
430-
.LogicOr:
430+
.LogicOr,
431+
.NullCoalesce:
431432
return state.type(of: inputs[0]) | state.type(of: inputs[1])
432433
}
433434
}

Sources/Fuzzilli/FuzzIL/JsOperations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,8 @@ public enum BinaryOperator: String, CaseIterable {
14031403
case RShift = ">>"
14041404
case Exp = "**"
14051405
case UnRShift = ">>>"
1406+
// Nullish coalescing operator (??)
1407+
case NullCoalesce = "??"
14061408

14071409
var token: String {
14081410
return self.rawValue

Sources/Fuzzilli/Lifting/JavaScriptExploreLifting.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ struct JavaScriptExploreLifting {
247247
//
248248
function exploreObject(o) {
249249
if (o === null) {
250-
// Can't do anything with null.
251-
return NO_ACTION;
250+
return exploreNullish(o);
252251
}
253252
254253
// TODO: Add special handling for ArrayBuffers: most of the time, wrap these into a Uint8Array to be able to modify them.
@@ -405,6 +404,11 @@ struct JavaScriptExploreLifting {
405404
return action;
406405
}
407406
407+
function exploreNullish(v) {
408+
// Best thing we can do with nullish values is a NullCoalescing (??) operation.
409+
return new Action(OP_NULL_COALESCE, [exploredValueInput, Inputs.randomArgument()])
410+
}
411+
408412
// Explores the given value and returns an action to perform on it.
409413
function exploreValue(id, v) {
410414
if (isObject(v)) {
@@ -422,8 +426,7 @@ struct JavaScriptExploreLifting {
422426
} else if (isBoolean(v)) {
423427
return exploreBoolean(v);
424428
} else if (isUndefined(v)) {
425-
// Can't do anything with undefined.
426-
return NO_ACTION;
429+
return exploreNullish(v);
427430
} else {
428431
throw "Unexpected value type: " + typeof v;
429432
}

Sources/Fuzzilli/Lifting/JavaScriptRuntimeAssistedMutatorLifting.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ struct JavaScriptRuntimeAssistedMutatorLifting {
389389
const OP_LOGICAL_AND = 'LOGICAL_AND';
390390
const OP_LOGICAL_OR = 'LOGICAL_OR';
391391
const OP_LOGICAL_NOT = 'LOGICAL_NOT';
392+
const OP_NULL_COALESCE = 'NULL_COALESCE';
392393
393394
const OP_BITWISE_AND = 'BITWISE_AND';
394395
const OP_BITWISE_OR = 'BITWISE_OR';
@@ -511,6 +512,7 @@ struct JavaScriptRuntimeAssistedMutatorLifting {
511512
[OP_LOGICAL_AND]: (inputs) => inputs[0] && inputs[1],
512513
[OP_LOGICAL_OR]: (inputs) => inputs[0] || inputs[1],
513514
[OP_LOGICAL_NOT]: (inputs) => !inputs[0],
515+
[OP_NULL_COALESCE]: (inputs) => inputs[0] ?? inputs[1],
514516
[OP_BITWISE_AND]: (inputs) => inputs[0] & inputs[1],
515517
[OP_BITWISE_OR]: (inputs) => inputs[0] | inputs[1],
516518
[OP_BITWISE_XOR]: (inputs) => inputs[0] ^ inputs[1],

Sources/Fuzzilli/Mutators/RuntimeAssistedMutator.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public class RuntimeAssistedMutator: Mutator {
186186
case LogicalAnd = "LOGICAL_AND"
187187
case LogicalOr = "LOGICAL_OR"
188188
case LogicalNot = "LOGICAL_NOT"
189+
case NullCoalesce = "NULL_COALESCE"
189190
case BitwiseAnd = "BITWISE_AND"
190191
case BitwiseOr = "BITWISE_OR"
191192
case BitwiseXor = "BITWISE_XOR"
@@ -385,6 +386,8 @@ extension RuntimeAssistedMutator.Action {
385386
try translateBinaryOperation(.BitOr)
386387
case .BitwiseXor:
387388
try translateBinaryOperation(.Xor)
389+
case .NullCoalesce:
390+
try translateBinaryOperation(.NullCoalesce)
388391
case .LeftShift:
389392
try translateBinaryOperation(.LShift)
390393
case .SignedRightShift:

0 commit comments

Comments
 (0)