Skip to content

Commit

Permalink
btf/validation: add simple enum handling
Browse files Browse the repository at this point in the history
Add enum btf handling by translating it to tthe corresponding int type.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed May 22, 2024
1 parent 9a943a6 commit e7c9ec3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/btf/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ func getKernelType(arg btf.Type) string {
return "union " + union.Name + suffix
}

enum, ok := arg.(*btf.Enum)
if ok {
prefix := "u"
if enum.Signed {
prefix = "s"
}
switch enum.Size {
case 1:
case 2:
case 4:
case 8:
default:
// Not sure what to do here, so just dump the type name
return arg.TypeName() + suffix
}
return fmt.Sprintf("%s%d%s", prefix, 8*enum.Size, suffix)
}

cnst, ok := arg.(*btf.Const)
if ok {
// NB: ignore const
Expand Down
14 changes: 14 additions & 0 deletions pkg/btf/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cilium/ebpf/btf"
"github.com/cilium/tetragon/pkg/tracingpolicy"
"github.com/stretchr/testify/require"
)

func expectError(t *testing.T, err error) error {
Expand Down Expand Up @@ -79,3 +80,16 @@ func TestSpecs(t *testing.T) {
})
}
}

func TestEnum(t *testing.T) {
require.Equal(t,
getKernelType(&btf.Enum{
Size: 2,
Signed: false,
}), "u16")
require.Equal(t,
getKernelType(&btf.Enum{
Size: 4,
Signed: true,
}), "s32")
}

0 comments on commit e7c9ec3

Please sign in to comment.