-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Passing many parameters to fun binding violates ABI #4890
Comments
Have you tried using different llvm versions on the compiler? |
I haven't. I wrote a C function however calling the target function like in Crystal, and ran that through clang. The interesting point is that Clang unpacks the structure and then passes the first two as separate parameters (Which should end up just like what Crystal does), but the last parameter is not unpacked, but explicitly passed So my best guess right now is that Crystal not unpacking the structure is fine (as far this issue is concerned), but it has to do another check if the following parameters have to be passed With that, it doesn't look like an issue with alignment at all like I first assumed. Clang
Crystal
|
Unfortunately LLVM knows nothing about the C ABI. Check I can confirm this is a bug in the ABI logic in Crystal. |
Indeed, it seems after I implemented this (more than 2 years ago) Rust's code had many changes. If anyone wants to re-port this code (seems much more complex than before because Rust's code is now a bit tied with Rust's type system), the relevant file in Rust is https://github.com/rust-lang/rust/blob/master/src/librustc_trans/cabi_x86_64.rs and in Crystal it's https://github.com/crystal-lang/crystal/blob/master/src/llvm/abi/x86_64.cr with specs in https://github.com/crystal-lang/crystal/blob/master/spec/compiler/codegen/c_abi/c_abi_x86_64_spec.cr . Of course other ABIs should be updated too. |
BTW I've always been confused by this need to declare an ABI manually. It sounds exactly like something that LLVM should deal with, and looking into LLVM it seems that the ABI is already declared by the Data Layout string, for which we just reuse what LLVM is telling us... |
@ysbaddaden I know. "That's the whole point of using LLVM", I thought. But LLVM doesn't take care of that... just search "llvm c abi" in google and you'll see a bunch of results that clearly show or suggest that LLVM doesn't take care of that. Instead, all that logic is in Clang, and replicated in Rust, Crystal, and I don't know how many other frontends. Had I known that, I would probably have started with a C backend. That is, compile Crystal to C code. Then using clang or any other C compiler would take care of the C ABI... so much simpler. And maybe even faster, LLVM is a turtle. Nim got it right, they compile to C. |
I can't believe that compiling to C would be faster. You have to emit and reparse the C files, plus clang is already the fastest C compiler I believe. I can't think of much other advantages from compiling to C either, apart from the ABI issue, and this is the first ever ABI bug I've seen. Regardless, there's little point discussing what would be impractical to change. |
Hi,
While working on the Qt5 bindings, I noticed that some methods taking many strings crashed the process. Upon investigating it appears that passing much data through parameters breaks after a certain amount of total bytes if a previous parameter was of a different size.
The following test-case was modeled after the original causing method: It takes a pointer, and up to three strings following that. In the bindings, this is done through the pointer, and then passing three times a structure of type
CrystalString
:I was surprised to find that indeed, passing only the structure type and not a pointer before it "fixed" the issue. That's why I assume it's a aligning issue, as per disassembly, the C function expects all of these on the stack.
I may dig around a bit tomorrow.
The text was updated successfully, but these errors were encountered: