Skip to content
6 changes: 6 additions & 0 deletions pkg/device/iluvatar/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"flag"
"fmt"
"math"
"slices"
"strings"

Expand Down Expand Up @@ -216,6 +217,11 @@ func (dev *IluvatarDevices) GenerateResourceRequests(ctr *corev1.Container) devi
memnum = int(memnums) * 256
}
}
if memnum < 0 || memnum > math.MaxInt32 {
klog.ErrorS(nil, "iluvatar memory request exceeds int32 range; rejecting to avoid silent under-allocation",
"container", ctr.Name)
return device.ContainerDeviceRequest{}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
corenum := int32(0)
core, ok := ctr.Resources.Limits[iluvatarResourceCores]
if !ok {
Expand Down
15 changes: 15 additions & 0 deletions pkg/device/iluvatar/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ func Test_GenerateResourceRequests(t *testing.T) {
Coresreq: int32(0),
},
},
{
// A Gi-scale memory quantity, times the x256 factor, overflows
// int32. Rather than silently wrapping to Memreq: 0 (which would
// let the pod schedule onto a full device), the request is rejected.
name: "memory overflowing int32 is rejected, not truncated to zero",
args: &corev1.Container{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"iluvatar.ai/MR-V100-vgpu": resource.MustParse("1"),
"iluvatar.ai/MR-V100.vMem": resource.MustParse("16Gi"),
},
},
},
want: device.ContainerDeviceRequest{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/device/mthreads/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ func (dev *MthreadsDevices) GenerateResourceRequests(ctr *corev1.Container) devi
"allocatedMem", memnum)
}
}
if memnum < 0 || memnum > math.MaxInt32 {
klog.ErrorS(nil, "mthreads memory request exceeds int32 range; rejecting to avoid silent under-allocation",
"container", ctr.Name)
return device.ContainerDeviceRequest{}
}
corenum := int32(0)
core, ok := ctr.Resources.Limits[mthreadsResourceCores]
if !ok {
Expand Down
17 changes: 17 additions & 0 deletions pkg/device/mthreads/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,23 @@ func Test_GenerateResourceRequests(t *testing.T) {
},
want: device.ContainerDeviceRequest{},
},
{
// A Gi-scale memory quantity, times the x512 factor, overflows
// int32. Rather than silently wrapping to Memreq: 0 (which would
// let the pod schedule onto a full device), the request is rejected,
// mirroring the count-overflow handling above.
name: "memory overflowing int32 is rejected, not truncated to zero",
args: &corev1.Container{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"mthreads.com/vgpu": resource.MustParse("1"),
"mthreads.com/sgpu-memory": resource.MustParse("16Gi"),
"mthreads.com/sgpu-core": resource.MustParse("1"),
},
},
},
want: device.ContainerDeviceRequest{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
Loading