When a high memory limit is set, for example
.withMemoryLimits(1 << 14, 1 << 14))
we exit out early anyway, because the instanceMemory on org.extism.sdk.chicory.Kernel is always instanciated with the default limits of 16,max. We seem to exit out when we hit the inital pages, not the limit.
So plugin.memory().memory() will have 16 initial pages, while
plugin.mainInstance.memory()
takes on the size defined in our limits.
My observation is that the memory limits on Manifest are only relevant for "initialising" the Instance, like in org.extism.sdk.chicory.e2e.PluginTest#testGreetFailingMemory
Reproduction:
extism-js script.js -i script.d.ts
script.d.ts
script.js
public void testFailingMemoryHog()
throws IOException, NoSuchFieldException, IllegalAccessException {
byte[] bytes =
PluginTest.class.getClassLoader().getResourceAsStream("memory.wasm").readAllBytes();
var wasm = ManifestWasm.fromBytes(bytes).build();
int memorySize = 1 << 14;
var manifest = Manifest.ofWasms(wasm).withOptions(
new Manifest.Options().withAoT().withWasi(WasiOptions.builder().build()).withAoT(true)
.withAllowedHosts("jsonplaceholder.typicode.com").withHttpConfig(
HttpConfig.builder().withClientAdapter(() -> null).withJsonCodec(() -> null).build())
.withMemoryLimits(memorySize, memorySize)).build();
Plugin plugin = Plugin.ofManifest(manifest).build();
try {
plugin.call("leak", new byte[0]);
} catch (ExtismFunctionException ex) {
assertTrue(ex.getMessage().contains("out of memory"));
Field mainInstanceField = Plugin.class.getDeclaredField("mainInstance");
mainInstanceField.setAccessible(true);
Instance mainInstance = (Instance) mainInstanceField.get(plugin);
Field limitsByteArrayMemory = ByteArrayMemory.class.getDeclaredField("limits");
limitsByteArrayMemory.setAccessible(true);
MemoryLimits memoryLimitsMainInstance =
(MemoryLimits) limitsByteArrayMemory.get(mainInstance.memory());
assertEquals(memorySize, memoryLimitsMainInstance.maximumPages());
assertEquals(memorySize, memoryLimitsMainInstance.initialPages());
Field limitsByteBuffer = ByteBufferMemory.class.getDeclaredField("limits");
limitsByteBuffer.setAccessible(true);
ByteBufferMemory memoryHostEnv = (ByteBufferMemory) plugin.memory().memory();
MemoryLimits bufferLimits = (MemoryLimits) limitsByteBuffer.get(memoryHostEnv);
// fails here expected:<16384> but was:<16>
assertEquals(memorySize, bufferLimits.initialPages());
// fails here expected:<16384> but was:<65536>
assertEquals(memorySize, bufferLimits.maximumPages());
}
}
When a high memory limit is set, for example
.withMemoryLimits(1 << 14, 1 << 14))we exit out early anyway, because the
instanceMemoryonorg.extism.sdk.chicory.Kernelis always instanciated with the default limits of16,max. We seem to exit out when we hit the inital pages, not the limit.So
plugin.memory().memory()will have 16 initial pages, whileplugin.mainInstance.memory()takes on the size defined in our limits.
My observation is that the memory limits on Manifest are only relevant for "initialising" the Instance, like in org.extism.sdk.chicory.e2e.PluginTest#testGreetFailingMemory
Reproduction:
extism-js script.js -i script.d.tsscript.d.ts
script.js