Skip to content

Commit 25f599e

Browse files
Validate overflow in stack layout calculation
1 parent 2063f28 commit 25f599e

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed
Binary file not shown.

FuzzTesting/differential.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def run(args):
5151
crash_file = os.path.join(fail_dir, f"diff-{i}.wasm")
5252
shutil.copy(wasm_file, crash_file)
5353
print(f"Found crash in iteration {i};"
54-
" reproduce with {args.program} {crash_file}")
54+
f" reproduce with {args.program} {crash_file}")
5555
except subprocess.TimeoutExpired:
5656
timeout_file = os.path.join(fail_dir, f"timeout-{i}.wasm")
5757
shutil.copy(wasm_file, timeout_file)
5858
print(f"Timeout in iteration {i};"
59-
" reproduce with {args.program} {timeout_file})")
59+
f" reproduce with {args.program} {timeout_file})")
6060
except KeyboardInterrupt:
6161
print("Interrupted by user")
6262
break

Sources/WasmKit/Execution/Function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ struct WasmFunctionEntity {
181181
@inline(never)
182182
mutating func compile(runtime: RuntimeRef, code: InternalUncompiledCode) throws -> InstructionSequence {
183183
let type = self.type
184-
var translator = InstructionTranslator(
184+
var translator = try InstructionTranslator(
185185
allocator: runtime.value.store.allocator.iseqAllocator,
186186
runtimeConfiguration: runtime.value.configuration,
187187
funcTypeInterner: runtime.value.funcTypeInterner,

Sources/WasmKit/Execution/Instances.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public struct Instance {
140140
let (iseq, locals, _) = function.assumeCompiled()
141141

142142
// Print slot space information
143-
let stackLayout = StackLayout(
143+
let stackLayout = try StackLayout(
144144
type: runtime.funcTypeInterner.resolve(function.type),
145145
numberOfLocals: locals,
146146
codeSize: code.expression.count

Sources/WasmKit/Translator.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ struct StackLayout {
221221
return VReg(numberOfLocals + constantSlotSize)
222222
}
223223

224-
init(type: FunctionType, numberOfLocals: Int, codeSize: Int) {
224+
init(type: FunctionType, numberOfLocals: Int, codeSize: Int) throws {
225225
self.frameHeader = FrameHeaderLayout(type: type)
226226
self.numberOfLocals = numberOfLocals
227227
// The number of constant slots is determined by the code size
228228
self.constantSlotSize = max(codeSize / 20, 4)
229+
let (maxSlots, overflow) = self.constantSlotSize.addingReportingOverflow(numberOfLocals)
230+
guard !overflow, maxSlots < VReg.max else {
231+
throw TranslationError("The number of constant slots overflows")
232+
}
229233
}
230234

231235
func localReg(_ index: LocalIndex) -> VReg {
@@ -805,14 +809,14 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
805809
functionIndex: FunctionIndex,
806810
codeSize: Int,
807811
intercepting: Bool
808-
) {
812+
) throws {
809813
self.allocator = allocator
810814
self.funcTypeInterner = funcTypeInterner
811815
self.type = type
812816
self.module = module
813817
self.iseqBuilder = ISeqBuilder(runtimeConfiguration: runtimeConfiguration)
814818
self.controlStack = ControlStack()
815-
self.stackLayout = StackLayout(
819+
self.stackLayout = try StackLayout(
816820
type: type,
817821
numberOfLocals: locals.count,
818822
codeSize: codeSize

0 commit comments

Comments
 (0)