Skip to content

Commit c8bf388

Browse files
randall77gopherbot
authored andcommitted
cmd/compile: align stack-allocated backing stores higher than required
Because that's what mallocgc did and some user code came to rely on it. Fixes #73199 Change-Id: I45ca00d2ea448e6729ef9ac4cec3c1eb0ceccc89 Reviewed-on: https://go-review.googlesource.com/c/go/+/666116 Reviewed-by: t hepudds <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 524946d commit c8bf388

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/cmd/compile/internal/walk/builtin.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,23 @@ func walkMakeSlice(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
600600
lenCap.Body.Append(mkcall("panicmakeslicecap", nil, &lenCap.Body))
601601
nif.Body.Append(lenCap)
602602

603-
t := types.NewArray(t.Elem(), K) // [K]E
604-
arr := typecheck.TempAt(base.Pos, ir.CurFunc, t) // var arr [K]E
605-
nif.Body.Append(ir.NewAssignStmt(base.Pos, arr, nil)) // arr = {} (zero it)
606-
s := ir.NewSliceExpr(base.Pos, ir.OSLICE, arr, nil, len, cap) // arr[:len:cap]
607-
nif.Body.Append(ir.NewAssignStmt(base.Pos, slice, s)) // slice = arr[:len:cap]
603+
t := types.NewArray(t.Elem(), K) // [K]E
604+
// Wrap in a struct containing a [0]uintptr field to force
605+
// pointer alignment. Some user code expects higher alignment
606+
// than what is guaranteed by the element type, because that's
607+
// the behavior they observed of mallocgc, and then relied upon.
608+
// See issue 73199.
609+
field := typecheck.Lookup("arr")
610+
t = types.NewStruct([]*types.Field{
611+
{Sym: types.BlankSym, Type: types.NewArray(types.Types[types.TUINTPTR], 0)},
612+
{Sym: field, Type: t},
613+
})
614+
t.SetNoalg(true)
615+
store := typecheck.TempAt(base.Pos, ir.CurFunc, t) // var store struct{_ uintptr[0]; arr [K]E}
616+
nif.Body.Append(ir.NewAssignStmt(base.Pos, store, nil)) // store = {} (zero it)
617+
arr := ir.NewSelectorExpr(base.Pos, ir.ODOT, store, field) // arr = store.arr
618+
s := ir.NewSliceExpr(base.Pos, ir.OSLICE, arr, nil, len, cap) // store.arr[:len:cap]
619+
nif.Body.Append(ir.NewAssignStmt(base.Pos, slice, s)) // slice = store.arr[:len:cap]
608620

609621
appendWalkStmt(init, typecheck.Stmt(nif))
610622

0 commit comments

Comments
 (0)