Skip to content

Commit

Permalink
[backport] SI-9376 don't crash when inlining a closure body that throws.
Browse files Browse the repository at this point in the history
If the closure body method has return type Nothing$, add an `ATHROW`
instruction after the callsite. This is required for computing stack
map frames, as explained in a comment in BCodeBodyBuilder.adapt.

Similar for closure bodies with return type Null$.
  • Loading branch information
lrytz committed Jul 23, 2015
1 parent e511375 commit 1b0703e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.collection.mutable
import scala.reflect.internal.util.Collections._
import scala.tools.asm.commons.CodeSizeEvaluator
import scala.tools.asm.tree.analysis._
import scala.tools.asm.{MethodWriter, ClassWriter, Label, Opcodes}
import scala.tools.asm.{MethodWriter, ClassWriter, Label, Opcodes, Type}
import scala.tools.asm.tree._
import GenBCode._
import scala.collection.convert.decorateAsScala._
Expand Down Expand Up @@ -330,6 +330,26 @@ object BytecodeUtils {
)).toList
}

/**
* This method is used by optimizer components to eliminate phantom values of instruction
* that load a value of type `Nothing$` or `Null$`. Such values on the stack don't interact well
* with stack map frames.
*
* For example, `opt.getOrElse(throw e)` is re-written to an invocation of the lambda body, a
* method with return type `Nothing$`. Similarly for `opt.getOrElse(null)` and `Null$`.
*
* During bytecode generation this is handled by BCodeBodyBuilder.adapt. See the comment in that
* method which explains the issue with such phantom values.
*/
def fixLoadedNothingOrNullValue(loadedType: Type, loadInstr: AbstractInsnNode, methodNode: MethodNode, bTypes: BTypes): Unit = {
if (loadedType == bTypes.coreBTypes.RT_NOTHING.toASMType) {
methodNode.instructions.insert(loadInstr, new InsnNode(Opcodes.ATHROW))
} else if (loadedType == bTypes.coreBTypes.RT_NULL.toASMType) {
methodNode.instructions.insert(loadInstr, new InsnNode(Opcodes.ACONST_NULL))
methodNode.instructions.insert(loadInstr, new InsnNode(Opcodes.POP))
}
}

/**
* A wrapper to make ASM's Analyzer a bit easier to use.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ class ClosureOptimizer[BT <: BTypes](val btypes: BT) {
val isInterface = bodyOpcode == INVOKEINTERFACE
val bodyInvocation = new MethodInsnNode(bodyOpcode, lambdaBodyHandle.getOwner, lambdaBodyHandle.getName, lambdaBodyHandle.getDesc, isInterface)
methodNode.instructions.insertBefore(invocation, bodyInvocation)

val returnType = Type.getReturnType(lambdaBodyHandle.getDesc)
fixLoadedNothingOrNullValue(returnType, bodyInvocation, methodNode, btypes) // see comment of that method

methodNode.instructions.remove(invocation)

// update the call graph
Expand Down

0 comments on commit 1b0703e

Please sign in to comment.