Skip to content

Commit

Permalink
-changing Method case class to deal with MethodReference; doesn't kee…
Browse files Browse the repository at this point in the history
…p track of static/non-static any more

-dealing with TypeReference instead of TypeAbstraction (might introduce errors)
-moving secret method handling to call-return edge (instead of call-start)
  • Loading branch information
amaurremi committed Nov 28, 2014
1 parent 09da73c commit 192b12c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 57 deletions.
12 changes: 5 additions & 7 deletions src/main/scala/ca/uwaterloo/dataflow/common/Method.scala
@@ -1,30 +1,28 @@
package ca.uwaterloo.dataflow.common

import com.ibm.wala.classLoader.IMethod
import com.ibm.wala.types.MethodReference

case class Method(
name: String,
parameterNum: Int,
isStatic: Boolean,
retType: String
) {

def equalsUpToParamNum(any: Any): Boolean =
any match {
case Method(n, _, s, r) =>
n == name && s == isStatic && r == retType
case Method(n, _, r) =>
n == name && r == retType
case _ =>
false
}
}

object Method {

def apply(method: IMethod): Method =
def apply(method: MethodReference): Method =
Method(
method.getName.toString,
if (method.isStatic) method.getNumberOfParameters else method.getNumberOfParameters - 1,
method.isStatic,
method.getNumberOfParameters,
method.getReturnType.getName.toString
)
}
Expand Up @@ -6,6 +6,7 @@ import com.ibm.wala.ipa.callgraph.impl.ClassHierarchyMethodTargetSelector
import com.ibm.wala.ipa.cfg.BasicBlockInContext
import com.ibm.wala.ssa._
import com.ibm.wala.ssa.analysis.IExplodedBasicBlock
import com.ibm.wala.types.MethodReference

