use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.
the class Main method computeReferencedResources.
private void computeReferencedResources() {
for (Item genericItem : outputDex.getFieldIds().items()) {
FieldIdItem item = (FieldIdItem) genericItem;
CstType fieldClass = item.getDefiningClass();
CstString fieldName = item.getRef().getNat().getName();
if (fieldClass.getClassType().getDescriptor().contains("/R$")) {
// We ignore the name of the containing class for simplicity.
resourceNames.add(fieldName.getString());
}
}
}
use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.
the class OutputFinisher method assignIndices.
/**
* Helper for {@link #assignIndices} which does assignment for one
* instruction.
*
* @param insn {@code non-null;} the instruction
* @param callback {@code non-null;} the callback
*/
private static void assignIndices(CstInsn insn, DalvCode.AssignIndicesCallback callback) {
Constant cst = insn.getConstant();
int index = callback.getIndex(cst);
if (index >= 0) {
insn.setIndex(index);
}
if (cst instanceof CstMemberRef) {
CstMemberRef member = (CstMemberRef) cst;
CstType definer = member.getDefiningClass();
index = callback.getIndex(definer);
if (index >= 0) {
insn.setClassIndex(index);
}
}
}
use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.
the class RopToDop method dopFor.
/**
* Returns the dalvik opcode appropriate for the given register-based
* instruction.
*
* @param insn {@code non-null;} the original instruction
* @return the corresponding dalvik opcode; one of the constants in
* {@link Dops}
*/
public static Dop dopFor(Insn insn) {
Rop rop = insn.getOpcode();
/*
* First, just try looking up the rop in the MAP of easy
* cases.
*/
Dop result = MAP.get(rop);
if (result != null) {
return result;
}
switch(rop.getOpcode()) {
case RegOps.MOVE_EXCEPTION:
return Dops.MOVE_EXCEPTION;
case RegOps.INVOKE_STATIC:
return Dops.INVOKE_STATIC;
case RegOps.INVOKE_VIRTUAL:
return Dops.INVOKE_VIRTUAL;
case RegOps.INVOKE_SUPER:
return Dops.INVOKE_SUPER;
case RegOps.INVOKE_DIRECT:
return Dops.INVOKE_DIRECT;
case RegOps.INVOKE_INTERFACE:
return Dops.INVOKE_INTERFACE;
case RegOps.NEW_ARRAY:
return Dops.NEW_ARRAY;
case RegOps.FILLED_NEW_ARRAY:
return Dops.FILLED_NEW_ARRAY;
case RegOps.FILL_ARRAY_DATA:
return Dops.FILL_ARRAY_DATA;
case RegOps.MOVE_RESULT:
{
RegisterSpec resultReg = insn.getResult();
if (resultReg == null) {
return Dops.NOP;
} else {
switch(resultReg.getBasicType()) {
case Type.BT_INT:
case Type.BT_FLOAT:
case Type.BT_BOOLEAN:
case Type.BT_BYTE:
case Type.BT_CHAR:
case Type.BT_SHORT:
return Dops.MOVE_RESULT;
case Type.BT_LONG:
case Type.BT_DOUBLE:
return Dops.MOVE_RESULT_WIDE;
case Type.BT_OBJECT:
return Dops.MOVE_RESULT_OBJECT;
default:
{
throw new RuntimeException("Unexpected basic type");
}
}
}
}
case RegOps.GET_FIELD:
{
CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
int basicType = ref.getBasicType();
switch(basicType) {
case Type.BT_BOOLEAN:
return Dops.IGET_BOOLEAN;
case Type.BT_BYTE:
return Dops.IGET_BYTE;
case Type.BT_CHAR:
return Dops.IGET_CHAR;
case Type.BT_SHORT:
return Dops.IGET_SHORT;
case Type.BT_INT:
return Dops.IGET;
}
break;
}
case RegOps.PUT_FIELD:
{
CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
int basicType = ref.getBasicType();
switch(basicType) {
case Type.BT_BOOLEAN:
return Dops.IPUT_BOOLEAN;
case Type.BT_BYTE:
return Dops.IPUT_BYTE;
case Type.BT_CHAR:
return Dops.IPUT_CHAR;
case Type.BT_SHORT:
return Dops.IPUT_SHORT;
case Type.BT_INT:
return Dops.IPUT;
}
break;
}
case RegOps.GET_STATIC:
{
CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
int basicType = ref.getBasicType();
switch(basicType) {
case Type.BT_BOOLEAN:
return Dops.SGET_BOOLEAN;
case Type.BT_BYTE:
return Dops.SGET_BYTE;
case Type.BT_CHAR:
return Dops.SGET_CHAR;
case Type.BT_SHORT:
return Dops.SGET_SHORT;
case Type.BT_INT:
return Dops.SGET;
}
break;
}
case RegOps.PUT_STATIC:
{
CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
int basicType = ref.getBasicType();
switch(basicType) {
case Type.BT_BOOLEAN:
return Dops.SPUT_BOOLEAN;
case Type.BT_BYTE:
return Dops.SPUT_BYTE;
case Type.BT_CHAR:
return Dops.SPUT_CHAR;
case Type.BT_SHORT:
return Dops.SPUT_SHORT;
case Type.BT_INT:
return Dops.SPUT;
}
break;
}
case RegOps.CONST:
{
Constant cst = ((ThrowingCstInsn) insn).getConstant();
if (cst instanceof CstType) {
return Dops.CONST_CLASS;
} else if (cst instanceof CstString) {
return Dops.CONST_STRING;
}
break;
}
}
throw new RuntimeException("unknown rop: " + rop);
}
use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.
the class StdAttributeFactory method code.
/**
* Parses a {@code Code} attribute.
*/
private Attribute code(DirectClassFile cf, int offset, int length, ParseObserver observer) {
if (length < 12) {
return throwSeverelyTruncated();
}
ByteArray bytes = cf.getBytes();
ConstantPool pool = cf.getConstantPool();
// u2 max_stack
int maxStack = bytes.getUnsignedShort(offset);
// u2 max_locals
int maxLocals = bytes.getUnsignedShort(offset + 2);
// u4 code_length
int codeLength = bytes.getInt(offset + 4);
int origOffset = offset;
if (observer != null) {
observer.parsed(bytes, offset, 2, "max_stack: " + Hex.u2(maxStack));
observer.parsed(bytes, offset + 2, 2, "max_locals: " + Hex.u2(maxLocals));
observer.parsed(bytes, offset + 4, 4, "code_length: " + Hex.u4(codeLength));
}
offset += 8;
length -= 8;
if (length < (codeLength + 4)) {
return throwTruncated();
}
int codeOffset = offset;
offset += codeLength;
length -= codeLength;
BytecodeArray code = new BytecodeArray(bytes.slice(codeOffset, codeOffset + codeLength), pool);
if (observer != null) {
code.forEach(new CodeObserver(code.getBytes(), observer));
}
// u2 exception_table_length
int exceptionTableLength = bytes.getUnsignedShort(offset);
ByteCatchList catches = (exceptionTableLength == 0) ? ByteCatchList.EMPTY : new ByteCatchList(exceptionTableLength);
if (observer != null) {
observer.parsed(bytes, offset, 2, "exception_table_length: " + Hex.u2(exceptionTableLength));
}
offset += 2;
length -= 2;
if (length < (exceptionTableLength * 8 + 2)) {
return throwTruncated();
}
for (int i = 0; i < exceptionTableLength; i++) {
if (observer != null) {
observer.changeIndent(1);
}
int startPc = bytes.getUnsignedShort(offset);
int endPc = bytes.getUnsignedShort(offset + 2);
int handlerPc = bytes.getUnsignedShort(offset + 4);
int catchTypeIdx = bytes.getUnsignedShort(offset + 6);
CstType catchType = (CstType) pool.get0Ok(catchTypeIdx);
catches.set(i, startPc, endPc, handlerPc, catchType);
if (observer != null) {
observer.parsed(bytes, offset, 8, Hex.u2(startPc) + ".." + Hex.u2(endPc) + " -> " + Hex.u2(handlerPc) + " " + ((catchType == null) ? "<any>" : catchType.toHuman()));
}
offset += 8;
length -= 8;
if (observer != null) {
observer.changeIndent(-1);
}
}
catches.setImmutable();
AttributeListParser parser = new AttributeListParser(cf, CTX_CODE, offset, this);
parser.setObserver(observer);
StdAttributeList attributes = parser.getList();
attributes.setImmutable();
int attributeByteCount = parser.getEndOffset() - offset;
if (attributeByteCount != length) {
return throwBadLength(attributeByteCount + (offset - origOffset));
}
return new AttCode(maxStack, maxLocals, code, catches, attributes);
}
use of com.taobao.android.dx.rop.cst.CstType in project atlas by alibaba.
the class StdCatchBuilder method handlersFor.
/**
* Makes the {@link CatchHandlerList} for the given basic block.
*
* @param block {@code non-null;} block to get entries for
* @param addresses {@code non-null;} address objects for each block
* @return {@code non-null;} array of entries
*/
private static CatchHandlerList handlersFor(BasicBlock block, BlockAddresses addresses) {
IntList successors = block.getSuccessors();
int succSize = successors.size();
int primary = block.getPrimarySuccessor();
TypeList catches = block.getLastInsn().getCatches();
int catchSize = catches.size();
if (catchSize == 0) {
return CatchHandlerList.EMPTY;
}
if (((primary == -1) && (succSize != catchSize)) || ((primary != -1) && ((succSize != (catchSize + 1)) || (primary != successors.get(catchSize))))) {
/*
* Blocks that throw are supposed to list their primary
* successor -- if any -- last in the successors list, but
* that constraint appears to be violated here.
*/
throw new RuntimeException("shouldn't happen: weird successors list");
}
/*
* Reduce the effective catchSize if we spot a catch-all that
* isn't at the end.
*/
for (int i = 0; i < catchSize; i++) {
Type type = catches.getType(i);
if (type.equals(Type.OBJECT)) {
catchSize = i + 1;
break;
}
}
CatchHandlerList result = new CatchHandlerList(catchSize);
for (int i = 0; i < catchSize; i++) {
CstType oneType = new CstType(catches.getType(i));
CodeAddress oneHandler = addresses.getStart(successors.get(i));
result.set(i, oneType, oneHandler.getAddress());
}
result.setImmutable();
return result;
}
Aggregations