Skip to content

Conversation

@SrinivasShekar
Copy link
Contributor

Issue:

The VTL2 settings processing code crashes with a divide-by-zero panic when the chunk_size_in_kb field is missing from LUN configurations in the protobuf schema.

Closes: #1796

Fix:

Fix the code wherein chunk_size_in_bytes takes the default value when the field is missing in the protobuf schema.

From the striped code,

// If chunk_size_in_bytes is missing in protobuf, the below code will be Some(0).unwrap_or(..) which will result in zero.
let chunk_size_in_bytes = chunk_size_in_bytes.unwrap_or(CHUNK_SIZE_128K); 
....
let sector_count_per_chunk = (chunk_size_in_bytes / sector_size) as u64;  // Will result in divide-by-zero.

Include a conditional filter for setting the chunk_size to default(CHUNK_SIZE_128K) for such scenarios.

Validation:

Locally ran vmm_tests(striped_disk related tests) successfully.

Nextest run ID eeab7f93-a5a0-45a0-9ebf-3fad41281477 with nextest profile: default
Starting 2 tests across 2 binaries (171 tests skipped)
Running [ 00:00:00] 0/2: 0 running, 0 passed, 0 skipped
SLOW [> 60.000s] vmm_tests::tests x86_64::storage::openvmm_openhcl_uefi_x64_ubuntu_2504_server_x64_openhcl_linux_stripe_storvsp
SLOW [> 60.000s] vmm_tests::tests x86_64::storage::openvmm_openhcl_linux_x64_openhcl_linux_stripe_storvsp
PASS [ 97.852s] vmm_tests::tests x86_64::storage::openvmm_openhcl_linux_x64_openhcl_linux_stripe_storvsp
SLOW [>120.000s] vmm_tests::tests x86_64::storage::openvmm_openhcl_uefi_x64_ubuntu_2504_server_x64_openhcl_linux_stripe_storvsp
SLOW [>180.000s] vmm_tests::tests x86_64::storage::openvmm_openhcl_uefi_x64_ubuntu_2504_server_x64_openhcl_linux_stripe_storvsp
PASS [ 184.070s] vmm_tests::tests x86_64::storage::openvmm_openhcl_uefi_x64_ubuntu_2504_server_x64_openhcl_linux_stripe_storvsp
────────────
Summary [ 184.140s] 2 tests run: 2 passed (2 slow), 171 skipped

@SrinivasShekar SrinivasShekar requested a review from a team October 13, 2025 10:53
@SrinivasShekar SrinivasShekar self-assigned this Oct 13, 2025
@SrinivasShekar SrinivasShekar requested a review from a team as a code owner October 13, 2025 10:53
@Copilot Copilot AI review requested due to automatic review settings October 13, 2025 10:53
@SrinivasShekar SrinivasShekar requested a review from a team as a code owner October 13, 2025 10:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Fixes a divide-by-zero panic in the disk striping functionality when the chunk_size_in_kb field is missing from VTL2 settings protobuf schema. The issue occurred because missing protobuf fields default to 0, which caused division by zero in subsequent calculations.

  • Adds a filter to check for zero chunk size values before using the default
  • Prevents divide-by-zero errors when chunk_size_in_kb is missing from protobuf

Comment on lines 252 to 259
let read_only = devices[0].is_read_only();
let chunk_size_in_bytes = chunk_size_in_bytes.unwrap_or(CHUNK_SIZE_128K);
let chunk_size_in_bytes = chunk_size_in_bytes
.filter(|&chunk_size| chunk_size != 0)
.unwrap_or(CHUNK_SIZE_128K);
if !chunk_size_in_bytes.is_multiple_of(sector_size) {
return Err(NewDeviceError::InvalidChunkSize(
chunk_size_in_bytes,
sector_size,
));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elegant fix, but given that this code is already doing some validation, suggest you actually just return an error to the user. Or, are there some cases where you think this would regress something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR Matt!

There are two scenarios where chunk_size_in_bytes will be Some(0)
(1) when it's not explicitly specified in the VTL2 settings - This is possible as per the bug
(2) set to 0 by the user.

Now for the latter scenario, we could return an error but for the former, IMO, it would break the existing user experience and tests probably (tests should be okay as we could fix them up).

But the intention of using unwrap_or() on chunk_size_in_bytes field was to set it to default when it's not specified, hence followed on the similar lines to accommodate the fix.

Please let me know your thoughts too :)

Copy link
Contributor Author

@SrinivasShekar SrinivasShekar Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the protobuf schema: [Lun settings] , chunk_size_in_kb is not an optional field.

Whereas in the stripeddiskhandle struct, the same field is optional which is causing the confusion here (Some(0) vs None for missing fields) --

pub chunk_size_in_bytes: Option<u32>,

From the vtl2LunBuilder test code,
We set the default value to 0 for this field which is incorrect, rather it should be set to 128KB which is why we could repro the issue with the storage.rs test script and not with other tools.

chunk_size_in_kb: 0,

But I agree that there should be a validation for zero in the below code to avoid the case where it is set to 0 explicitly by the user --

    if **chunk_size_in_bytes == 0** || !chunk_size_in_bytes.is_multiple_of(sector_size) { 
        return Err(NewDeviceError::InvalidChunkSize(
            chunk_size_in_bytes,
            sector_size,
        ));

I'll update the PR title to reflect the issue.

cc: @mattkur , @smalis-msft

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably change either vtl2settings or the lunbuilder so that either they're both optional or both required, right? It sounds like both optional should be the right path, and petri should set it to None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was another option I was thinking off that both should be kept optional in the vtl2settings but I need to check if it's not breaking anywhere else.

@smalis-msft
Copy link
Contributor

smalis-msft commented Oct 13, 2025

If this is a case where a field is missing why are we deserializing it into Some(0) instead of None?

@jstarks
Copy link
Member

jstarks commented Oct 22, 2025

Given that the chunk size is so fundamental to the data layout (and if it changes, then the data gets silently corrupted), are you sure you want to make it quietly optional in the VTL2 settings? I would just add the == 0 check and call it good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VTL2 Settings: Divide by zero panic when chunk_size_in_kb is missing from LUN configuration

4 participants