| 1 | // Copyright 2020 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 framepointer defines an Analyzer that reports assembly code |
| 6 | // that clobbers the frame pointer before saving it. |
| 7 | package framepointerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/ |
| 8 | |
| 9 | import ( |
| 10 | "go/buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/" |
| 11 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/" |
| 14 | |
| 15 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 16 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 17 | ) |
| 18 | |
| 19 | const Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/Doc. = "report assembly that clobbers the frame pointer before saving it" |
| 20 | |
| 21 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/Analyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 22 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "framepointer", |
| 23 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/Doc., |
| 24 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/framepointer", |
| 25 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/run()., |
| 26 | } |
| 27 | |
| 28 | // Per-architecture checks for instructions. |
| 29 | // Assume comments, leading and trailing spaces are removed. |
| 30 | type archscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch# struct { |
| 31 | isFPWritescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPWrite. func(string) bool |
| 32 | isFPReadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPRead. func(string) bool |
| 33 | isUnconditionalBranchscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isUnconditionalBranch. func(string) bool |
| 34 | } |
| 35 | |
| 36 | var rescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/re. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile(). |
| 37 | |
| 38 | func hasAnyPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/hasAnyPrefix().(s string, prefixes ...string) bool { |
| 39 | for _, p := range prefixes { |
| 40 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(s, p) { |
| 41 | return true |
| 42 | } |
| 43 | } |
| 44 | return false |
| 45 | } |
| 46 | |
| 47 | var archesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches. = map[string]archscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#{ |
| 48 | "amd64": { |
| 49 | isFPWritescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPWrite.: rescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/re.(`,\s*BP$`).MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString()., // TODO: can have false positive, e.g. for TESTQ BP,BP. Seems unlikely. |
| 50 | isFPReadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPRead.: rescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/re.(`\bBP\b`).MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString()., |
| 51 | isUnconditionalBranchscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isUnconditionalBranch.: func(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. string) bool { |
| 52 | return hasAnyPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/hasAnyPrefix().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s., "JMP", "RET") |
| 53 | }, |
| 54 | }, |
| 55 | "arm64": { |
| 56 | isFPWritescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPWrite.: func(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. string) bool { |
| 57 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s., ","); i > 0 && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s.[i:], "R29") { |
| 58 | return true |
| 59 | } |
| 60 | if hasAnyPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/hasAnyPrefix().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s., "LDP", "LDAXP", "LDXP", "CASP") { |
| 61 | // Instructions which write to a pair of registers, e.g. |
| 62 | // LDP 8(R0), (R26, R29) |
| 63 | // CASPD (R2, R3), (R2), (R26, R29) |
| 64 | lp := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s., "(") |
| 65 | rp := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s., ")") |
| 66 | if lp > -1 && lp < rp { |
| 67 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s.[lp:rp], ",") && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s.[lp:rp], "R29") |
| 68 | } |
| 69 | } |
| 70 | return false |
| 71 | }, |
| 72 | isFPReadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPRead.: rescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/re.(`\bR29\b`).MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString()., |
| 73 | isUnconditionalBranchscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isUnconditionalBranch.: func(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. string) bool { |
| 74 | // Get just the instruction |
| 75 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexFuncscip-go gomod github.com/golang/go/src go1.25.0 strings/IndexFunc().(sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s., unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsSpacescip-go gomod github.com/golang/go/src go1.25.0 unicode/IsSpace().); i > 0 { |
| 76 | sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. = sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s.[:i] |
| 77 | } |
| 78 | return sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. == "B" || sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. == "JMP" || sscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches:s. == "RET" |
| 79 | }, |
| 80 | }, |
| 81 | } |
| 82 | |
| 83 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/run().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 84 | arch, ok := archesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arches.[buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Default..GOARCHscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOARCH.] |
| 85 | if !ok { |
| 86 | return nil, nil |
| 87 | } |
| 88 | if buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Default..GOOSscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOOS. != "linux" && buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Default..GOOSscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOOS. != "darwin" { |
| 89 | return nil, nil |
| 90 | } |
| 91 | |
| 92 | // Find assembly files to work on. |
| 93 | var sfiles []string |
| 94 | for _, fname := range pass.OtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#OtherFiles. { |
| 95 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(fname, ".s") && pass.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg..Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().() != "runtime" { |
| 96 | sfiles = append(sfiles, fname) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | for _, fname := range sfiles { |
| 101 | content, tf, err := analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.ReadFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/ReadFile().(pass, fname) |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | |
| 106 | lines := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitAfterscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitAfter().(string(content), "\n") |
| 107 | active := false |
| 108 | for lineno, line := range lines { |
| 109 | lineno++ |
| 110 | |
| 111 | // Ignore comments and commented-out code. |
| 112 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(line, "//"); i >= 0 { |
| 113 | line = line[:i] |
| 114 | } |
| 115 | line = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(line) |
| 116 | if line == "" { |
| 117 | continue |
| 118 | } |
| 119 | |
| 120 | // We start checking code at a TEXT line for a frameless function. |
| 121 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(line, "TEXT") && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(line, "(SB)") && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(line, "$0") { |
| 122 | active = true |
| 123 | continue |
| 124 | } |
| 125 | if !active { |
| 126 | continue |
| 127 | } |
| 128 | |
| 129 | if arch.isFPWritescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPWrite.(line) { |
| 130 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(tf.LineStartscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineStart().(lineno), "frame pointer is clobbered before saving") |
| 131 | active = false |
| 132 | continue |
| 133 | } |
| 134 | if arch.isFPReadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isFPRead.(line) || arch.isUnconditionalBranchscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/framepointer`/arch#isUnconditionalBranch.(line) { |
| 135 | active = false |
| 136 | continue |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return nil, nil |
| 141 | } |