Skip to content

Commit 3c9005c

Browse files
Limit the number of locals in a function
1 parent b721d43 commit 3c9005c

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// Limits for parsing WebAssembly modules.
2+
struct ParsingLimits {
3+
/// Maximum number of locals in a function.
4+
var maxFunctionLocals: UInt64
5+
6+
/// The default limits for parsing.
7+
static var `default`: ParsingLimits {
8+
return ParsingLimits(maxFunctionLocals: 50000)
9+
}
10+
}

Sources/WasmParser/WasmParser.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public struct Parser<Stream: ByteStream> {
1111
let stream: Stream
1212
public private(set) var hasDataCount: Bool = false
1313
public let features: WasmFeatureSet
14+
let limits: ParsingLimits
1415

1516
enum NextParseTarget {
1617
case header
@@ -27,6 +28,7 @@ public struct Parser<Stream: ByteStream> {
2728
self.features = features
2829
self.hasDataCount = hasDataCount
2930
self.nextParseTarget = .header
31+
self.limits = .default
3032
}
3133
}
3234

@@ -186,7 +188,7 @@ public enum WasmParserError: Swift.Error {
186188
/// The data count and data section length are inconsistent
187189
case inconsistentDataCountAndDataSectionLength(dataCount: UInt32, dataSection: Int)
188190
/// The local count is too large
189-
case tooManyLocals
191+
case tooManyLocals(UInt64, limit: UInt64)
190192
/// The type is expected to be a reference type, but it's not
191193
case expectedRefType(actual: ValueType)
192194
/// The instruction is not implemented
@@ -1014,8 +1016,8 @@ extension Parser {
10141016
return (n, t)
10151017
}
10161018
let totalLocals = localTypes.reduce(UInt64(0)) { $0 + UInt64($1.n) }
1017-
guard totalLocals < UInt32.max else {
1018-
throw WasmParserError.tooManyLocals
1019+
guard totalLocals < limits.maxFunctionLocals else {
1020+
throw WasmParserError.tooManyLocals(totalLocals, limit: limits.maxFunctionLocals)
10191021
}
10201022

10211023
let locals = localTypes.flatMap { (n: UInt32, type: ValueType) in

0 commit comments

Comments
 (0)