Skip to content

Commit bbabfb4

Browse files
committed
Add a test case for a deadlock.
The CGO_ENABLED=0 failure mode is discussed in: golang/go#50113 At the present time, this only passes when the psx package is compiled CGO_ENABLED=1. The problem being that a blocking read cannot be interrupted by the CGO_ENABLED=0 build of package "psx". It does not deadlock when compiled CGO_ENABLED=1 because the psx signal wakes the reading thread up back into user space. Signed-off-by: Andrew G. Morgan <[email protected]>
1 parent f25a1b7 commit bbabfb4

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

go/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ good-names.go
22
compare-cap
33
try-launching
44
try-launching-cgo
5+
psx-fd
6+
psx-fd-cgo
57
psx-signals
68
psx-signals-cgo
79
b210613

go/Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ ifeq ($(CGO_REQUIRED),0)
8383
CC="$(CC)" CGO_ENABLED="1" $(CGO_LDFLAGS_ALLOW) $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@-cgo $<
8484
endif
8585

86+
# This is a test case developed from the deadlock investigation,
87+
# https://github.com/golang/go/issues/50113 . Note the psx-fd.go code
88+
# works when compiled CGO_ENABLED=1, but deadlocks when compiled
89+
# CGO_ENABLED=0. At the time of writing, this is true for go1.16+.
90+
psx-fd: psx-fd.go PSXGOPACKAGE
91+
CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(CGO_LDFLAGS_ALLOW) $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
92+
93+
ifeq ($(CGO_REQUIRED),0)
94+
psx-fd-cgo: psx-fd.go PSXGOPACKAGE
95+
CC="$(CC)" CGO_ENABLED="1" $(CGO_LDFLAGS_ALLOW) $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
96+
endif
97+
8698
psx-signals: psx-signals.go PSXGOPACKAGE
8799
CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(CGO_LDFLAGS_ALLOW) CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
88100

@@ -110,16 +122,18 @@ mismatch-cgo: mismatch.go CAPGOPACKAGE
110122
CC="$(CC)" CGO_ENABLED="1" $(CGO_LDFLAGS_ALLOW) CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
111123
endif
112124

113-
test: setid gowns captree $(TESTS)
125+
test: setid gowns captree psx-fd $(TESTS)
114126
CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(CGO_LDFLAGS_ALLOW) $(GO) test -mod=vendor $(IMPORTDIR)/psx
115127
CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(CGO_LDFLAGS_ALLOW) $(GO) test -mod=vendor $(IMPORTDIR)/cap
116128
LD_LIBRARY_PATH=../libcap ./compare-cap
117129
./psx-signals
118130
./mismatch || exit 0 ; exit 1
131+
timeout 5 ./psx-fd || echo "this is a known Go bug"
119132
ifeq ($(CGO_REQUIRED),0)
120-
$(MAKE) psx-signals-cgo mismatch-cgo
133+
$(MAKE) psx-signals-cgo mismatch-cgo psx-fd-cgo
121134
./psx-signals-cgo
122135
./mismatch-cgo || exit 0 ; exit 1
136+
./psx-fd-cgo
123137
endif
124138
./setid --caps=false
125139
./gowns -- -c "echo gowns runs"
@@ -167,5 +181,5 @@ clean:
167181
rm -f compare-cap try-launching try-launching-cgo
168182
rm -f $(topdir)/cap/*~ $(topdir)/psx/*~
169183
rm -f b210613 b215283 b215283-cgo psx-signals psx-signals-cgo
170-
rm -f mismatch mismatch-cgo
184+
rm -f mismatch mismatch-cgo psx-fd psx-fd-cgo
171185
rm -fr vendor CAPGOPACKAGE PSXGOPACKAGE go.sum

go/psx-fd.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"syscall"
7+
"time"
8+
9+
"kernel.org/pub/linux/libs/security/libcap/psx"
10+
)
11+
12+
const prSetKeepCaps = 8
13+
14+
func main() {
15+
r, w, err := os.Pipe()
16+
if err != nil {
17+
log.Fatalf("failed to obtain pipe: %v", err)
18+
}
19+
data := make([]byte, 2+r.Fd())
20+
go r.Read(data)
21+
time.Sleep(500 * time.Millisecond)
22+
psx.Syscall3(syscall.SYS_PRCTL, prSetKeepCaps, 1, 0)
23+
w.Close()
24+
r.Close()
25+
}

0 commit comments

Comments
 (0)