| 1 |
// Copyright 2012 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 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"flag" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
. "runtime" |
| 12 |
"runtime/debug" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
"unsafe" |
| 16 |
) |
| 17 |
|
| 18 |
var flagQuick = flag.Bool("quick", false, "skip slow tests, for second run in all.bash") |
| 19 |
|
| 20 |
func init() { |
| 21 |
// We're testing the runtime, so make tracebacks show things |
| 22 |
// in the runtime. This only raises the level, so it won't |
| 23 |
// override GOTRACEBACK=crash from the user. |
| 24 |
SetTracebackEnv("system") |
| 25 |
} |
| 26 |
|
| 27 |
var errf error |
| 28 |
|
| 29 |
func errfn() error { |
| 30 |
return errf |
| 31 |
} |
| 32 |
|
| 33 |
func errfn1() error { |
| 34 |
return io.EOF |
| 35 |
} |
| 36 |
|
| 37 |
func BenchmarkIfaceCmp100(b *testing.B) { |
| 38 |
for i := 0; i < b.N; i++ { |
| 39 |
for j := 0; j < 100; j++ { |
| 40 |
if errfn() == io.EOF { |
| 41 |
b.Fatal("bad comparison") |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func BenchmarkIfaceCmpNil100(b *testing.B) { |
| 48 |
for i := 0; i < b.N; i++ { |
| 49 |
for j := 0; j < 100; j++ { |
| 50 |
if errfn1() == nil { |
| 51 |
b.Fatal("bad comparison") |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
var efaceCmp1 any |
| 58 |
var efaceCmp2 any |
| 59 |
|
| 60 |
func BenchmarkEfaceCmpDiff(b *testing.B) { |
| 61 |
x := 5 |
| 62 |
efaceCmp1 = &x |
| 63 |
y := 6 |
| 64 |
efaceCmp2 = &y |
| 65 |
for i := 0; i < b.N; i++ { |
| 66 |
for j := 0; j < 100; j++ { |
| 67 |
if efaceCmp1 == efaceCmp2 { |
| 68 |
b.Fatal("bad comparison") |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func BenchmarkEfaceCmpDiffIndirect(b *testing.B) { |
| 75 |
efaceCmp1 = [2]int{1, 2} |
| 76 |
efaceCmp2 = [2]int{1, 2} |
| 77 |
for i := 0; i < b.N; i++ { |
| 78 |
for j := 0; j < 100; j++ { |
| 79 |
if efaceCmp1 != efaceCmp2 { |
| 80 |
b.Fatal("bad comparison") |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func BenchmarkDefer(b *testing.B) { |
| 87 |
for i := 0; i < b.N; i++ { |
| 88 |
defer1() |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func defer1() { |
| 93 |
defer func(x, y, z int) { |
| 94 |
if recover() != nil || x != 1 || y != 2 || z != 3 { |
| 95 |
panic("bad recover") |
| 96 |
} |
| 97 |
}(1, 2, 3) |
| 98 |
} |
| 99 |
|
| 100 |
func BenchmarkDefer10(b *testing.B) { |
| 101 |
for i := 0; i < b.N/10; i++ { |
| 102 |
defer2() |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func defer2() { |
| 107 |
for i := 0; i < 10; i++ { |
| 108 |
defer func(x, y, z int) { |
| 109 |
if recover() != nil || x != 1 || y != 2 || z != 3 { |
| 110 |
panic("bad recover") |
| 111 |
} |
| 112 |
}(1, 2, 3) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func BenchmarkDeferMany(b *testing.B) { |
| 117 |
for i := 0; i < b.N; i++ { |
| 118 |
defer func(x, y, z int) { |
| 119 |
if recover() != nil || x != 1 || y != 2 || z != 3 { |
| 120 |
panic("bad recover") |
| 121 |
} |
| 122 |
}(1, 2, 3) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func BenchmarkPanicRecover(b *testing.B) { |
| 127 |
for i := 0; i < b.N; i++ { |
| 128 |
defer3() |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func defer3() { |
| 133 |
defer func(x, y, z int) { |
| 134 |
if recover() == nil { |
| 135 |
panic("failed recover") |
| 136 |
} |
| 137 |
}(1, 2, 3) |
| 138 |
panic("hi") |
| 139 |
} |
| 140 |
|
| 141 |
// golang.org/issue/7063 |
| 142 |
func TestStopCPUProfilingWithProfilerOff(t *testing.T) { |
| 143 |
SetCPUProfileRate(0) |
| 144 |
} |
| 145 |
|
| 146 |
// Addresses to test for faulting behavior. |
| 147 |
// This is less a test of SetPanicOnFault and more a check that |
| 148 |
// the operating system and the runtime can process these faults |
| 149 |
// correctly. That is, we're indirectly testing that without SetPanicOnFault |
| 150 |
// these would manage to turn into ordinary crashes. |
| 151 |
// Note that these are truncated on 32-bit systems, so the bottom 32 bits |
| 152 |
// of the larger addresses must themselves be invalid addresses. |
| 153 |
// We might get unlucky and the OS might have mapped one of these |
| 154 |
// addresses, but probably not: they're all in the first page, very high |
| 155 |
// addresses that normally an OS would reserve for itself, or malformed |
| 156 |
// addresses. Even so, we might have to remove one or two on different |
| 157 |
// systems. We will see. |
| 158 |
|
| 159 |
var faultAddrs = []uint64{ |
| 160 |
// low addresses |
| 161 |
0, |
| 162 |
1, |
| 163 |
0xfff, |
| 164 |
// high (kernel) addresses |
| 165 |
// or else malformed. |
| 166 |
0xffffffffffffffff, |
| 167 |
0xfffffffffffff001, |
| 168 |
0xffffffffffff0001, |
| 169 |
0xfffffffffff00001, |
| 170 |
0xffffffffff000001, |
| 171 |
0xfffffffff0000001, |
| 172 |
0xffffffff00000001, |
| 173 |
0xfffffff000000001, |
| 174 |
0xffffff0000000001, |
| 175 |
0xfffff00000000001, |
| 176 |
0xffff000000000001, |
| 177 |
0xfff0000000000001, |
| 178 |
0xff00000000000001, |
| 179 |
0xf000000000000001, |
| 180 |
0x8000000000000001, |
| 181 |
} |
| 182 |
|
| 183 |
func TestSetPanicOnFault(t *testing.T) { |
| 184 |
old := debug.SetPanicOnFault(true) |
| 185 |
defer debug.SetPanicOnFault(old) |
| 186 |
|
| 187 |
nfault := 0 |
| 188 |
for _, addr := range faultAddrs { |
| 189 |
testSetPanicOnFault(t, uintptr(addr), &nfault) |
| 190 |
} |
| 191 |
if nfault == 0 { |
| 192 |
t.Fatalf("none of the addresses faulted") |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// testSetPanicOnFault tests one potentially faulting address. |
| 197 |
// It deliberately constructs and uses an invalid pointer, |
| 198 |
// so mark it as nocheckptr. |
| 199 |
//go:nocheckptr |
| 200 |
func testSetPanicOnFault(t *testing.T, addr uintptr, nfault *int) { |
| 201 |
if strings.Contains(Version(), "llvm") { |
| 202 |
t.Skip("LLVM doesn't support non-call exception") |
| 203 |
} |
| 204 |
if GOOS == "js" { |
| 205 |
t.Skip("js does not support catching faults") |
| 206 |
} |
| 207 |
|
| 208 |
defer func() { |
| 209 |
if err := recover(); err != nil { |
| 210 |
*nfault++ |
| 211 |
} |
| 212 |
}() |
| 213 |
|
| 214 |
// The read should fault, except that sometimes we hit |
| 215 |
// addresses that have had C or kernel pages mapped there |
| 216 |
// readable by user code. So just log the content. |
| 217 |
// If no addresses fault, we'll fail the test. |
| 218 |
v := *(*byte)(unsafe.Pointer(addr)) |
| 219 |
t.Logf("addr %#x: %#x\n", addr, v) |
| 220 |
} |
| 221 |
|
| 222 |
func eqstring_generic(s1, s2 string) bool { |
| 223 |
if len(s1) != len(s2) { |
| 224 |
return false |
| 225 |
} |
| 226 |
// optimization in assembly versions: |
| 227 |
// if s1.str == s2.str { return true } |
| 228 |
for i := 0; i < len(s1); i++ { |
| 229 |
if s1[i] != s2[i] { |
| 230 |
return false |
| 231 |
} |
| 232 |
} |
| 233 |
return true |
| 234 |
} |
| 235 |
|
| 236 |
func TestEqString(t *testing.T) { |
| 237 |
// This isn't really an exhaustive test of == on strings, it's |
| 238 |
// just a convenient way of documenting (via eqstring_generic) |
| 239 |
// what == does. |
| 240 |
s := []string{ |
| 241 |
"", |
| 242 |
"a", |
| 243 |
"c", |
| 244 |
"aaa", |
| 245 |
"ccc", |
| 246 |
"cccc"[:3], // same contents, different string |
| 247 |
"1234567890", |
| 248 |
} |
| 249 |
for _, s1 := range s { |
| 250 |
for _, s2 := range s { |
| 251 |
x := s1 == s2 |
| 252 |
y := eqstring_generic(s1, s2) |
| 253 |
if x != y { |
| 254 |
t.Errorf(`("%s" == "%s") = %t, want %t`, s1, s2, x, y) |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
func TestTrailingZero(t *testing.T) { |
| 262 |
// make sure we add padding for structs with trailing zero-sized fields |
| 263 |
type T1 struct { |
| 264 |
n int32 |
| 265 |
z [0]byte |
| 266 |
} |
| 267 |
if unsafe.Sizeof(T1{}) != 8 { |
| 268 |
t.Errorf("sizeof(%#v)==%d, want 8", T1{}, unsafe.Sizeof(T1{})) |
| 269 |
} |
| 270 |
type T2 struct { |
| 271 |
n int64 |
| 272 |
z struct{} |
| 273 |
} |
| 274 |
if unsafe.Sizeof(T2{}) != 8+unsafe.Sizeof(uintptr(0)) { |
| 275 |
t.Errorf("sizeof(%#v)==%d, want %d", T2{}, unsafe.Sizeof(T2{}), 8+unsafe.Sizeof(uintptr(0))) |
| 276 |
} |
| 277 |
type T3 struct { |
| 278 |
n byte |
| 279 |
z [4]struct{} |
| 280 |
} |
| 281 |
if unsafe.Sizeof(T3{}) != 2 { |
| 282 |
t.Errorf("sizeof(%#v)==%d, want 2", T3{}, unsafe.Sizeof(T3{})) |
| 283 |
} |
| 284 |
// make sure padding can double for both zerosize and alignment |
| 285 |
type T4 struct { |
| 286 |
a int32 |
| 287 |
b int16 |
| 288 |
c int8 |
| 289 |
z struct{} |
| 290 |
} |
| 291 |
if unsafe.Sizeof(T4{}) != 8 { |
| 292 |
t.Errorf("sizeof(%#v)==%d, want 8", T4{}, unsafe.Sizeof(T4{})) |
| 293 |
} |
| 294 |
// make sure we don't pad a zero-sized thing |
| 295 |
type T5 struct { |
| 296 |
} |
| 297 |
if unsafe.Sizeof(T5{}) != 0 { |
| 298 |
t.Errorf("sizeof(%#v)==%d, want 0", T5{}, unsafe.Sizeof(T5{})) |
| 299 |
} |
| 300 |
} |
| 301 |
*/ |
| 302 |
|
| 303 |
func TestAppendGrowth(t *testing.T) { |
| 304 |
var x []int64 |
| 305 |
check := func(want int) { |
| 306 |
if cap(x) != want { |
| 307 |
t.Errorf("len=%d, cap=%d, want cap=%d", len(x), cap(x), want) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
check(0) |
| 312 |
want := 1 |
| 313 |
for i := 1; i <= 100; i++ { |
| 314 |
x = append(x, 1) |
| 315 |
check(want) |
| 316 |
if i&(i-1) == 0 { |
| 317 |
want = 2 * i |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
var One = []int64{1} |
| 323 |
|
| 324 |
func TestAppendSliceGrowth(t *testing.T) { |
| 325 |
var x []int64 |
| 326 |
check := func(want int) { |
| 327 |
if cap(x) != want { |
| 328 |
t.Errorf("len=%d, cap=%d, want cap=%d", len(x), cap(x), want) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
check(0) |
| 333 |
want := 1 |
| 334 |
for i := 1; i <= 100; i++ { |
| 335 |
x = append(x, One...) |
| 336 |
check(want) |
| 337 |
if i&(i-1) == 0 { |
| 338 |
want = 2 * i |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func TestGoroutineProfileTrivial(t *testing.T) { |
| 344 |
// Calling GoroutineProfile twice in a row should find the same number of goroutines, |
| 345 |
// but it's possible there are goroutines just about to exit, so we might end up |
| 346 |
// with fewer in the second call. Try a few times; it should converge once those |
| 347 |
// zombies are gone. |
| 348 |
for i := 0; ; i++ { |
| 349 |
n1, ok := GoroutineProfile(nil) // should fail, there's at least 1 goroutine |
| 350 |
if n1 < 1 || ok { |
| 351 |
t.Fatalf("GoroutineProfile(nil) = %d, %v, want >0, false", n1, ok) |
| 352 |
} |
| 353 |
n2, ok := GoroutineProfile(make([]StackRecord, n1)) |
| 354 |
if n2 == n1 && ok { |
| 355 |
break |
| 356 |
} |
| 357 |
t.Logf("GoroutineProfile(%d) = %d, %v, want %d, true", n1, n2, ok, n1) |
| 358 |
if i >= 10 { |
| 359 |
t.Fatalf("GoroutineProfile not converging") |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
func TestVersion(t *testing.T) { |
| 365 |
// Test that version does not contain \r or \n. |
| 366 |
vers := Version() |
| 367 |
if strings.Contains(vers, "\r") || strings.Contains(vers, "\n") { |
| 368 |
t.Fatalf("cr/nl in version: %q", vers) |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
func TestTimediv(t *testing.T) { |
| 373 |
for _, tc := range []struct { |
| 374 |
num int64 |
| 375 |
div int32 |
| 376 |
ret int32 |
| 377 |
rem int32 |
| 378 |
}{ |
| 379 |
{ |
| 380 |
num: 8, |
| 381 |
div: 2, |
| 382 |
ret: 4, |
| 383 |
rem: 0, |
| 384 |
}, |
| 385 |
{ |
| 386 |
num: 9, |
| 387 |
div: 2, |
| 388 |
ret: 4, |
| 389 |
rem: 1, |
| 390 |
}, |
| 391 |
{ |
| 392 |
// Used by runtime.check. |
| 393 |
num: 12345*1000000000 + 54321, |
| 394 |
div: 1000000000, |
| 395 |
ret: 12345, |
| 396 |
rem: 54321, |
| 397 |
}, |
| 398 |
{ |
| 399 |
num: 1<<32 - 1, |
| 400 |
div: 2, |
| 401 |
ret: 1<<31 - 1, // no overflow. |
| 402 |
rem: 1, |
| 403 |
}, |
| 404 |
{ |
| 405 |
num: 1 << 32, |
| 406 |
div: 2, |
| 407 |
ret: 1<<31 - 1, // overflow. |
| 408 |
rem: 0, |
| 409 |
}, |
| 410 |
{ |
| 411 |
num: 1 << 40, |
| 412 |
div: 2, |
| 413 |
ret: 1<<31 - 1, // overflow. |
| 414 |
rem: 0, |
| 415 |
}, |
| 416 |
{ |
| 417 |
num: 1<<40 + 1, |
| 418 |
div: 1 << 10, |
| 419 |
ret: 1 << 30, |
| 420 |
rem: 1, |
| 421 |
}, |
| 422 |
} { |
| 423 |
name := fmt.Sprintf("%d div %d", tc.num, tc.div) |
| 424 |
t.Run(name, func(t *testing.T) { |
| 425 |
// Double check that the inputs make sense using |
| 426 |
// standard 64-bit division. |
| 427 |
ret64 := tc.num / int64(tc.div) |
| 428 |
rem64 := tc.num % int64(tc.div) |
| 429 |
if ret64 != int64(int32(ret64)) { |
| 430 |
// Simulate timediv overflow value. |
| 431 |
ret64 = 1<<31 - 1 |
| 432 |
rem64 = 0 |
| 433 |
} |
| 434 |
if ret64 != int64(tc.ret) { |
| 435 |
t.Errorf("%d / %d got ret %d rem %d want ret %d rem %d", tc.num, tc.div, ret64, rem64, tc.ret, tc.rem) |
| 436 |
} |
| 437 |
|
| 438 |
var rem int32 |
| 439 |
ret := Timediv(tc.num, tc.div, &rem) |
| 440 |
if ret != tc.ret || rem != tc.rem { |
| 441 |
t.Errorf("timediv %d / %d got ret %d rem %d want ret %d rem %d", tc.num, tc.div, ret, rem, tc.ret, tc.rem) |
| 442 |
} |
| 443 |
}) |
| 444 |
} |
| 445 |
} |