test/tools/javac/scope/HashCollisionTest.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 962
0ff2bbd38f10
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7004029
aoqi@0 27 * @summary Ensure Scope impl can cope with hash collisions
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.lang.reflect.*;
aoqi@0 31 import java.io.*;
aoqi@0 32 import com.sun.tools.javac.util.*;
aoqi@0 33 import com.sun.tools.javac.code.*;
aoqi@0 34 import com.sun.tools.javac.code.Scope.*;
aoqi@0 35 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 36 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 37 import static com.sun.tools.javac.code.Kinds.*;
aoqi@0 38
aoqi@0 39 public class HashCollisionTest {
aoqi@0 40 public static void main(String... args) throws Exception {
aoqi@0 41 new HashCollisionTest().run();
aoqi@0 42 }
aoqi@0 43
aoqi@0 44 void run() throws Exception {
aoqi@0 45 // set up basic environment for test
aoqi@0 46 Context context = new Context();
aoqi@0 47 JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
aoqi@0 48 names = Names.instance(context); // Name.Table impls tied to an instance of Names
aoqi@0 49 symtab = Symtab.instance(context);
aoqi@0 50
aoqi@0 51 // determine hashMask for an empty scope
aoqi@0 52 Scope emptyScope = new Scope(symtab.unnamedPackage); // any owner will do
aoqi@0 53 Field sHashMask = Scope.class.getDeclaredField("hashMask");
aoqi@0 54 sHashMask.setAccessible(true);
aoqi@0 55 scopeHashMask = sHashMask.getInt(emptyScope);
aoqi@0 56 log("scopeHashMask: " + scopeHashMask);
aoqi@0 57
aoqi@0 58 // 1. determine the Name.hashCode of "Entry", and therefore the index of
aoqi@0 59 // Entry in an empty scope. i.e. name.hashCode() & Scope.hashMask
aoqi@0 60 Name entry = names.fromString("Entry");
aoqi@0 61
aoqi@0 62 // 2. create names of the form *$Entry until we find a name with a
aoqi@0 63 // hashcode which yields the same index as Entry in an empty scope.
aoqi@0 64 // Since Name.hashCode is a function of position (and not content) it
aoqi@0 65 // should work to create successively longer names until one with the
aoqi@0 66 // desired characteristics is found.
aoqi@0 67 Name outerName;
aoqi@0 68 Name innerName;
aoqi@0 69 StringBuilder sb = new StringBuilder("C");
aoqi@0 70 int i = 0;
aoqi@0 71 do {
aoqi@0 72 sb.append(Integer.toString(i % 10));
aoqi@0 73 innerName = names.fromString(sb + "$Entry");
aoqi@0 74 } while (!clash(entry, innerName) && (++i) < MAX_TRIES);
aoqi@0 75
aoqi@0 76 if (clash(entry, innerName)) {
aoqi@0 77 log("Detected expected hash collision for " + entry + " and " + innerName
aoqi@0 78 + " after " + i + " tries");
aoqi@0 79 } else {
aoqi@0 80 throw new Exception("No potential collision found after " + i + " tries");
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 outerName = names.fromString(sb.toString());
aoqi@0 84
aoqi@0 85 /*
aoqi@0 86 * Now we can set up the scenario.
aoqi@0 87 */
aoqi@0 88
aoqi@0 89 // 3. Create a nested class named Entry
aoqi@0 90 ClassSymbol cc = createClass(names.fromString("C"), symtab.unnamedPackage);
aoqi@0 91 ClassSymbol ce = createClass(entry, cc);
aoqi@0 92
aoqi@0 93 // 4. Create a package containing a nested class using the name from 2
aoqi@0 94 PackageSymbol p = new PackageSymbol(names.fromString("p"), symtab.rootPackage);
aoqi@0 95 p.members_field = new Scope(p);
aoqi@0 96 ClassSymbol inner = createClass(innerName, p);
aoqi@0 97 // we'll need this later when we "rename" cn
aoqi@0 98 ClassSymbol outer = createClass(outerName, p);
aoqi@0 99
aoqi@0 100 // 5. Create a star-import scope
aoqi@0 101 log ("createStarImportScope");
aoqi@0 102
aoqi@0 103 // if StarImportScope exists, use it, otherwise, for testing legacy code,
aoqi@0 104 // fall back on ImportScope
aoqi@0 105 Scope starImportScope;
aoqi@0 106 Method importAll;
aoqi@0 107 PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage);
aoqi@0 108 try {
aoqi@0 109 Class<?> c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope");
aoqi@0 110 Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class });
aoqi@0 111 importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class });
aoqi@0 112 starImportScope = (Scope) ctor.newInstance(new Object[] { pkg });
aoqi@0 113 } catch (ClassNotFoundException e) {
aoqi@0 114 starImportScope = new ImportScope(pkg);
aoqi@0 115 importAll = null;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 dump("initial", starImportScope);
aoqi@0 119
aoqi@0 120 // 6. Insert the contents of the package from 4.
aoqi@0 121 Scope p_members = p.members();
aoqi@0 122 if (importAll != null) {
aoqi@0 123 importAll.invoke(starImportScope, p_members);
aoqi@0 124 } else {
aoqi@0 125 Scope fromScope = p_members;
aoqi@0 126 Scope toScope = starImportScope;
aoqi@0 127 // The following lines are taken from MemberEnter.importAll,
aoqi@0 128 // before the use of StarImportScope.importAll.
aoqi@0 129 for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
aoqi@0 130 if (e.sym.kind == TYP && !toScope.includes(e.sym))
aoqi@0 131 toScope.enter(e.sym, fromScope);
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 dump("imported p", starImportScope);
aoqi@0 136
aoqi@0 137 // 7. Insert the class from 3.
aoqi@0 138 starImportScope.enter(ce, cc.members_field);
aoqi@0 139 dump("imported ce", starImportScope);
aoqi@0 140
aoqi@0 141 /*
aoqi@0 142 * Set the trap.
aoqi@0 143 */
aoqi@0 144
aoqi@0 145 // 8. Rename the nested class to Entry. so that there is a bogus entry in the star-import scope
aoqi@0 146 p.members_field.remove(inner);
aoqi@0 147 inner.name = entry;
aoqi@0 148 inner.owner = outer;
aoqi@0 149 outer.members_field.enter(inner);
aoqi@0 150
aoqi@0 151 // 9. Lookup Entry
aoqi@0 152 Scope.Entry e = starImportScope.lookup(entry);
aoqi@0 153 dump("final", starImportScope);
aoqi@0 154
aoqi@0 155 if (e.sym == null)
aoqi@0 156 throw new Exception("symbol not found: " + entry);
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 /*
aoqi@0 160 * Check for a (probable) hash collision in an empty scope.
aoqi@0 161 */
aoqi@0 162 boolean clash(Name n1, Name n2) {
aoqi@0 163 log(n1 + " hc:" + n1.hashCode() + " v:" + (n1.hashCode() & scopeHashMask) + ", " +
aoqi@0 164 n2 + " hc:" + n2.hashCode() + " v:" + (n2.hashCode() & scopeHashMask));
aoqi@0 165 return (n1.hashCode() & scopeHashMask) == (n2.hashCode() & scopeHashMask);
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 /**
aoqi@0 169 * Create a class symbol, init the members scope, and add it to owner's scope.
aoqi@0 170 */
aoqi@0 171 ClassSymbol createClass(Name name, Symbol owner) {
aoqi@0 172 ClassSymbol sym = new ClassSymbol(0, name, owner);
aoqi@0 173 sym.members_field = new Scope(sym);
aoqi@0 174 if (owner != symtab.unnamedPackage)
aoqi@0 175 owner.members().enter(sym);
aoqi@0 176 return sym;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 /**
aoqi@0 180 * Dump the contents of a scope to System.err.
aoqi@0 181 */
aoqi@0 182 void dump(String label, Scope s) throws Exception {
aoqi@0 183 dump(label, s, System.err);
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 /**
aoqi@0 187 * Dump the contents of a scope to a stream.
aoqi@0 188 */
aoqi@0 189 void dump(String label, Scope s, PrintStream out) throws Exception {
aoqi@0 190 out.println(label);
aoqi@0 191 Field sTable = Scope.class.getDeclaredField("table");
aoqi@0 192 sTable.setAccessible(true);
aoqi@0 193
aoqi@0 194 out.println("owner:" + s.owner);
aoqi@0 195 Scope.Entry[] table = (Scope.Entry[]) sTable.get(s);
aoqi@0 196 for (int i = 0; i < table.length; i++) {
aoqi@0 197 if (i > 0)
aoqi@0 198 out.print(", ");
aoqi@0 199 out.print(i + ":" + toString(table[i], table, false));
aoqi@0 200 }
aoqi@0 201 out.println();
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 /**
aoqi@0 205 * Create a string showing the contents of an entry, using the table
aoqi@0 206 * to help identify cross-references to other entries in the table.
aoqi@0 207 * @param e the entry to be shown
aoqi@0 208 * @param table the table containing the other entries
aoqi@0 209 */
aoqi@0 210 String toString(Scope.Entry e, Scope.Entry[] table, boolean ref) {
aoqi@0 211 if (e == null)
aoqi@0 212 return "null";
aoqi@0 213 if (e.sym == null)
aoqi@0 214 return "sent"; // sentinel
aoqi@0 215 if (ref) {
aoqi@0 216 int index = indexOf(table, e);
aoqi@0 217 if (index != -1)
aoqi@0 218 return String.valueOf(index);
aoqi@0 219 }
aoqi@0 220 return "(" + e.sym.name + ":" + e.sym
aoqi@0 221 + ",shdw:" + toString(e.next(), table, true)
aoqi@0 222 + ",sibl:" + toString(e.sibling, table, true)
aoqi@0 223 + ((e.sym.owner != e.scope.owner)
aoqi@0 224 ? (",BOGUS[" + e.sym.owner + "," + e.scope.owner + "]")
aoqi@0 225 : "")
aoqi@0 226 + ")";
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 <T> int indexOf(T[] array, T item) {
aoqi@0 230 for (int i = 0; i < array.length; i++) {
aoqi@0 231 if (array[i] == item)
aoqi@0 232 return i;
aoqi@0 233 }
aoqi@0 234 return -1;
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 /**
aoqi@0 238 * Write a message to stderr.
aoqi@0 239 */
aoqi@0 240 void log(String msg) {
aoqi@0 241 System.err.println(msg);
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 int MAX_TRIES = 100; // max tries to find a hash clash before giving up.
aoqi@0 245 int scopeHashMask;
aoqi@0 246
aoqi@0 247 Names names;
aoqi@0 248 Symtab symtab;
aoqi@0 249 }

mercurial