Skip to content

Commit eb3444b

Browse files
authored
Merge Upstream version 2
2 parents af1c312 + d53bbaf commit eb3444b

12 files changed

Lines changed: 129 additions & 110 deletions

File tree

.github/workflows/swift.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: build
1+
name: build_test
22

33
on:
44
push:
@@ -7,10 +7,18 @@ on:
77
branches: [ main ]
88

99
jobs:
10-
build:
10+
build_test:
11+
timeout-minutes: 30
1112
strategy:
13+
# If macos-latest fails, we still don't want to cancel ubuntu-latest or the other way around.
14+
fail-fast: false
1215
matrix:
1316
os: [macos-latest, ubuntu-latest]
17+
kind: [debug]
18+
include:
19+
# On linux also build and test release.
20+
- os: ubuntu-latest
21+
kind: release
1422

1523
runs-on: ${{ matrix.os }}
1624

@@ -26,11 +34,14 @@ jobs:
2634
uses: swift-actions/setup-swift@v2
2735
with:
2836
swift-version: "6.0.3"
29-
3037
- uses: actions/checkout@v2
31-
3238
- name: Build
33-
run: swift build -v
34-
35-
- name: Run tests
36-
run: swift test -v
39+
run: swift build -c ${{ matrix.kind }} -v
40+
- name: Run tests with Node.js
41+
run: swift test -c ${{ matrix.kind }} -v
42+
- name: Install jsvu
43+
run: npm install jsvu -g
44+
- name: Install d8
45+
run: jsvu --os=default --engines=v8
46+
- name: Run tests with d8
47+
run: FUZZILLI_TEST_SHELL=~/.jsvu/engines/v8/v8 swift test -c ${{ matrix.kind }} -v

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,14 @@ public class ProgramBuilder {
19331933
}
19341934
}
19351935

1936+
// Like build(n:by) but forcing BuildingMode to generating. Splicing is an operation that
1937+
// affects the whole program, so we shouldn't roll a die on every buildRecursive() call in a
1938+
// code generator whether we'd want to splice an operation into the current block (which happens
1939+
// with the default mode .generatingAndSplicing).
1940+
public func buildRecursive(n budget: Int) {
1941+
build(n: budget, by: .generating)
1942+
}
1943+
19361944
/// Run ValueGenerators until we have created at least N new variables.
19371945
/// Returns both the number of generated instructions and of newly created variables.
19381946
@discardableResult
@@ -1998,7 +2006,7 @@ public class ProgramBuilder {
19982006
// We need to update the inputs later, so take note of the visible variables here.
19992007
let oldVisibleVariables = visibleVariables
20002008

2001-
build(n: defaultCodeGenerationAmount)
2009+
build(n: defaultCodeGenerationAmount, by: mode)
20022010

20032011
let newVisibleVariables = visibleVariables.filter { v in
20042012
let t = type(of: v)
@@ -2111,7 +2119,7 @@ public class ProgramBuilder {
21112119
// Check if we need to or can create types here.
21122120
createRequiredInputVariables(forTypes: inputTypes)
21132121
// Build into the block.
2114-
build(n: budgetPerYieldPoint)
2122+
buildRecursive(n: budgetPerYieldPoint)
21152123
// Call the next scheduled stub.
21162124
let _ = callNext()
21172125
numberOfGeneratedInstructions += code.count - codeSizePre
@@ -2970,6 +2978,12 @@ public class ProgramBuilder {
29702978
}
29712979
}
29722980

2981+
public func maybeReturnRandomJsVariable(_ prob: Double) {
2982+
if probability(prob) {
2983+
doReturn(randomJsVariable())
2984+
}
2985+
}
2986+
29732987
@discardableResult
29742988
public func yield(_ value: Variable? = nil) -> Variable {
29752989
if let argument = value {
@@ -3117,6 +3131,15 @@ public class ProgramBuilder {
31173131
return emit(CreateNamedAsyncDisposableVariable(name), withInputs: [initialValue]).output
31183132
}
31193133

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+
31203143
@discardableResult
31213144
public func eval(_ string: String, with arguments: [Variable] = [], hasOutput: Bool = false) -> Variable? {
31223145
let instr = emit(Eval(string, numArguments: arguments.count, hasOutput: hasOutput), withInputs: arguments)

Sources/Fuzzilli/CodeGen/CodeGenerators.swift

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public let CodeGenerators: [CodeGenerator] = [
268268
}
269269

270270
let o = b.buildObjectLiteral { obj in
271-
b.build(n: Int.random(in: 0...10))
271+
b.buildRecursive(n: Int.random(in: 0...10))
272272
}
273273

274274
objType = b.type(of: o)
@@ -344,7 +344,7 @@ public let CodeGenerators: [CodeGenerator] = [
344344

345345
// Create the class.
346346
let c = b.buildClassDefinition(withSuperclass: superclass, isExpression: probability(0.3)) { cls in
347-
b.build(n: defaultCodeGenerationAmount)
347+
b.buildRecursive(n: defaultCodeGenerationAmount)
348348
}
349349

350350
// And construct a few instances of it.
@@ -426,12 +426,11 @@ 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
434-
b.doReturn(b.randomJsVariable())
433+
b.maybeReturnRandomJsVariable(0.9)
435434
}
436435
}
437436
b.loadDisposableVariable(disposableVariable)
@@ -442,13 +441,12 @@ 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)) {
450448
args in
451-
b.doReturn(b.randomJsVariable())
449+
b.maybeReturnRandomJsVariable(0.9)
452450
}
453451
}
454452
b.loadAsyncDisposableVariable(asyncDisposableVariable)
@@ -861,7 +859,7 @@ public let CodeGenerators: [CodeGenerator] = [
861859
"ClassInstanceMethodEndGenerator",
862860
inContext: .single([.javascript, .subroutine, .method, .classMethod])
863861
) { b in
864-
b.doReturn(b.randomJsVariable())
862+
b.maybeReturnRandomJsVariable(0.9)
865863
b.emit(EndClassInstanceMethod())
866864
},
867865
]),
@@ -899,7 +897,7 @@ public let CodeGenerators: [CodeGenerator] = [
899897
"ClassInstanceComputedMethodEndGenerator",
900898
inContext: .single([.javascript, .subroutine, .method, .classMethod])
901899
) { b in
902-
b.doReturn(b.randomJsVariable())
900+
b.maybeReturnRandomJsVariable(0.9)
903901
b.emit(EndClassInstanceComputedMethod())
904902
},
905903
]),
@@ -1107,7 +1105,7 @@ public let CodeGenerators: [CodeGenerator] = [
11071105
"ClassStaticMethodEndGenerator",
11081106
inContext: .single([.javascript, .classMethod, .subroutine, .method])
11091107
) { b in
1110-
b.doReturn(b.randomJsVariable())
1108+
b.maybeReturnRandomJsVariable(0.9)
11111109
b.emit(EndClassStaticMethod())
11121110
},
11131111
]),
@@ -1145,7 +1143,7 @@ public let CodeGenerators: [CodeGenerator] = [
11451143
"ClassStaticComputedMethodEndGenerator",
11461144
inContext: .single([.javascript, .subroutine, .method, .classMethod])
11471145
) { b in
1148-
b.doReturn(b.randomJsVariable())
1146+
b.maybeReturnRandomJsVariable(0.9)
11491147
b.emit(EndClassStaticComputedMethod())
11501148
},
11511149
]),
@@ -1283,7 +1281,7 @@ public let CodeGenerators: [CodeGenerator] = [
12831281
"ClassPrivateInstanceMethodEndGenerator",
12841282
inContext: .single([.javascript, .subroutine, .method, .classMethod])
12851283
) { b in
1286-
b.doReturn(b.randomJsVariable())
1284+
b.maybeReturnRandomJsVariable(0.9)
12871285
b.emit(EndClassPrivateInstanceMethod())
12881286
},
12891287
]),
@@ -1345,7 +1343,7 @@ public let CodeGenerators: [CodeGenerator] = [
13451343
"ClassPrivateStaticMethodEndGenerator",
13461344
inContext: .single([.javascript, .subroutine, .method, .classMethod])
13471345
) { b in
1348-
b.doReturn(b.randomJsVariable())
1346+
b.maybeReturnRandomJsVariable(0.9)
13491347
b.emit(EndClassPrivateStaticMethod())
13501348
},
13511349

