GoPLS Viewer

Home|gopls/go/analysis/passes/framepointer/framepointer.go
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.
7package framepointer
8
9import (
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
18const Doc = "report assembly that clobbers the frame pointer before saving it"
19
20var Analyzer = &analysis.Analyzer{
21    Name"framepointer",
22    Doc:  Doc,
23    Run:  run,
24}
25
26var (
27    re             = regexp.MustCompile
28    asmWriteBP     = re(`,\s*BP$`// TODO: can have false positive, e.g. for TESTQ BP,BP. Seems unlikely.
29    asmMentionBP   = re(`\bBP\b`)
30    asmControlFlow = re(`^(J|RET)`)
31)
32
33func run(pass *analysis.Pass) (interface{}, error) {
34    if build.Default.GOARCH != "amd64" { // TODO: arm64 also?
35        return nilnil
36    }
37    if build.Default.GOOS != "linux" && build.Default.GOOS != "darwin" {
38        return nilnil
39    }
40
41    // Find assembly files to work on.
42    var sfiles []string
43    for _fname := range pass.OtherFiles {
44        if strings.HasSuffix(fname".s") && pass.Pkg.Path() != "runtime" {
45            sfiles = append(sfilesfname)
46        }
47    }
48
49    for _fname := range sfiles {
50        contenttferr := analysisutil.ReadFile(pass.Fsetfname)
51        if err != nil {
52            return nilerr
53        }
54
55        lines := strings.SplitAfter(string(content), "\n")
56        active := false
57        for linenoline := range lines {
58            lineno++
59
60            // Ignore comments and commented-out code.
61            if i := strings.Index(line"//"); i >= 0 {
62                line = line[:i]
63            }
64            line = strings.TrimSpace(line)
65
66            // We start checking code at a TEXT line for a frameless function.
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) { // clobber of BP, function is not OK
76                pass.Reportf(analysisutil.LineStart(tflineno), "frame pointer is clobbered before saving")
77                active = false
78                continue
79            }
80            if asmMentionBP.MatchString(line) { // any other use of BP might be a read, so function is OK
81                active = false
82                continue
83            }
84            if asmControlFlow.MatchString(line) { // give up after any branch instruction
85                active = false
86                continue
87            }
88        }
89    }
90    return nilnil
91}
92
MembersX
run.RangeStmt_1112.fname
run.RangeStmt_1265.BlockStmt.content
run.RangeStmt_1265.BlockStmt.tf
run.RangeStmt_1265.BlockStmt.RangeStmt_1473.lineno
run.RangeStmt_1265.BlockStmt.RangeStmt_1473.BlockStmt.i
build
re
run.RangeStmt_1265.fname
run.RangeStmt_1265.BlockStmt.err
run.RangeStmt_1265.BlockStmt.active
run.RangeStmt_1265.BlockStmt.RangeStmt_1473.line
regexp
Doc
run.sfiles
run.RangeStmt_1265.BlockStmt.lines
analysis
analysisutil
run
run.pass
strings
Members
X