@@ -1703,9 +1703,42 @@ mod tests {
17031703 if reset_xsave. len ( ) >= 576 {
17041704 expected_xsave[ 512 ..576 ] . copy_from_slice ( & reset_xsave[ 512 ..576 ] ) ;
17051705 }
1706+
1707+ // Compute the end of valid XSAVE component data. In compacted format (MSHV/WHP),
1708+ // components are packed contiguously starting at offset 576. Bytes beyond the last
1709+ // component are undefined
1710+ let xcomp_bv = u64:: from_le_bytes ( reset_xsave[ 520 ..528 ] . try_into ( ) . unwrap_or ( [ 0u8 ; 8 ] ) ) ;
1711+ let compacted = ( xcomp_bv & ( 1u64 << 63 ) ) != 0 ;
1712+ let valid_end = if compacted {
1713+ // Compacted format: compute end from CPUID component sizes
1714+ let supported = xsave_supported_components ( ) ;
1715+ let mut offset = 576usize ;
1716+ for comp_id in 2 ..63u32 {
1717+ if ( supported & ( 1u64 << comp_id) ) == 0 {
1718+ continue ;
1719+ }
1720+ if ( xcomp_bv & ( 1u64 << comp_id) ) == 0 {
1721+ continue ;
1722+ }
1723+ let ( size, _, align_64) = xsave_component_info ( comp_id) ;
1724+ if align_64 {
1725+ offset = offset. next_multiple_of ( 64 ) ;
1726+ }
1727+ offset += size;
1728+ }
1729+ offset
1730+ } else {
1731+ // Standard format (KVM): full buffer is valid
1732+ reset_xsave. len ( )
1733+ } ;
1734+
1735+ // Only compare bytes within the valid region
17061736 assert_eq ! (
1707- reset_xsave, expected_xsave,
1708- "xsave should be zeroed except for hypervisor-specific fields"
1737+ & reset_xsave[ ..valid_end] ,
1738+ & expected_xsave[ ..valid_end] ,
1739+ "xsave should be zeroed except for hypervisor-specific fields \
1740+ (comparing bytes 0..{valid_end} of {} total)",
1741+ reset_xsave. len( )
17091742 ) ;
17101743
17111744 // Verify sregs are reset to defaults (CR3 is 0 as passed to reset_vcpu)
0 commit comments