From 5a1bc13634ceea8fe1f120919293083045479cf9 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Tue, 11 Apr 2017 18:44:37 +0200 Subject: SI-2464 Resiliance against missing InnerClass attributes Adapted from scalac commit 2a19cd56258884e25f26565d7b865cc2ec931b23 by Jason Zaugg, but without the testing infrastructure added: A classfile in the wild related to Vaadin lacked the InnerClasses attribute. As such, our class file parser treated a nested enum class as top-level, which led to a crash when trying to find its linked module. More details of the investigation are available in the JIRA comments. The test introduces a new facility to rewrite classfiles. This commit turns this situation into a logged warning, rather than crashing. Code by paulp, test by yours truly. --- .../src/dotty/tools/dotc/core/classfile/ClassfileParser.scala | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala index 26c823f97..e7306f956 100644 --- a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala +++ b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala @@ -227,8 +227,12 @@ class ClassfileParser( // seal java enums if (isEnum) { val enumClass = sym.owner.linkedClass - if (!(enumClass is Flags.Sealed)) enumClass.setFlag(Flags.AbstractSealed) - enumClass.addAnnotation(Annotation.makeChild(sym)) + if (!enumClass.exists) + ctx.warning(s"no linked class for java enum $sym in ${sym.owner}. A referencing class file might be missing an InnerClasses entry.") + else { + if (!(enumClass is Flags.Sealed)) enumClass.setFlag(Flags.AbstractSealed) + enumClass.addAnnotation(Annotation.makeChild(sym)) + } } } finally { in.bp = oldbp -- cgit v1.2.3 From d313143b4b4de1e6ac0a81582fc6164609a5eae1 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Tue, 11 Apr 2017 18:59:48 +0200 Subject: SI-7455 Drop dummy param for synthetic access constructor Adapted from scalac commit 050b4c951c838699c2fe30cbf01b63942c63a299 by Jason Zaugg: Java synthesizes public constructors in private classes to allow access from inner classes. The signature of that synthetic constructor (known as a "access constructor") has a dummy parameter appended to avoid overloading clashes. javac chooses the type "Enclosing$1" for the dummy parameter (called the "access constructor tag") which is either an existing anonymous class or a synthesized class for this purpose. In OpenJDK, this transformation is performed in: langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java (Incidentally, scalac would just emits a byte-code public constructor in this situation, rather than a private constructor / access constructor pair.) Scala parses the signature of the access contructor, and drops the $outer parameter, but retains the dummy parameter. This causes havoc when it tries to parse the bytecode for that anonymous class; the class file parser doesn't have the enclosing type parameters of Vector in scope and crash ensues. In any case, we shouldn't allow user code to see that constructor; it should only be called from within its own compilation unit. This commit drops the dummy parameter from access constructor signatures in class file parsing. --- .../tools/dotc/core/classfile/ClassfileParser.scala | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala index e7306f956..bebc4ab2c 100644 --- a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala +++ b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala @@ -194,13 +194,21 @@ class ClassfileParser( val name = pool.getName(in.nextChar) val isConstructor = name eq nme.CONSTRUCTOR - /** Strip leading outer param from constructor. - * Todo: Also strip trailing access tag for private inner constructors? + /** Strip leading outer param from constructor and trailing access tag for + * private inner constructors. */ - def stripOuterParamFromConstructor() = innerClasses.get(currentClassName) match { + def normalizeConstructorParams() = innerClasses.get(currentClassName) match { case Some(entry) if !isStatic(entry.jflags) => val mt @ MethodTpe(paramNames, paramTypes, resultType) = denot.info - denot.info = mt.derivedLambdaType(paramNames.tail, paramTypes.tail, resultType) + var normalizedParamNames = paramNames.tail + var normalizedParamTypes = paramTypes.tail + if ((jflags & JAVA_ACC_SYNTHETIC) != 0) { + // SI-7455 strip trailing dummy argument ("access constructor tag") from synthetic constructors which + // are added when an inner class needs to access a private constructor. + normalizedParamNames = paramNames.dropRight(1) + normalizedParamTypes = paramTypes.dropRight(1) + } + denot.info = mt.derivedLambdaType(normalizedParamNames, normalizedParamTypes, resultType) case _ => } @@ -216,7 +224,7 @@ class ClassfileParser( denot.info = pool.getType(in.nextChar) if (isEnum) denot.info = ConstantType(Constant(sym)) - if (isConstructor) stripOuterParamFromConstructor() + if (isConstructor) normalizeConstructorParams() setPrivateWithin(denot, jflags) denot.info = translateTempPoly(parseAttributes(sym, denot.info)) if (isConstructor) normalizeConstructorInfo() -- cgit v1.2.3 From 65f438e56b88843fd91287ca60b2e5bdd3af9cb7 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Tue, 11 Apr 2017 19:16:44 +0200 Subject: SI-9915 Utf8_info are modified UTF8 Adapted from scalac commit 3c5990ce5839f4bdfca8fed7f2c415a72f6a8bd8 by Som Snytt: Use DataInputStream.readUTF to read CONSTANT_Utf8_info. This fixes reading embedded null char and supplementary chars. --- .../tools/dotc/core/classfile/ClassfileParser.scala | 8 ++++++-- tests/run/t9915/C_1.java | 20 ++++++++++++++++++++ tests/run/t9915/Test_2.scala | 12 ++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/run/t9915/C_1.java create mode 100644 tests/run/t9915/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala index bebc4ab2c..9415c047f 100644 --- a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala +++ b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala @@ -7,7 +7,7 @@ import Contexts._, Symbols._, Types._, Names._, StdNames._, NameOps._, Scopes._, import SymDenotations._, unpickleScala2.Scala2Unpickler._, Constants._, Annotations._, util.Positions._ import NameKinds.{ModuleClassName, DefaultGetterName} import ast.tpd._ -import java.io.{ File, IOException } +import java.io.{ ByteArrayInputStream, DataInputStream, File, IOException } import java.lang.Integer.toHexString import scala.collection.{ mutable, immutable } import scala.collection.mutable.{ ListBuffer, ArrayBuffer } @@ -935,12 +935,16 @@ class ClassfileParser( case null => val start = starts(index) if (in.buf(start).toInt != CONSTANT_UTF8) errorBadTag(start) - val name = termName(in.buf, start + 3, in.getChar(start + 1)) + val len = in.getChar(start + 1).toInt + val name = termName(fromMUTF8(in.buf, start + 1, len + 2)) values(index) = name name } } + private def fromMUTF8(bytes: Array[Byte], offset: Int, len: Int): String = + new DataInputStream(new ByteArrayInputStream(bytes, offset, len)).readUTF + /** Return the name found at given index in the constant pool, with '/' replaced by '.'. */ def getExternalName(index: Int): SimpleTermName = { if (index <= 0 || len <= index) diff --git a/tests/run/t9915/C_1.java b/tests/run/t9915/C_1.java new file mode 100644 index 000000000..4269cf74e --- /dev/null +++ b/tests/run/t9915/C_1.java @@ -0,0 +1,20 @@ +/* + * javac: -encoding UTF-8 + */ +public class C_1 { + public static final String NULLED = "X\000ABC"; + public static final String SUPPED = "𐒈𐒝𐒑𐒛𐒐𐒘𐒕𐒖"; + + public String nulled() { + return C_1.NULLED; + } + public String supped() { + return C_1.SUPPED; + } + public int nulledSize() { + return C_1.NULLED.length(); + } + public int suppedSize() { + return C_1.SUPPED.length(); + } +} diff --git a/tests/run/t9915/Test_2.scala b/tests/run/t9915/Test_2.scala new file mode 100644 index 000000000..afed667cc --- /dev/null +++ b/tests/run/t9915/Test_2.scala @@ -0,0 +1,12 @@ + +object Test extends App { + val c = new C_1 + assert(c.nulled == "X\u0000ABC") // "X\000ABC" + assert(c.supped == "𐒈𐒝𐒑𐒛𐒐𐒘𐒕𐒖") + + assert(C_1.NULLED == "X\u0000ABC") // "X\000ABC" + assert(C_1.SUPPED == "𐒈𐒝𐒑𐒛𐒐𐒘𐒕𐒖") + + assert(C_1.NULLED.size == "XYABC".size) + assert(C_1.SUPPED.codePointCount(0, C_1.SUPPED.length) == 8) +} -- cgit v1.2.3 From cc4045fb39d777785e315f3003959724ead13b88 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Tue, 11 Apr 2017 23:48:45 +0200 Subject: Disable t5293, benchmarks should not be run as tests --- tests/disabled/run/t5293.scala | 83 ++++++++++++++++++++++++++++++++++++++++++ tests/run/t5293.scala | 83 ------------------------------------------ 2 files changed, 83 insertions(+), 83 deletions(-) create mode 100644 tests/disabled/run/t5293.scala delete mode 100644 tests/run/t5293.scala diff --git a/tests/disabled/run/t5293.scala b/tests/disabled/run/t5293.scala new file mode 100644 index 000000000..8a99989c5 --- /dev/null +++ b/tests/disabled/run/t5293.scala @@ -0,0 +1,83 @@ + + + +import scala.collection.JavaConverters._ + + + +object Test extends dotty.runtime.LegacyApp { + + def bench(label: String)(body: => Unit): Long = { + val start = System.nanoTime + + 0.until(10).foreach(_ => body) + + val end = System.nanoTime + + //println("%s: %s ms".format(label, (end - start) / 1000.0 / 1000.0)) + + end - start + } + + def benchJava(values: java.util.Collection[Int]) = { + bench("Java Set") { + val set = new java.util.HashSet[Int] + + set.addAll(values) + } + } + + def benchScala(values: Iterable[Int]) = { + bench("Scala Set") { + val set = new scala.collection.mutable.HashSet[Int] + + set ++= values + } + } + + def benchScalaSorted(values: Iterable[Int]) = { + bench("Scala Set sorted") { + val set = new scala.collection.mutable.HashSet[Int] + + set ++= values.toArray.sorted + } + } + + def benchScalaPar(values: Iterable[Int]) = { + bench("Scala ParSet") { + val set = new scala.collection.parallel.mutable.ParHashSet[Int] map { x => x } + + set ++= values + } + } + + val values = 0 until 50000 + val set = scala.collection.mutable.HashSet.empty[Int] + + set ++= values + + // warmup + for (x <- 0 until 5) { + benchJava(set.asJava) + benchScala(set) + benchScalaPar(set) + benchJava(set.asJava) + benchScala(set) + benchScalaPar(set) + } + + val javaset = benchJava(set.asJava) + val scalaset = benchScala(set) + val scalaparset = benchScalaPar(set) + + assert(scalaset < (javaset * 8), "scalaset: " + scalaset + " vs. javaset: " + javaset) + assert(scalaparset < (javaset * 8), "scalaparset: " + scalaparset + " vs. javaset: " + javaset) +} + + + + + + + + diff --git a/tests/run/t5293.scala b/tests/run/t5293.scala deleted file mode 100644 index 8a99989c5..000000000 --- a/tests/run/t5293.scala +++ /dev/null @@ -1,83 +0,0 @@ - - - -import scala.collection.JavaConverters._ - - - -object Test extends dotty.runtime.LegacyApp { - - def bench(label: String)(body: => Unit): Long = { - val start = System.nanoTime - - 0.until(10).foreach(_ => body) - - val end = System.nanoTime - - //println("%s: %s ms".format(label, (end - start) / 1000.0 / 1000.0)) - - end - start - } - - def benchJava(values: java.util.Collection[Int]) = { - bench("Java Set") { - val set = new java.util.HashSet[Int] - - set.addAll(values) - } - } - - def benchScala(values: Iterable[Int]) = { - bench("Scala Set") { - val set = new scala.collection.mutable.HashSet[Int] - - set ++= values - } - } - - def benchScalaSorted(values: Iterable[Int]) = { - bench("Scala Set sorted") { - val set = new scala.collection.mutable.HashSet[Int] - - set ++= values.toArray.sorted - } - } - - def benchScalaPar(values: Iterable[Int]) = { - bench("Scala ParSet") { - val set = new scala.collection.parallel.mutable.ParHashSet[Int] map { x => x } - - set ++= values - } - } - - val values = 0 until 50000 - val set = scala.collection.mutable.HashSet.empty[Int] - - set ++= values - - // warmup - for (x <- 0 until 5) { - benchJava(set.asJava) - benchScala(set) - benchScalaPar(set) - benchJava(set.asJava) - benchScala(set) - benchScalaPar(set) - } - - val javaset = benchJava(set.asJava) - val scalaset = benchScala(set) - val scalaparset = benchScalaPar(set) - - assert(scalaset < (javaset * 8), "scalaset: " + scalaset + " vs. javaset: " + javaset) - assert(scalaparset < (javaset * 8), "scalaparset: " + scalaparset + " vs. javaset: " + javaset) -} - - - - - - - - -- cgit v1.2.3