Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
l-kent committed Feb 5, 2024
1 parent d45fed6 commit 0ac0e7e
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 430 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Main {

@main(name = "BASIL")
case class Config(
@arg(name = "input", short = 'i', doc = "BAP .adt file or Gtirb/ASLi .gts file")
@arg(name = "input", short = 'i', doc = "BAP .adt file or GTIRB/ASLi .gts file")
inputFileName: String,
@arg(name = "relf", short = 'r', doc = "Name of the file containing the output of 'readelf -s -r -W'.")
relfFileName: String,
Expand Down
170 changes: 85 additions & 85 deletions src/main/scala/gtirb/MapDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gtirb
import java.io.FileInputStream
import com.google.protobuf.ByteString
import java.io.ByteArrayInputStream
import scala.collection.mutable.Set
import scala.collection.mutable.Map
import scala.collection.mutable
import java.nio.charset.StandardCharsets

Expand All @@ -15,98 +17,96 @@ import java.nio.charset.StandardCharsets
*/
object MapDecoder {

def decode_set(totalBytes: Seq[ByteString]): collection.mutable.Map[ByteString, collection.mutable.Set[ByteString]] = {

val totalMap: collection.mutable.Map[ByteString, collection.mutable.Set[ByteString]]
= collection.mutable.Map.empty[ByteString, collection.mutable.Set[ByteString]]

for (bytes <- totalBytes) {
val bytesArr: Array[Byte] = bytes.toByteArray()
val byteStream = new ByteArrayInputStream(bytesArr)

val map : collection.mutable.Map[ByteString, collection.mutable.Set[ByteString]]
= collection.mutable.Map.empty[ByteString, collection.mutable.Set[ByteString]]

val len = bytesToInt(read_bytes(8, byteStream), true)
val num = len.toInt
for (s <- 0 until num) {
val key = ByteString.copyFrom(read_bytes(16, byteStream))
val uuids: collection.mutable.Set[ByteString] = collection.mutable.Set[ByteString]();
val len = bytesToInt(read_bytes(8, byteStream), true);
for (k <- 0 until len.toInt) {
val byte = ByteString.copyFrom(read_bytes(16, byteStream))
uuids += byte
}

map += (key -> uuids);
}
totalMap ++= map
}
return totalMap

}
def decode_set(totalBytes: Seq[ByteString]): mutable.Map[ByteString, mutable.Set[ByteString]] = {

val totalMap: mutable.Map[ByteString, mutable.Set[ByteString]]
= mutable.Map.empty[ByteString, mutable.Set[ByteString]]

for (bytes <- totalBytes) {
val bytesArr: Array[Byte] = bytes.toByteArray
val byteStream = ByteArrayInputStream(bytesArr)

def decode_uuid(totalBytes: Seq[ByteString]): collection.mutable.Map[ByteString, ByteString] = {

val totalMap : collection.mutable.Map[ByteString, ByteString]
= collection.mutable.Map.empty[ByteString, ByteString]

for (bytes <- totalBytes) {
val bytesArr: Array[Byte] = bytes.toByteArray()
val byteStream = new ByteArrayInputStream(bytesArr)

val map : collection.mutable.Map[ByteString, ByteString]
= collection.mutable.Map.empty[ByteString, ByteString]

val len = bytesToInt(read_bytes(8, byteStream), true)
val num = len.toInt
for (s <- 0 until num) {
val key = ByteString.copyFrom(read_bytes(16, byteStream))
val uuid = ByteString.copyFrom(read_bytes(16, byteStream))
map += (key -> uuid);
}
totalMap ++= map
val map = mutable.Map.empty[ByteString, mutable.Set[ByteString]]

val len = bytesToInt(read_bytes(8, byteStream), true)
val num = len.toInt
for (s <- 0 until num) {
val key = ByteString.copyFrom(read_bytes(16, byteStream))
val uuids = mutable.Set[ByteString]();
val len = bytesToInt(read_bytes(8, byteStream), true);
for (k <- 0 until len.toInt) {
val byte = ByteString.copyFrom(read_bytes(16, byteStream))
uuids += byte
}
return totalMap
}

def decode_string(totalBytes: Seq[ByteString]): mutable.Map[ByteString, String] = {
// THIS DOESNT WORK YET
// literally can't figure out what's wrong, might be something to do with java/scala treating all bits as signed,
// when api is unsigned
val totalMap : mutable.Map[ByteString, String] = mutable.Map.empty[ByteString, String]

for (bytes <- totalBytes) {

val map : mutable.Map[ByteString, String] = mutable.Map.empty[ByteString, String]
val bytesArr: Array[Byte] = bytes.toByteArray()
val byteStream = new ByteArrayInputStream(bytesArr)

val len = bytesToInt(read_bytes(8, byteStream), true)
val num = len.toInt
for (s <- 0 until num) {
val key = ByteString.copyFrom(read_bytes(16, byteStream))
val len = bytesToInt(read_bytes(8, byteStream), true);
val str = new String(read_bytes(len.toInt, byteStream), StandardCharsets.UTF_8)
map += (key -> str);
}
totalMap ++= map
}
return totalMap
map += (key -> uuids)
}
totalMap ++= map
}
totalMap

}

def decode_uuid(totalBytes: Seq[ByteString]): mutable.Map[ByteString, ByteString] = {

val totalMap : mutable.Map[ByteString, ByteString]
= mutable.Map.empty[ByteString, ByteString]

def read_bytes(size: Int, byteStream: ByteArrayInputStream): Array[Byte] = {
byteStream.readNBytes(size)
for (bytes <- totalBytes) {
val bytesArr: Array[Byte] = bytes.toByteArray
val byteStream = ByteArrayInputStream(bytesArr)

val map = mutable.Map.empty[ByteString, ByteString]

val len = bytesToInt(read_bytes(8, byteStream), true)
val num = len.toInt
for (s <- 0 until num) {
val key = ByteString.copyFrom(read_bytes(16, byteStream))
val uuid = ByteString.copyFrom(read_bytes(16, byteStream))
map += (key -> uuid);
}
totalMap ++= map
}
totalMap
}

def decode_string(totalBytes: Seq[ByteString]): mutable.Map[ByteString, String] = {
// THIS DOESNT WORK YET
// literally can't figure out what's wrong, might be something to do with java/scala treating all bits as signed,
// when api is unsigned
val totalMap = mutable.Map.empty[ByteString, String]

for (bytes <- totalBytes) {

val map = mutable.Map.empty[ByteString, String]
val bytesArr: Array[Byte] = bytes.toByteArray
val byteStream = ByteArrayInputStream(bytesArr)

val len = bytesToInt(read_bytes(8, byteStream), true)
val num = len.toInt
for (s <- 0 until num) {
val key = ByteString.copyFrom(read_bytes(16, byteStream))
val len = bytesToInt(read_bytes(8, byteStream), true)
val str = String(read_bytes(len.toInt, byteStream), StandardCharsets.UTF_8)
map += (key -> str)
}
totalMap ++= map
}
totalMap
}

def bytesToInt(bytes: Array[Byte], littleEndian: Boolean): Long = {
val buffer = java.nio.ByteBuffer.wrap(bytes)
if (littleEndian) {
return buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN).getLong
} else {
return buffer.getLong
}

def read_bytes(size: Int, byteStream: ByteArrayInputStream): Array[Byte] = {
byteStream.readNBytes(size)
}

def bytesToInt(bytes: Array[Byte], littleEndian: Boolean): Long = {
val buffer = java.nio.ByteBuffer.wrap(bytes)
if (littleEndian) {
buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN).getLong
} else {
buffer.getLong
}

}

}
11 changes: 4 additions & 7 deletions src/main/scala/ir/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ case class IntLiteral(value: BigInt) extends Literal {
}

/**
* end: high bit exclusive
* start: low bit inclusive
* body: idfk
*
* @param end
* @param start
* @param end : high bit exclusive
* @param start : low bit inclusive
* @param body
*/
case class Extract(var end: Int, var start: Int, var body: Expr) extends Expr {
case class Extract(end: Int, start: Int, body: Expr) extends Expr {
override def toBoogie: BExpr = BVExtract(end, start, body.toBoogie)
override def gammas: Set[Expr] = body.gammas
override def variables: Set[Variable] = body.variables
Expand Down Expand Up @@ -377,6 +373,7 @@ case class Register(override val name: String, override val irType: IRType) exte
override def toBoogie: BVar = BVariable(s"$name", irType.toBoogie, Scope.Global)
override def toString: String = s"Register($name, $irType)"
override def acceptVisit(visitor: Visitor): Variable = visitor.visitRegister(this)
override def size: Int = irType.asInstanceOf[BitVecType].size
}

case class LocalVar(override val name: String, override val irType: IRType) extends Variable {
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/ir/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ class Interpreter() {
evalRight match {
case BitVecLiteral(value, size) =>
Logger.debug(s"MemoryAssign ${assign.lhs} := 0x${value.toString(16)}[u$size]\n")
case _ => throw new Exception("cannot register non-bitvectors")
}
case _ : NOP => ()
case _ : NOP =>
case assert: Assert =>
Logger.debug(assert)
// TODO
Expand Down
Loading

0 comments on commit 0ac0e7e

Please sign in to comment.