• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2023 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package unix_test
6
7import (
8	"internal/goarch"
9	"internal/syscall/unix"
10	"runtime"
11	"strings"
12	"testing"
13	"unsafe"
14)
15
16// TestSiginfoChildLayout validates SiginfoChild layout. Modelled after
17// static assertions in linux kernel's arch/*/kernel/signal*.c.
18func TestSiginfoChildLayout(t *testing.T) {
19	var si unix.SiginfoChild
20
21	const host64bit = goarch.PtrSize == 8
22
23	if v := unsafe.Sizeof(si); v != 128 {
24		t.Fatalf("sizeof: got %d, want 128", v)
25	}
26
27	ofSigno := 0
28	ofErrno := 4
29	ofCode := 8
30	if strings.HasPrefix(runtime.GOARCH, "mips") {
31		// These two fields are swapped on MIPS platforms.
32		ofErrno, ofCode = ofCode, ofErrno
33	}
34	ofPid := 12
35	if host64bit {
36		ofPid = 16
37	}
38	ofUid := ofPid + 4
39	ofStatus := ofPid + 8
40
41	offsets := []struct {
42		name string
43		got  uintptr
44		want int
45	}{
46		{"Signo", unsafe.Offsetof(si.Signo), ofSigno},
47		{"Errno", unsafe.Offsetof(si.Errno), ofErrno},
48		{"Code", unsafe.Offsetof(si.Code), ofCode},
49		{"Pid", unsafe.Offsetof(si.Pid), ofPid},
50		{"Uid", unsafe.Offsetof(si.Uid), ofUid},
51		{"Status", unsafe.Offsetof(si.Status), ofStatus},
52	}
53
54	for _, tc := range offsets {
55		if int(tc.got) != tc.want {
56			t.Errorf("offsetof %s: got %d, want %d", tc.name, tc.got, tc.want)
57		}
58	}
59}
60