From 4be6607c34bbbfce45fca287393bc4553d8dfd56 Mon Sep 17 00:00:00 2001 From: LSantha Date: Mon, 23 Nov 2026 18:46:55 +0000 Subject: [PATCH 1/4] Made ItemFactory per-visitor; boot clean. Assisted-by: opencode/beam-free --- core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java | 4 ++-- .../org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java b/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java index 679063347f..d82c0f34d2 100644 --- a/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java +++ b/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java @@ -309,9 +309,9 @@ private Item createNew(int jvmType) { } /** - * Hidden constructor. + * Constructor. */ - private ItemFactory() { + ItemFactory() { } /** diff --git a/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java b/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java index d5fd38ab6a..64d795b0cc 100644 --- a/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java +++ b/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java @@ -280,7 +280,7 @@ public X86BytecodeVisitor(NativeStream outputStream, CompiledMethod cm, gprPool = new X86RegisterPool.GPRs64(); xmmPool = new X86RegisterPool.XMMs64(); } - this.ifac = ItemFactory.getFactory(); + this.ifac = new ItemFactory(); final AbstractX86StackManager stackMgr = vstack.createStackMgr(gprPool, ifac); this.helper = new X86CompilerHelper(os, stackMgr, context, isBootstrap); this.cm = cm; From f03140c8d9d19e82d136f69466a7a55c6355318a Mon Sep 17 00:00:00 2001 From: LSantha Date: Mon, 23 Nov 2026 19:06:36 +0000 Subject: [PATCH 2/4] All review feedback addressed. Assisted-by: opencode/beam-free --- .../vm/x86/compiler/l1a/ItemFactory.java | 16 --- .../x86/compiler/l1a/X86BytecodeVisitor.java | 3 + .../test/org/jnode/test/CoreTestSuite.java | 2 + .../l1a/X86Level1AItemFactoryBalanceTest.java | 116 ++++++++++++++++++ 4 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java diff --git a/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java b/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java index d82c0f34d2..ec12e81401 100644 --- a/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java +++ b/core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java @@ -33,8 +33,6 @@ final class ItemFactory { public static final boolean CHECK_BALANCED_ITEM_FACTORY = true; - private static ThreadLocal itemFactory = new ThreadLocal(); - private final ArrayList intItems = new ArrayList(); private final ArrayList longItems = new ArrayList(); @@ -314,20 +312,6 @@ private Item createNew(int jvmType) { ItemFactory() { } - /** - * Gets the item factory. This item factory is singleton per thread. - * - * @return - */ - static ItemFactory getFactory() { - ItemFactory fac = (ItemFactory) itemFactory.get(); - if (fac == null) { - fac = new ItemFactory(); - itemFactory.set(fac); - } - return fac; - } - boolean isBalanced() { return getOrCreateCount == releaseCount; } diff --git a/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java b/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java index 64d795b0cc..eedcca3d12 100644 --- a/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java +++ b/core/src/core/org/jnode/vm/x86/compiler/l1a/X86BytecodeVisitor.java @@ -280,6 +280,9 @@ public X86BytecodeVisitor(NativeStream outputStream, CompiledMethod cm, gprPool = new X86RegisterPool.GPRs64(); xmmPool = new X86RegisterPool.XMMs64(); } + // Each visitor gets its own factory: visitors are pooled per thread and + // reused across methods via reset(), but simultaneously-live (nested) + // visitors must not share item accounting. this.ifac = new ItemFactory(); final AbstractX86StackManager stackMgr = vstack.createStackMgr(gprPool, ifac); this.helper = new X86CompilerHelper(os, stackMgr, context, isBootstrap); diff --git a/core/src/test/org/jnode/test/CoreTestSuite.java b/core/src/test/org/jnode/test/CoreTestSuite.java index 0ffc905bd6..fc1337321d 100644 --- a/core/src/test/org/jnode/test/CoreTestSuite.java +++ b/core/src/test/org/jnode/test/CoreTestSuite.java @@ -31,6 +31,7 @@ import org.jnode.vm.memmgr.VmHeapManagerOomeTest; import org.jnode.vm.scheduler.VmThreadSchedulingTest; import org.jnode.vm.x86.compiler.l1a.X86Level1ACompilerVisitorTest; +import org.jnode.vm.x86.compiler.l1a.X86Level1AItemFactoryBalanceTest; import org.jnode.vm.x86.compiler.l1a.X86RegisterPoolTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -55,6 +56,7 @@ WaitTest.class, JavaCommandParserTest.class, ThreadingTestJUnit4.class, + X86Level1AItemFactoryBalanceTest.class, X86Level1ACompilerVisitorTest.class, X86RegisterPoolTest.class, X86StreamTest.class, diff --git a/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java b/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java new file mode 100644 index 0000000000..545f3d4752 --- /dev/null +++ b/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java @@ -0,0 +1,116 @@ +/* + * $Id$ + * + * Copyright (C) 2003-2015 JNode.org + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; If not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package org.jnode.vm.x86.compiler.l1a; + +import java.io.File; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.net.URL; + +import org.jnode.assembler.x86.X86Constants.Mode; +import org.jnode.assembler.x86.X86TextAssembler; +import org.jnode.vm.VmImpl; +import org.jnode.vm.VmSystemClassLoader; +import org.jnode.vm.classmgr.VmMethod; +import org.jnode.vm.classmgr.VmType; +import org.jnode.vm.facade.VmUtils; +import org.jnode.vm.x86.VmX86Architecture32; +import org.jnode.vm.x86.X86CpuID; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * Regression test for issue #986: nested compilations triggered by constant + * resolution must not corrupt the outer method's item accounting. Compiling the + * two methods that exposed the bug must leave the per-visitor {@link ItemFactory} + * balanced. + */ +public class X86Level1AItemFactoryBalanceTest { + + private static VmSystemClassLoader cl; + private static X86Level1ACompiler compiler; + private static ThreadLocal holder; + private static Field ifacField; + + private static File findFile(String... candidates) { + for (String c : candidates) { + File f = new File(c); + if (f.exists()) { + return f; + } + } + return new File(candidates[0]); + } + + @BeforeClass + public static void setUpClass() throws Exception { + if (VmUtils.getVm() == null) { + final VmX86Architecture32 arch = new VmX86Architecture32(); + final File coreClasses = findFile("./core/build/classes", "core/build/classes", "../core/build/classes"); + final File classlibJar = findFile("./all/lib/classlib.jar", "all/lib/classlib.jar", "../all/lib/classlib.jar"); + final URL[] urls = new URL[] { + coreClasses.getCanonicalFile().toURI().toURL(), + new URL("jar:" + classlibJar.getCanonicalFile().toURI().toURL() + "!/") + }; + cl = new VmSystemClassLoader(urls, arch); + final VmImpl vm = new VmImpl("?", arch, cl.getSharedStatics(), false, cl, null); + vm.toString(); + VmType.initializeForBootImage(cl); + compiler = new X86Level1ACompiler(false); + compiler.initialize(cl); + final Field holderField = X86Level1ACompiler.class.getDeclaredField("byteCodeVisitorHolder"); + holderField.setAccessible(true); + holder = (ThreadLocal) holderField.get(compiler); + ifacField = X86BytecodeVisitor.class.getDeclaredField("ifac"); + ifacField.setAccessible(true); + } + } + + private void compileAndCheck(String clsName, String methodName) throws Exception { + final VmType type = cl.loadClass(clsName, true); + VmMethod method = null; + for (int i = 0; i < type.getNoDeclaredMethods(); i++) { + final VmMethod m = type.getDeclaredMethod(i); + if (methodName.equals(m.getName())) { + method = m; + } + } + final StringWriter sw = new StringWriter(); + final X86TextAssembler os = new X86TextAssembler(sw, X86CpuID.createID("p5"), Mode.CODE32, + method.getMangledName()); + compiler.compileBootstrap(method, os, 0); + os.flush(); + final X86BytecodeVisitor v = (X86BytecodeVisitor) holder.get(); + final ItemFactory ifac = (ItemFactory) ifacField.get(v); + assertTrue(clsName + "#" + methodName + " item factory must be balanced", ifac.isBalanced()); + } + + @Test + public void vmClassHelperGetPackagePortionIsBalanced() throws Exception { + compileAndCheck("gnu.java.lang.VMClassHelper", "getPackagePortion"); + } + + @Test + public void reflectionFactoryNewMethodIsBalanced() throws Exception { + compileAndCheck("sun.reflect.ReflectionFactory", "newMethod"); + } +} \ No newline at end of file From 1e730efd71897148aab80fa76842244ce39ddc0a Mon Sep 17 00:00:00 2001 From: LSantha Date: Mon, 23 Nov 2026 19:30:43 +0000 Subject: [PATCH 3/4] Tests pass; pre-fix fails. Assisted-by: opencode/beam-free --- .github/workflows/ant.yml | 12 ++++ .../l1a/X86Level1AItemFactoryBalanceTest.java | 67 +++++++++++++------ 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ant.yml b/.github/workflows/ant.yml index 8c0550e657..765f824a68 100644 --- a/.github/workflows/ant.yml +++ b/.github/workflows/ant.yml @@ -129,6 +129,12 @@ jobs: if timeout 300s bash -c 'until grep -q -i "System has finished" /tmp/com1.txt; do sleep 1; done'; then echo "Successfully booted JNode 32-bit!" + if grep -q "Unbalanced item handling" /tmp/com1.txt; then + echo "Unbalanced item handling detected in boot log!" + kill $qemu_pid $tail_pid || true + cat /tmp/com1.txt + exit 1 + fi kill $qemu_pid $tail_pid || true exit 0 fi @@ -190,6 +196,12 @@ jobs: if timeout 300s bash -c 'until grep -q -i "System has finished" /tmp/com1_64.txt; do sleep 1; done'; then echo "Successfully booted JNode 64-bit!" + if grep -q "Unbalanced item handling" /tmp/com1_64.txt; then + echo "Unbalanced item handling detected in boot log!" + kill $qemu_pid $tail_pid || true + cat /tmp/com1_64.txt + exit 1 + fi kill $qemu_pid $tail_pid || true exit 0 fi diff --git a/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java b/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java index 545f3d4752..9249076429 100644 --- a/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java +++ b/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java @@ -33,16 +33,18 @@ import org.jnode.vm.facade.VmUtils; import org.jnode.vm.x86.VmX86Architecture32; import org.jnode.vm.x86.X86CpuID; +import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; /** * Regression test for issue #986: nested compilations triggered by constant * resolution must not corrupt the outer method's item accounting. Compiling the * two methods that exposed the bug must leave the per-visitor {@link ItemFactory} - * balanced. + * balanced, and simultaneously-live visitors must not share one factory. */ public class X86Level1AItemFactoryBalanceTest { @@ -50,6 +52,7 @@ public class X86Level1AItemFactoryBalanceTest { private static X86Level1ACompiler compiler; private static ThreadLocal holder; private static Field ifacField; + private static Object previousVm; private static File findFile(String... candidates) { for (String c : candidates) { @@ -63,26 +66,32 @@ private static File findFile(String... candidates) { @BeforeClass public static void setUpClass() throws Exception { - if (VmUtils.getVm() == null) { - final VmX86Architecture32 arch = new VmX86Architecture32(); - final File coreClasses = findFile("./core/build/classes", "core/build/classes", "../core/build/classes"); - final File classlibJar = findFile("./all/lib/classlib.jar", "all/lib/classlib.jar", "../all/lib/classlib.jar"); - final URL[] urls = new URL[] { - coreClasses.getCanonicalFile().toURI().toURL(), - new URL("jar:" + classlibJar.getCanonicalFile().toURI().toURL() + "!/") - }; - cl = new VmSystemClassLoader(urls, arch); - final VmImpl vm = new VmImpl("?", arch, cl.getSharedStatics(), false, cl, null); - vm.toString(); - VmType.initializeForBootImage(cl); - compiler = new X86Level1ACompiler(false); - compiler.initialize(cl); - final Field holderField = X86Level1ACompiler.class.getDeclaredField("byteCodeVisitorHolder"); - holderField.setAccessible(true); - holder = (ThreadLocal) holderField.get(compiler); - ifacField = X86BytecodeVisitor.class.getDeclaredField("ifac"); - ifacField.setAccessible(true); - } + previousVm = VmUtils.getVm(); + final VmX86Architecture32 arch = new VmX86Architecture32(); + final File coreClasses = findFile("./core/build/classes", "core/build/classes", "../core/build/classes"); + final File classlibJar = findFile("./all/lib/classlib.jar", "all/lib/classlib.jar", "../all/lib/classlib.jar"); + final URL[] urls = new URL[] { + coreClasses.getCanonicalFile().toURI().toURL(), + new URL("jar:" + classlibJar.getCanonicalFile().toURI().toURL() + "!/") + }; + cl = new VmSystemClassLoader(urls, arch); + final VmImpl vm = new VmImpl("?", arch, cl.getSharedStatics(), false, cl, null); + vm.toString(); + VmType.initializeForBootImage(cl); + compiler = new X86Level1ACompiler(false); + compiler.initialize(cl); + final Field holderField = X86Level1ACompiler.class.getDeclaredField("byteCodeVisitorHolder"); + holderField.setAccessible(true); + holder = (ThreadLocal) holderField.get(compiler); + ifacField = X86BytecodeVisitor.class.getDeclaredField("ifac"); + ifacField.setAccessible(true); + } + + @AfterClass + public static void tearDownClass() throws Exception { + final Field vmField = VmUtils.class.getDeclaredField("VM_INSTANCE"); + vmField.setAccessible(true); + vmField.set(null, previousVm); } private void compileAndCheck(String clsName, String methodName) throws Exception { @@ -113,4 +122,18 @@ public void vmClassHelperGetPackagePortionIsBalanced() throws Exception { public void reflectionFactoryNewMethodIsBalanced() throws Exception { compileAndCheck("sun.reflect.ReflectionFactory", "newMethod"); } -} \ No newline at end of file + + @Test + public void nestedCompilationsUseDistinctItemFactories() throws Exception { + holder.remove(); + final StringWriter sw = new StringWriter(); + final X86TextAssembler os = new X86TextAssembler(sw, X86CpuID.createID("p5"), Mode.CODE32, "nested"); + final X86BytecodeVisitor v1 = + (X86BytecodeVisitor) compiler.createBytecodeVisitor(null, null, os, 0, false); + final X86BytecodeVisitor v2 = + (X86BytecodeVisitor) compiler.createBytecodeVisitor(null, null, os, 0, false); + final ItemFactory f1 = (ItemFactory) ifacField.get(v1); + final ItemFactory f2 = (ItemFactory) ifacField.get(v2); + assertNotSame("simultaneously-live visitors must not share an ItemFactory", f1, f2); + } +} From c44288cf6cd3d32b02276717d2c5848a9dfd9875 Mon Sep 17 00:00:00 2001 From: LSantha Date: Mon, 23 Nov 2026 19:48:29 +0000 Subject: [PATCH 4/4] Cleared VM_INSTANCE before VmImpl; tests pass both orders. Assisted-by: opencode/beam-free --- .../vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java b/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java index 9249076429..21c99aa6a3 100644 --- a/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java +++ b/core/src/test/org/jnode/vm/x86/compiler/l1a/X86Level1AItemFactoryBalanceTest.java @@ -67,6 +67,9 @@ private static File findFile(String... candidates) { @BeforeClass public static void setUpClass() throws Exception { previousVm = VmUtils.getVm(); + final Field vmField = VmUtils.class.getDeclaredField("VM_INSTANCE"); + vmField.setAccessible(true); + vmField.set(null, null); final VmX86Architecture32 arch = new VmX86Architecture32(); final File coreClasses = findFile("./core/build/classes", "core/build/classes", "../core/build/classes"); final File classlibJar = findFile("./all/lib/classlib.jar", "all/lib/classlib.jar", "../all/lib/classlib.jar");