Skip to content

Commit

Permalink
BASIL-19: Fixed issue with external functios
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggyfish committed Oct 5, 2023
1 parent 4ff3b01 commit 3109458
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/main/scala/analysis/NonReturningFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package analysis

import bap.{BAPJump, BAPSubroutine}
import ir.{Block, DirectCall, GoTo, IndirectCall, Jump, Procedure, Statement}
import specification.ExternalFunction

import scala.collection.mutable.{Map, Queue}
import collection.parallel.CollectionConverters.seqIsParallelizable
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.collection.parallel.CollectionConverters.*

Expand All @@ -15,6 +17,8 @@ class NonReturningFunctions {
val mapJumpsToBlocks: Map[String, ArrayBuffer[(Jump, Block)]] = Map()
val mapBlocksToProcedure: Map[String, (Procedure, Integer)] = Map()



def isEndlessLoop(proc: Procedure, goTo: GoTo, index: Integer): Boolean = {

if (goTo.condition.isEmpty && mapBlocksToProcedure.contains(goTo.target.label) && mapBlocksToProcedure(goTo.target.label)._2 < index) {
Expand Down Expand Up @@ -45,6 +49,7 @@ class NonReturningFunctions {
for (proc <- procedures) {
var numberOfReturns = 0
for ((block, index) <- proc.blocks.zipWithIndex) {

mapBlocksToProcedure.addOne(block.label, (proc, index))
for (jump <- block.jumps) {

Expand All @@ -59,7 +64,7 @@ class NonReturningFunctions {
case goTo: GoTo =>
mapJumpsToBlocks.put(goTo.target.label, mapJumpsToBlocks.getOrElse(goTo.target.label, ArrayBuffer()).addOne((goTo, block)))
if (proc.blocks.length > index && isEndlessLoop(proc, goTo, index)) {
blocksToRemove.enqueue(proc.blocks(index+1).label)
blocksToRemove.enqueue(proc.blocks(index + 1).label)
}
case _ =>
}
Expand All @@ -72,8 +77,7 @@ class NonReturningFunctions {
while (blocksDeleted) {
blocksDeleted = false
for (proc <- procedures) {

if (proc.calculateReturnCount() == 0) {
if (!proc.externalFunction && proc.calculateReturnCount() == 0) {
mapJumpsToBlocks.get(proc.name) match {
case Some(v) => for (block <- v) {
val (_, containingBlock) = block
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/ir/Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class Procedure(
var address: Option[Int],
var blocks: ArrayBuffer[Block],
var in: ArrayBuffer[Parameter],
var out: ArrayBuffer[Parameter]
var out: ArrayBuffer[Parameter],
val externalFunction: Boolean
) {

def calls: Set[Procedure] = blocks.flatMap(_.calls).toSet
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/translating/BAPToIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import scala.collection.mutable
import scala.collection.mutable.Map
import scala.collection.mutable.ArrayBuffer

class BAPToIR(var program: BAPProgram, mainAddress: Int) {
class BAPToIR(var program: BAPProgram, mainAddress: Int, externalFunctions: Set[ExternalFunction]) {

private val nameToProcedure: mutable.Map[String, Procedure] = mutable.Map()
private val labelToBlock: mutable.Map[String, Block] = mutable.Map()

def translate: Program = {
var mainProcedure: Option[Procedure] = None
val procedures: ArrayBuffer[Procedure] = ArrayBuffer()
val externalFunctionNames = externalFunctions.map(func => func.name)
for (s <- program.subroutines) {
val blocks: ArrayBuffer[Block] = ArrayBuffer()
for (b <- s.blocks) {
Expand All @@ -32,7 +33,7 @@ class BAPToIR(var program: BAPProgram, mainAddress: Int) {
for (p <- s.out) {
out.append(p.toIR)
}
val procedure = Procedure(s.name, Some(s.address), blocks, in, out)
val procedure = Procedure(s.name, Some(s.address), blocks, in, out, externalFunctionNames.contains(s.name))
if (s.address == mainAddress) {
mainProcedure = Some(procedure)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/util/RunUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ object RunUtils {

val (externalFunctions, globals, globalOffsets, mainAddress) = loadReadELF(readELFFileName)

val IRTranslator = BAPToIR(bapProgram, mainAddress)
val IRTranslator = BAPToIR(bapProgram, mainAddress, externalFunctions)
var IRProgram = IRTranslator.translate
NonReturningFunctions().transform(IRProgram.procedures)
NonReturningFunctions().transform(IRProgram.procedures, externalFunctions)

val specification = loadSpecification(specFileName, IRProgram, globals)

Expand Down Expand Up @@ -316,7 +316,7 @@ object RunUtils {
}

def addFakeProcedure(name: String): Unit = {
IRProgram.procedures += Procedure(name, None, ArrayBuffer(), ArrayBuffer(), ArrayBuffer())
IRProgram.procedures += Procedure(name, None, ArrayBuffer(), ArrayBuffer(), ArrayBuffer(), true)
}

def resolveAddresses(valueSet: Set[Value]): Set[AddressValue] = {
Expand Down

0 comments on commit 3109458

Please sign in to comment.