|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2018 IBM Corporation. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * IBM Corporation - initial API and implementation |
| 10 | + *****************************************************************************/ |
| 11 | +package com.ibm.wala.cast.python.ipa.callgraph; |
| 12 | + |
| 13 | +import static com.ibm.wala.cast.python.types.Util.getGlobalName; |
| 14 | +import static com.ibm.wala.cast.python.types.Util.makeGlobalRef; |
| 15 | +import static com.ibm.wala.cast.python.util.Util.isClassMethod; |
| 16 | + |
| 17 | +import com.ibm.wala.cast.ir.ssa.AstGlobalRead; |
| 18 | +import com.ibm.wala.cast.loader.DynamicCallSiteReference; |
| 19 | +import com.ibm.wala.cast.python.ipa.summaries.PythonSummary; |
| 20 | +import com.ibm.wala.cast.python.ir.PythonLanguage; |
| 21 | +import com.ibm.wala.cast.python.ssa.PythonInvokeInstruction; |
| 22 | +import com.ibm.wala.cast.python.types.PythonTypes; |
| 23 | +import com.ibm.wala.classLoader.CallSiteReference; |
| 24 | +import com.ibm.wala.classLoader.IClass; |
| 25 | +import com.ibm.wala.core.util.strings.Atom; |
| 26 | +import com.ibm.wala.ipa.callgraph.CGNode; |
| 27 | +import com.ibm.wala.ipa.callgraph.MethodTargetSelector; |
| 28 | +import com.ibm.wala.ipa.cha.IClassHierarchy; |
| 29 | +import com.ibm.wala.ssa.SSAInstructionFactory; |
| 30 | +import com.ibm.wala.ssa.SSAReturnInstruction; |
| 31 | +import com.ibm.wala.types.FieldReference; |
| 32 | +import com.ibm.wala.util.collections.HashMapFactory; |
| 33 | +import com.ibm.wala.util.collections.Pair; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.logging.Logger; |
| 36 | + |
| 37 | +/** |
| 38 | + * A trampoline for <a href="https://docs.python.org/3/library/functions.html#classmethod">class |
| 39 | + * methods</a> that are not called using object instances. |
| 40 | + * |
| 41 | + * @author <a href="mailto:[email protected]">Raffi Khatchadourian</a> |
| 42 | + */ |
| 43 | +public class PythonClassMethodTrampolineTargetSelector<T> |
| 44 | + extends PythonMethodTrampolineTargetSelector<T> { |
| 45 | + |
| 46 | + protected static final Logger LOGGER = |
| 47 | + Logger.getLogger(PythonClassMethodTrampolineTargetSelector.class.getName()); |
| 48 | + |
| 49 | + public PythonClassMethodTrampolineTargetSelector(MethodTargetSelector base) { |
| 50 | + super(base); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected boolean shouldProcess(CGNode caller, CallSiteReference site, IClass receiver) { |
| 55 | + IClassHierarchy cha = receiver.getClassHierarchy(); |
| 56 | + |
| 57 | + // Are we calling a class method? |
| 58 | + boolean classMethodReceiver = isClassMethod(receiver); |
| 59 | + |
| 60 | + // Is the caller a trampoline? |
| 61 | + boolean trampoline = |
| 62 | + caller |
| 63 | + .getMethod() |
| 64 | + .getSelector() |
| 65 | + .getName() |
| 66 | + .startsWith(Atom.findOrCreateAsciiAtom("trampoline")); |
| 67 | + |
| 68 | + return classMethodReceiver |
| 69 | + && !cha.isSubclassOf(receiver, cha.lookupClass(PythonTypes.trampoline)) |
| 70 | + && !trampoline; |
| 71 | + } |
| 72 | + |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + @Override |
| 75 | + protected void populate( |
| 76 | + PythonSummary x, int v, IClass receiver, PythonInvokeInstruction call, Logger logger) { |
| 77 | + Map<Integer, Atom> names = HashMapFactory.make(); |
| 78 | + SSAInstructionFactory insts = PythonLanguage.Python.instructionFactory(); |
| 79 | + |
| 80 | + // Read the class from the global scope. |
| 81 | + String globalName = getGlobalName(receiver.getReference()); |
| 82 | + FieldReference globalRef = makeGlobalRef(receiver.getClassLoader(), globalName); |
| 83 | + int globalReadRes = v++; |
| 84 | + int pc = 0; |
| 85 | + |
| 86 | + x.addStatement(new AstGlobalRead(pc++, globalReadRes, globalRef)); |
| 87 | + |
| 88 | + int getInstRes = v++; |
| 89 | + |
| 90 | + // Read the field from the class corresponding to the called method. |
| 91 | + FieldReference method = |
| 92 | + FieldReference.findOrCreate( |
| 93 | + PythonTypes.Root, Atom.findOrCreateUnicodeAtom("the_class_method"), PythonTypes.Root); |
| 94 | + |
| 95 | + x.addStatement(insts.GetInstruction(pc++, getInstRes, globalReadRes, method)); |
| 96 | + |
| 97 | + int i = 0; |
| 98 | + int paramSize = Math.max(2, call.getNumberOfPositionalParameters() + 1); |
| 99 | + int[] params = new int[paramSize]; |
| 100 | + params[i++] = getInstRes; |
| 101 | + params[i++] = globalReadRes; |
| 102 | + |
| 103 | + for (int j = 1; j < call.getNumberOfPositionalParameters(); j++) params[i++] = j + 1; |
| 104 | + |
| 105 | + int ki = 0, ji = call.getNumberOfPositionalParameters() + 1; |
| 106 | + Pair<String, Integer>[] keys = new Pair[0]; |
| 107 | + |
| 108 | + if (call.getKeywords() != null) { |
| 109 | + keys = new Pair[call.getKeywords().size()]; |
| 110 | + |
| 111 | + for (String k : call.getKeywords()) { |
| 112 | + names.put(ji, Atom.findOrCreateUnicodeAtom(k)); |
| 113 | + keys[ki++] = Pair.make(k, ji++); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + CallSiteReference ref = new DynamicCallSiteReference(call.getCallSite().getDeclaredTarget(), 2); |
| 118 | + |
| 119 | + int except = v++; |
| 120 | + int invokeResult = v++; |
| 121 | + |
| 122 | + x.addStatement(new PythonInvokeInstruction(pc++, invokeResult, except, ref, params, keys)); |
| 123 | + x.addStatement(new SSAReturnInstruction(pc++, invokeResult, false)); |
| 124 | + x.setValueNames(names); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected Logger getLogger() { |
| 129 | + return LOGGER; |
| 130 | + } |
| 131 | +} |
0 commit comments