Skip to content

Commit f48d02b

Browse files
Validation: Check the type of the initial value of a global variable
1 parent df493cc commit f48d02b

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

Sources/WasmKit/Execution/ConstEvaluation.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,3 @@ extension WasmParser.ElementSegment {
104104
}
105105
}
106106
}
107-
108-
extension WasmTypes.Reference {
109-
fileprivate func checkType(_ type: WasmTypes.ReferenceType) throws {
110-
switch (self, type) {
111-
case (.function, .funcRef): return
112-
case (.extern, .externRef): return
113-
default:
114-
throw ValidationError("Expect \(type) but got \(self)")
115-
}
116-
}
117-
}
118-
119-
extension Value {
120-
fileprivate func checkType(_ type: WasmTypes.ValueType) throws {
121-
switch (self, type) {
122-
case (.i32, .i32): return
123-
case (.i64, .i64): return
124-
case (.f32, .f32): return
125-
case (.f64, .f64): return
126-
case (.ref(let ref), .ref(let refType)):
127-
try ref.checkType(refType)
128-
default:
129-
throw ValidationError("Expect \(type) but got \(self)")
130-
}
131-
}
132-
}

Sources/WasmKit/Execution/Instances.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ struct GlobalEntity /* : ~Copyable */ {
571571
}
572572
let globalType: GlobalType
573573

574-
init(globalType: GlobalType, initialValue: Value) {
574+
init(globalType: GlobalType, initialValue: Value) throws {
575+
try initialValue.checkType(globalType.valueType)
575576
rawValue = UntypedValue(initialValue)
576577
self.globalType = globalType
577578
}
@@ -620,14 +621,14 @@ public struct Global: Equatable {
620621
/// WebAssembly module.
621622
@available(*, deprecated, renamed: "init(store:type:value:)")
622623
public init(globalType: GlobalType, initialValue: Value, store: Store) {
623-
self.init(store: store, type: globalType, value: initialValue)
624+
try! self.init(store: store, type: globalType, value: initialValue)
624625
}
625626

626627
/// Initializes a new global instance with the given type and initial value.
627628
/// The returned global instance may be used to instantiate a new
628629
/// WebAssembly module.
629-
public init(store: Store, type: GlobalType, value: Value) {
630-
let handle = store.allocator.allocate(globalType: type, initialValue: value)
630+
public init(store: Store, type: GlobalType, value: Value) throws {
631+
let handle = try store.allocator.allocate(globalType: type, initialValue: value)
631632
self.init(handle: handle, allocator: store.allocator)
632633
}
633634
}

Sources/WasmKit/Execution/StoreAllocator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ extension StoreAllocator {
374374
let initialValue = try global.initializer.evaluate(
375375
context: constEvalContext, expectedType: global.type.valueType
376376
)
377-
return allocate(globalType: global.type, initialValue: initialValue)
377+
return try allocate(globalType: global.type, initialValue: initialValue)
378378
}
379379
)
380380

@@ -507,8 +507,8 @@ extension StoreAllocator {
507507

508508
/// > Note:
509509
/// <https://webassembly.github.io/spec/core/exec/modules.html#alloc-global>
510-
func allocate(globalType: GlobalType, initialValue: Value) -> InternalGlobal {
511-
let pointer = globals.allocate(initializing: GlobalEntity(globalType: globalType, initialValue: initialValue))
510+
func allocate(globalType: GlobalType, initialValue: Value) throws -> InternalGlobal {
511+
let pointer = try globals.allocate(initializing: GlobalEntity(globalType: globalType, initialValue: initialValue))
512512
return InternalGlobal(unsafe: pointer)
513513
}
514514

Sources/WasmKit/Validator.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,29 @@ struct ModuleValidator {
140140
}
141141
}
142142
}
143+
144+
extension WasmTypes.Reference {
145+
func checkType(_ type: WasmTypes.ReferenceType) throws {
146+
switch (self, type) {
147+
case (.function, .funcRef): return
148+
case (.extern, .externRef): return
149+
default:
150+
throw ValidationError("Expect \(type) but got \(self)")
151+
}
152+
}
153+
}
154+
155+
extension Value {
156+
func checkType(_ type: WasmTypes.ValueType) throws {
157+
switch (self, type) {
158+
case (.i32, .i32): return
159+
case (.i64, .i64): return
160+
case (.f32, .f32): return
161+
case (.f64, .f64): return
162+
case (.ref(let ref), .ref(let refType)):
163+
try ref.checkType(refType)
164+
default:
165+
throw ValidationError("Expect \(type) but got \(self)")
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)