From 5fc16e4ee7ab0be163f4a12600538ce628a6ce2a Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Thu, 5 Jun 2014 10:37:20 +0200 Subject: Test: classfile reading during inlining works if there is dead code --- test/files/run/icode-reader-dead-code.check | 19 +++++++ test/files/run/icode-reader-dead-code.scala | 82 +++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 test/files/run/icode-reader-dead-code.check create mode 100644 test/files/run/icode-reader-dead-code.scala (limited to 'test') diff --git a/test/files/run/icode-reader-dead-code.check b/test/files/run/icode-reader-dead-code.check new file mode 100644 index 0000000000..d1739fed3b --- /dev/null +++ b/test/files/run/icode-reader-dead-code.check @@ -0,0 +1,19 @@ +Bytecode for method f + L0 + LINENUMBER 4 L0 + ICONST_1 + IRETURN + L1 + LOCALVARIABLE this Lp/A; L0 L1 0 + MAXSTACK = 1 + MAXLOCALS = 1 +Bytecode for method f + L0 + LINENUMBER 4 L0 + ICONST_1 + ATHROW + IRETURN + L1 + LOCALVARIABLE this Lp/A; L0 L1 0 + MAXSTACK = 1 + MAXLOCALS = 1 diff --git a/test/files/run/icode-reader-dead-code.scala b/test/files/run/icode-reader-dead-code.scala new file mode 100644 index 0000000000..00ba58829f --- /dev/null +++ b/test/files/run/icode-reader-dead-code.scala @@ -0,0 +1,82 @@ +import java.io.{FileOutputStream, FileInputStream} + +import scala.tools.asm.{ClassWriter, Opcodes, ClassReader} +import scala.tools.asm.tree.{InsnNode, ClassNode} +import scala.tools.nsc.backend.jvm.AsmUtils +import scala.tools.partest.DirectTest +import scala.collection.JavaConverters._ + +/** + * Test that the ICodeReader does not crash if the bytecode of a method has unreachable code. + */ +object Test extends DirectTest { + def code: String = ??? + + def show(): Unit = { + // The bytecode of f will be modified using ASM by `addDeadCode` + val aCode = + """ + |package p + |class A { + | @inline final def f = 1 + |} + """.stripMargin + + val bCode = + """ + |package p + |class B { + | def g = (new A()).f + |} + """.stripMargin + + compileString(newCompiler("-usejavacp"))(aCode) + + addDeadCode() + + // If inlining fails, the compiler will issue an inliner warning that is not present in the + // check file + compileString(newCompiler("-usejavacp", "-optimise"))(bCode) + } + + def readClass(file: String) = { + val cnode = new ClassNode() + val is = new FileInputStream(file) + val reader = new ClassReader(is) + reader.accept(cnode, 0) + is.close() + cnode + } + + def writeClass(file: String, cnode: ClassNode): Unit = { + val writer = new ClassWriter(0) + cnode.accept(writer) + + val os = new FileOutputStream(file) + os.write(writer.toByteArray) + os.close() + } + + def addDeadCode() { + val file = (testOutput / "p" / "A.class").path + val cnode = readClass(file) + val method = cnode.methods.asScala.find(_.name == "f").head + + AsmUtils.traceMethod(method) + + val insns = method.instructions + val it = insns.iterator() + while (it.hasNext) { + val in = it.next() + if (in.getOpcode == Opcodes.IRETURN) { + // Insert an ATHROW before the IRETURN. The IRETURN will then be dead code. + // The ICodeReader should not crash if there's dead code. + insns.insert(in.getPrevious, new InsnNode(Opcodes.ATHROW)) + } + } + + AsmUtils.traceMethod(method) + + writeClass(file, cnode) + } +} -- cgit v1.2.3