Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 2 additions & 18 deletions core/src/core/org/jnode/vm/x86/compiler/l1a/ItemFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntItem> intItems = new ArrayList<IntItem>();

private final ArrayList<LongItem> longItems = new ArrayList<LongItem>();
Expand Down Expand Up @@ -309,23 +307,9 @@ private Item createNew(int jvmType) {
}

/**
* Hidden constructor.
*/
private ItemFactory() {
}

/**
* Gets the item factory. This item factory is singleton per thread.
*
* @return
* Constructor.
*/
static ItemFactory getFactory() {
ItemFactory fac = (ItemFactory) itemFactory.get();
if (fac == null) {
fac = new ItemFactory();
itemFactory.set(fac);
}
return fac;
ItemFactory() {
}

boolean isBalanced() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ public X86BytecodeVisitor(NativeStream outputStream, CompiledMethod cm,
gprPool = new X86RegisterPool.GPRs64();
xmmPool = new X86RegisterPool.XMMs64();
}
this.ifac = ItemFactory.getFactory();
// 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);
this.cm = cm;
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/org/jnode/test/CoreTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,6 +56,7 @@
WaitTest.class,
JavaCommandParserTest.class,
ThreadingTestJUnit4.class,
X86Level1AItemFactoryBalanceTest.class,
X86Level1ACompilerVisitorTest.class,
X86RegisterPoolTest.class,
X86StreamTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* $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.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, and simultaneously-live visitors must not share one factory.
*/
public class X86Level1AItemFactoryBalanceTest {

private static VmSystemClassLoader cl;
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) {
File f = new File(c);
if (f.exists()) {
return f;
}
}
return new File(candidates[0]);
}

@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");
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 {
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");
}

@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);
}
}
Loading