Skip to content

fix(device): reject int32-overflowing memory requests in iluvatar and mthreads - #2285

Merged
hami-robot[bot] merged 7 commits into
Project-HAMi:masterfrom
veyron-kairo:fix/mem-int32-overflow
Aug 20, 2026
Merged

fix(device): reject int32-overflowing memory requests in iluvatar and mthreads#2285
hami-robot[bot] merged 7 commits into
Project-HAMi:masterfrom
veyron-kairo:fix/mem-int32-overflow

Conversation

@veyron-kairo

@veyron-kairo veyron-kairo commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?
/kind bug

What this PR does / why we need it:
GenerateResourceRequests in the nvidia, cambricon, iluvatar, and mthreads
backends multiplies the requested device memory by a per-backend factor and
narrows the result to int32. A normal Gi-scale quantity like 16Gi wraps past
math.MaxInt32 to Memreq: 0, which passes the Totalmem - Usedmem < memreq
check in Fit vacuously, so the pod is scheduled onto an already-full device —
oversubscription and OOM.

This bounds the memory value before the multiply and rejects the request on
overflow instead of silently truncating to zero. It also rejects decimal-form
quantities (AsInt64() returns false for values like 16.0Gi, which previously
fell through and left the request at zero). Mirrors the device-count guard in
mthreads (#2134) and the enflame int32 bounding in #2145 / #2190.

Which issue(s) this PR fixes:
Fixes #2284
Fixes #2278

Special notes for your reviewer:
Covers nvidia, cambricon, iluvatar, and mthreads (nvidia added per @archlitchi's
review). Each backend has regression cases for both an int32-overflowing integer
(16Gi) and a decimal-form quantity (16.0Gi), both of which are now rejected
rather than truncated. Merged current master to resolve conflicts with the new
MemoryFactor const. make verify and the device unit tests pass locally.

AI assistance disclosure:
I used AI assistance (Claude Code) to help locate the overflow across backends,
resolve the merge, and draft the tests. I reviewed and verified the change,
confirmed the before/after behavior with the regression tests, ran make verify
locally, and take responsibility for it.

Does this PR introduce a user-facing change?:
NONE

Summary by CodeRabbit

  • Bug Fixes

    • Prevented invalid memory requests from overflowing, truncating, or being interpreted as zero during device allocation.
    • Out-of-range, negative, and non-integer memory quantities are now rejected safely.
    • Valid memory requests continue to be processed normally.
  • Tests

    • Added coverage for oversized and decimal-form memory requests across supported device types.
    • Verified invalid requests return empty device allocations.

… mthreads

GenerateResourceRequests multiplied the requested memory by a per-backend
factor (x256 for iluvatar, x512 for mthreads) and narrowed the result to
int32. A Gi-scale quantity like 16Gi wraps past math.MaxInt32 to Memreq: 0,
which passes the Totalmem-Usedmem < memreq check vacuously and lets the pod
schedule onto a full device (oversubscription / OOM).

Guard the memory value against the int32 range and reject the request when it
overflows, mirroring the device-count guard already present in mthreads
(Project-HAMi#2134) and the enflame int32 bounding in Project-HAMi#2145 / Project-HAMi#2190. Adds a regression
case to each backend's Test_GenerateResourceRequests.

Fixes Project-HAMi#2284

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: yes labels Aug 3, 2026
@hami-robot
hami-robot Bot requested review from DSFans2014 and wawa0210 August 3, 2026 01:19
@hami-robot hami-robot Bot added the size/M label Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Iluvatar, Mthreads, Cambricon, and NVIDIA now reject invalid, negative, and overflowing memory requests before int32 conversion. Tests cover oversized Gi-scale and decimal-form quantities.

Changes

Device memory range validation

Layer / File(s) Summary
Backend memory validation
pkg/device/iluvatar/device.go, pkg/device/mthreads/device.go, pkg/device/cambricon/device.go, pkg/device/nvidia/device.go
GenerateResourceRequests validates integer parsing, nonnegative values, and scaled int32 bounds. Invalid requests are logged and return empty device requests.
Invalid memory regression coverage
pkg/device/iluvatar/device_test.go, pkg/device/mthreads/device_test.go, pkg/device/cambricon/device_test.go, pkg/device/nvidia/device_test.go
Tests verify rejection of oversized 16Gi values and decimal-form 16.0Gi values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

  • Issue 2336: Covers related GenerateResourceRequests int32 overflow handling across device backends.
  • Issue 2383: Tracks the same memory-overflow class for other device backends.

Possibly related PRs

Suggested reviewers: wawa0210, dsfans2014

Poem

A rabbit checks each memory byte,
Before it enters signed-range light.
Four backends guard the gate,
Invalid requests return empty state,
Tests keep every bound in sight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately identifies the overflow fix in Iluvatar and MThreads, but it omits the related Cambricon and NVIDIA changes.
Linked Issues check ✅ Passed The changes reject overflowing and invalid memory requests for Iluvatar, MThreads, and Cambricon, satisfying issues #2284 and #2278.
Out of Scope Changes check ✅ Passed All changes address the same int32 memory-overflow defect, and NVIDIA is explicitly included in the PR objective; no unrelated changes are shown.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested a review from archlitchi August 3, 2026 01:20

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/device/iluvatar/device.go`:
- Around line 220-224: Validate memnums before converting or multiplying in both
backends: in pkg/device/iluvatar/device.go at lines 220-224, reject values above
int64(math.MaxInt32)/256 before the int(memnums)*256 calculation; in
pkg/device/mthreads/device.go at lines 225-229, apply the corresponding
int64(math.MaxInt32)/512 check, then divide the validated product by n before
assigning Memreq. Preserve the existing rejection behavior and container
context.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 957c20e8-c92f-47b0-acf6-87ebcde17d5b

📥 Commits

Reviewing files that changed from the base of the PR and between 65f59ae and 0d7459d.

📒 Files selected for processing (4)
  • pkg/device/iluvatar/device.go
  • pkg/device/iluvatar/device_test.go
  • pkg/device/mthreads/device.go
  • pkg/device/mthreads/device_test.go

Comment thread pkg/device/iluvatar/device.go Outdated
Move the int32 range check ahead of the int(memnums)*factor multiplication so
an extreme AsInt64 value cannot overflow the intermediate product before it is
checked. Bounds memnums against int64(math.MaxInt32)/factor, matching the
bound-before-conversion approach in enflame (Project-HAMi#2145). Addresses CodeRabbit
review feedback; behavior and tests unchanged.

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 62.22% <100.00%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
pkg/device/cambricon/device.go 86.41% <100.00%> (+2.88%) ⬆️
pkg/device/iluvatar/device.go 59.03% <100.00%> (+0.54%) ⬆️
pkg/device/mthreads/device.go 86.63% <100.00%> (+0.16%) ⬆️
pkg/device/nvidia/device.go 95.27% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mesutoezdil

Copy link
Copy Markdown
Contributor

Cambricon has the same overflow class and is tracked separately in #2278, with no open PR yet. Given you already have the pattern worked out here (negative/overflow guard before the int32 narrowing), would you be open to extending this PR to cover cambricon too, or opening a follow-up PR for it?

Cambricon's GenerateResourceRequests has the same int32 overflow as iluvatar
and mthreads: memory is multiplied by 256 and narrowed to int32, so a Gi-scale
request wraps to Memreq: 0 and passes the memory fit check vacuously. Apply the
same bound-before-multiply guard and add a regression case.

Requested by a reviewer on the PR; also closes Project-HAMi#2278.

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@veyron-kairo

Copy link
Copy Markdown
Contributor Author

thanks — extended it to cambricon in bf214f2, same guard (bound before the ×256) plus a regression case, and linked #2278 so it closes on merge.

one thing I noticed while doing this: nvidia's GenerateResourceRequests has the same int32(memnum) narrowing. for nvidia it's only reachable via a misconfigured Gi value or a large MemoryFactor since it doesn't multiply by a fixed unit, so it's a weaker case — happy to fold it in here too or leave it out to keep this focused. your call.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/device/cambricon/device.go`:
- Around line 255-259: The Cambricon memory-request validation must reject
decimal-backed quantities instead of leaving memnum at zero when
Quantity.AsInt64() fails. In pkg/device/cambricon/device.go at lines 255-259,
handle AsInt64 failure using Sign()/CmpInt64() and return the existing empty
ContainerDeviceRequest for invalid or out-of-range values; in
pkg/device/cambricon/device_test.go at lines 290-304, add regression coverage
for a decimal resource such as 16.0Gi and verify it is rejected.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ecfb6d04-2f63-447b-8b56-b28848488f2b

📥 Commits

Reviewing files that changed from the base of the PR and between 9cae023 and bf214f2.

📒 Files selected for processing (2)
  • pkg/device/cambricon/device.go
  • pkg/device/cambricon/device_test.go

Comment thread pkg/device/cambricon/device.go Outdated
@mesutoezdil

Copy link
Copy Markdown
Contributor

cc @archlitchi wdyt, fold nvidia's same int32 narrowing into this PR too, or keep it separate?

@archlitchi

Copy link
Copy Markdown
Member

cc @archlitchi wdyt, fold nvidia's same int32 narrowing into this PR too, or keep it separate?

i think we should narrow them into this PR too

@archlitchi

Copy link
Copy Markdown
Member

please resolve these conflicts and resolve the code-rabbit comment

Resolve conflicts in cambricon/iluvatar/mthreads GenerateResourceRequests:
master moved the hardcoded memory factor to the new MemoryFactor const; keep
that and re-apply the overflow guard on top.

Per review on Project-HAMi#2285:
- extend the guard to nvidia's GenerateResourceRequests (int32(memnum))
- reject decimal-form quantities: AsInt64 returns false for values like 16.0Gi,
  which previously fell through and left the request at zero
- add decimal-form regression cases for all four backends

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@hami-robot hami-robot Bot removed the size/M label Aug 6, 2026
@hami-robot hami-robot Bot added the size/L label Aug 6, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/device/nvidia/device_test.go (1)

2016-2039: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover the scaled overflow boundary.

Lines 2016-2039 use MemoryFactor: 1. They only test a value that already exceeds int32 before scaling. Add a MemoryFactor: 2 case with 1Gi, which fits before scaling but exceeds int32 after scaling. Expect an empty ContainerDeviceRequest. This protects the bound-before-multiply check at line 549.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/device/nvidia/device_test.go` around lines 2016 - 2039, Extend the
relevant device request test cases to cover scaled overflow by adding a
MemoryFactor: 2 scenario with a 1Gi memory request, which fits before scaling
but must be rejected after multiplication. Assert an empty
device.ContainerDeviceRequest and target the test setup exercising the
bound-before-multiply logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/device/nvidia/device_test.go`:
- Around line 2016-2039: Extend the relevant device request test cases to cover
scaled overflow by adding a MemoryFactor: 2 scenario with a 1Gi memory request,
which fits before scaling but must be rejected after multiplication. Assert an
empty device.ContainerDeviceRequest and target the test setup exercising the
bound-before-multiply logic.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8947ca5d-9888-41f2-a298-43fe49bbb477

📥 Commits

Reviewing files that changed from the base of the PR and between bf214f2 and aa96b96.

📒 Files selected for processing (8)
  • pkg/device/cambricon/device.go
  • pkg/device/cambricon/device_test.go
  • pkg/device/iluvatar/device.go
  • pkg/device/iluvatar/device_test.go
  • pkg/device/mthreads/device.go
  • pkg/device/mthreads/device_test.go
  • pkg/device/nvidia/device.go
  • pkg/device/nvidia/device_test.go
🚧 Files skipped from review as they are similar to previous changes (6)
  • pkg/device/iluvatar/device_test.go
  • pkg/device/cambricon/device_test.go
  • pkg/device/mthreads/device.go
  • pkg/device/cambricon/device.go
  • pkg/device/iluvatar/device.go
  • pkg/device/mthreads/device_test.go

@veyron-kairo

veyron-kairo commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

pushed the changes.

nvidia's in now, the guard bounds the value before the int32(memnum) narrowing there too. resolved the conflicts against master (kept the new MemoryFactor const and put the overflow check back on top of it). for the coderabbit comment: the guard used to sit inside the AsInt64 ok-branch, so a decimal quantity like 16.0Gi (where AsInt64 returns false) slipped past it and left the request at 0. it rejects on !parsed or out-of-range now. added tests for both 16Gi and 16.0Gi across the four backends.

one thing on the red Unit test check, that's not coming from this PR. master itself is failing the scheduler quota tests right now because my #2313 and #2347 conflict once they're both on master (each passed CI on its own base). fix is up in #2408, it's test-only. once that merges i'll re-merge master here and this should go green.

@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026
Comment thread pkg/device/cambricon/device_test.go Outdated
@coderabbitai coderabbitai Bot mentioned this pull request Aug 7, 2026
The test case names already state what each case covers, and the rest of
these tables carry no comments. Addresses review feedback.

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@mesutoezdil

Copy link
Copy Markdown
Contributor

You can view the relevant rule here.
https://github.com/Project-HAMi/HAMi/blob/master/CONTRIBUTING.md#contribution-gates
"4. Review replies. The reply you post must be written by you and must address the specific point raised. Verbatim or canned AI replies, or replies that do not engage the comment, lead to the PR being closed."

@veyron-kairo

veyron-kairo commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

@mesutoezdil , actually I write and give it to ai every time I write to polish and make the content shorter , isn't that allowed the whole thinking and the idea is mine .

if its not allowed I'll write. it in my own words this time .!

@mesutoezdil mesutoezdil reopened this Aug 10, 2026
Comment thread pkg/device/nvidia/device.go
…rflow

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@veyron-kairo

Copy link
Copy Markdown
Contributor Author

@archlitchi , resolved the conflicts .

@FouoF

FouoF commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@veyron-kairo I think we should not only reject the memory requests over int32, but also remind the users that they may used invalid unit. As your example shown, this condition happens on the user input like 16Gi not an integer over int32. So I suggest update the error message to include "Memory unit is treated as MB not Byte".

@mesutoezdil

Copy link
Copy Markdown
Contributor

title only says iluvatar/mthreads, but nvidia and cambricon are in scope too, fix the title

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot

hami-robot Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, veyron-kairo

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the approved label Aug 20, 2026
@hami-robot
hami-robot Bot merged commit c784199 into Project-HAMi:master Aug 20, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants