Skip to content

Commit

Permalink
- Fix memory leak
Browse files Browse the repository at this point in the history
- package wasmer into single arch dist
  • Loading branch information
zacharywhitley committed Mar 12, 2021
1 parent 09a3858 commit fcaff40
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 65 deletions.
54 changes: 14 additions & 40 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.semantalytics.stardog</groupId>
<artifactId>wasm</artifactId>
<version>1.0.0</version>
<version>1.0.0-rc2</version>

<developers>
<developer>
Expand Down Expand Up @@ -46,21 +46,21 @@

<properties>
<stardog.major.version>7</stardog.major.version>
<stardog.version>${stardog.major.version}.5.0</stardog.version>
<stardog.version>${stardog.major.version}.6.0</stardog.version>
<junit.version>4.12</junit.version>
<slf4j.version>1.7.21</slf4j.version>
<jansi.version>1.16</jansi.version>
<assertj.version>3.15.0</assertj.version>
<xmlapis.version>1.4.01</xmlapis.version>
<maven.version>3.3.9</maven.version>
<commons-text.version>1.7</commons-text.version>
<commons-lang3.version>3.9</commons-lang3.version>
<emoji4j.version>5.0</emoji4j.version>
<simmetrics-core.version>4.1.1</simmetrics-core.version>
<java-sting-similarity.version>0.24</java-sting-similarity.version>
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
<dependency>
<groupId>com.complexible.stardog</groupId>
<artifactId>server</artifactId>
Expand Down Expand Up @@ -98,9 +98,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.semantalytics</groupId>
<artifactId>wasmer-jni-amd64-linux</artifactId>
<version>0.3.0</version>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.3.5</version>
</dependency>
</dependencies>

Expand All @@ -125,12 +125,12 @@
</repositories>

<build>
<finalName>semantalytics-stardog-${project.artifactId}-${project.version}-sd${stardog.major.version}-amd64-linux</finalName>
<finalName>semantalytics-stardog-${project.artifactId}-${project.version}-sd${stardog.major.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -141,7 +141,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<version>3.1.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
Expand Down Expand Up @@ -205,7 +205,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<version>2.22.2</version>
<configuration>
<argLine>-Djava.library.path=${env.STARDOG}/lib</argLine>
<excludes>
Expand All @@ -216,32 +216,6 @@
</includes>
</configuration>
</plugin>
<!--
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>proguard</goal></goals>
</execution>
</executions>
<configuration>
<options>
<option>-allowaccessmodification</option>
<option>-keep public class * extends com.complexible.stardog.plan.filter.functions.AbstractFunction { *; }</option>
</options>
<outjar>${project.build.finalName}.jar</outjar>
<outjar>${project.build.finalName}-small.jar</outjar>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jsse.jar</lib>
</libs>
</configuration>
</plugin>
-->
</plugins>
</build>
</project>
26 changes: 15 additions & 11 deletions src/main/java/com/semantalytics/stardog/kibble/wasm/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,27 @@ public ValueOrError evaluate(final ValueSolution valueSolution) {

final Optional<BindingSet> bs = selectQueryResult.stream().findFirst();

if (bs.isPresent()) {
if (bs.get().size() > 1) {
final MappingDictionary mappingDictionary = valueSolution.getDictionary();
final long[] ids = bs.get().stream().map(b -> b.get()).mapToLong(v -> mappingDictionary.add(v)).toArray();
return ValueOrError.General.of(new ArrayLiteral(ids));
} else if (bs.get().size() == 1) {
final Optional<Binding> firstVar = bs.get().stream().findFirst();
if (firstVar.isPresent()) {
return ValueOrError.General.of(firstVar.get().value());
try {
if (bs.isPresent()) {
if (bs.get().size() > 1) {
final MappingDictionary mappingDictionary = valueSolution.getDictionary();
final long[] ids = bs.get().stream().map(b -> b.get()).mapToLong(v -> mappingDictionary.add(v)).toArray();
return ValueOrError.General.of(new ArrayLiteral(ids));
} else if (bs.get().size() == 1) {
final Optional<Binding> firstVar = bs.get().stream().findFirst();
if (firstVar.isPresent()) {
return ValueOrError.General.of(firstVar.get().value());
} else {
return ValueOrError.Error;
}
} else {
return ValueOrError.Error;
}
} else {
return ValueOrError.Error;
}
} else {
return ValueOrError.Error;
} finally {
instance.exports.getFunction("deallocate").apply(input_pointer, byteArrayOutputStream.toByteArray().length);
}
} else {
return ValueOrError.Error;
Expand Down

This file was deleted.

This file was deleted.

93 changes: 93 additions & 0 deletions src/main/java/org/wasmer/Exports.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.wasmer;

import org.wasmer.exports.Export;
import org.wasmer.exports.Function;

import java.lang.ClassCastException;
import java.util.HashMap;
import java.util.Map;

/**
* `Exports` is a Java class that represents the set of WebAssembly exports.
*
* Example:
* <pre>{@code
* Instance instance = new Instance(wasmBytes);
*
* // Get and run an exported function.
* Object[] result = instance.exports.getFunction("sum").apply(1, 2);
*
* // Get, manually downcast, and run an exported function.
* Export sum = instance.exports.get("sum");
* Object[] result = ((Function) sum).apply(1, 2);
* }</pre>
*/
public class Exports {
private Map<String, Export> inner;
private Instance instance;

/**
* The constructor instantiates new exported functions.
*
* @param instance Instance object which holds the exports object.
*/
protected Exports(Instance instance) {
this.inner = new HashMap<String, Export>();
this.instance = instance;
}

/**
* Return the export with the name `name`.
*
* @param name Name of the export to return.
*/
public Export get(String name) {
return this.inner.get(name);
}

/**
* Return the export with the name `name` as an exported function.
*
* @param name Name of the exported function.
*/
public Function getFunction(String name) throws ClassCastException {
return (Function) this.inner.get(name);
}

/**
* Return the export with the name `name` as an exported memory.
*
* @param name Name of the exported memory.
*/
public Memory getMemory(String name) throws ClassCastException {
return (Memory) this.inner.get(name);
}

/**
* Called by Rust to add a new exported function.
*/
private void addFunction(String name) {
this.inner.put(name, this.generateFunctionWrapper(name));
}

/**
* Called by Rust to add a new exported memory.
*/
private void addMemory(String name, Memory memory) {
this.inner.put(name, memory);
}

/**
* Lambda expression for currying.
* This takes a function name and returns the function to call WebAssembly function.
*/
private java.util.function.Function<String, Function> functionWrapperGenerator =
functionName -> arguments -> this.instance.nativeCallExportedFunction(this.instance.instancePointer, functionName, arguments);

/**
* Generate the exported function wrapper.
*/
private Function generateFunctionWrapper(String functionName) {
return this.functionWrapperGenerator.apply(functionName);
}
}
76 changes: 76 additions & 0 deletions src/main/java/org/wasmer/Instance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.wasmer;

import org.scijava.nativelib.NativeLoader;

import java.io.IOException;

/**
* `Instance` is a Java class that represents a WebAssembly instance.
*
* Example:
* <pre>{@code
* Instance instance = new Instance(wasmBytes);
* }</pre>
*/
public class Instance {
/**
* Native bindings.
*/
static {
try {
NativeLoader.loadLibrary("wasmer_jni");
} catch (IOException e) {
e.printStackTrace();
}
}
private native long nativeInstantiate(Instance self, byte[] moduleBytes) throws RuntimeException;
private native void nativeDrop(long instancePointer);
protected native Object[] nativeCallExportedFunction(long instancePointer, String exportName, Object[] arguments) throws RuntimeException;
protected static native void nativeInitializeExportedFunctions(long instancePointer);
protected static native void nativeInitializeExportedMemories(long instancePointer);

/**
* All WebAssembly exports.
*/
public final Exports exports;

/**
The instance pointer.
*/
protected long instancePointer;

/**
* The constructor instantiates a new WebAssembly instance based on
* WebAssembly bytes.
*
* @param moduleBytes WebAssembly bytes.
*/
public Instance(byte[] moduleBytes) throws RuntimeException {
this.exports = new Exports(this);

long instancePointer = this.nativeInstantiate(this, moduleBytes);
this.instancePointer = instancePointer;

this.nativeInitializeExportedFunctions(instancePointer);
this.nativeInitializeExportedMemories(instancePointer);
}

protected Instance() {
this.exports = new Exports(this);
}

/**
* Delete an instance object pointer.
*/
public void close() {
this.nativeDrop(this.instancePointer);
}

/**
* Delete an instance object pointer, which is called by the garbage collector
* before an object is removed from the memory.
*/
public void finalize() {
this.close();
}
}
Loading

0 comments on commit fcaff40

Please sign in to comment.