Skip to content

fix(hygon,metax): guard int32 narrowing in GenerateResourceRequests - #2388

Merged
hami-robot[bot] merged 3 commits into
Project-HAMi:masterfrom
Eshiv-Pandey:fix/int32-overflow-hygon-metax-sgpu
Aug 14, 2026
Merged

fix(hygon,metax): guard int32 narrowing in GenerateResourceRequests#2388
hami-robot[bot] merged 3 commits into
Project-HAMi:masterfrom
Eshiv-Pandey:fix/int32-overflow-hygon-metax-sgpu

Conversation

@Eshiv-Pandey

@Eshiv-Pandey Eshiv-Pandey commented Aug 5, 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 hygon and metax-sgpu device backends converts user-controlled int64 resource limits (device count, memory, cores) into int32 fields on ContainerDeviceRequest (Nums, Memreq, Coresreq) without bounds checking. A resource limit exceeding math.MaxInt32 silently wraps to an incorrect small or negative value that flows into Fit(), causing the scheduler to incorrectly assess device availability and potentially schedule pods onto overcommitted devices.

This PR adds range guards that return an empty ContainerDeviceRequest{} (the existing rejection sentinel) before any int32() narrowing, matching the overflow protection already implemented in the enflame, iluvatar, mthreads, and amd backends.

Which issue(s) this PR fixes:
Fixes #2383

Special notes for your reviewer:

  • The hygon fix guards three conversion points: device count, memory pre-factor, and memory post-factor (after MemoryFactor multiplication).
  • The metax-sgpu fix guards three conversion points: device count, core, and memory.
  • Test coverage includes both pre-factor overflow (16Gi exceeds MaxInt32 directly) and post-factor overflow (raw value 3000000 fits in int32, but 3000000 * 1024 overflows), plus an out-of-range memory case for metax-sgpu.
  • Follows the same validation pattern introduced in fix(device): reject int32-overflowing memory requests in iluvatar and mthreads #2285.

Does this PR introduce a user-facing change?:

Oversized device resource requests (e.g. hygon.com/dcumem: 16Gi, or metax-tech.com/vmemory exceeding the int32 range) are now correctly rejected instead of silently wrapping to an incorrect memory value.

AI assistance disclosure: I used an AI tool for codebase navigation and review support.

Summary by CodeRabbit

  • Bug Fixes
    • Added validation for device counts, memory, and core resource requests to reject negative or oversized values.
    • Prevented integer overflow when converting memory requests, including values affected by memory scaling.
    • Invalid resource requests now return an empty device request instead of producing incorrect results.
  • Tests
    • Added regression coverage for oversized and overflow-prone resource requests.

@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: yes labels Aug 5, 2026
@hami-robot
hami-robot Bot requested review from archlitchi and mesutoezdil August 5, 2026 17:06
@hami-robot hami-robot Bot added the size/M label Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 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

Hygon and Metax resource request generation now rejects negative or int32-overflowing counts, memory values, scaled memory values, and core values. Regression tests cover invalid inputs and memory-factor overflow.

Changes

Resource range validation

Layer / File(s) Summary
Hygon request validation
pkg/device/hygon/device.go, pkg/device/hygon/device_test.go
Hygon validates DCU counts, memory values, scaled memory values, and core values. Tests cover oversized memory, invalid counts and cores, and memory-factor overflow.
Metax request validation
pkg/device/metax/sdevice.go, pkg/device/metax/sdevice_test.go
Metax validates SGPU counts, core values, and converted memory values. Memory conversion uses explicit int64 multiplication. Tests cover oversized resource requests.

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

Possibly related issues

  • Project-HAMi/HAMi#2336 — Identifies the same int32 overflow in the Hygon and Metax-SGPU implementations.
  • Project-HAMi/HAMi#2382 — Addresses the same Hygon and Metax-SGPU memory overflow behavior.
  • Project-HAMi/HAMi#2284 — Applies similar int32 overflow handling to other device backends.

Possibly related PRs

  • Project-HAMi/HAMi#2285 — Adds comparable int32 overflow validation and regression tests for other GPU backends.
  • Project-HAMi/HAMi#2338 — Modifies Hygon and Metax request handling with a different out-of-range behavior.
  • Project-HAMi/HAMi#2134 — Adds math.MaxInt32 validation to resource request generation across GPU backends.

Suggested reviewers: mesutoezdil

Poem

