json-rust icon indicating copy to clipboard operation
json-rust copied to clipboard

dylib function that returns JsonValue when retrieved produces Segmentation Fault on macOS

Open fasihrana opened this issue 7 years ago • 5 comments

Not sure if macOS is supported or not, but the following line is producing a Segmentation Fault. Same thing works perfectly fine on Linux and Windows.

let findfuncs: lib::Symbol<fn() -> JsonValue> = dylib.get(b"methods")?;

fasihrana avatar Sep 01 '17 07:09 fasihrana

There is nothing OS specific in the code, I'm on linux now but the crate was originally built on OSX.

Do you have an example on GitHub that I could run or look into with dependencies?

maciejhirsz avatar Sep 01 '17 11:09 maciejhirsz

Yeap. The code is as follows:

Code for the dynamic library function is as follows:

#[macro_use]
extern crate json;

use json::JsonValue;

#[no_mangle]
pub fn methods() -> JsonValue
{
	let rt = array![
		object!{
			"fn" => "method 1"
    		}
	];
	rt
}

Code for my main.rs file is as follows:

extern crate json;
extern crate libloading as lib;

use std::env;
use json::JsonValue;

unsafe fn call_dynamic(libname: &String) -> lib::Result<()> {
	let dylib = try!(lib::Library::new(libname));
	let findmethods: lib::Symbol<fn() -> JsonValue> = dylib.get(b"methods")?;
	let funcs = findmethods();
	println!("{:#}", funcs);
	Ok(())
}

fn main(){
	let args: Vec<String> = env::args().collect();
	unsafe {
		call_dynamic(&args[1]).unwrap();
	}
}

fasihrana avatar Sep 01 '17 13:09 fasihrana

Not sure the issue is with json-rust here.

  • Can you try to reproduce it if the dynamic function returns something like Vec<Box<u32>> with few entries?
  • If not, can you reproduce it with JsonValue that doesn't use objects (nested array with string etc.)?

The only thing I can think of is that I'm passing around raw pointers in object internals, which should be memory safe but aren't checked by the compiler (for obvious reasons). If there is an issue with memory safety, that would help me narrow it down.

maciejhirsz avatar Sep 01 '17 14:09 maciejhirsz

I think this maybe a macOS specific error in libloading itself.

I created a simple enum in a module which i reference in both my dynamic lib and also in my executable. As soon as I try to retrieve the function that returns this enum, it segfaults. It may just be a failure in handling custom return types within libloading.

fasihrana avatar Sep 04 '17 14:09 fasihrana

@fasihrana does it also segfault if you put #[repr(C)] attribute on your enums? If not, I could potentially add that as behind a compiler feature flag to json-rust.

maciejhirsz avatar Sep 04 '17 15:09 maciejhirsz