Skip to content

Commit fdcfd69

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
ARM64: Add bit masks to allow some wrapping
In these cases, values are intentionally truncated, either to split them into multiple values or to ensure that user-supplied data does not cause Firecracker to panic with a try_from/unwrap failure. Clippy will allow us to use "as" when we use bit masks to limit the size of an explicitly-sized value (u16, u32, u64). It doesn't work on usize values or on some more complicated expressions. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commits so I can debug more easily. Signed-off-by: Jonathan Browne <[email protected]>
1 parent e43bd82 commit fdcfd69

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/vmm/src/arch/aarch64/vcpu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub enum VcpuError {
4242
pub fn get_manufacturer_id_from_state(regs: &Aarch64RegisterVec) -> Result<u32, VcpuError> {
4343
let midr_el1 = regs.iter().find(|reg| reg.id == MIDR_EL1);
4444
match midr_el1 {
45-
Some(register) => Ok(register.value::<u64, 8>() as u32 >> 24),
45+
Some(register) => Ok(((register.value::<u64, 8>() >> 24) & 0xFF) as u32),
4646
None => Err(VcpuError::GetMidrEl1(
4747
"Failed to find MIDR_EL1 in vCPU state!".to_string(),
4848
)),

src/vmm/src/cpu_config/aarch64/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ impl CpuConfiguration {
3333
for (modifier, mut reg) in template.reg_modifiers.iter().zip(self.regs.iter_mut()) {
3434
match reg.size() {
3535
RegSize::U32 => {
36-
reg.set_value(modifier.bitmap.apply(u128::from(reg.value::<u32, 4>())) as u32);
36+
reg.set_value(
37+
(modifier.bitmap.apply(u128::from(reg.value::<u32, 4>())) & 0xFFFF_FFFF)
38+
as u32,
39+
);
3740
}
3841
RegSize::U64 => {
39-
reg.set_value(modifier.bitmap.apply(u128::from(reg.value::<u64, 8>())) as u64);
42+
reg.set_value(
43+
(modifier.bitmap.apply(u128::from(reg.value::<u64, 8>()))
44+
& 0xFFFF_FFFF_FFFF_FFFF) as u64,
45+
);
4046
}
4147
RegSize::U128 => {
4248
reg.set_value(modifier.bitmap.apply(reg.value::<u128, 16>()));

0 commit comments

Comments
 (0)