gvisor/tools/checklocks
Adin Scannell 16b751b6c6 Mix checklocks and atomic analyzers.
This change makes the checklocks analyzer considerable more powerful, adding:
* The ability to traverse complex structures, e.g. to have multiple nested
  fields as part of the annotation.
* The ability to resolve simple anonymous functions and closures, and perform
  lock analysis across these invocations. This does not apply to closures that
  are passed elsewhere, since it is not possible to know the context in which
  they might be invoked.
* The ability to annotate return values in addition to receivers and other
  parameters, with the same complex structures noted above.
* Ignoring locking semantics for "fresh" objects, i.e. objects that are
  allocated in the local frame (typically a new-style function).
* Sanity checking of locking state across block transitions and returns, to
  ensure that no unexpected locks are held.

Note that initially, most of these findings are excluded by a comprehensive
nogo.yaml. The findings that are included are fundamental lock violations.
The changes here should be relatively low risk, minor refactorings to either
include necessary annotations to simplify the code structure (in general
removing closures in favor of methods) so that the analyzer can be easily
track the lock state.

This change additional includes two changes to nogo itself:
* Sanity checking of all types to ensure that the binary and ast-derived
  types have a consistent objectpath, to prevent the bug above from occurring
  silently (and causing much confusion). This also requires a trick in
  order to ensure that serialized facts are consumable downstream. This can
  be removed with https://go-review.googlesource.com/c/tools/+/331789 merged.
* A minor refactoring to isolation the objdump settings in its own package.
  This was originally used to implement the sanity check above, but this
  information is now being passed another way. The minor refactor is preserved
  however, since it cleans up the code slightly and is minimal risk.

PiperOrigin-RevId: 382613300
2021-07-01 15:07:56 -07:00
..
test Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
BUILD Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
README.md Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
analysis.go Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
annotations.go Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
checklocks.go Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
facts.go Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00
state.go Mix checklocks and atomic analyzers. 2021-07-01 15:07:56 -07:00

README.md

CheckLocks Analyzer

Checklocks is an analyzer for lock and atomic constraints. The analyzer relies on explicit annotations to identify fields that should be checked for access.

Atomic annotations

Individual struct members may be noted as requiring atomic access. These annotations are of the form:

type foo struct {
  // +checkatomic
  bar int32
}

This will ensure that all accesses to bar are atomic, with the exception of operations on newly allocated objects.

Lock annotations

Individual struct members may be protected by annotations that indicate locking requirements for accessing members. These annotations are of the form:

type foo struct {
    mu sync.Mutex
    // +checklocks:mu
    bar int

    foo int  // No annotation on foo means it's not guarded by mu.

    secondMu sync.Mutex

    // Multiple annotations indicate that both must be held but the
    // checker does not assert any lock ordering.
    // +checklocks:secondMu
    // +checklocks:mu
    foobar int
}

The checklocks annotation may also apply to functions. For example:

// +checklocks:f.mu
func (f *foo) doThingLocked() { }

This will check that the "f.mu" is locked for any calls, where possible.

In case of functions which initialize structs that may have annotations one can use the following annotation on the function to disable reporting by the lock checker. The lock checker will still track any mutexes acquired or released but won't report any failures for this function for unguarded field access.

// +checklocks:ignore
func newXXX() *X {
...
}

The checker treats both 'sync.Mutex' and 'sync.RWMutex' identically, i.e, as a sync.Mutex. The checker does not distinguish between read locks vs. exclusive locks and treats all locks as exclusive locks.

For cases the checker is able to correctly handle today please see test/test.go.

The checklocks check also flags any invalid annotations where the mutex annotation refers either to something that is not a 'sync.Mutex' or 'sync.RWMutex' or where the field does not exist at all. This will prevent the annotations from becoming stale over time as fields are renamed, etc.

Currently not supported

  1. Anonymous functions are not correctly evaluated. The analyzer does not currently support specifying annotations on anonymous functions as a result evaluation of a function that accesses protected fields will fail.
type A struct {
  mu sync.Mutex

  // +checklocks:mu
  x int
}

func abc() {
  var a A
  f := func()  { a.x = 1 }  <=== This line will be flagged by analyzer
  a.mu.Lock()
  f()
  a.mu.Unlock()
}

Explicitly Not Supported

  1. Checking for embedded mutexes as sync.Locker rather than directly as 'sync.Mutex'. In other words, the checker will not track mutex Lock and Unlock() methods where the mutex is behind an interface dispatch.

An example that we won't handle is shown below (this in fact will fail to build):

type A struct {
  mu sync.Locker

  // +checklocks:mu
  x int
}

func abc() {
   mu sync.Mutex
   a := A{mu: &mu}
   a.x = 1 // This won't be flagged by copylocks checker.
}

  1. The checker will not support guards on anything other than the cases described above. For example, global mutexes cannot be referred to by checklocks. Only struct members can be used.

  2. The checker will not support checking for lock ordering violations.

Mixed mode

Some members may allow read-only atomic access, but be protected against writes by a mutex. Generally, this imposes the following requirements:

For a read, one of the following must be true:

  1. A lock held be held.
  2. The access is atomic.

For a write, both of the following must be true:

  1. The lock must be held.
  2. The write must be atomic.

In order to annotate a relevant field, simply apply both annotations from above. For example:

type foo struct {
  mu sync.Mutex
  // +checklocks:mu
  // +checkatomic
  bar int32
}