OPAL: Why can SingleOriginReference not be found any more after externalizing code into a method?

64 views Asked by At

I am developing a static analysis of Java Bytecode using the OPAL framework.

I currently need to change the structure of the code, to add some functionality.

This originated in a big method, of which I have to externalize a part into a separate method:

def singleCallUpperTypeBounds(
  caller: Method,
  pc: Int,
  calleeDescriptor: MethodDescriptor,
  project: Project[URL],
  callGraph: CallGraph,
  propertyStore: PropertyStore): Iterable[(Int, Set[FieldType])] = {
  val classFile = project.classFile(caller)
  val callDescriptor = caller.body.get.instructions(pc) match {
    case INVOKEVIRTUAL(_, _, d) ⇒ d
    case INVOKESPECIAL(_, _, d) ⇒ d
    case INVOKESTATIC(_, _, d) ⇒ d
    case INVOKEINTERFACE(_, _, d) ⇒ d
  }
  val analysisResult = if (!notClientCallable(caller, propertyStore) || worklist.contains(caller))
    BaseAI.perform(classFile, caller, new DefaultDomain(project, classFile, caller))(None)
  else {
    val callerTypeMap = intermediateResult.getOrElse(caller, {
      worklist = worklist.+:(caller)
      val result = singleMethodUpperTypeBounds(caller, project, callGraph, propertyStore)
      worklist = worklist.diff(Seq(caller))
      result
    })
    //Create all combinations of the upper type bounds of the parameters.
    //If a parameter is not in the TypeMap, 
    //e.g. because it is a primitive value, add it as a one element set.
    val typeCombinations = allCombinations(caller.descriptor.parameterTypes.zipWithIndex.map {
      case (t, index) =>
        callerTypeMap.getOrElse(index,
          Set[FieldType](caller.descriptor.parameterTypes(index)))
    })
    println(typeCombinations)
    //TODO Use the type combinations
    BaseAI.perform(classFile, caller, new DefaultDomain(project, classFile, caller))(None)
  }
  if (analysisResult.evaluatedInstructions.contains(pc))
    for {
      parameterIndex ← callDescriptor.parameterTypes.zipWithIndex.collect {
        //we are not interested in primitive array types
        case (t: ReferenceType, index) if {
          //may be the case for sinature polymorphic methods
          if (index >= calleeDescriptor.parametersCount) {
            true
          } else {
            val expectedType = calleeDescriptor.parameterType(index)
            !(expectedType.isArrayType && expectedType.asArrayType.elementType.isBaseType)
          }
        } ⇒ index
      }
      compileTimeType = callDescriptor.parameterType(parameterIndex)
      stackIndex = (callDescriptor.parametersCount - 1) - parameterIndex
    } yield {
      val operand = analysisResult.operandsArray(pc)(stackIndex)
      val runTimeTypes: Set[FieldType] = operand match {
        case v: analysisResult.domain.SingleOriginReferenceValue ⇒
          v.upperTypeBound.foldLeft(Set[FieldType]())((set, t) ⇒ set + t)
        case analysisResult.domain.MultipleReferenceValues(singleOriginReferenceValues) ⇒
          singleOriginReferenceValues.foldLeft(Set[FieldType]())((set, sorv) ⇒ set ++
            sorv.upperTypeBound.foldLeft(Set[FieldType]())((s, t) ⇒ s + t))
      }
      (parameterIndex, runTimeTypes)
    }
  //If the call was not evaluated, it is on a dead path. So ignore this call.
  else {
    Set[(Int, Set[FieldType])]()
  }
}

This is why I externalized the big if block at the end into a separate method:

def evaluateAIResult(
  analysisResult: AIResult,
  pc: Int,
  calleeDescriptor: MethodDescriptor,
  callDescriptor: MethodDescriptor): Iterable[(Int, Set[FieldType])] = {
  if (analysisResult.evaluatedInstructions.contains(pc))
    for {
      parameterIndex ← callDescriptor.parameterTypes.zipWithIndex.collect {
        //we are not interested in primitive array types
        case (t: ReferenceType, index) if {
          //may be the case for sinature polymorphic methods
          if (index >= calleeDescriptor.parametersCount) {
            true
          } else {
            val expectedType = calleeDescriptor.parameterType(index)
            !(expectedType.isArrayType && expectedType.asArrayType.elementType.isBaseType)
          }
        } ⇒ index
      }
      compileTimeType = callDescriptor.parameterType(parameterIndex)
      stackIndex = (callDescriptor.parametersCount - 1) - parameterIndex
    } yield {
      val operand = analysisResult.operandsArray(pc)(stackIndex)
      val runTimeTypes: Set[FieldType] = operand match {
        case v: analysisResult.domain.SingleOriginReferenceValue ⇒
          v.upperTypeBound.foldLeft(Set[FieldType]())((set, t) ⇒ set + t)
        case analysisResult.domain.MultipleReferenceValues(singleOriginReferenceValues) ⇒
          singleOriginReferenceValues.foldLeft(Set[FieldType]())((set, sorv) ⇒ set ++
            sorv.upperTypeBound.foldLeft(Set[FieldType]())((s, t) ⇒ s + t))
      }
      (parameterIndex, runTimeTypes)
    }
  //If the call was not evaluated, it is on a dead path. So ignore this call.
  else {
    Set[(Int, Set[FieldType])]()
  }
}

For any reason, I now get some errors for these lines in Scala IDE:

case v: analysisResult.domain.SingleOriginReferenceValue ⇒
          v.upperTypeBound.foldLeft(Set[FieldType]())((set, t) ⇒ set + t)
case analysisResult.domain.MultipleReferenceValues(singleOriginReferenceValues) ⇒
          singleOriginReferenceValues.foldLeft(Set[FieldType]())((set, sorv) ⇒ set ++
            sorv.upperTypeBound.foldLeft(Set[FieldType]())((s, t) ⇒ s + t))

The error messages are the following:

type SingleOriginReferenceValue is not a member of org.opalj.ai.Domain

and

value MultipleReferenceValues is not a member of org.opalj.ai.Domain

Before I externalized this if-block into a separate method, these error messages did not occur. Changing these lines to

case v: SingleOriginReferenceValue ⇒
          v.upperTypeBound.foldLeft(Set[FieldType]())((set, t) ⇒ set + t)
case MultipleReferenceValues(singleOriginReferenceValues) ⇒
          singleOriginReferenceValues.foldLeft(Set[FieldType]())((set, sorv) ⇒ set ++
            sorv.upperTypeBound.foldLeft(Set[FieldType]())((s, t) ⇒ s + t))

and doing the imports import org.opalj.ai.domain.l1.ReferenceValues.SingleOriginReferenceValue import org.opalj.ai.domain.l1.ReferenceValues.MultipleReferenceValues also does not help.

Can anyone tell me, what's going wrong here?

1

There are 1 answers

1
Delors On BEST ANSWER

In this case you need to specify (for the helper) that you require an AIResult with a specific type of a domain. (OPAL-AI makes heavy use of so-called path-dependent types.)

The following change of the signature of the helper method should help:

def evaluateAIResult(
  analysisResult: AIResult { val domain: l1.DefaultDomain /*or whatever your domain requirements are*/},
  pc: Int,
  calleeDescriptor: MethodDescriptor,
  callDescriptor: MethodDescriptor): Iterable[(Int, Set[FieldType])] = {...