Dan Willemsen | 38f2dba | 2016-07-08 14:54:35 -0700 | [diff] [blame] | 1 | // Copyright 2012 The Go Authors. All rights reserved. |
Brent Austin | ba3052e | 2015-04-21 16:08:23 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package runtime_test |
| 6 | |
| 7 | import ( |
| 8 | . "runtime" |
| 9 | "syscall" |
| 10 | "testing" |
Dan Willemsen | a322328 | 2018-02-27 19:41:43 -0800 | [diff] [blame] | 11 | "time" |
Dan Willemsen | 38f2dba | 2016-07-08 14:54:35 -0700 | [diff] [blame] | 12 | "unsafe" |
Brent Austin | ba3052e | 2015-04-21 16:08:23 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var pid, tid int |
| 16 | |
| 17 | func init() { |
| 18 | // Record pid and tid of init thread for use during test. |
| 19 | // The call to LockOSThread is just to exercise it; |
| 20 | // we can't test that it does anything. |
| 21 | // Instead we're testing that the conditions are good |
| 22 | // for how it is used in init (must be on main thread). |
| 23 | pid, tid = syscall.Getpid(), syscall.Gettid() |
| 24 | LockOSThread() |
Dan Willemsen | a322328 | 2018-02-27 19:41:43 -0800 | [diff] [blame] | 25 | |
| 26 | sysNanosleep = func(d time.Duration) { |
| 27 | // Invoke a blocking syscall directly; calling time.Sleep() |
| 28 | // would deschedule the goroutine instead. |
| 29 | ts := syscall.NsecToTimespec(d.Nanoseconds()) |
| 30 | for { |
| 31 | if err := syscall.Nanosleep(&ts, &ts); err != syscall.EINTR { |
| 32 | return |
| 33 | } |
| 34 | } |
| 35 | } |
Brent Austin | ba3052e | 2015-04-21 16:08:23 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func TestLockOSThread(t *testing.T) { |
| 39 | if pid != tid { |
| 40 | t.Fatalf("pid=%d but tid=%d", pid, tid) |
| 41 | } |
| 42 | } |
Dan Willemsen | 38f2dba | 2016-07-08 14:54:35 -0700 | [diff] [blame] | 43 | |
Patrice Arruda | 748609c | 2020-06-25 12:12:21 -0700 | [diff] [blame] | 44 | // Test that error values are negative. |
| 45 | // Use a misaligned pointer to get -EINVAL. |
Dan Willemsen | 38f2dba | 2016-07-08 14:54:35 -0700 | [diff] [blame] | 46 | func TestMincoreErrorSign(t *testing.T) { |
| 47 | var dst byte |
Patrice Arruda | 748609c | 2020-06-25 12:12:21 -0700 | [diff] [blame] | 48 | v := Mincore(Add(unsafe.Pointer(new(int32)), 1), 1, &dst) |
Dan Willemsen | 38f2dba | 2016-07-08 14:54:35 -0700 | [diff] [blame] | 49 | |
| 50 | const EINVAL = 0x16 |
| 51 | if v != -EINVAL { |
| 52 | t.Errorf("mincore = %v, want %v", v, -EINVAL) |
| 53 | } |
| 54 | } |
Dan Willemsen | c741332 | 2018-08-27 23:21:26 -0700 | [diff] [blame] | 55 | |
| 56 | func TestEpollctlErrorSign(t *testing.T) { |
Patrice Arruda | 748609c | 2020-06-25 12:12:21 -0700 | [diff] [blame] | 57 | v := Epollctl(-1, 1, -1, unsafe.Pointer(&EpollEvent{})) |
Dan Willemsen | c741332 | 2018-08-27 23:21:26 -0700 | [diff] [blame] | 58 | |
| 59 | const EBADF = 0x09 |
| 60 | if v != -EBADF { |
| 61 | t.Errorf("epollctl = %v, want %v", v, -EBADF) |
| 62 | } |
| 63 | } |
Dan Willemsen | bc60c3c | 2021-12-15 01:09:00 -0800 | [diff] [blame] | 64 | |
| 65 | func TestKernelStructSize(t *testing.T) { |
| 66 | // Check that the Go definitions of structures exchanged with the kernel are |
| 67 | // the same size as what the kernel defines. |
| 68 | if have, want := unsafe.Sizeof(Siginfo{}), uintptr(SiginfoMaxSize); have != want { |
| 69 | t.Errorf("Go's siginfo struct is %d bytes long; kernel expects %d", have, want) |
| 70 | } |
| 71 | if have, want := unsafe.Sizeof(Sigevent{}), uintptr(SigeventMaxSize); have != want { |
| 72 | t.Errorf("Go's sigevent struct is %d bytes long; kernel expects %d", have, want) |
| 73 | } |
| 74 | } |