]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/cgo/internal/test/issue1435.go
misc/cgo: move registerCgoTests tests to cmd/cgo/internal
[gostls13.git] / src / cmd / cgo / internal / test / issue1435.go
1 // Copyright 2019 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
5 //go:build linux && cgo
6
7 package cgotest
8
9 import (
10         "fmt"
11         "os"
12         "runtime"
13         "sort"
14         "strings"
15         "syscall"
16         "testing"
17 )
18
19 // #include <stdio.h>
20 // #include <stdlib.h>
21 // #include <pthread.h>
22 // #include <unistd.h>
23 // #include <sys/types.h>
24 //
25 // pthread_t *t = NULL;
26 // pthread_mutex_t mu;
27 // int nts = 0;
28 // int all_done = 0;
29 //
30 // static void *aFn(void *vargp) {
31 //   int done = 0;
32 //   while (!done) {
33 //     usleep(100);
34 //     pthread_mutex_lock(&mu);
35 //     done = all_done;
36 //     pthread_mutex_unlock(&mu);
37 //   }
38 //   return NULL;
39 // }
40 //
41 // void trial(int argc) {
42 //   int i;
43 //   nts = argc;
44 //   t = calloc(nts, sizeof(pthread_t));
45 //   pthread_mutex_init(&mu, NULL);
46 //   for (i = 0; i < nts; i++) {
47 //     pthread_create(&t[i], NULL, aFn, NULL);
48 //   }
49 // }
50 //
51 // void cleanup(void) {
52 //   int i;
53 //   pthread_mutex_lock(&mu);
54 //   all_done = 1;
55 //   pthread_mutex_unlock(&mu);
56 //   for (i = 0; i < nts; i++) {
57 //     pthread_join(t[i], NULL);
58 //   }
59 //   pthread_mutex_destroy(&mu);
60 //   free(t);
61 // }
62 import "C"
63
64 // compareStatus is used to confirm the contents of the thread
65 // specific status files match expectations.
66 func compareStatus(filter, expect string) error {
67         expected := filter + expect
68         pid := syscall.Getpid()
69         fs, err := os.ReadDir(fmt.Sprintf("/proc/%d/task", pid))
70         if err != nil {
71                 return fmt.Errorf("unable to find %d tasks: %v", pid, err)
72         }
73         expectedProc := fmt.Sprintf("Pid:\t%d", pid)
74         foundAThread := false
75         for _, f := range fs {
76                 tf := fmt.Sprintf("/proc/%s/status", f.Name())
77                 d, err := os.ReadFile(tf)
78                 if err != nil {
79                         // There are a surprising number of ways this
80                         // can error out on linux.  We've seen all of
81                         // the following, so treat any error here as
82                         // equivalent to the "process is gone":
83                         //    os.IsNotExist(err),
84                         //    "... : no such process",
85                         //    "... : bad file descriptor.
86                         continue
87                 }
88                 lines := strings.Split(string(d), "\n")
89                 for _, line := range lines {
90                         // Different kernel vintages pad differently.
91                         line = strings.TrimSpace(line)
92                         if strings.HasPrefix(line, "Pid:\t") {
93                                 // On loaded systems, it is possible
94                                 // for a TID to be reused really
95                                 // quickly. As such, we need to
96                                 // validate that the thread status
97                                 // info we just read is a task of the
98                                 // same process PID as we are
99                                 // currently running, and not a
100                                 // recently terminated thread
101                                 // resurfaced in a different process.
102                                 if line != expectedProc {
103                                         break
104                                 }
105                                 // Fall through in the unlikely case
106                                 // that filter at some point is
107                                 // "Pid:\t".
108                         }
109                         if strings.HasPrefix(line, filter) {
110                                 if line == expected {
111                                         foundAThread = true
112                                         break
113                                 }
114                                 if filter == "Groups:" && strings.HasPrefix(line, "Groups:\t") {
115                                         // https://github.com/golang/go/issues/46145
116                                         // Containers don't reliably output this line in sorted order so manually sort and compare that.
117                                         a := strings.Split(line[8:], " ")
118                                         sort.Strings(a)
119                                         got := strings.Join(a, " ")
120                                         if got == expected[8:] {
121                                                 foundAThread = true
122                                                 break
123                                         }
124
125                                 }
126                                 return fmt.Errorf("%q got:%q want:%q (bad) [pid=%d file:'%s' %v]\n", tf, line, expected, pid, string(d), expectedProc)
127                         }
128                 }
129         }
130         if !foundAThread {
131                 return fmt.Errorf("found no thread /proc/<TID>/status files for process %q", expectedProc)
132         }
133         return nil
134 }
135
136 // test1435 test 9 glibc implemented setuid/gid syscall functions are
137 // mapped.  This test is a slightly more expansive test than that of
138 // src/syscall/syscall_linux_test.go:TestSetuidEtc() insofar as it
139 // launches concurrent threads from C code via CGo and validates that
140 // they are subject to the system calls being tested. For the actual
141 // Go functionality being tested here, the syscall_linux_test version
142 // is considered authoritative, but non-trivial improvements to that
143 // should be mirrored here.
144 func test1435(t *testing.T) {
145         if syscall.Getuid() != 0 {
146                 t.Skip("skipping root only test")
147         }
148         if runtime.GOOS == "linux" {
149                 if _, err := os.Stat("/etc/alpine-release"); err == nil {
150                         t.Skip("skipping failing test on alpine - go.dev/issue/19938")
151                 }
152         }
153
154         // Launch some threads in C.
155         const cts = 5
156         C.trial(cts)
157         defer C.cleanup()
158
159         vs := []struct {
160                 call           string
161                 fn             func() error
162                 filter, expect string
163         }{
164                 {call: "Setegid(1)", fn: func() error { return syscall.Setegid(1) }, filter: "Gid:", expect: "\t0\t1\t0\t1"},
165                 {call: "Setegid(0)", fn: func() error { return syscall.Setegid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
166
167                 {call: "Seteuid(1)", fn: func() error { return syscall.Seteuid(1) }, filter: "Uid:", expect: "\t0\t1\t0\t1"},
168                 {call: "Setuid(0)", fn: func() error { return syscall.Setuid(0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"},
169
170                 {call: "Setgid(1)", fn: func() error { return syscall.Setgid(1) }, filter: "Gid:", expect: "\t1\t1\t1\t1"},
171                 {call: "Setgid(0)", fn: func() error { return syscall.Setgid(0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
172
173                 {call: "Setgroups([]int{0,1,2,3})", fn: func() error { return syscall.Setgroups([]int{0, 1, 2, 3}) }, filter: "Groups:", expect: "\t0 1 2 3"},
174                 {call: "Setgroups(nil)", fn: func() error { return syscall.Setgroups(nil) }, filter: "Groups:", expect: ""},
175                 {call: "Setgroups([]int{0})", fn: func() error { return syscall.Setgroups([]int{0}) }, filter: "Groups:", expect: "\t0"},
176
177                 {call: "Setregid(101,0)", fn: func() error { return syscall.Setregid(101, 0) }, filter: "Gid:", expect: "\t101\t0\t0\t0"},
178                 {call: "Setregid(0,102)", fn: func() error { return syscall.Setregid(0, 102) }, filter: "Gid:", expect: "\t0\t102\t102\t102"},
179                 {call: "Setregid(0,0)", fn: func() error { return syscall.Setregid(0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
180
181                 {call: "Setreuid(1,0)", fn: func() error { return syscall.Setreuid(1, 0) }, filter: "Uid:", expect: "\t1\t0\t0\t0"},
182                 {call: "Setreuid(0,2)", fn: func() error { return syscall.Setreuid(0, 2) }, filter: "Uid:", expect: "\t0\t2\t2\t2"},
183                 {call: "Setreuid(0,0)", fn: func() error { return syscall.Setreuid(0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"},
184
185                 {call: "Setresgid(101,0,102)", fn: func() error { return syscall.Setresgid(101, 0, 102) }, filter: "Gid:", expect: "\t101\t0\t102\t0"},
186                 {call: "Setresgid(0,102,101)", fn: func() error { return syscall.Setresgid(0, 102, 101) }, filter: "Gid:", expect: "\t0\t102\t101\t102"},
187                 {call: "Setresgid(0,0,0)", fn: func() error { return syscall.Setresgid(0, 0, 0) }, filter: "Gid:", expect: "\t0\t0\t0\t0"},
188
189                 {call: "Setresuid(1,0,2)", fn: func() error { return syscall.Setresuid(1, 0, 2) }, filter: "Uid:", expect: "\t1\t0\t2\t0"},
190                 {call: "Setresuid(0,2,1)", fn: func() error { return syscall.Setresuid(0, 2, 1) }, filter: "Uid:", expect: "\t0\t2\t1\t2"},
191                 {call: "Setresuid(0,0,0)", fn: func() error { return syscall.Setresuid(0, 0, 0) }, filter: "Uid:", expect: "\t0\t0\t0\t0"},
192         }
193
194         for i, v := range vs {
195                 if err := v.fn(); err != nil {
196                         t.Errorf("[%d] %q failed: %v", i, v.call, err)
197                         continue
198                 }
199                 if err := compareStatus(v.filter, v.expect); err != nil {
200                         t.Errorf("[%d] %q comparison: %v", i, v.call, err)
201                 }
202         }
203 }