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