Skip to content

sparc ABI issue when C program calls Rust function which returns structure #52638

Closed
@psumbera

Description

@psumbera

This was original encountered with Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1476252

Following is sample program which builds on https://github.com/alexcrichton/rust-ffi-examples

The issue is that on Sparc (tested on Solaris) the returing structure contains just zeros (on intel Solaris it works as expected).

git clone https://github.com/alexcrichton/rust-ffi-examples.git
cd rust-ffi-examples
gpatch -p1 <<EOF
index 78b6a46..0a4f23b 100644
--- a/cpp-to-rust/src/example.h
+++ b/cpp-to-rust/src/example.h
@@ -7,6 +7,12 @@ extern "C"{

 int double_input(int input);

+struct MyStruct {
+  bool a;
+};
+
+const MyStruct GetMyStruct();
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/cpp-to-rust/src/lib.rs b/cpp-to-rust/src/lib.rs
index bf573d1..96270c2 100644
--- a/cpp-to-rust/src/lib.rs
+++ b/cpp-to-rust/src/lib.rs
@@ -2,3 +2,16 @@
 pub extern fn double_input(input: i32) -> i32 {
     input * 2
 }
+
+#[repr(C)]
+#[derive(Debug, Copy, Clone)]
+pub struct MyStruct {
+    pub a: bool,
+}
+
+#[no_mangle]
+pub extern fn GetMyStruct() -> MyStruct {
+    MyStruct {
+        a: true,
+    }
+}
diff --git a/cpp-to-rust/src/main.cpp b/cpp-to-rust/src/main.cpp
index e4c5ced..f66c298 100644
--- a/cpp-to-rust/src/main.cpp
+++ b/cpp-to-rust/src/main.cpp
@@ -7,6 +7,8 @@ int main() {
   int input = 10;
   int output = double_input(input);
   cout<<input<<" * 2 = "<<output<<"\n";
+  const MyStruct mystruct = GetMyStruct();
+  cout<<"GetMyStruct returns "<<mystruct.a<<"\n";

   return 0;
 }
EOF
cd cpp-to-rust
gmake
10 * 2 = 20
GetMyStruct returns 0  <==== Should be 1!!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)O-SPARCTarget: SPARC processorsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions