Skip to content

Commit 0025b94

Browse files
Engine API: Migrate fuzzing targets
1 parent 72dd2ac commit 0025b94

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

FuzzTesting/Sources/FuzzDifferential/FuzzDifferential.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ extension wasm_name_t {
9292
struct WasmKitEngine: Engine {
9393
func run(moduleBytes: [UInt8]) throws -> ExecResult {
9494
let module = try WasmKit.parseWasm(bytes: moduleBytes)
95-
let runtime = Runtime()
96-
let instance = try runtime.instantiate(module: module)
97-
let exports = instance.exports.sorted(by: { $0.key < $1.key })
95+
let engine = WasmKit.Engine()
96+
let store = WasmKit.Store(engine: engine)
97+
let instance = try module.instantiate(store: store)
98+
let exports = instance.exports.sorted(by: { $0.name < $1.name })
9899
let memories: [Memory] = exports.compactMap {
99100
guard case let .memory(memory) = $0.value else {
100101
return nil
@@ -117,7 +118,7 @@ struct WasmKitEngine: Engine {
117118
let type = fn.type
118119
let arguments = type.parameters.map { $0.defaultValue }
119120
do {
120-
let results = try fn.invoke(arguments, runtime: runtime)
121+
let results = try fn(arguments)
121122
return ExecResult(values: results, trap: nil, memory: memory?.data)
122123
} catch {
123124
return ExecResult(values: nil, trap: String(describing: error), memory: memory?.data)

FuzzTesting/Sources/FuzzExecute/FuzzExecute.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ public func FuzzCheck(_ start: UnsafePointer<UInt8>, _ count: Int) -> CInt {
1414
let bytes = Array(UnsafeBufferPointer(start: start, count: count))
1515
do {
1616
let module = try WasmKit.parseWasm(bytes: bytes)
17-
let runtime = WasmKit.Runtime()
18-
runtime.store.resourceLimiter = FuzzerResourceLimiter()
19-
let instance = try runtime.instantiate(module: module)
17+
let engine = WasmKit.Engine()
18+
let store = WasmKit.Store(engine: engine)
19+
store.resourceLimiter = FuzzerResourceLimiter()
20+
let instance = try module.instantiate(store: store)
2021
for export in instance.exports.values {
2122
guard case let .function(fn) = export else {
2223
continue
2324
}
2425
let type = fn.type
2526
let arguments = type.parameters.map { $0.defaultValue }
26-
_ = try fn.invoke(arguments, runtime: runtime)
27+
_ = try fn(arguments)
2728
}
2829
} catch {
2930
// Ignore errors

Sources/WasmKit/Execution/Instances.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,16 @@ typealias InternalInstance = EntityHandle<InstanceEntity>
8585
/// A map of exported entities by name.
8686
public struct Exports: Sequence {
8787
let store: Store
88-
let values: [String: InternalExternalValue]
88+
let items: [String: InternalExternalValue]
89+
90+
/// A collection of exported entities without their names.
91+
public var values: [ExternalValue] {
92+
self.map { $0.value }
93+
}
8994

9095
/// Returns the exported entity with the given name.
9196
public subscript(_ name: String) -> ExternalValue? {
92-
guard let entity = values[name] else { return nil }
97+
guard let entity = items[name] else { return nil }
9398
return ExternalValue(handle: entity, store: store)
9499
}
95100

@@ -123,10 +128,10 @@ public struct Exports: Sequence {
123128

124129
init(parent: Exports) {
125130
self.store = parent.store
126-
self.iterator = parent.values.makeIterator()
131+
self.iterator = parent.items.makeIterator()
127132
}
128133

129-
public mutating func next() -> (String, ExternalValue)? {
134+
public mutating func next() -> (name: String, value: ExternalValue)? {
130135
guard let (name, entity) = iterator.next() else { return nil }
131136
return (name, ExternalValue(handle: entity, store: store))
132137
}
@@ -170,7 +175,7 @@ public struct Instance {
170175

171176
/// A dictionary of exported entities by name.
172177
public var exports: Exports {
173-
Exports(store: store, values: handle.exports)
178+
Exports(store: store, items: handle.exports)
174179
}
175180

176181
/// Dumps the textual representation of all functions in the instance.

Sources/WasmKit/Execution/Runtime.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public final class Runtime {
5858
throw ImportError.moduleInstanceAlreadyRegistered(name)
5959
}
6060

61-
availableExports[name] = Dictionary(uniqueKeysWithValues: instance.exports)
61+
availableExports[name] = Dictionary(uniqueKeysWithValues: instance.exports.map { ($0, $1) })
6262
}
6363

6464
/// Legacy compatibility method to register a host module with a name.

Sources/WasmKit/Imports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct Imports {
1818
/// - module: The module name to be used for resolving the imports.
1919
/// - values: The values to be imported keyed by their name.
2020
public mutating func define(module: String, _ values: Exports) {
21-
definitions[module, default: [:]].merge(values, uniquingKeysWith: { _, new in new })
21+
definitions[module, default: [:]].merge(values.map { ($0, $1) }, uniquingKeysWith: { _, new in new })
2222
}
2323

2424
mutating func define(_ importEntry: Import, _ value: ExternalValue) {

0 commit comments

Comments
 (0)