| 1 |
// Copyright 2021 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 a |
| 6 |
|
| 7 |
// This file tests facts produced by ctrlflow. |
| 8 |
|
| 9 |
import ( |
| 10 |
"hash/maphash" |
| 11 |
"log" |
| 12 |
"os" |
| 13 |
"runtime" |
| 14 |
"syscall" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"lib" |
| 18 |
) |
| 19 |
|
| 20 |
var cond bool |
| 21 |
|
| 22 |
func a() { // want a:"noReturn" |
| 23 |
if cond { |
| 24 |
b() |
| 25 |
} else { |
| 26 |
for { |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func b() { // want b:"noReturn" |
| 32 |
select {} |
| 33 |
} |
| 34 |
|
| 35 |
func f(x int) { // no fact here |
| 36 |
switch x { |
| 37 |
case 0: |
| 38 |
os.Exit(0) |
| 39 |
case 1: |
| 40 |
panic(0) |
| 41 |
} |
| 42 |
// default case returns |
| 43 |
} |
| 44 |
|
| 45 |
type T int |
| 46 |
|
| 47 |
func (T) method1() { // want method1:"noReturn" |
| 48 |
a() |
| 49 |
} |
| 50 |
|
| 51 |
func (T) method2() { // (may return) |
| 52 |
if cond { |
| 53 |
a() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Checking for the noreturn fact associated with F ensures that |
| 58 |
// ctrlflow proved each of the listed functions was "noReturn". |
| 59 |
func standardFunctions(x int) { // want standardFunctions:"noReturn" |
| 60 |
t := new(testing.T) |
| 61 |
switch x { |
| 62 |
case 0: |
| 63 |
t.FailNow() |
| 64 |
case 1: |
| 65 |
t.Fatal() |
| 66 |
case 2: |
| 67 |
t.Fatalf("") |
| 68 |
case 3: |
| 69 |
t.Skip() |
| 70 |
case 4: |
| 71 |
t.SkipNow() |
| 72 |
case 5: |
| 73 |
t.Skipf("") |
| 74 |
case 6: |
| 75 |
log.Fatal() |
| 76 |
case 7: |
| 77 |
log.Fatalf("") |
| 78 |
case 8: |
| 79 |
log.Fatalln() |
| 80 |
case 9: |
| 81 |
os.Exit(0) |
| 82 |
case 10: |
| 83 |
syscall.Exit(0) |
| 84 |
case 11: |
| 85 |
runtime.Goexit() |
| 86 |
case 12: |
| 87 |
log.Panic() |
| 88 |
case 13: |
| 89 |
log.Panicln() |
| 90 |
case 14: |
| 91 |
log.Panicf("") |
| 92 |
default: |
| 93 |
panic(0) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func panicRecover() { |
| 98 |
defer func() { recover() }() |
| 99 |
panic(nil) |
| 100 |
} |
| 101 |
|
| 102 |
func noBody() |
| 103 |
|
| 104 |
func g() { |
| 105 |
lib.CanReturn() |
| 106 |
} |
| 107 |
|
| 108 |
func h() { // want h:"noReturn" |
| 109 |
lib.NoReturn() |
| 110 |
} |
| 111 |
|
| 112 |
func returns() { |
| 113 |
print(1) |
| 114 |
print(2) |
| 115 |
print(3) |
| 116 |
} |
| 117 |
|
| 118 |
func nobody() // a function with no body is assumed to return |
| 119 |
|
| 120 |
func hasPanic() { // want hasPanic:"noReturn" |
| 121 |
print(1) |
| 122 |
panic(2) |
| 123 |
print(3) |
| 124 |
} |
| 125 |
|
| 126 |
func hasSelect() { // want hasSelect:"noReturn" |
| 127 |
print(1) |
| 128 |
select {} |
| 129 |
print(3) |
| 130 |
} |
| 131 |
|
| 132 |
func infiniteLoop() { // want infiniteLoop:"noReturn" |
| 133 |
print(1) |
| 134 |
for { |
| 135 |
} |
| 136 |
print(3) |
| 137 |
} |
| 138 |
|
| 139 |
func ifElse(cond bool) { // want ifElse:"noReturn" |
| 140 |
print(1) |
| 141 |
if cond { |
| 142 |
hasSelect() |
| 143 |
} else { |
| 144 |
infiniteLoop() |
| 145 |
} |
| 146 |
print(3) |
| 147 |
} |
| 148 |
|
| 149 |
func swtch(x int) { // want swtch:"noReturn" |
| 150 |
print(1) |
| 151 |
switch x { |
| 152 |
case 1: |
| 153 |
hasSelect() |
| 154 |
case 2: |
| 155 |
goexit() |
| 156 |
case 3: |
| 157 |
logFatal() |
| 158 |
case 4: |
| 159 |
osexit() |
| 160 |
default: |
| 161 |
panic(3) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func _if(cond bool) { |
| 166 |
print(1) |
| 167 |
if cond { |
| 168 |
hasSelect() |
| 169 |
} |
| 170 |
print(3) |
| 171 |
} |
| 172 |
|
| 173 |
func logFatal() { // want logFatal:"noReturn" |
| 174 |
print(1) |
| 175 |
log.Fatal("oops") |
| 176 |
print(2) |
| 177 |
} |
| 178 |
|
| 179 |
func testFatal(t *testing.T) { // want testFatal:"noReturn" |
| 180 |
print(1) |
| 181 |
t.Fatal("oops") |
| 182 |
print(2) |
| 183 |
} |
| 184 |
|
| 185 |
func goexit() { // want goexit:"noReturn" |
| 186 |
print(1) |
| 187 |
runtime.Goexit() |
| 188 |
print(2) |
| 189 |
} |
| 190 |
|
| 191 |
func osexit() { // want osexit:"noReturn" |
| 192 |
print(1) |
| 193 |
os.Exit(0) |
| 194 |
print(2) |
| 195 |
} |
| 196 |
|
| 197 |
func intrinsic() { // (no fact) |
| 198 |
|
| 199 |
// Comparable calls abi.EscapeNonString, whose body appears to panic; |
| 200 |
// but that's a lie, as EscapeNonString is a compiler intrinsic. |
| 201 |
// (go1.24 used a different intrinsic, maphash.escapeForHash.) |
| 202 |
maphash.Comparable[int](maphash.Seed{}, 0) |
| 203 |
} |