import scala.collection.JavaConverters._
import scala.collection.breakOut
Expand Down Expand Up @@ -117,7 +118,7 @@ trait WalaInstructions extends Phis { this: VariableFacts with ExplodedGraphType
/**
* Value number of a constructor
*/
def initValNum(method: IMethod, callInstr: SSAInvokeInstruction): ValueNumber = {
def initValNum(method: MethodReference, callInstr: SSAInvokeInstruction): ValueNumber = {
assert(method.isInit)
callInstr.getUse(0)
}
Expand Down
Expand Up @@ -8,6 +8,7 @@ import com.ibm.wala.dataflow.IFDS.{ICFGSupergraph, ISupergraph}
import com.ibm.wala.ipa.callgraph.CallGraph
import com.ibm.wala.ipa.callgraph.propagation.{InstanceKey, PointerAnalysis}
import com.ibm.wala.ssa._
import com.ibm.wala.types.MethodReference
import com.ibm.wala.util.collections.HashSetMultiMap
import com.typesafe.config.{ConfigFactory, ConfigParseOptions, ConfigResolveOptions}
import edu.illinois.wala.ipa.callgraph.FlexibleCallGraphBuilder
Expand Down Expand Up @@ -79,7 +80,7 @@ abstract class IfdsTaintAnalysis(configPath: String) extends IfdsProblem with Va
// Fields
case putInstr: SSAPutInstruction
if factSameAsVar(d1, method, putInstr.getVal) ||
isConcatClass(getTypeInference(enclProc(n1.node)).getType(putInstr.getVal)) =>
isConcatClass(getTypeInference(enclProc(n1.node)).getType(putInstr.getVal).getTypeReference) =>
defaultResult + Field(getIField(method.getClassHierarchy, putInstr.getDeclaredField))
case getInstr: SSAGetInstruction =>
d1 match {
Expand Down Expand Up @@ -116,7 +117,7 @@ abstract class IfdsTaintAnalysis(configPath: String) extends IfdsProblem with Va
ideN1.d match {
case v@Variable(method, vn)
if getParameterNumber(ideN1).isDefined &&
isConcatClass(getTypeInference(enclProc(ideN1.n.node)).getType(vn)) =>
isConcatClass(getTypeInference(enclProc(ideN1.n.node)).getType(vn).getTypeReference) =>
// We passed a StringBuilder/StringBuffer as a parameter to the enclosing method;
// the current fact ideN1.d corresponds to this parameter. We need to make sure
// that the StringBuilder/buffer in the calling method becomes secret.
Expand Down Expand Up @@ -168,34 +169,44 @@ abstract class IfdsTaintAnalysis(configPath: String) extends IfdsProblem with Va
val valNum = callValNum(callInstr)
lazy val defaultPlusVar = if (valNum.isDefined) default + Variable(method, valNum.get) else default
val value = if (callInstr.getNumberOfReturnValues == 0) None else Some(callInstr.getReturnValue(0))
getOperationType(callInstr.getDeclaredTarget, n1.getNode, value) match {
case Some(SecretLibraryCall) =>
defaultPlusVar
case Some(ReturnsStaticSecretOrPreservesSecret)
if callInstr.isStatic && d1 == Λ =>
val targetMethod = callInstr.getDeclaredTarget

if (isSecret(targetMethod))
defaultPlusVar
else if (isConcatConstructor(targetMethod) && d1 == Λ){
val valNum = initValNum(targetMethod, callInstr)
val phis = getPhis(n1, valNum, method)
default ++ phis
} else {
getOperationType(targetMethod, n1.getNode, value) match {
case Some(SecretLibraryCall) =>
defaultPlusVar
case Some(ReturnsStaticSecretOrPreservesSecret)
if callInstr.isStatic && d1 == Λ =>
defaultPlusVar
case Some(SecretIfSecretArgument)
if hasSecretArgument(d1, method, callInstr) =>
case Some(SecretIfSecretArgument)
if hasSecretArgument(d1, method, callInstr) =>
defaultPlusVar
case Some(opType) if !callInstr.isStatic =>
val receiver = callInstr.getReceiver
val factEqReceiver = factSameAsVar(d1, method, receiver)
opType match {
case ReturnsStaticSecretOrPreservesSecret if factEqReceiver =>
defaultPlusVar
case ReturnsSecretArray if factEqReceiver =>
default + ArrayElement
case ConcatenatesStrings
if factEqReceiver || isSecondArgument(d1, method, callInstr) =>
case Some(opType) if !callInstr.isStatic =>
val receiver = callInstr.getReceiver
val factEqReceiver = factSameAsVar(d1, method, receiver)
opType match {
case ReturnsStaticSecretOrPreservesSecret if factEqReceiver =>
defaultPlusVar
case ReturnsSecretArray if factEqReceiver =>
default + ArrayElement
case ConcatenatesStrings
if factEqReceiver || isSecondArgument(d1, method, callInstr) =>
defaultPlusVar + Variable(method, callInstr.getReceiver)
case StringConcatConstructor
if isSecondArgument(d1, method, callInstr) =>
case StringConcatConstructor
if isSecondArgument(d1, method, callInstr) =>
default + Variable(method, callInstr.getUse(0))
case _ =>
default
}
case _ =>
default
case _ =>
default
}
case _ =>
default
}
}
}
}
Expand Down Expand Up @@ -223,14 +234,7 @@ abstract class IfdsTaintAnalysis(configPath: String) extends IfdsProblem with Va
}
n1.getLastInstruction match {
case callInstr: SSAInvokeInstruction =>
if (isSecret(targetMethod) && d1 == Λ) {
val valNum = callValNum(callInstr).get
defaultResult + Variable(callerMethod, valNum)
} else if (isConcatConstructor(targetMethod) && d1 == Λ){
val valNum = initValNum(targetMethod, callInstr)
val phis = getPhis(n1, valNum, callerMethod)
defaultResult ++ phis
} else if (exclude(n1, callInstr))
if (exclude(n1, callInstr))
Set.empty
else
getParameterNumber(ideN1, callInstr) match { // checks if we are passing d1 as an argument to the function
Expand All @@ -246,8 +250,8 @@ abstract class IfdsTaintAnalysis(configPath: String) extends IfdsProblem with Va
}
}

private[this] def isConcatConstructor(method: IMethod): Boolean =
method.isInit && isConcatClass(new PointType(method.getDeclaringClass))
private[this] def isConcatConstructor(method: MethodReference): Boolean =
method.isInit && isConcatClass(method.getDeclaringClass)

/**
* Should the call instruction be excluded from the analysis?
Expand Down
Expand Up @@ -29,11 +29,11 @@ trait SecretDefinition extends VariableFacts {
klass: String
)

def isSecret(method: IMethod): Boolean
def isSecret(method: MethodReference): Boolean

def secretTypes: Set[String]

def isConcatClass(typeRef: TypeAbstraction): Boolean
def isConcatClass(typeRef: TypeReference): Boolean

def isSecretArrayElementType(typeRef: TypeReference): Boolean

Expand Down
Expand Up @@ -40,11 +40,10 @@ trait SecretDefFromConfig extends SecretDefinition {
name == of || amongSubtypes
}

override def isSecret(method: IMethod) =
override def isSecret(method: MethodReference) =
stringConfig.secretMethods exists {
sm =>
(sm.method equalsUpToParamNum Method(method)) &&
isSubType(method.getDeclaringClass.getReference, sm.enclClass, method.getClassHierarchy)
sm.method equalsUpToParamNum Method(method)
}

lazy val stringConfig: SecretConfig = {
Expand All @@ -59,7 +58,6 @@ trait SecretDefFromConfig extends SecretDefinition {
Method(
conf getString "name",
-1,
conf getBoolean "static",
conf getString "type"
),
conf getString "enclosing"
Expand Down Expand Up @@ -100,11 +98,10 @@ trait SecretDefFromConfig extends SecretDefinition {

override def secretTypes: Set[String] = stringConfig.secretMethods map { _.method.retType }

override def isConcatClass(typeAbs: TypeAbstraction): Boolean =
typeAbs != TypeAbstraction.TOP &&
(stringConfig.appendMethods exists {
_.klass == typeName(typeAbs.getTypeReference)
})
override def isConcatClass(typeRef: TypeReference): Boolean =
stringConfig.appendMethods exists {
_.klass == typeName(typeRef)
}

override def isSecretArrayElementType(typeRef: TypeReference) =
stringConfig.arrayElemTypes contains typeName(typeRef)
Expand Down
Expand Up @@ -13,7 +13,7 @@ sealed abstract class AbstractTaintAnalysisSpecBuilder (
fileName: String
) extends IfdsTaintAnalysis(fileName) with VariableFacts with AbstractIdeToIfds with Assertions {

def secretAssertion(name: String) = Method(name, 1, isStatic = true, "V")
def secretAssertion(name: String) = Method(name, 1, "V")

protected val secret = secretAssertion("secret")
protected val notSecret = secretAssertion("notSecret")
Expand All @@ -33,7 +33,7 @@ sealed abstract class AbstractTaintAnalysisSpecBuilder (
case (node, invokeInstr) =>
targetStartNodes(NormalNode(node)) foreach {
startNode =>
assertionMap.get(Method(startNode.node.getMethod)) foreach {
assertionMap.get(Method(startNode.node.getMethod.getReference)) foreach {
assertResult(_)(getResultAtCallNode(node, invokeInstr))
}
}
Expand Down

0 comments on commit 192b12c

Please sign in to comment.