github.com/cilium/ebpf@v0.10.0/internal/sys/signals_test.go (about)

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