Skip to content

Commit bf67855

Browse files
mi-acV8-internal LUCI CQ
authored andcommitted
Refactoring - helper for Symbol properties
Also canonically hide the intermediate variable used for "Symbol" as done in some of the existing use cases. Bug: 446634535 Change-Id: I00794d4120057ef7e096ca913f827f8872d6ce41 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8646836 Reviewed-by: Matthias Liedtke <mliedtke@google.com> Commit-Queue: Michael Achenbach <machenbach@google.com>
1 parent 99d89a2 commit bf67855

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,6 +3131,15 @@ public class ProgramBuilder {
31313131
return emit(CreateNamedAsyncDisposableVariable(name), withInputs: [initialValue]).output
31323132
}
31333133

3134+
@discardableResult
3135+
public func createSymbolProperty(_ name: String) -> Variable {
3136+
let Symbol = createNamedVariable(forBuiltin: "Symbol")
3137+
// The Symbol constructor is just a "side effect" and probably
3138+
// shouldn't be used by following generators.
3139+
hide(Symbol)
3140+
return getProperty(name, of: Symbol)
3141+
}
3142+
31343143
@discardableResult
31353144
public func eval(_ string: String, with arguments: [Variable] = [], hasOutput: Bool = false) -> Variable? {
31363145
let instr = emit(Eval(string, numArguments: arguments.count, hasOutput: hasOutput), withInputs: arguments)

Sources/Fuzzilli/CodeGen/CodeGenerators.swift

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,7 @@ public let CodeGenerators: [CodeGenerator] = [
426426
"DisposableVariableGenerator", inContext: .single(.subroutine), inputs: .one
427427
) { b, val in
428428
assert(b.context.contains(.subroutine))
429-
let dispose = b.getProperty(
430-
"dispose", of: b.createNamedVariable(forBuiltin: "Symbol"))
429+
let dispose = b.createSymbolProperty("dispose")
431430
let disposableVariable = b.buildObjectLiteral { obj in
432431
obj.addProperty("value", as: val)
433432
obj.addComputedMethod(dispose, with: .parameters(n: 0)) { args in
@@ -442,8 +441,7 @@ public let CodeGenerators: [CodeGenerator] = [
442441
inputs: .one
443442
) { b, val in
444443
assert(b.context.contains(.asyncFunction))
445-
let asyncDispose = b.getProperty(
446-
"asyncDispose", of: b.createNamedVariable(forBuiltin: "Symbol"))
444+
let asyncDispose = b.createSymbolProperty("asyncDispose")
447445
let asyncDisposableVariable = b.buildObjectLiteral { obj in
448446
obj.addProperty("value", as: val)
449447
obj.addComputedMethod(asyncDispose, with: .parameters(n: 0)) {
@@ -2672,22 +2670,17 @@ public let CodeGenerators: [CodeGenerator] = [
26722670
CodeGenerator(
26732671
"WellKnownPropertyLoadGenerator", inputs: .preferred(.object())
26742672
) { b, obj in
2675-
let Symbol = b.createNamedVariable(forBuiltin: "Symbol")
2676-
// The Symbol constructor is just a "side effect" of this generator and probably shouldn't be used by following generators.
2677-
b.hide(Symbol)
2678-
let name = chooseUniform(from: JavaScriptEnvironment.wellKnownSymbols)
2679-
let propertyName = b.getProperty(name, of: Symbol)
2673+
let propertyName = b.createSymbolProperty(
2674+
chooseUniform(from: JavaScriptEnvironment.wellKnownSymbols))
26802675
let needGuard = b.type(of: obj).MayBe(.nullish)
26812676
b.getComputedProperty(propertyName, of: obj, guard: needGuard)
26822677
},
26832678

26842679
CodeGenerator(
26852680
"WellKnownPropertyStoreGenerator", inputs: .preferred(.object())
26862681
) { b, obj in
2687-
let Symbol = b.createNamedVariable(forBuiltin: "Symbol")
2688-
b.hide(Symbol)
2689-
let name = chooseUniform(from: JavaScriptEnvironment.wellKnownSymbols)
2690-
let propertyName = b.getProperty(name, of: Symbol)
2682+
let propertyName = b.createSymbolProperty(
2683+
chooseUniform(from: JavaScriptEnvironment.wellKnownSymbols))
26912684
let val = b.randomJsVariable()
26922685
b.setComputedProperty(propertyName, of: obj, to: val)
26932686
},
@@ -2937,9 +2930,7 @@ public let CodeGenerators: [CodeGenerator] = [
29372930
}
29382931
}
29392932
} else {
2940-
let toPrimitive = b.getProperty(
2941-
"toPrimitive",
2942-
of: b.createNamedVariable(forBuiltin: "Symbol"))
2933+
let toPrimitive = b.createSymbolProperty("toPrimitive")
29432934
imitation = b.buildObjectLiteral { obj in
29442935
obj.addComputedMethod(toPrimitive, with: .parameters(n: 0))
29452936
{ _ in
@@ -3081,9 +3072,7 @@ public let CodeGenerators: [CodeGenerator] = [
30813072
},
30823073

30833074
CodeGenerator("IteratorGenerator", produces: [.iterable]) { b in
3084-
let Symbol = b.createNamedVariable(forBuiltin: "Symbol")
3085-
b.hide(Symbol)
3086-
let iteratorSymbol = b.getProperty("iterator", of: Symbol)
3075+
let iteratorSymbol = b.createSymbolProperty("iterator")
30873076
b.hide(iteratorSymbol)
30883077
let iterableObject = b.buildObjectLiteral { obj in
30893078
obj.addComputedMethod(iteratorSymbol, with: .parameters(n: 0)) {

Tests/FuzzilliTests/LifterTest.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ class LifterTests: XCTestCase {
518518
let null = b.loadNull()
519519
let v4 = b.binary(v3, v1, with: .Add)
520520
let otherObject = b.createNamedVariable(forBuiltin: "SomeObject")
521-
let toPrimitive = b.getProperty("toPrimitive", of: b.createNamedVariable(forBuiltin: "Symbol"))
521+
let toPrimitive = b.createSymbolProperty("toPrimitive")
522522
b.buildObjectLiteral { obj in
523523
obj.addProperty("p1", as: v1)
524524
obj.addProperty("__proto__", as: null)
@@ -653,9 +653,7 @@ class LifterTests: XCTestCase {
653653
let two = b.loadInt(2)
654654
let baz = b.loadString("baz")
655655
let baz42 = b.binary(baz, i, with: .Add)
656-
let toPrimitive = b.getProperty(
657-
"toPrimitive",
658-
of: b.createNamedVariable(forBuiltin: "Symbol"))
656+
let toPrimitive = b.createSymbolProperty("toPrimitive")
659657
let sm = b.loadString("sm")
660658
let C = b.buildClassDefinition() { cls in
661659
cls.addInstanceProperty("foo")
@@ -1827,8 +1825,7 @@ class LifterTests: XCTestCase {
18271825
let b = fuzzer.makeBuilder()
18281826

18291827
let s = b.loadString("Hello World")
1830-
let Symbol = b.createNamedVariable(forBuiltin: "Symbol")
1831-
let iterator = b.getProperty("iterator", of: Symbol)
1828+
let iterator = b.createSymbolProperty("iterator")
18321829
let r = b.callComputedMethod(iterator, on: s)
18331830
b.callMethod("next", on: r)
18341831

@@ -3168,7 +3165,10 @@ class LifterTests: XCTestCase {
31683165
let f = b.buildPlainFunction(with: .parameters(n: 0)) { args in
31693166
let v1 = b.loadInt(1)
31703167
let v2 = b.loadInt(42)
3171-
let dispose = b.getProperty("dispose", of: b.createNamedVariable(forBuiltin: "Symbol"));
3168+
let numVariables = b.numberOfVisibleVariables
3169+
let dispose = b.createSymbolProperty("dispose");
3170+
// Test that the intermediate variable for "Symbol" stays hidden.
3171+
XCTAssertEqual(b.numberOfVisibleVariables, numVariables + 1)
31723172
let disposableVariable = b.buildObjectLiteral { obj in
31733173
obj.addProperty("value", as: v1)
31743174
obj.addComputedMethod(dispose, with: .parameters(n:0)) { args in
@@ -3220,7 +3220,7 @@ class LifterTests: XCTestCase {
32203220
let f = b.buildAsyncFunction(with: .parameters(n: 0)) { args in
32213221
let v1 = b.loadInt(1)
32223222
let v2 = b.loadInt(42)
3223-
let asyncDispose = b.getProperty("asyncDispose", of: b.createNamedVariable(forBuiltin: "Symbol"))
3223+
let asyncDispose = b.createSymbolProperty("asyncDispose")
32243224
let asyncDisposableVariable = b.buildObjectLiteral { obj in
32253225
obj.addProperty("value", as: v1)
32263226
obj.addComputedMethod(asyncDispose, with: .parameters(n:0)) { args in

0 commit comments

Comments
 (0)