Skip to content
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

[wasm] UnmanagedCallersOnly generates incorrect wasm export types #96853

Closed
silesmo opened this issue Jan 11, 2024 · 15 comments
Closed

[wasm] UnmanagedCallersOnly generates incorrect wasm export types #96853

silesmo opened this issue Jan 11, 2024 · 15 comments
Assignees
Labels
arch-wasm WebAssembly architecture area-Interop-mono
Milestone

Comments

@silesmo
Copy link

silesmo commented Jan 11, 2024

Description

When building an application targeting wasm and wasi and exporting it a function using UnmanagedCallersOnly results in the incorrect wasm type to be exported in the built wasm binary.

This is blocking progress with https://github.com/bytecodealliance/wit-bindgen for the mono implementation.

It's also worth mentioning that it's not generating the _initialize function instead of _start for libraries which is what we do with native aot llvm.

Related: #96419

@AaronRobinsonMSFT @lewing @yowl @jsturtevant

Reproduction Steps

Example function:
image

Expected behavior

Expected export type:
Two I32 as parameters and one I32 as the result
image

Actual behavior

Actual export type:
Six I32 as parameters and one I32 as the result
image

Regression?

No response

Known Workarounds

No response

Configuration

Full example can be found here: https://github.com/silesmo/wasm-unmanaged-callers-bug

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 11, 2024
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 11, 2024
@lambdageek lambdageek added the arch-wasm WebAssembly architecture label Jan 11, 2024
@ghost
Copy link

ghost commented Jan 11, 2024

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

When building an application targeting wasm and wasi and exporting it a function using UnmanagedCallersOnly results in the incorrect wasm type to be exported in the built wasm binary.

This is blocking progress with https://github.com/bytecodealliance/wit-bindgen for the mono implementation.

It's also worth mentioning that it's not generating the _initialize function instead of _start for libraries which is what we do with native aot llvm.

@AaronRobinsonMSFT @lewing @yowl @jsturtevant

Reproduction Steps

Example function:
image

Expected behavior

Expected export type:
Two I32 as parameters and one I32 as the result
image

Actual behavior

Actual export type:
Six I32 as parameters and one I32 as the result
image

Regression?

No response

Known Workarounds

No response

Configuration

Full example can be found here: https://github.com/silesmo/wasm-unmanaged-callers-bug

Other information

No response

Author: silesmo
Assignees: -
Labels:

arch-wasm, untriaged, needs-area-label

Milestone: -

@lambdageek lambdageek added area-Interop-mono and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 11, 2024
@lambdageek lambdageek added this to the 9.0.0 milestone Jan 11, 2024
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jan 11, 2024
@lambdageek
Copy link
Member

/cc @kg

@kg
Copy link
Contributor

kg commented Jan 11, 2024

Description

When building an application targeting wasm and wasi and exporting it a function using UnmanagedCallersOnly results in the incorrect wasm type to be exported in the built wasm binary.

This is blocking progress with https://github.com/bytecodealliance/wit-bindgen for the mono implementation.

It's also worth mentioning that it's not generating the _initialize function instead of _start for libraries which is what we do with native aot llvm.

Related: #96419

@AaronRobinsonMSFT @lewing @yowl @jsturtevant

Reproduction Steps

Example function: image

Expected behavior

Expected export type: Two I32 as parameters and one I32 as the result image

Actual behavior

Actual export type: Six I32 as parameters and one I32 as the result image

Regression?

No response

Known Workarounds

No response

Configuration

Full example can be found here: https://github.com/silesmo/wasm-unmanaged-callers-bug

Other information

No response

In these screenshots you're comparing roundtrip-s8 and roundtrip-u8. Can you show me the definition of roundtrip-s8?

What tool are you using to disassemble the module? Are you sure the function labelled roundtrip-u8 is actually roundtrip-u8?

@silesmo
Copy link
Author

silesmo commented Jan 12, 2024

Description

When building an application targeting wasm and wasi and exporting it a function using UnmanagedCallersOnly results in the incorrect wasm type to be exported in the built wasm binary.
This is blocking progress with https://github.com/bytecodealliance/wit-bindgen for the mono implementation.
It's also worth mentioning that it's not generating the _initialize function instead of _start for libraries which is what we do with native aot llvm.
Related: #96419
@AaronRobinsonMSFT @lewing @yowl @jsturtevant

Reproduction Steps

Example function: image

Expected behavior

Expected export type: Two I32 as parameters and one I32 as the result image

Actual behavior

Actual export type: Six I32 as parameters and one I32 as the result image

Regression?

No response

Known Workarounds

No response

Configuration

