Skip to content

Commit 1bf8017

Browse files
Validation: Ban internal global usage in constant expressions
1 parent 652fe23 commit 1bf8017

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

Sources/WasmKit/Execution/ConstEvaluation.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ protocol ConstEvaluationContextProtocol {
55
func globalValue(_ index: GlobalIndex) throws -> Value
66
}
77

8-
extension InternalInstance: ConstEvaluationContextProtocol {
9-
func functionRef(_ index: FunctionIndex) throws -> Reference {
10-
return try .function(from: self.functions[validating: Int(index)])
11-
}
12-
func globalValue(_ index: GlobalIndex) throws -> Value {
13-
return try self.globals[validating: Int(index)].value
14-
}
15-
}
16-
178
struct ConstEvaluationContext: ConstEvaluationContextProtocol {
189
let functions: ImmutableArray<InternalFunction>
1910
var globals: [Value]
11+
12+
init(functions: ImmutableArray<InternalFunction>, globals: [Value]) {
13+
self.functions = functions
14+
self.globals = globals
15+
}
16+
17+
init(instance: InternalInstance, moduleImports: ModuleImports) {
18+
// Constant expressions can only reference imported globals
19+
let externalGlobals = instance.globals
20+
.prefix(moduleImports.numberOfGlobals)
21+
.map { $0.value }
22+
self.init(functions: instance.functions, globals: Array(externalGlobals))
23+
}
24+
2025
func functionRef(_ index: FunctionIndex) throws -> Reference {
2126
return try .function(from: self.functions[validating: Int(index)])
2227
}

Sources/WasmKit/Module.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,22 @@ public struct Module {
147147
try? store.nameRegistry.register(instance: instance, nameSection: nameSection)
148148
}
149149

150+
let constEvalContext = ConstEvaluationContext(instance: instance, moduleImports: moduleImports)
150151
// Step 12-13.
151152

152153
// Steps 14-15.
153154
do {
154155
for element in elements {
155156
guard case let .active(tableIndex, offset) = element.mode else { continue }
156-
let offsetValue = try offset.evaluate(context: instance)
157+
let offsetValue = try offset.evaluate(context: constEvalContext)
157158
let table = try instance.tables[validating: Int(tableIndex)]
158159
try table.withValue { table in
159160
guard let offset = offsetValue.maybeAddressOffset(table.limits.isMemory64) else {
160161
throw InstantiationError.unsupported(
161162
"Expect \(ValueType.addressType(isMemory64: table.limits.isMemory64)) offset of active element segment but got \(offsetValue)"
162163
)
163164
}
164-
let references = try element.evaluateInits(context: instance)
165+
let references = try element.evaluateInits(context: constEvalContext)
165166
try table.initialize(
166167
references, from: 0, to: Int(offset), count: references.count
167168
)
@@ -176,7 +177,7 @@ public struct Module {
176177
// Step 16.
177178
do {
178179
for case let .active(data) in data {
179-
let offsetValue = try data.offset.evaluate(context: instance)
180+
let offsetValue = try data.offset.evaluate(context: constEvalContext)
180181
let memory = try instance.memories[validating: Int(data.index)]
181182
try memory.withValue { memory in
182183
guard let offset = offsetValue.maybeAddressOffset(memory.limit.isMemory64) else {

Tests/WasmKitTests/SpectestTests.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ final class SpectestTests: XCTestCase {
2222
path: Self.testPaths,
2323
include: [],
2424
exclude: [
25-
"br_if.wast",
26-
"call.wast",
27-
"call_indirect.wast",
28-
"data.wast",
2925
"elem.wast",
3026
"exports.wast",
3127
"func.wast",

0 commit comments

Comments
 (0)