...
1
2
3
4
5
6
7 package framepointer
8
9 import (
10 "go/build"
11 "regexp"
12 "strings"
13
14 "golang.org/x/tools/go/analysis"
15 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
16 )
17
18 const Doc = "report assembly that clobbers the frame pointer before saving it"
19
20 var Analyzer = &analysis.Analyzer{
21 Name: "framepointer",
22 Doc: Doc,
23 Run: run,
24 }
25
26 var (
27 re = regexp.MustCompile
28 asmWriteBP = re(`,\s*BP$`)
29 asmMentionBP = re(`\bBP\b`)
30 asmControlFlow = re(`^(J|RET)`)
31 )
32
33 func run(pass *analysis.Pass) (interface{}, error) {
34 if build.Default.GOARCH != "amd64" {
35 return nil, nil
36 }
37 if build.Default.GOOS != "linux" && build.Default.GOOS != "darwin" {
38 return nil, nil
39 }
40
41
42 var sfiles []string
43 for _, fname := range pass.OtherFiles {
44 if strings.HasSuffix(fname, ".s") && pass.Pkg.Path() != "runtime" {
45 sfiles = append(sfiles, fname)
46 }
47 }
48
49 for _, fname := range sfiles {
50 content, tf, err := analysisutil.ReadFile(pass.Fset, fname)
51 if err != nil {
52 return nil, err
53 }
54
55 lines := strings.SplitAfter(string(content), "\n")
56 active := false
57 for lineno, line := range lines {
58 lineno++
59
60
61 if i := strings.Index(line, "//"); i >= 0 {
62 line = line[:i]
63 }
64 line = strings.TrimSpace(line)
65
66
67 if strings.HasPrefix(line, "TEXT") && strings.Contains(line, "(SB)") && strings.Contains(line, "$0") {
68 active = true
69 continue
70 }
71 if !active {
72 continue
73 }
74
75 if asmWriteBP.MatchString(line) {
76 pass.Reportf(analysisutil.LineStart(tf, lineno), "frame pointer is clobbered before saving")
77 active = false
78 continue
79 }
80 if asmMentionBP.MatchString(line) {
81 active = false
82 continue
83 }
84 if asmControlFlow.MatchString(line) {
85 active = false
86 continue
87 }
88 }
89 }
90 return nil, nil
91 }
92
View as plain text