0

I am writing some Rust code that uses a Rust library that doesn't seem to have a corresponding Java library (didcomm-rs).

Based on examples I've found, to pass String values between Rust and Java, I can use String in Java, and *const c_char in Rust.
And this seems to work fine, except that I'm running into something strange now.

I have a unit test in Java to test my code and on the last call into Rust it crashes with the following:

The forked VM terminated without properly saying goodbye. VM crash or System.exit called?

I have put println! statements in the Rust code and I can see in the resulting dump file (where these println statements go) that the code does reach the end.

This is the last few lines of code in my Rust library:


    // other code
    let result = to_ptr(base64::encode(&message));
    println!("6");
    result
}

fn to_ptr(string: String) -> *const c_char {
    let cs = CString::new(string.as_bytes()).unwrap();
    let ptr = cs.as_ptr();
    // Tell Rust not to clean up the string while we still have a pointer to it.
    // Otherwise, we'll get a segfault.
    mem::forget(cs);
    ptr
}

I suspect it might be related to either the Rust code or the Java code cleaning up a variable or something like that, but I'm not sure how to debug this.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
Quintonn
  • 770
  • 8
  • 29
  • It looks like something binary & memory related. But on the ligature ffi: java has a java.text.Normalizer which can compose and decompose ligatures too, so one gets instead of one char **ffi** three single chars **f**, **f**, **i**. – Joop Eggen Sep 15 '22 at 11:24
  • Not really an answer to the question but have you tried the jni crate? You can then avoid handling pointers at all for the most part and use normal rust Strings. – Holloway Sep 15 '22 at 11:48
  • there's also https://github.com/sicpa-dlab/didcomm-jvm – Marc Stroebel Sep 15 '22 at 12:02

1 Answers1

0

So my problem ended up not having anything to do with pointers.

I was using the wrong keys/certificates during encryption and decryption of the didcomm-rs library.

Although, running the same code multiple times would result in the Rust code panicking at different locations which really put me off.

For some of the comments, I did look at the the didcomm-jvm library, but it requires implementing some non-trivial interfaces, and for the jni crate, it would require more steps to first create a header file from the Java code and then import that into Rust as opposed to the other way, from what I could tell.
And I was able to solve my problem, so I didn't dive too deep into that.

This ticket can be close/deleted if necessary since the problem and solution don't particularly match

Quintonn
  • 770
  • 8
  • 29