1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import Fuzzilli
fileprivate let ForceJITCompilationThroughLoopGenerator = CodeGenerator("ForceJITCompilationThroughLoopGenerator", inputs: .required(.function())) { b, f in
assert(b.type(of: f).Is(.function()))
let arguments = b.randomArguments(forCalling: f)
b.buildRepeatLoop(n: 100) { _ in
b.callFunction(f, withArgs: arguments)
}
}
fileprivate let ForceTurboFanCompilationGenerator = CodeGenerator("ForceTurboFanCompilationGenerator", inputs: .required(.function())) { b, f in
assert(b.type(of: f).Is(.function()))
let arguments = b.randomArguments(forCalling: f)
b.callFunction(f, withArgs: arguments)
b.eval("%PrepareFunctionForOptimization(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
b.callFunction(f, withArgs: arguments)
b.eval("%OptimizeFunctionOnNextCall(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
}
fileprivate let ForceMaglevCompilationGenerator = CodeGenerator("ForceMaglevCompilationGenerator", inputs: .required(.function())) { b, f in
assert(b.type(of: f).Is(.function()))
let arguments = b.randomArguments(forCalling: f)
b.callFunction(f, withArgs: arguments)
b.eval("%PrepareFunctionForOptimization(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
b.callFunction(f, withArgs: arguments)
b.eval("%OptimizeMaglevOnNextCall(%@)", with: [f]);
b.callFunction(f, withArgs: arguments)
}
fileprivate let TurbofanVerifyTypeGenerator = CodeGenerator("TurbofanVerifyTypeGenerator", inputs: .one) { b, v in
b.eval("%VerifyType(%@)", with: [v])
}
// Insert random GC calls throughout our code.
fileprivate let GcGenerator = CodeGenerator("GcGenerator") { b in
let gc = b.createNamedVariable(forBuiltin: "gc")
// Do minor GCs more frequently.
let type = b.loadString(probability(0.25) ? "major" : "minor")
// If the execution type is 'async', gc() returns a Promise, we currently
// do not really handle other than typing the return of gc to .undefined |
// .jsPromise. One could either chain a .then or create two wrapper
// functions that are differently typed such that fuzzilli always knows
// what the type of the return value is.
let execution = b.loadString(probability(0.5) ? "sync" : "async")
b.callFunction(gc, withArgs: [b.createObject(with: ["type": type, "execution": execution])])
}
public extension ILType {
{% for enum in database.enumerations %}
static let js{{enum.identifier}} = ILType.enumeration(ofName: "{{enum.identifier}}", withValues: [ {{enum.values|map("tojson")|join(", ")}} ])
{% endfor %}
{% for interface in database.interfaces|sort_object_groups %}
{% set inter = interface|parse_interface -%}
static let js{{interface.identifier}} = {{inter[0].fuzzilli_repr()}}
{% endfor %}
{% for d in database.dictionaries|sort_object_groups %}
{% set dictionary = d|parse_dictionary -%}
static let js{{d.identifier}} = {{dictionary[0].fuzzilli_repr()}}
{% endfor %}
{% for interface in database.interfaces|sort_object_groups -%}
{% set constructor = interface|parse_constructors -%}
{% if constructor[0] %}
static let js{{interface.identifier}}Constructor = {{constructor[0].fuzzilli_repr()}}
{% endif %}
{%- endfor %}
{% for t in database.typedefs %}
{% set il_type = t.idl_type|idl_type_to_iltype -%}
static let js{{t.identifier}} = {{il_type.fuzzilli_repr()}}
{% endfor %}
{% for interface in database.callback_interfaces %}
{% set inter = interface|parse_interface -%}
static let js{{interface.identifier}} = {{inter[0].fuzzilli_repr()}}
{% endfor %}
{% for c in database.callback_functions %}
{% set op = c|parse_operation -%}
static let js{{c.identifier}} = ILType.function({{op.fuzzilli_repr()}})
{% endfor %}
}
{%- macro define_group(identifier, group) %}
let js{{identifier}} = ObjectGroup(
name: "{{group.name}}",
instanceType: {{group.instanceType.fuzzilli_repr()}},
properties: [
{%- for k, v in group.properties.items() %}
"{{k}}" : {{v.fuzzilli_repr()}},
{% else %}
:
{% endfor -%}
],
methods: [
{%- for k, v in group.methods.items() %}
"{{k}}" : {{v.fuzzilli_repr()}},
{% else %}
:
{% endfor -%}
]{%- if group.parent %},
parent: "{{group.parent}}"
{%- endif %}
)
{%- endmacro -%}
{% for interface in database.interfaces|sort_object_groups %}
{%- set inter = interface|parse_interface -%}
{{define_group(interface.identifier, inter[1])}}
{% endfor %}
{% for interface in database.interfaces|sort_object_groups %}
{%- set constructor = interface|parse_constructors -%}
{% if constructor[1] %}
{{define_group(interface.identifier+"Constructor", constructor[1])}}
{% endif %}
{%- endfor %}
{% for d in database.dictionaries|sort_object_groups %}
{%- set dictionary = d|parse_dictionary -%}
{{define_group(d.identifier, dictionary[1])}}
{% endfor -%}
{% for interface in database.callback_interfaces %}
{%- set inter = interface|parse_interface -%}
{{define_group(interface.identifier, inter[1])}}
{% endfor -%}
let fastCallables : [(group: ILType, method: String)] = [
{%- for interface in database.interfaces -%}
{%- for operation in interface.operations -%}
{%- if operation.is_static == False -%}
{%- if 'NoAllocDirectCall' in operation.extended_attributes %}
(group: js{{interface.identifier}}, method: "{{operation.identifier}}"),
{%- endif %}
{%- endif -%}
{%- endfor -%}
{%- endfor %}
]
fileprivate let FastApiCallFuzzer = ProgramTemplate("FastApiCallFuzzer") { b in
b.buildPrefix()
b.build(n: 20)
let parameterCount = probability(0.5) ? 0 : Int.random(in: 1...4)
let f = b.buildPlainFunction(with: .parameters(n: parameterCount)) { args in
b.build(n: 10)
let target = fastCallables.randomElement()!
let apiObj = b.findOrGenerateType(target.group)
let functionSig = chooseUniform(from: b.methodSignatures(of: target.method, on: target.group))
let apiCall = b.callMethod(target.method, on: apiObj, withArgs: b.findOrGenerateArguments(forSignature: functionSig), guard: true)
b.doReturn(apiCall)
}
let args = b.randomVariables(n: Int.random(in: 0...5))
b.callFunction(f, withArgs: args)
b.eval("%PrepareFunctionForOptimization(%@)", with: [f]);
b.callFunction(f, withArgs: args)
b.callFunction(f, withArgs: args)
b.eval("%OptimizeFunctionOnNextCall(%@)", with: [f]);
b.callFunction(f, withArgs: args)
b.build(n: 10)
}
let WasmFastCallFuzzer = WasmProgramTemplate("WasmFastCallFuzzer") { b in
b.buildPrefix()
b.build(n: 10)
let target = fastCallables.randomElement()!
let apiObj = b.findOrGenerateType(target.group)
// Bind the API function so that it can be called from WebAssembly.
let wrapped = b.bindMethod(target.method, on: apiObj)
let functionSig = chooseUniform(from: b.methodSignatures(of: target.method, on: target.group))
let wrappedSig = Signature(expects: [.plain(b.type(of: apiObj))] + functionSig.parameters, returns: functionSig.outputType)
let m = b.buildWasmModule { m in
let allWasmTypes: WeightedList<ILType> = WeightedList([(.wasmi32, 1), (.wasmi64, 1), (.wasmf32, 1), (.wasmf64, 1), (.wasmExternRef, 1), (.wasmFuncRef, 1)])
let wasmSignature = ProgramBuilder.convertJsSignatureToWasmSignature(wrappedSig, availableTypes: allWasmTypes)
m.addWasmFunction(with: wasmSignature) {fbuilder, _ in
let args = b.randomWasmArguments(forWasmSignature: wasmSignature)
if let args {
let maybeRet = fbuilder.wasmJsCall(function: wrapped, withArgs: args, withWasmSignature: wasmSignature)
if let ret = maybeRet {
fbuilder.wasmReturn(ret)
}
} else {
logger.error("Arguments should have been generated")
}
}
}
let exports = m.loadExports()
for (methodName, _) in m.getExportedMethods() {
let exportedMethod = b.getProperty(methodName, of: exports)
b.eval("%WasmTierUpFunction(%@)", with: [exportedMethod])
let args = b.findOrGenerateArguments(forSignature: wrappedSig)
b.callMethod(methodName, on: exports, withArgs: args)
}
}
let chromiumProfile = Profile(
processArgs: { _ in
var args: [String] = []
return args
},
processEnv: ["ASAN_OPTIONS":"detect_odr_violation=0", "DISPLAY":":20"],
maxExecsBeforeRespawn: 100,
timeout: 8000,
codePrefix: """
""",
codeSuffix: """
""",
ecmaVersion: ECMAScriptVersion.es6,
startupTests: [
("fuzzilli('FUZZILLI_CRASH', 0)", .shouldCrash),
("fuzzilli('FUZZILLI_CRASH', 1)", .shouldCrash),
("fuzzilli('FUZZILLI_CRASH', 2)", .shouldCrash),
("fuzzilli('FUZZILLI_CRASH', 3)", .shouldCrash),
],
additionalCodeGenerators: [
(ForceJITCompilationThroughLoopGenerator, 5),
(ForceTurboFanCompilationGenerator, 5),
(ForceMaglevCompilationGenerator, 5),
(TurbofanVerifyTypeGenerator, 10),
(GcGenerator, 10),
],
additionalProgramTemplates: WeightedList<ProgramTemplate>([
(FastApiCallFuzzer, 5),
(WasmFastCallFuzzer, 5),
]),
disabledCodeGenerators: [],
disabledMutators: [],
additionalBuiltins: [
{%- for interface in database.interfaces|sort_object_groups -%}
{% set constructor = interface|parse_constructors -%}
{% if constructor[0] %}
"{{interface.identifier}}": ILType.js{{interface.identifier}}Constructor,
{% endif %}
{%- endfor %}
"window": .jsWindow,
"document": .jsDocument,
"gc" : .function([] => (.undefined | .jsPromise)),
"d8": .object(),
],
additionalObjectGroups: [
{%- for interface in database.interfaces|sort_object_groups %}
js{{interface.identifier}},
{% endfor -%}
{% for interface in database.interfaces|sort_object_groups -%}
{% set constructor = interface|parse_constructors -%}
{% if constructor[1] %}
js{{interface.identifier}}Constructor,
{% endif %}
{%- endfor -%}
{% for d in database.dictionaries|sort_object_groups %}
js{{d.identifier}},
{% endfor -%}
{% for interface in database.callback_interfaces %}
js{{interface.identifier}},
{% endfor -%}
],
optionalPostProcessor: nil
)
|