Skip to content

Commit 1fbb469

Browse files
authored
[ghidra2cpg] Simplify checking of external functions (#5449)
1 parent e67b8f8 commit 1fbb469

File tree

5 files changed

+10
-16
lines changed

5 files changed

+10
-16
lines changed

joern-cli/frontends/ghidra2cpg/src/main/scala/io/joern/ghidra2cpg/passes/PCodePass.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ class PCodePass(currentProgram: Program, fileName: String, functions: List[Funct
117117
override def runOnPart(diffGraphBuilder: DiffGraphBuilder, function: Function): Unit = {
118118
val localDiffGraph = Cpg.newDiffGraphBuilder
119119
// we need it just once with default settings
120-
val blockNode = nodes.NewBlock().code("").order(0)
121-
val methodNode = createMethodNode(decompiler, function, fileName, checkIfExternal(currentProgram, function.getName))
120+
val blockNode = nodes.NewBlock().code("").order(0)
121+
val methodNode = createMethodNode(decompiler, function, fileName)
122122
val methodReturn = createReturnNode()
123123

124124
localDiffGraph.addNode(methodNode)

joern-cli/frontends/ghidra2cpg/src/main/scala/io/joern/ghidra2cpg/passes/arm/ArmFunctionPass.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ghidra.program.model.listing.{Function, Program}
44
import io.joern.ghidra2cpg.utils.Decompiler
55
import io.joern.ghidra2cpg.passes.FunctionPass
66
import io.joern.ghidra2cpg.processors.ArmProcessor
7-
import io.joern.ghidra2cpg.utils.Utils.{checkIfExternal, createMethodNode, createReturnNode}
7+
import io.joern.ghidra2cpg.utils.Utils.{createMethodNode, createReturnNode}
88
import io.shiftleft.codepropertygraph.generated.nodes.NewBlock
99
import io.shiftleft.codepropertygraph.generated.{Cpg, EdgeTypes, nodes}
1010

@@ -22,7 +22,7 @@ class ArmFunctionPass(
2222
val blockNode: NewBlock = nodes.NewBlock().code("").order(0)
2323
try {
2424
val methodNode =
25-
createMethodNode(decompiler, function, filename, checkIfExternal(currentProgram, function.getName))
25+
createMethodNode(decompiler, function, filename)
2626
val methodReturn = createReturnNode()
2727
localDiffGraph.addNode(methodNode)
2828
localDiffGraph.addNode(blockNode)

joern-cli/frontends/ghidra2cpg/src/main/scala/io/joern/ghidra2cpg/passes/mips/MipsFunctionPass.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ class MipsFunctionPass(
261261
val localDiffGraph = Cpg.newDiffGraphBuilder
262262
// we need it just once with default settings
263263
val blockNode: NewBlock = nodes.NewBlock().code("").order(0)
264-
val methodNode = createMethodNode(decompiler, function, filename, checkIfExternal(currentProgram, function.getName))
265-
val methodReturn = createReturnNode()
264+
val methodNode = createMethodNode(decompiler, function, filename)
265+
val methodReturn = createReturnNode()
266266
localDiffGraph.addNode(methodNode)
267267
localDiffGraph.addNode(blockNode)
268268
localDiffGraph.addEdge(methodNode, blockNode, EdgeTypes.AST)

joern-cli/frontends/ghidra2cpg/src/main/scala/io/joern/ghidra2cpg/passes/x86/X86FunctionPass.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class X86FunctionPass(
5050
// we need it just once with default settings
5151
val blockNode: NewBlock = nodes.NewBlock().code("").order(0)
5252
val methodNode =
53-
createMethodNode(decompiler, function, filename, checkIfExternal(currentProgram, function.getName))
53+
createMethodNode(decompiler, function, filename)
5454
val localGraphBuilder = Cpg.newDiffGraphBuilder
5555
val methodReturn = createReturnNode()
5656
localGraphBuilder.addNode(methodNode)

joern-cli/frontends/ghidra2cpg/src/main/scala/io/joern/ghidra2cpg/utils/Utils.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ object Utils {
6464
def createReturnNode(): NewMethodReturn = {
6565
NewMethodReturn().order(1)
6666
}
67-
def createMethodNode(decompiler: Decompiler, function: Function, fileName: String, isExternal: Boolean): NewMethod = {
68-
val code = decompiler.toDecompiledFunction(function).map(_.getC).getOrElse("")
67+
def createMethodNode(decompiler: Decompiler, function: Function, fileName: String): NewMethod = {
68+
val code = decompiler.toDecompiledFunction(function).map(_.getC).getOrElse("")
69+
val isExternal = Option(function.getThunkedFunction(true)).map(_.isExternal).getOrElse(function.isExternal)
6970
val lineNumberEnd = Option(function.getReturn)
7071
.flatMap(x => Option(x.getMinAddress))
7172
.flatMap(x => Option(x.getOffsetAsBigInteger))
@@ -86,13 +87,6 @@ object Utils {
8687
.astParentType(NodeTypes.NAMESPACE_BLOCK)
8788
.astParentFullName(s"$fileName:<global>")
8889
}
89-
def checkIfExternal(currentProgram: Program, functionName: String): Boolean = {
90-
currentProgram.getFunctionManager.getExternalFunctions
91-
.iterator()
92-
.asScala
93-
.map(_.getName)
94-
.contains(functionName)
95-
}
9690
def getInstructions(program: Program, function: Function): Seq[Instruction] =
9791
program.getListing.getInstructions(function.getBody, true).iterator().asScala.toList
9892

0 commit comments

Comments
 (0)