Skip to content

Commit 780a0ea

Browse files
Fuzzing: Set up a resource limiter for FuzzTranslator
And validate memory type when creating a new memory instance outside of the instantiation process.
1 parent 0ef2c5b commit 780a0ea

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

FuzzTesting/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let package = Package(
3535
.product(name: "WasmKit", package: "WasmKit")
3636
]),
3737
.target(name: "FuzzExecute", dependencies: [
38+
"WasmKitFuzzing",
3839
.product(name: "WasmKit", package: "WasmKit"),
3940
]),
4041
.executableTarget(name: "FuzzDifferential", dependencies: [

FuzzTesting/Sources/FuzzExecute/FuzzExecute.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
@_spi(Fuzzing) import WasmKit
2-
3-
struct FuzzerResourceLimiter: ResourceLimiter {
4-
func limitMemoryGrowth(to desired: Int) throws -> Bool {
5-
return desired < 1024 * 1024 * 1024
6-
}
7-
func limitTableGrowth(to desired: Int) throws -> Bool {
8-
return desired < 1024 * 1024
9-
}
10-
}
2+
import WasmKitFuzzing
113

124
@_cdecl("LLVMFuzzerTestOneInput")
135
public func FuzzCheck(_ start: UnsafePointer<UInt8>, _ count: Int) -> CInt {

FuzzTesting/Sources/WasmKitFuzzing/WasmKitFuzzing.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
// This module defines utilities for fuzzing WasmKit.
22

3-
import WasmKit
3+
@_spi(Fuzzing) import WasmKit
4+
5+
/// A resource limiter that restricts allocations to fuzzer limits.
6+
public struct FuzzerResourceLimiter: ResourceLimiter {
7+
public init() {}
8+
9+
public func limitMemoryGrowth(to desired: Int) throws -> Bool {
10+
return desired < 1024 * 1024 * 1024
11+
}
12+
public func limitTableGrowth(to desired: Int) throws -> Bool {
13+
return desired < 1024 * 1024
14+
}
15+
}
416

517
/// Check if a Wasm module can be instantiated without crashing.
618
///
@@ -9,6 +21,7 @@ public func fuzzInstantiation(bytes: [UInt8]) throws {
921
let module = try WasmKit.parseWasm(bytes: bytes)
1022
let engine = Engine(configuration: EngineConfiguration(compilationMode: .eager))
1123
let store = Store(engine: engine)
24+
store.resourceLimiter = FuzzerResourceLimiter()
1225

1326
// Prepare dummy imports
1427
var imports = Imports()

Sources/WasmKit/Engine.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import _CWasmKit.Platform
22

3+
import struct WasmParser.WasmFeatureSet
4+
35
/// A WebAssembly execution engine.
46
///
57
/// An engine is responsible storing the configuration for the execution of
@@ -85,14 +87,30 @@ public struct EngineConfiguration {
8587
/// "call stack exhausted" ``Trap`` errors thrown by the interpreter.
8688
public var stackSize: Int
8789

90+
/// The WebAssembly features that can be used by Wasm modules running on this engine.
91+
public var features: WasmFeatureSet
92+
8893
/// Initializes a new instance of `EngineConfiguration`.
94+
///
8995
/// - Parameter threadingModel: The threading model to use for the virtual
9096
/// machine interpreter. If `nil`, the default threading model for the
9197
/// current platform will be used.
92-
public init(threadingModel: ThreadingModel? = nil, compilationMode: CompilationMode? = nil, stackSize: Int? = nil) {
98+
/// - Parameter compilationMode: The compilation mode to use for WebAssembly
99+
/// modules. If `nil`, the default compilation mode (lazy) will be used.
100+
/// - Parameter stackSize: The stack size in bytes for the virtual machine
101+
/// interpreter. If `nil`, the default stack size (512KB) will be used.
102+
/// - Parameter features: The WebAssembly features that can be used by Wasm
103+
/// modules running on this engine.
104+
public init(
105+
threadingModel: ThreadingModel? = nil,
106+
compilationMode: CompilationMode? = nil,
107+
stackSize: Int? = nil,
108+
features: WasmFeatureSet = .default
109+
) {
93110
self.threadingModel = threadingModel ?? .defaultForCurrentPlatform
94111
self.compilationMode = compilationMode ?? .lazy
95112
self.stackSize = stackSize ?? (1 << 19)
113+
self.features = features
96114
}
97115
}
98116

Sources/WasmKit/Execution/Instances.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ public struct Memory: Equatable {
580580
/// let instance = try module.instantiate(store: store, imports: imports)
581581
/// ```
582582
public init(store: Store, type: MemoryType) throws {
583+
// Validate the memory type because the type is not validated at instantiation time.
584+
try ModuleValidator.checkMemoryType(type, features: store.engine.configuration.features)
585+
583586
self.init(
584587
handle: try store.allocator.allocate(memoryType: type, resourceLimiter: store.resourceLimiter),
585588
allocator: store.allocator

0 commit comments

Comments
 (0)