| 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. |
| 4 | |
| 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 |
| 10 | package atomicalignscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/atomicalign`/ |
| 11 | |
| 12 | import ( |
| 13 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 14 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 15 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 16 | |
| 17 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 18 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 19 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 20 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 21 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 22 | ) |
| 23 | |
| 24 | const Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/atomicalign`/Doc. = "check for non-64-bits-aligned arguments to sync/atomic functions" |
| 25 | |
| 26 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/atomicalign`/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#{ |
| 27 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "atomicalign", |
| 28 | 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/atomicalign`/Doc., |
| 29 | 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/atomicalign", |
| 30 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*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#{inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.}, |
| 31 | 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/atomicalign`/run()., |
| 32 | } |
| 33 | |
| 34 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/atomicalign`/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) { |
| 35 | if 8*pass.TypesSizesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesSizes..Sizeofscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Sizes#Sizeof().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Uintptrscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Uintptr.]) == 64 { |
| 36 | return nil, nil // 64-bit platform |
| 37 | } |
| 38 | if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/Imports().(pass.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg., "sync/atomic") { |
| 39 | return nil, nil // doesn't directly import sync/atomic |
| 40 | } |
| 41 | |
| 42 | inspect := pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.].(*inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) |
| 43 | nodeFilter := []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#{ |
| 44 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)(nil), |
| 45 | } |
| 46 | funcNames := []string{ |
| 47 | "AddInt64", "AddUint64", |
| 48 | "LoadInt64", "LoadUint64", |
| 49 | "StoreInt64", "StoreUint64", |
| 50 | "SwapInt64", "SwapUint64", |
| 51 | "CompareAndSwapInt64", "CompareAndSwapUint64", |
| 52 | } |
| 53 | |
| 54 | inspect.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().(nodeFilter, func(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) { |
| 55 | call := node.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 56 | obj := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., call) |
| 57 | if typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsFunctionNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsFunctionNamed().(obj, "sync/atomic", funcNames...) { |
| 58 | // For all the listed functions, the expression to check is always the first function argument. |
| 59 | check64BitAlignmentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/atomicalign`/check64BitAlignment().(pass, obj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0]) |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | return nil, nil |
| 64 | } |
| 65 | |
| 66 | func check64BitAlignmentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/atomicalign`/check64BitAlignment().(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#, funcName string, arg astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) { |
| 67 | // Checks the argument is made of the address operator (&) applied to |
| 68 | // a struct field (as opposed to a variable as the first word of |
| 69 | // uint64 and int64 variables can be relied upon to be 64-bit aligned). |
| 70 | unary, ok := arg.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.UnaryExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/UnaryExpr#) |
| 71 | if !ok || unary.Opscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/UnaryExpr#Op. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.ANDscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/AND. { |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | // Retrieve the types.Struct in order to get the offset of the |
| 76 | // atomically accessed field. |
| 77 | sel, ok := unary.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/UnaryExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 78 | if !ok { |
| 79 | return |
| 80 | } |
| 81 | tvar, ok := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Selectionsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Selections.[sel].Objscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Obj().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#) |
| 82 | if !ok || !tvar.IsFieldscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#IsField().() { |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | stype, ok := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[sel.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X.].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type..Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Structscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#) |
| 87 | if !ok { |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | var offset int64 |
| 92 | var fields []*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var# |
| 93 | for i := 0; i < stype.NumFieldsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#NumFields().(); i++ { |
| 94 | f := stype.Fieldscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#Field().(i) |
| 95 | fields = append(fields, f) |
| 96 | if f == tvar { |
| 97 | // We're done, this is the field we were looking for, |
| 98 | // no need to fill the fields slice further. |
| 99 | offset = pass.TypesSizesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesSizes..Offsetsofscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Sizes#Offsetsof().(fields)[i] |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | if offset&7 == 0 { |
| 104 | return // 64-bit aligned |
| 105 | } |
| 106 | |
| 107 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(arg, "address of non 64-bit aligned field .%s passed to atomic.%s", tvar.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().(), funcName) |
| 108 | } |