Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNI bugfixes #1687

Merged
merged 6 commits into from
Feb 21, 2025
Merged

JNI bugfixes #1687

merged 6 commits into from
Feb 21, 2025

Conversation

str4d
Copy link
Contributor

@str4d str4d commented Feb 19, 2025

This fixes multiple JNI type signature inconsistencies, and cleans up argument handling on the Rust side to be less error-prone.

fixes #1657, fixes #1684

Note
This code review checklist is intended to serve as a starting point for the author and reviewer, although it may not be appropriate for all types of changes (e.g. fixing a spelling typo in documentation). For more in-depth discussion of how we think about code review, please see Code Review Guidelines.

Author

  • Self-review your own code in GitHub's web interface1
  • Add automated tests as appropriate
  • Update the manual tests2 as appropriate
  • Check the code coverage3 report for the automated tests
  • Update documentation as appropriate (e.g README.md, Architecture.md, etc.)
  • Run the demo app and try the changes
  • Pull in the latest changes from the main branch and squash your commits before assigning a reviewer4

Reviewer

  • Check the code with the Code Review Guidelines checklist
  • Perform an ad hoc review5
  • Review the automated tests
  • Review the manual tests
  • Review the documentation, README.md, Architecture.md, etc. as appropriate
  • Run the demo app and try the changes6

Footnotes

  1. Code often looks different when reviewing the diff in a browser, making it easier to spot potential bugs.

  2. While we aim for automated testing of the SDK, some aspects require manual testing. If you had to manually test
    something during development of this pull request, write those steps down.

  3. While we are not looking for perfect coverage, the tool can point out potential cases that have been missed. Code coverage can be generated with: ./gradlew check for Kotlin modules and ./gradlew connectedCheck -PIS_ANDROID_INSTRUMENTATION_TEST_COVERAGE_ENABLED=true for Android modules.

  4. Having your code up to date and squashed will make it easier for others to review. Use best judgement when squashing commits, as some changes (such as refactoring) might be easier to review as a separate commit.

  5. In addition to a first pass using the code review guidelines, do a second pass using your best judgement and experience which may identify additional questions or comments. Research shows that code review is most effective when done in multiple passes, where reviewers look for different things through each pass.

  6. While the CI server runs the demo app to look for build failures or crashes, humans running the demo app are
    more likely to notice unexpected log messages, UI inconsistencies, or bad output data. Perform this step last, after verifying the code changes are safe to run locally.

Unlike a C FFI where we need some mechanism to signal an error state,
with JNI thrown exceptions trigger as soon as the JNI function returns.
These functions had no declared return value on the Kotlin side and
thus were not using the returned boolean in any way; this change fixes
the type signatures to match.
@@ -1809,7 +1802,7 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_proposeTr
let network = parse_network(network_id as u32)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 1801 the wrong method name is used in the info_span!.

Comment on lines +1859 to +1863
let memo = utils::java_nullable_bytes_to_rust(env, &memo)?
.as_deref()
.map(MemoBytes::from_bytes)
.transpose()
.map_err(|e| anyhow!("Invalid MemoBytes: {}", e))?;
Copy link
Contributor

@daira daira Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern occurs twice (also in RustBackend.proposeShielding); consider adding a nullable_memo_bytes_from_jni helper.

Comment on lines +560 to +563
let seed_fingerprint =
utils::java_nullable_bytes_to_rust(env, &seed_fingerprint_bytes)?
.and_then(|b| b.as_slice().try_into().ok())
.map(SeedFingerprint::from_bytes);
Copy link
Contributor

@daira daira Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this happens to only be used once, it's doing the same kind of thing as the other conversion functions, so I might be inclined to add a nullable_seed_fingerprint_from_jni helper anyway.

Comment on lines +831 to 834
let addr = utils::java_string_to_rust(env, &addr)?;

