Skip to content

Calls from C to Rust get miscompiled on Windows 64 #24427

Closed
@vosen

Description

@vosen

Rust code:

#[repr(C)]
pub struct Data {
    bar: u64,
    baz: u64,
}

#[no_mangle]
pub extern fn foo(a1: Data,
                  a2: Data,
                  a3: Data,
                  a4: Data,
                  a5: Data) {
    println!("{:}", a1.bar);
    println!("{:}", a2.bar);
    println!("{:}", a3.bar);
    println!("{:}", a4.bar);
    println!("{:}", a5.bar);
}


extern {
    fn c_main();
}

fn main() {
    unsafe { c_main() };
}

C code:

#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:

rustc 1.0.0-nightly (6790b0e51 2015-04-11) (built 2015-04-12)
binary: rustc
commit-hash: 6790b0e51967b1487728d155e0800a1ed03a30d3
commit-date: 2015-04-11
build-date: 2015-04-12
host: x86_64-pc-windows-gnu
release: 1.0.0-nightly

Compiled and ran with gcc main.c -c -o libcmain.a && rustc main.rs -l cmain -L. && main.exe

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-windowsOperating system: Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions