1 // Copyright 2019 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.
5 // Package atomicalign defines an Analyzer that checks for non-64-bit-aligned
6 // arguments to sync/atomic functions. On non-32-bit platforms, those functions
7 // panic if their argument variables are not 64-bit aligned. It is therefore
8 // the caller's responsibility to arrange for 64-bit alignment of such variables.
9 // See https://golang.org/pkg/sync/atomic/#pkg-note-BUG
17 "golang.org/x/tools/go/analysis"
18 "golang.org/x/tools/go/analysis/passes/inspect"
19 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
20 "golang.org/x/tools/go/ast/inspector"
23 const Doc = "check for non-64-bits-aligned arguments to sync/atomic functions"
25 var Analyzer = &analysis.Analyzer{
28 Requires: []*analysis.Analyzer{inspect.Analyzer},
32 func run(pass *analysis.Pass) (interface{}, error) {
33 if 8*pass.TypesSizes.Sizeof(types.Typ[types.Uintptr]) == 64 {
34 return nil, nil // 64-bit platform
36 if !analysisutil.Imports(pass.Pkg, "sync/atomic") {
37 return nil, nil // doesn't directly import sync/atomic
40 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
41 nodeFilter := []ast.Node{
45 inspect.Preorder(nodeFilter, func(node ast.Node) {
46 call := node.(*ast.CallExpr)
47 sel, ok := call.Fun.(*ast.SelectorExpr)
51 pkgIdent, ok := sel.X.(*ast.Ident)
55 pkgName, ok := pass.TypesInfo.Uses[pkgIdent].(*types.PkgName)
56 if !ok || pkgName.Imported().Path() != "sync/atomic" {
61 case "AddInt64", "AddUint64",
62 "LoadInt64", "LoadUint64",
63 "StoreInt64", "StoreUint64",
64 "SwapInt64", "SwapUint64",
65 "CompareAndSwapInt64", "CompareAndSwapUint64":
67 // For all the listed functions, the expression to check is always the first function argument.
68 check64BitAlignment(pass, sel.Sel.Name, call.Args[0])
75 func check64BitAlignment(pass *analysis.Pass, funcName string, arg ast.Expr) {
76 // Checks the argument is made of the address operator (&) applied to
77 // to a struct field (as opposed to a variable as the first word of
78 // uint64 and int64 variables can be relied upon to be 64-bit aligned.
79 unary, ok := arg.(*ast.UnaryExpr)
80 if !ok || unary.Op != token.AND {
84 // Retrieve the types.Struct in order to get the offset of the
85 // atomically accessed field.
86 sel, ok := unary.X.(*ast.SelectorExpr)
90 tvar, ok := pass.TypesInfo.Selections[sel].Obj().(*types.Var)
91 if !ok || !tvar.IsField() {
95 stype, ok := pass.TypesInfo.Types[sel.X].Type.Underlying().(*types.Struct)
101 var fields []*types.Var
102 for i := 0; i < stype.NumFields(); i++ {
104 fields = append(fields, f)
106 // We're done, this is the field we were looking for,
107 // no need to fill the fields slice further.
108 offset = pass.TypesSizes.Offsetsof(fields)[i]
113 return // 64-bit aligned
116 pass.ReportRangef(arg, "address of non 64-bit aligned field .%s passed to atomic.%s", tvar.Name(), funcName)