12 Commits
Author SHA1 Message Date
Andrei Vagin f010ae01ac Fix a few typos 2025-01-29 21:16:51 -08:00
Andrew Dunham e3db6f0c3a tools/checklocks: support field comments for guard specifications
This allows specifying guards in the format:

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

This syntax, while more limited, makes writing lock guards less verbose
for structures where field documentation isn't attached to each field.
For example, being able to write something like the following with field
comments reduces the number of lines in the struct by >25% and is, in my
opinion, easier to read.

    type widget struct {
        mu sync.Mutex // guards following

        // The following counters track [...]

        success  int // +checklocks:mu
        failure  int // +checklocks:mu
        retries  int // +checklocks:mu
        timeouts int // +checklocks:mu
    }

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
2024-06-26 13:58:40 -04:00
Jarek Kowalski 1b02da0f3e checklocks: always allow calls to methods of atomic wrappers
This allows the use of wrappers such as atomic.Int32 introduced in
Go 1.19 without triggering the `unexpected call to atomic function`.
2023-08-21 18:58:18 -07:00
Adin ScannellandgVisor bot 1ceb814544 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Adin ScannellandgVisor bot 1ff543e17e Handle cross-package global guards.
Updates #7721

PiperOrigin-RevId: 455029306
2022-06-14 21:23:28 -07:00
Kevin KrakauerandgVisor bot 39790bd3a1 switch remaining sync/atomic to atomicbitops for 32 bit values
PiperOrigin-RevId: 443571047
2022-04-21 22:27:05 -07:00
Adin ScannellandgVisor bot 80cba65bd8 Add automatic lock inference and globals support.
Lock inference will apply annotations to all fields that seem to be
protected. This is currently disabled for all code by default, but it
can be enabled as annotations are applied more broadly.

PiperOrigin-RevId: 407501915
2021-11-03 22:17:30 -07:00
Adin ScannellandgVisor bot 7551b0590d Minor checklocks improvements.
* Support sync.Locker.
* Prevent runaway recursion when locks are acquired in a loop.
* Allowing ignoring of anonymous functions (inherited from parent function).
* Add support for aliases.

PiperOrigin-RevId: 407221521
2021-11-02 18:04:26 -07:00
Adin ScannellandgVisor bot 4f6cda4d0e Support distinction for RWMutex and read-only locks.
Fixes #6590

PiperOrigin-RevId: 404007524
2021-10-18 11:09:39 -07:00
Adin ScannellandgVisor bot 6bcacb2fd1 Support anonymous structs in checklocks.
Fixes #6558

PiperOrigin-RevId: 396393293
2021-09-13 10:54:24 -07:00
Adin ScannellandgVisor bot 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
Bhasker HariharanandgVisor bot 3e69f5d088 Add checklocks analyzer.
This validates that struct fields if annotated with "// checklocks:mu" where
"mu" is a mutex field in the same struct then access to the field is only
done with "mu" locked.

All types that are guarded by a mutex must be annotated with

// +checklocks:<mutex field name>

For more details please refer to README.md.

PiperOrigin-RevId: 360729328
2021-03-03 12:24:21 -08:00