Skip to content

Commit fcaff40

Browse files
- Fix memory leak
- package wasmer into single arch dist
1 parent 09a3858 commit fcaff40

File tree

14 files changed

+436
-65
lines changed

14 files changed

+436
-65
lines changed

pom.xml

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.semantalytics.stardog</groupId>
88
<artifactId>wasm</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.0-rc2</version>
1010

1111
<developers>
1212
<developer>
@@ -46,21 +46,21 @@
4646

4747
<properties>
4848
<stardog.major.version>7</stardog.major.version>
49-
<stardog.version>${stardog.major.version}.5.0</stardog.version>
49+
<stardog.version>${stardog.major.version}.6.0</stardog.version>
5050
<junit.version>4.12</junit.version>
5151
<slf4j.version>1.7.21</slf4j.version>
5252
<jansi.version>1.16</jansi.version>
5353
<assertj.version>3.15.0</assertj.version>
5454
<xmlapis.version>1.4.01</xmlapis.version>
5555
<maven.version>3.3.9</maven.version>
56-
<commons-text.version>1.7</commons-text.version>
57-
<commons-lang3.version>3.9</commons-lang3.version>
58-
<emoji4j.version>5.0</emoji4j.version>
59-
<simmetrics-core.version>4.1.1</simmetrics-core.version>
60-
<java-sting-similarity.version>0.24</java-sting-similarity.version>
6156
</properties>
6257

6358
<dependencies>
59+
<dependency>
60+
<groupId>com.google.guava</groupId>
61+
<artifactId>guava</artifactId>
62+
<version>30.1-jre</version>
63+
</dependency>
6464
<dependency>
6565
<groupId>com.complexible.stardog</groupId>
6666
<artifactId>server</artifactId>
@@ -98,9 +98,9 @@
9898
<scope>test</scope>
9999
</dependency>
100100
<dependency>
101-
<groupId>com.semantalytics</groupId>
102-
<artifactId>wasmer-jni-amd64-linux</artifactId>
103-
<version>0.3.0</version>
101+
<groupId>org.scijava</groupId>
102+
<artifactId>native-lib-loader</artifactId>
103+
<version>2.3.5</version>
104104
</dependency>
105105
</dependencies>
106106

@@ -125,12 +125,12 @@
125125
</repositories>
126126

127127
<build>
128-
<finalName>semantalytics-stardog-${project.artifactId}-${project.version}-sd${stardog.major.version}-amd64-linux</finalName>
128+
<finalName>semantalytics-stardog-${project.artifactId}-${project.version}-sd${stardog.major.version}</finalName>
129129
<plugins>
130130
<plugin>
131131
<groupId>org.apache.maven.plugins</groupId>
132132
<artifactId>maven-compiler-plugin</artifactId>
133-
<version>3.5.1</version>
133+
<version>3.8.1</version>
134134
<configuration>
135135
<source>1.8</source>
136136
<target>1.8</target>
@@ -141,7 +141,7 @@
141141
<plugin>
142142
<groupId>org.apache.maven.plugins</groupId>
143143
<artifactId>maven-resources-plugin</artifactId>
144-
<version>2.7</version>
144+
<version>3.1.0</version>
145145
<configuration>
146146
<encoding>UTF-8</encoding>
147147
</configuration>
@@ -205,7 +205,7 @@
205205
<plugin>
206206
<groupId>org.apache.maven.plugins</groupId>
207207
<artifactId>maven-surefire-plugin</artifactId>
208-
<version>2.12.4</version>
208+
<version>2.22.2</version>
209209
<configuration>
210210
<argLine>-Djava.library.path=${env.STARDOG}/lib</argLine>
211211
<excludes>
@@ -216,32 +216,6 @@
216216
</includes>
217217
</configuration>
218218
</plugin>
219-
<!--
220-
<plugin>
221-
<groupId>com.github.wvengen</groupId>
222-
<artifactId>proguard-maven-plugin</artifactId>
223-
<version>2.2.0</version>
224-
<executions>
225-
<execution>
226-
<phase>package</phase>
227-
<goals><goal>proguard</goal></goals>
228-
</execution>
229-
</executions>
230-
<configuration>
231-
<options>
232-
<option>-allowaccessmodification</option>
233-
<option>-keep public class * extends com.complexible.stardog.plan.filter.functions.AbstractFunction { *; }</option>
234-
</options>
235-
<outjar>${project.build.finalName}.jar</outjar>
236-
<outjar>${project.build.finalName}-small.jar</outjar>
237-
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
238-
<libs>
239-
<lib>${java.home}/lib/rt.jar</lib>
240-
<lib>${java.home}/lib/jsse.jar</lib>
241-
</libs>
242-
</configuration>
243-
</plugin>
244-
-->
245219
</plugins>
246220
</build>
247221
</project>

