@@ -141,7 +141,7 @@ func (pa *ABIParamAssignment) RegisterTypesAndOffsets() ([]*types.Type, []int64)
141141 }
142142 typs := make ([]* types.Type , 0 , l )
143143 offs := make ([]int64 , 0 , l )
144- offs , _ = appendParamOffsets (offs , 0 , pa .Type )
144+ offs , _ = appendParamOffsets (offs , 0 , pa .Type ) // 0 is aligned for everything.
145145 return appendParamTypes (typs , pa .Type ), offs
146146}
147147
@@ -193,8 +193,8 @@ func appendParamTypes(rts []*types.Type, t *types.Type) []*types.Type {
193193
194194// appendParamOffsets appends the offset(s) of type t, starting from "at",
195195// to input offsets, and returns the longer slice and the next unused offset.
196+ // at should already be aligned for t.
196197func appendParamOffsets (offsets []int64 , at int64 , t * types.Type ) ([]int64 , int64 ) {
197- at = align (at , t )
198198 w := t .Size ()
199199 if w == 0 {
200200 return offsets , at
@@ -210,11 +210,15 @@ func appendParamOffsets(offsets []int64, at int64, t *types.Type) ([]int64, int6
210210 typ := t .Kind ()
211211 switch typ {
212212 case types .TARRAY :
213+ te := t .Elem ()
213214 for i := int64 (0 ); i < t .NumElem (); i ++ {
214- offsets , at = appendParamOffsets (offsets , at , t .Elem ())
215+ at = align (at , te )
216+ offsets , at = appendParamOffsets (offsets , at , te )
215217 }
216218 case types .TSTRUCT :
219+ at0 := at
217220 for i , f := range t .Fields () {
221+ at = at0 + f .Offset // Fields may be over-aligned, see wasm32.
218222 offsets , at = appendParamOffsets (offsets , at , f .Type )
219223 if f .Type .Size () == 0 && i == t .NumFields ()- 1 {
220224 at ++ // last field has zero width
@@ -668,12 +672,13 @@ func (pa *ABIParamAssignment) ComputePadding(storage []uint64) []uint64 {
668672 if len (types ) != nr {
669673 panic ("internal error" )
670674 }
675+ offsets , _ := appendParamOffsets ([]int64 {}, 0 , pa .Type )
671676 off := int64 (0 )
672677 for idx , t := range types {
673678 ts := t .Size ()
674679 off += int64 (ts )
675680 if idx < len (types )- 1 {
676- noff := align ( off , types [idx + 1 ])
681+ noff := offsets [idx + 1 ]
677682 if noff != off {
678683 padding [idx ] = uint64 (noff - off )
679684 }
0 commit comments