time: detect copies of Timer and Ticker

The public Timer and Ticker types are only the beginning of the larger
runtime.timeTimer types. User allocations of the public types will not
initialize the runtime-only portion of the type. The existing init bool
fields allow the methods to detect user allocations and panic.

Copies have a similar problem. If a user copies a valid Timer or Ticker,
only the beginning of the actual type is copied and it will not be
usable [1]. The init bool is not sufficient to detect this, as a copy
will copy the bool field. Instead, change the init bool to a self
pointer. If the object is copied, the pointer will no longer match.

Alternatively, we could use a pointer field to a separate runtime type.
This would make the type safe to copy. This was the initial approach in
patch set 1 of CL 568339, but it was changed to avoid the extra
allocation.

[1] Even if the entire structure was defined in time, copying would
still not be safe, as runtime.timeTimer is not safe to copy. e.g., it
contains mutex values. This is what CL 568339 was addressing.

Fixes #73886.

Change-Id: I6a6a636ce703c0e946aa9ee545f756b60eafff0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/676537
Auto-Submit: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
6 files changed
tree: 25b301504ba22718df13fb2364c93e0ff7cd640b
  1. .github/
  2. api/
  3. doc/
  4. lib/
  5. misc/
  6. src/
  7. test/
  8. .gitattributes
  9. .gitignore
  10. codereview.cfg
  11. CONTRIBUTING.md
  12. go.env
  13. LICENSE
  14. PATENTS
  15. README.md
  16. SECURITY.md
README.md

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 4.0 Attribution license.

Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at https://go.dev/dl/.

After downloading a binary release, visit https://go.dev/doc/install for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit https://go.dev/doc/install/source for source installation instructions.

Contributing

Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at https://go.dev/doc/contribute.

Note that the Go project uses the issue tracker for bug reports and proposals only. See https://go.dev/wiki/Questions for a list of places to ask questions about the Go language.