src/cmd/compile/internal/ssa/writebarrier.go | 15 +++++++++++++++ test/codegen/writebarrier.go | 25 +++++++++++++++++++++++++ diff --git a/src/cmd/compile/internal/ssa/writebarrier.go b/src/cmd/compile/internal/ssa/writebarrier.go index 1caccb7c18d3c43ca24ff9814ef7b10d0660826d..71acefbf8ae52641a5376db296fea23e52b6d2ff 100644 --- a/src/cmd/compile/internal/ssa/writebarrier.go +++ b/src/cmd/compile/internal/ssa/writebarrier.go @@ -252,6 +252,7 @@ var last *Value var start, end int var nonPtrStores int values := b.Values + hasMove := false FindSeq: for i := len(values) - 1; i >= 0; i-- { w := values[i] @@ -263,6 +264,9 @@ last = w end = i + 1 } nonPtrStores = 0 + if w.Op == OpMoveWB { + hasMove = true + } case OpVarDef, OpVarLive: continue case OpStore: @@ -271,6 +275,17 @@ continue } nonPtrStores++ if nonPtrStores > 2 { + break FindSeq + } + if hasMove { + // We need to ensure that this store happens + // before we issue a wbMove, as the wbMove might + // use the result of this store as its source. + // Even though this store is not write-barrier + // eligible, it might nevertheless be the store + // of a pointer to the stack, which is then the + // source of the move. + // See issue 71228. break FindSeq } default: diff --git a/test/codegen/writebarrier.go b/test/codegen/writebarrier.go index cfcfe15a4038563d279c348dec034cfd83ff3339..4e0da144334dd56b005b5e25386ef462e7302271 100644 --- a/test/codegen/writebarrier.go +++ b/test/codegen/writebarrier.go @@ -53,3 +53,28 @@ // amd64:-`.*runtime[.]gcWriteBarrier` // arm64:-`.*runtime[.]gcWriteBarrier` p[3] = d } + +type S struct { + a, b string + c *int +} + +var g1, g2 *int + +func issue71228(dst *S, ptr *int) { + // Make sure that the non-write-barrier write. + // "sp.c = ptr" happens before the large write + // barrier "*dst = *sp". We approximate testing + // that by ensuring that two global variable write + // barriers aren't combined. + _ = *dst + var s S + sp := &s + //amd64:`.*runtime[.]gcWriteBarrier1` + g1 = nil + sp.c = ptr // outside of any write barrier + //amd64:`.*runtime[.]gcWriteBarrier1` + g2 = nil + //amd64:`.*runtime[.]wbMove` + *dst = *sp +}