FFI complex math does not work on 32 bit machine with rust 1.2


#1

Here is basically the same example code from: http://rustbyexample.com/std_misc/ffi.html

Cargo.toml

[package]
name = "complex"
version = "0.1.0"
authors = ["andres"]

[dependencies]
libc = "*"

main.rs

extern crate libc;

#[repr(C)]
#[derive(Clone, Copy)]
pub struct FFIComplex32 {
    pub re: libc::c_float,
    pub im: libc::c_float,
}

#[link(name="m")]
extern {
    pub fn cexpf(z: FFIComplex32) -> FFIComplex32;
}

fn main() {
    let c = unsafe {cexpf(FFIComplex32 {re: 0.0, im: 0.0})};

    println!("Hello, world!");
}

The problem is that it only works on 64 bit machine.
If I try the same thing on 32 bit Ubuntu, I got:

rust-gdb -statistics --args ./target/debug/complex
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Command execution time: 0.000000 (cpu), 0.000003 (wall)
Space used: 1269760 (+0 for this command)
Reading symbols from ./target/debug/complex...done.
(gdb) run
Starting program: /mnt/hgfs/Development/cubehub/complex/target/debug/complex
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
Hello, world!

Program received signal SIGSEGV, Segmentation fault.
0xbf800000 in ?? ()
Command execution time: 0.112000 (cpu), 0.118593 (wall)
Space used: 5021696 (+3379200 for this command)

rustc 1.2.0 (082e47636 2015-08-03)


#2

Bug filed in rust-by-example to use a different example – it’s not portable.


#3

RFC issue tracking adding some way to interface with C’s complex.