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

JVM memory leak fix #4070

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codegen-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- name: Lint Dockerfile
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linux-ci-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
permissions:
contents: read
checks: write
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:

# Run Rust tests in WASM.
test-wasm:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux-ci-sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
build:
if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.fork == false
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux-sampleapp-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wasm-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v3
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ let package = Package(
targets: [
.binaryTarget(
name: "WalletCore",
url: "https://github.com/trustwallet/wallet-core/releases/download/4.0.33/WalletCore.xcframework.zip",
checksum: "2fb8b833047b9697bba6ade66a9bdeede622b2fe0fb7a9b90cb9edb4651ec866"
url: "https://github.com/trustwallet/wallet-core/releases/download/4.1.12/WalletCore.xcframework.zip",
checksum: "1632bbbab1c6a588689eec77a24e1468d9a6746968652cf0a7e669e996c3d24d"
),
.binaryTarget(
name: "SwiftProtobuf",
url: "https://github.com/trustwallet/wallet-core/releases/download/4.0.33/SwiftProtobuf.xcframework.zip",
checksum: "05557735dd607c5a369dc378eb3f299504b880614ef13f136a028ecd320b0e4d"
url: "https://github.com/trustwallet/wallet-core/releases/download/4.1.12/SwiftProtobuf.xcframework.zip",
checksum: "33d80c20428c9db4fcf99d1272ba19655f7c6ee7e5b1809fa8a7e7d4aa1b222b"
)
]
)
28 changes: 3 additions & 25 deletions codegen/lib/templates/java/class.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,10 @@ public final class <%= entity.name %> {
throw new InvalidParameterException();
}

<%= entity.name %>PhantomReference.register(this, nativeHandle);
GenericPhantomReference.register(someObject, someHandle, handle -> {
nativeDelete(nativeHandle);
});
}

<%- end -%>
}
<% unless entity.methods.select{ |x| x.name == "Delete" }.empty? -%>
class <%= entity.name %>PhantomReference extends java.lang.ref.PhantomReference<<%= entity.name %>> {
private static java.util.Set<<%= entity.name %>PhantomReference> references = new HashSet<<%= entity.name %>PhantomReference>();
private static java.lang.ref.ReferenceQueue<<%= entity.name %>> queue = new java.lang.ref.ReferenceQueue<<%= entity.name %>>();
private long nativeHandle;

private <%= entity.name %>PhantomReference(<%= entity.name %> referent, long nativeHandle) {
super(referent, queue);
this.nativeHandle = nativeHandle;
}

static void register(<%= entity.name %> referent, long nativeHandle) {
references.add(new <%= entity.name %>PhantomReference(referent, nativeHandle));
}

public static void doDeletes() {
<%= entity.name %>PhantomReference ref = (<%= entity.name %>PhantomReference) queue.poll();
for (; ref != null; ref = (<%= entity.name %>PhantomReference) queue.poll()) {
<%= entity.name %>.nativeDelete(ref.nativeHandle);
references.remove(ref);
}
}
}
<% end -%>
51 changes: 51 additions & 0 deletions jni/java/wallet/core/java/GenericPhantomReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package wallet.core.java;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.HashSet;
import java.util.Set;

