From e3a5bd0c12a1ad9463b8e185e6f8b2e1d00266a0 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Thu, 26 Sep 2024 18:51:22 -0700 Subject: [PATCH] Revert #393 and #396, then apply correct fix for field alignment issue (#398) --- CHANGELOG.md | 4 ++++ langsupport/langtypeinfo.go | 1 + langsupport/typeinfo.go | 17 +++++++---------- languages/assemblyscript/handler_objects.go | 12 +++++------- languages/assemblyscript/typeinfo.go | 5 +++++ languages/golang/typeinfo.go | 5 +++++ 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d3373b6..ea81cfdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## UNRELEASED + +- Revert #393 and #396, then apply correct fix for field alignment issue [#397](https://github.com/hypermodeAI/runtime/pull/397) + ## 2024-09-26 - Version 0.12.5 - Fix AssemblyScript error unpinning objects from memory [#396](https://github.com/hypermodeAI/runtime/pull/396) diff --git a/langsupport/langtypeinfo.go b/langsupport/langtypeinfo.go index 5a7c70f1..6e43693d 100644 --- a/langsupport/langtypeinfo.go +++ b/langsupport/langtypeinfo.go @@ -15,6 +15,7 @@ type LanguageTypeInfo interface { GetAlignmentOfType(ctx context.Context, typ string) (uint32, error) GetDataSizeOfType(ctx context.Context, typ string) (uint32, error) GetEncodingLengthOfType(ctx context.Context, typ string) (uint32, error) + ObjectsUseMaxFieldAlignment() bool GetListSubtype(typ string) string GetMapSubtypes(typ string) (string, string) diff --git a/langsupport/typeinfo.go b/langsupport/typeinfo.go index 2d604396..8779b68c 100644 --- a/langsupport/typeinfo.go +++ b/langsupport/typeinfo.go @@ -136,6 +136,7 @@ func GetTypeInfo(ctx context.Context, lti LanguageTypeInfo, typeName string, typ } offset := uint32(0) + maxAlignment := uint32(0) info.fieldTypes = make([]TypeInfo, len(def.Fields)) info.fieldOffsets = make([]uint32, len(def.Fields)) for i, field := range def.Fields { @@ -150,20 +151,16 @@ func GetTypeInfo(ctx context.Context, lti LanguageTypeInfo, typeName string, typ offset = AlignOffset(offset, alignment) info.fieldOffsets[i] = offset offset += size - } - if size, err := lti.GetDataSizeOfType(ctx, typeName); err != nil { - return nil, err - } else { - info.dataSize = size + if alignment > maxAlignment { + maxAlignment = alignment + } } + info.dataSize = AlignOffset(offset, maxAlignment) - if alignment, err := lti.GetAlignmentOfType(ctx, typeName); err != nil { - return nil, err - } else { - info.alignment = alignment + if lti.ObjectsUseMaxFieldAlignment() { + info.alignment = maxAlignment } - } info.flags = flags diff --git a/languages/assemblyscript/handler_objects.go b/languages/assemblyscript/handler_objects.go index 23fac5cc..13ac94e4 100644 --- a/languages/assemblyscript/handler_objects.go +++ b/languages/assemblyscript/handler_objects.go @@ -186,14 +186,12 @@ func (h *managedObjectHandler) doWrite(ctx context.Context, wa langsupport.WasmA return 0, cln, err } - // TODO: the following should work, but it in some cases we are finding that the inner objects fail to unpin even though they were successfully pinned - // we can unpin the inner objects early, since they are now referenced by the managed object - // if c != nil { - // if err := c.Clean(); err != nil { - // return 0, cln, err - // } - // } + if c != nil { + if err := c.Clean(); err != nil { + return 0, cln, err + } + } return ptr, cln, nil } diff --git a/languages/assemblyscript/typeinfo.go b/languages/assemblyscript/typeinfo.go index 5c914ade..45840412 100644 --- a/languages/assemblyscript/typeinfo.go +++ b/languages/assemblyscript/typeinfo.go @@ -206,6 +206,11 @@ func (lti *langTypeInfo) GetAlignmentOfType(ctx context.Context, typ string) (ui return lti.GetSizeOfType(ctx, typ) } +func (lti *langTypeInfo) ObjectsUseMaxFieldAlignment() bool { + // AssemblyScript classes are aligned to the pointer size (4 bytes), not the max field alignment. + return false +} + func (lti *langTypeInfo) GetDataSizeOfType(ctx context.Context, typ string) (uint32, error) { switch typ { case "u64", "i64", "f64": diff --git a/languages/golang/typeinfo.go b/languages/golang/typeinfo.go index d6facc2b..0ae24f9d 100644 --- a/languages/golang/typeinfo.go +++ b/languages/golang/typeinfo.go @@ -316,6 +316,11 @@ func (lti *langTypeInfo) GetAlignmentOfType(ctx context.Context, typ string) (ui return lti.getAlignmentOfStruct(ctx, typ) } +func (lti *langTypeInfo) ObjectsUseMaxFieldAlignment() bool { + // Go structs are aligned to the maximum alignment of their fields + return true +} + func (lti *langTypeInfo) getAlignmentOfStruct(ctx context.Context, typ string) (uint32, error) { def, err := lti.GetTypeDefinition(ctx, typ) if err != nil {