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

Commit 1af773d

Browse files
authored
StubRemovalPass (#212)
* Stub removal pass * Complete StubRemovalPass * Include a comment * Set CPG version
1 parent e88bb9d commit 1af773d

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ organization := "io.shiftleft"
33
scalaVersion := "2.13.1"
44
enablePlugins(GitVersioning)
55

6-
val cpgVersion = "0.11.331"
6+
val cpgVersion = "0.11.334"
77
val antlrVersion = "4.7.2"
88

99
libraryDependencies ++= Seq(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.shiftleft.fuzzyc2cpg.passes
2+
3+
import io.shiftleft.codepropertygraph.Cpg
4+
import io.shiftleft.passes.{DiffGraph, ParallelCpgPass}
5+
import io.shiftleft.semanticcpg.language._
6+
import io.shiftleft.codepropertygraph.generated.nodes
7+
8+
/**
9+
* A pass that ensures that for any method m for which a body exists,
10+
* there are no more method stubs for corresponding declarations.
11+
* */
12+
class StubRemovalPass(cpg: Cpg) extends ParallelCpgPass[nodes.Method](cpg) {
13+
override def partIterator: Iterator[nodes.Method] =
14+
cpg.method.isNotStub.iterator
15+
16+
override def runOnPart(method: nodes.Method): Iterator[DiffGraph] = {
17+
val diffGraph = DiffGraph.newBuilder
18+
cpg.method.isStub.where(m => m.signature == method.signature).foreach { stubMethod =>
19+
stubMethod.ast.l.foreach { node =>
20+
diffGraph.removeNode(node.id2())
21+
}
22+
}
23+
Iterator(diffGraph.build)
24+
}
25+
}

src/main/scala/io/shiftleft/fuzzyc2cpg/passes/astcreation/AstCreator.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,8 @@ private[astcreation] class AstCreator(diffGraph: DiffGraph.Builder,
103103
} else {
104104
"int"
105105
}
106-
val signature = new StringBuilder()
107-
.append(returnType)
108-
.append("(")
109-
.append(astFunction.getParameterList.getEscapedCodeStr(false))
110-
.append(")")
111-
.toString()
106+
107+
val signature = astFunction.getFunctionSignature(false)
112108

113109
val location = astFunction.getLocation
114110
val method = nodes.NewMethod(
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.shiftleft.fuzzyc2cpg.passes
2+
3+
import better.files.File
4+
import io.shiftleft.codepropertygraph.Cpg
5+
import io.shiftleft.passes.IntervalKeyPool
6+
import org.scalatest.{Matchers, WordSpec}
7+
import io.shiftleft.semanticcpg.language._
8+
9+
class StubRemovalPassTests extends WordSpec with Matchers {
10+
11+
"StubRemovalPass" should {
12+
"remove stub if non-stub with same signature exists" in StubRemovalPassFixture("""
13+
|int foo(int x);
14+
|int foo(int x) {
15+
| return 0;
16+
|}
17+
|""".stripMargin) { cpg =>
18+
cpg.method.name.l shouldBe List("foo")
19+
cpg.method.isStub.l shouldBe List()
20+
cpg.parameter.name.l shouldBe List("x")
21+
cpg.methodReturn.l.size shouldBe 1
22+
}
23+
24+
"remove stub even if even parameter names differ" in StubRemovalPassFixture("""
25+
|int foo(int another_name);
26+
|int foo(int x) {
27+
| return 0;
28+
|}
29+
|""".stripMargin) { cpg =>
30+
cpg.method.name.l shouldBe List("foo")
31+
cpg.method.isStub.l shouldBe List()
32+
cpg.parameter.name.l shouldBe List("x")
33+
cpg.methodReturn.l.size shouldBe 1
34+
}
35+
36+
"keep multiple implementations" in StubRemovalPassFixture("""
37+
|int foo(int x) { return x; }
38+
|int foo(int x) {
39+
| return 0;
40+
|}
41+
|""".stripMargin) { cpg =>
42+
cpg.method.name.l shouldBe List("foo", "foo")
43+
}
44+
45+
}
46+
47+
}
48+
49+
object StubRemovalPassFixture {
50+
def apply(file1Code: String)(f: Cpg => Unit): Unit = {
51+
File.usingTemporaryDirectory("fuzzyctest") { dir =>
52+
val file1 = (dir / "file1.c")
53+
file1.write(file1Code)
54+
val cpg = Cpg.emptyCpg
55+
val keyPool = new IntervalKeyPool(1001, 2000)
56+
val filenames = List(file1.path.toAbsolutePath.toString)
57+
new AstCreationPass(filenames, cpg, keyPool).createAndApply()
58+
val cfgKeyPool = new IntervalKeyPool(2001, 3000)
59+
new CfgCreationPass(cpg, cfgKeyPool).createAndApply()
60+
new StubRemovalPass(cpg).createAndApply()
61+
f(cpg)
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)