Skip to content

Commit 56221fc

Browse files
Validation: Check table types
1 parent 556b787 commit 56221fc

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Sources/WasmKit/Execution/Instances.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ struct TableEntity /* : ~Copyable */ {
264264
let tableType: TableType
265265
var limits: Limits { tableType.limits }
266266

267+
static func maxSize(isMemory64: Bool) -> UInt64 {
268+
return UInt64(UInt32.max)
269+
}
270+
267271
init(_ tableType: TableType, resourceLimiter: any ResourceLimiter) throws {
268272
let emptyElement: Reference
269273
switch tableType.elementType {

Sources/WasmKit/Validator.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ struct ModuleValidator {
6969
for memoryType in module.memoryTypes {
7070
try Self.checkMemoryType(memoryType, features: module.features)
7171
}
72+
for tableType in module.tableTypes {
73+
try Self.checkTableType(tableType, features: module.features)
74+
}
7275
try checkStartFunction()
7376
}
7477

@@ -107,6 +110,29 @@ struct ModuleValidator {
107110
}
108111
}
109112

113+
static func checkTableType(_ type: TableType, features: WasmFeatureSet) throws {
114+
if type.elementType != .funcRef, !features.contains(.referenceTypes) {
115+
throw ValidationError("reference-types feature is required for non-funcref tables")
116+
}
117+
try checkLimit(type.limits)
118+
119+
if type.limits.isMemory64 {
120+
guard features.contains(.memory64) else {
121+
throw ValidationError("memory64 feature is required for 64-bit tables")
122+
}
123+
}
124+
125+
let hardMax = TableEntity.maxSize(isMemory64: type.limits.isMemory64)
126+
127+
if type.limits.min > hardMax {
128+
throw ValidationError("size minimum must not be greater than \(hardMax)")
129+
}
130+
131+
if let max = type.limits.max, max > hardMax {
132+
throw ValidationError("size maximum must not be greater than \(hardMax)")
133+
}
134+
}
135+
110136
private static func checkLimit(_ limit: Limits) throws {
111137
guard let max = limit.max else { return }
112138
if limit.min > max {

Tests/WasmKitTests/SpectestTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class SpectestTests: XCTestCase {
2222
path: Self.testPaths,
2323
include: [],
2424
exclude: [
25-
"table.wast",
25+
"/br_table.wast",
2626
"table_get.wast",
2727
"table_grow.wast",
2828
"table_size.wast",

0 commit comments

Comments
 (0)