https://codeql.github.com/docs/codeql-language-guides/navigating-the-call-graph/

两个重要的类

Callable
A Callable is something that can be invoked.
Call
A Call is something that invokes a Callable.

点击查看【processon】
代码示例

  1. class Super {
  2. int x;
  3. // callable
  4. public Super() {
  5. this(23); // call
  6. }
  7. // callable
  8. public Super(int x) {
  9. this.x = x;
  10. }
  11. // callable
  12. public int getX() {
  13. return x;
  14. }
  15. }
  16. class Sub extends Super {
  17. // callable
  18. public Sub(int x) {
  19. super(x+19); // call
  20. }
  21. // callable
  22. public int getX() {
  23. return x-19;
  24. }
  25. }
  26. class Client {
  27. // callable
  28. public static void main(String[] args) {
  29. Super s = new Sub(42); // call
  30. s.getX(); // call
  31. }
  32. }

成员谓词

Call类

getCallee谓词,Gets the target callable of this call.

  1. Callable Call::getCallee()

getCaller谓词,Gets the callable invoking this call.

  1. Callable Call::getCaller()

代码示例

  1. import java
  2. from Call sink
  3. select sink,sink.getCallee(),sink.getCaller()

结果匹配示例

sink sink.getCallee() sink.getCaller()
request.getHeader(“x-requested-with”) getHeader commence

image.png
在如上代码中sink是MethodAccess类型,commence是Method类型

Callable类

calls谓词,Holds if this callable calls target,如果这个Callable调用了target则返回True

  1. predicate Callable::calls(Callable target)

polyCalls谓词,如果这个Callable直接调用了m,或者间接调用(Callable调用的某个方法覆盖了m)了m则返回True

  1. predicate Callable::polyCalls(Callable m)

代码示例

  1. import java
  2. from Callable caller, Callable callee
  3. where caller.calls(callee)
  4. select caller, callee

匹配结果示例

image.png

查找未被调用的方法

寻找已经被定义,但是从未被调用过的方法