This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfdleak_test.go
538 lines (476 loc) · 9.71 KB
/
fdleak_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"sort"
"strconv"
"strings"
"syscall"
"testing"
)
func fcntl(fd int, cmd int, arg int) (val int, err error) {
r0, _, errno := syscall.Syscall(
syscall.SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg))
val = int(r0)
if errno != 0 {
err = errno
}
return
}
// FdLeakDetector detects fd leaks by letting its user take snapshots of the
// list of opened fds at key points in the profiled program and compare two
// snapshots.
type FdLeakDetector struct {
}
type FdInfo struct {
Fd int
Flags int
CloseOnExec bool
Text string
}
func (info *FdInfo) dump(w io.Writer) {
flags := make([]string, 5)
if info.CloseOnExec {
flags = append(flags, "cloexec")
}
// file status
if info.Flags&syscall.O_APPEND != 0 {
flags = append(flags, "append")
}
if info.Flags&syscall.O_NONBLOCK != 0 {
flags = append(flags, "nonblock")
}
// acc mode
if info.Flags&syscall.O_RDONLY != 0 {
flags = append(flags, "read-only")
}
if info.Flags&syscall.O_RDWR != 0 {
flags = append(flags, "read-write")
}
if info.Flags&syscall.O_WRONLY != 0 {
flags = append(flags, "write-only")
}
if info.Flags&syscall.O_DSYNC != 0 {
flags = append(flags, "dsync")
}
if info.Flags&syscall.O_RSYNC != 0 {
flags = append(flags, "rsync")
}
if info.Flags&syscall.O_SYNC != 0 {
flags = append(flags, "sync")
}
fmt.Fprintf(w, " %d: %s (%s)\n", info.Fd, info.Text,
strings.Join(flags, ""))
}
func (info *FdInfo) equal(other *FdInfo) bool {
return reflect.DeepEqual(info, other)
}
type FdSnapshot struct {
Fds []FdInfo
}
// ByFd implements sort.Interface for []FdInfo based on the Fd field.
type ByFd []FdInfo
func (a ByFd) Len() int { return len(a) }
func (a ByFd) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByFd) Less(i, j int) bool { return a[i].Fd < a[j].Fd }
func newFdSnapshot() *FdSnapshot {
return &FdSnapshot{}
}
func (snap *FdSnapshot) FdInfo(i int) *FdInfo {
return &snap.Fds[i]
}
func (snap *FdSnapshot) dump(w io.Writer) {
for _, info := range snap.Fds {
info.dump(w)
}
}
// NewFdLeadDetector creates a new FdLeakDetector
func NewFdLeadDetector() *FdLeakDetector {
return &FdLeakDetector{}
}
const selfFdPath = "/proc/self/fd"
// Snapshot captures the list of opened file descriptors of the current
// process
func (d *FdLeakDetector) Snapshot() (snap *FdSnapshot, err error) {
root, err := os.Open(selfFdPath)
if err != nil {
return nil, err
}
defer root.Close()
infos, err := root.Readdir(-1)
if err != nil {
return nil, err
}
snap = newFdSnapshot()
for _, info := range infos {
fd, err := strconv.Atoi(info.Name())
if err != nil {
return nil, err
}
// Don't capture the fd used to list /proc/self/fd
if fd == int(root.Fd()) {
continue
}
flags, err := fcntl(fd, syscall.F_GETFL, 0)
if err != nil {
return nil, err
}
fdFlags, err := fcntl(fd, syscall.F_GETFD, 0)
if err != nil {
return nil, err
}
// readlink on /proc/self/fd gives nice textual information
// about the fd
rl, err := os.Readlink(fmt.Sprintf("%s/%d", selfFdPath, fd))
if err != nil {
return nil, err
}
snap.Fds = append(snap.Fds, FdInfo{
Fd: fd,
Flags: flags,
CloseOnExec: fdFlags&syscall.FD_CLOEXEC != 0,
Text: rl,
})
}
sort.Sort(ByFd(snap.Fds))
return
}
// Compare writes to w the differences between the a and b snaphots.
// true will be returned if the two snapshots are equal, false otherwise.
func (d *FdLeakDetector) Compare(w io.Writer, a, b *FdSnapshot) bool {
if a == nil || b == nil {
return a == b
}
equal := true
i := 0
j := 0
for i < len(a.Fds) && j < len(b.Fds) {
aInfo := a.FdInfo(i)
bInfo := b.FdInfo(j)
if aInfo.Fd == bInfo.Fd {
// File descriptor found in both snapshots
equal = equal && aInfo.equal(bInfo)
i++
j++
if !equal {
fmt.Fprintf(w, "- fd %d\n", aInfo.Fd)
aInfo.dump(w)
fmt.Fprintf(w, "+ fd %d\n", bInfo.Fd)
bInfo.dump(w)
}
} else if aInfo.Fd < bInfo.Fd {
// File descriptor present in a but not in b
equal = false
i++
fmt.Fprintf(w, "- fd %d\n", aInfo.Fd)
aInfo.dump(w)
} else {
// File descriptor present in b but not in a
equal = false
j++
fmt.Fprintf(w, "+ fd %d\n", bInfo.Fd)
bInfo.dump(w)
}
}
for ; i < len(a.Fds); i++ {
// File descriptor present in a but not in b
equal = false
aInfo := a.FdInfo(i)
fmt.Fprintf(w, "- fd %d\n", aInfo.Fd)
aInfo.dump(w)
}
for ; j < len(b.Fds); j++ {
// File descriptor present in b but not in a
equal = false
bInfo := b.FdInfo(j)
fmt.Fprintf(w, "+ fd %d\n", bInfo.Fd)
bInfo.dump(w)
}
return equal
}
func TestFdDetectorNoLeak(t *testing.T) {
detector := NewFdLeadDetector()
old, err := detector.Snapshot()
if err != nil {
t.Error(err)
}
new, err := detector.Snapshot()
if err != nil {
t.Error(err)
}
buffer := bytes.NewBuffer(nil)
equal := detector.Compare(buffer, old, new)
if buffer.Len() != 0 {
fmt.Print(buffer.String())
t.Fatal()
}
if !equal {
fmt.Print(buffer.String())
t.Fatal()
}
}
func TestFdDetectorLeak(t *testing.T) {
detector := NewFdLeadDetector()
old, err := detector.Snapshot()
if err != nil {
t.Error(err)
}
f, err := os.Open("/dev/null")
if err != nil {
t.Error(err)
}
new, err := detector.Snapshot()
if err != nil {
t.Error(err)
}
buffer := bytes.NewBuffer(nil)
equal := detector.Compare(buffer, old, new)
if equal {
fmt.Print(buffer.String())
t.Fatal()
}
if err := f.Close(); err != nil {
t.Error(err)
}
}
func TestFdDetectorCompare(t *testing.T) {
tests := []struct {
old, new *FdSnapshot
equal bool
}{
// handle nil arguments
{old: nil, new: nil, equal: true},
{old: &FdSnapshot{}, new: nil, equal: false},
{old: nil, new: &FdSnapshot{}, equal: false},
// Same fds
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
equal: true,
},
// Same fd number, different flags
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDONLY,
Text: "/foo",
},
},
},
equal: false,
},
// Same fd number, different close on exec status
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
CloseOnExec: true,
Text: "/foo",
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
equal: false,
},
// Same fd number, different readlink
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/bar",
},
},
},
equal: false,
},
// old has more fds
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
},
},
equal: false,
},
// new has more fds
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
equal: false,
},
// Same number of fds in the two snapshots, different fds at
// index 0 but same fds at index 1. We used to have a bug
// saying the two snapshots were identical.
{
old: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDONLY,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
new: &FdSnapshot{
Fds: []FdInfo{
{
Fd: 0,
Flags: syscall.O_RDWR,
},
{
Fd: 1,
Flags: syscall.O_RDWR,
Text: "/foo",
},
},
},
equal: false,
},
}
detector := NewFdLeadDetector()
for i, test := range tests {
buffer := bytes.NewBuffer(nil)
equal := detector.Compare(buffer, test.old, test.new)
if equal != test.equal {
test.old.dump(os.Stderr)
test.new.dump(os.Stderr)
fmt.Print(buffer.String())
t.Fatal(fmt.Sprintf("Failed test #%d", i))
}
}
}