Full example can be found here: https://github.com/silesmo/wasm-unmanaged-callers-bug

Other information

No response

In these screenshots you're comparing roundtrip-s8 and roundtrip-u8. Can you show me the definition of roundtrip-s8?

What tool are you using to disassemble the module? Are you sure the function labelled roundtrip-u8 is actually roundtrip-u8?

Oh sorry I had accidentally taken the wrong picture. But this still holds true. You can see the full definition here https://github.com/silesmo/wasm-unmanaged-callers-bug/blob/main/mono-example/wit.exports.test.numbers.TestInterop.cs.

The tool for disassembly is https://wa2.dev/ and I'm sure it's correct. I built it myself and it have gone through lots of testing and several thousand users each month.

@kg

@silesmo
Copy link
Author

silesmo commented Jan 12, 2024

roundtrip-u8 was just an example from the example project. There are many more exports that get the wrong export type in that project.

@kg
Copy link
Contributor

kg commented Jan 12, 2024

roundtrip-u8 was just an example from the example project. There are many more exports that get the wrong export type in that project.

OK, it's helpful to know that it's not just one specific export. I was looking through the code in the repository and couldn't come up with an explanation for why only that one would be affected.

@silesmo
Copy link
Author

silesmo commented Jan 12, 2024

I have looked into it a bit more this morning. There might actually be something wrong happening with the export types in the tool in certain scenarios. I will get back to you on this. I will get back with the findings @kg

@silesmo
Copy link
Author

silesmo commented Jan 12, 2024

Well that's embarrassing. There was indeed an issue with the export types not being displayed correctly @kg.
I have fixed it in the tool and the new version is deployed. Sorry about that.

@silesmo
Copy link
Author

silesmo commented Jan 12, 2024

The module can't be initialized without enabling the threads proposal in Wasmtime which is not something that is currently supported with wasi_preview2 with the component model. Can a dependence on the threads proposal please be removed until the shared everything threads proposal have been implemented. #96629.

This makes us unable to initialize the module in the component model.
@kg

This can be tested using wasmtime run --invoke test-imports -Spreview2=n -Sthreads=n ./csharp-wasm.wasm

@kg
Copy link
Contributor

kg commented Jan 12, 2024

Well that's embarrassing. There was indeed an issue with the export types not being displayed correctly @kg. I have fixed it in the tool and the new version is deployed. Sorry about that.

Nothing to be ashamed of, I have my own wasm decompiler that has had similar problems before. It's a tricky spec and I bear some of the responsibility for that...

The module can't be initialized without enabling the threads proposal in Wasmtime which is not something that is currently supported with wasi_preview2 with the component model. Can a dependence on the threads proposal please be removed until the shared everything threads proposal have been implemented. #96629.

This makes us unable to initialize the module in the component model. @kg

This can be tested using wasmtime run --invoke test-imports -Spreview2=n -Sthreads=n ./csharp-wasm.wasm

We can definitely look into why threads support is required. Normally, it shouldn't be required since multi-threaded builds of the runtime are not the default.

@lewing lewing self-assigned this Jan 26, 2024
@lewing
Copy link
Member

lewing commented Jan 26, 2024

We can definitely look into why threads support is required. Normally, it shouldn't be required since multi-threaded builds of the runtime are not the default.

Yeah, it is very odd that there would be any thread dependency in the wasi build and last time I tested there was none. Sometime could have slipped in with the build unification. We'll look into it when we have some capacity.

@lewing lewing assigned kg Feb 5, 2024
@lewing
Copy link
Member

lewing commented Apr 1, 2024

@silesmo I wasn't seeing the threading requirement on recent builds, are you still seeing it?

@lewing
Copy link
Member

lewing commented Apr 11, 2024

I believe this was fixed in #100652 and I've tested locally, the test case was dependent on a particular name mangling semantic and I've updated the generator to follow the same pattern

@lewing lewing closed this as completed Apr 11, 2024
@silesmo
Copy link
Author

silesmo commented May 6, 2024

Sorry, been a bit swamped recently. I will verify if this has been resolved and get back to you, this week.

Thank you for looking into it!

@silesmo
Copy link
Author

silesmo commented May 12, 2024

This issue seem to be resolved as you said @lewing. There however seems to be a few other issues related to the initialization of a module with the component model. Essentially related to this: bytecodealliance/preview2-prototyping#99

Specifically the part about reactors having an initialization function.

See this one and linked PR for how it was done for Native AOT LLVM dotnet/runtimelab#2359

I can create a separate issue about it in the coming week to start the discussion how we can tackle that for mono.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly architecture area-Interop-mono
Projects
None yet
Development

No branches or pull requests

4 participants