Commit Graph

75 Commits

Author SHA1 Message Date
Michael Pratt f3e4a1fc3b Remove last remaining !go1.22 build tag
The last remaining !go1.22 build is protecting the definition of
pkg/sync.maptype, which is a copy of runtime.maptype. We need to ensure these
definitions match so we can safely access the hasher field.

At its core, this CL achieves this check by ensuring that
unsafe.Offsetof(maptype{}.Hasher) matches the offset in the runtime version of
the type.

Several things happen along the way to achieve this:

* As of May 2023, runtime.maptype is actually a type alias for
internal/abi.MapType. checkoffset was failing to record the offsets because it
skipped type aliases for no good reason. Simply removing the type alias check
is sufficient to make type aliases work. (This part of the CL is technically
unnecessary because this CL ultimately references internal/abi.MapType
directly in anticipation of removal of the type alias. But there is no reason
not to allow type aliases).

* The checkconst / checkoffset regexp unintentionally does not allow / in
package paths, even though the rest of the package supports /. Fix this.

* checkconst was comparing the literal AST expression string against the
runtime value (i.e., "unsafe.Offsetof(maptype{}.Hasher)" vs "72", which fails
comparison. Switch to getting the resolved constant value from the type
checker.

* nogo/check.importer only loads package facts on direct import (stored in
importer.cache). If a package is not directly imported ImportPackageFact will
not find the facts. Typically packages need to ensure they directly depend on
packages they want facts from (e.g., pkg/sync has a dummy import of runtime in
runtime.go). This doesn't work for internal/abi because we cannot directly
import an internal package. Work around this as a hack by unconditionally
"importing" internal/abi when analyzing any package.

With regard to the last point, not that the nogo/defs.bzl nogo integration only
provides facts from the direct dependencies and the entire stdlib (since the
stdlib is analyzed as one bundle). So this trick only works for a stdlib
package. A bazel package indirect dependency would be missing facts altogether.

PiperOrigin-RevId: 549999084
2023-07-21 11:22:36 -07:00
Andrei Vagin 52692c3647 fdtable: avoid large arrays
FDTable.descriptorTable is a slice of unsafe.Pointer-s and its maximum length
is MaxInt32. It requires up to 16GB of memory. A process can use just a few
descriptors but sets one or more of them to high numbers. In this case,
FDTable.descriptorTable is extended to the maximum size.

The problem here is that go-runtime zeros memory regions when they are reused.
In the case of fdtable, the memory region is 16GB, so it is a time consuming
operation. Second, it forces the kernel to allocate physical pages to
the entire region.

This change adds another level to descriptorTable, so the first level is
a slice of buckets where each bucket is a slice of descriptors. The bucket
size is fixed to 512 entries to fit one page.

Before:
BenchmarkFDLookupAndDecRef-12              	50834290	        23.70 ns/op
BenchmarkCreateWithMaxFD-12                	       2	7194873988 ns/op
BenchmarkFDLookupAndDecRefConcurrent-12    	23775555	        49.68 ns/op
BenchmarkTableLookup-12                    	412888780	         2.835 ns/op
BenchmarkTableMapLookup-12                 	87944782	        12.84 ns/op

After:
BenchmarkFDLookupAndDecRef-12              	46229940	        25.03 ns/op
BenchmarkCreateWithMaxFD-12                	      13	  82573899 ns/op
BenchmarkFDLookupAndDecRefConcurrent-12    	21889380	        54.13 ns/op
BenchmarkTableLookup-12                    	415851230	         2.821 ns/op
BenchmarkTableMapLookup-12                 	97236267	        11.89 ns/op

Reported-by: syzbot+af17678e3bfb7ca7c65a@syzkaller.appspotmail.com
PiperOrigin-RevId: 539138632
2023-06-09 11:49:28 -07:00
Kevin Krakauer d2b4837570 fix nogo error
Avoids messages like:
error checking types: pkg/sync/runtime_unsafe.go:105:21: predeclared any requires go1.18 or later

PiperOrigin-RevId: 525559255
2023-04-19 15:02:04 -07:00
Adin Scannell 5ba34bd1a5 Add explicit NOFRAME annotation in assembly functions.
For all trivial and zero frame-sized functions, we now require an explicit
NOFRAME annotation as the heuristic has changed. See go.dev/cl/466316.

Most of these functions do not  *require* NOFRAME, but this change encodes
the existing behavior in order to avoid accidental bugs or regressions.

PiperOrigin-RevId: 513946210
2023-03-04 00:53:20 -08:00
Adin Scannell 1ceb814544 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Andrei Vagin 426deb60fd lockdep: fix the TOCTTOU issue
The problem is that it checkes circular dependencies and one then adds the
target lock to the graph.

Reported-by: syzbot+7687e27a2029723c7fb4@syzkaller.appspotmail.com
PiperOrigin-RevId: 512637945
2023-02-27 08:49:01 -08:00
Adin Scannell bd561fd3f9 Move from facts render to facts validation.
This allows fact information to be validated in the underlying source files,
but requires us to explicitly maintain this in appropriate version-tagged, and
architecture-tagged files. This is more explicit and safer.

This mechanism uses a special regular expression for matching a +checkconst
stanza to validate constant values, sizes and offsets. This applies to both Go
source files and assembly files.

PiperOrigin-RevId: 511867507
2023-02-23 13:17:49 -08:00
Adin Scannell 6a4908b262 Process nogo targets across all architectures.
PiperOrigin-RevId: 511343590
2023-02-21 17:06:02 -08:00
Michael Pratt bcac7fc9ad Bump sync build tag
cl/504661697 removes this build tag once and for all, but it is blocked on
additional work needed to get it working with OSS bazel builds.

Until that is ready, bump the build tag to unblock testing Go tip (1.21).

PiperOrigin-RevId: 505923005
2023-01-31 09:44:32 -08:00
Michael Pratt ee3a8735b5 Move runtime constants to dynamic facts render
PiperOrigin-RevId: 505172527
2023-01-27 11:52:00 -08:00
Michael Pratt a8d6cc4072 Move nmspinning address lookup to dynamic facts render
This removes the need to check the offset every release.

I've also removed the negative build tag from runtime_amd64.go, which less
obviously correct. In theory, we should check that this package's use of
nmspinning is still valid in each release, but there is no way to automate that
and I don't realistically seeing checking happening beyond verifying tests
work. Additionally, the existing use is already suspect. :)

The good news is the misuse is likely to cause scheduling issues, not memory
corruption.

PiperOrigin-RevId: 505114683
2023-01-27 07:43:26 -08:00
Michael Pratt 8604e1c200 Avoid use of gohacks.SliceHeader to access pointer to data
Accessing a pointer to the data in a slice can be achieved with
`unsafe.Pointer(&slice[0])`.

f051ec6463 motivated using gohacks.SliceHeader in
this way with "we often use SliceHeader to extract pointers from slices in a
way that avoids bounds checking and/or handles nil slices correctly", but this
no longer seems to be the case. None of the remaining uses are obviously
performance sensitive or necessarily include bounds checks, nor get used with
nil slices.

This brings us one step closer to removing gohacks.SliceHeader, which is one
less internal detail to keep in sync with Go.

For #8422

PiperOrigin-RevId: 504408578
2023-01-24 16:42:27 -08:00
Etienne Perot 445fa6f40c Lockdep: Print more info in the "unbalanced unlock" case.
This CL does the following:

- Add the ability for nested locks to have names.
- Give names to all current uses of nested locks in the codebase.
- Truncate `lockdep` debug stack traces to avoid the clutter from the
  `lockdep` code itself
- Simplify `lockdep` to not longer require `classMap`.

PiperOrigin-RevId: 491486620
2022-11-28 17:53:09 -08:00
Nicolas Lacasse 4fa2196b6a Call *post*GoReadyWakeSuppression() after goready.
We were calling preGoReadyWakeSuppression, which must have been a typo.

PiperOrigin-RevId: 490337652
2022-11-22 14:30:19 -08:00
Kevin Krakauer 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
Ian Gudger 288fcb8218 Use standard library sync.Mutex.TryLock.
The standard library sync.Mutex has included a TryLock method since Go 1.18.
There is no longer a need to implement a parallel version with unsafe. Using
the standard library version reduces the ongoing maintenance cost of this
package.

https://pkg.go.dev/sync#Mutex.TryLock
https://tip.golang.org/doc/go1.18#minor_library_changes
2022-10-15 23:55:24 -07:00
Michael Pratt 974792ae18 Bump negative build tag constraints to go1.21
PiperOrigin-RevId: 465062413
2022-08-03 08:30:46 -07:00
Andrei Vagin 605841baad lockdep: print more info for the unbalance unlock case
* print a lock type
* print a list of taken locks

PiperOrigin-RevId: 454310284
2022-06-11 00:22:29 -07:00
Nicolas Lacasse 1d70ac89c5 Typo fix: unknow -> unknown
PiperOrigin-RevId: 454205194
2022-06-10 11:25:45 -07:00
Andrei Vagin 8b41af9330 sync/lockdep: use RangeRepeatable instead of Range
It is lockless and so it guarantes to avoid nested locks (deadlocks).

Reported-by: syzbot+7f2b94fef2e1109bcd40@syzkaller.appspotmail.com
2022-06-09 12:00:42 -07:00
Ayush Ranjan bda5ce7977 Simplify codebase.
Ran gofmt -w -s ./

PiperOrigin-RevId: 449415155
2022-05-18 00:56:31 -07:00
Ayush Ranjan f6ed4523dc Reformat codebase.
PiperOrigin-RevId: 449358041
2022-05-17 17:48:35 -07:00
Andrei Vagin 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 Krakauer 7124367b82 prohibit direct use of sync/atomic (u)int32 functions
Also adds the "// +checkalignedignore" escape hatch for packages to opt out of
checking.

See cl/439349432 for justification (https://github.com/google/gvisor/pull/7376).

PiperOrigin-RevId: 444918125
2022-04-27 11:25:14 -07:00
Kevin Krakauer 39790bd3a1 switch remaining sync/atomic to atomicbitops for 32 bit values
PiperOrigin-RevId: 443571047
2022-04-21 22:27:05 -07:00