summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/kernel/futex/futex_test.go
blob: 7b81358ec6602e691d6578ba91d84ea23335ac87 (plain)
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
// Copyright 2018 Google Inc.
//
// 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 futex

import (
	"math"
	"runtime"
	"sync"
	"sync/atomic"
	"syscall"
	"testing"
	"unsafe"
)

const (
	testMutexSize            = 4
	testMutexLocked   uint32 = 1
	testMutexUnlocked uint32 = 0
)

// testData implements the Checker interface, and allows us to
// treat the address passed for futex operations as an index in
// a byte slice for testing simplicity.
type testData []byte

func newTestData(size uint) testData {
	return make([]byte, size)
}

func (t testData) Check(addr uintptr, val uint32) error {
	if val != atomic.LoadUint32((*uint32)(unsafe.Pointer(&t[addr]))) {
		return syscall.EAGAIN
	}
	return nil
}

func (t testData) Op(addr uintptr, val uint32) (bool, error) {
	return val == 0, nil
}

// testMutex ties together a testData slice, an address, and a
// futex manager in order to implement the sync.Locker interface.
// Beyond being used as a Locker, this is a simple mechanism for
// changing the underlying values for simpler tests.
type testMutex struct {
	a uintptr
	d testData
	m *Manager
}

func newTestMutex(addr uintptr, d testData, m *Manager) *testMutex {
	return &testMutex{a: addr, d: d, m: m}
}

// Lock acquires the testMutex.
// This may wait for it to be available via the futex manager.
func (t *testMutex) Lock() {
	for {
		// Attempt to grab the lock.
		if atomic.CompareAndSwapUint32(
			((*uint32)(unsafe.Pointer(&t.d[t.a]))),
			testMutexUnlocked,
			testMutexLocked) {
			// Lock held.
			return
		}

		// Wait for it to be "not locked".
		w := NewWaiter()
		err := t.m.WaitPrepare(w, t.d, t.a, testMutexLocked, ^uint32(0))
		if err == syscall.EAGAIN {
			continue
		}
		if err != nil {
			// Should never happen.
			panic("WaitPrepare returned unexpected error: " + err.Error())
		}
		<-w.C
		t.m.WaitComplete(w)
	}
}

// Unlock releases the testMutex.
// This will notify any waiters via the futex manager.
func (t *testMutex) Unlock() {
	// Unlock.
	atomic.StoreUint32(((*uint32)(unsafe.Pointer(&t.d[t.a]))), testMutexUnlocked)

	// Notify all waiters.
	t.m.Wake(t.a, ^uint32(0), math.MaxInt32)
}

func TestFutexWake(t *testing.T) {
	m := NewManager()
	d := newTestData(testMutexSize)

	// Wait for it to be locked.
	// (This won't trigger the wake in testMutex)
	w := NewWaiter()
	m.WaitPrepare(w, d, 0, testMutexUnlocked, ^uint32(0))

	// Wake the single thread.
	if _, err := m.Wake(0, ^uint32(0), 1); err != nil {
		t.Error("wake error:", err)
	}

	<-w.C
	m.WaitComplete(w)
}

func TestFutexWakeBitmask(t *testing.T) {
	m := NewManager()
	d := newTestData(testMutexSize)

	// Wait for it to be locked.
	// (This won't trigger the wake in testMutex)
	w := NewWaiter()
	m.WaitPrepare(w, d, 0, testMutexUnlocked, 0x0000ffff)

	// Wake the single thread, not using the bitmask.
	if _, err := m.Wake(0, 0xffff0000, 1); err != nil {
		t.Error("wake non-matching bitmask error:", err)
	}

	select {
	case <-w.C:
		t.Error("w is alive?")
	default:
	}

	// Now use a matching bitmask.
	if _, err := m.Wake(0, 0x00000001, 1); err != nil {
		t.Error("wake matching bitmask error:", err)
	}

	<-w.C
	m.WaitComplete(w)
}

func TestFutexWakeTwo(t *testing.T) {
	m := NewManager()
	d := newTestData(testMutexSize)

	// Wait for it to be locked.
	// (This won't trigger the wake in testMutex)
	w1 := NewWaiter()
	w2 := NewWaiter()
	w3 := NewWaiter()
	m.WaitPrepare(w1, d, 0, testMutexUnlocked, ^uint32(0))
	m.WaitPrepare(w2, d, 0, testMutexUnlocked, ^uint32(0))
	m.WaitPrepare(w3, d, 0, testMutexUnlocked, ^uint32(0))

	// Wake exactly two threads.
	if _, err := m.Wake(0, ^uint32(0), 2); err != nil {
		t.Error("wake error:", err)
	}

	// Ensure exactly two are alive.
	// We don't get guarantees about exactly which two,
	// (although we expect them to be w1 and w2).
	awake := 0
	for {
		select {
		case <-w1.C:
			awake++
		case <-w2.C:
			awake++
		case <-w3.C:
			awake++
		default:
			if awake != 2 {
				t.Error("awake != 2?")
			}

			// Success.
			return
		}
	}
}

