aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7004029 aoqi@0: * @summary Ensure Scope impl can cope with hash collisions aoqi@0: */ aoqi@0: aoqi@0: import java.lang.reflect.*; aoqi@0: import java.io.*; aoqi@0: import com.sun.tools.javac.util.*; aoqi@0: import com.sun.tools.javac.code.*; aoqi@0: import com.sun.tools.javac.code.Scope.*; aoqi@0: import com.sun.tools.javac.code.Symbol.*; aoqi@0: import com.sun.tools.javac.file.JavacFileManager; aoqi@0: import static com.sun.tools.javac.code.Kinds.*; aoqi@0: aoqi@0: public class HashCollisionTest { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new HashCollisionTest().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: // set up basic environment for test aoqi@0: Context context = new Context(); aoqi@0: JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab aoqi@0: names = Names.instance(context); // Name.Table impls tied to an instance of Names aoqi@0: symtab = Symtab.instance(context); aoqi@0: aoqi@0: // determine hashMask for an empty scope aoqi@0: Scope emptyScope = new Scope(symtab.unnamedPackage); // any owner will do aoqi@0: Field sHashMask = Scope.class.getDeclaredField("hashMask"); aoqi@0: sHashMask.setAccessible(true); aoqi@0: scopeHashMask = sHashMask.getInt(emptyScope); aoqi@0: log("scopeHashMask: " + scopeHashMask); aoqi@0: aoqi@0: // 1. determine the Name.hashCode of "Entry", and therefore the index of aoqi@0: // Entry in an empty scope. i.e. name.hashCode() & Scope.hashMask aoqi@0: Name entry = names.fromString("Entry"); aoqi@0: aoqi@0: // 2. create names of the form *$Entry until we find a name with a aoqi@0: // hashcode which yields the same index as Entry in an empty scope. aoqi@0: // Since Name.hashCode is a function of position (and not content) it aoqi@0: // should work to create successively longer names until one with the aoqi@0: // desired characteristics is found. aoqi@0: Name outerName; aoqi@0: Name innerName; aoqi@0: StringBuilder sb = new StringBuilder("C"); aoqi@0: int i = 0; aoqi@0: do { aoqi@0: sb.append(Integer.toString(i % 10)); aoqi@0: innerName = names.fromString(sb + "$Entry"); aoqi@0: } while (!clash(entry, innerName) && (++i) < MAX_TRIES); aoqi@0: aoqi@0: if (clash(entry, innerName)) { aoqi@0: log("Detected expected hash collision for " + entry + " and " + innerName aoqi@0: + " after " + i + " tries"); aoqi@0: } else { aoqi@0: throw new Exception("No potential collision found after " + i + " tries"); aoqi@0: } aoqi@0: aoqi@0: outerName = names.fromString(sb.toString()); aoqi@0: aoqi@0: /* aoqi@0: * Now we can set up the scenario. aoqi@0: */ aoqi@0: aoqi@0: // 3. Create a nested class named Entry aoqi@0: ClassSymbol cc = createClass(names.fromString("C"), symtab.unnamedPackage); aoqi@0: ClassSymbol ce = createClass(entry, cc); aoqi@0: aoqi@0: // 4. Create a package containing a nested class using the name from 2 aoqi@0: PackageSymbol p = new PackageSymbol(names.fromString("p"), symtab.rootPackage); aoqi@0: p.members_field = new Scope(p); aoqi@0: ClassSymbol inner = createClass(innerName, p); aoqi@0: // we'll need this later when we "rename" cn aoqi@0: ClassSymbol outer = createClass(outerName, p); aoqi@0: aoqi@0: // 5. Create a star-import scope aoqi@0: log ("createStarImportScope"); aoqi@0: aoqi@0: // if StarImportScope exists, use it, otherwise, for testing legacy code, aoqi@0: // fall back on ImportScope aoqi@0: Scope starImportScope; aoqi@0: Method importAll; aoqi@0: PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage); aoqi@0: try { aoqi@0: Class c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope"); aoqi@0: Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class }); aoqi@0: importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class }); aoqi@0: starImportScope = (Scope) ctor.newInstance(new Object[] { pkg }); aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: starImportScope = new ImportScope(pkg); aoqi@0: importAll = null; aoqi@0: } aoqi@0: aoqi@0: dump("initial", starImportScope); aoqi@0: aoqi@0: // 6. Insert the contents of the package from 4. aoqi@0: Scope p_members = p.members(); aoqi@0: if (importAll != null) { aoqi@0: importAll.invoke(starImportScope, p_members); aoqi@0: } else { aoqi@0: Scope fromScope = p_members; aoqi@0: Scope toScope = starImportScope; aoqi@0: // The following lines are taken from MemberEnter.importAll, aoqi@0: // before the use of StarImportScope.importAll. aoqi@0: for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { aoqi@0: if (e.sym.kind == TYP && !toScope.includes(e.sym)) aoqi@0: toScope.enter(e.sym, fromScope); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: dump("imported p", starImportScope); aoqi@0: aoqi@0: // 7. Insert the class from 3. aoqi@0: starImportScope.enter(ce, cc.members_field); aoqi@0: dump("imported ce", starImportScope); aoqi@0: aoqi@0: /* aoqi@0: * Set the trap. aoqi@0: */ aoqi@0: aoqi@0: // 8. Rename the nested class to Entry. so that there is a bogus entry in the star-import scope aoqi@0: p.members_field.remove(inner); aoqi@0: inner.name = entry; aoqi@0: inner.owner = outer; aoqi@0: outer.members_field.enter(inner); aoqi@0: aoqi@0: // 9. Lookup Entry aoqi@0: Scope.Entry e = starImportScope.lookup(entry); aoqi@0: dump("final", starImportScope); aoqi@0: aoqi@0: if (e.sym == null) aoqi@0: throw new Exception("symbol not found: " + entry); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Check for a (probable) hash collision in an empty scope. aoqi@0: */ aoqi@0: boolean clash(Name n1, Name n2) { aoqi@0: log(n1 + " hc:" + n1.hashCode() + " v:" + (n1.hashCode() & scopeHashMask) + ", " + aoqi@0: n2 + " hc:" + n2.hashCode() + " v:" + (n2.hashCode() & scopeHashMask)); aoqi@0: return (n1.hashCode() & scopeHashMask) == (n2.hashCode() & scopeHashMask); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a class symbol, init the members scope, and add it to owner's scope. aoqi@0: */ aoqi@0: ClassSymbol createClass(Name name, Symbol owner) { aoqi@0: ClassSymbol sym = new ClassSymbol(0, name, owner); aoqi@0: sym.members_field = new Scope(sym); aoqi@0: if (owner != symtab.unnamedPackage) aoqi@0: owner.members().enter(sym); aoqi@0: return sym; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Dump the contents of a scope to System.err. aoqi@0: */ aoqi@0: void dump(String label, Scope s) throws Exception { aoqi@0: dump(label, s, System.err); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Dump the contents of a scope to a stream. aoqi@0: */ aoqi@0: void dump(String label, Scope s, PrintStream out) throws Exception { aoqi@0: out.println(label); aoqi@0: Field sTable = Scope.class.getDeclaredField("table"); aoqi@0: sTable.setAccessible(true); aoqi@0: aoqi@0: out.println("owner:" + s.owner); aoqi@0: Scope.Entry[] table = (Scope.Entry[]) sTable.get(s); aoqi@0: for (int i = 0; i < table.length; i++) { aoqi@0: if (i > 0) aoqi@0: out.print(", "); aoqi@0: out.print(i + ":" + toString(table[i], table, false)); aoqi@0: } aoqi@0: out.println(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a string showing the contents of an entry, using the table aoqi@0: * to help identify cross-references to other entries in the table. aoqi@0: * @param e the entry to be shown aoqi@0: * @param table the table containing the other entries aoqi@0: */ aoqi@0: String toString(Scope.Entry e, Scope.Entry[] table, boolean ref) { aoqi@0: if (e == null) aoqi@0: return "null"; aoqi@0: if (e.sym == null) aoqi@0: return "sent"; // sentinel aoqi@0: if (ref) { aoqi@0: int index = indexOf(table, e); aoqi@0: if (index != -1) aoqi@0: return String.valueOf(index); aoqi@0: } aoqi@0: return "(" + e.sym.name + ":" + e.sym aoqi@0: + ",shdw:" + toString(e.next(), table, true) aoqi@0: + ",sibl:" + toString(e.sibling, table, true) aoqi@0: + ((e.sym.owner != e.scope.owner) aoqi@0: ? (",BOGUS[" + e.sym.owner + "," + e.scope.owner + "]") aoqi@0: : "") aoqi@0: + ")"; aoqi@0: } aoqi@0: aoqi@0: int indexOf(T[] array, T item) { aoqi@0: for (int i = 0; i < array.length; i++) { aoqi@0: if (array[i] == item) aoqi@0: return i; aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write a message to stderr. aoqi@0: */ aoqi@0: void log(String msg) { aoqi@0: System.err.println(msg); aoqi@0: } aoqi@0: aoqi@0: int MAX_TRIES = 100; // max tries to find a hash clash before giving up. aoqi@0: int scopeHashMask; aoqi@0: aoqi@0: Names names; aoqi@0: Symtab symtab; aoqi@0: }