match Address::decode(&network, &addr) {
Some(addr) => match addr {
Copy link
Contributor

@daira daira Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this helper:

fn parse_address(env: &JNIEnv, network_id: jint, addr: JString) -> anyhow::Result<ZcashAddress> {
    let network = parse_network(network_id as u32)?;
    let addr = utils::java_string_to_rust(env, &addr)?;
    Address::decode(&network, &addr).ok_or(anyhow!("Address is for the wrong network"))
}

Then lines 830-839 become just:

            match parse_address(network_id, addr)? {
                Address::Sapling(_) => Ok(JNI_TRUE),
                Address::Transparent(_) | Address::Unified(_) | Address::Tex(_) => Ok(JNI_FALSE),
            }

This can also be used in isValid{Transparent,Unified,Tex}Address, saving 15 non-blank lines overall I think.

@@ -2036,12 +2025,12 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_createPro
let network = parse_network(network_id as u32)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 2024 the wrong method name is used in the info_span!.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked all other uses of info_span! and made comments for any inconsistencies.

@@ -2169,8 +2157,7 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_requiresS
let res = catch_unwind(&mut env, |env| {
let _span = tracing::info_span!("RustBackend.pcztRequiresSaplingProofs").entered();
Copy link
Contributor

@daira daira Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the name used in Kotlin at the wrapper layer, but at the JNI layer it is called requiresSaplingProofs. In other cases the name is the same. Rename the JNI method to pcztRequiresSaplingProofs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot rename the JNI method, because the arguments between the extern function and the Backend trait method are identical. You'll see I use different names for several such methods, whereas when the Backend trait method has fewer arguments (e.g. due to us inserting a db_data path for the JNI) we use the same method name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to systematically use a different name (e.g. add an ffi prefix), rather than sometimes use the same name and sometimes a different one.

@@ -2169,8 +2157,7 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_requiresS
let res = catch_unwind(&mut env, |env| {
let _span = tracing::info_span!("RustBackend.pcztRequiresSaplingProofs").entered();

let pczt = Pczt::parse(&env.convert_byte_array(pczt)?[..])
.map_err(|e| anyhow!("Invalid PCZT: {:?}", e))?;
let pczt = parse_pczt(env, &pczt)?;

let prover = Prover::new(pczt);

Copy link
Contributor

@daira daira Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside (just below): I'm perplexed that there is no From<bool> for jboolean. Doesn't matter I guess; this pattern only occurs once. Filed jni-rs/jni-rs#564

@@ -1050,7 +1050,7 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_writeBloc
_: JClass<'local>,
db_cache: JString<'local>,
block_meta: JObjectArray<'local>,
) -> jboolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same argument applies to initBlockMetaDb: the return value is always discarded.

Copy link
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK with non-blocking suggestions. The errors in info_span! strings should be addressed but it would be fine to do so in another PR.

Note that, as pointed out in #1688, there are easily fixable clippy lint failures. Also needs ktlint --format.

env.get_string(jstring)
.expect("Couldn't get Java string!")
.into()
pub(crate) fn java_bytes_to_rust(env: &JNIEnv, jbytes: &JByteArray) -> anyhow::Result<Vec<u8>> {
Copy link
Contributor

@daira daira Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be jbytes: JByteArray since it is effectively already a pointer (see the comment on parse_secret).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is because the method it wraps takes a reference, and this method doesn't do anything special on top (the type-specific helper methods do by comparison; they effectively are converting type, so we want to move there).

(!jstring.is_null()).then(|| java_string_to_rust(env, jstring))
pub(crate) fn java_nullable_bytes_to_rust(
env: &JNIEnv,
jbytes: &JByteArray,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be jbytes: JByteArray since it is effectively already a pointer (see the comment on parse_secret).

.transpose()
}

pub(crate) fn java_string_to_rust(env: &mut JNIEnv, jstring: &JString) -> anyhow::Result<String> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be jstring: JString since it is effectively already a pointer (see the comment on parse_secret).


pub(crate) fn java_nullable_string_to_rust(
env: &mut JNIEnv,
jstring: &JString,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be jstring: JString since it is effectively already a pointer (see the comment on parse_secret).

Copy link
Contributor

@nuttycom nuttycom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK c78afc7

Copy link
Contributor

@nuttycom nuttycom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 5529922 with suggestion

Comment on lines +2700 to +2710
impl zcash_address::TryFromRawAddress for UnifiedAddressParser {
type Error = anyhow::Error;

fn try_from_raw_unified(
data: zcash_address::unified::Address,
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
data.try_into()
.map(UnifiedAddressParser)
.map_err(|e| anyhow!("Invalid Unified Address: {}", e).into())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use https://github.com/zcash/librustzcash/blob/main/zcash_keys/src/address.rs#L28?

It's really weird to me to have to go through TryFromRawAddress for these straightforward operations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I mean, why not use it directly?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using that; it's the data.try_into() call. The rest of the TryFromRawAddress machinery here is ensuring that if someone passes a string that isn't a UA (and is instead some other kind of address, or not an address at all), we get correct errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. So, looking at the call sites of parse_ua, what is the reason to restrict these to unified addresses? If we parsed an arbitrary ZcashAddress at these call sites, we could still extract the transparent and/or Sapling receivers if present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be worth considering, but that's outside the scope of this PR.

@str4d str4d merged commit 7a8eb9c into rust-updates Feb 21, 2025
10 of 12 checks passed
@str4d str4d deleted the jni-bugfixes branch February 21, 2025 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants