Skip to content

fix(enflame): bound profile values before int32 conversion in Fit - #2190

Merged
hami-robot[bot] merged 2 commits into
Project-HAMi:masterfrom
mesutoezdil:fix/enflame-int32-bounds
Jul 31, 2026
Merged

fix(enflame): bound profile values before int32 conversion in Fit#2190
hami-robot[bot] merged 2 commits into
Project-HAMi:masterfrom
mesutoezdil:fix/enflame-int32-bounds

Conversation

@mesutoezdil

@mesutoezdil mesutoezdil commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

/kind bug
Fit in pkg/device/enflame/device.go converts three profile fields to int32 without an upper bound. All three trace back to strconv.Atoi in parseProfile, so a profile name such as 4294967299g.4294967299gb wraps to 3 slices and 3072 MiB and the profile is accepted. The bogus drsSlice also reaches the container annotation. This is CodeQL alert go/incorrect-integer-conversion at pkg/device/enflame/device.go:394. #2145 added clampToInt32 and applied it elsewhere in the file but left these three sites.

@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: yes labels Jul 29, 2026
@hami-robot
hami-robot Bot requested review from FouoF and lengrongfu July 29, 2026 10:03
@hami-robot hami-robot Bot added the size/M label Jul 29, 2026
@coderabbitai

coderabbitai Bot commented Jul 29, 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

Enflame profile sizing now validates int32-safe bounds before conversion. Regression coverage verifies that oversized slice and memory values, plus boundary-sized profiles, are rejected without selecting devices.

Changes

Enflame profile validation

Layer / File(s) Summary
Bounded profile sizing and regression coverage
pkg/device/enflame/device.go, pkg/device/enflame/device_test.go
Fit rejects out-of-range slice and memory values before conversion, rejects oversized core values, removes the non-positive core clamp, and verifies invalid profiles produce no selected devices.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: dongjiang1989

Poem

I’m a bunny guarding slices tonight,
No giant numbers escape my sight.
Memory stays within its measured row,
Invalid profiles fade away,
And no device joins the array!

🚥 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
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: bounding Enflame profile values before int32 conversion in Fit.
✨ 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: 2

🤖 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/enflame/device_test.go`:
- Around line 442-447: Update the regression test around Fit to use a safe
Memreq value and a MemPercentagereq that selects the oversized profile through
the intended selection path, allowing execution to reach the overflow checks in
selectProfileByRequest. Add assertions verifying the slice-specific overflow
behavior rather than only invoking Fit and ignoring its results.

In `@pkg/device/enflame/device.go`:
- Around line 394-406: Update the profile-size handling around requiredSlice and
the drsSlice construction to consistently enforce the supported slice bound: use
the bounded requiredSlice value when populating drsSlice, or reject out-of-range
profile.Size values before allocation. Ensure oversized positive values cannot
pass validation or be emitted through the original profile.Size.
🪄 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: edb6e2e3-6ba0-4b37-b73c-35edd0838088

📥 Commits

Reviewing files that changed from the base of the PR and between c8fdb12 and 7372890.

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

Comment thread pkg/device/enflame/device_test.go
Comment thread pkg/device/enflame/device.go Outdated
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 62.93% <100.00%> (+0.10%) ⬆️

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

Files with missing lines Coverage Δ
pkg/device/enflame/device.go 61.73% <100.00%> (+2.32%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

profile.Size, MemoryGB and CorePercent come from strconv.Atoi on a
profile name, so a name like 4294967299g.4294967299gb wraps to 3 slices
and 3072 MiB and Fit accepts it. Reject a slice count or memory size
that would overflow, and clamp the core percent on the int value rather
than after the conversion, so every int32 cast in Fit has both bounds
established beforehand.

Signed-off-by: mesutoezdil <mesudozdil@gmail.com>

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/device/enflame/device.go (1)

407-415: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Guard the derived core percentage before narrowing to int32.

collectDRSProfiles derives CorePercent with int(math.Ceil(float64(size)*100/float64(maxSlice))). Go makes out-of-range float-to-integer conversion implementation-dependent, so a calculated ratio larger than the platform int range may bypass this MaxInt32 check and then become invalid when assigned to Usedcores. Reject the profile before conversion when the double-valued ratio is outside the intended int32 range, e.g. using float64(maxSlice) in an MaxInt32/0 bound check, and keep this 0/MaxInt32 guard here.

🤖 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/enflame/device.go` around lines 407 - 415, Guard the derived
CorePercent in collectDRSProfiles before converting the calculated ratio to int,
rejecting ratios at or below 0 or above math.MaxInt32 using the float64
size/maxSlice calculation. Preserve the existing profile.CorePercent MaxInt32/0
validation in the surrounding device logic, then safely narrow
profile.CorePercent to int32 for Usedcores.