func TestFutexWakeUnrelated(t *testing.T) {
	m := NewManager()
	d := newTestData(2 * testMutexSize)

	// Wait for it to be locked.
	w1 := NewWaiter()
	w2 := NewWaiter()
	m.WaitPrepare(w1, d, 0*testMutexSize, testMutexUnlocked, ^uint32(0))
	m.WaitPrepare(w2, d, 1*testMutexSize, testMutexUnlocked, ^uint32(0))

	// Wake only the second one.
	if _, err := m.Wake(1*testMutexSize, ^uint32(0), 2); err != nil {
		t.Error("wake error:", err)
	}

	// Ensure only r2 is alive.
	select {
	case <-w1.C:
		t.Error("w1 is alive?")
	default:
	}
	<-w2.C
}

// This function was shamelessly stolen from mutex_test.go.
func HammerMutex(l sync.Locker, loops int, cdone chan bool) {
	for i := 0; i < loops; i++ {
		l.Lock()
		runtime.Gosched()
		l.Unlock()
	}
	cdone <- true
}

func TestFutexStress(t *testing.T) {
	m := NewManager()
	d := newTestData(testMutexSize)
	tm := newTestMutex(0*testMutexSize, d, m)
	c := make(chan bool)

	for i := 0; i < 10; i++ {
		go HammerMutex(tm, 1000, c)
	}

	for i := 0; i < 10; i++ {
		<-c
	}
}

func TestWakeOpEmpty(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	n, err := m.WakeOp(d, 0, 4, 10, 10, 0)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 0 {
		t.Fatalf("Invalid number of wakes: want 0, got %d", n)
	}
}

func TestWakeOpFirstNonEmpty(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add two waiters on address 0.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	// Wake up all waiters on address 0.
	n, err := m.WakeOp(d, 0, 4, 10, 10, 0)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 2 {
		t.Fatalf("Invalid number of wakes: want 2, got %d", n)
	}
}

func TestWakeOpSecondNonEmpty(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add two waiters on address 4.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	// Wake up all waiters on address 4.
	n, err := m.WakeOp(d, 0, 4, 10, 10, 0)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 2 {
		t.Fatalf("Invalid number of wakes: want 2, got %d", n)
	}
}

func TestWakeOpSecondNonEmptyFailingOp(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add two waiters on address 4.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	// Wake up all waiters on address 4.
	n, err := m.WakeOp(d, 0, 4, 10, 10, 1)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 0 {
		t.Fatalf("Invalid number of wakes: want 0, got %d", n)
	}
}

func TestWakeOpAllNonEmpty(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add two waiters on address 0.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	// Add two waiters on address 4.
	w3 := NewWaiter()
	if err := m.WaitPrepare(w3, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w3)

	w4 := NewWaiter()
	if err := m.WaitPrepare(w4, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w4)

	// Wake up all waiters on both addresses.
	n, err := m.WakeOp(d, 0, 4, 10, 10, 0)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 4 {
		t.Fatalf("Invalid number of wakes: want 4, got %d", n)
	}
}

func TestWakeOpAllNonEmptyFailingOp(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add two waiters on address 0.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	// Add two waiters on address 4.
	w3 := NewWaiter()
	if err := m.WaitPrepare(w3, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w3)

	w4 := NewWaiter()
	if err := m.WaitPrepare(w4, d, 4, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w4)

	// Wake up all waiters on both addresses.
	n, err := m.WakeOp(d, 0, 4, 10, 10, 1)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 2 {
		t.Fatalf("Invalid number of wakes: want 2, got %d", n)
	}
}

func TestWakeOpSameAddress(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add four waiters on address 0.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	w3 := NewWaiter()
	if err := m.WaitPrepare(w3, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w3)

	w4 := NewWaiter()
	if err := m.WaitPrepare(w4, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w4)

	// Use the same address, with one at most one waiter from each.
	n, err := m.WakeOp(d, 0, 0, 1, 1, 0)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 2 {
		t.Fatalf("Invalid number of wakes: want 2, got %d", n)
	}
}

func TestWakeOpSameAddressFailingOp(t *testing.T) {
	m := NewManager()
	d := newTestData(8)

	// Add four waiters on address 0.
	w1 := NewWaiter()
	if err := m.WaitPrepare(w1, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w1)

	w2 := NewWaiter()
	if err := m.WaitPrepare(w2, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w2)

	w3 := NewWaiter()
	if err := m.WaitPrepare(w3, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w3)

	w4 := NewWaiter()
	if err := m.WaitPrepare(w4, d, 0, 0, ^uint32(0)); err != nil {
		t.Fatalf("WaitPrepare failed: %v", err)
	}
	defer m.WaitComplete(w4)

	// Use the same address, with one at most one waiter from each.
	n, err := m.WakeOp(d, 0, 0, 1, 1, 1)
	if err != nil {
		t.Fatalf("WakeOp failed: %v", err)
	}

	if n != 1 {
		t.Fatalf("Invalid number of wakes: want 1, got %d", n)
	}
}