A rabbit checks each value’s range,
Hygon and Metax keep counts unchanged.
Cores stay valid, memory stays sound,
Overflow finds no path around.
Invalid requests return empty ground.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.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 clearly describes the int32 narrowing fix in the two affected backends.
Linked Issues check ✅ Passed The changes address issue #2383 by validating hygon and metax-sgpu resource values before int32 conversion and adding regression tests.
Out of Scope Changes check ✅ Passed All implementation changes and tests directly support the overflow protection objectives in issue #2383.
✨ 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 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/metax/sdevice.go`:
- Around line 262-266: Update the memory validation in the device request
handling around MemoryFactor so the unscaled value v is checked against
int64(math.MaxInt32)/int64(MemoryFactor) before calculating mem, preventing
overflowing products from wrapping into an apparently valid int32 range.
Preserve rejection of negative or otherwise out-of-range requests, and add a
regression test covering an int64-overflowing product.
🪄 Autofix

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: 7dceaeac-6df5-427e-be54-b155a55fec7a

📥 Commits

Reviewing files that changed from the base of the PR and between e6d0902 and ee4eb56.

📒 Files selected for processing (4)
  • pkg/device/hygon/device.go
  • pkg/device/hygon/device_test.go
  • pkg/device/metax/sdevice.go
  • pkg/device/metax/sdevice_test.go

Comment thread pkg/device/metax/sdevice.go
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/device/metax/sdevice.go 76.92% 2 Missing and 1 partial ⚠️
Flag Coverage Δ
unittests 63.42% <88.00%> (+1.08%) ⬆️

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

Files with missing lines Coverage Δ
pkg/device/hygon/device.go 94.44% <100.00%> (+1.79%) ⬆️
pkg/device/metax/sdevice.go 76.76% <76.92%> (-0.05%) ⬇️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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/hygon/device_test.go (1)

619-658: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add lower-bound regression cases for both backends.

The new tables cover values above math.MaxInt32, but the implementations also reject non-positive counts and negative memory or core values.

  • pkg/device/hygon/device_test.go#L619-L658: add zero and negative hygon.com/dcunum cases and a negative hygon.com/dcumem case.
  • pkg/device/metax/sdevice_test.go#L468-L490: add zero or negative metax-tech.com/sgpu, negative metax-tech.com/vcore, and negative unitless metax-tech.com/vmemory cases.

Based on the supplied backend implementation snippets, these lower-bound branches are not exercised in the new tables.

🤖 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/hygon/device_test.go` around lines 619 - 658, The out-of-range
regression tables only cover upper bounds; add lower-bound cases for both
backends. In pkg/device/hygon/device_test.go lines 619-658, extend
Test_GenerateResourceRequests_OutOfRangeValues with zero and negative
hynog.com/dcunum values plus a negative hynog.com/dcumem case, expecting an
empty request. In pkg/device/metax/sdevice_test.go lines 468-490, add zero or
negative metax-tech.com/sgpu, negative metax-tech.com/vcore, and negative
unitless metax-tech.com/vmemory cases, likewise expecting the existing
empty-request result.
🤖 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/hygon/device_test.go`:
- Around line 619-658: The out-of-range regression tables only cover upper
bounds; add lower-bound cases for both backends. In
pkg/device/hygon/device_test.go lines 619-658, extend
Test_GenerateResourceRequests_OutOfRangeValues with zero and negative
hynog.com/dcunum values plus a negative hynog.com/dcumem case, expecting an
empty request. In pkg/device/metax/sdevice_test.go lines 468-490, add zero or
negative metax-tech.com/sgpu, negative metax-tech.com/vcore, and negative
unitless metax-tech.com/vmemory cases, likewise expecting the existing
empty-request result.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a79f8eaf-dc6f-4424-a1e3-c657e8eacf81

📥 Commits

Reviewing files that changed from the base of the PR and between ee4eb56 and 404e7f7.

📒 Files selected for processing (3)
  • pkg/device/hygon/device_test.go
  • pkg/device/metax/sdevice.go
  • pkg/device/metax/sdevice_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/device/metax/sdevice.go

Comment thread pkg/device/metax/sdevice.go Outdated
@Eshiv-Pandey
Eshiv-Pandey force-pushed the fix/int32-overflow-hygon-metax-sgpu branch from 754ae25 to 10279bf Compare August 6, 2026 10:30
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

GenerateResourceRequests in the hygon and metax-sgpu backends converts
user-controlled int64 resource limits (count, memory, cores) into int32
fields on ContainerDeviceRequest (Nums, Memreq, Coresreq) without bounds
checking. A limit exceeding math.MaxInt32 silently wraps, producing an
incorrect small or negative value that flows into Fit().

For example, hygon.com/dcumem: 16Gi yields AsInt64() = 17179869184. After
applying MemoryFactor, int32(result) wraps to 0, causing the scheduler to
treat the device as having sufficient memory when it does not.

Add range guards that return empty ContainerDeviceRequest{} (the existing
reject sentinel) before any int32() narrowing, matching the overflow
protection already in enflame, iluvatar, mthreads, and amd backends.

Add test coverage for pre-factor overflow (16Gi direct) and post-factor
overflow (3000000 * 1024 > MaxInt32) in hygon, and out-of-range memory
in metax-sgpu.

AI assistance disclosure: I used an AI tool for codebase navigation and review support.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
The int32 range check ran after `mem = v * int64(MemoryFactor)`, so the
multiplication could itself overflow int64 and wrap to a small in-range
value that slipped past the check. Validate `v` against
`math.MaxInt32 / MemoryFactor` before multiplying in the no-unit branch.

Also add regression tests for the reachable overflow guards in both
backends (device count and core out-of-range, negative core), raising
patch coverage of the new validation paths.

AI assistance disclosure: I used an AI tool for codebase navigation and review support.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
Address review feedback: drop the explanatory comments on the pre-factor
overflow guard in GenerateResourceRequests. The check is self-explanatory
and the surrounding backends keep these guards uncommented.

AI assistance disclosure: I used an AI tool for codebase navigation and review support.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
@Eshiv-Pandey
Eshiv-Pandey force-pushed the fix/int32-overflow-hygon-metax-sgpu branch from 10279bf to d987f40 Compare August 6, 2026 11:00
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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/hygon/device.go`:
- Around line 192-202: Update the DCU resource quantity conversions for memory
and core requests to check the success flag returned by AsInt64() before using
the values. Reject non-integer, decimal, and negative quantities instead of
allowing memnums or corenum to retain their defaults, applying the same
validation to the later conversion around the core handling logic. Preserve the
existing range and overflow checks after successful conversion.
🪄 Autofix

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: 33b0e4d5-2abc-41f4-a991-69716e7bcf39