@@ -2414,7 +2412,7 @@ public let CodeGenerators: [CodeGenerator] = [
24142412
let loopVar = b.loadInt(0)
24152413
b.buildDoWhileLoop(
24162414
do: {
2417-
b.build(n: defaultCodeGenerationAmount)
2415+
b.buildRecursive(n: defaultCodeGenerationAmount)
24182416
b.unary(.PostInc, loopVar)
24192417
},
24202418
while: {
@@ -2462,7 +2460,7 @@ public let CodeGenerators: [CodeGenerator] = [
24622460
// Generate a for-loop without any loop variables.
24632461
let counter = b.loadInt(10)
24642462
b.buildForLoop({}, { b.unary(.PostDec, counter) }) {
2465-
b.build(n: 4)
2463+
b.buildRecursive(n: 4)
24662464
}
24672465
} else {
24682466
// Generate a for-loop with two loop variables.
@@ -2475,7 +2473,7 @@ public let CodeGenerators: [CodeGenerator] = [
24752473
b.unary(.PostDec, vs[1])
24762474
}
24772475
) { _ in
2478-
b.build(n: 4)
2476+
b.buildRecursive(n: 4)
24792477
}
24802478
}
24812479
},
@@ -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
},
@@ -2932,18 +2925,16 @@ public let CodeGenerators: [CodeGenerator] = [
29322925
if probability(0.5) {
29332926
imitation = b.buildObjectLiteral { obj in
29342927
obj.addMethod("valueOf", with: .parameters(n: 0)) { _ in
2935-
b.build(n: 3)
2928+
b.buildRecursive(n: 3)
29362929
b.doReturn(orig)
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
2946-
b.build(n: 3)
2937+
b.buildRecursive(n: 3)
29472938
b.doReturn(orig)
29482939
}
29492940
}
@@ -2961,14 +2952,14 @@ public let CodeGenerators: [CodeGenerator] = [
29612952
let constructor = b.getProperty("constructor", of: orig)
29622953
let cls = b.buildClassDefinition(withSuperclass: constructor, isExpression: probability(0.3)) {
29632954
_ in
2964-
b.build(n: 3)
2955+
b.buildRecursive(n: 3)
29652956
}
29662957
imitation = b.construct(
29672958
cls, withArgs: b.randomArguments(forCalling: cls))
29682959
} else {
29692960
imitation = b.buildObjectLiteral { obj in
29702961
obj.setPrototype(to: orig)
2971-
b.build(n: 3)
2962+
b.buildRecursive(n: 3)
29722963
}
29732964
}
29742965
} else {
@@ -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)) {

0 commit comments

Comments
 (0)