|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.amazon.lambda.snapstart; |
| 5 | + |
| 6 | +import edu.umd.cs.findbugs.BugAccumulator; |
| 7 | +import edu.umd.cs.findbugs.BugReporter; |
| 8 | +import edu.umd.cs.findbugs.Detector; |
| 9 | +import edu.umd.cs.findbugs.ba.AnalysisContext; |
| 10 | +import edu.umd.cs.findbugs.ba.CFGBuilderException; |
| 11 | +import edu.umd.cs.findbugs.ba.ClassContext; |
| 12 | +import edu.umd.cs.findbugs.ba.DataflowAnalysisException; |
| 13 | +import edu.umd.cs.findbugs.ba.Location; |
| 14 | +import edu.umd.cs.findbugs.ba.MissingClassException; |
| 15 | +import edu.umd.cs.findbugs.ba.SignatureConverter; |
| 16 | +import edu.umd.cs.findbugs.ba.ca.Call; |
| 17 | +import edu.umd.cs.findbugs.ba.ca.CallList; |
| 18 | +import edu.umd.cs.findbugs.ba.ca.CallListDataflow; |
| 19 | +import edu.umd.cs.findbugs.classfile.CheckedAnalysisException; |
| 20 | +import edu.umd.cs.findbugs.classfile.DescriptorFactory; |
| 21 | +import edu.umd.cs.findbugs.classfile.Global; |
| 22 | +import edu.umd.cs.findbugs.classfile.MethodDescriptor; |
| 23 | +import edu.umd.cs.findbugs.log.Profiler; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import org.apache.bcel.Const; |
| 29 | +import org.apache.bcel.classfile.Method; |
| 30 | +import org.apache.bcel.generic.ConstantPoolGen; |
| 31 | +import org.apache.bcel.generic.Instruction; |
| 32 | +import org.apache.bcel.generic.InvokeInstruction; |
| 33 | +import org.apache.bcel.generic.ReturnInstruction; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class is a detector implementation which runs in the first pass of analysis. With a very simplistic assumption |
| 37 | + * this identifies whether a method might return a pseudo-random value purely based on the fact that it calls a method |
| 38 | + * that's already known to return a pseudo-random value. A better approach is doing proper dataflow analysis to |
| 39 | + * understand whether the pseudo-random value makes it to the return instruction. |
| 40 | + */ |
| 41 | +public class BuildRandomReturningMethodsDatabase implements Detector { |
| 42 | + |
| 43 | + private final BugReporter bugReporter; |
| 44 | + private final BugAccumulator bugAccumulator; |
| 45 | + private final ReturnValueRandomnessPropertyDatabase database; |
| 46 | + private final ByteCodeIntrospector introspector; |
| 47 | + |
| 48 | + // Transient state |
| 49 | + private ClassContext classContext; |
| 50 | + private Method method; |
| 51 | + private MethodDescriptor methodDescriptor; |
| 52 | + private CallListDataflow callListDataflow; |
| 53 | + private Map<Call, MethodDescriptor> callMethodDescriptorMap; |
| 54 | + private CallGraph callGraph; |
| 55 | + |
| 56 | + public BuildRandomReturningMethodsDatabase(BugReporter reporter) { |
| 57 | + this.bugReporter = reporter; |
| 58 | + this.bugAccumulator = new BugAccumulator(reporter); |
| 59 | + database = new ReturnValueRandomnessPropertyDatabase(); |
| 60 | + Global.getAnalysisCache().eagerlyPutDatabase(ReturnValueRandomnessPropertyDatabase.class, database); |
| 61 | + callGraph = new CallGraph(); |
| 62 | + callMethodDescriptorMap = new HashMap<>(); |
| 63 | + introspector = new ByteCodeIntrospector(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void visitClassContext(ClassContext classContext) { |
| 68 | + this.classContext = classContext; |
| 69 | + |
| 70 | + String currentMethod = null; |
| 71 | + List<Method> methodsInCallOrder = classContext.getMethodsInCallOrder(); |
| 72 | + for (Method method : methodsInCallOrder) { |
| 73 | + try { |
| 74 | + if (method.isAbstract() || method.isNative() || method.getCode() == null) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + currentMethod = SignatureConverter.convertMethodSignature(classContext.getJavaClass(), method); |
| 78 | + analyzeMethod(method); |
| 79 | + } catch (MissingClassException e) { |
| 80 | + bugReporter.reportMissingClass(e.getClassNotFoundException()); |
| 81 | + } catch (DataflowAnalysisException | CFGBuilderException e) { |
| 82 | + bugReporter.logError("While analyzing " + currentMethod + ": BuildRandomReturningMethodsDatabase caught an exception", e); |
| 83 | + } |
| 84 | + bugAccumulator.reportAccumulatedBugs(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void analyzeMethod(Method method) throws DataflowAnalysisException, CFGBuilderException { |
| 89 | + if ((method.getAccessFlags() & Const.ACC_BRIDGE) != 0) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + this.method = method; |
| 94 | + this.methodDescriptor = DescriptorFactory.instance().getMethodDescriptor(classContext.getJavaClass(), method); |
| 95 | + callListDataflow = classContext.getCallListDataflow(method); |
| 96 | + |
| 97 | + checkInvokeAndReturnInstructions(); |
| 98 | + } |
| 99 | + |
| 100 | + private void checkInvokeAndReturnInstructions() { |
| 101 | + Profiler profiler = Global.getAnalysisCache().getProfiler(); |
| 102 | + profiler.start(BuildRandomReturningMethodsDatabase.class); |
| 103 | + try { |
| 104 | + for (Iterator<Location> i = classContext.getCFG(method).locationIterator(); i.hasNext();) { |
| 105 | + Location location = i.next(); |
| 106 | + Instruction ins = location.getHandle().getInstruction(); |
| 107 | + |
| 108 | + if (ins instanceof ReturnInstruction) { |
| 109 | + examineReturnInstruction(location); |
| 110 | + } else if (ins instanceof InvokeInstruction) { |
| 111 | + examineInvokeInstruction((InvokeInstruction) ins); |
| 112 | + } |
| 113 | + } |
| 114 | + } catch (CheckedAnalysisException e) { |
| 115 | + AnalysisContext.logError("error:", e); |
| 116 | + } finally { |
| 117 | + profiler.end(BuildRandomReturningMethodsDatabase.class); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void examineInvokeInstruction(InvokeInstruction inv) { |
| 122 | + ConstantPoolGen cpg = classContext.getConstantPoolGen(); |
| 123 | + MethodDescriptor md = new MethodDescriptor(inv, classContext.getConstantPoolGen()); |
| 124 | + Call call = new Call(inv.getClassName(cpg), inv.getName(cpg), inv.getSignature(cpg)); |
| 125 | + callMethodDescriptorMap.put(call, md); |
| 126 | + } |
| 127 | + |
| 128 | + private void examineReturnInstruction(Location location) throws DataflowAnalysisException { |
| 129 | + // We have a crude assumption here that is any method call effects the return value of the caller method. |
| 130 | + CallList callList = callListDataflow.getFactAtLocation(location); |
| 131 | + if (!callList.isValid()) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + Iterator<Call> callIterator = callList.callIterator(); |
| 136 | + while (callIterator.hasNext()) { |
| 137 | + Call call = callIterator.next(); |
| 138 | + MethodDescriptor caller = methodDescriptor; |
| 139 | + MethodDescriptor called = callMethodDescriptorMap.get(call); |
| 140 | + if (called != null) { |
| 141 | + recordCalledMethod(caller, called); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private void recordCalledMethod(MethodDescriptor caller, MethodDescriptor called) { |
| 147 | + Boolean returnsRandom = database.getProperty(called); |
| 148 | + if (returnsRandom != null && returnsRandom) { |
| 149 | + callGraph.flushCallersToDatabase(caller, database, true); |
| 150 | + } else { |
| 151 | + if (callGraph.isInCallGraph(caller) || isLambdaHandlerInitMethod()) { |
| 152 | + // Call chain from Lambda event handler checks out |
| 153 | + callGraph.record(caller, called); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private boolean isLambdaHandlerInitMethod() { |
| 159 | + if (introspector.isLambdaHandler(classContext.getXClass())) { |
| 160 | + return Const.STATIC_INITIALIZER_NAME.equals(method.getName()) |
| 161 | + || Const.CONSTRUCTOR_NAME.equals(method.getName()); |
| 162 | + } |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void report() { |
| 168 | + // this is a non-reporting detector |
| 169 | + } |
| 170 | +} |
0 commit comments