public class GenericPhantomReference extends PhantomReference<Object> {
private final long handle;
private final OnDeleteCallback onDelete;

private static final Set<GenericPhantomReference> references = new HashSet<>();
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();

static {
Thread finalizingDaemon = new Thread(() -> {
try {
doDeletes();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
finalizingDaemon.setName("WCFinalizingDaemon");
finalizingDaemon.setDaemon(true);
finalizingDaemon.setPriority(Thread.NORM_PRIORITY);
finalizingDaemon.start();
}

private GenericPhantomReference(Object referent, long handle, OnDeleteCallback onDelete) {
super(referent, queue);
this.handle = handle;
this.onDelete = onDelete;
}

public static void register(Object referent, long handle, OnDeleteCallback onDelete) {
references.add(new GenericPhantomReference(referent, handle, onDelete));
}

private static void doDeletes() throws InterruptedException {
while (true) {
GenericPhantomReference ref = (GenericPhantomReference) queue.remove();
ref.onDelete.onDelete(ref.handle);
references.remove(ref);
}
}

@FunctionalInterface
public interface OnDeleteCallback {
void onDelete(long handle);
}
}
4 changes: 4 additions & 0 deletions kotlin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ kotlin = "1.8.21"
agp = "8.0.0"
wire = "4.5.6"

androidx-test-runner = "1.5.2"

[libraries]
wire-runtime = { module = "com.squareup.wire:wire-runtime", version.ref = "wire" }
wire-compiler = { module = "com.squareup.wire:wire-compiler", version.ref = "wire" }

androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
17 changes: 10 additions & 7 deletions kotlin/wallet-core-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ kotlin {
}
}

getByName("commonTest") {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
Expand All @@ -85,6 +85,13 @@ kotlin {
implementation(npm(name = "webpack", version = "5.89.0"))
}
}

getByName("androidInstrumentedTest") {
dependsOn(commonTest)
dependencies {
implementation(libs.androidx.test.runner)
}
}
}

nativeTargets.forEach { nativeTarget ->
Expand Down Expand Up @@ -119,6 +126,8 @@ android {
arguments += listOf("-DCMAKE_BUILD_TYPE=Release", "-DKOTLIN=True", "-DTW_UNITY_BUILD=ON")
}
}

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
Expand All @@ -132,12 +141,6 @@ android {
viewBinding = false
}

androidComponents {
beforeVariants {
it.enable = it.name == "release"
}
}

externalNativeBuild {
cmake {
version = libs.versions.android.cmake.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.trustwallet.core

actual object LibLoader {
actual fun loadLibrary() {
throw NotImplementedError()
System.loadLibrary("TrustWalletCore")
}
}
19 changes: 14 additions & 5 deletions rust/chains/tw_aptos/src/transaction_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ fn serialize_argument(arg: &TransactionArgument) -> EncodingResult<Data> {
TransactionArgument::U256(v) => bcs::encode(v),
TransactionArgument::U8Vector(v) => bcs::encode(v),
TransactionArgument::Bool(v) => bcs::encode(v),
TransactionArgument::Address(v) => {
let serialized_v = bcs::encode(v)?;
bcs::encode(&serialized_v)
},
TransactionArgument::Address(v) => bcs::encode(v),
}
}

Expand Down Expand Up @@ -220,6 +217,10 @@ mod tests {

let v = EntryFunction::try_from(payload_value.clone()).unwrap();
assert_eq!(payload_value, v.to_json());

let tp = TransactionPayload::EntryFunction(v);
let serialized = bcs::encode(&tp).unwrap();
assert_eq!(hex::encode(serialized, false), "02c23c3b70956ce8d88fb18ad9ed3b463fe873cb045db3f6d2e2fb15b9aab71d500349464f0772656c65617365030748e0e3958d42b8d452c9199d4a221d0d1b15d14655787453dbe77208ced9051705636f696e730442555344000748e0e3958d42b8d452c9199d4a221d0d1b15d14655787453dbe77208ced9051705636f696e730344414900079936836587ca33240d3d3f91844651b16cb07802faf5e34514ed6f78580deb0a0575696e7473025531000120c95db29a67a848940829b3df6119b5e67b788ff0248676e4484c7c6f29c0f5e6");
}

#[test]
Expand Down Expand Up @@ -255,13 +256,21 @@ mod tests {
);
let tp = TransactionPayload::EntryFunction(entry);
let serialized = bcs::encode(&tp).unwrap();
assert_eq!(hex::encode(serialized, false), "02000000000000000000000000000000000000000000000000000000000000000104636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b08e803000000000000");
let expected_serialized = "02000000000000000000000000000000000000000000000000000000000000000104636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220eeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b08e803000000000000";
assert_eq!(hex::encode(serialized, false), expected_serialized);
let payload_value: Value = json!({
"function": "0x1::coin::transfer",
"type": "entry_function_payload",
"arguments": ["0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b", "1000"],
"type_arguments": ["0x1::aptos_coin::AptosCoin"]
});
assert_eq!(tp.to_json(), payload_value);

// Rebuild a new EntryFunction object from the JSON above
let v = EntryFunction::try_from(payload_value.clone()).unwrap();
let tp = TransactionPayload::EntryFunction(v);
// Serialize the new EntryFunction object and compare with the expected serialized value
let serialized = bcs::encode(&tp).unwrap();
assert_eq!(hex::encode(serialized, false), expected_serialized);
}
}
1 change: 1 addition & 0 deletions src/Filecoin/Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Address.h"

#include <algorithm>
#include <climits>

#include "../Base32.h"
Expand Down
3 changes: 2 additions & 1 deletion src/Groestlcoin/Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Copyright © 2017 Trust Wallet.

#include "Address.h"
#include "Base58.h"

#include "../Base58.h"
#include <algorithm>
#include <TrezorCrypto/ecdsa.h>

namespace TW::Groestlcoin {
Expand Down
1 change: 0 additions & 1 deletion src/NEO/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Transaction* Transaction::deserializeFrom(const Data& data, size_t initial_pos)
break;
default:
throw std::invalid_argument("Transaction::deserializeFrom Invalid transaction type");
break;
}
resp->deserialize(data, initial_pos);
return resp;
Expand Down
3 changes: 2 additions & 1 deletion src/Nano/Signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include "../BinaryCoding.h"
#include "../HexCoding.h"
#include "../uint256.h"
#include <nlohmann/json.hpp>

#include <algorithm>
#include <nlohmann/json.hpp>
#include <google/protobuf/util/json_util.h>

using namespace TW;
Expand Down
1 change: 1 addition & 0 deletions src/interface/TWCardano.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// Copyright © 2017 Trust Wallet.

#include <algorithm>
#include <TrustWalletCore/TWCardano.h>

#include "Cardano/Transaction.h"
Expand Down