Skip to content

Memory Limits do not take effect for execution, only for instantiation #66

@BSchneppe

Description

@BSchneppe

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());
    }

  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions