github.com/lzhfromustc/gofuzz@v0.0.0-20211116160056-151b3108bbd1/runtime/myutils.go (about) 1 package runtime 2 3 import ( 4 "internal/bytealg" 5 "math/bits" 6 ) 7 8 func GoID() int64 { 9 return getg().goid 10 } 11 12 func SleepMS(numMs int) { 13 durationMS := 1000 14 for i := 0; i < numMs; i++ { 15 usleep(uint32(durationMS)) // seems usleep can at most sleep for 10ms 16 } 17 } 18 19 func Byte_to_Uint16(b []byte) uint16 { 20 return uint16(b[1]) | uint16(b[0])<<8 21 } 22 23 func Uint16_to_Byte(b []byte, v uint16) { 24 b[0] = byte(v >> 8) 25 b[1] = byte(v) 26 } 27 28 func Byte_to_Uint32(b []byte) uint32 { 29 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 30 } 31 32 func Uint32_to_Byte(b []byte, v uint32) { 33 b[0] = byte(v >> 24) 34 b[1] = byte(v >> 16) 35 b[2] = byte(v >> 8) 36 b[3] = byte(v) 37 } 38 39 func XorUint16(a, b uint16) uint16 { 40 byteA := []byte{0, 0} 41 byteB := []byte{0, 0} 42 Uint16_to_Byte(byteA, a) 43 Uint16_to_Byte(byteB, b) 44 45 byteA = XorByte(byteA, byteB) 46 return Byte_to_Uint16(byteA) 47 } 48 49 func XorUint32(a, b uint32) uint32 { 50 byteA := []byte{0, 0, 0, 0} 51 byteB := []byte{0, 0, 0, 0} 52 Uint32_to_Byte(byteA, a) 53 Uint32_to_Byte(byteB, b) 54 55 byteA = XorByte(byteA, byteB) 56 return Byte_to_Uint32(byteA) 57 } 58 59 60 func XorByte(a, b []byte) []byte { 61 for i, _ := range a { 62 a[i] ^= b[i] 63 } 64 return a 65 } 66 67 // Parse the string returned by Stack(*, false) 68 // e.g.: 69 //goroutine 181 [running]: 70 //runtime.BeforeBlock() 71 ///usr/local/go/src/runtime/mycode.go:30 +0x90 72 //google.golang.org/grpc.(*addrConn).resetTransport(0xc0001a2840) 73 ///data/ziheng/shared/gotest/stubs/grpc/grpc-last/src/google.golang.org/grpc/clientconn.go:1213 +0x676 74 //created by google.golang.org/grpc.(*addrConn).connect 75 ///data/ziheng/shared/gotest/stubs/grpc/grpc-last/src/google.golang.org/grpc/clientconn.go:843 +0x12a 76 77 type StackSingleGo struct { 78 GoID string 79 GoStatus string // this may not help 80 VecFuncName, VecFuncFile, VecFuncLine []string 81 CreaterName, CreaterFile, CreaterLine string 82 OnOtherThread bool // Sometimes the stack is unavailable, if the goroutine is on another thread 83 } 84 85 func ParseStackStr(stackStr string) StackSingleGo { 86 stackSingleGo := StackSingleGo{} 87 // first line 88 indexGoroutine := Index(stackStr, "goroutine ") 89 indexLeftParen := Index(stackStr, " [") 90 indexRightParen := Index(stackStr, "]") 91 if Index(stackStr, "goroutine ") == -1 || Index(stackStr, " [") == -1 || Index(stackStr, "]") == -1 { 92 print("detected1\n") 93 print(stackStr) 94 } 95 stackSingleGo.GoID = stackStr[indexGoroutine + 10: indexLeftParen] 96 stackSingleGo.GoStatus = stackStr[indexLeftParen + 2: indexRightParen] 97 98 if Index(stackStr, "goroutine running on other thread; stack unavailable") > -1 { 99 stackSingleGo.OnOtherThread = true 100 return stackSingleGo 101 } 102 103 str := stackStr 104 for { 105 indexEnter := Index(str, "\n") 106 if indexEnter == -1 { 107 break 108 } 109 str = str[indexEnter + 1:] // remove the last line 110 if str == "" { 111 break 112 } 113 indexCreatedBy := Index(str, "created by ") 114 boolCreatedBy := indexCreatedBy > -1 && Index(str, "\n") > indexCreatedBy // this line indicates which function creates this goroutine 115 if boolCreatedBy { 116 stackSingleGo.CreaterName = str[indexCreatedBy + 11 : Index(str, "\n") ] 117 if Index(str, "\n") == -1 { 118 print("detected2\n") 119 print(str) 120 } 121 } else { 122 indexLastLeftParen := LastIndex(str, "(") 123 if LastIndex(str, "(") == -1 { 124 print("detected3\n") 125 print(str) 126 } 127 stackSingleGo.VecFuncName = append(stackSingleGo.VecFuncName, str[ : indexLastLeftParen]) 128 } 129 130 str = str[Index(str, "\n") + 1:] 131 indexColon := Index(str, ":") 132 indexSpace := Index(str, " ") 133 if -1 == Index(str, ":") || -1 == Index(str, " ") { 134 print("detected4\n") 135 print(str) 136 } 137 if boolCreatedBy { 138 strFuncFile := str[ : indexColon] 139 if indexTab := Index(strFuncFile, "\t"); indexTab > -1 { // remove "\t" 140 strFuncFile = strFuncFile[indexTab + 1:] 141 } 142 stackSingleGo.CreaterFile = strFuncFile 143 stackSingleGo.CreaterLine = str[indexColon + 1: indexSpace] 144 } else { 145 strFuncFile := str[ : indexColon] 146 if indexTab := Index(strFuncFile, "\t"); indexTab > -1 { // remove "\t" 147 strFuncFile = strFuncFile[indexTab + 1:] 148 } 149 stackSingleGo.VecFuncFile = append(stackSingleGo.VecFuncFile, strFuncFile) 150 stackSingleGo.VecFuncLine = append(stackSingleGo.VecFuncLine, str[indexColon + 1: indexSpace]) 151 } 152 153 } 154 return stackSingleGo 155 } 156 157 func Index(s, substr string) int { 158 n := len(substr) 159 switch { 160 case n == 0: 161 return 0 162 case n == 1: 163 return IndexByte(s, substr[0]) 164 case n == len(s): 165 if substr == s { 166 return 0 167 } 168 return -1 169 case n > len(s): 170 return -1 171 case n <= bytealg.MaxLen: 172 // Use brute force when s and substr both are small 173 if len(s) <= bytealg.MaxBruteForce { 174 return bytealg.IndexString(s, substr) 175 } 176 c0 := substr[0] 177 c1 := substr[1] 178 i := 0 179 t := len(s) - n + 1 180 fails := 0 181 for i < t { 182 if s[i] != c0 { 183 // IndexByte is faster than bytealg.IndexString, so use it as long as 184 // we're not getting lots of false positives. 185 o := IndexByte(s[i:t], c0) 186 if o < 0 { 187 return -1 188 } 189 i += o 190 } 191 if s[i+1] == c1 && s[i:i+n] == substr { 192 return i 193 } 194 fails++ 195 i++ 196 // Switch to bytealg.IndexString when IndexByte produces too many false positives. 197 if fails > bytealg.Cutover(i) { 198 r := bytealg.IndexString(s[i:], substr) 199 if r >= 0 { 200 return r + i 201 } 202 return -1 203 } 204 } 205 return -1 206 } 207 c0 := substr[0] 208 c1 := substr[1] 209 i := 0 210 t := len(s) - n + 1 211 fails := 0 212 for i < t { 213 if s[i] != c0 { 214 o := IndexByte(s[i:t], c0) 215 if o < 0 { 216 return -1 217 } 218 i += o 219 } 220 if s[i+1] == c1 && s[i:i+n] == substr { 221 return i 222 } 223 i++ 224 fails++ 225 if fails >= 4+i>>4 && i < t { 226 // See comment in ../bytes/bytes.go. 227 j := indexRabinKarp(s[i:], substr) 228 if j < 0 { 229 return -1 230 } 231 return i + j 232 } 233 } 234 return -1 235 } 236 237 // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s. 238 func LastIndex(s, substr string) int { 239 n := len(substr) 240 switch { 241 case n == 0: 242 return len(s) 243 case n == 1: 244 return LastIndexByte(s, substr[0]) 245 case n == len(s): 246 if substr == s { 247 return 0 248 } 249 return -1 250 case n > len(s): 251 return -1 252 } 253 // Rabin-Karp search from the end of the string 254 hashss, pow := hashStrRev(substr) 255 last := len(s) - n 256 var h uint32 257 for i := len(s) - 1; i >= last; i-- { 258 h = h*primeRK + uint32(s[i]) 259 } 260 if h == hashss && s[last:] == substr { 261 return last 262 } 263 for i := last - 1; i >= 0; i-- { 264 h *= primeRK 265 h += uint32(s[i]) 266 h -= pow * uint32(s[i+n]) 267 if h == hashss && s[i:i+n] == substr { 268 return i 269 } 270 } 271 return -1 272 } 273 274 // hashStrRev returns the hash of the reverse of sep and the 275 // appropriate multiplicative factor for use in Rabin-Karp algorithm. 276 func hashStrRev(sep string) (uint32, uint32) { 277 hash := uint32(0) 278 for i := len(sep) - 1; i >= 0; i-- { 279 hash = hash*primeRK + uint32(sep[i]) 280 } 281 var pow, sq uint32 = 1, primeRK 282 for i := len(sep); i > 0; i >>= 1 { 283 if i&1 != 0 { 284 pow *= sq 285 } 286 sq *= sq 287 } 288 return hash, pow 289 } 290 291 // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s. 292 func LastIndexByte(s string, c byte) int { 293 for i := len(s) - 1; i >= 0; i-- { 294 if s[i] == c { 295 return i 296 } 297 } 298 return -1 299 } 300 301 func IndexByte(s string, c byte) int { 302 return bytealg.IndexByteString(s, c) 303 } 304 305 func indexRabinKarp(s, substr string) int { 306 // Rabin-Karp search 307 hashss, pow := hashStr(substr) 308 n := len(substr) 309 var h uint32 310 for i := 0; i < n; i++ { 311 h = h*primeRK + uint32(s[i]) 312 } 313 if h == hashss && s[:n] == substr { 314 return 0 315 } 316 for i := n; i < len(s); { 317 h *= primeRK 318 h += uint32(s[i]) 319 h -= pow * uint32(s[i-n]) 320 i++ 321 if h == hashss && s[i-n:i] == substr { 322 return i - n 323 } 324 } 325 return -1 326 } 327 328 func hashStr(sep string) (uint32, uint32) { 329 hash := uint32(0) 330 for i := 0; i < len(sep); i++ { 331 hash = hash*primeRK + uint32(sep[i]) 332 } 333 var pow, sq uint32 = 1, primeRK 334 for i := len(sep); i > 0; i >>= 1 { 335 if i&1 != 0 { 336 pow *= sq 337 } 338 sq *= sq 339 } 340 return hash, pow 341 } 342 // primeRK is the prime base used in Rabin-Karp algorithm. 343 const primeRK = 16777619 344 345 // FormatInt returns the string representation of i in the given base, 346 // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' 347 // for digit values >= 10. 348 func FormatInt(i int64, base int) string { 349 if fastSmalls && 0 <= i && i < nSmalls && base == 10 { 350 return small(int(i)) 351 } 352 _, s := formatBits(nil, uint64(i), base, i < 0, false) 353 return s 354 } 355 356 // Itoa is equivalent to FormatInt(int64(i), 10). 357 func Itoa(i int) string { 358 return FormatInt(int64(i), 10) 359 } 360 361 // small returns the string for an i with 0 <= i < nSmalls. 362 func small(i int) string { 363 if i < 10 { 364 return digits[i : i+1] 365 } 366 return smallsString[i*2 : i*2+2] 367 } 368 369 // formatBits computes the string representation of u in the given base. 370 // If neg is set, u is treated as negative int64 value. If append_ is 371 // set, the string is appended to dst and the resulting byte slice is 372 // returned as the first result value; otherwise the string is returned 373 // as the second result value. 374 // 375 func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) { 376 if base < 2 || base > len(digits) { 377 panic("strconv: illegal AppendInt/FormatInt base") 378 } 379 // 2 <= base && base <= len(digits) 380 381 var a [64 + 1]byte // +1 for sign of 64bit value in base 2 382 i := len(a) 383 384 if neg { 385 u = -u 386 } 387 388 // convert bits 389 // We use uint values where we can because those will 390 // fit into a single register even on a 32bit machine. 391 if base == 10 { 392 // common case: use constants for / because 393 // the compiler can optimize it into a multiply+shift 394 395 if host32bit { 396 // convert the lower digits using 32bit operations 397 for u >= 1e9 { 398 // Avoid using r = a%b in addition to q = a/b 399 // since 64bit division and modulo operations 400 // are calculated by runtime functions on 32bit machines. 401 q := u / 1e9 402 us := uint(u - q*1e9) // u % 1e9 fits into a uint 403 for j := 4; j > 0; j-- { 404 is := us % 100 * 2 405 us /= 100 406 i -= 2 407 a[i+1] = smallsString[is+1] 408 a[i+0] = smallsString[is+0] 409 } 410 411 // us < 10, since it contains the last digit 412 // from the initial 9-digit us. 413 i-- 414 a[i] = smallsString[us*2+1] 415 416 u = q 417 } 418 // u < 1e9 419 } 420 421 // u guaranteed to fit into a uint 422 us := uint(u) 423 for us >= 100 { 424 is := us % 100 * 2 425 us /= 100 426 i -= 2 427 a[i+1] = smallsString[is+1] 428 a[i+0] = smallsString[is+0] 429 } 430 431 // us < 100 432 is := us * 2 433 i-- 434 a[i] = smallsString[is+1] 435 if us >= 10 { 436 i-- 437 a[i] = smallsString[is] 438 } 439 440 } else if isPowerOfTwo_int(base) { 441 // Use shifts and masks instead of / and %. 442 // Base is a power of 2 and 2 <= base <= len(digits) where len(digits) is 36. 443 // The largest power of 2 below or equal to 36 is 32, which is 1 << 5; 444 // i.e., the largest possible shift count is 5. By &-ind that value with 445 // the constant 7 we tell the compiler that the shift count is always 446 // less than 8 which is smaller than any register width. This allows 447 // the compiler to generate better code for the shift operation. 448 shift := uint(bits.TrailingZeros(uint(base))) & 7 449 b := uint64(base) 450 m := uint(base) - 1 // == 1<<shift - 1 451 for u >= b { 452 i-- 453 a[i] = digits[uint(u)&m] 454 u >>= shift 455 } 456 // u < base 457 i-- 458 a[i] = digits[uint(u)] 459 } else { 460 // general case 461 b := uint64(base) 462 for u >= b { 463 i-- 464 // Avoid using r = a%b in addition to q = a/b 465 // since 64bit division and modulo operations 466 // are calculated by runtime functions on 32bit machines. 467 q := u / b 468 a[i] = digits[uint(u-q*b)] 469 u = q 470 } 471 // u < base 472 i-- 473 a[i] = digits[uint(u)] 474 } 475 476 // add sign, if any 477 if neg { 478 i-- 479 a[i] = '-' 480 } 481 482 if append_ { 483 d = append(dst, a[i:]...) 484 return 485 } 486 s = string(a[i:]) 487 return 488 } 489 490 func isPowerOfTwo_int(x int) bool { 491 return x&(x-1) == 0 492 } 493 494 495 const fastSmalls = true 496 const nSmalls = 100 497 const smallsString = "00010203040506070809" + 498 "10111213141516171819" + 499 "20212223242526272829" + 500 "30313233343536373839" + 501 "40414243444546474849" + 502 "50515253545556575859" + 503 "60616263646566676869" + 504 "70717273747576777879" + 505 "80818283848586878889" + 506 "90919293949596979899" 507 const host32bit = ^uint(0)>>32 == 0 508 const digits = "0123456789abcdefghijklmnopqrstuvwxyz" 509 510 511 // Following two functions comes from https://stackoverflow.com/questions/35212985/is-it-possible-get-information-about-caller-function-in-golang 512 func getFrame(skipFrames int) Frame { 513 // We need the frame at index skipFrames+2, since we never want runtime.Callers and getFrame 514 targetFrameIndex := skipFrames + 2 515 516 // Set size to targetFrameIndex+2 to ensure we have room for one more caller than we need 517 programCounters := make([]uintptr, targetFrameIndex+2) 518 n := Callers(0, programCounters) 519 520 frame := Frame{Function: "unknown"} 521 if n > 0 { 522 frames := CallersFrames(programCounters[:n]) 523 for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ { 524 var frameCandidate Frame 525 frameCandidate, more = frames.Next() 526 if frameIndex == targetFrameIndex { 527 frame = frameCandidate 528 } 529 } 530 } 531 532 return frame 533 } 534 535 // MyCaller returns the caller of the function that called it 536 func MyCaller(skip int) string { 537 // Skip GetCallerFunctionName and the function to get the caller of 538 return getFrame(skip + 2).Function 539 } 540 541 func PrintCurrentStack() { 542 const size = 64 << 10 543 buf := make([]byte, size) 544 buf = buf[:Stack(buf, false)] 545 println(string(buf)) 546 }