Skip to content

Commit 4768a7d

Browse files
Validation: Check start function type
1 parent d3fb8f9 commit 4768a7d

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Sources/WasmKit/Module.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public struct Module {
5858
public let types: [FunctionType]
5959

6060
let moduleImports: ModuleImports
61+
let importedFunctionTypes: [TypeIndex]
6162
let memoryTypes: [MemoryType]
6263
let tableTypes: [TableType]
6364
let allocator: ISeqAllocator
@@ -92,19 +93,20 @@ public struct Module {
9293
self.features = features
9394
self.hasDataCount = hasDataCount
9495

95-
var functionTypeIndices: [TypeIndex] = []
96+
var importedFunctionTypes: [TypeIndex] = []
9697
var globalTypes: [GlobalType] = []
9798
var memoryTypes: [MemoryType] = []
9899
var tableTypes: [TableType] = []
99100

100101
self.moduleImports = ModuleImports.build(
101102
from: imports,
102-
functionTypeIndices: &functionTypeIndices,
103+
functionTypeIndices: &importedFunctionTypes,
103104
globalTypes: &globalTypes,
104105
memoryTypes: &memoryTypes,
105106
tableTypes: &tableTypes
106107
)
107108
self.types = types
109+
self.importedFunctionTypes = importedFunctionTypes
108110
self.memoryTypes = memoryTypes + memories
109111
self.tableTypes = tableTypes + tables
110112
}
@@ -116,6 +118,19 @@ public struct Module {
116118
return typeSection[Int(index)]
117119
}
118120

121+
internal func resolveFunctionType(_ index: FunctionIndex) throws -> FunctionType {
122+
guard Int(index) < functions.count + self.moduleImports.numberOfFunctions else {
123+
throw TranslationError("Function index \(index) is out of range")
124+
}
125+
if Int(index) < self.moduleImports.numberOfFunctions {
126+
return try Self.resolveType(
127+
importedFunctionTypes[Int(index)],
128+
typeSection: types
129+
)
130+
}
131+
return functions[Int(index) - self.moduleImports.numberOfFunctions].type
132+
}
133+
119134
/// Instantiate this module in the given imports.
120135
///
121136
/// - Parameters:

Sources/WasmKit/Validator.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ struct ModuleValidator {
6161
for memoryType in module.memoryTypes {
6262
try Self.checkMemoryType(memoryType, features: module.features)
6363
}
64+
try checkStartFunction()
65+
}
66+
67+
func checkStartFunction() throws {
68+
if let startFunction = module.start {
69+
let type = try module.resolveFunctionType(startFunction)
70+
guard type.parameters.isEmpty, type.results.isEmpty else {
71+
throw ValidationError("Start function must have no parameters and no results")
72+
}
73+
}
6474
}
6575

6676
static func checkMemoryType(_ type: MemoryType, features: WasmFeatureSet) throws {

Tests/WasmKitTests/SpectestTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ final class SpectestTests: XCTestCase {
2222
path: Self.testPaths,
2323
include: [],
2424
exclude: [
25-
"start.wast",
2625
"table-sub.wast",
2726
"table.wast",
2827
"table_get.wast",

0 commit comments

Comments
 (0)