Skip to content

Commit

Permalink
feat: add ability to limit the number of instructions executed by a p…
Browse files Browse the repository at this point in the history
…lugin (#28)

Depends on extism/extism#754
  • Loading branch information
zshipko authored Aug 23, 2024
1 parent 1448042 commit 54b8143
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/extism/sdk/LibExtism.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Pointer extism_function_new(String name,

void extism_function_free(Pointer function);


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


/**
* Free error message from `extism_plugin_new`
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/extism/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,48 @@ public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions)
this.pluginPointer = p;
}


public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions, long fuelLimit) {

Objects.requireNonNull(manifestBytes, "manifestBytes");

Pointer[] ptrArr = new Pointer[functions == null ? 0 : functions.length];

if (functions != null)
for (int i = 0; i < functions.length; i++) {
ptrArr[i] = functions[i].pointer;
}

Pointer[] errormsg = new Pointer[1];
Pointer p = LibExtism.INSTANCE.extism_plugin_new_with_fuel_limit(manifestBytes, manifestBytes.length,
ptrArr,
functions == null ? 0 : functions.length,
withWASI,
fuelLimit,
errormsg);
if (p == null) {
if (functions != null) {
for (int i = 0; i < functions.length; i++) {
LibExtism.INSTANCE.extism_function_free(functions[i].pointer);
}
}
String msg = errormsg[0].getString(0);
LibExtism.INSTANCE.extism_plugin_new_error_free(errormsg[0]);
throw new ExtismException(msg);
}

this.functions = functions;
this.pluginPointer = p;
}

public Plugin(Manifest manifest, boolean withWASI, HostFunction[] functions) {
this(serialize(manifest), withWASI, functions);
}


public Plugin(Manifest manifest, boolean withWASI, HostFunction[] functions, long fuelLimit) {
this(serialize(manifest), withWASI, functions, fuelLimit);
}

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

0 comments on commit 54b8143

Please sign in to comment.