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

CONTAINS edges incomplete #1082

Open
m1cm1c opened this issue Jan 7, 2021 · 1 comment
Open

CONTAINS edges incomplete #1082

m1cm1c opened this issue Jan 7, 2021 · 1 comment

Comments

@m1cm1c
Copy link
Contributor

m1cm1c commented Jan 7, 2021

I am very confused about CONTAINS edges. I did not find any documentation on them. However, I did find a file called EdgeTypes.java in this repo which contains this:

 /** Shortcut over multiple AST edges */
 public static final String CONTAINS = "CONTAINS";

Given that that's all I know about these edges, I'd assume that ... well ... CONTAINS edges are the transitive closure of AST edges. I'm assuming that the transitive closure is meant by "shortcut". However, this is not the case in the implementation.

Take the graph that is created for this code:

void calculateWinner() {
  int hash = getHashOfBlock();
  if(hash < 27) {
	int receiver = 0;
	send(15);
  }
}

If we count the number of nodes connected to the method node via CONTAINS edges, we get 14:

joern> cpg.method.name("calculateWinner").outE(EdgeTypes.CONTAINS).inV.l.size 
res124: Int = 14

Now lets tally up the nodes in the transitive closure of AST edges:

joern> cpg.method.name("calculateWinner").astChildren.l.size 
res125: Int = 2

joern> cpg.method.name("calculateWinner").astChildren.astChildren.l.size 
res126: Int = 3

joern> cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.l.size 
res127: Int = 4

joern> 
cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.astChildren.l.size 
res128: Int = 5

joern> 
cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.astChildren.astChildren.l.size 
res129: Int = 3

joern> 
cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.astChildren.astChildren.astChildren.l.size 
res130: Int = 0

They sum up to 17, not 14.

This is not because of duplicate nodes (not that that would make sense in an abstract syntax tree) because after deduplication, we're still at 17:

joern> 
Traversal(cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.astChildren.astChildren.l.concat(cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.astChildren.l).concat(cpg.method.name("calculateWinner").astChildren.astChildren.astChildren.l).concat(cpg.method.name("calculateWinner").astChildren.astChildren.l).concat(cpg.method.name("calculateWinner").astChildren.l)).dedup.size 
res139: Int = 17

I have no idea how to best diff traversals in Joern, so I diffed them externally. In this particular case, the METHOD_RETURN node and the LOCAL nodes are missing. But I have no idea what nodes are missing in general.

@bbrehm
Copy link
Contributor

bbrehm commented Jul 19, 2021

In general you can look up the code in ContainsEdgePass.scala.

I agree that this behavior is weird -- one would naively expect this to be set for all AST nodes. It's not a bug, though -- other components know about this quirk and work with it.

Do we want to change this?

To me it looks like this behavior is just due to the coding patterns we used in 2018, and not consciously intended. But maybe there is an actual reason?

cc @mpollmeier @ml86 , because the furthest back I could track this behavior is #71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants