|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package org.graalvm.wasm.test; |
| 42 | + |
| 43 | +import static org.graalvm.wasm.utils.WasmBinaryTools.compileWat; |
| 44 | + |
| 45 | +import java.io.IOException; |
| 46 | +import java.lang.ref.WeakReference; |
| 47 | +import java.nio.ByteOrder; |
| 48 | +import java.util.ArrayList; |
| 49 | + |
| 50 | +import org.graalvm.polyglot.Context; |
| 51 | +import org.graalvm.polyglot.Engine; |
| 52 | +import org.graalvm.wasm.WasmModule; |
| 53 | +import org.graalvm.wasm.WasmTable; |
| 54 | +import org.graalvm.wasm.api.Dictionary; |
| 55 | +import org.graalvm.wasm.api.WebAssembly; |
| 56 | +import org.junit.Assert; |
| 57 | +import org.junit.Test; |
| 58 | + |
| 59 | +import com.oracle.truffle.api.interop.InteropException; |
| 60 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 61 | + |
| 62 | +public class WasmMemoryLeakSuite { |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testMemoryLeak() throws IOException, InterruptedException { |
| 66 | + final byte[] binary = compileWat("leak", """ |
| 67 | + (module |
| 68 | + (memory (export "mem") 128) ;; 8 MiB |
| 69 | + (func $fun (export "fun") (result i32) i32.const 42) |
| 70 | + (table (export "tab") 1 funcref) |
| 71 | + (elem (i32.const 0) $fun) |
| 72 | + ) |
| 73 | + """); |
| 74 | + WasmJsApiSuite.runTest(b -> b.engine(Engine.create()), context -> { |
| 75 | + InteropLibrary lib = InteropLibrary.getUncached(); |
| 76 | + WebAssembly wasm = new WebAssembly(context); |
| 77 | + WasmModule module = wasm.moduleDecode(binary); |
| 78 | + Object importObject = new Dictionary(); |
| 79 | + try { |
| 80 | + var weakStores = new ArrayList<WeakReference<Object>>(); |
| 81 | + var weakInstances = new ArrayList<WeakReference<Object>>(); |
| 82 | + var weakMemories = new ArrayList<WeakReference<Object>>(); |
| 83 | + var weakFunctions = new ArrayList<WeakReference<Object>>(); |
| 84 | + var weakTables = new ArrayList<WeakReference<Object>>(); |
| 85 | + var strongInstances = new ArrayList<>(); |
| 86 | + for (int iter = 0; iter < 16; iter++) { |
| 87 | + var instance = wasm.moduleInstantiate(module, importObject); |
| 88 | + Object mem = lib.readMember(instance, "mem"); |
| 89 | + Object fun = lib.readMember(instance, "fun"); |
| 90 | + Object tab = lib.readMember(instance, "tab"); |
| 91 | + |
| 92 | + weakStores.add(new WeakReference<>(instance.store())); |
| 93 | + weakInstances.add(new WeakReference<>(instance)); |
| 94 | + weakMemories.add(new WeakReference<>(mem)); |
| 95 | + weakFunctions.add(new WeakReference<>(fun)); |
| 96 | + weakTables.add(new WeakReference<>(tab)); |
| 97 | + |
| 98 | + // a single WasmInstance, WasmFunctionInstance, or WasmMemory instance |
| 99 | + // should prevent the context from being closed and garbage-collected. |
| 100 | + switch (iter % 4) { |
| 101 | + case 0 -> strongInstances.add(instance); |
| 102 | + case 1 -> strongInstances.add(fun); |
| 103 | + case 2 -> strongInstances.add(mem); |
| 104 | + case 3 -> strongInstances.add(tab); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + processReferenceQueueAndGC(); |
| 109 | + |
| 110 | + for (int i = 0; i < strongInstances.size(); i++) { |
| 111 | + Object instance = strongInstances.get(i); |
| 112 | + Assert.assertNotNull(instance); |
| 113 | + switch (i % 4) { |
| 114 | + case 0 -> { |
| 115 | + // WasmInstance |
| 116 | + Assert.assertTrue(lib.isMemberReadable(instance, "fun")); |
| 117 | + } |
| 118 | + case 1 -> { |
| 119 | + // WasmFunction |
| 120 | + Assert.assertTrue(lib.isExecutable(instance)); |
| 121 | + Assert.assertEquals(42, lib.execute(instance)); |
| 122 | + } |
| 123 | + case 2 -> { |
| 124 | + // WasmMemory |
| 125 | + Assert.assertEquals(128 * 64 * 1024, lib.getBufferSize(instance)); |
| 126 | + Assert.assertEquals(0, lib.readBufferInt(instance, ByteOrder.LITTLE_ENDIAN, 0)); |
| 127 | + } |
| 128 | + case 3 -> { |
| 129 | + // WasmTable |
| 130 | + // No table interop support (GR-57847). |
| 131 | + Object fun = WebAssembly.tableRead((WasmTable) instance, 0); |
| 132 | + Assert.assertTrue(lib.isExecutable(fun)); |
| 133 | + Assert.assertEquals(42, lib.execute(fun)); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // memories are kept alive either by the WasmMemory object itself or by the context |
| 139 | + Assert.assertTrue("WebAssembly memories should not have been freed yet.", |
| 140 | + weakMemories.stream().noneMatch(weakRef -> weakRef.refersTo(null))); |
| 141 | + // contexts may be freed in the case of only WasmMemory references |
| 142 | + |
| 143 | + strongInstances.clear(); |
| 144 | + processReferenceQueueAndGC(); |
| 145 | + |
| 146 | + Assert.assertTrue("WebAssembly memories should have been freed.", |
| 147 | + weakMemories.stream().anyMatch(weakRef -> weakRef.refersTo(null))); |
| 148 | + Assert.assertTrue("WebAssembly stores should have been freed.", |
| 149 | + weakStores.stream().anyMatch(weakRef -> weakRef.refersTo(null))); |
| 150 | + } catch (InteropException e) { |
| 151 | + throw new RuntimeException(e); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + private static void processReferenceQueueAndGC() { |
| 157 | + System.gc(); |
| 158 | + Context.create().close(); // process reference queue |
| 159 | + System.gc(); |
| 160 | + } |
| 161 | +} |
0 commit comments