]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: vendor in x/tools, enable framepointer vet check
authorKeith Randall <khr@golang.org>
Tue, 8 Dec 2020 22:07:19 +0000 (14:07 -0800)
committerKeith Randall <khr@golang.org>
Tue, 8 Dec 2020 22:58:14 +0000 (22:58 +0000)
Vendor in latest x/tools.
Add framepointer vet check to vet.

Fixes #43014

Change-Id: Ife72f85b1261aa60c0028041c58040d60a40918a
Reviewed-on: https://go-review.googlesource.com/c/go/+/276372
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/go.mod
src/cmd/go.sum
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/framepointer/framepointer.go [new file with mode: 0644]
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/ifaceassert/ifaceassert.go
src/cmd/vendor/modules.txt
src/cmd/vet/main.go

index 98ef23d61b85799fc23f9434d53e9102edeafdbc..c7d43873efe821e202d5e97bc8b163b28f228741 100644 (file)
@@ -8,5 +8,5 @@ require (
        golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
        golang.org/x/mod v0.4.0
        golang.org/x/sys v0.0.0-20201204225414-ed752295db88 // indirect
-       golang.org/x/tools v0.0.0-20201110201400-7099162a900a
+       golang.org/x/tools v0.0.0-20201208211828-de58e7c01d49
 )
index 70f14f6640d080d8a6ad3031aa907a6df1d0685a..30edf77282df6d3f59e4abc8adc1dd95922ea168 100644 (file)
@@ -31,8 +31,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
-golang.org/x/tools v0.0.0-20201110201400-7099162a900a h1:5E6TPwSBG74zT8xSrVc8W59K4ch4NFobVTnh2BYzHyU=
-golang.org/x/tools v0.0.0-20201110201400-7099162a900a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+golang.org/x/tools v0.0.0-20201208211828-de58e7c01d49 h1:K1QAOVIWIvmQ66F1Z3AEa9Wzp0bj+xU3YzLkvROk2Ds=
+golang.org/x/tools v0.0.0-20201208211828-de58e7c01d49/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
diff --git a/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/framepointer/framepointer.go b/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/framepointer/framepointer.go
new file mode 100644 (file)
index 0000000..741492e
--- /dev/null
@@ -0,0 +1,91 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package framepointer defines an Analyzer that reports assembly code
+// that clobbers the frame pointer before saving it.
+package framepointer
+
+import (
+       "go/build"
+       "regexp"
+       "strings"
+
+       "golang.org/x/tools/go/analysis"
+       "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
+)
+
+const Doc = "report assembly that clobbers the frame pointer before saving it"
+
+var Analyzer = &analysis.Analyzer{
+       Name: "framepointer",
+       Doc:  Doc,
+       Run:  run,
+}
+
+var (
+       re             = regexp.MustCompile
+       asmWriteBP     = re(`,\s*BP$`) // TODO: can have false positive, e.g. for TESTQ BP,BP. Seems unlikely.
+       asmMentionBP   = re(`\bBP\b`)
+       asmControlFlow = re(`^(J|RET)`)
+)
+
+func run(pass *analysis.Pass) (interface{}, error) {
+       if build.Default.GOARCH != "amd64" { // TODO: arm64 also?
+               return nil, nil
+       }
+       if build.Default.GOOS != "linux" && build.Default.GOOS != "darwin" {
+               return nil, nil
+       }
+
+       // Find assembly files to work on.
+       var sfiles []string
+       for _, fname := range pass.OtherFiles {
+               if strings.HasSuffix(fname, ".s") && pass.Pkg.Path() != "runtime" {
+                       sfiles = append(sfiles, fname)
+               }
+       }
+
+       for _, fname := range sfiles {
+               content, tf, err := analysisutil.ReadFile(pass.Fset, fname)
+               if err != nil {
+                       return nil, err
+               }
+
+               lines := strings.SplitAfter(string(content), "\n")
+               active := false
+               for lineno, line := range lines {
+                       lineno++
+
+                       // Ignore comments and commented-out code.
+                       if i := strings.Index(line, "//"); i >= 0 {
+                               line = line[:i]
+                       }
+                       line = strings.TrimSpace(line)
+
+                       // We start checking code at a TEXT line for a frameless function.
+                       if strings.HasPrefix(line, "TEXT") && strings.Contains(line, "(SB)") && strings.Contains(line, "$0") {
+                               active = true
+                               continue
+                       }
+                       if !active {
+                               continue
+                       }
+
+                       if asmWriteBP.MatchString(line) { // clobber of BP, function is not OK
+                               pass.Reportf(analysisutil.LineStart(tf, lineno), "frame pointer is clobbered before saving")
+                               active = false
+                               continue
+                       }
+                       if asmMentionBP.MatchString(line) { // any other use of BP might be a read, so function is OK
+                               active = false
+                               continue
+                       }
+                       if asmControlFlow.MatchString(line) { // give up after any branch instruction
+                               active = false
+                               continue
+                       }
+               }
+       }
+       return nil, nil
+}
index c5a71a7c570beacb348a5be39a8868980feb158b..fd2285332ccec2dc6cf503b1e7847ca873af74c6 100644 (file)
@@ -41,6 +41,10 @@ var Analyzer = &analysis.Analyzer{
 // assertableTo checks whether interface v can be asserted into t. It returns
 // nil on success, or the first conflicting method on failure.
 func assertableTo(v, t types.Type) *types.Func {
+       if t == nil || v == nil {
+               // not assertable to, but there is no missing method
+               return nil
+       }
        // ensure that v and t are interfaces
        V, _ := v.Underlying().(*types.Interface)
        T, _ := t.Underlying().(*types.Interface)
index cda5fd0556cffeae0dafdf6f9063e5616c7e6c37..b549258cfa28c51e59e5db37593e847be66bc65e 100644 (file)
@@ -44,7 +44,7 @@ golang.org/x/mod/zip
 golang.org/x/sys/internal/unsafeheader
 golang.org/x/sys/unix
 golang.org/x/sys/windows
-# golang.org/x/tools v0.0.0-20201110201400-7099162a900a
+# golang.org/x/tools v0.0.0-20201208211828-de58e7c01d49
 ## explicit
 golang.org/x/tools/go/analysis
 golang.org/x/tools/go/analysis/internal/analysisflags
@@ -59,6 +59,7 @@ golang.org/x/tools/go/analysis/passes/composite
 golang.org/x/tools/go/analysis/passes/copylock
 golang.org/x/tools/go/analysis/passes/ctrlflow
 golang.org/x/tools/go/analysis/passes/errorsas
+golang.org/x/tools/go/analysis/passes/framepointer
 golang.org/x/tools/go/analysis/passes/httpresponse
 golang.org/x/tools/go/analysis/passes/ifaceassert
 golang.org/x/tools/go/analysis/passes/inspect
index bad3807039d9bcc0e81c5133b3a9b6815c961307..d50c45d691ee82fa64bb8cf110f68ed3f19c2380 100644 (file)
@@ -14,6 +14,7 @@ import (
        "golang.org/x/tools/go/analysis/passes/composite"
        "golang.org/x/tools/go/analysis/passes/copylock"
        "golang.org/x/tools/go/analysis/passes/errorsas"
+       "golang.org/x/tools/go/analysis/passes/framepointer"
        "golang.org/x/tools/go/analysis/passes/httpresponse"
        "golang.org/x/tools/go/analysis/passes/ifaceassert"
        "golang.org/x/tools/go/analysis/passes/loopclosure"
@@ -45,6 +46,7 @@ func main() {
                composite.Analyzer,
                copylock.Analyzer,
                errorsas.Analyzer,
+               framepointer.Analyzer,
                httpresponse.Analyzer,
                ifaceassert.Analyzer,
                loopclosure.Analyzer,