From: Keith Randall Date: Thu, 17 Apr 2025 05:48:06 +0000 (-0700) Subject: cmd/compile: align stack-allocated backing stores higher than required X-Git-Tag: go1.25rc1~216 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c8bf388bad9bf350b513c562bba22868bc976247;p=gostls13.git 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 Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI Auto-Submit: Keith Randall Reviewed-by: Cherry Mui --- diff --git a/src/cmd/compile/internal/walk/builtin.go b/src/cmd/compile/internal/walk/builtin.go index 99cf2d784d..84e7436103 100644 --- a/src/cmd/compile/internal/walk/builtin.go +++ b/src/cmd/compile/internal/walk/builtin.go @@ -600,11 +600,23 @@ func walkMakeSlice(n *ir.MakeExpr, init *ir.Nodes) ir.Node { lenCap.Body.Append(mkcall("panicmakeslicecap", nil, &lenCap.Body)) nif.Body.Append(lenCap) - t := types.NewArray(t.Elem(), K) // [K]E - arr := typecheck.TempAt(base.Pos, ir.CurFunc, t) // var arr [K]E - nif.Body.Append(ir.NewAssignStmt(base.Pos, arr, nil)) // arr = {} (zero it) - s := ir.NewSliceExpr(base.Pos, ir.OSLICE, arr, nil, len, cap) // arr[:len:cap] - nif.Body.Append(ir.NewAssignStmt(base.Pos, slice, s)) // slice = arr[:len:cap] + t := types.NewArray(t.Elem(), K) // [K]E + // Wrap in a struct containing a [0]uintptr field to force + // pointer alignment. Some user code expects higher alignment + // than what is guaranteed by the element type, because that's + // the behavior they observed of mallocgc, and then relied upon. + // See issue 73199. + field := typecheck.Lookup("arr") + t = types.NewStruct([]*types.Field{ + {Sym: types.BlankSym, Type: types.NewArray(types.Types[types.TUINTPTR], 0)}, + {Sym: field, Type: t}, + }) + t.SetNoalg(true) + store := typecheck.TempAt(base.Pos, ir.CurFunc, t) // var store struct{_ uintptr[0]; arr [K]E} + nif.Body.Append(ir.NewAssignStmt(base.Pos, store, nil)) // store = {} (zero it) + arr := ir.NewSelectorExpr(base.Pos, ir.ODOT, store, field) // arr = store.arr + s := ir.NewSliceExpr(base.Pos, ir.OSLICE, arr, nil, len, cap) // store.arr[:len:cap] + nif.Body.Append(ir.NewAssignStmt(base.Pos, slice, s)) // slice = store.arr[:len:cap] appendWalkStmt(init, typecheck.Stmt(nif))