Source: Linters/SAST tools

♻️ Duplicate comments (1)
pkg/device/enflame/device_test.go (1)

446-469: 🎯 Functional Correctness | 🟠 Major

Make the regression cases reach the overflow checks.

MemPercentagereq: 101 selects direct mode, where selectProfileByRequest only accepts a profile whose size equals request.Memreq (3). None of these profiles matches, so every call returns before the new validation in Fit; the assertions currently pass without testing slice, memory, or core overflow handling.

Use a non-direct request mode, such as MemPercentagereq: 100, while keeping Memreq: 3.

🤖 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/enflame/device_test.go` around lines 446 - 469, Update the
regression requests in the affected Fit test cases to use non-direct mode by
changing MemPercentagereq from 101 to 100 while preserving Memreq as 3. Keep the
existing overflow-specific profiles and assertions unchanged so each case
reaches Fit’s slice, memory, and core overflow validation.
🤖 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.

Outside diff comments:
In `@pkg/device/enflame/device.go`:
- Around line 407-415: Guard the derived CorePercent in collectDRSProfiles
before converting the calculated ratio to int, rejecting ratios at or below 0 or
above math.MaxInt32 using the float64 size/maxSlice calculation. Preserve the
existing profile.CorePercent MaxInt32/0 validation in the surrounding device
logic, then safely narrow profile.CorePercent to int32 for Usedcores.

---

Duplicate comments:
In `@pkg/device/enflame/device_test.go`:
- Around line 446-469: Update the regression requests in the affected Fit test
cases to use non-direct mode by changing MemPercentagereq from 101 to 100 while
preserving Memreq as 3. Keep the existing overflow-specific profiles and
assertions unchanged so each case reaches Fit’s slice, memory, and core overflow
validation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8afc6d34-1c7d-4969-9aee-e29db32739a8

📥 Commits

Reviewing files that changed from the base of the PR and between 4dd0511 and ba64855.

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

Comment thread pkg/device/enflame/device.go Outdated
The existing case tripped the slice bound first, so the memory and core
percent rejections were never exercised. Add a profile that clears the
slice bound but overflows on memory, and one that pairs a maxSlice of 1
with a max int32 slice count to push the derived core percent past
int32.

Drop the non-positive core percent clamp with it: collectDRSProfiles
skips non-positive sizes and derives CorePercent as
ceil(size*100/maxSlice) with maxSlice >= 1, so the value is always at
least 1 by the time Fit sees it.

Signed-off-by: mesutoezdil <mesudozdil@gmail.com>
@FouoF

FouoF commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

/lgtm

@hami-robot hami-robot Bot added the lgtm label Jul 31, 2026
@hami-robot

hami-robot Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FouoF, mesutoezdil

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 Jul 31, 2026
@hami-robot
hami-robot Bot merged commit 18ae984 into Project-HAMi:master Jul 31, 2026
18 checks passed
hami-robot Bot pushed a commit that referenced this pull request Aug 20, 2026
… mthreads (#2285)

* fix(device): reject int32-overflowing memory requests in iluvatar and 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
(#2134) and the enflame int32 bounding in #2145 / #2190. Adds a regression
case to each backend's Test_GenerateResourceRequests.

Fixes #2284

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

* fix(device): validate memory request before the multiply

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 (#2145). Addresses CodeRabbit
review feedback; behavior and tests unchanged.

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

* fix(device): extend memory overflow guard to cambricon

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 #2278.

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

* test(device): drop explanatory comments from the overflow test cases

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>

* test(nvidia): check 1Gi is rejected when MemoryFactor doubles it

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

---------

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
Co-authored-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.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.

3 participants