runtime: make use of stringslite.{HasPrefix, HasSuffix}

Change-Id: I7461a892e1591e3bad876f0a718a99e6de2c4659
Reviewed-on: https://go-review.googlesource.com/c/go/+/585435
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Jes Cok 2024-05-15 02:37:41 +08:00 committed by Michael Knyszek
parent d11e417285
commit 2c635b68fd
11 changed files with 33 additions and 28 deletions

View file

@ -58,6 +58,7 @@ var runtimePkgs = []string{
"internal/godebugs", "internal/godebugs",
"internal/goexperiment", "internal/goexperiment",
"internal/goos", "internal/goos",
"internal/stringslite",
} }
// extraNoInstrumentPkgs is the set of packages in addition to runtimePkgs that // extraNoInstrumentPkgs is the set of packages in addition to runtimePkgs that

View file

@ -8,6 +8,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/stringslite"
"unsafe" "unsafe"
) )
@ -145,7 +146,7 @@ func (h *debugCallHandler) handle(info *siginfo, ctxt *sigctxt, gp2 *g) bool {
return false return false
} }
f := findfunc(ctxt.sigpc()) f := findfunc(ctxt.sigpc())
if !(hasPrefix(funcname(f), "runtime.debugCall") || hasPrefix(funcname(f), "debugCall")) { if !(stringslite.HasPrefix(funcname(f), "runtime.debugCall") || stringslite.HasPrefix(funcname(f), "debugCall")) {
println("trap in unknown function", funcname(f)) println("trap in unknown function", funcname(f))
return false return false
} }

View file

