Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sadrabt committed Nov 25, 2024
1 parent 1f89d21 commit 3baa6e3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 82 deletions.
49 changes: 23 additions & 26 deletions src/main/scala/analysis/data_structure_analysis/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,30 +192,27 @@ class Graph(val proc: Procedure,
// creates the globals from the symbol tables
val globalMapping = mutable.Map[AddressRange, Field]()
globals.foreach {
global =>
global match
case FuncEntry(name, size, address) =>
val func = Node(Some(this), size)
func.allocationRegions.add(Function(name))
func.flags.global = true
func.flags.incomplete = true
func.flags.function = true
// globalMapping.update(AddressRange(address, address + (size / 8)), Field(func, 0))

val pointer = Node(Some(this), size)
pointer.allocationRegions.add(DataLocation(name, address, size / 8)) // todo check that size 0 is correct
pointer.flags.global = true
pointer.flags.incomplete = true
pointer.cells(0).pointee = Some(Slice(func.cells(0), 0))
globalMapping.update(AddressRange(address, address + (size / 8)), Field(pointer, 0))
case SpecGlobal(name, size, arraySize, address) =>
val node = Node(Some(this), size)
node.allocationRegions.add(DataLocation(name, address, size / 8))
node.flags.global = true
node.flags.incomplete = true
globalMapping.update(AddressRange(address, address + size / 8), Field(node, 0))
case _ => ???

case FuncEntry(name, size, address) =>
val func = Node(Some(this), size)
func.allocationRegions.add(Function(name))
func.flags.global = true
func.flags.incomplete = true
func.flags.function = true
// globalMapping.update(AddressRange(address, address + (size / 8)), Field(func, 0))

val pointer = Node(Some(this), size)
pointer.allocationRegions.add(DataPointer(name, address, size / 8)) // todo check that size 0 is correct
pointer.flags.global = true
pointer.flags.incomplete = true
pointer.cells(0).pointee = Some(Slice(func.cells(0), 0))
globalMapping.update(AddressRange(address, address + (size / 8)), Field(pointer, 0))
case SpecGlobal(name, size, arraySize, address) =>
val node = Node(Some(this), size)
node.allocationRegions.add(DataPointer(name, address, size / 8))
node.flags.global = true
node.flags.incomplete = true
globalMapping.update(AddressRange(address, address + size / 8), Field(node, 0))
case _ => ???
}

// creates a global for each relocation entry in the symbol table
Expand All @@ -238,7 +235,7 @@ class Graph(val proc: Procedure,

case None =>
val node = Node(Some(this))
node.allocationRegions.add(DataLocation(s"Relocated_$relocatedAddress", relocatedAddress, 8))
node.allocationRegions.add(DataPointer(s"Relocated_$relocatedAddress", relocatedAddress, 8))
node.flags.global = true
node.flags.incomplete = true
globalMapping.update(AddressRange(relocatedAddress, relocatedAddress + 8), Field(node, 0))
Expand All @@ -252,7 +249,7 @@ class Graph(val proc: Procedure,

externalFunctions.foreach { external =>
val node = Node(Some(this))
node.allocationRegions.add(DataLocation(external.name, external.offset, 0))
node.allocationRegions.add(DataPointer(external.name, external.offset, 0))
node.flags.global = true
node.flags.incomplete = true
globalMapping.update(AddressRange(external.offset, external.offset), Field(node, 0))
Expand Down
14 changes: 11 additions & 3 deletions src/main/scala/analysis/data_structure_analysis/LocalPhase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,27 @@ class LocalPhase(proc: Procedure,
}



/**
* Performs overlapping access to the pointer cell while preserving size each dereferenced cell as separate
* (not collapsing them together)
* @param lhsOrValue either lhs in a load or value in a store
* @param pointer the cell which is being dereferenced
* @param size size of the dereference
*/
def multiAccess(lhsOrValue: Cell, pointer: Cell, size: Int): Unit = {
// TODO there should be another check here to see we cover the bytesize
// otherwise can fall back on expanding

val diff = pointer.offset - lhsOrValue.offset
val startPointerOffset = pointer.offset
val startPointerOffset = pointer.offset // starting offset for the dereference
val lhsNode = lhsOrValue.node.get


// collect all cells that are being dereferenced
val pointers = pointer.node.get.cells.filter((offset, _) => offset >= startPointerOffset && offset < startPointerOffset + size).toSeq.sortBy((offset, cell) => offset)
for (((offset, cell), i) <- pointers.zipWithIndex) {
for (((offset, cell), i) <- pointers.zipWithIndex) { // iterate and merge each pointee at correct offset it lhs/value cell
val lhs = lhsNode.addCell(offset - diff, 0) // todo check if 0 is the right size
// update the access size of the pointer
graph.find(cell).growSize(if i < pointers.size - 1 then (pointers(i + 1)._1 - offset - diff).toInt else (size - (offset - diff)).toInt)
val res = graph.mergeCells(lhs, graph.adjust(graph.find(cell).getPointee))
graph.handleOverlapping(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case class HeapLocation(override val regionIdentifier: String, proc: Procedure,
override def toString: String = s"Heap($regionIdentifier, $size)"
}

case class DataLocation(override val regionIdentifier: String, start: BigInt, size: BigInt) extends MemoryLocation {
case class DataPointer(override val regionIdentifier: String, start: BigInt, size: BigInt) extends MemoryLocation {
override def toString: String = s"Data($regionIdentifier, $start, $size)"
}

Expand Down
57 changes: 5 additions & 52 deletions src/test/scala/DataStructureAnalysisTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,8 @@ class DataStructureAnalysisTest extends AnyFunSuite {
}

test("overlapping access") {
val results = RunUtils.loadAndTranslate(
BASILConfig(
loading = ILLoadingConfig(
inputFile = "src/test/indirect_calls/jumptable/clang/jumptable.adt",
relfFile = "src/test/indirect_calls/jumptable/clang/jumptable.relf",
specFile = None,
dumpIL = None,
),
staticAnalysis = Some(StaticAnalysisConfig()),
boogieTranslation = BoogieGeneratorConfig(),
outputPrefix = "boogie_out",
)
)
val results = runTest("src/test/indirect_calls/jumptable/clang/jumptable")


// the dsg of the main procedure after the local phase
val program = results.ir.program
Expand Down Expand Up @@ -100,19 +89,7 @@ class DataStructureAnalysisTest extends AnyFunSuite {


test("stack interproc overlapping") {
val results = RunUtils.loadAndTranslate(
BASILConfig(
loading = ILLoadingConfig(
inputFile = "src/test/dsa/stack_interproc_overlapping/stack_interproc_overlapping.adt",
relfFile = "src/test/dsa/stack_interproc_overlapping/stack_interproc_overlapping.relf",
specFile = None,
dumpIL = None,
),
staticAnalysis = Some(StaticAnalysisConfig()),
boogieTranslation = BoogieGeneratorConfig(),
outputPrefix = "boogie_out",
)
)
val results = runTest("src/test/dsa/stack_interproc_overlapping/stack_interproc_overlapping")

// the dsg of the main procedure after the all phases
val program = results.ir.program
Expand Down Expand Up @@ -155,19 +132,7 @@ class DataStructureAnalysisTest extends AnyFunSuite {
}

test("global interproc overlapping") {
val results = RunUtils.loadAndTranslate(
BASILConfig(
loading = ILLoadingConfig(
inputFile = "src/test/dsa/global_interproc_overlapping/global_interproc_overlapping.adt",
relfFile = "src/test/dsa/global_interproc_overlapping/global_interproc_overlapping.relf",
specFile = None,
dumpIL = None,
),
staticAnalysis = Some(StaticAnalysisConfig()),
boogieTranslation = BoogieGeneratorConfig(),
outputPrefix = "boogie_out",
)
)
val results = runTest("src/test/dsa/global_interproc_overlapping/global_interproc_overlapping")

// the dsg of the main procedure after the local phase
val program = results.ir.program
Expand All @@ -190,19 +155,7 @@ class DataStructureAnalysisTest extends AnyFunSuite {


test("indirect overlapping") {
val results = RunUtils.loadAndTranslate(
BASILConfig(
loading = ILLoadingConfig(
inputFile = "src/test/dsa/indirect_overlapping/indirect_overlapping.adt",
relfFile = "src/test/dsa/indirect_overlapping/indirect_overlapping.relf",
specFile = None,
dumpIL = None,
),
staticAnalysis = Some(StaticAnalysisConfig()),
boogieTranslation = BoogieGeneratorConfig(),
outputPrefix = "boogie_out",
)
)
val results = runTest("src/test/dsa/indirect_overlapping/indirect_overlapping")

val program = results.ir.program
val dsg = results.analysis.get.localDSA(program.mainProcedure)
Expand Down

0 comments on commit 3baa6e3

Please sign in to comment.