Skip to content

Commit 970495a

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
ARM64: Add try_from()/unwrap() in cases where it's safe
Some places in the code don't allow us to re-type values to let the compiler verify that those conversions are safe, particularly with length values. All of these are marked with comments justifying why they are safe. 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 fdcfd69 commit 970495a

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/vmm/src/arch/aarch64/cache_info.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,14 @@ fn mask_str2bit_count(mask_str: &str) -> Result<u16, CacheInfoError> {
248248
if s_zero_free.is_empty() {
249249
s_zero_free = "0";
250250
}
251-
bit_count += u32::from_str_radix(s_zero_free, 16)
252-
.map_err(|err| {
253-
CacheInfoError::InvalidCacheAttr("shared_cpu_map".to_string(), err.to_string())
254-
})?
255-
.count_ones() as u16;
251+
bit_count += u16::try_from(
252+
u32::from_str_radix(s_zero_free, 16)
253+
.map_err(|err| {
254+
CacheInfoError::InvalidCacheAttr("shared_cpu_map".to_string(), err.to_string())
255+
})?
256+
.count_ones(),
257+
)
258+
.unwrap(); // Safe because this is at most 32
256259
}
257260
if bit_count == 0 {
258261
return Err(CacheInfoError::InvalidCacheAttr(

src/vmm/src/arch/aarch64/fdt.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<(), FdtEr
169169
// The operation is safe since we already checked when creating cache attributes that
170170
// cpus_per_unit is not 0 (.e look for mask_str2bit_count function).
171171
let cache_phandle = LAST_CACHE_PHANDLE
172-
- (num_cpus * (cache.level - 2) as usize + cpu_index / cache.cpus_per_unit as usize)
173-
as u32;
172+
- u32::try_from(
173+
num_cpus * (cache.level - 2) as usize
174+
+ cpu_index / cache.cpus_per_unit as usize,
175+
)
176+
.unwrap(); // Safe because the number of CPUs is bounded
174177

175178
if prev_level != cache.level {
176179
fdt.property_u32("next-level-cache", cache_phandle)?;

src/vmm/src/cpu_config/aarch64/custom_cpu_template.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ impl CustomCpuTemplate {
6363
let reg_size = reg_size(modifier.addr);
6464
match RegSize::from(reg_size) {
6565
RegSize::U32 | RegSize::U64 => {
66-
let limit = 2u128.pow(reg_size as u32 * 8) - 1;
66+
// Safe to unwrap because the number of bits is limited
67+
let limit = 2u128.pow(u32::try_from(reg_size).unwrap() * 8) - 1;
6768
if limit < modifier.bitmap.value || limit < modifier.bitmap.filter {
6869
return Err(serde_json::Error::custom(format!(
6970
"Invalid size of bitmap for register {:#x}, should be <= {} bits",

0 commit comments

Comments
 (0)