You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calls from C to Rust get miscompiled on Windows 64 #24427
#include <stdint.h>
typedef struct data {
uint64_t bar;
uint64_t baz;
} data;
void foo(data a1,
data a2,
data a3,
data a4,
data a5);
void c_main(void) {
data a1;
a1.bar = 1;
data a2;
a2.bar = 2;
data a3;
a3.bar = 3;
data a4;
a4.bar = 4;
data a5;
a5.bar = 5;
foo(a1, a2, a3, a4, a5);
}
Expected output:
1
2
3
4
5
Actual output:
1
2
3
4
2357776
I couldn't reproduce it with larger number of smaller arguments (eg. passing ten u64s), couldn't reproduce it on 32 bits either. Changing debug/optimization options has no effect.
Works correctly when calling C from C (eg. using this code).
It originally manifested on another 64 bit machine, but I don't have access to it at the moment, though I can get its enviroment too.
LLVM IR: here
Assembly: here
Gcc version: 4.9.2 x86_64-pc-msys (verbose output)
Rust version:
Looks like there a slight misunderstanding about the calling convention between GCC and LLVM.
GCC passes a pointer to a5, whereas LLVM expects a5 to be by-value. Changing the last line of foo() to this ↓ produces the expected result.
unsafe {
let x5: *const Data = std::mem::transmute(a5.bar);
println!("{:}", (*x5).bar);
}
The IR generated by rustc looks correct... unless I am missing some implication of the byval attribute.
You forgot to specify the calling convention for your Rust function, so it is using the rust call convention which is different from the C calling convention.
Once you've set the calling convention from both sides to ensure they match up, if it still doesn't work, then you have an actual bug.
No, looking at the --pretty=expanded output, function convention correctly defaults to pub extern "win64" fn foo(...).
Also, I forgot to mention in the original post, but, I've tried every calling convention mentioned in the FFI part of the book.
Activity
vadimcn commentedon Apr 16, 2015
Looks like there a slight misunderstanding about the calling convention between GCC and LLVM.
GCC passes a pointer to
a5, whereas LLVM expectsa5to be by-value. Changing the last line offoo()to this ↓ produces the expected result.The IR generated by rustc looks correct... unless I am missing some implication of the
byvalattribute.Hmm... LLVM codegen bug?
retep998 commentedon Apr 17, 2015
You forgot to specify the calling convention for your Rust function, so it is using the rust call convention which is different from the C calling convention.
Once you've set the calling convention from both sides to ensure they match up, if it still doesn't work, then you have an actual bug.
vosen commentedon Apr 17, 2015
No, looking at the
--pretty=expandedoutput, function convention correctly defaults topub extern "win64" fn foo(...).Also, I forgot to mention in the original post, but, I've tried every calling convention mentioned in the FFI part of the book.
retep998 commentedon Apr 17, 2015
Okay, I tested this locally, made sure the calling conventions match up, and yet I can still reproduce this issue.
pravic commentedon Apr 8, 2016
This also was fixed by #29012, btw.
alexcrichton commentedon Apr 8, 2016
Yay!