Skip to content

Commit a9855f2

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

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

FuzzTesting/Sources/FuzzTranslator/FuzzTranslator.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,28 @@ public func FuzzCheck(_ start: UnsafePointer<UInt8>, _ count: Int) -> CInt {
55
let bytes = Array(UnsafeBufferPointer(start: start, count: count))
66
do {
77
var module = try WasmKit.parseWasm(bytes: bytes)
8-
try module.materializeAll()
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+
fatalError("unreachable")
19+
}
20+
case .global(let globalType):
21+
value = try Global(store: store, type: globalType, value: .i32(0))
22+
case .memory(let memoryType):
23+
value = try Memory(store: store, type: memoryType)
24+
case .table(let tableType):
25+
value = try Table(store: store, type: tableType)
26+
}
27+
imports.define(module: importEntry.module, name: importEntry.name, value.externalValue)
28+
}
29+
_ = try module.instantiate(store: store, imports: imports)
930
} catch {
1031
// Ignore errors
1132
}

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final class FuzzTranslatorRegressionTests: XCTestCase {
2323
let value: ExternalValueConvertible
2424
switch importEntry.descriptor {
2525
case .function(let typeIndex):
26+
guard typeIndex < module.types.count else { return 0 }
2627
let type = module.types[Int(typeIndex)]
2728
value = Function(store: store, type: type) { _, _ in
2829
fatalError("unreachable")

0 commit comments

Comments
 (0)