Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alignchecker: refactor into single gotest #1588

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/bin
lib/*
/tetragon
/tetragon-alignchecker
/alignchecker
/tetragon-bench
/tetragon-operator
/tetra
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ endif
GO_BUILD = CGO_ENABLED=0 GOARCH=$(GOARCH) $(GO) build $(GO_BUILD_FLAGS)

.PHONY: all
all: tetragon-bpf tetragon tetra tetragon-alignchecker test-compile tester-progs protoc-gen-go-tetragon tetragon-bench
all: tetragon-bpf tetragon tetra test-compile tester-progs protoc-gen-go-tetragon tetragon-bench

-include Makefile.docker
-include Makefile.cli
Expand Down Expand Up @@ -160,7 +160,7 @@ tetragon-bpf-container:
verify: tetragon-bpf
sudo contrib/verify/verify.sh bpf/objs

.PHONY: tetragon tetra tetragon-operator tetragon-alignchecker tetragon-bench
.PHONY: tetragon tetra tetragon-operator tetragon-bench
tetragon:
$(GO_BUILD) ./cmd/tetragon/

Expand All @@ -173,8 +173,9 @@ tetragon-bench:
tetragon-operator:
$(GO_BUILD) -o $@ ./operator

tetragon-alignchecker:
$(GO_BUILD) -o $@ ./tools/alignchecker/
.PHONY: alignchecker
alignchecker:
$(GO) test -c ./pkg/alignchecker -o alignchecker

.PHONY: ksyms
ksyms:
Expand Down Expand Up @@ -206,7 +207,7 @@ vendor:
.PHONY: clean
clean: cli-clean tarball-clean
$(MAKE) -C ./bpf clean
rm -f go-tests/*.test ./ksyms ./tetragon ./tetragon-operator ./tetra ./tetragon-alignchecker
rm -f go-tests/*.test ./ksyms ./tetragon ./tetragon-operator ./tetra ./alignchecker
rm -f contrib/sigkill-tester/sigkill-tester contrib/namespace-tester/test_ns contrib/capabilities-tester/test_caps
$(MAKE) -C $(TESTER_PROGS_DIR) clean

Expand Down
27 changes: 14 additions & 13 deletions bpf/alignchecker/bpf_alignchecker.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
#include "process/retprobe_map.h"
#include "process/types/basic.h"

struct msg_generic_kprobe _1;
struct msg_execve_event _2;
struct msg_exit _3;
struct msg_test _4;
struct msg_cgroup_event _5;
struct msg_cred _6;
struct msg_cred_minimal _7;
// event messages
struct msg_generic_kprobe _msg_generic_kprobe;
struct msg_execve_event _msg_execve_event;
struct msg_exit _msg_exit;
struct msg_test _msg_test;
struct msg_cgroup_event _msg_cgroup_event;
struct msg_cred _msg_cred;
struct msg_cred_minimal _msg_cred_minimal;

// from maps
struct event _8;
struct msg_execve_key _9;
struct execve_map_value _10;
struct event_config _11;
struct tetragon_conf _12;
struct cgroup_tracking_value _13;
struct event _event;
struct msg_execve_key _msg_execve_key;
struct execve_map_value _execve_map_value;
struct event_config _event_config;
struct tetragon_conf _tetragon_conf;
struct cgroup_tracking_value _cgroup_tracking_value;
49 changes: 17 additions & 32 deletions pkg/alignchecker/alignchecker.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package alignchecker

import (
"github.com/cilium/tetragon/pkg/api/confapi"
"github.com/cilium/cilium/pkg/alignchecker"

"github.com/cilium/tetragon/pkg/api/processapi"
"github.com/cilium/tetragon/pkg/api/testapi"
"github.com/cilium/tetragon/pkg/api/tracingapi"
"github.com/cilium/tetragon/pkg/sensors/cgroup/cgrouptrackmap"
"github.com/cilium/tetragon/pkg/sensors/config/confmap"
"github.com/cilium/tetragon/pkg/sensors/exec/execvemap"

check "github.com/cilium/cilium/pkg/alignchecker"
)

// CheckStructAlignments checks whether size and offsets of the C and Go
Expand All @@ -24,39 +24,24 @@ import (
// `align:"field_name_in_c_struct". In the case of unnamed union field, such
// union fields can be referred with special tags - `align:"$union0"`,
// `align:"$union1"`, etc.
func CheckStructAlignments(path string) error {
// Validate alignments of C and Go equivalent structs
toCheck := map[string][]any{
func CheckStructAlignments(pathToObj string) error {
alignments := map[string][]any{
// from perf_event_output
"msg_exit": {processapi.MsgExitEvent{}},
"msg_test": {testapi.MsgTestEvent{}},
"msg_execve_key": {processapi.MsgExecveKey{}},
"execve_map_value": {execvemap.ExecveValue{}},
"event_config": {tracingapi.EventConfig{}},
"tetragon_conf": {confapi.TetragonConf{}},
"cgroup_tracking_value": {processapi.MsgCgroupData{}},
"msg_cgroup_event": {processapi.MsgCgroupEvent{}},
"msg_cred": {tracingapi.MsgGenericCred{}},
"msg_cred_minimal": {processapi.MsgGenericCredMinimal{}},
}

confMap := map[string][]any{
"msg_exit": {processapi.MsgExitEvent{}},
"msg_test": {testapi.MsgTestEvent{}},
"msg_execve_key": {processapi.MsgExecveKey{}},
"execve_map_value": {execvemap.ExecveValue{}},
"msg_cgroup_event": {processapi.MsgCgroupEvent{}},
"msg_cred": {tracingapi.MsgGenericCred{}},
"msg_cred_minimal": {processapi.MsgGenericCredMinimal{}},

// configuration
"event_config": {tracingapi.EventConfig{}},
"tetragon_conf": {confmap.TetragonConfValue{}},
}

cgrpmap := map[string][]any{
// cgroup
"cgroup_tracking_value": {cgrouptrackmap.CgrpTrackingValue{}},
}

err := check.CheckStructAlignments(path, toCheck, true)
if err != nil {
return err
}

err = check.CheckStructAlignments(path, confMap, true)
if err != nil {
return err
}

return check.CheckStructAlignments(path, cgrpmap, true)
return alignchecker.CheckStructAlignments(pathToObj, alignments, true)
}
12 changes: 5 additions & 7 deletions pkg/alignchecker/alignchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

tus "github.com/cilium/tetragon/pkg/testutils/sensors"
"github.com/stretchr/testify/assert"
)

var tetragonLib string
Expand All @@ -22,12 +23,9 @@ func init() {
}
}

func TestStructAlignments(t *testing.T) {
func Test_Alignments(t *testing.T) {
bpfObjPath := filepath.Join(tetragonLib, "bpf_alignchecker.o")
if _, err := os.Stat(bpfObjPath); err != nil {
t.Fatalf("Cannot check alignment against %s: %s\n", bpfObjPath, err)
}
if err := CheckStructAlignments(bpfObjPath); err != nil {
t.Fatalf("C and Go structs alignment check failed: %s\n", err)
}

err := CheckStructAlignments(bpfObjPath)
assert.NoError(t, err, "structs must align")
}
32 changes: 0 additions & 32 deletions tools/alignchecker/main.go

This file was deleted.