Skip to content

Commit

Permalink
Changes to consider local variables and initial offsets of a procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
yousifpatti committed Aug 19, 2024
1 parent 5a20e37 commit f798374
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
29 changes: 24 additions & 5 deletions src/main/scala/analysis/MemoryRegionAnalysis.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package analysis

import analysis.BitVectorEval.isNegative
import analysis.solvers.WorklistFixpointSolverWithReachability
import ir.*
import util.Logger
Expand Down Expand Up @@ -94,7 +95,7 @@ trait MemoryRegionAnalysis(val program: Program,
def reducibleToRegion(binExpr: BinaryExpr, n: Command): Set[MemoryRegion] = {
var reducedRegions = Set.empty[MemoryRegion]
binExpr.arg1 match {
case variable: Variable =>
case variable: Variable if !spList.contains(variable) =>
val ctx = getUse(variable, n, reachingDefs)
for (i <- ctx) {
val regions = i.rhs match {
Expand All @@ -121,10 +122,25 @@ trait MemoryRegionAnalysis(val program: Program,
}
}
case _ =>
eval(binExpr, Set.empty, n)
}
reducedRegions
}

def reducibleVariable(variable: Variable, n: Command): Set[MemoryRegion] = {
var regions = Set.empty[MemoryRegion]
val ctx = getDefinition(variable, n, reachingDefs)
for (i <- ctx) {
i.rhs match {
case binaryExpr: BinaryExpr =>
regions = regions ++ reducibleToRegion(binaryExpr, i)
case _ =>
//regions = regions ++ eval(i.rhs, Set.empty, i)
}
}
regions
}

def eval(exp: Expr, env: Set[MemoryRegion], n: Command): Set[MemoryRegion] = {
Logger.debug(s"evaluating $exp")
Logger.debug(s"env: $env")
Expand All @@ -133,7 +149,12 @@ trait MemoryRegionAnalysis(val program: Program,
case binOp: BinaryExpr =>
if (spList.contains(binOp.arg1)) {
evaluateExpression(binOp.arg2, constantProp(n)) match {
case Some(b: BitVecLiteral) => Set(poolMaster(b, IRWalk.procedure(n)))
case Some(b: BitVecLiteral) =>
if (isNegative(b)) {
Set(poolMaster(BitVecLiteral(0, 64), IRWalk.procedure(n)))
} else {
Set(poolMaster(b, IRWalk.procedure(n)))
}
case None => env
}
} else if (reducibleToRegion(binOp, n).nonEmpty) {
Expand All @@ -146,16 +167,14 @@ trait MemoryRegionAnalysis(val program: Program,
}
case variable: Variable =>
variable match {
case _: LocalVar =>
env
case reg: Register if spList.contains(reg) =>
eval(BitVecLiteral(0, 64), env, n)
case _ =>
evaluateExpression(variable, constantProp(n)) match {
case Some(b: BitVecLiteral) =>
eval(b, env, n)
case _ =>
env // we cannot evaluate this to a concrete value, we need VSA for this
reducibleVariable(variable, n)
}
}
case memoryLoad: MemoryLoad =>
Expand Down
27 changes: 22 additions & 5 deletions src/main/scala/analysis/RegionInjector.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package analysis

import analysis.BitVectorEval.isNegative
import ir.*
import util.Logger

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

Expand Down Expand Up @@ -156,19 +158,22 @@ class RegionInjector(domain: mutable.Set[CFGPosition],
Logger.debug("found: " + regions)
res ++= regions
} else {
if (isNegative(b)) {
val region = mmm.findStackObject(0)
if (region.isDefined) {
res = res + region.get
}
}
val region = mmm.findStackObject(b.value)
if (region.isDefined) {
res = res + region.get
}
}
}
res
case binaryExpr: BinaryExpr =>
res ++= reducibleToRegion(binaryExpr, n)
res
case v: Variable if v == stackPointer =>
res ++= mmm.findStackObject(0)
res
case v: Variable =>
evaluateExpressionWithSSA(expr, constantProp(n), n, reachingDefs).foreach { b =>
Logger.debug("BitVecLiteral: " + b)
Expand All @@ -177,6 +182,17 @@ class RegionInjector(domain: mutable.Set[CFGPosition],
res += region.get
}
}
if (res.isEmpty) {
val ctx = getDefinition(v, n, reachingDefs)
for (i <- ctx) {
i.rhs match {
case be: BinaryExpr =>
res = res ++ exprToRegion(eval(i.rhs, i), n)
case _ =>
}
}
}

if (res.isEmpty) { // may be passed as param
val ctx = getUse(v, n, reachingDefs)
for (i <- ctx) {
Expand All @@ -190,7 +206,8 @@ class RegionInjector(domain: mutable.Set[CFGPosition],
}
}
}
res
case load: MemoryLoad => // treat as a region
res ++= exprToRegion(load.index, n)
case _ =>
evaluateExpressionWithSSA(expr, constantProp(n), n, reachingDefs).foreach { b =>
Logger.debug("BitVecLiteral: " + b)
Expand All @@ -199,8 +216,8 @@ class RegionInjector(domain: mutable.Set[CFGPosition],
res += region.get
}
}
res
}
res
}

/** Default implementation of eval.
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/util/RunUtils.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package util

import java.io.{File, PrintWriter, FileInputStream, BufferedWriter, FileWriter, IOException}
import java.io.{BufferedWriter, File, FileInputStream, FileWriter, IOException, PrintWriter}
import com.grammatech.gtirb.proto.IR.IR
import com.grammatech.gtirb.proto.Module.Module
import com.grammatech.gtirb.proto.Section.Section
import spray.json.*
import gtirb.*

import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import java.io.{File, PrintWriter}
Expand All @@ -25,13 +26,15 @@ import org.antlr.v4.runtime.BailErrorStrategy
import org.antlr.v4.runtime.{CharStreams, CommonTokenStream, Token}
import translating.*
import util.Logger

import java.util.Base64
import spray.json.DefaultJsonProtocol.*
import util.intrusive_list.IntrusiveList
import analysis.CfgCommandNode

import scala.annotation.tailrec
import scala.collection.mutable
import scala.sys.exit

/** This file contains the main program execution. See RunUtils.loadAndTranslate for the high-level process.
*/
Expand Down

0 comments on commit f798374

Please sign in to comment.