Skip to content

Commit 87da7dd

Browse files
committed
feat: add ability to limit the number of instructions executed by a plugin
1 parent 1448042 commit 87da7dd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/main/java/org/extism/sdk/LibExtism.java

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Pointer extism_function_new(String name,
6666

6767
void extism_function_free(Pointer function);
6868

69+
6970
/**
7071
* Get the length of an allocated block
7172
* NOTE: this should only be called from host functions.
@@ -119,6 +120,8 @@ Pointer extism_function_new(String name,
119120
* @return pointer to the plugin, or null in case of error
120121
*/
121122
Pointer extism_plugin_new(byte[] wasm, long wasmSize, Pointer[] functions, int nFunctions, boolean withWASI, Pointer[] errmsg);
123+
Pointer extism_plugin_new_with_fuel_limit(byte[] wasm, long wasmSize, Pointer[] functions, int nFunctions, boolean withWASI, long fuelLimit, Pointer[] errmsg);
124+
122125

123126
/**
124127
* Free error message from `extism_plugin_new`

src/main/java/org/extism/sdk/Plugin.java

+38
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,48 @@ public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions)
5656
this.pluginPointer = p;
5757
}
5858

59+
60+
public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions, long fuelLimit) {
61+
62+
Objects.requireNonNull(manifestBytes, "manifestBytes");
63+
64+
Pointer[] ptrArr = new Pointer[functions == null ? 0 : functions.length];
65+
66+
if (functions != null)
67+
for (int i = 0; i < functions.length; i++) {
68+
ptrArr[i] = functions[i].pointer;
69+
}
70+
71+
Pointer[] errormsg = new Pointer[1];
72+
Pointer p = LibExtism.INSTANCE.extism_plugin_new_with_fuel_limit(manifestBytes, manifestBytes.length,
73+
ptrArr,
74+
functions == null ? 0 : functions.length,
75+
withWASI,
76+
fuelLimit,
77+
errormsg);
78+
if (p == null) {
79+
if (functions != null) {
80+
for (int i = 0; i < functions.length; i++) {
81+
LibExtism.INSTANCE.extism_function_free(functions[i].pointer);
82+
}
83+
}
84+
String msg = errormsg[0].getString(0);
85+
LibExtism.INSTANCE.extism_plugin_new_error_free(errormsg[0]);
86+
throw new ExtismException(msg);
87+
}
88+
89+
this.functions = functions;
90+
this.pluginPointer = p;
91+
}
92+
5993
public Plugin(Manifest manifest, boolean withWASI, HostFunction[] functions) {
6094
this(serialize(manifest), withWASI, functions);
6195
}
6296

97+
98+
public Plugin(Manifest manifest, boolean withWASI, HostFunction[] functions, long fuelLimit) {
99+
this(serialize(manifest), withWASI, functions, fuelLimit);
100+
}
63101

64102
private static byte[] serialize(Manifest manifest) {
65103
Objects.requireNonNull(manifest, "manifest");

0 commit comments

Comments
 (0)