Commit Graph
20 Commits
Author SHA1 Message Date
Jarek Kowalski 79d20045de 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 2911d8c3b8 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Adin ScannellandgVisor bot 6236f90e3a Fix type assertion failures with latest tools packages.
PiperOrigin-RevId: 511564602
2023-02-22 12:23:43 -08:00
Adin ScannellandgVisor bot 234d1c6a9f Move goid to dynamic facts render.
This removes the need for ongoing tags.

This change requires some minor updates to remove dependency cycles, since
the goid package is a base library used by many internals (log, sync, etc.).

PiperOrigin-RevId: 504066914
2023-01-23 13:29:37 -08:00
Kevin KrakauerandgVisor bot 3a76efe831 convert uses of interface{} to any
Done via:
  find . -name "*.go" | xargs sed -i -E 's/interface\{\}/any/g'

PiperOrigin-RevId: 487033228
2022-11-08 13:14:06 -08:00
Adin ScannellandgVisor bot 9487c570a6 checklocks: make behavior configurable.
Defaults are the same, but certain behaviors can now be disabled to more easily
use the analyzer on its own. Now wrappers, lock inferrence and atomic default
analysis can be disabled. For example:

```
go vet -vettool=$HOME/go/bin/checklocks -wrappers=false -inferred=false -atomic=false ./...
```

Fixes #7721

PiperOrigin-RevId: 455393957
2022-06-16 08:58:04 -07:00
Adin ScannellandgVisor bot bf2490102d Handle cross-package global guards.
Updates #7721

PiperOrigin-RevId: 455029306
2022-06-14 21:23:28 -07:00
Andrei VaginandgVisor bot 3b36067c81 Add new locks with the correctness validator
All locks are separated into classes. The validator builds a dependency
graph and checks that it doesn't have cycles.

PiperOrigin-RevId: 447812244
2022-05-10 13:24:51 -07:00
Kevin KrakauerandgVisor bot 71ffac9ee0 switch remaining sync/atomic to atomicbitops for 32 bit values
PiperOrigin-RevId: 443571047
2022-04-21 22:27:05 -07:00
Adin ScannellandgVisor bot 8b60115f51 Ensure that nogo is enabled.
A recent change stopped using the correct file (the export data, not the
archive) and checklocks started failing. Unfortunately, this was suppressed,
since the filter command was not failing with findings.

This change fixes that problem and adds a test to ensure that this cannot
happen again. If nogo starts failing to identify problems, the sanity_test in
nogo/sanity will also start to fail.

This change also requires updating the WORKSPACE to the latest rules_go and
Go version, in order to pick up the fixed go_tools. The latest rules_go in
turn required an updated bazel, which in turn required a minor change in the
coverdata implementation.

Fixing the fact propagation brought forward a number of problems with caching
for bazel workers. Its unclear whether this was a core worker issue or whether
some caching was broken, but the situation was basically undebugable. Instead,
the way facts are stored and loaded is optimized to be able to remove the use
of workers altogether and ideally make nogo debuggable.

PiperOrigin-RevId: 426327186
2022-02-03 23:45:40 -08:00
Adin ScannellandgVisor bot 5d23a9cbef Clean documentation and add go vet support for checklocks.
This makes it easier to iterate with checklocks. This pattern will be
duplicated with more complex analyzers.

Updates #7038

PiperOrigin-RevId: 426317618
2022-02-03 22:26:47 -08:00
Darren Rambaud d127ec6f40 tools/checklocks: fix typo in readme 2021-12-29 21:27:56 -06:00
Adin ScannellandgVisor bot a3907dbbb0 Allow reading for mixed atomic semantics.
This relaxes constraints on mixed atomic / lock protected fields. We
explicitly allow reads in this case, since this should be safe.

PiperOrigin-RevId: 414476414
2021-12-06 10:12:13 -08:00
Adin ScannellandgVisor bot 9d999bd2ab 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 2e330e733d 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 e877a7046d Support distinction for RWMutex and read-only locks.
Fixes #6590

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

PiperOrigin-RevId: 396393293
2021-09-13 10:54:24 -07:00
Adin ScannellandgVisor bot 0f15e2418e 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
Adin ScannellandgVisor bot 787ac1f88a nogo: enable bazel workers and other optimizations.
This is a suite of changes intended to dramatically speed up nogo speed.

First, there are minor changes that help efficiency significantly.

* Gob-based encoding is used internally, and JSON only used for the final
  set of findings. This is done to preserve the existing format (which is
  consumed by external tooling), and to facilitate manual debugging.

* Unnecessary regex compilation is elided in the configuration, and care is
  taken for merges to prevent redundant entries. I'm not sure quite sure how,
  but it turns out that this was consumed a significant amount of time,
  presumably compiling the same regexes over and over again.

Second, this change enables bazel workers for nogo analyzers.

Workers enable persistent processes instead of creating and tearing down a
sandbox every invocation. A library is introduced to abstraction these details,
and allow the tools to still be written using standard flags, etc.

The key here is that these binaries and the core of nogo become aware of
caches with worker.Cache. This allows us to save significant time loading the
same set of files and findings over and over again. These caches are keyed by
the digests that are provided by bazel, and are capped in overall size.

Note that the worker package attempts to capture output during each run, but
tools are no longer permitted to write to stdout. This necessitated dropping
some spurious output from checklocks.

PiperOrigin-RevId: 370505732
2021-04-26 11:42:49 -07:00
Bhasker HariharanandgVisor bot 311ac90a8a 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