Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
StubRemovalPass (#212)
Browse files Browse the repository at this point in the history
* Stub removal pass

* Complete StubRemovalPass

* Include a comment

* Set CPG version
  • Loading branch information
fabsx00 authored Jul 23, 2020
1 parent e88bb9d commit 1af773d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ organization := "io.shiftleft"
scalaVersion := "2.13.1"
enablePlugins(GitVersioning)

val cpgVersion = "0.11.331"
val cpgVersion = "0.11.334"
val antlrVersion = "4.7.2"

libraryDependencies ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.shiftleft.fuzzyc2cpg.passes

import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.passes.{DiffGraph, ParallelCpgPass}
import io.shiftleft.semanticcpg.language._
import io.shiftleft.codepropertygraph.generated.nodes

/**
* A pass that ensures that for any method m for which a body exists,
* there are no more method stubs for corresponding declarations.
* */
class StubRemovalPass(cpg: Cpg) extends ParallelCpgPass[nodes.Method](cpg) {
override def partIterator: Iterator[nodes.Method] =
cpg.method.isNotStub.iterator

override def runOnPart(method: nodes.Method): Iterator[DiffGraph] = {
val diffGraph = DiffGraph.newBuilder
cpg.method.isStub.where(m => m.signature == method.signature).foreach { stubMethod =>
stubMethod.ast.l.foreach { node =>
diffGraph.removeNode(node.id2())
}
}
Iterator(diffGraph.build)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,8 @@ private[astcreation] class AstCreator(diffGraph: DiffGraph.Builder,
} else {
"int"
}
val signature = new StringBuilder()
.append(returnType)
.append("(")
.append(astFunction.getParameterList.getEscapedCodeStr(false))
.append(")")
.toString()

val signature = astFunction.getFunctionSignature(false)

val location = astFunction.getLocation
val method = nodes.NewMethod(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.shiftleft.fuzzyc2cpg.passes

import better.files.File
import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.passes.IntervalKeyPool
import org.scalatest.{Matchers, WordSpec}
import io.shiftleft.semanticcpg.language._

class StubRemovalPassTests extends WordSpec with Matchers {

"StubRemovalPass" should {
"remove stub if non-stub with same signature exists" in StubRemovalPassFixture("""
|int foo(int x);
|int foo(int x) {
| return 0;
|}
|""".stripMargin) { cpg =>
cpg.method.name.l shouldBe List("foo")
cpg.method.isStub.l shouldBe List()
cpg.parameter.name.l shouldBe List("x")
cpg.methodReturn.l.size shouldBe 1
}

"remove stub even if even parameter names differ" in StubRemovalPassFixture("""
|int foo(int another_name);
|int foo(int x) {
| return 0;
|}
|""".stripMargin) { cpg =>
cpg.method.name.l shouldBe List("foo")
cpg.method.isStub.l shouldBe List()
cpg.parameter.name.l shouldBe List("x")
cpg.methodReturn.l.size shouldBe 1
}

"keep multiple implementations" in StubRemovalPassFixture("""
|int foo(int x) { return x; }
|int foo(int x) {
| return 0;
|}
|""".stripMargin) { cpg =>
cpg.method.name.l shouldBe List("foo", "foo")
}

}

}

object StubRemovalPassFixture {
def apply(file1Code: String)(f: Cpg => Unit): Unit = {
File.usingTemporaryDirectory("fuzzyctest") { dir =>
val file1 = (dir / "file1.c")
file1.write(file1Code)
val cpg = Cpg.emptyCpg
val keyPool = new IntervalKeyPool(1001, 2000)
val filenames = List(file1.path.toAbsolutePath.toString)
new AstCreationPass(filenames, cpg, keyPool).createAndApply()
val cfgKeyPool = new IntervalKeyPool(2001, 3000)
new CfgCreationPass(cpg, cfgKeyPool).createAndApply()
new StubRemovalPass(cpg).createAndApply()
f(cpg)
}
}
}

0 comments on commit 1af773d

Please sign in to comment.