fix:correct Sizeof to align size properly

This commit is contained in:
luoliwoshang
2024-07-11 18:06:41 +08:00
parent aedaf57249
commit 88cfeb2791

View File

@@ -114,7 +114,7 @@ func (p *goProgram) Offsetsof(fields []*types.Var) (ret []int64) {
func (p *goProgram) Sizeof(T types.Type) int64 {
prog := Program(p)
ptrSize := int64(prog.PointerSize())
return prog.sizes.Sizeof(T) + extraSize(T, ptrSize)
return align((prog.sizes.Sizeof(T) + extraSize(T, ptrSize)), prog.sizes.Alignof(T))
}
func extraSize(t types.Type, ptrSize int64) (ret int64) {
@@ -134,6 +134,10 @@ func extraSize(t types.Type, ptrSize int64) (ret int64) {
return 0
}
func align(x, a int64) int64 {
return (x + a - 1) &^ (a - 1)
}
// -----------------------------------------------------------------------------
type rawType struct {