25 Commits
Author SHA1 Message Date
gVisor bot e0435b9a53 Merge pull request #11415 from avagin:codespell
PiperOrigin-RevId: 721421397
2025-01-30 09:44:28 -08:00
Andrei Vagin f010ae01ac Fix a few typos 2025-01-29 21:16:51 -08:00
gVisor bot 8bdf76c5ca Internal change.
PiperOrigin-RevId: 721019932
2025-01-29 10:13:52 -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
Etienne PerotandgVisor bot 69e0c7643d Use clear on map types wherever possible.
This is similar as pull request #9749 but for maps rather than slices.

PiperOrigin-RevId: 586504320
2023-11-29 18:00:07 -08: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 442b16ee24 Fix type assertion failures with latest tools packages.
PiperOrigin-RevId: 511564602
2023-02-22 12:23:43 -08:00
Adin ScannellandgVisor bot 12a930a63e 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 d8aa09e04c 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 9dfa65ebdb 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 1ff543e17e Handle cross-package global guards.
Updates #7721

PiperOrigin-RevId: 455029306
2022-06-14 21:23:28 -07:00
Andrei VaginandgVisor bot 3aab92297a 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 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 a87bb4aae2 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 dce4528a1a 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 871aaa768d tools/checklocks: fix typo in readme 2021-12-29 21:27:56 -06:00
Adin ScannellandgVisor bot 3f2ffc9f0c 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 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
Adin ScannellandgVisor bot 5b7b7daa42 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 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