Skip to content

Commit fe341ee

Browse files
committed
cm, wit: fix record size calculation
Ensure the final size is aligned to the record alignment. See here for more context: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#element-size
1 parent 6bcf449 commit fe341ee

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

cm/result_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,37 @@ func TestIssue344BoolResult(t *testing.T) {
387387
t.Errorf("*v.OK(): %v, expected %v", got, want)
388388
}
389389
}
390+
391+
func TestIssue350(t *testing.T) {
392+
type Shape struct {
393+
_ HostLayout
394+
shape [unsafe.Sizeof(Tuple3[uint16, Result[uint64, uint64, struct{}], uint8]{})]byte
395+
// Previously, with bug in (*Record).Size() algorithm:
396+
// shape [unsafe.Sizeof(Option[[3]string]{})]byte
397+
}
398+
type O Option[[3]string]
399+
type E Tuple3[uint16, Result[uint64, uint64, struct{}], uint8]
400+
type T Result[Shape, O, E]
401+
402+
shapeSize := unsafe.Sizeof(Shape{})
403+
errSize := unsafe.Sizeof(E{})
404+
405+
if errSize > shapeSize {
406+
t.Errorf("size of err type (%d) > size of shape type (%d)", errSize, shapeSize)
407+
408+
var e E
409+
base := uintptr(unsafe.Pointer(&e))
410+
f0 := uintptr(unsafe.Pointer(&e.F0)) - base
411+
f1 := uintptr(unsafe.Pointer(&e.F1)) - base
412+
f2 := uintptr(unsafe.Pointer(&e.F2)) - base
413+
t.Logf("Offsets: F0: %d F1: %d F2 %d", f0, f1, f2)
414+
}
415+
416+
// _ = Err[T](
417+
// Err, uint8]{
418+
// F0: 0,
419+
// F1: OK[Result[uint64, uint64, struct{}]](0),
420+
// F2: 0,
421+
// },
422+
// )
423+
}

wit/record.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (r *Record) Size() uintptr {
1818
s = Align(s, f.Type.Align())
1919
s += f.Type.Size()
2020
}
21-
return s
21+
return Align(s, r.Align())
2222
}
2323

2424
// Align returns the [ABI byte alignment] for [Record] r.

0 commit comments

Comments
 (0)