]> Cypherpunks repositories - gostls13.git/commit
[release-branch.go1.18] runtime: make GC see object as allocated after it is initialized
authorCherry Mui <cherryyz@google.com>
Wed, 9 Nov 2022 15:55:54 +0000 (10:55 -0500)
committerCherry Mui <cherryyz@google.com>
Fri, 25 Nov 2022 02:37:05 +0000 (02:37 +0000)
commit3115ed23bdaa3dcc465feba4100da40c2d22f964
treee63e94c60d7f82fe9f24bce74921055c575354b7
parentd96e584773bf0577a4da2ffc9543111ddd80627c
[release-branch.go1.18] runtime: make GC see object as allocated after it is initialized

When the GC is scanning some memory (possibly conservatively),
finding a pointer, while concurrently another goroutine is
allocating an object at the same address as the found pointer, the
GC may see the pointer before the object and/or the heap bits are
initialized. This may cause the GC to see bad pointers and
possibly crash.

To prevent this, we make it that the scanner can only see the
object as allocated after the object and the heap bits are
initialized. Currently the allocator uses freeindex to find the
next available slot, and that code is coupled with updating the
free index to a new slot past it. The scanner also uses the
freeindex to determine if an object is allocated. This is somewhat
racy. This CL makes the scanner use a different field, which is
only updated after the object initialization (and a memory
barrier).

Updates #54596.
Fixes #56751.

Change-Id: I2a57a226369926e7192c253dd0d21d3faf22297c
Reviewed-on: https://go-review.googlesource.com/c/go/+/449017
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
(cherry picked from commit febe7b8e2a4dd7cce6ab8d02cf79a5430819cbe5)
Reviewed-on: https://go-review.googlesource.com/c/go/+/453255
src/runtime/malloc.go
src/runtime/mbitmap.go
src/runtime/mgcsweep.go
src/runtime/mheap.go