Skip to content

Commit 087d3fc

Browse files
committed
pci: add unit test for accesses to invalid registers
Make sure we handle correctly accessing invalid registers. Signed-off-by: Babis Chalios <[email protected]>
1 parent 958b14a commit 087d3fc

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/pci/src/configuration.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,10 @@ impl PciConfiguration {
831831
}
832832

833833
pub fn write_config_register(&mut self, reg_idx: usize, offset: u64, data: &[u8]) {
834+
if reg_idx >= NUM_CONFIGURATION_REGISTERS {
835+
return;
836+
}
837+
834838
if offset as usize + data.len() > 4 {
835839
return;
836840
}
@@ -1401,4 +1405,50 @@ mod tests {
14011405
assert_eq!(pci_config.get_bar_addr(0), 0x1000);
14021406
assert_eq!(pci_config.get_bar_addr(2), 0x1_0000_0000);
14031407
}
1408+
1409+
#[test]
1410+
fn test_access_invalid_reg() {
1411+
let mut pci_config = PciConfiguration::new(
1412+
0x42,
1413+
0x0,
1414+
0x0,
1415+
PciClassCode::MassStorage,
1416+
&PciMassStorageSubclass::SerialScsiController,
1417+
None,
1418+
PciHeaderType::Device,
1419+
0x13,
1420+
0x12,
1421+
None,
1422+
None,
1423+
);
1424+
1425+
// Can't read past the end of the configuration space
1426+
assert_eq!(
1427+
pci_config.read_reg(NUM_CONFIGURATION_REGISTERS),
1428+
0xffff_ffff
1429+
);
1430+
1431+
// Read out all of configuration space
1432+
let config_space: Vec<u32> = (0..NUM_CONFIGURATION_REGISTERS)
1433+
.map(|reg_idx| pci_config.read_reg(reg_idx))
1434+
.collect();
1435+
1436+
// Various invalid write accesses
1437+
1438+
// Past the end of config space
1439+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 0, &[0x42]);
1440+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 0, &[0x42, 0x42]);
1441+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 0, &[0x42, 0x42, 0x42, 0x42]);
1442+
1443+
// Past register boundaries
1444+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 1, &[0x42, 0x42, 0x42, 0x42]);
1445+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 2, &[0x42, 0x42, 0x42]);
1446+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 3, &[0x42, 0x42]);
1447+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 4, &[0x42]);
1448+
pci_config.write_config_register(NUM_CONFIGURATION_REGISTERS, 5, &[]);
1449+
1450+
for (reg_idx, reg) in config_space.iter().enumerate() {
1451+
assert_eq!(*reg, pci_config.read_reg(reg_idx));
1452+
}
1453+
}
14041454
}

0 commit comments

Comments
 (0)