Skip to content

fix(hypervisors): correct memory unit conversion and validation - #826

Draft
Nachiket-Roy wants to merge 1 commit into
urunc-dev:mainfrom
Nachiket-Roy:fix/memory-conversion-and-validation
Draft

Nachiket-Roy wants to merge 1 commit into
urunc-dev:mainfrom
Nachiket-Roy:fix/memory-conversion-and-validation

Conversation

@Nachiket-Roy

@Nachiket-Roy Nachiket-Roy commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR fixes the memory handling issues reported in #818 and #819 by replacing the previous global conversion with monitor-specific handling.

Previously, OCI memory limits were converted using decimal MB for all monitors, which could cause the guest to receive more memory than requested. Additionally, requests that rounded to 0 were treated as unspecified and silently replaced with DefaultMemory (256MB).

This PR updates each backend to use the most accurate representation it supports:

  • QEMU: pass exact byte values (-m <bytes>B).
  • Cloud Hypervisor: pass exact byte values (--memory size=<bytes>).
  • Firecracker: convert to MiB and clamp values below 1 MiB to the minimum representable value.
  • Solo5: convert to MiB and clamp values below 1 MiB to satisfy its CLI validation.

An unspecified (0) memory request continues to fall back to DefaultMemory, while valid non-zero requests are preserved as accurately as each monitor allows.

Related issues

How was this tested?

  • Verified each monitor's supported memory units from its implementation and documentation.
  • Boot-tested QEMU, Cloud Hypervisor, Firecracker, and Solo5 with real guest images.
  • Confirmed QEMU and Cloud Hypervisor preserve exact byte values end-to-end.
  • Added unit tests for the new conversion helpers and updated QEMU tests.
  • Ran go test -v ./pkg/....

LLM usage

Claude (Anthropic) Sonnet 5 was used to discuss implementation trade-offs. All implementation details and behavior were independently verified through source inspection, documentation, and runtime testing.

Checklist

  • I have read the contribution guide.
  • The linter passes locally (make lint).
  • The e2e tests of at least one tool pass locally (make test_ctr, make test_nerdctl, make test_docker, make test_crictl).
  • If LLMs were used: I have read the llm policy.

@netlify

netlify Bot commented Jul 15, 2026

Copy link
Copy Markdown

👷 Deploy Preview for urunc processing.

Name Link
🔨 Latest commit 92fab26
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/6a5798d7f2549900089a4474

@netlify

netlify Bot commented Jul 15, 2026

Copy link
Copy Markdown

✅ Deploy Preview for urunc canceled.

Name Link
🔨 Latest commit 92fab26
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/6a5798d7f2549900089a4474

@Nachiket-Roy
Nachiket-Roy marked this pull request as draft September 8, 2026 21:05
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days.

@github-actions github-actions Bot added the Stale label Nov 8, 2026
@cmainas cmainas removed the Stale label Nov 9, 2026
@cmainas

cmainas commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Hello @Nachiket-Roy ,

can you bring this up-to-date with main?

Update various VMM backends (QEMU, Firecracker, HVT, SPT, and Cloud
Hypervisor) to correctly convert and validate memory sizes to their
expected units (bytes/MiB) instead of MB.

- QEMU and Cloud Hypervisor now use raw bytes.
- Firecracker, HVT, and SPT now use MiB.
- Low memory values (<1 MiB) for Solo5 (HVT/SPT) and Firecracker
  are clamped to 1 MiB instead of falling back to default 256 MiB.
- Replace utility BytesToStringMB with BytesToBString and
  BytesToMiBString, and update tests accordingly.

Fixes: urunc-dev#818
Fixes: urunc-dev#819

Signed-off-by: Nachiket Roy <nachiket.roy.2@gmail.com>
@Nachiket-Roy
Nachiket-Roy force-pushed the fix/memory-conversion-and-validation branch from 92fab26 to 6731e28 Compare November 14, 2026 12:40
@netlify

netlify Bot commented Nov 14, 2026

Copy link
Copy Markdown

👷 Deploy request for urunc pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 6731e28

@cmainas cmainas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hello @Nachiket-Roy ,

the changes in this PR break cloud-hypervisor and Qemu which now do not boot. Please test your changes before opening a PR or pushing any changes in a PR.

Some generic comments for this PR:

  • Since Firecracker and Solo5 use MiB by default, we can use this unit for all monitors.
  • One monitor hyperlight-unikraft is missing.
  • The handling of less than 1MiB memory should be uniform across all implementations. Qemu and CLoud-hypervisor have different behavior than Solo5 and Firecracker.
  • We should end up with a single function to handle memory with all rules applied in that function and only there. All other functions should get removed/replaced.

}

// Memory configuration
memSize := args.MemSizeB

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Memory for cloud-hypervisor should be a multiple of a page size. As a result, passing 1G as memory limit the cli option will become size=1000000000, which will fail the booting in cloud-hypervisor. See https://github.com/cloud-hypervisor/cloud-hypervisor/blob/v54.1/vmm/src/memory_manager.rs#L686

func (h *HVT) BuildExecCmd(args types.ExecArgs, ukernel types.Unikernel) ([]string, error) {
hvtMem := BytesToStringMB(args.MemSizeB)
hvtMem := BytesToMiBString(args.MemSizeB)
if args.MemSizeB > 0 && args.MemSizeB < 1024*1024 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This check should be inside BytesToMiBString.

hvtMem := BytesToMiBString(args.MemSizeB)
if args.MemSizeB > 0 && args.MemSizeB < 1024*1024 {
hvtMem = "1"
vmmLog.Warnf("Requested memory (%d bytes) is below Solo5's minimum unit (1 MiB); clamping to 1 MiB", args.MemSizeB)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use a different word than "clamping"

exArgs := []string{
q.binaryPath,
"-m", qemuMem + "M",
"-m", qemuMem + "B",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Qemu rounds up the memory to a multiple of 8KiB See https://github.com/qemu/qemu/blob/v11.1.3/hw/core/machine.c#L600 but it does not do the same for memory-backend-file and therefore these might end up different. Please use MiB everywhere.

name: "custom memory sub-1-MiB clamps to 1 MiB",
args: types.ExecArgs{UnikernelPath: testKernelPath, MemSizeB: 512 * 1024},
unikernel: &fakeUnikernel{},
mustContain: []string{"--mem=1"},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be change to an exact match because the current check will also get cases like --mem=10

return ch.binaryPath
}

// BuildExecCmd builds and validates the Cloud Hypervisor command arguments without executing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why?

input uint64
expected string
}{
{"zero (Case A)", 0, "268435456"}, // 256 MB * 1024 * 1024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is Case A?

func bytesToMB(bytes uint64) uint64 {
const bytesInMB = 1000 * 1000
return bytes / bytesInMB
func BytesToBString(argMem uint64) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this exported?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

2 participants