Debian Codegraph @

internal/sys/signals_test.go

raw symbols SCIP
1 //go:build linux
2
3 package sys
4
5 import (
6 "runtime"
7 "testing"
8 "unsafe"
9
10 "github.com/go-quicktest/qt"
11
12 "github.com/cilium/ebpf/internal/unix"
13 )
14
15 func TestSigset(t *testing.T) {
16 const maxSignal = unix.Signal(unsafe.Sizeof(unix.Sigset_t{}) * 8)
17
18 // Type-infer a sigset word. This is a typed uint of 32 or 64 bits depending
19 // on the target architecture, so we can't use an untyped uint.
20 zero := unix.Sigset_t{}.Val[0]
21 words := len(unix.Sigset_t{}.Val)
22
23 var want, got unix.Sigset_t
24 // Flip the first bit of the first word.
25 if err := sigsetAdd(&got, 1); err != nil {
26 t.Fatal(err)
27 }
28 want.Val[0] = 1
29 if want != got {
30 t.Fatalf("expected first word to be 0x%x, got: 0x%x", want, got)
31 }
32
33 // And the last bit of the last word.
34 if err := sigsetAdd(&got, maxSignal); err != nil {
35 t.Fatal(err)
36 }
37 want.Val[words-1] = ^(^zero >> 1)
38 if want != got {
39 t.Fatalf("expected last word to be 0x%x, got: 0x%x", want, got)
40 }
41
42 if err := sigsetAdd(&got, maxSignal+1); err == nil {
43 t.Fatal("expected out-of-bounds add to be rejected")
44 }
45 if err := sigsetAdd(&got, -1); err == nil {
46 t.Fatal("expected negative signal to be rejected")
47 }
48 }
49
50 func TestProfilerSignal(t *testing.T) {
51 // Additional goroutine lock to make the PthreadSigmask below execute on the
52 // same OS thread as the functions under test. UnlockOSThread needs to be
53 // called as many times as LockOSThread to unlock the goroutine.
54 runtime.LockOSThread()
55 defer runtime.UnlockOSThread()
56
57 var old unix.Sigset_t
58 if err := unix.PthreadSigmask(0, nil, &old); err != nil {
59 t.Fatal("get sigmask:", err)
60 }
61
62 maskProfilerSignal()
63
64 var have unix.Sigset_t
65 if err := unix.PthreadSigmask(0, nil, &have); err != nil {
66 t.Fatal("get sigmask:", err)
67 }
68
69 want := have
70 qt.Assert(t, qt.IsNil(sigsetAdd(&want, unix.SIGPROF)))
71 qt.Assert(t, qt.Equals(have, want))
72
73 unmaskProfilerSignal()
74
75 if err := unix.PthreadSigmask(0, nil, &have); err != nil {
76 t.Fatal("get sigmask:", err)
77 }
78
79 qt.Assert(t, qt.Equals(have, old))
80 }