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

feat: add ability to limit the number of instructions executed by a plugin #28

Merged
merged 1 commit into from
Aug 23, 2024
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
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
Loading