MCPcopy Create free account
hub / github.com/LFYSec/MScan / GrpcPlugin

Class GrpcPlugin

src/main/java/fdu/secsys/microservice/plugin/GrpcPlugin.java:32–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30
31
32public class GrpcPlugin implements Plugin {
33
34 Logger logger = LogManager.getLogger(GrpcPlugin.class);
35
36 Solver solver;
37
38 MultiMap<Var, Invoke> var2InvokeMap = Maps.newMultiMap();
39
40 MultiMap<Invoke, JMethod> invoke2calleeMap = Maps.newMultiMap();
41
42 @Override
43 public void setSolver(Solver solver) {
44 this.solver = solver;
45 }
46
47 @Override
48 public void onStart() {
49 JClass stubClass = World.get().getClassHierarchy().getClass("io.grpc.stub.AbstractStub");
50 JClass serviceClass = World.get().getClassHierarchy().getClass("io.grpc.BindableService");
51 MultiMap<String, JMethod> serviceMethod = Maps.newMultiMap();
52 World.get().getClassHierarchy().getAllSubclassesOf(serviceClass).stream().filter(Predicate.not(JClass::isAbstract)).filter(jClass -> Arrays.stream(Config.classpathKeywords).anyMatch(keyword -> jClass.getName().contains(keyword))).forEach(jClass -> {
53 jClass.getDeclaredMethods().stream().filter(jMethod -> jMethod.getParamCount() > 0).forEach(jMethod -> {
54 serviceMethod.put(jMethod.getName(), jMethod);
55 });
56 });
57 World.get().getClassHierarchy().applicationClasses().filter(jClass -> Arrays.stream(Config.classpathKeywords).anyMatch(keyword -> jClass.getName().contains(keyword))).forEach(jClass -> {
58 jClass.getDeclaredMethods().stream().filter(jMethod -> !jMethod.isAbstract()).forEach(jMethod -> jMethod.getIR().invokes(false).filter(invoke -> !invoke.isSpecial()).forEach(invoke -> {
59 JMethod callee = invoke.getMethodRef().resolveNullable();
60 if (callee != null && World.get().getClassHierarchy().isSubclass(stubClass, callee.getDeclaringClass()) && !callee.getDeclaringClass().getName().startsWith("io.grpc.") && !callee.getName().equals("build")) {
61 if (callee.getParamCount() > 0) {
62 Var var = InvokeUtils.getVar(invoke, 0);
63 if (var != null) {
64 var2InvokeMap.put(var, invoke);
65 if (serviceMethod.containsKey(callee.getName())) {
66 invoke2calleeMap.putAll(invoke, serviceMethod.get(callee.getName()));
67 }
68 } else {
69 logger.debug("var is none");
70 }
71 }
72 }
73 }));
74 });
75 }
76
77 @Override
78 public void onNewCSMethod(CSMethod csMethod) {
79 StmtVisitor<Void> visitor = new GrpcPlugin.Visitor(csMethod);
80 csMethod.getMethod().getIR().getStmts().forEach(stmt -> stmt.accept(visitor));
81 }
82
83 private class Visitor implements StmtVisitor<Void> {
84
85 private final CSMethod csMethod;
86
87 private final Context context;
88
89 private Visitor(CSMethod csMethod) {

Callers

nothing calls this directly

Calls 1

newMultiMapMethod · 0.95

Tested by

no test coverage detected