Skip to content

Commit 1b26057

Browse files
Fuzzing: Update FuzzTranslator to instantiate modules explicitly
1 parent 65ed01b commit 1b26057

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

FuzzTesting/Sources/FuzzTranslator/FuzzTranslator.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@ import WasmKit
44
public func FuzzCheck(_ start: UnsafePointer<UInt8>, _ count: Int) -> CInt {
55
let bytes = Array(UnsafeBufferPointer(start: start, count: count))
66
do {
7-
var module = try WasmKit.parseWasm(bytes: bytes)
8-
try module.materializeAll()
7+
let module = try WasmKit.parseWasm(bytes: bytes)
8+
let engine = Engine(configuration: EngineConfiguration(compilationMode: .eager))
9+
let store = Store(engine: engine)
10+
var imports = Imports()
11+
for importEntry in module.imports {
12+
let value: ExternalValueConvertible
13+
switch importEntry.descriptor {
14+
case .function(let typeIndex):
15+
guard typeIndex < module.types.count else { return 0 }
16+
let type = module.types[Int(typeIndex)]
17+
value = Function(store: store, type: type) { _, _ in
18+
// Provide "start function" with empty results
19+
if type.results.isEmpty { return [] }
20+
fatalError("Unexpected function call")
21+
}
22+
case .global(let globalType):
23+
value = try Global(store: store, type: globalType, value: .i32(0))
24+
case .memory(let memoryType):
25+
value = try Memory(store: store, type: memoryType)
26+
case .table(let tableType):
27+
value = try Table(store: store, type: tableType)
28+
}
29+
imports.define(module: importEntry.module, name: importEntry.name, value.externalValue)
30+
}
31+
_ = try module.instantiate(store: store, imports: imports)
932
} catch {
1033
// Ignore errors
1134
}

Sources/WasmKit/Module.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,8 @@ public struct Module {
227227
}
228228

229229
/// Materialize lazily-computed elements in this module
230-
public mutating func materializeAll() throws {
231-
let allocator = ISeqAllocator()
232-
let funcTypeInterner = Interner<FunctionType>()
233-
for function in functions {
234-
_ = try function.compile(module: self, funcTypeInterner: funcTypeInterner, allocator: allocator)
235-
}
236-
}
230+
@available(*, deprecated, message: "Module materialization is no longer supported. Instantiate the module explicitly instead.")
231+
public mutating func materializeAll() throws {}
237232
}
238233

239234
extension Module {
@@ -275,8 +270,4 @@ typealias LabelIndex = UInt32
275270
struct GuestFunction {
276271
let type: FunctionType
277272
let code: Code
278-
279-
func compile(module: Module, funcTypeInterner: Interner<FunctionType>, allocator: ISeqAllocator) throws -> InstructionSequence {
280-
throw TranslationError("Compile without instantiation is no longer supported")
281-
}
282273
}

Tests/WasmKitTests/FuzzTranslatorRegressionTests.swift

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import WasmKit
22
import XCTest
33

44
final class FuzzTranslatorRegressionTests: XCTestCase {
5-
func testRunAll() async throws {
5+
func testRunAll() throws {
66
let sourceRoot = URL(fileURLWithPath: #filePath)
77
.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
88
let failCasesDir =
@@ -12,34 +12,40 @@ final class FuzzTranslatorRegressionTests: XCTestCase {
1212
for file in try FileManager.default.contentsOfDirectory(atPath: failCasesDir.path) {
1313
let path = failCasesDir.appendingPathComponent(file).path
1414
print("Fuzz regression test: \(path.dropFirst(sourceRoot.path.count + 1))")
15-
16-
let data = try Data(contentsOf: URL(fileURLWithPath: path))
17-
do {
18-
let module = try WasmKit.parseWasm(bytes: Array(data))
19-
let engine = Engine(configuration: EngineConfiguration(compilationMode: .eager))
20-
let store = Store(engine: engine)
21-
var imports = Imports()
22-
for importEntry in module.imports {
23-
let value: ExternalValueConvertible
24-
switch importEntry.descriptor {
25-
case .function(let typeIndex):
26-
let type = module.types[Int(typeIndex)]
27-
value = Function(store: store, type: type) { _, _ in
28-
fatalError("unreachable")
29-
}
30-
case .global(let globalType):
31-
value = try Global(store: store, type: globalType, value: .i32(0))
32-
case .memory(let memoryType):
33-
value = try Memory(store: store, type: memoryType)
34-
case .table(let tableType):
35-
value = try Table(store: store, type: tableType)
36-
}
37-
imports.define(module: importEntry.module, name: importEntry.name, value.externalValue)
38-
}
39-
_ = try module.instantiate(store: store, imports: imports)
40-
} catch {
41-
// Explicit errors are ok
42-
}
15+
try checkFuzzCase(path: path)
4316
}
4417
}
18+
19+
func checkFuzzCase(path: String) throws {
20+
let data = try Data(contentsOf: URL(fileURLWithPath: path))
21+
do {
22+
let module = try WasmKit.parseWasm(bytes: Array(data))
23+
let engine = Engine(configuration: EngineConfiguration(compilationMode: .eager))
24+
let store = Store(engine: engine)
25+
var imports = Imports()
26+
for importEntry in module.imports {
27+
let value: ExternalValueConvertible
28+
switch importEntry.descriptor {
29+
case .function(let typeIndex):
30+
guard typeIndex < module.types.count else { return }
31+
let type = module.types[Int(typeIndex)]
32+
value = Function(store: store, type: type) { _, _ in
33+
// Provide "start function" with empty results
34+
if type.results.isEmpty { return [] }
35+
fatalError("Unexpected function call")
36+
}
37+
case .global(let globalType):
38+
value = try Global(store: store, type: globalType, value: .i32(0))
39+
case .memory(let memoryType):
40+
value = try Memory(store: store, type: memoryType)
41+
case .table(let tableType):
42+
value = try Table(store: store, type: tableType)
43+
}
44+
imports.define(module: importEntry.module, name: importEntry.name, value.externalValue)
45+
}
46+
_ = try module.instantiate(store: store, imports: imports)
47+
} catch {
48+
// Explicit errors are ok
49+
}
50+
}
4551
}

0 commit comments

Comments
 (0)