Skip to content
New issue

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

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

Already on GitHub? # to your account

encoder: add compact fuzz tests and fix bugs related to unhandled event types #2819

Merged
merged 6 commits into from
Aug 16, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 13 additions & 1 deletion pkg/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ func HumanStackTrace(response *tetragon.GetEventsResponse, colorer *Colorer) str

func (p *CompactEncoder) EventToString(response *tetragon.GetEventsResponse) (string, error) {
switch response.Event.(type) {
case *tetragon.GetEventsResponse_Test:
// This shouldn't normally be reachable since Tetragon won't generate Test
// events outside of specific unit tests.
return "TEST EVENT", nil
case *tetragon.GetEventsResponse_ProcessExec:
exec := response.GetProcessExec()
if exec.Process == nil {
Expand Down Expand Up @@ -491,6 +495,14 @@ func (p *CompactEncoder) EventToString(response *tetragon.GetEventsResponse) (st
event := p.Colorer.Blue.Sprintf("⁉️ %-7s", "tracepoint")
return CapTrailorPrinter(fmt.Sprintf("%s %s %s %s", event, processInfo, tp.Subsys, tp.Event), caps), nil
}
case *tetragon.GetEventsResponse_ProcessUprobe:
uprobe := response.GetProcessUprobe()
if uprobe.Process == nil {
return "", ErrMissingProcessInfo
}
processInfo, caps := p.Colorer.ProcessInfo(response.NodeName, uprobe.Process)
event := p.Colorer.Blue.Sprintf("🕵️ %-7s", "uprobe")
return CapTrailorPrinter(fmt.Sprintf("%s %s %s %s", event, processInfo, uprobe.Path, uprobe.Symbol), caps), nil
case *tetragon.GetEventsResponse_ProcessLsm:
lsm := response.GetProcessLsm()
if lsm.Process == nil {
Expand All @@ -501,7 +513,7 @@ func (p *CompactEncoder) EventToString(response *tetragon.GetEventsResponse) (st
return CapTrailorPrinter(fmt.Sprintf("%s %s %s", event, processInfo, lsm.FunctionName), caps), nil
}

return "", ErrUnknownEventType
return "", fmt.Errorf("%w: %s", ErrUnknownEventType, response.EventType())
}

func rawSyscallEnter(tp *tetragon.ProcessTracepoint) string {
Expand Down
49 changes: 49 additions & 0 deletions pkg/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/api/v1/tetragon/codegen/helpers"
"github.com/sryoya/protorand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -535,3 +536,51 @@ func FuzzProtojsonCompatibility(f *testing.F) {
assert.True(t, proto.Equal(msg, msgProtojson))
})
}

func FuzzCompactEncoder(f *testing.F) {
for _, n := range []int64{
1337,
78776406,
56343416,
68876713,
51156281,
45544244,
4011756,
} {
for _, cm := range []uint8{0, 1, 2} {
for _, ts := range []bool{true, false} {
for _, st := range []bool{true, false} {
f.Add(n, cm, ts, st)
}
}
}
}
f.Fuzz(func(t *testing.T, seed int64, colorMode uint8, timestamps bool, stackTraces bool) {
var cm ColorMode
switch colorMode % 3 {
case 0:
cm = "never"
case 1:
cm = "always"
case 2:
cm = "auto"
default:
panic("unreachable")
}

pr := protorand.New()
pr.Seed(seed)
ev := &tetragon.GetEventsResponse{}
msg, err := pr.Gen(ev)
require.NoError(t, err)

if helpers.ResponseGetProcess(msg.(*tetragon.GetEventsResponse)) == nil {
t.Skipf("Empty process")
}

var buf1 bytes.Buffer
compactEncoder := NewCompactEncoder(&buf1, cm, timestamps, stackTraces)
err = compactEncoder.Encode(msg)
require.NoError(t, err)
})
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776374)
byte('\x02')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776519)
byte('.')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343604)
byte('\u0096')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343515)
byte('\x02')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156230)
byte('\u0086')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(45544323)
byte('L')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(45544350)
byte('\x1e')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1284)
byte('\x01')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156231)
byte('\x11')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156190)
byte('_')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(45544205)
byte('U')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(4011786)
byte('\x01')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776489)
byte('\x00')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(68876694)
byte('5')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776485)
byte('\x02')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1560)
byte('b')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(68876791)
byte('Z')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776456)
byte('\x00')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1575)
byte('b')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1420)
byte('\x02')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(45544220)
byte('\x02')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(4011791)
byte('\x01')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156231)
byte('\x01')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(68876745)
byte('\x00')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(4011706)
byte('\x00')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343414)
byte('#')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343363)
byte('V')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776601)
byte('\x00')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156303)
byte('\x00')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776406)
byte('.')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776323)
byte('\x00')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776435)
byte('\x05')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(4011620)
byte('\r')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156230)
byte('\x00')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776385)
byte('[')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1351)
byte('\x01')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(68876842)
byte('\x16')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1306)
byte('\x01')
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(78776334)
byte('\x0f')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1371)
byte('7')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(45544228)
byte('/')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(51156359)
byte('\x02')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343431)
byte('$')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343330)
byte('Y')
bool(true)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343506)
byte('\x00')
bool(false)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343604)
byte('\u0096')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1291)
byte('\x02')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(68876708)
byte('\x00')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(1275)
byte('\x00')
bool(false)
bool(false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go test fuzz v1
int64(56343514)
byte('\x00')
bool(true)
bool(true)
Loading
Loading