From 99de81ade825433c58c5c54be61fb45ae435cca0 Mon Sep 17 00:00:00 2001 From: Viacheslav Kulish <32914239+vcoolish@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:07:14 +0400 Subject: [PATCH 1/5] Fix Java JVM leak --- .../trustwallet/core/GenericPhantomReference.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt b/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt index b6ca8dd3653..71b38813316 100644 --- a/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt +++ b/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt @@ -2,6 +2,9 @@ package com.trustwallet.core import java.lang.ref.PhantomReference import java.lang.ref.ReferenceQueue +import java.util.Collections +import java.util.concurrent.locks.ReentrantLock +import kotlin.concurrent.withLock internal class GenericPhantomReference private constructor( referent: Any, @@ -10,8 +13,9 @@ internal class GenericPhantomReference private constructor( ) : PhantomReference(referent, queue) { companion object { - private val references: MutableSet = HashSet() + private val references: MutableSet = Collections.synchronizedSet(HashSet()) private val queue: ReferenceQueue = ReferenceQueue() + private val lock = ReentrantLock() init { Thread { @@ -33,14 +37,18 @@ internal class GenericPhantomReference private constructor( handle: Long, onDelete: (Long) -> Unit, ) { - references.add(GenericPhantomReference(referent, handle, onDelete)) + lock.withLock { + references.add(GenericPhantomReference(referent, handle, onDelete)) + } } private fun doDeletes() { while (true) { val ref = queue.remove() as GenericPhantomReference ref.onDelete(ref.handle) - references.remove(ref) + lock.withLock { + references.remove(ref) + } } } } From 12a7633b9e84de4a09925e9c45a0eb6e691ecc32 Mon Sep 17 00:00:00 2001 From: Viacheslav Kulish <32914239+vcoolish@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:12:20 +0400 Subject: [PATCH 2/5] clean --- .../com/trustwallet/core/GenericPhantomReference.kt | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt b/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt index 71b38813316..0e2ffdd5b8f 100644 --- a/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt +++ b/kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt @@ -3,8 +3,6 @@ package com.trustwallet.core import java.lang.ref.PhantomReference import java.lang.ref.ReferenceQueue import java.util.Collections -import java.util.concurrent.locks.ReentrantLock -import kotlin.concurrent.withLock internal class GenericPhantomReference private constructor( referent: Any, @@ -15,7 +13,6 @@ internal class GenericPhantomReference private constructor( companion object { private val references: MutableSet = Collections.synchronizedSet(HashSet()) private val queue: ReferenceQueue = ReferenceQueue() - private val lock = ReentrantLock() init { Thread { @@ -37,18 +34,14 @@ internal class GenericPhantomReference private constructor( handle: Long, onDelete: (Long) -> Unit, ) { - lock.withLock { - references.add(GenericPhantomReference(referent, handle, onDelete)) - } + references.add(GenericPhantomReference(referent, handle, onDelete)) } private fun doDeletes() { while (true) { val ref = queue.remove() as GenericPhantomReference ref.onDelete(ref.handle) - lock.withLock { - references.remove(ref) - } + references.remove(ref) } } } From c95da5c0f6b684c7c986f6bebd6521574ee1b777 Mon Sep 17 00:00:00 2001 From: Viacheslav Kulish <32914239+vcoolish@users.noreply.github.com> Date: Fri, 17 Jan 2025 00:19:21 +0400 Subject: [PATCH 3/5] apply to jni --- jni/java/wallet/core/java/GenericPhantomReference.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jni/java/wallet/core/java/GenericPhantomReference.java b/jni/java/wallet/core/java/GenericPhantomReference.java index 1c5ee46dc76..ef7808b902e 100644 --- a/jni/java/wallet/core/java/GenericPhantomReference.java +++ b/jni/java/wallet/core/java/GenericPhantomReference.java @@ -4,12 +4,13 @@ import java.lang.ref.ReferenceQueue; import java.util.Set; import java.util.HashSet; +import java.util.Collections; public class GenericPhantomReference extends PhantomReference { private final long nativeHandle; private final OnDeleteCallback onDeleteCallback; - private static final Set references = new HashSet<>(); + private static final Set references = Collections.synchronizedSet(new HashSet<>()); private static final ReferenceQueue queue = new ReferenceQueue<>(); static { From e3a959b223c2ab195f764345a2d1a5a012beff0f Mon Sep 17 00:00:00 2001 From: Satoshi Otomakan Date: Thu, 16 Jan 2025 23:45:41 +0100 Subject: [PATCH 4/5] [Misc]: Upgrade Rust toolchain to `nightly-2025-01-16` --- tools/install-rust-dependencies | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/install-rust-dependencies b/tools/install-rust-dependencies index 1b12239c010..5ab14b69151 100755 --- a/tools/install-rust-dependencies +++ b/tools/install-rust-dependencies @@ -2,7 +2,7 @@ set -e -NIGHTLY="nightly-2024-06-13" +NIGHTLY="nightly-2025-01-16" rustup toolchain install $NIGHTLY rustup default $NIGHTLY From efceff113dd8b1a2fb6316e365377e20fe86dac0 Mon Sep 17 00:00:00 2001 From: Satoshi Otomakan Date: Fri, 17 Jan 2025 00:01:29 +0100 Subject: [PATCH 5/5] [Misc]: Fix Clippy warnings --- rust/chains/tw_solana/src/transaction/versioned.rs | 2 +- rust/frameworks/tw_ton_sdk/src/boc/binary_writer.rs | 2 +- rust/frameworks/tw_ton_sdk/src/boc/raw.rs | 2 +- rust/tw_any_coin/src/wallet_connect_request.rs | 4 +--- rust/tw_encoding/src/hex.rs | 2 +- rust/tw_evm/src/message/eip712/message_types.rs | 2 +- rust/tw_misc/src/test_utils/json.rs | 4 ++-- 7 files changed, 8 insertions(+), 10 deletions(-) diff --git a/rust/chains/tw_solana/src/transaction/versioned.rs b/rust/chains/tw_solana/src/transaction/versioned.rs index 18b81a81576..a43c73d4380 100644 --- a/rust/chains/tw_solana/src/transaction/versioned.rs +++ b/rust/chains/tw_solana/src/transaction/versioned.rs @@ -189,7 +189,7 @@ impl<'de> Deserialize<'de> for MessagePrefix { { struct PrefixVisitor; - impl<'de> Visitor<'de> for PrefixVisitor { + impl Visitor<'_> for PrefixVisitor { type Value = MessagePrefix; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { diff --git a/rust/frameworks/tw_ton_sdk/src/boc/binary_writer.rs b/rust/frameworks/tw_ton_sdk/src/boc/binary_writer.rs index 688d71f4e18..91d75f4430b 100644 --- a/rust/frameworks/tw_ton_sdk/src/boc/binary_writer.rs +++ b/rust/frameworks/tw_ton_sdk/src/boc/binary_writer.rs @@ -54,7 +54,7 @@ impl BinaryWriter { } else { self.write_bytes(&data[..data_len - 1])?; let last_byte = data[data_len - 1]; - let l = last_byte | 1 << (8 - rest_bits - 1); + let l = last_byte | (1 << (8 - rest_bits - 1)); self.write(8, l)?; } diff --git a/rust/frameworks/tw_ton_sdk/src/boc/raw.rs b/rust/frameworks/tw_ton_sdk/src/boc/raw.rs index 3b02a02d415..8a210da9006 100644 --- a/rust/frameworks/tw_ton_sdk/src/boc/raw.rs +++ b/rust/frameworks/tw_ton_sdk/src/boc/raw.rs @@ -263,7 +263,7 @@ fn write_raw_cell( if !full_bytes { writer.write_bytes(&data[..data_len_bytes - 1])?; let last_byte = data[data_len_bytes - 1]; - let l = last_byte | 1 << (8 - padding_bits - 1); + let l = last_byte | (1 << (8 - padding_bits - 1)); writer.write(8, l)?; } else { writer.write_bytes(data)?; diff --git a/rust/tw_any_coin/src/wallet_connect_request.rs b/rust/tw_any_coin/src/wallet_connect_request.rs index 22099edad58..77867b48925 100644 --- a/rust/tw_any_coin/src/wallet_connect_request.rs +++ b/rust/tw_any_coin/src/wallet_connect_request.rs @@ -16,8 +16,6 @@ impl WalletConnectRequest { #[inline] pub fn parse(coin: CoinType, input: &[u8]) -> SigningResult { let (ctx, entry) = coin_dispatcher(coin)?; - entry - .wallet_connect_parse_request(&ctx, input) - .map_err(SigningError::from) + entry.wallet_connect_parse_request(&ctx, input) } } diff --git a/rust/tw_encoding/src/hex.rs b/rust/tw_encoding/src/hex.rs index 5db6e8038a3..2d683c60e23 100644 --- a/rust/tw_encoding/src/hex.rs +++ b/rust/tw_encoding/src/hex.rs @@ -30,7 +30,7 @@ pub trait DecodeHex { fn decode_hex(&self) -> FromHexResult; } -impl<'a> DecodeHex for &'a str { +impl DecodeHex for &str { fn decode_hex(&self) -> FromHexResult { decode(self) } diff --git a/rust/tw_evm/src/message/eip712/message_types.rs b/rust/tw_evm/src/message/eip712/message_types.rs index 4a0d8f0aeae..85321686b15 100644 --- a/rust/tw_evm/src/message/eip712/message_types.rs +++ b/rust/tw_evm/src/message/eip712/message_types.rs @@ -38,7 +38,7 @@ pub struct CustomTypeBuilder<'a> { type_properties: &'a mut Vec, } -impl<'a> CustomTypeBuilder<'a> { +impl CustomTypeBuilder<'_> { pub fn add_property(&mut self, name: &str, property_type: PropertyType) -> &mut Self { self.type_properties.push(Property { name: name.to_string(), diff --git a/rust/tw_misc/src/test_utils/json.rs b/rust/tw_misc/src/test_utils/json.rs index 06da9d7ed63..93f5fa826f7 100644 --- a/rust/tw_misc/src/test_utils/json.rs +++ b/rust/tw_misc/src/test_utils/json.rs @@ -16,7 +16,7 @@ impl ToJson for Json { } } -impl<'a> ToJson for Cow<'a, str> { +impl ToJson for Cow<'_, str> { #[track_caller] fn to_json(&self) -> Json { self.as_ref().to_json() @@ -30,7 +30,7 @@ impl ToJson for String { } } -impl<'a> ToJson for &'a str { +impl ToJson for &str { #[track_caller] fn to_json(&self) -> Json { serde_json::from_str(self).expect("Error on deserializing JSON from string")