Skip to content

Commit 52f4a0b

Browse files
mi-acv8-internal-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
Revert "[code generators] Introduce explicit "useInPrefix""
This reverts commit a799a23. Reason for revert: Suspected to crash with: Fatal error: Code generators must contain at least one generator to be used in the prefix Original change's description: > [code generators] Introduce explicit "useInPrefix" > > Before this CL, we chose which CodeGenerators to use in the prefix based > on IsValueGenerator property (= all stubs in the generator require no > inputs and produce something). > > However, we want to have CodeGenerators which satisfy that > property, but which are not used in the prefix. > > This CL solves this problem by adding an explicit way to mark which code > generators should be used in the prefix. > > CONV=fe946adb-0cf0-4d0b-a600-a45a70ecddad > TAG=agy > Bug: 526979176 > Change-Id: Ib5dd42efbaf082e7196992c00aa867a23c4609b0 > Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/9457934 > Commit-Queue: Marja Hölttä <marja@google.com> > Reviewed-by: Matthias Liedtke <mliedtke@google.com> Bug: 526979176 Change-Id: I1acc51b237c4ece57cf9ef7b4637a6bd16c6ebf6 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/9482515 Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Michael Achenbach <machenbach@google.com> Reviewed-by: Marja Hölttä <marja@google.com>
1 parent 8b79bbb commit 52f4a0b

7 files changed

Lines changed: 56 additions & 73 deletions

File tree

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,11 +2285,11 @@ public class ProgramBuilder {
22852285
build(n: budget, by: .generating)
22862286
}
22872287

2288-
/// Run generators marked with "useInPrefix" until we have created at least N new variables.
2288+
/// Run ValueGenerators until we have created at least N new variables.
22892289
/// Returns both the number of generated instructions and of newly created variables.
22902290
@discardableResult
22912291
public func buildValues(_ n: Int) -> (generatedInstructions: Int, generatedVariables: Int) {
2292-
var valueGenerators = fuzzer.codeGenerators.filter({ $0.useInPrefix })
2292+
var valueGenerators = fuzzer.codeGenerators.filter({ $0.isValueGenerator })
22932293
// Filter for the current context
22942294
valueGenerators = valueGenerators.filter { context.contains($0.requiredContext) }
22952295

Sources/Fuzzilli/CodeGen/CodeGenerator.swift

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ private struct GeneratorAdapter4Args: GeneratorAdapter {
6969
}
7070

7171
public class GeneratorStub: Contributor {
72+
/// Whether this code generator is a value generator. A value generator will create at least one new variable containing
73+
/// a newly created value (e.g. a primitive value or some kind of object). Further, value generators must be able to
74+
/// run even if there are no existing variables. This way, they can be used to "bootstrap" code generation.
75+
public var isValueStub: Bool {
76+
self.inputs.isEmpty && !self.produces.isEmpty
77+
}
78+
7279
/// How many different values of the same type ValueGenerators should aim to generate.
7380
public static let numberOfValuesToGenerateByValueGenerators = 3
7481

@@ -407,22 +414,9 @@ public class CodeGenerator {
407414
// Here, I think we might have different Contexts that each yield point could provide, e.g. [.javascript | .subroutine, .wasmFunction]. Unsure if there is a situation where this matters? instead of having .javascript | .subroutine | .wasmFunction?
408415
public let providedContexts: [Context]
409416

410-
// Whether this code generator shall be used for creating values when bootstrapping code generation.
411-
public let useInPrefix: Bool
412-
413-
public init(_ name: String, useInPrefix: Bool = false, _ generators: [GeneratorStub]) {
417+
public init(_ name: String, _ generators: [GeneratorStub]) {
414418
self.parts = generators
415419
self.name = name
416-
self.useInPrefix = useInPrefix
417-
418-
if useInPrefix {
419-
assert(
420-
generators.first!.inputs.isEmpty,
421-
"CodeGenerators used in the prefix must not require any inputs")
422-
assert(
423-
generators.contains { !$0.produces.isEmpty },
424-
"CodeGenerators used in the prefix must produce at least one value")
425-
}
426420

427421
// Calculate all contexts provided at any time by this CodeGenerator.
428422
var ctxSet = Set<Context>()
@@ -435,6 +429,12 @@ public class CodeGenerator {
435429
self.providedContexts = Array(ctxSet)
436430
}
437431

432+
// This essentially means that all stubs have no requirements.
433+
// Usually there is only a single element in the CodeGenerator if it is a ValueGenerator.
434+
public var isValueGenerator: Bool {
435+
return self.parts.allSatisfy { $0.isValueStub }
436+
}
437+
438438
// This is the context required by the first part of the CodeGenerator.
439439
public var requiredContext: Context {
440440
// This has to be a single one for the first Generator.
@@ -469,12 +469,10 @@ public class CodeGenerator {
469469

470470
public convenience init(
471471
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
472-
produces: [ILType] = [], provides: [Context] = [], useInPrefix: Bool = false,
473-
_ f: @escaping GeneratorFuncNoArgs
472+
produces: [ILType] = [], provides: [Context] = [], _ f: @escaping GeneratorFuncNoArgs
474473
) {
475474
self.init(
476475
name,
477-
useInPrefix: useInPrefix,
478476
[
479477
GeneratorStub(
480478
name: name, inputs: .none, produces: produces, context: context,
@@ -485,12 +483,10 @@ public class CodeGenerator {
485483
public convenience init(
486484
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
487485
producesComplex: [GeneratorStub.Constraint], provides: [Context] = [],
488-
useInPrefix: Bool = false,
489486
_ f: @escaping GeneratorFuncNoArgs
490487
) {
491488
self.init(
492489
name,
493-
useInPrefix: useInPrefix,
494490
[
495491
GeneratorStub(
496492
name: name, inputs: .none, produces: producesComplex, context: context,
@@ -501,13 +497,11 @@ public class CodeGenerator {
501497
public convenience init(
502498
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
503499
inputs: GeneratorStub.Inputs, produces: [ILType] = [], provides: [Context] = [],
504-
useInPrefix: Bool = false,
505500
_ f: @escaping GeneratorFunc1Arg
506501
) {
507502
assert(inputs.count == 1)
508503
self.init(
509504
name,
510-
useInPrefix: useInPrefix,
511505
[
512506
GeneratorStub(
513507
name: name, inputs: inputs, produces: produces, context: context,
@@ -518,12 +512,11 @@ public class CodeGenerator {
518512
public convenience init(
519513
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
520514
inputs: GeneratorStub.Inputs, producesComplex: [GeneratorStub.Constraint],
521-
provides: [Context] = [], useInPrefix: Bool = false, _ f: @escaping GeneratorFunc1Arg
515+
provides: [Context] = [], _ f: @escaping GeneratorFunc1Arg
522516
) {
523517
assert(inputs.count == 1)
524518
self.init(
525519
name,
526-
useInPrefix: useInPrefix,
527520
[
528521
GeneratorStub(
529522
name: name, inputs: inputs, produces: producesComplex, context: context,
@@ -534,13 +527,11 @@ public class CodeGenerator {
534527
public convenience init(
535528
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
536529
inputs: GeneratorStub.Inputs, produces: [ILType] = [], provides: [Context] = [],
537-
useInPrefix: Bool = false,
538530
_ f: @escaping GeneratorFunc2Args
539531
) {
540532
assert(inputs.count == 2)
541533
self.init(
542534
name,
543-
useInPrefix: useInPrefix,
544535
[
545536
GeneratorStub(
546537
name: name, inputs: inputs, produces: produces, context: context,
@@ -551,13 +542,11 @@ public class CodeGenerator {
551542
public convenience init(
552543
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
553544
inputs: GeneratorStub.Inputs, produces: [ILType] = [], provides: [Context] = [],
554-
useInPrefix: Bool = false,
555545
_ f: @escaping GeneratorFunc3Args
556546
) {
557547
assert(inputs.count == 3)
558548
self.init(
559549
name,
560-
useInPrefix: useInPrefix,
561550
[
562551
GeneratorStub(
563552
name: name, inputs: inputs, produces: produces, context: context,
@@ -568,13 +557,11 @@ public class CodeGenerator {
568557
public convenience init(
569558
_ name: String, inContext context: GeneratorStub.ContextRequirement = .single(.javascript),
570559
inputs: GeneratorStub.Inputs, produces: [ILType] = [], provides: [Context] = [],
571-
useInPrefix: Bool = false,
572560
_ f: @escaping GeneratorFunc4Args
573561
) {
574562
assert(inputs.count == 4)
575563
self.init(
576564
name,
577-
useInPrefix: useInPrefix,
578565
[
579566
GeneratorStub(
580567
name: name, inputs: inputs, produces: produces, context: context,

Sources/Fuzzilli/CodeGen/CodeGenerators.swift

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,12 @@ func makeObjectDestructForOfLoopGenerator(
258258
// These insert one or more instructions into a program.
259259
//
260260
public let CodeGenerators: [CodeGenerator] = [
261-
// Code generators with useInPrefix: true are used to "bootstrap" code generation by creating some initial variables
261+
//
262+
// Value Generators: Code Generators that generate one or more new values, i.e. they have a `produces` annotation.
263+
//
264+
// These behave like any other CodeGenerator in that they will be randomly chosen to generate code
265+
// and have a weight assigned to them to determine how frequently they are selected, but in addition
266+
// ValueGenerators are also used to "bootstrap" code generation by creating some initial variables
262267
// that following code can then operate on.
263268
//
264269
// These:
@@ -268,19 +273,19 @@ public let CodeGenerators: [CodeGenerator] = [
268273
// - Should generate |n| different values of the same type, but may generate fewer.
269274
// - May be recursive, for example to fill bodies of newly created blocks.
270275
//
271-
CodeGenerator("IntegerGenerator", produces: [.integer], useInPrefix: true) { b in
276+
CodeGenerator("IntegerGenerator", produces: [.integer]) { b in
272277
b.loadInt(b.randomInt())
273278
},
274279

275-
CodeGenerator("BigIntGenerator", produces: [.bigint], useInPrefix: true) { b in
280+
CodeGenerator("BigIntGenerator", produces: [.bigint]) { b in
276281
b.loadBigInt(b.randomInt())
277282
},
278283

279-
CodeGenerator("FloatGenerator", produces: [.float], useInPrefix: true) { b in
284+
CodeGenerator("FloatGenerator", produces: [.float]) { b in
280285
b.loadFloat(b.randomFloat())
281286
},
282287

283-
CodeGenerator("StringGenerator", produces: [.string], useInPrefix: true) { b in
288+
CodeGenerator("StringGenerator", produces: [.string]) { b in
284289
b.loadString(b.randomString())
285290
},
286291

@@ -301,22 +306,22 @@ public let CodeGenerators: [CodeGenerator] = [
301306
}
302307
},
303308

304-
CodeGenerator("BooleanGenerator", produces: [.boolean], useInPrefix: true) { b in
309+
CodeGenerator("BooleanGenerator", produces: [.boolean]) { b in
305310
// It's probably not too useful to generate multiple boolean values here.
306311
b.loadBool(Bool.random())
307312
},
308313

309-
CodeGenerator("UndefinedGenerator", produces: [.undefined], useInPrefix: true) { b in
314+
CodeGenerator("UndefinedGenerator", produces: [.undefined]) { b in
310315
// There is only one 'undefined' value, so don't generate it multiple times.
311316
b.loadUndefined()
312317
},
313318

314-
CodeGenerator("NullGenerator", produces: [.undefined], useInPrefix: true) { b in
319+
CodeGenerator("NullGenerator", produces: [.undefined]) { b in
315320
// There is only one 'null' value, so don't generate it multiple times.
316321
b.loadNull()
317322
},
318323

319-
CodeGenerator("ArrayGenerator", produces: [.jsArray], useInPrefix: true) { b in
324+
CodeGenerator("ArrayGenerator", produces: [.jsArray]) { b in
320325
// If we can only generate empty arrays, then only create one such array.
321326
if !b.hasVisibleJsVariables {
322327
b.createArray(with: [])
@@ -328,12 +333,12 @@ public let CodeGenerators: [CodeGenerator] = [
328333
}
329334
},
330335

331-
CodeGenerator("IntArrayGenerator", produces: [.jsArray], useInPrefix: true) { b in
336+
CodeGenerator("IntArrayGenerator", produces: [.jsArray]) { b in
332337
let values = (0..<Int.random(in: 1...10)).map({ _ in b.randomInt() })
333338
b.createIntArray(with: values)
334339
},
335340

336-
CodeGenerator("FloatArrayGenerator", produces: [.jsArray], useInPrefix: true) { b in
341+
CodeGenerator("FloatArrayGenerator", produces: [.jsArray]) { b in
337342
let values = (0..<Int.random(in: 1...10)).map({ _ in b.randomFloat() })
338343
b.createFloatArray(with: values)
339344
},
@@ -381,7 +386,7 @@ public let CodeGenerators: [CodeGenerator] = [
381386
b.callMethod(transitionMethod, on: Object, withArgs: [obj])
382387
},
383388

384-
CodeGenerator("BuiltinObjectInstanceGenerator", produces: [.object()], useInPrefix: true) {
389+
CodeGenerator("BuiltinObjectInstanceGenerator", produces: [.object()]) {
385390
b in
386391
let builtin = chooseUniform(from: [
387392
"Array", "Map", "WeakMap", "Set", "WeakSet", "Date",
@@ -438,7 +443,7 @@ public let CodeGenerators: [CodeGenerator] = [
438443
b.constructTemporalDate, b.constructTemporalDateTime, b.constructTemporalZonedDateTime,
439444
])()
440445
},
441-
CodeGenerator("TypedArrayGenerator", produces: [.object()], useInPrefix: true) { b in
446+
CodeGenerator("TypedArrayGenerator", produces: [.object()]) { b in
442447
let size = b.loadInt(b.randomSize(upTo: 0x1000))
443448
let constructor = b.createNamedVariable(
444449
forBuiltin: chooseUniform(
@@ -614,7 +619,7 @@ public let CodeGenerators: [CodeGenerator] = [
614619
)
615620
},
616621

617-
CodeGenerator("RegExpGenerator", produces: [.jsRegExp], useInPrefix: true) { b in
622+
CodeGenerator("RegExpGenerator", produces: [.jsRegExp]) { b in
618623
let (regexpPattern, flags) = b.randomRegExpPatternAndFlags()
619624
b.loadRegExp(regexpPattern, flags)
620625
},
@@ -658,7 +663,7 @@ public let CodeGenerators: [CodeGenerator] = [
658663
}
659664
},
660665

661-
CodeGenerator("ObjectConstructorGenerator", produces: [.constructor()], useInPrefix: true) {
666+
CodeGenerator("ObjectConstructorGenerator", produces: [.constructor()]) {
662667
b in
663668
let maxProperties = 3
664669
assert(b.fuzzer.environment.customProperties.count >= maxProperties)
@@ -740,7 +745,7 @@ public let CodeGenerators: [CodeGenerator] = [
740745
]
741746
),
742747

743-
CodeGenerator("TrivialFunctionGenerator", produces: [.function()], useInPrefix: true) { b in
748+
CodeGenerator("TrivialFunctionGenerator", produces: [.function()]) { b in
744749
// Generating more than one function has a fairly high probability of generating
745750
// essentially identical functions, so we just generate one.
746751
let maybeReturnValue =
@@ -1603,7 +1608,7 @@ public let CodeGenerators: [CodeGenerator] = [
16031608

16041609
CodeGenerator(
16051610
"StringNormalizeGenerator",
1606-
produces: [.jsString], useInPrefix: true
1611+
produces: [.jsString]
16071612
) { b in
16081613
let form = b.loadString(
16091614
chooseUniform(
@@ -3366,8 +3371,7 @@ public let CodeGenerators: [CodeGenerator] = [
33663371
// assert(b.type(of: imitation) == b.type(of: orig))
33673372
},
33683373

3369-
CodeGenerator("ResizableArrayBufferGenerator", produces: [.jsArrayBuffer], useInPrefix: true) {
3370-
b in
3374+
CodeGenerator("ResizableArrayBufferGenerator", produces: [.jsArrayBuffer]) { b in
33713375
let size = b.randomSize(upTo: 0x1000)
33723376
var maxSize = b.randomSize()
33733377
if maxSize < size {
@@ -3392,9 +3396,7 @@ public let CodeGenerators: [CodeGenerator] = [
33923396
b.construct(View, withArgs: [ab])
33933397
},
33943398

3395-
CodeGenerator(
3396-
"GrowableSharedArrayBufferGenerator", produces: [.jsSharedArrayBuffer], useInPrefix: true
3397-
) { b in
3399+
CodeGenerator("GrowableSharedArrayBufferGenerator", produces: [.jsSharedArrayBuffer]) { b in
33983400
let size = b.randomSize(upTo: 0x1000)
33993401
var maxSize = b.randomSize()
34003402
if maxSize < size {
@@ -3467,7 +3469,7 @@ public let CodeGenerators: [CodeGenerator] = [
34673469
}
34683470
},
34693471

3470-
CodeGenerator("IteratorGenerator", produces: [.iterable()], useInPrefix: true) { b in
3472+
CodeGenerator("IteratorGenerator", produces: [.iterable()]) { b in
34713473
let iteratorSymbol = b.createSymbolProperty("iterator")
34723474
b.hide(iteratorSymbol)
34733475
let iterableObject = b.buildObjectLiteral { obj in
@@ -3494,7 +3496,7 @@ public let CodeGenerators: [CodeGenerator] = [
34943496
b.setType(ofVariable: iterableObject, to: .iterable() + .object())
34953497
},
34963498

3497-
CodeGenerator("DisposableGenerator", produces: [.disposable()], useInPrefix: true) { b in
3499+
CodeGenerator("DisposableGenerator", produces: [.disposable()]) { b in
34983500
let disposeSymbol = b.createSymbolProperty("dispose")
34993501
b.hide(disposeSymbol)
35003502
b.buildObjectLiteral { obj in
@@ -3503,8 +3505,7 @@ public let CodeGenerators: [CodeGenerator] = [
35033505
}
35043506
},
35053507

3506-
CodeGenerator("AsyncDisposableGenerator", produces: [.asyncDisposable()], useInPrefix: true) {
3507-
b in
3508+
CodeGenerator("AsyncDisposableGenerator", produces: [.asyncDisposable()]) { b in
35083509
let asyncDisposeSymbol = b.createSymbolProperty("asyncDispose")
35093510
b.hide(asyncDisposeSymbol)
35103511
b.buildObjectLiteral { obj in

Sources/Fuzzilli/CodeGen/WasmCodeGenerators.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
2525
CodeGenerator(
2626
"WasmGlobalGenerator",
2727
inContext: .single(.javascript),
28-
produces: [.object(ofGroup: "WasmGlobal")], useInPrefix: true
28+
produces: [.object(ofGroup: "WasmGlobal")]
2929
) { b in
3030
b.createWasmGlobal(
3131
value: b.randomWasmGlobal(forContext: .javascript), isMutable: probability(0.5))
@@ -34,7 +34,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
3434
CodeGenerator(
3535
"WasmMemoryGenerator",
3636
inContext: .single(.javascript),
37-
produces: [.object(ofGroup: "WasmMemory")], useInPrefix: true
37+
produces: [.object(ofGroup: "WasmMemory")]
3838
) { b in
3939
let minPages = Int.random(in: 0..<10)
4040
let isShared = probability(0.5)
@@ -48,7 +48,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
4848
CodeGenerator(
4949
"WasmTableGenerator",
5050
inContext: .single(.javascript),
51-
produces: [.object(ofGroup: "WasmTable")], useInPrefix: true
51+
produces: [.object(ofGroup: "WasmTable")]
5252
) { b in
5353
let elementType: ILType = chooseUniform(from: [
5454
.wasmFuncRef(), .wasmExternRef(), .wasmI31Ref(), .wasmAnyRef(), .wasmEqRef(),
@@ -67,7 +67,7 @@ public let WasmCodeGenerators: [CodeGenerator] = [
6767
CodeGenerator(
6868
"WasmTagGenerator",
6969
inContext: .single(.javascript),
70-
produces: [.object(ofGroup: "WasmTag")], useInPrefix: true
70+
produces: [.object(ofGroup: "WasmTag")]
7171
) { b in
7272
if probability(0.5) {
7373
b.createWasmJSTag()

Sources/Fuzzilli/Fuzzer.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,8 @@ public class Fuzzer {
263263

264264
/// Set the CodeGenerators (and their respecitve weight) to use when generating new code.
265265
public func setCodeGenerators(_ generators: WeightedList<CodeGenerator>) {
266-
guard generators.contains(where: { $0.useInPrefix }) else {
267-
fatalError(
268-
"Code generators must contain at least one generator to be used in the prefix")
266+
guard generators.contains(where: { $0.isValueGenerator }) else {
267+
fatalError("Code generators must contain at least one value generator")
269268
}
270269
// This builds a graph that we need later for scheduling generators.
271270
self.contextGraph = ContextGraph(

0 commit comments

Comments
 (0)