2012-09-04 22:56:05 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*******************************************************************************/
|
|
|
|
package com.ibm.wala.classLoader;
|
|
|
|
|
2016-10-26 16:44:12 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
|
2012-09-04 22:56:05 +00:00
|
|
|
import com.ibm.wala.cfg.ControlFlowGraph;
|
|
|
|
import com.ibm.wala.cfg.ShrikeCFG;
|
|
|
|
import com.ibm.wala.ipa.callgraph.Context;
|
|
|
|
import com.ibm.wala.shrikeCT.InvalidClassFileException;
|
2016-10-27 11:36:11 +00:00
|
|
|
import com.ibm.wala.ssa.DefUse;
|
2012-09-04 22:56:05 +00:00
|
|
|
import com.ibm.wala.ssa.IR;
|
|
|
|
import com.ibm.wala.ssa.IRFactory;
|
2016-10-27 11:36:11 +00:00
|
|
|
import com.ibm.wala.ssa.SSAArrayLengthInstruction;
|
2012-09-04 22:56:05 +00:00
|
|
|
import com.ibm.wala.ssa.SSABuilder;
|
|
|
|
import com.ibm.wala.ssa.SSACFG;
|
|
|
|
import com.ibm.wala.ssa.SSAInstruction;
|
2016-10-26 16:44:12 +00:00
|
|
|
import com.ibm.wala.ssa.SSANewInstruction;
|
2012-09-04 22:56:05 +00:00
|
|
|
import com.ibm.wala.ssa.SSAOptions;
|
|
|
|
import com.ibm.wala.ssa.ShrikeIndirectionData;
|
|
|
|
import com.ibm.wala.ssa.SymbolTable;
|
|
|
|
import com.ibm.wala.ssa.analysis.DeadAssignmentElimination;
|
2016-10-26 16:44:12 +00:00
|
|
|
import com.ibm.wala.types.TypeReference;
|
2013-01-21 23:27:13 +00:00
|
|
|
import com.ibm.wala.util.WalaRuntimeException;
|
2012-09-04 22:56:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An {@link IRFactory} that for methods that originate from Shrike.
|
|
|
|
*/
|
|
|
|
public class ShrikeIRFactory implements IRFactory<IBytecodeMethod> {
|
|
|
|
|
|
|
|
public final static boolean buildLocalMap = true;
|
|
|
|
|
|
|
|
public ControlFlowGraph makeCFG(final IBytecodeMethod method, Context C) {
|
|
|
|
return ShrikeCFG.make(method);
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-09-04 22:56:05 +00:00
|
|
|
public IR makeIR(final IBytecodeMethod method, Context C, final SSAOptions options) throws IllegalArgumentException {
|
|
|
|
|
|
|
|
if (method == null) {
|
|
|
|
throw new IllegalArgumentException("null method");
|
|
|
|
}
|
|
|
|
com.ibm.wala.shrikeBT.IInstruction[] shrikeInstructions = null;
|
|
|
|
try {
|
|
|
|
shrikeInstructions = method.getInstructions();
|
|
|
|
} catch (InvalidClassFileException e) {
|
2013-01-21 23:27:13 +00:00
|
|
|
throw new WalaRuntimeException("bad method bytecodes", e);
|
2012-09-04 22:56:05 +00:00
|
|
|
}
|
|
|
|
final ShrikeCFG shrikeCFG = (ShrikeCFG) makeCFG(method, C);
|
|
|
|
|
|
|
|
final SymbolTable symbolTable = new SymbolTable(method.getNumberOfParameters());
|
|
|
|
final SSAInstruction[] newInstrs = new SSAInstruction[shrikeInstructions.length];
|
|
|
|
|
|
|
|
final SSACFG newCfg = new SSACFG(method, shrikeCFG, newInstrs);
|
|
|
|
|
|
|
|
return new IR(method, newInstrs, symbolTable, newCfg, options) {
|
|
|
|
private final SSA2LocalMap localMap;
|
|
|
|
|
|
|
|
private final ShrikeIndirectionData indirectionData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any phis that are dead assignments.
|
|
|
|
*
|
|
|
|
* TODO: move this elsewhere?
|
|
|
|
*/
|
|
|
|
private void eliminateDeadPhis() {
|
|
|
|
DeadAssignmentElimination.perform(this);
|
|
|
|
}
|
2016-10-26 16:44:12 +00:00
|
|
|
private void pruneExceptionsForSafeArrayCreations() {
|
2016-10-27 11:36:11 +00:00
|
|
|
DefUse du = new DefUse(this);
|
2016-10-26 16:44:12 +00:00
|
|
|
for (int i = 0; i < newInstrs.length; i++) {
|
|
|
|
SSAInstruction instr = newInstrs[i];
|
|
|
|
if (instr instanceof SSANewInstruction) {
|
|
|
|
SSANewInstruction newInstr = (SSANewInstruction) instr;
|
|
|
|
if (newInstr.getConcreteType().isArrayType()) {
|
2016-12-07 18:27:59 +00:00
|
|
|
boolean isSafe = true;
|
|
|
|
final int[] params = new int[newInstr.getNumberOfUses()];
|
|
|
|
for (int u = 0; u < newInstr.getNumberOfUses(); u++) {
|
|
|
|
int vLength = newInstr.getUse(u);
|
|
|
|
params[u] = vLength;
|
|
|
|
isSafe &= (isNonNegativeConstant(vLength) || isDefdByArrayLength(vLength, du));
|
|
|
|
}
|
|
|
|
if (isSafe) {
|
|
|
|
// newInstr is either obtained from
|
|
|
|
// JavaLanguage.JavaInstructionFactory#NewInstruction(int iindex, int result, NewSiteReference site, int[] params)
|
|
|
|
// or
|
|
|
|
// JavaLanguage.JavaInstructionFactory#NewInstruction(int iindex, int result, NewSiteReference site)
|
|
|
|
// , both provide anonymous subclasses of SSANewInstruction which differ
|
|
|
|
// from SSANewInstruction only in the implementation of getExceptionTypes().
|
|
|
|
// Hence, it is OK to just defining a new anonymous subclasses of SSANewInstruction, overriding getExceptionTypes().
|
|
|
|
newInstrs[i] = new SSANewInstruction(newInstr.iindex, newInstr.getDef(), newInstr.getNewSite(), params) {
|
2016-10-26 16:44:12 +00:00
|
|
|
@Override
|
|
|
|
public Collection<TypeReference> getExceptionTypes() {
|
|
|
|
return JavaLanguage.getNewSafeArrayExceptions();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 11:36:11 +00:00
|
|
|
private boolean isNonNegativeConstant(int vLength) {
|
|
|
|
return symbolTable.isIntegerConstant(vLength) && symbolTable.getIntValue(vLength) >= 0;
|
|
|
|
}
|
|
|
|
private boolean isDefdByArrayLength(int vLength, DefUse du) {
|
|
|
|
return du.getDef(vLength) instanceof SSAArrayLengthInstruction;
|
|
|
|
}
|
2012-09-04 22:56:05 +00:00
|
|
|
@Override
|
|
|
|
protected String instructionPosition(int instructionIndex) {
|
|
|
|
try {
|
|
|
|
int bcIndex = method.getBytecodeIndex(instructionIndex);
|
|
|
|
int lineNumber = method.getLineNumber(bcIndex);
|
|
|
|
|
|
|
|
if (lineNumber == -1) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return "(line " + lineNumber + ")";
|
|
|
|
}
|
|
|
|
} catch (InvalidClassFileException e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SSA2LocalMap getLocalMap() {
|
|
|
|
return localMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SSABuilder builder = SSABuilder.make(method, newCfg, shrikeCFG, newInstrs, symbolTable, buildLocalMap, options
|
|
|
|
.getPiNodePolicy());
|
|
|
|
builder.build();
|
|
|
|
if (buildLocalMap)
|
|
|
|
localMap = builder.getLocalMap();
|
|
|
|
else
|
|
|
|
localMap = null;
|
|
|
|
|
|
|
|
indirectionData = builder.getIndirectionData();
|
|
|
|
|
|
|
|
eliminateDeadPhis();
|
2016-10-26 16:44:12 +00:00
|
|
|
pruneExceptionsForSafeArrayCreations();
|
2012-09-04 22:56:05 +00:00
|
|
|
|
|
|
|
setupLocationMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
protected ShrikeIndirectionData getIndirectionData() {
|
|
|
|
return indirectionData;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2012-09-04 22:56:05 +00:00
|
|
|
public boolean contextIsIrrelevant(IBytecodeMethod method) {
|
|
|
|
// this factory always returns the same IR for a method
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|