Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basil 138 program generator #139

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ object Main {
)

def main(args: Array[String]): Unit = {

val parser = ParserForClass[Config]
val parsed = parser.constructEither(args.toSeq)

Expand Down
18 changes: 9 additions & 9 deletions src/main/scala/ir/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ class Interpreter() {
case BVSHL => smt_bvshl(left, right)
case BVLSHR => smt_bvlshr(left, right)
case BVULT => smt_bvult(left, right)
case BVNAND => ???
case BVNOR => ???
case BVXOR => ???
case BVXNOR => ???
case BVNAND => smt_bvnand(left, right)
case BVNOR => smt_bvnor(left, right)
case BVXOR => smt_bvxor(left, right)
case BVXNOR => smt_bvxnor(left, right)
case BVCOMP => smt_bvcomp(left, right)
case BVSUB => smt_bvsub(left, right)
case BVSDIV => smt_bvsdiv(left, right)
case BVSREM => smt_bvsrem(left, right)
case BVSMOD => ???
case BVSMOD => smt_bvsmod(left, right)
case BVASHR => smt_bvashr(left, right)
case BVULE => smt_bvule(left, right)
case BVUGT => ???
case BVUGE => ???
case BVUGT => smt_bvugt(left, right)
case BVUGE => smt_bvuge(left, right)
case BVSLT => smt_bvslt(left, right)
case BVSLE => smt_bvsle(left, right)
case BVSGT => ???
case BVSGE => ???
case BVSGT => smt_bvsgt(left, right)
case BVSGE => smt_bvsge(left, right)
case BVEQ => smt_bveq(left, right)
case BVNEQ => smt_bvneq(left, right)
case BVCONCAT => smt_concat(left, right)
Expand Down
48 changes: 48 additions & 0 deletions src/main/scala/util/ProgramGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import scala.collection.mutable.ArrayBuffer

object ProgramGenerator {

private val operatorList = Array("+", "-", "*", "/", "|", "^", "&")
def generateProgram(): String = {
"#include <stdio.h>\n" +
"void computeValue() {\n" +
generateBody() +
"\n}\n\n" +
"void main() {\n" +
"\tcomputeValue()\n" +
"}\n"


}
def generateBody(): String = {
var programBody = ""
val rand = new scala.util.Random
val variables = rand.between(1, 100)

for (countVars <- 1 to variables) {

val terms: ArrayBuffer[String] = ArrayBuffer()
for (generatedVar <- 1 until countVars) {
terms.addOne("variable" + generatedVar)
}

val numberOfConstants = rand.between(1, 100)
for (i <- 1 to numberOfConstants) {
terms.addOne(rand.between(1,500).toString)
}
val shuffledTerms = rand.shuffle(terms)
var generatedLine = "\tint variable" + countVars + " ="
for ((t, i) <- shuffledTerms.zipWithIndex) {
if (i != 0) {
generatedLine += operatorList(rand.nextInt(operatorList.length))
}
generatedLine += " " + t + " "
}

programBody += generatedLine + "\n"
}
programBody + "\n\tprintf(\"%d\", variable" + variables + ")"
}
}
11 changes: 9 additions & 2 deletions src/test/scala/ir/InterpreterTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.BeforeAndAfter
import specification.SpecGlobal
import translating.BAPToIR
import util.{LogLevel, Logger}
import util.{LogLevel, Logger, ProgramGenerator}
import util.RunUtils.{loadBAP, loadReadELF}

class InterpreterTests extends AnyFunSuite with BeforeAndAfter {
Expand All @@ -26,7 +26,9 @@ class InterpreterTests extends AnyFunSuite with BeforeAndAfter {

(IRProgram, globals)
}

def generateProgram(): String = {
ProgramGenerator.generateProgram()
}
def testInterpret(name: String, expected: Map[String, Int]): Unit = {
val (program, globals) = getProgram(name)
val regs = i.interpret(program)
Expand Down Expand Up @@ -58,6 +60,7 @@ class InterpreterTests extends AnyFunSuite with BeforeAndAfter {
i = Interpreter()
}


test("getMemory in LittleEndian") {
i.mems(0) = BitVecLiteral(BigInt("0D", 16), 8)
i.mems(1) = BitVecLiteral(BigInt("0C", 16), 8)
Expand Down Expand Up @@ -190,4 +193,8 @@ class InterpreterTests extends AnyFunSuite with BeforeAndAfter {
)
testInterpret("no_interference_update_y", expected)
}

test("Generate Test Program") {
assert(generateProgram().nonEmpty)
}
}