Skip to content

[Wasm RyuJIT] Generate element section and populate function pointer table#125531

Merged
kg merged 5 commits intodotnet:mainfrom
kg:wasm-ftnpointers-1
Mar 17, 2026
Merged

[Wasm RyuJIT] Generate element section and populate function pointer table#125531
kg merged 5 commits intodotnet:mainfrom
kg:wasm-ftnpointers-1

Conversation

@kg
Copy link
Member

@kg kg commented Mar 13, 2026

  • Rename r2r_start to image_base
  • Add image_function_pointer_base import, the offset to populate the function pointer table at
  • Fix us allocating the function pointer table with an initial size of 0
  • Generate an element section in our modules containing a single element that creates a function pointer for each of our functions in sequential order, i.e.
(import "env" "__image_function_pointer_base" (global (;2;) i32))
...
(elem (;0;) (offset global.get 2 i32.const 0 i32.add) func 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)

kg added 2 commits March 13, 2026 12:08
Add image_pointer_base
Scaffolding for element sections
Copilot AI review requested due to automatic review settings March 13, 2026 19:28
@kg kg added arch-wasm WebAssembly architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Mar 13, 2026
@kg
Copy link
Member Author

kg commented Mar 13, 2026

cc @adamperlin

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

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 updates the Wasm object writer used by CoreCLR tooling to support function-pointer table initialization by emitting a WebAssembly element section, and aligns the imported globals used as bases for data and function-pointer placement.

Changes:

  • Rename the imported module base global from __r2r_start to __image_base and update data segment placement to use it.
  • Add a new imported global __image_pointer_base and use it as the offset expression for an emitted element segment.
  • Emit an element section that initializes the table with function indices in sequential order, and prepend the element-segment count during emission.

You can also share your feedback on Copilot code review. Take the survey.

@kg kg marked this pull request as ready for review March 13, 2026 20:00
@kg kg requested a review from MichalStrehovsky as a code owner March 13, 2026 20:00
Copilot AI review requested due to automatic review settings March 13, 2026 20:00
@kg
Copy link
Member Author

kg commented Mar 13, 2026

With these changes applied, callVoidFunc works by accident given this module:

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public static unsafe class Program {
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void voidFunc () {
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void callVoidFunc () {
        voidFunc();
    }
}

Because voidFunc gets allocated funcindex 0 and function pointer index 0, callVoidFunc can dereference the PEP 0 to get the function pointer index 0 and then perform an indirect call.

@kg kg closed this Mar 13, 2026
@kg kg reopened this Mar 13, 2026
Copy link
Contributor

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 updates the Wasm object writer to emit a Wasm Element section that initializes the function pointer table, and adjusts related global imports/offset handling to use __image_base/__image_pointer_base.

Changes:

  • Added a Wasm Element section and logic to write an element segment that initializes table entries for all generated functions.
  • Renamed __r2r_start usage to __image_base and added a new __image_pointer_base global import for table population offsets.
  • Fixed table section limits so the function pointer table is allocated with a non-zero initial size.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Contributor

@adamperlin adamperlin left a comment

Choose a reason for hiding this comment

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

I think this looks good! Thanks for doing this work. Now that I'm reading it, we might want to rename __image_pointer_base to _image_table_base or something similar, just so we're very clear to distinguish between data pointers and function/table indices. Not crucial though.

@kg
Copy link
Member Author

kg commented Mar 13, 2026

I think this looks good! Thanks for doing this work. Now that I'm reading it, we might want to rename __image_pointer_base to _image_table_base or something similar, just so we're very clear to distinguish between data pointers and function/table indices. Not crucial though.

image_func_pointer_base maybe?

@adamperlin
Copy link
Contributor

I think this looks good! Thanks for doing this work. Now that I'm reading it, we might want to rename __image_pointer_base to _image_table_base or something similar, just so we're very clear to distinguish between data pointers and function/table indices. Not crucial though.

image_func_pointer_base maybe?

I think that works!

Copilot AI review requested due to automatic review settings March 17, 2026 04:07
Copy link
Contributor

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 updates the CoreCLR Wasm object writer to support initializing the function pointer table via an emitted element section, while also renaming the module base import and introducing a new imported global intended to represent the function-pointer-table base offset.

Changes:

  • Add a new wasm.element section and emit a single active element segment that initializes the table with all generated functions.
  • Rename the imported module base global from __r2r_start to __image_base and add a new imported global for the function pointer table base (__image_function_pointer_base).
  • Fix the table limits encoding so the table’s minimum/maximum size are no longer emitted with an initial size of 0.

You can also share your feedback on Copilot code review. Take the survey.

@kg
Copy link
Member Author

kg commented Mar 17, 2026

/ba-g iOS deadletter and known flakes for configurations unaffected by this wasm-only change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants