Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method return error handling #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package io.joern.dataflowengineoss.passes.reachingdef

import io.joern.dataflowengineoss.{globalFromLiteral, identifierToFirstUsages}
import io.joern.dataflowengineoss.queryengine.AccessPathUsage.toTrackedBaseAndAccessPathSimple
import io.joern.dataflowengineoss.semanticsloader.Semantics
import io.joern.dataflowengineoss.{globalFromLiteral, identifierToFirstUsages}
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.codepropertygraph.generated.{EdgeTypes, Operators, PropertyNames}
import io.shiftleft.semanticcpg.accesspath.MatchResult
import io.shiftleft.semanticcpg.language.*
import org.slf4j.LoggerFactory
import overflowdb.BatchedUpdate.DiffGraphBuilder

import scala.collection.{Set, mutable}

/** Creation of data dependence edges based on solution of the ReachingDefProblem.
*/
class DdgGenerator(semantics: Semantics) {

private val logger = LoggerFactory.getLogger(getClass)
implicit val s: Semantics = semantics

/** Once reaching definitions have been computed, we create a data dependence graph by checking which reaching
Expand Down Expand Up @@ -168,6 +169,7 @@ class DdgGenerator(semantics: Semantics) {
}

def addEdgesToCapturedIdentifiersAndParameters(): Unit = {

val identifierDestPairs =
method._identifierViaContainsOut
.flatMap { identifier =>
Expand Down Expand Up @@ -202,18 +204,22 @@ class DdgGenerator(semantics: Semantics) {
}
}
}
try {
addEdgesFromEntryNode()
allNodes.foreach {
case call: Call => addEdgesToCallSite(call)
case ret: Return => addEdgesToReturn(ret)
case paramOut: MethodParameterOut => addEdgesToMethodParameterOut(paramOut)
case _ =>
}

addEdgesFromEntryNode()
allNodes.foreach {
case call: Call => addEdgesToCallSite(call)
case ret: Return => addEdgesToReturn(ret)
case paramOut: MethodParameterOut => addEdgesToMethodParameterOut(paramOut)
case _ =>
addEdgesToCapturedIdentifiersAndParameters()
addEdgesToExitNode(method.methodReturn)
addEdgesFromLoneIdentifiersToExit(method)
} catch {
case exception: Exception =>
logger.warn("Error in DdgGenerator,", exception)
}

addEdgesToCapturedIdentifiersAndParameters()
addEdgesToExitNode(method.methodReturn)
addEdgesFromLoneIdentifiersToExit(method)
}

private def addEdge(fromNode: StoredNode, toNode: StoredNode, variable: String = "")(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,22 @@ object UsageSlicing {
typeDecl.fullName,
typeDecl.member.map(m => DefComponent.fromNode(m)).collectAll[LocalDef].l,
typeDecl.method
.map(m =>
ObservedCall(
m.name,
Option(m.fullName),
m.parameter.map(_.typeFullName).toList,
m.methodReturn.typeFullName,
m.lineNumber.map(_.intValue()),
m.columnNumber.map(_.intValue())
)
.flatMap(m =>
Try(
ObservedCall(
m.name,
Option(m.fullName),
m.parameter.map(_.typeFullName).toList,
m.methodReturn.typeFullName,
m.lineNumber.map(_.intValue()),
m.columnNumber.map(_.intValue())
)
) match {
case Success(value) => Some(value)
case Failure(exception) =>
logger.warn("Error in dataflow UsageSlicing", exception)
None
}
)
.l,
typeDecl.filename,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,25 @@ abstract class RecoverForXCompilationUnit[CompilationUnitType <: AstNode](
}

override def run(): Unit = {
// Set known aliases that point to imports for local and external methods/modules
importNodes.foreach(visitImport)
// Look at symbols with existing type info
prepopulateSymbolTable()
// Prune import names if the methods exist in the CPG
postVisitImports()
// Populate local symbol table with assignments
assignments.foreach(visitAssignments)
// See if any new information are in the parameters of methods
returns.foreach(visitReturns)
// Persist findings
setTypeInformation()
// Entrypoint for any final changes
postSetTypeInformation()
try {
// Set known aliases that point to imports for local and external methods/modules
importNodes.foreach(visitImport)
// Look at symbols with existing type info
prepopulateSymbolTable()
// Prune import names if the methods exist in the CPG
postVisitImports()
// Populate local symbol table with assignments
assignments.foreach(visitAssignments)
// See if any new information are in the parameters of methods
returns.foreach(visitReturns)
// Persist findings
setTypeInformation()
// Entrypoint for any final changes
postSetTypeInformation()
} catch {
case ex: Exception =>
logger.warn(s"Error in XTypeRecovery ", ex)
}
}

private def debugLocation(n: AstNode): String = {
Expand Down Expand Up @@ -643,7 +648,14 @@ abstract class RecoverForXCompilationUnit[CompilationUnitType <: AstNode](
protected def methodReturnValues(methodFullNames: Seq[String]): Set[String] = {
val rs = cpg.method
.fullNameExact(methodFullNames*)
.methodReturn
.flatMap(method => {
Try(method.methodReturn) match {
case Success(value) => Some(value)
case Failure(exception) =>
logger.warn(s"Error in Type Recovery for method ${method.fullName} from file ${method.filename}")
None
}
})
.flatMap(mr => mr.typeFullName +: (mr.dynamicTypeHintFullName ++ mr.possibleTypes))
.filterNot(_.equals("ANY"))
.toSet
Expand Down
Loading