From a46cc8c472582d1b094f73ded7d691c4695bed0d Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 11 Nov 2021 17:34:41 +0000 Subject: [PATCH] Hold a reference when we pass Option to rust This resolves a NPE when calling trait methods if the user doesn't hold their own reference to the underlying trait, which is quite possible for, eg, the `Filter` object. We also adapt `HumanObjectPeerTest` to test this with a `Filter`. --- gen_type_mapping.py | 4 +++- src/test/java/org/ldk/HumanObjectPeerTest.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/gen_type_mapping.py b/gen_type_mapping.py index cffdc839..39ee2605 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -371,6 +371,7 @@ class TypeMappingGenerator: # underlying unlike Vecs, and it gives Java more freedom. base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");" if ty_info.rust_obj in self.complex_enums: + to_hu_conv_sfx = "" if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns: # We really need a full clone here, but for now we just implement # a manual clone explicitly for Options @@ -378,6 +379,7 @@ class TypeMappingGenerator: assert ty_info.rust_obj.startswith("LDKCOption") # We don't support contained traits for anything else yet optional_ty = ty_info.rust_obj[11:-1] assert "LDK" + optional_ty in self.trait_structs # We don't support contained traits for anything else yet + to_hu_conv_sfx = "this.ptrs_to.add(" + ty_info.var_name + ")" base_conv += "\nif (" + ty_info.var_name + "_conv.tag == " + ty_info.rust_obj + "_Some) {" base_conv += "\n\t// Manually implement clone for Java trait instances" optional_ty_info = self.java_c_types("LDK" + optional_ty + " " + ty_info.var_name, None) @@ -393,7 +395,7 @@ class TypeMappingGenerator: arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = ret_conv, ret_conv_name = ty_info.var_name + "_ref", to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");\n" + ty_info.var_name + "_hu_conv.ptrs_to.add(this);", - to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + ".ptr", "")) + to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + ".ptr", to_hu_conv_sfx)) if ty_info.rust_obj in self.result_types: if holds_ref: # If we're trying to return a ref, we have to clone. diff --git a/src/test/java/org/ldk/HumanObjectPeerTest.java b/src/test/java/org/ldk/HumanObjectPeerTest.java index a123b6e3..b51df842 100644 --- a/src/test/java/org/ldk/HumanObjectPeerTest.java +++ b/src/test/java/org/ldk/HumanObjectPeerTest.java @@ -174,7 +174,7 @@ class HumanObjectPeerTestInstance { NetGraphMsgHandler route_handler; final Watch chain_watch; final HashSet filter_additions; - final Option_FilterZ filter; + Option_FilterZ filter; ChannelManager chan_manager; PeerManager peer_manager; final HashMap monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here. @@ -387,6 +387,7 @@ class HumanObjectPeerTestInstance { } this.node_id = chan_manager.get_our_node_id(); + this.filter = null; bind_nio(); System.gc(); } @@ -493,6 +494,7 @@ class HumanObjectPeerTestInstance { // the ChannelSigner. this.ptr_to = orig.chan_manager; } + this.filter = null; } TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] connect_block(Block b, int height, long expected_monitor_update_len) { -- 2.30.2