Skip to content

[wasm coreclr] Fix fields alignment #115364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025

Conversation

radekdoulik
Copy link
Member

@radekdoulik radekdoulik commented May 7, 2025

Fixes #115363

The problem happened in MethodTableBuilder::PlaceInstanceFields where in wasm case the fields were placed at wrong offsets.

The native representation looks like this:

class  field                                        wasm type          offset
-----------------------------------------------------------------------------------------------
Object
       PTR_MethodTable m_pMethTab                   i32                0
AssemblyLoadContextBaseObject
       int64_t         _id                          i64                8
       OBJECTREF       _unloadLock                  i32                16
       OBJECTREF       _resolvingUnmanagedDll       i32
       OBJECTREF       _resolving                   i32
       OBJECTREF       _unloading                   i32
       OBJECTREF       _name                        i32
       INT_PTR         _nativeAssemblyLoadContext   i32
       DWORD           _state                       i32                40
       CLR_BOOL        _isCollectible               i8                 44

While the managed field offsets were calculated wrong, the _id was placed at offset 4 and thus all the following fields were at wrong offsets too. The size check then failed.

By enabling FEATURE_64BIT_ALIGNMENT the dwOffsetBias is set to TARGET_POINTER_SIZE and the fields layout is calculated properly.

The 8 bytes wide types in wasm should be aligned to 8 bytes

https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md#data-representation

so hopefully the rest of the changes enabled by this feature should apply as well. Before this feature was enabled only for arm 32bits.

Fixes dotnet#115363

The problem happened in MethodTableBuilder::PlaceInstanceFields where in wasm case
the fields were placed at wrong offsets.

The native representation looks like this:

```
class  field                                        wasm type          offset
-----------------------------------------------------------------------------------------------
Object
       PTR_MethodTable m_pMethTab                   i32                0
AssemblyLoadContextBaseObject
       int64_t         _id                          i64                8
       OBJECTREF       _unloadLock                  i32                12
       OBJECTREF       _resolvingUnmanagedDll       i32
       OBJECTREF       _resolving                   i32
       OBJECTREF       _unloading                   i32
       OBJECTREF       _name                        i32
       INT_PTR         _nativeAssemblyLoadContext   i32
       DWORD           _state                       i32                40
       CLR_BOOL        _isCollectible               i8                 44
```

While the managed field offsets were calculated wrong, the _id was
placed at offset 4 and thus all the following fields were at wrong
offsets too. The size check then failed.

By enabling FEATURE_64BIT_ALIGNMENT the dwOffsetBias is set to
TARGET_POINTER_SIZE and the fields layout is calculated properly.

The 8 bytes types in wasm should be aligned to 8 bytes

https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md#data-representation

so hopefully the rest of the changes enabled by this feature should
apply as well. Before this feature was enabled only for arm 32bits.
@Copilot Copilot AI review requested due to automatic review settings May 7, 2025 12:53
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 7, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes field alignment issues in wasm CoreCLR by correcting the managed field offset calculation.

  • Expanded the 64-bit alignment feature to include the wasm platform using the TARGET_BROWSER flag.
  • Updates in both switches.h and gcenv.object.h files enable proper alignment for 8-byte types on wasm.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/coreclr/inc/switches.h Added TARGET_BROWSER to enable FEATURE_64BIT_ALIGNMENT
src/coreclr/gc/env/gcenv.object.h Added TARGET_BROWSER to enable FEATURE_64BIT_ALIGNMENT

@radekdoulik radekdoulik requested review from jkotas and janvorli May 7, 2025 12:55
@radekdoulik radekdoulik added area-TypeSystem-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 7, 2025
@radekdoulik radekdoulik added this to the Future milestone May 7, 2025
Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@janvorli janvorli merged commit 8d07196 into dotnet:main May 7, 2025
95 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jun 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[wasm coreclr] wrong fields alignment
4 participants