Skip to content

Commit 67aa2c8

Browse files
LiedtkeV8-internal LUCI CQ
authored andcommitted
[wasm] Fix crash in generateAlignedMemoryIndexes
We need to prevent that the randomNonNegativeIndex() gets called with a negative index as that will lead to crashes. Bug: 427134598 Change-Id: I28bd483054790a1a299ddfb4af0c5e8abe81e7f2 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8460619 Commit-Queue: Carl Smith <[email protected]> Reviewed-by: Carl Smith <[email protected]> Auto-Submit: Matthias Liedtke <[email protected]>
1 parent 38b76d7 commit 67aa2c8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3905,11 +3905,14 @@ public class ProgramBuilder {
39053905
//
39063906
// Note: In rare cases, the returned values may lead to an out-of-bounds memory access.
39073907
func generateAlignedMemoryIndexes(forMemory memory: Variable, alignment: Int64) -> (address: Variable, offset: Int64) {
3908+
assert(alignment > 0, "Alignment must be positive")
39083909
let memoryTypeInfo = self.type(of: memory).wasmMemoryType!
39093910
let memSize = Int64(memoryTypeInfo.limits.min * WasmConstants.specWasmMemPageSize)
39103911
let function = self.currentWasmModule.currentWasmFunction
3911-
assert(memSize >= alignment, "Memory size must be large enough to satisfy alignment")
3912-
assert(alignment > 0, "Alignment must be positive")
3912+
if memSize < alignment {
3913+
// We can't generate in-bounds accesses here, so simply return address 0.
3914+
return (function.memoryArgument(0, memoryTypeInfo), 0)
3915+
}
39133916

39143917
// Generate an in-bounds offset (dynamicOffset + alignedStaticOffset) into the memory.
39153918
// The '+1' allows out-of-bounds access (dynamicOffset + alignedStaticOffset == memSize)

Tests/FuzzilliTests/ProgramBuilderTest.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,4 +2844,35 @@ class ProgramBuilderTests: XCTestCase {
28442844
}
28452845
}
28462846
}
2847+
2848+
func testEmptyMemoryGenerateMemoryIndices() {
2849+
let env = JavaScriptEnvironment()
2850+
let config = Configuration(logLevel: .error)
2851+
let fuzzer = makeMockFuzzer(config: config, environment: env)
2852+
let b = fuzzer.makeBuilder()
2853+
do {
2854+
let emptyMemory = b.createWasmMemory(minPages: 0)
2855+
b.buildWasmModule { wasmModule in
2856+
wasmModule.addWasmFunction(with: [] => [.wasmi32]) { function, label, args in
2857+
let (dynamicOffset, staticOffset) = b.generateAlignedMemoryIndexes(forMemory: emptyMemory, alignment: 1)
2858+
return [function.wasmMemoryLoad(memory: emptyMemory,
2859+
dynamicOffset: dynamicOffset, loadType: .I32LoadMem,
2860+
staticOffset: staticOffset)]
2861+
}
2862+
}
2863+
}
2864+
let actual = b.finalize()
2865+
do {
2866+
let emptyMemory = b.createWasmMemory(minPages: 0)
2867+
b.buildWasmModule { wasmModule in
2868+
wasmModule.addWasmFunction(with: [] => [.wasmi32]) { function, label, args in
2869+
return [function.wasmMemoryLoad(memory: emptyMemory,
2870+
dynamicOffset: function.consti32(0), loadType: .I32LoadMem,
2871+
staticOffset: 0)]
2872+
}
2873+
}
2874+
}
2875+
let expected = b.finalize()
2876+
XCTAssertEqual(actual, expected)
2877+
}
28472878
}

0 commit comments

Comments
 (0)