src/main/java/com/semantalytics/stardog/kibble/wasm/Call.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,27 @@ public ValueOrError evaluate(final ValueSolution valueSolution) {
128128

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

131-
if (bs.isPresent()) {
132-
if (bs.get().size() > 1) {
133-
final MappingDictionary mappingDictionary = valueSolution.getDictionary();
134-
final long[] ids = bs.get().stream().map(b -> b.get()).mapToLong(v -> mappingDictionary.add(v)).toArray();
135-
return ValueOrError.General.of(new ArrayLiteral(ids));
136-
} else if (bs.get().size() == 1) {
137-
final Optional<Binding> firstVar = bs.get().stream().findFirst();
138-
if (firstVar.isPresent()) {
139-
return ValueOrError.General.of(firstVar.get().value());
131+
try {
132+
if (bs.isPresent()) {
133+
if (bs.get().size() > 1) {
134+
final MappingDictionary mappingDictionary = valueSolution.getDictionary();
135+
final long[] ids = bs.get().stream().map(b -> b.get()).mapToLong(v -> mappingDictionary.add(v)).toArray();
136+
return ValueOrError.General.of(new ArrayLiteral(ids));
137+
} else if (bs.get().size() == 1) {
138+
final Optional<Binding> firstVar = bs.get().stream().findFirst();
139+
if (firstVar.isPresent()) {
140+
return ValueOrError.General.of(firstVar.get().value());
141+
} else {
142+
return ValueOrError.Error;
143+
}
140144
} else {
141145
return ValueOrError.Error;
142146
}
143147
} else {
144148
return ValueOrError.Error;
145149
}
146-
} else {
147-
return ValueOrError.Error;
150+
} finally {
151+
instance.exports.getFunction("deallocate").apply(input_pointer, byteArrayOutputStream.toByteArray().length);
148152
}
149153
} else {
150154
return ValueOrError.Error;

src/main/java/com/semantalytics/stardog/kibble/wasm/echo/Cargo.toml

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/com/semantalytics/stardog/kibble/wasm/echo/src/echo.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.wasmer;
2+
3+
import org.wasmer.exports.Export;
4+
import org.wasmer.exports.Function;
5+
6+
import java.lang.ClassCastException;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* `Exports` is a Java class that represents the set of WebAssembly exports.
12+
*
13+
* Example:
14+
* <pre>{@code
15+
* Instance instance = new Instance(wasmBytes);
16+
*
17+
* // Get and run an exported function.
18+
* Object[] result = instance.exports.getFunction("sum").apply(1, 2);
19+
*
20+
* // Get, manually downcast, and run an exported function.
21+
* Export sum = instance.exports.get("sum");
22+
* Object[] result = ((Function) sum).apply(1, 2);
23+
* }</pre>
24+
*/
25+
public class Exports {
26+
private Map<String, Export> inner;
27+
private Instance instance;
28+
29+
/**
30+
* The constructor instantiates new exported functions.
31+
*
32+
* @param instance Instance object which holds the exports object.
33+
*/
34+
protected Exports(Instance instance) {
35+
this.inner = new HashMap<String, Export>();
36+
this.instance = instance;
37+
}
38+
39+
/**
40+
* Return the export with the name `name`.
41+
*
42+
* @param name Name of the export to return.
43+
*/
44+
public Export get(String name) {
45+
return this.inner.get(name);
46+
}
47+
48+
/**
49+
* Return the export with the name `name` as an exported function.
50+
*
51+
* @param name Name of the exported function.
52+
*/
53+
public Function getFunction(String name) throws ClassCastException {
54+
return (Function) this.inner.get(name);
55+
}
56+
57+
/**
58+
* Return the export with the name `name` as an exported memory.
59+
*
60+
* @param name Name of the exported memory.
61+
*/
62+
public Memory getMemory(String name) throws ClassCastException {
63+
return (Memory) this.inner.get(name);
64+
}
65+
66+
/**
67+
* Called by Rust to add a new exported function.
68+
*/
69+
private void addFunction(String name) {
70+
this.inner.put(name, this.generateFunctionWrapper(name));
71+
}
72+
73+
/**
74+
* Called by Rust to add a new exported memory.
75+
*/
76+
private void addMemory(String name, Memory memory) {
77+
this.inner.put(name, memory);
78+
}
79+
80+
/**
81+
* Lambda expression for currying.
82+
* This takes a function name and returns the function to call WebAssembly function.
83+
*/
84+
private java.util.function.Function<String, Function> functionWrapperGenerator =
85+
functionName -> arguments -> this.instance.nativeCallExportedFunction(this.instance.instancePointer, functionName, arguments);
86+
87+
/**
88+
* Generate the exported function wrapper.
89+
*/
90+
private Function generateFunctionWrapper(String functionName) {
91+
return this.functionWrapperGenerator.apply(functionName);
92+
}
93+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.wasmer;
2+
3+
import org.scijava.nativelib.NativeLoader;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* `Instance` is a Java class that represents a WebAssembly instance.
9+
*
10+
* Example:
11+
* <pre>{@code
12+
* Instance instance = new Instance(wasmBytes);
13+
* }</pre>
14+
*/
15+
public class Instance {
16+
/**
17+
* Native bindings.
18+
*/
19+
static {
20+
try {
21+
NativeLoader.loadLibrary("wasmer_jni");
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
private native long nativeInstantiate(Instance self, byte[] moduleBytes) throws RuntimeException;
27+
private native void nativeDrop(long instancePointer);
28+
protected native Object[] nativeCallExportedFunction(long instancePointer, String exportName, Object[] arguments) throws RuntimeException;
29+
protected static native void nativeInitializeExportedFunctions(long instancePointer);
30+
protected static native void nativeInitializeExportedMemories(long instancePointer);
31+
32+
/**
33+
* All WebAssembly exports.
34+
*/
35+
public final Exports exports;
36+
37+
/**
38+
The instance pointer.
39+
*/
40+
protected long instancePointer;
41+
42+
/**
43+
* The constructor instantiates a new WebAssembly instance based on
44+
* WebAssembly bytes.
45+
*
46+
* @param moduleBytes WebAssembly bytes.
47+
*/
48+
public Instance(byte[] moduleBytes) throws RuntimeException {
49+
this.exports = new Exports(this);
50+
51+
long instancePointer = this.nativeInstantiate(this, moduleBytes);
52+
this.instancePointer = instancePointer;
53+
54+
this.nativeInitializeExportedFunctions(instancePointer);
55+
this.nativeInitializeExportedMemories(instancePointer);
56+
}
57+
58+
protected Instance() {
59+
this.exports = new Exports(this);
60+
}
61+
62+
/**
63+
* Delete an instance object pointer.
64+
*/
65+
public void close() {
66+
this.nativeDrop(this.instancePointer);
67+
}
68+
69+
/**
70+
* Delete an instance object pointer, which is called by the garbage collector
71+
* before an object is removed from the memory.
72+
*/
73+
public void finalize() {
74+
this.close();
75+
}
76+
}

0 commit comments

Comments
 (0)