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

Incorrect Calling Conventions on Windows #188

Closed
CryZe opened this issue Feb 16, 2019 · 9 comments
Closed

Incorrect Calling Conventions on Windows #188

CryZe opened this issue Feb 16, 2019 · 9 comments
Labels
bug Something isn't working

Comments

@CryZe
Copy link
Contributor

CryZe commented Feb 16, 2019

Unfortunately #183 isn't actually quite resolved yet as the same problem exists for the wasm code calling into the host with an f64 argument. I believe we are dealing with S2 then or something (because of the additional context argument)? I haven't yet investigated this, so I don't quite know enough details yet.

@CryZe
Copy link
Contributor Author

CryZe commented Feb 16, 2019

It seems like the problem there is that ctx is actually passed after the f64 or something. f64 is in the XMM0 register even though the ABI requires it to be in the XMM1 register if it's the second argument, which is also not the case for Linux, so this would explain this being a Windows only problem again. I think I saw the ctx argument being moved to the first parameter in some PR. Maybe it didn't do that all the way?

Here you can see XMM1 being used:
https://rust.godbolt.org/z/v4BTVw

Here's the jitted code filling the value into XMM0:

https://i.imgur.com/y3zHMDn.png

Alternatively, cranelift may not know about the Windows calling convention and thus just fill the value into XMM0 like it would on Linux (either it isn't being told about it, or it doesn't support it yet).

@CryZe
Copy link
Contributor Author

CryZe commented Feb 16, 2019

https://i.imgur.com/8Jgwgh9.png

Defining the wrap function to always be of "sysv64" calling convention fixes the bug, but that seems more like a workaround rather than a proper fix, as cranelift should probably just emit the correct calling convention in the first place. I don't know where this is done though. (Update: This fixes some things but breaks others, I'm thinking cranelift actually is going for the windows calling convention but the ctx parameter isn't implemented correctly)

I'd encourage some maintainer to verify if all the calling conventions between the jit'ed code and rust are actually correct (all the trampolines, libcalls, ...).

@CryZe CryZe changed the title f64 arguments are broken on Windows as well Incorrect Calling Conventions on Windows Feb 16, 2019
@lachlansneff
Copy link
Contributor

@CryZe Thanks for the deep dive! I "own" most of the runtime code, so I'll take a look at this on Monday. It's probably the calling convention getting set up wrong somewhere.

@lachlansneff
Copy link
Contributor

@CryZe Sorry this took so long, but I've just had time to investigate and indeed yes, cranelift implements the x64 calling convention incorrectly. I honestly don't blame them, it seems to waste registers.

Here's the spec: https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2017#example-of-argument-passing-3---mixed-ints-and-floats

@syrusakbary
Copy link
Member

We created an issue in Cranelift: bytecodealliance/cranelift#691

@syrusakbary
Copy link
Member

syrusakbary commented Mar 15, 2019

This issue was fixed in: bytecodealliance/cranelift#701 (related issue in cranelift: bytecodealliance/cranelift#691)

Once we update the Cranelift version the issue should be fixed in our runtime.

Keeping the issue open until then!

@syrusakbary syrusakbary added the bug Something isn't working label Mar 15, 2019
@CryZe
Copy link
Contributor Author

CryZe commented Mar 15, 2019

The implementation was unfortunately incorrect. So they still need to fix that. (Oh wait, they fixed that too).

@hrydgard
Copy link

I had similar problems with floating point arguments on Windows, but indeed appears to work correctly on master with the the updated cranelift.

Are you guys planning to tag a new release in the near future? Would be convenient :)

@CryZe
Copy link
Contributor Author

CryZe commented Apr 27, 2019

This seems to be resolved :)

@CryZe CryZe closed this as completed Apr 27, 2019
nlewycky pushed a commit that referenced this issue Aug 13, 2020
feat(wasm-common) Update feature flags when requiring `wasm-common`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants