Skip to content

Commit fe61f9c

Browse files
ppennaCopilot
andcommitted
vmm_tests: check that each backend refuses unsupported restore state
The backend contract tests had no case for a restore of a saved state that holds an element that the partition lacks, which, before the previous commit, panicked in the generated restore_all on every backend (microsoft/nvx#1168). A save never holds one, since it holds only what the partition has, so no other contract reached it. A new module, unsupported_state, holds two contracts, which every native backend owes and which register with the default, unenlightened fixture. They craft the decoded snapshot from protobuf with the field numbers of VpSavedState and VmSavedState, and require that the restore fails with StateError::Unsupported naming the element: - refuses_unsupported_cpu_state restores, one at a time, each element that the fixture's partition lacks: SynIC state, which it always lacks, and, depending on the backend, the host, and the CPUID of the fixture, IA32_SPEC_CTRL, IA32_UMWAIT_CONTROL, IA32_DEBUGCTL, the feature and power MSRs, and the machine-check state, of which MSHV and WHP hold none of the last four. Each comes with registers that precede it in the restore order and differ from the VP's, and the VP's registers must stay as they were. - refuses_unsupported_vm_state restores each element of the VM state, the hypercall MSRs, the reference time, and the reference TSC page, alone. Both first check that the fixture's partition lacks Hyper-V enlightenments. The module has a file of its own, registered between time and xsave for the hosts of every native backend, since restore.rs holds the contracts that only KVM owes and builds for Linux alone. The Guide's page of the contract tests lists the cases. Results, one process per case on Windows, nextest on Linux, debug build. With restore_all as it was at 359169f, which asserts that each element is present instead of refusing it, both new cases fail on every backend with a panic in the restore, "assertion failed: <SyntheticMsrs as ...>::is_present(self.caps())" for the CPU case, at the expansion of state_trait! in vmm_core/virt/src/x86/vp.rs, and "<HypercallMsrs as ...>::is_present(self.caps())" for the VM case, in x86/vm.rs. With the previous commit, both pass, and the other cases of the binary pass as before: WHP, Windows 11 on a Core i9-12900H (local) fail -> pass WHP, nvx-whp-test (Windows VM) fail -> pass MSHV, nvx-mshv-test (Azure Linux 3 VM) fail -> pass KVM, nvx-kvm-test (Azure Linux 3 VM, Xeon Platinum 8573C, Linux 6.18.57) fail -> pass Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 30ce395 commit fe61f9c

3 files changed

Lines changed: 182 additions & 0 deletions

File tree

‎Guide/src/dev_guide/tests/vmm.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ access them, have cases of their own in `msr_state.rs`:
6262
saves it, changes the VP, and restores it, and two that check that a reset
6363
returns the feature MSRs and the machine-check state to what a new VP holds.
6464
MSHV and WHP do not hold these elements, so these cases report ignored there.
65+
A restore refuses a saved state that holds an element that the partition lacks,
66+
such as SynIC state for a partition without Hyper-V enlightenments: it fails
67+
with an error that names the element, before it changes any state.
68+
`refuses_unsupported_cpu_state` and `refuses_unsupported_vm_state`, in
69+
`unsupported_state.rs`, restore crafted saved states that hold such an element,
70+
one at a time, and check this. The first also checks that the registers keep
71+
their values, and tries `IA32_SPEC_CTRL` and the MSRs above in addition to SynIC
72+
state, skipping each that the fixture's partition holds.
6573

6674
The contract functions operate on the public `virt` interfaces. Each function
6775
has an adjacent `backend_test!` declaration that registers it with Petri for

‎vmm_tests/vmm_tests/tests/virt_backend_tests/main.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ mod speculation;
6363
any(target_os = "linux", windows)
6464
))]
6565
mod time;
66+
#[cfg(all(
67+
guest_arch = "x86_64",
68+
guest_is_native,
69+
any(target_os = "linux", windows)
70+
))]
71+
mod unsupported_state;
6672
#[cfg(all(guest_arch = "x86_64", guest_is_native, target_os = "linux"))]
6773
mod xsave;
6874

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//! Guest-free contract tests of the saved state that a restore refuses.
5+
//!
6+
//! A snapshot that was edited, or taken from a partition with other
7+
//! capabilities, can hold an element that the partition that restores it lacks,
8+
//! and a decode accepts it. The restore must then fail, with an error that names
9+
//! the element, before it changes any state.
10+
11+
use super::native::backend_test;
12+
use hvdef::Vtl;
13+
use mesh::payload::Protobuf;
14+
use virt::Partition;
15+
use virt::PartitionAccessState;
16+
use virt::Processor;
17+
use virt::state::StateError;
18+
use virt::vm;
19+
use virt::vm::AccessVmState as _;
20+
use virt::vp;
21+
use virt::vp::AccessVpState as _;
22+
23+
const RAX: u64 = 0x1234_5678;
24+
25+
// The registers and the elements that only some partitions hold, of a saved CPU
26+
// state, with the numbers that `VpSavedState` gives them.
27+
#[derive(Default, Protobuf)]
28+
#[mesh(package = "virt.x86")]
29+
struct CpuState {
30+
#[mesh(1)]
31+
registers: Option<vp::Registers>,
32+
#[mesh(16)]
33+
spec_ctrl: Option<vp::SpecCtrl>,
34+
#[mesh(17)]
35+
umwait_control: Option<vp::UmwaitControl>,
36+
#[mesh(18)]
37+
debug_ctl: Option<vp::DebugCtl>,
38+
#[mesh(19)]
39+
misc_msrs: Option<vp::MiscMsrs>,
40+
#[mesh(20)]
41+
machine_check: Option<vp::MachineCheck>,
42+
#[mesh(100)]
43+
synic: Option<vp::SyntheticMsrs>,
44+
}
45+
46+
// The elements of a saved VM state, with the numbers that `VmSavedState` gives
47+
// them.
48+
#[derive(Default, Protobuf)]
49+
#[mesh(package = "virt.x86")]
50+
struct VmState {
51+
#[mesh(1)]
52+
hypercall: Option<vm::HypercallMsrs>,
53+
#[mesh(2)]
54+
reftime: Option<vm::ReferenceTime>,
55+
#[mesh(3)]
56+
reference_tsc_page: Option<vm::ReferenceTscPage>,
57+
}
58+
59+
// Restoring a decoded snapshot that holds an element that the partition lacks
60+
// fails and leaves the VP as it was. The registers come first in the restore
61+
// order, so a restore that checked the elements one by one would have set them
62+
// already. SynIC state is always one that the partition lacks, as the fixture
63+
// has no Hyper-V enlightenments; the others depend on the backend and the host,
64+
// and a case skips an element that the partition holds.
65+
backend_test!(refuses_unsupported_cpu_state, requires: []);
66+
fn refuses_unsupported_cpu_state(
67+
partition: &impl Partition,
68+
processor: &mut impl Processor,
69+
) -> anyhow::Result<()> {
70+
let caps = partition.caps();
71+
anyhow::ensure!(
72+
!caps.hv1,
73+
"the fixture's partition has Hyper-V enlightenments"
74+
);
75+
let mut state = processor.access_state(Vtl::Vtl0);
76+
let before = state.registers()?;
77+
78+
let elements: [(&str, bool, fn(&mut CpuState)); 6] = [
79+
("synic", true, |s| s.synic = Some(Default::default())),
80+
("spec_ctrl", !caps.spec_ctrl, |s| {
81+
s.spec_ctrl = Some(Default::default())
82+
}),
83+
("umwait_control", !caps.umwait_control, |s| {
84+
s.umwait_control = Some(Default::default())
85+
}),
86+
("debug_ctl", !caps.debug_ctl, |s| {
87+
s.debug_ctl = Some(Default::default())
88+
}),
89+
("misc_msrs", !caps.misc_msrs, |s| {
90+
s.misc_msrs = Some(Default::default())
91+
}),
92+
("machine_check", !caps.machine_check, |s| {
93+
s.machine_check = Some(Default::default())
94+
}),
95+
];
96+
for (element, lacked, add) in elements {
97+
if !lacked {
98+
continue;
99+
}
100+
let mut crafted = CpuState {
101+
registers: Some(vp::Registers { rax: RAX, ..before }),
102+
..Default::default()
103+
};
104+
add(&mut crafted);
105+
let crafted = mesh::payload::decode::<vp::VpSavedState>(&mesh::payload::encode(crafted))?;
106+
match state.restore_all(&crafted) {
107+
Err(StateError::Unsupported { element: refused }) if refused == element => {}
108+
result => anyhow::bail!(
109+
"restoring {element} into a partition that lacks it did not refuse it: {result:?}"
110+
),
111+
}
112+
anyhow::ensure!(
113+
state.registers()? == before,
114+
"the restore that refused {element} changed the registers"
115+
);
116+
}
117+
Ok(())
118+
}
119+
120+
// The same for each element of the VM state, which a partition without Hyper-V
121+
// enlightenments lacks.
122+
backend_test!(refuses_unsupported_vm_state, requires: []);
123+
fn refuses_unsupported_vm_state(
124+
partition: &(impl Partition + PartitionAccessState),
125+
_processor: &mut impl Processor,
126+
) -> anyhow::Result<()> {
127+
anyhow::ensure!(
128+
!partition.caps().hv1 && !partition.caps().hv1_reference_tsc_page,
129+
"the fixture's partition has Hyper-V enlightenments"
130+
);
131+
for (element, crafted) in [
132+
(
133+
"hypercall",
134+
VmState {
135+
hypercall: Some(vm::HypercallMsrs {
136+
guest_os_id: 1,
137+
hypercall: 1,
138+
}),
139+
..Default::default()
140+
},
141+
),
142+
(
143+
"reftime",
144+
VmState {
145+
reftime: Some(vm::ReferenceTime { value: 1 }),
146+
..Default::default()
147+
},
148+
),
149+
(
150+
"reference_tsc_page",
151+
VmState {
152+
reference_tsc_page: Some(vm::ReferenceTscPage {
153+
tsc_reference_page: 1,
154+
}),
155+
..Default::default()
156+
},
157+
),
158+
] {
159+
let crafted = mesh::payload::decode::<vm::VmSavedState>(&mesh::payload::encode(crafted))?;
160+
match partition.access_state(Vtl::Vtl0).restore_all(&crafted) {
161+
Err(StateError::Unsupported { element: refused }) if refused == element => {}
162+
result => anyhow::bail!(
163+
"restoring {element} into a partition that lacks it did not refuse it: {result:?}"
164+
),
165+
}
166+
}
167+
Ok(())
168+
}

0 commit comments

Comments
 (0)