Skip to content

Struct passed by-value from C to Rust get's corrupted (using FFI and .dll from Visual Studio) #11343

Closed
@Matthias247

Description

@Matthias247

I'm interfacing an external DLL which I'm creating with Visual Studio 2012.

I've tried to pass a struct by-value from C to Rust in a callback.
The struct and the callback are defined as follows:

pub struct Value {
    a: u32,
    b: i32,
    c: i32,
    d: u8,
    e: u8,
    f: u8,
    g: u8
}

#[deriving(Clone,Eq)]
pub struct Notification {
    notification_type: i32,
    value_id: Value,
    data: u8
}

extern "C" fn on_notification(notification_target: *mut LibWrap, notification: Notification) {
  ...
}

However the struct received in on_notification is corrupted and contains not the data which is sent in C++.

I wrote a test program to isolate and reproduce the issue.
The gist therefore is here: https://gist.github.com/Matthias247/8283183
It can be cloned from https://gist.github.com/8283183.git
It includes the source code for the library with a visual studio project to built it and the rust test program.
I also included make.sh to build it on Linux. However in Linux I can't reproduct the issue. The struct get's copied correctly there.

This is the output from my test program:

Starting
rust notification size: 24
rust value size: 16
Initializing library
C++ notification size: 24
C++ value size: 16
 1  0  0  0  2  0  0  0  3  0  0  0  4  0  0  0  5  6  7  8  9 CC CC CC
 1  0  0  0  2  0  0  0  3  0  0  0  4  0  0  0  5  9 48 64 80 CC CC CC
Notification: Notification{notification_type: 1i32, value_id: Value{a: 2u32, b:
3i32, c: 4i32, d: 5u8, e: 9u8, f: 72u8, g: 100u8}, data: 128u8}

The first line is the content of the notification in bytes in C++, the second the content in Rust.
You can see that the last bytes of the notification structure are different in C++ in Rust. Interesting hereby is also that the last field (notification.data == 9) gets copied into notification.value_id.d (verified this with using other values for notification.data) and the remaining notification fields are garbage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions