1
votes

I can call my test Rust program with integers as input and handle these fine, even without reference to ctypes. However I cannot seem to get a string without segfaulting in Rust.

Here is my test Rust code:

use std::env;

#[no_mangle]
pub extern fn helloworld(names: &str ) {
  println!("{}", names);
  println!("helloworld...");
}

#[no_mangle]
pub extern fn ihelloworld(names: i32 ) {
  println!("{}", names);
  println!("ihelloworld...");
}

ihelloworld works just fine. But I cannot find a way to get a string from python into Rust even if I use ctypes.

Here is the calling Python code:

import sys, ctypes, os
from ctypes import cdll
from ctypes import c_char_p
from ctypes import *


if __name__ == "__main__":
    directory = os.path.dirname(os.path.abspath(__file__))
    lib = cdll.LoadLibrary(os.path.join(directory, "target/release/libembeded.so"))

    lib.ihelloworld(1)
    lib.helloworld.argtypes = [c_char_p]
    #lib.helloworld(str("test user"))
    #lib.helloworld(u'test user')
    lib.helloworld(c_char_p("test user"))

    print("finished running!")

The output is:

1
ihelloworld...
Segmentation fault (core dumped)

The ihellowworld Rust function works just fine, but I cannot seem to get helloworld working.

2
@Shepmaster Thanks, I had a look, I presume Omnibus is yours - its good for covering these little points which I cannot seem to find in the Rust documentation. - disruptive

2 Answers

1
votes

The string sent from Python should be represented in Rust as a CString instead.

1
votes

I used the Rust FFI Omnibus and now my code seems to work just fine.

use std::env;
use std::ffi::{CString, CStr};
use std::os::raw::c_char;

#[no_mangle]
pub extern "C" fn helloworld(names: *const c_char) {

    unsafe {
        let c_str = CStr::from_ptr(names).to_str().unwrap();
        println!("{:?}", c_str);

    }
    println!("helloworld...");

}