@ -7,6 +7,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/goarch" "internal/goarch"
"internal/stringslite"
"unsafe" "unsafe"
) )
@ -47,7 +48,7 @@ func sighandler(_ureg *ureg, note *byte, gp *g) int {
// level by the program but will otherwise be ignored. // level by the program but will otherwise be ignored.
flags = _SigNotify flags = _SigNotify
for sig, t = range sigtable { for sig, t = range sigtable {
if hasPrefix(notestr, t.name) { if stringslite.HasPrefix(notestr, t.name) {
flags = t.flags flags = t.flags
break break
} }

View file

@ -7,6 +7,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/runtime/atomic" "internal/runtime/atomic"
"internal/stringslite"
"unsafe" "unsafe"
) )
@ -124,7 +125,7 @@ func indexNoFloat(s, t string) int {
return 0 return 0
} }
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if s[i] == t[0] && hasPrefix(s[i:], t) { if s[i] == t[0] && stringslite.HasPrefix(s[i:], t) {
return i return i
} }
} }
@ -132,20 +133,20 @@ func indexNoFloat(s, t string) int {
} }
func atolwhex(p string) int64 { func atolwhex(p string) int64 {
for hasPrefix(p, " ") || hasPrefix(p, "\t") { for stringslite.HasPrefix(p, " ") || stringslite.HasPrefix(p, "\t") {
p = p[1:] p = p[1:]
} }
neg := false neg := false
if hasPrefix(p, "-") || hasPrefix(p, "+") { if stringslite.HasPrefix(p, "-") || stringslite.HasPrefix(p, "+") {
neg = p[0] == '-' neg = p[0] == '-'
p = p[1:] p = p[1:]
for hasPrefix(p, " ") || hasPrefix(p, "\t") { for stringslite.HasPrefix(p, " ") || stringslite.HasPrefix(p, "\t") {
p = p[1:] p = p[1:]
} }
} }
var n int64 var n int64
switch { switch {
case hasPrefix(p, "0x"), hasPrefix(p, "0X"): case stringslite.HasPrefix(p, "0x"), stringslite.HasPrefix(p, "0X"):
p = p[2:] p = p[2:]
for ; len(p) > 0; p = p[1:] { for ; len(p) > 0; p = p[1:] {
if '0' <= p[0] && p[0] <= '9' { if '0' <= p[0] && p[0] <= '9' {
@ -158,7 +159,7 @@ func atolwhex(p string) int64 {
break break
} }
} }
case hasPrefix(p, "0"): case stringslite.HasPrefix(p, "0"):
for ; len(p) > 0 && '0' <= p[0] && p[0] <= '7'; p = p[1:] { for ; len(p) > 0 && '0' <= p[0] && p[0] <= '7'; p = p[1:] {
n = n*8 + int64(p[0]-'0') n = n*8 + int64(p[0]-'0')
} }

View file

@ -8,6 +8,7 @@ import (
"internal/abi" "internal/abi"
"internal/goarch" "internal/goarch"
"internal/runtime/atomic" "internal/runtime/atomic"
"internal/stringslite"
"runtime/internal/sys" "runtime/internal/sys"
"unsafe" "unsafe"
) )
@ -53,7 +54,7 @@ const (
// pc should be the program counter of the compiler-generated code that // pc should be the program counter of the compiler-generated code that
// triggered this panic. // triggered this panic.
func panicCheck1(pc uintptr, msg string) { func panicCheck1(pc uintptr, msg string) {
if goarch.IsWasm == 0 && hasPrefix(funcname(findfunc(pc)), "runtime.") { if goarch.IsWasm == 0 && stringslite.HasPrefix(funcname(findfunc(pc)), "runtime.") {
// Note: wasm can't tail call, so we can't get the original caller's pc. // Note: wasm can't tail call, so we can't get the original caller's pc.
throw(msg) throw(msg)
} }

View file

@ -55,6 +55,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/goarch" "internal/goarch"
"internal/stringslite"
) )
type suspendGState struct { type suspendGState struct {
@ -416,9 +417,9 @@ func isAsyncSafePoint(gp *g, pc, sp, lr uintptr) (bool, uintptr) {
// Check the inner-most name // Check the inner-most name
u, uf := newInlineUnwinder(f, pc) u, uf := newInlineUnwinder(f, pc)
name := u.srcFunc(uf).name() name := u.srcFunc(uf).name()
if hasPrefix(name, "runtime.") || if stringslite.HasPrefix(name, "runtime.") ||
hasPrefix(name, "runtime/internal/") || stringslite.HasPrefix(name, "runtime/internal/") ||
hasPrefix(name, "reflect.") { stringslite.HasPrefix(name, "reflect.") {
// For now we never async preempt the runtime or // For now we never async preempt the runtime or
// anything closely tied to the runtime. Known issues // anything closely tied to the runtime. Known issues
// include: various points in the scheduler ("don't // include: various points in the scheduler ("don't

View file

@ -10,6 +10,7 @@ import (
"internal/goarch" "internal/goarch"
"internal/goos" "internal/goos"
"internal/runtime/atomic" "internal/runtime/atomic"
"internal/stringslite"
"runtime/internal/sys" "runtime/internal/sys"
"unsafe" "unsafe"
) )
@ -729,7 +730,7 @@ func getGodebugEarly() string {
p := argv_index(argv, argc+1+i) p := argv_index(argv, argc+1+i)
s := unsafe.String(p, findnull(p)) s := unsafe.String(p, findnull(p))
if hasPrefix(s, prefix) { if stringslite.HasPrefix(s, prefix) {
env = gostring(p)[len(prefix):] env = gostring(p)[len(prefix):]
break break
} }
@ -5268,7 +5269,7 @@ func sigprof(pc, sp, lr uintptr, gp *g, mp *m) {
// received from somewhere else (with _LostSIGPROFDuringAtomic64 as pc). // received from somewhere else (with _LostSIGPROFDuringAtomic64 as pc).
if GOARCH == "mips" || GOARCH == "mipsle" || GOARCH == "arm" { if GOARCH == "mips" || GOARCH == "mipsle" || GOARCH == "arm" {
if f := findfunc(pc); f.valid() { if f := findfunc(pc); f.valid() {
if hasPrefix(funcname(f), "internal/runtime/atomic") { if stringslite.HasPrefix(funcname(f), "internal/runtime/atomic") {
cpuprof.lostAtomic++ cpuprof.lostAtomic++
return return
} }

View file

@ -6,6 +6,10 @@
package runtime package runtime
import (
"internal/stringslite"
)
func secure() { func secure() {
initSecureMode() initSecureMode()
@ -25,7 +29,7 @@ func secure() {
func secureEnv() { func secureEnv() {
var hasTraceback bool var hasTraceback bool
for i := 0; i < len(envs); i++ { for i := 0; i < len(envs); i++ {
if hasPrefix(envs[i], "GOTRACEBACK=") { if stringslite.HasPrefix(envs[i], "GOTRACEBACK=") {
hasTraceback = true hasTraceback = true
envs[i] = "GOTRACEBACK=none" envs[i] = "GOTRACEBACK=none"
} }

View file

@ -341,14 +341,6 @@ func gostringn(p *byte, l int) string {
return s return s
} }
func hasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}
func hasSuffix(s, suffix string) bool {
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}
const ( const (
maxUint64 = ^uint64(0) maxUint64 = ^uint64(0)
maxInt64 = int64(maxUint64 >> 1) maxInt64 = int64(maxUint64 >> 1)

View file

@ -6,6 +6,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/stringslite"
"runtime/internal/sys" "runtime/internal/sys"
) )
@ -50,7 +51,7 @@ func XTestInlineUnwinder(t TestingT) {
for ; uf.valid(); uf = u.next(uf) { for ; uf.valid(); uf = u.next(uf) {
file, line := u.fileLine(uf) file, line := u.fileLine(uf)
const wantFile = "symtabinl_test.go" const wantFile = "symtabinl_test.go"
if !hasSuffix(file, wantFile) { if !stringslite.HasSuffix(file, wantFile) {
t.Errorf("tiuTest+%#x: want file ...%s, got %s", pc-pc1, wantFile, file) t.Errorf("tiuTest+%#x: want file ...%s, got %s", pc-pc1, wantFile, file)
} }
@ -58,10 +59,10 @@ func XTestInlineUnwinder(t TestingT) {
name := sf.name() name := sf.name()
const namePrefix = "runtime." const namePrefix = "runtime."
if hasPrefix(name, namePrefix) { if stringslite.HasPrefix(name, namePrefix) {
name = name[len(namePrefix):] name = name[len(namePrefix):]
} }
if !hasPrefix(name, "tiu") { if !stringslite.HasPrefix(name, "tiu") {
t.Errorf("tiuTest+%#x: unexpected function %s", pc-pc1, name) t.Errorf("tiuTest+%#x: unexpected function %s", pc-pc1, name)
} }

View file

@ -8,6 +8,7 @@ import (
"internal/abi" "internal/abi"
"internal/bytealg" "internal/bytealg"
"internal/goarch" "internal/goarch"
"internal/stringslite"
"runtime/internal/sys" "runtime/internal/sys"
"unsafe" "unsafe"
) )
@ -1131,7 +1132,7 @@ func showfuncinfo(sf srcFunc, firstFrame bool, calleeID abi.FuncID) bool {
return true return true
} }
return bytealg.IndexByteString(name, '.') >= 0 && (!hasPrefix(name, "runtime.") || isExportedRuntime(name)) return bytealg.IndexByteString(name, '.') >= 0 && (!stringslite.HasPrefix(name, "runtime.") || isExportedRuntime(name))
} }
// isExportedRuntime reports whether name is an exported runtime function. // isExportedRuntime reports whether name is an exported runtime function.
@ -1342,7 +1343,7 @@ func isSystemGoroutine(gp *g, fixed bool) bool {
} }
return fingStatus.Load()&fingRunningFinalizer == 0 return fingStatus.Load()&fingRunningFinalizer == 0
} }
return hasPrefix(funcname(f), "runtime.") return stringslite.HasPrefix(funcname(f), "runtime.")
} }
// SetCgoTraceback records three C functions to use to gather // SetCgoTraceback records three C functions to use to gather