From ac3dc27a738808d38e1148d3caefc5e89111ee32 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 9 Nov 2026 00:18:54 -0800 Subject: [PATCH] openvmm_helpers: compute memory-expansion ranges without overflow A microVM snapshot that declares a RAM capacity records, in its machine contract, the canonical memory-expansion ranges: the parts of the capacity's RAM layout beyond its base RAM, which memory.bin lacks. canonical_memory_expansion_ranges computes them when OpenVMM builds a contract and when it checks a contract's shape, as opening a snapshot does. It placed each range at `gpa_start + logical_start - file_offset`, so for the capacity's high-RAM range, at GPA 4 GiB and memory.bin offset 3 GiB, the addition overflowed whenever the base RAM was at least 2^64 - 4 GiB and below the capacity, although the result, below the range's end, fits. A debug build panicked with "attempt to add with overflow", and a release build wrapped back to the right address. No VM has such RAM, but a crafted manifest can declare it, and opening that snapshot panicked a debug build before the size check of memory.bin could refuse it (microsoft/nvx#1364). Subtract first, `gpa_start + (logical_start - file_offset)`. The subtraction cannot underflow, since logical_start is at least file_offset, and the sum is below the range's end, which canonical_microvm_memory_ranges has checked fits in a u64. Every range that the old expression computed without overflowing is unchanged. At feature/microvm/dev 154548a0e, a scratch test that opens a snapshot whose manifest declares 2^64 - 4 GiB of RAM and a 2^64 - 2 GiB capacity panicked in OpenedSnapshot::open on Windows; with the fix, the open fails with "memory.bin size (1024 bytes) doesn't match manifest (18446744069414584320 bytes)". Tests: a unit test that base RAM of 2^64 - 4 GiB with a capacity of 2^64 - 2 GiB expands by one 2 GiB range at GPA 2^64 - 3 GiB, that the shape check accepts the manifest whose contract records it, and that a restore to the capacity selects it. It panics without the fix on Linux and Windows and passes with it, as do the other openvmm_helpers tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../openvmm_helpers/src/snapshot/microvm.rs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/openvmm/openvmm_helpers/src/snapshot/microvm.rs b/openvmm/openvmm_helpers/src/snapshot/microvm.rs index 2dc6c870b0f..5dbd4d151e0 100644 --- a/openvmm/openvmm_helpers/src/snapshot/microvm.rs +++ b/openvmm/openvmm_helpers/src/snapshot/microvm.rs @@ -910,7 +910,9 @@ fn canonical_memory_expansion_ranges( continue; } ranges.push(SnapshotMemoryExpansionRange { - gpa_start: capacity_range.gpa_start + logical_start - capacity_range.file_offset, + // Offset into the range first: its end fits in a u64, but + // `gpa_start + logical_start` may not. + gpa_start: capacity_range.gpa_start + (logical_start - capacity_range.file_offset), length: logical_end - logical_start, }); } @@ -4236,6 +4238,38 @@ mod tests { assert!(canonical_microvm_memory_ranges(u64::MAX).is_err()); } + #[test] + fn memory_expansion_near_gpa_limit_does_not_overflow() { + const GB: u64 = 1024 * 1024 * 1024; + // RAM beyond 3 GiB resumes at 4 GiB, so base RAM of 2^64 - 4 GiB ends + // at 2^64 - 3 GiB, where its expansion to a capacity of 2^64 - 2 GiB + // begins. + let memory_size = u64::MAX - 4 * GB + 1; + let capacity = u64::MAX - 2 * GB + 1; + let expansion = [SnapshotMemoryExpansionRange { + gpa_start: u64::MAX - 3 * GB + 1, + length: 2 * GB, + }]; + assert_eq!( + canonical_memory_expansion_ranges(memory_size, capacity).unwrap(), + expansion + ); + + // Opening the snapshot checks the contract's shape, and a restore to + // the capacity selects the whole expansion. + let manifest = expandable_memory_manifest(memory_size, capacity); + validate_machine_contract_shape( + manifest.machine_contract.as_ref().unwrap(), + memory_size, + manifest.vp_count, + ) + .unwrap(); + assert_eq!( + validate_restore_memory_target(&manifest, capacity).unwrap(), + expansion + ); + } + fn canonical_worker_platform_command_line(processor_count: u32) -> String { worker_platform_command_line(processor_count, &[]) }