Skip to content

Commit

Permalink
prefix own functions with extension_
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jan 30, 2024
1 parent 52e2338 commit ff006a1
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 116 deletions.
5 changes: 2 additions & 3 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.extism.sdk</groupId>
<artifactId>extism</artifactId>
<packaging>jar</packaging>
<version>0.6.0</version>
<version>1.0.0</version>
<name>extism</name>
<url>https://github.com/extism/extism</url>
<description>Java-SDK for Extism to use webassembly from Java</description>
Expand Down Expand Up @@ -154,8 +154,7 @@
<version>${maven-surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<jna.library.path>../target/debug</jna.library.path>
<!-- TODO - /target/release -->
<jna.library.path>../target/release</jna.library.path>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
59 changes: 14 additions & 45 deletions java/src/main/java/org/extism/sdk/LibExtism.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@ Pointer extism_plugin_new(byte[] wasm,
*/
void extism_plugin_free(Pointer pluginPointer);

LibExtism.ExtismVal.ByReference wasm_plugin_call(
Pointer contextPointer,
String function_name,
ExtismVal.ByReference inputs,
int nInputs,
byte[] data,
int dataLength);

Pointer wasm_plugin_call_without_params(
Pointer contextPointer,
String function_name,
Expand All @@ -217,13 +209,6 @@ void wasm_plugin_call_without_results(
ExtismVal.ByReference inputs,
int nInputs);

Pointer extism_plugin_call_native(Pointer contextPointer, String function_name, ExtismVal inputs, int nInputs);

int extism_plugin_call_native_int(Pointer contextPointer, String function_name, ExtismVal.ByReference inputs, int nInputs, byte[] data, int dataLen);

void deallocate_plugin_call_results(LibExtism.ExtismVal.ByReference results, int length);


/**
* Update plugin config values, this will merge with the existing values.
*
Expand All @@ -239,46 +224,30 @@ void wasm_plugin_call_without_results(
boolean extism_plugin_cancel(Pointer contextPointer);
void extism_function_set_namespace(Pointer p, String name);

// void extism_reset(Pointer contextPointer);

void extism_plugin_reset(Pointer contextPointer);
// Pointer extism_get_lineary_memory_from_host_functions(Pointer plugin, int instanceIndex, String memoryName);

/** ###################################
* Following functions are not dependent of Extism and are used to run OPA and proxy wasm
################################### */

// TODO - ADDED
Pointer wasm_otoroshi_create_wasmtime_memory(String name, String namespace, int minPages, int maxPages);
// TODO - ADDED
void wasm_otoroshi_free_memory(Pointer memory);
// TODO - ADDED
void wasm_otoroshi_deallocate_results(LibExtism.ExtismVal.ByReference results, int length);
// TODO - ADDED
Pointer extism_plugin_new_with_memories(byte[] wasm,
Pointer extension_create_wasmtime_memory(String name, String namespace, int minPages, int maxPages);


void extension_deallocate_results(LibExtism.ExtismVal.ByReference results, int length);

Pointer extension_extism_plugin_new_with_memories(byte[] wasm,
long wasmSize,
Pointer[] functions,
int nFunctions,
Pointer[] memories,
int nMemories,
boolean withWASI,
Pointer[] errmsg);
// TODO - ADDED
LibExtism.ExtismVal.ByReference wasm_otoroshi_call(Pointer pluginPointer, String functionName, LibExtism.ExtismVal.ByReference inputs, int length);
// TODO - ADDED
Pointer wasm_otoroshi_wasm_plugin_call_without_params(Pointer pluginPointer, String functionName);
// TODO - ADDED
void wasm_otoroshi_wasm_plugin_call_without_results(Pointer pluginPointer,
String functionName,
LibExtism.ExtismVal.ByReference inputs,
int nInputs);
// TODO - ADDED
int wasm_otoroshi_extism_memory_write_bytes(Pointer pluginPointer, byte[] data, int n, int offset, String namespace, String name);
// TODO - ADDED
Pointer wasm_otoroshi_extism_get_memory(Pointer instance, String memoryName, String namespace);

Pointer wasm_otoroshi_extism_get_linear_memory(Pointer instance, String memoryName, String namespace);

// TODO - ADDED
int wasm_otoroshi_extism_memory_bytes(Pointer pluginPointer);

// TODO - ADDED

LibExtism.ExtismVal.ByReference extension_call(Pointer pluginPointer, String functionName, LibExtism.ExtismVal.ByReference inputs, int length);
int extension_extism_memory_write_bytes(Pointer pluginPointer, byte[] data, int n, int offset, String namespace, String name);

Pointer custom_memory_get(Pointer plugin);
int custom_memory_length(Pointer plugin, long n);
int custom_memory_alloc(Pointer plugin, long n);
Expand Down
15 changes: 6 additions & 9 deletions java/src/main/java/org/extism/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public Plugin(byte[] manifestBytes,
this.pluginPointer = p;
}

// TODO - ADDED
public Plugin(byte[] manifestBytes,
boolean withWASI,
HostFunction[] functions,
Expand All @@ -66,7 +65,7 @@ public Plugin(byte[] manifestBytes,
Pointer[] memoriesPtr = LinearMemory.arrayToPointer(memories);

Pointer[] errormsg = new Pointer[1];
Pointer p = LibExtism.INSTANCE.extism_plugin_new_with_memories(manifestBytes,
Pointer p = LibExtism.INSTANCE.extension_extism_plugin_new_with_memories(manifestBytes,
manifestBytes.length,
functionsPtr,
functions == null ? 0 : functions.length,
Expand All @@ -88,7 +87,6 @@ public Plugin(byte[] manifestBytes,
this.pluginPointer = p;
}

// TODO - ADDED
public Plugin(Manifest manifest,
boolean withWASI,
HostFunction[] functions,
Expand Down Expand Up @@ -158,17 +156,16 @@ protected String error() {
public Results call(String functionName, Parameters params, int resultsLength) {
params.getPtr().write();

LibExtism.ExtismVal.ByReference results = LibExtism.INSTANCE.wasm_otoroshi_call(
LibExtism.ExtismVal.ByReference results = LibExtism.INSTANCE.extension_call(
this.pluginPointer,
functionName,
params.getPtr(),
params.getLength()
);

if (results == null && resultsLength > 0) {
String error = error();
System.out.println(error);
// throw new ExtismException(error);
// String error = error();
// throw new ExtismException(error);
return new Results(0);
}

Expand Down Expand Up @@ -265,11 +262,11 @@ public void close() {
}

public int writeBytes(byte[] data, int n, int offset, String namespace) {
return LibExtism.INSTANCE.wasm_otoroshi_extism_memory_write_bytes(this.pluginPointer, data, n, offset, namespace, null);
return LibExtism.INSTANCE.extension_extism_memory_write_bytes(this.pluginPointer, data, n, offset, namespace, null);
}

public int writeBytes(byte[] data, int n, int offset, String namespace, String name) {
return LibExtism.INSTANCE.wasm_otoroshi_extism_memory_write_bytes(this.pluginPointer, data, n, offset, namespace, name);
return LibExtism.INSTANCE.extension_extism_memory_write_bytes(this.pluginPointer, data, n, offset, namespace, name);
}

public Pointer getLinearMemory(String namespace, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public LinearMemory(String name, String namespace, LinearMemoryOptions memoryOpt
}

private Pointer instanciate() {
return LibExtism.INSTANCE.wasm_otoroshi_create_wasmtime_memory(
return LibExtism.INSTANCE.extension_create_wasmtime_memory(
this.name,
this.namespace,
this.memoryOptions.getMin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void set(Parameters.AddFunction function, int i) {
public void close() {
if(!freed) {
freed = true;
LibExtism.INSTANCE.wasm_otoroshi_deallocate_results(this.ptr, this.length);
LibExtism.INSTANCE.extension_deallocate_results(this.ptr, this.length);
}
}

Expand Down
14 changes: 3 additions & 11 deletions java/src/test/java/org/extism/sdk/TestWasmSources.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public enum TestWasmSources {
public Path getWasmFilePath() {
return Paths.get(WASM_LOCATION, "code.wasm");
}

public Path getWasmFunctionsFilePath() {
return Paths.get(WASM_LOCATION, "code-functions.wasm");
}

public Path getWasmWebAssemblyFunctionFilePath() {
return Paths.get(WASM_LOCATION, "functions.wasm");
}
Expand All @@ -27,16 +29,12 @@ public Path getRawAdditionFilePath() {
return Paths.get(WASM_LOCATION, "addition.wasm");
}

public Path getCorazaFilePath() { return Paths.get(WASM_LOCATION, "coraza.wasm"); }

public Path getMajorReleasePath() { return Paths.get(WASM_LOCATION, "test-1.0.0-dev.wasm"); }

public Path getOPAPath() { return Paths.get(WASM_LOCATION, "OPA_POLICY-1.0.0-dev.wasm"); }

public Path getWasmWaf() {
return Paths.get(WASM_LOCATION, "coraza-proxy-wasm-v0.1.2.wasm");
// return Paths.get(WASM_LOCATION, "coraza.wasm");
// return Paths.get(WASM_LOCATION, "../../../../../coraza-proxy-wasm/build/mainraw.wasm");
return Paths.get(WASM_LOCATION, "coraza-proxy-wasm-v0.5.0.wasm");
}
};

Expand All @@ -48,8 +46,6 @@ public Path getWasmWaf() {

public abstract Path getRawAdditionFilePath();

public abstract Path getCorazaFilePath();

public abstract Path getMajorReleasePath();

public abstract Path getOPAPath();
Expand All @@ -68,10 +64,6 @@ public PathWasmSource getRawAdditionPath() {
return resolvePathWasmSource(getRawAdditionFilePath());
}

public PathWasmSource getCorazaPath() {
return resolvePathWasmSource(getCorazaFilePath());
}

public PathWasmSource getMajorRelease() {
return resolvePathWasmSource(getMajorReleasePath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,11 @@ public int ProxyGetHeaderMapPairs(ExtismCurrentPlugin plugin, VMData vmData, int
totalBytesLen += key.length() + 1 + value.length + 1; // key + \0 + value + \0
}

// TODO - try to call proxy_on_memory_allocate
int addr = plugin.customMemoryAlloc(totalBytesLen);

// TODO - manage error
// if err != nil {
// return int32(v2.ResultInvalidMemoryAccess)
// }

Pointer memory = plugin.customMemoryGet();

memory.setInt(addr, cloneMap.size());
// if err != nil {
// return int32(v2.ResultInvalidMemoryAccess)
// }

int lenPtr = addr + u32Len;
int dataPtr = lenPtr + (u32Len+u32Len) * cloneMap.size();
Expand Down
Binary file not shown.
Binary file removed java/src/test/resources/coraza.wasm
Binary file not shown.
46 changes: 23 additions & 23 deletions runtime/extism.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ typedef void (*ExtismLogDrainFunctionType)(const char *data, ExtismSize size);
extern "C" {
#endif // __cplusplus

void wasm_otoroshi_deallocate_results(ExtismVal *ptr, uintptr_t len);
void extension_deallocate_results(ExtismVal *ptr, uintptr_t len);

ExtismVal *wasm_otoroshi_call(ExtismPlugin *plugin,
const char *func_name,
const ExtismVal *params,
ExtismSize n_params);
ExtismVal *extension_call(ExtismPlugin *plugin,
const char *func_name,
const ExtismVal *params,
ExtismSize n_params);

ExtismVal *wasm_plugin_call_without_params(ExtismPlugin *plugin_ptr, const char *func_name);

Expand All @@ -125,22 +125,22 @@ void wasm_plugin_call_without_results(ExtismPlugin *plugin_ptr,
const ExtismVal *params,
ExtismSize n_params);

ExtismMemory *wasm_otoroshi_create_wasmtime_memory(const char *name,
const char *namespace_,
uint32_t min_pages,
uint32_t max_pages);
ExtismMemory *extension_create_wasmtime_memory(const char *name,
const char *namespace_,
uint32_t min_pages,
uint32_t max_pages);

/**
* Remove all plugins from the registry
*/
void custom_memory_reset_from_plugin(ExtismPlugin *plugin);

int8_t wasm_otoroshi_extism_memory_write_bytes(ExtismPlugin *instance_ptr,
const uint8_t *data,
ExtismSize data_size,
uint32_t offset,
const char *namespace_,
const char *name);
int8_t extension_extism_memory_write_bytes(ExtismPlugin *instance_ptr,
const uint8_t *data,
ExtismSize data_size,
uint32_t offset,
const char *namespace_,
const char *name);

uint8_t *linear_memory_get(ExtismCurrentPlugin *plugin, const char *namespace_, const char *name);

Expand Down Expand Up @@ -168,14 +168,14 @@ void custom_memory_free(ExtismCurrentPlugin *plugin, uint64_t ptr);

uint64_t custom_memory_alloc(ExtismCurrentPlugin *plugin, ExtismSize n);

ExtismPlugin *extism_plugin_new_with_memories(const uint8_t *wasm,
ExtismSize wasm_size,
const ExtismFunction **functions,
ExtismSize n_functions,
const ExtismMemory **memories,
int8_t n_memories,
bool with_wasi,
char **errmsg);
ExtismPlugin *extension_extism_plugin_new_with_memories(const uint8_t *wasm,
ExtismSize wasm_size,
const ExtismFunction **functions,
ExtismSize n_functions,
const ExtismMemory **memories,
int8_t n_memories,
bool with_wasi,
char **errmsg);

/**
* Get a plugin's ID, the returned bytes are a 16 byte buffer that represent a UUIDv4
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/current_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::*;

use self::otoroshi::custom_memory::PluginMemory;
use self::extension::custom_memory::PluginMemory;

/// CurrentPlugin stores data that is available to the caller in PDK functions, this should
/// only be accessed from inside a host function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;

use crate::{otoroshi::*, CurrentPlugin};
use crate::{extension::*, CurrentPlugin};

/// Handles memory for plugins
pub struct PluginMemory {
Expand Down
File renamed without changes.
Loading

0 comments on commit ff006a1

Please sign in to comment.