This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Stub removal pass * Complete StubRemovalPass * Include a comment * Set CPG version
- Loading branch information
Showing
4 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/scala/io/shiftleft/fuzzyc2cpg/passes/StubRemovalPass.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/scala/io/shiftleft/fuzzyc2cpg/passes/StubRemovalPassTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |