diff options
author | Martin Möhrmann <moehrmann@google.com> | 2020-05-07 23:43:22 +0200 |
---|---|---|
committer | Martin Möhrmann <moehrmann@google.com> | 2020-05-07 23:24:49 +0000 |
commit | 6ffca2260296d82f235d38367f0d0f008121521e (patch) | |
tree | 0e4a6766d00c07a5f98c6d5979c0ddb1c5727d09 /src/runtime/slice.go | |
parent | 78aa4af239749106b8eadc9fcfe0ab4dac0b1315 (diff) | |
download | go-6ffca2260296d82f235d38367f0d0f008121521e.tar.gz go-6ffca2260296d82f235d38367f0d0f008121521e.zip |
runtime: do not attempt bulkBarrierPreWrite when dst slice length is zero
If dst slice length is zero in makeslicecopy then the called mallocgc is
using a fast path to only return a pointer to runtime.zerobase.
There may be no heapBits for that address readable by
bulkBarrierPreWriteSrcOnly which will cause a panic.
Protect against this by not calling bulkBarrierPreWriteSrcOnly if
there is nothing to copy. This is the case for all cases where the
length of the destination slice is zero.
runtime.growslice and runtime.typedslicecopy have fast paths that
do not call bulkBarrierPreWrite for zero copy lengths either.
Fixes #38929
Change-Id: I78ece600203a0a8d24de5b6c9eef56f605d44e99
Reviewed-on: https://go-review.googlesource.com/c/go/+/232800
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/runtime/slice.go')
-rw-r--r-- | src/runtime/slice.go | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/runtime/slice.go b/src/runtime/slice.go index d9949e7939..0418ace25a 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -59,7 +59,7 @@ func makeslicecopy(et *_type, tolen int, fromlen int, from unsafe.Pointer) unsaf } else { // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory. to = mallocgc(tomem, et, true) - if writeBarrier.enabled { + if copymem > 0 && writeBarrier.enabled { // Only shade the pointers in old.array since we know the destination slice to // only contains nil pointers because it has been cleared during alloc. bulkBarrierPreWriteSrcOnly(uintptr(to), uintptr(from), copymem) |