Skip to content

Commit

Permalink
test: add test for handling invalid sizes
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Jan 5, 2022
1 parent 226ae2e commit 18d59b1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func SchedGetAttr(pid int) (SchedAttr, error) {

func SchedSetAttr(pid int, attr SchedAttr, flags SchedFlag) error {
attr.Size = uint32(unsafe.Sizeof(attr))
_, _, e1 := unix.Syscall(unix.SYS_SCHED_SETATTR, uintptr(pid), uintptr(unsafe.Pointer(&attr)), uintptr(flags))
return schedSetAttr(pid, unsafe.Pointer(&attr), flags)
}

func schedSetAttr(pid int, attr unsafe.Pointer, flags SchedFlag) error {
_, _, e1 := unix.Syscall(unix.SYS_SCHED_SETATTR, uintptr(pid), uintptr(attr), uintptr(flags))
if e1 != 0 {
return syscall.Errno(e1)
}
Expand Down
38 changes: 38 additions & 0 deletions test/sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"syscall"
"testing"
"unsafe"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -150,3 +151,40 @@ func TestSchedAttr(t *testing.T) {
require.Equal(t, 60, int(attr.SchedPriority))
}
}

func TestSchedAttrSize(t *testing.T) {
pid := os.Getpid()

dt := make([]byte, 2)
ptr := unsafe.Pointer(&dt[0])
err := schedSetAttr(pid, ptr, 0)
require.Error(t, err)
t.Logf("short read error: %v", err) // this error does not look consistent even with no emulation

dt = make([]byte, 80)
ptr = unsafe.Pointer(&dt[0])

err = schedSetAttr(pid, ptr, 0)
require.NoError(t, err) // empty size is ok for some reason

ints := (*[20]uint32)(ptr)
ints[0] = 8 // too small size

err = schedSetAttr(pid, ptr, 0)
require.Error(t, err)
require.True(t, errors.Is(err, syscall.E2BIG))
require.Equal(t, uint32(0x38), ints[0]) // expecting kernel 5.3+

ints[0] = 80 // too big but empty contents is ok
err = schedSetAttr(pid, ptr, 0)
require.NoError(t, err)

ints[18] = 0xff // too big and not empty

err = schedSetAttr(pid, ptr, 0)
require.Error(t, err)
require.True(t, errors.Is(err, syscall.E2BIG))
require.Equal(t, uint32(0x38), ints[0]) // expecting kernel 5.3+

runtime.KeepAlive(dt)
}

0 comments on commit 18d59b1

Please sign in to comment.