Skip to content

Commit

Permalink
Revert #393 and #396, then apply correct fix for field alignment issue (
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Sep 27, 2024
1 parent 7be28dd commit e3a5bd0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions langsupport/langtypeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions langsupport/typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions languages/assemblyscript/handler_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions languages/assemblyscript/typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
5 changes: 5 additions & 0 deletions languages/golang/typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e3a5bd0

Please sign in to comment.