📥 Commits

Reviewing files that changed from the base of the PR and between c9b9559 and d987f40.

📒 Files selected for processing (4)
  • pkg/device/hygon/device.go
  • pkg/device/hygon/device_test.go
  • pkg/device/metax/sdevice.go
  • pkg/device/metax/sdevice_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • pkg/device/metax/sdevice_test.go
  • pkg/device/hygon/device_test.go
  • pkg/device/metax/sdevice.go

Comment thread pkg/device/hygon/device.go
@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026
@mesutoezdil

Copy link
Copy Markdown
Contributor

/lgtm

@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 13, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, Eshiv-Pandey

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 13, 2026
@mesutoezdil

Copy link
Copy Markdown
Contributor

/lgtm

@hami-robot
hami-robot Bot merged commit cfd36a5 into Project-HAMi:master Aug 14, 2026
16 checks passed
hami-robot Bot pushed a commit that referenced this pull request Aug 18, 2026
* fix(ascend): guard int32 narrowing in GenerateResourceRequests

GenerateResourceRequests narrowed user-controlled int64 limits (device
count, memory, cores) into the int32 fields of ContainerDeviceRequest
without bounds checking. A value over math.MaxInt32 silently wrapped to a
small or negative number that flowed into Fit(), letting the scheduler
place pods onto overcommitted NPUs. A byte-suffixed memory quantity such
as 16Gi wrapped to 0 on the soft-partitioning path, and MemPercentagereq
then defaulted to 0 as well, so the fit check could never reject it.

Guard every narrowing point before the int32 cast, returning the existing
empty ContainerDeviceRequest{} rejection sentinel, matching the pattern
from #2388 (hygon, metax-sgpu). Memory guards cover both the pre-factor
value and the value after MemoryFactor scaling, on the soft-partitioning
and trimMemory paths alike. The memory error messages state that Ascend
memory is treated as MB not Byte, since a request like 16Gi is almost
always a wrong-unit mistake.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>

* fix(ascend): reject negative memory quantities before AsInt64

A negative fractional quantity such as -1m returns ok=false from
AsInt64, so it bypassed the range guard and silently defaulted to a
100% memory request. Check mem.Sign() before AsInt64 to reject all
negative quantities, and drop the now-redundant memnums < 0 branch.
Add a -1m regression case and shorten the explanatory comments.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>

* test(ascend): shorten verbose comments in device_test.go

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>

* fix(ascend): add regression test for negative whole memory request

The -1m test only exercised the fractional negative path where AsInt64
returns ok=false. A plain whole -100 passes AsInt64 (ok=true), so the
old int32 overflow guard alone would never catch it. This pins the
Sign() < 0 check to the whole-number case, directly covering the case
the reviewer asked about.

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>

---------

Signed-off-by: Eshiv Pandey <eshivpandey18@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: int32 overflow in GenerateResourceRequests silently drops memory request (hygon, metax-sgpu)

3 participants