From e7c9ec3533cd8faf2ce1b2c38aaabd70402ad930 Mon Sep 17 00:00:00 2001 From: Kornilios Kourtis Date: Fri, 17 May 2024 15:31:45 +0200 Subject: [PATCH] btf/validation: add simple enum handling Add enum btf handling by translating it to tthe corresponding int type. Signed-off-by: Kornilios Kourtis --- pkg/btf/validation.go | 18 ++++++++++++++++++ pkg/btf/validation_test.go | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pkg/btf/validation.go b/pkg/btf/validation.go index d53b4577b98..cb9aeb06d38 100644 --- a/pkg/btf/validation.go +++ b/pkg/btf/validation.go @@ -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 diff --git a/pkg/btf/validation_test.go b/pkg/btf/validation_test.go index fa785044665..007d4159bab 100644 --- a/pkg/btf/validation_test.go +++ b/pkg/btf/validation_test.go @@ -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 { @@ -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") +}