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

Support jni inside jar #826

Merged
merged 3 commits into from
Oct 9, 2023
Merged
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
8 changes: 2 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ googleJavaFormat {
}

dependencies {
api("org.fisco-bcos:fisco-bcos-tars-sdk:${tarsSDKVersion}")
api("org.fisco-bcos:fisco-bcos-tars-sdk" + ":${tarsSDKVersion}")
api("org.fisco-bcos:bcos-sdk-jni:${bcosSdkJniVersion}") {
exclude group : "org.slf4j"
exclude group : "com.fasterxml.jackson.core"
Expand All @@ -136,14 +136,10 @@ dependencies {
api("org.apache.commons:commons-lang3:${commonsLang3Version}")
api("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}")
api("commons-io:commons-io:${commonsIOVersion}")
// api("com.webank:key-mini-toolkit:${keyMiniToolkit}")
api("com.webank:webank-blockchain-java-crypto:${webankJavaCryptoVersion}")
api("com.moandjiezana.toml:toml4j:${toml4jVersion}") {
exclude group: "com.google.code.gson"
}
// api("org.apache.commons:commons-configuration2:${config2Version}"){
// exclude group: "commons-logging"
// }

integrationTestImplementation project
integrationWasmTestImplementation project
Expand Down Expand Up @@ -262,7 +258,7 @@ publishing {

jar {
// destinationDir file('dist/apps')
archiveName "fisco-bcos-" + project.name + '-' + project.version + '.jar'
archiveFileName="fisco-bcos-" + project.name + '-' + project.version + '.jar'
exclude '**/*.xml'
exclude '**/*.properties'

Expand Down
49 changes: 40 additions & 9 deletions src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.fisco.bcos.sdk.v3.client;

import java.io.File;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URL;
import java.nio.file.Files;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.fisco.bcos.sdk.tars.Callback;
Expand Down Expand Up @@ -90,8 +94,11 @@
this.transactionFactory = transactionFactory;
}

protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) {
protected TarsClient(String groupID, ConfigOption configOption, long nativePointer)
throws Exception {
super(groupID, configOption, nativePointer);

loadLibrary();
String connectionString =
RPCClient.toConnectionString(
new StringVector(configOption.getNetworkConfig().getTarsPeers()));
Expand Down Expand Up @@ -133,16 +140,40 @@
};
}

public static void loadLibrary() {
URL configUrl = TarsClient.class.getClassLoader().getResource(libFileName);
System.load(configUrl.getPath());
}
private static AtomicBoolean loaded = new AtomicBoolean(false);

public static void loadLibrary(String libPath) {
System.load(libPath);
private static void loadLibrary() throws Exception {
boolean inited = loaded.getAndSet(true);
if (inited) {
return;
}
try {
File jniFile = File.createTempFile(libFileName, UUID.randomUUID().toString());

Check warning

Code scanning / CodeQL

Local information disclosure in a temporary directory Medium

Local information disclosure vulnerability due to use of file readable by other local users.
String osName = System.getProperty("os.name");
if (osName.contains("Linux")) osName = "linux";
else if (osName.contains("Mac OS X")) osName = "darwin";
else if (osName.contains("Windows")) osName = "windows";

String osArch = System.getProperty("os.arch");
if (osArch.contains("amd64")) osArch = "x86_64";

InputStream jniStream =
TarsClient.class.getResourceAsStream(
"/" + osName + "-" + osArch + "/" + libFileName);
Files.copy(
jniStream,
jniFile.getAbsoluteFile().toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
System.load(jniFile.getAbsolutePath());
jniFile.deleteOnExit();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) {
public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer)
throws Exception {
logger.info(
"TarsClient build, groupID: {}, configOption: {}, nativePointer: {}",
groupId,
Expand Down
Loading