use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.
the class ConstantPoolParser method parseUtf8.
/**
* Parses a utf8 constant.
*
* @param at offset to the start of the constant (where the tag byte is)
* @return {@code non-null;} the parsed value
*/
private CstString parseUtf8(int at) {
int length = bytes.getUnsignedShort(at + 1);
// Skip to the data.
at += 3;
ByteArray ubytes = bytes.slice(at, at + length);
try {
return new CstString(ubytes);
} catch (IllegalArgumentException ex) {
// Translate the exception
throw new ParseException(ex);
}
}
use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.
the class AttributeFactory method parse.
/**
* Parses and makes an attribute based on the bytes at the
* indicated position in the given array. This method figures out
* the name, and then does all the setup to call on to {@link #parse0},
* which does the actual construction.
*
* @param cf {@code non-null;} class file to parse from
* @param context context to parse in; one of the {@code CTX_*}
* constants
* @param offset offset into {@code dcf}'s {@code bytes}
* to start parsing at
* @param observer {@code null-ok;} parse observer to report to, if any
* @return {@code non-null;} an appropriately-constructed {@link Attribute}
*/
public final Attribute parse(DirectClassFile cf, int context, int offset, ParseObserver observer) {
if (cf == null) {
throw new NullPointerException("cf == null");
}
if ((context < 0) || (context >= CTX_COUNT)) {
throw new IllegalArgumentException("bad context");
}
CstString name = null;
try {
ByteArray bytes = cf.getBytes();
ConstantPool pool = cf.getConstantPool();
int nameIdx = bytes.getUnsignedShort(offset);
int length = bytes.getInt(offset + 2);
name = (CstString) pool.get(nameIdx);
if (observer != null) {
observer.parsed(bytes, offset, 2, "name: " + name.toHuman());
observer.parsed(bytes, offset + 2, 4, "length: " + Hex.u4(length));
}
return parse0(cf, context, name.getString(), offset + 6, length, observer);
} catch (ParseException ex) {
ex.addContext("...while parsing " + ((name != null) ? (name.toHuman() + " ") : "") + "attribute at offset " + Hex.u4(offset));
throw ex;
}
}
use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.
the class AnnotationLister method process.
/** Processes based on configuration specified in constructor. */
void process() {
for (String path : args.files) {
ClassPathOpener opener;
opener = new ClassPathOpener(path, true, new ClassPathOpener.Consumer() {
public boolean processFileBytes(String name, long lastModified, byte[] bytes) {
if (!name.endsWith(".class")) {
return true;
}
ByteArray ba = new ByteArray(bytes);
DirectClassFile cf = new DirectClassFile(ba, name, true);
cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
AttributeList attributes = cf.getAttributes();
Attribute att;
String cfClassName = cf.getThisClass().getClassType().getClassName();
if (cfClassName.endsWith(PACKAGE_INFO)) {
att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
for (; att != null; att = attributes.findNext(att)) {
BaseAnnotations ann = (BaseAnnotations) att;
visitPackageAnnotation(cf, ann);
}
att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
for (; att != null; att = attributes.findNext(att)) {
BaseAnnotations ann = (BaseAnnotations) att;
visitPackageAnnotation(cf, ann);
}
} else if (isMatchingInnerClass(cfClassName) || isMatchingPackage(cfClassName)) {
printMatch(cf);
} else {
att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
for (; att != null; att = attributes.findNext(att)) {
BaseAnnotations ann = (BaseAnnotations) att;
visitClassAnnotation(cf, ann);
}
att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
for (; att != null; att = attributes.findNext(att)) {
BaseAnnotations ann = (BaseAnnotations) att;
visitClassAnnotation(cf, ann);
}
}
return true;
}
public void onException(Exception ex) {
throw new RuntimeException(ex);
}
public void onProcessArchiveStart(File file) {
}
});
opener.process();
}
}
use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.
the class BlockDumper method ropDump.
/**
* Does a registerizing dump.
*
* @param meth {@code non-null;} method data to dump
*/
private void ropDump(ConcreteMethod meth) {
TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
BytecodeArray code = meth.getCode();
ByteArray bytes = code.getBytes();
RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
StringBuffer sb = new StringBuffer(2000);
if (optimize) {
boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
int paramWidth = computeParamWidth(meth, isStatic);
rmeth = new Optimizer().optimize(rmeth, paramWidth, isStatic, true, advice);
}
BasicBlockList blocks = rmeth.getBlocks();
int[] order = blocks.getLabelsInOrder();
sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");
for (int label : order) {
BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
sb.append("block ");
sb.append(Hex.u2(label));
sb.append("\n");
IntList preds = rmeth.labelToPredecessors(label);
int psz = preds.size();
for (int i = 0; i < psz; i++) {
sb.append(" pred ");
sb.append(Hex.u2(preds.get(i)));
sb.append("\n");
}
InsnList il = bb.getInsns();
int ilsz = il.size();
for (int i = 0; i < ilsz; i++) {
Insn one = il.get(i);
sb.append(" ");
sb.append(il.get(i).toHuman());
sb.append("\n");
}
IntList successors = bb.getSuccessors();
int ssz = successors.size();
if (ssz == 0) {
sb.append(" returns\n");
} else {
int primary = bb.getPrimarySuccessor();
for (int i = 0; i < ssz; i++) {
int succ = successors.get(i);
sb.append(" next ");
sb.append(Hex.u2(succ));
if ((ssz != 1) && (succ == primary)) {
sb.append(" *");
}
sb.append("\n");
}
}
}
suppressDump = false;
setAt(bytes, 0);
parsed(bytes, 0, bytes.size(), sb.toString());
suppressDump = true;
}
use of com.taobao.android.dx.util.ByteArray in project atlas by alibaba.
the class BlockDumper method regularDump.
/**
* Does a regular basic block dump.
*
* @param meth {@code non-null;} method data to dump
*/
private void regularDump(ConcreteMethod meth) {
BytecodeArray code = meth.getCode();
ByteArray bytes = code.getBytes();
ByteBlockList list = BasicBlocker.identifyBlocks(meth);
int sz = list.size();
CodeObserver codeObserver = new CodeObserver(bytes, BlockDumper.this);
// Reset the dump cursor to the start of the bytecode.
setAt(bytes, 0);
suppressDump = false;
int byteAt = 0;
for (int i = 0; i < sz; i++) {
ByteBlock bb = list.get(i);
int start = bb.getStart();
int end = bb.getEnd();
if (byteAt < start) {
parsed(bytes, byteAt, start - byteAt, "dead code " + Hex.u2(byteAt) + ".." + Hex.u2(start));
}
parsed(bytes, start, 0, "block " + Hex.u2(bb.getLabel()) + ": " + Hex.u2(start) + ".." + Hex.u2(end));
changeIndent(1);
int len;
for (int j = start; j < end; j += len) {
len = code.parseInstruction(j, codeObserver);
codeObserver.setPreviousOffset(j);
}
IntList successors = bb.getSuccessors();
int ssz = successors.size();
if (ssz == 0) {
parsed(bytes, end, 0, "returns");
} else {
for (int j = 0; j < ssz; j++) {
int succ = successors.get(j);
parsed(bytes, end, 0, "next " + Hex.u2(succ));
}
}
ByteCatchList catches = bb.getCatches();
int csz = catches.size();
for (int j = 0; j < csz; j++) {
ByteCatchList.Item one = catches.get(j);
CstType exceptionClass = one.getExceptionClass();
parsed(bytes, end, 0, "catch " + ((exceptionClass == CstType.OBJECT) ? "<any>" : exceptionClass.toHuman()) + " -> " + Hex.u2(one.getHandlerPc()));
}
changeIndent(-1);
byteAt = end;
}
int end = bytes.size();
if (byteAt < end) {
parsed(bytes, byteAt, end - byteAt, "dead code " + Hex.u2(byteAt) + ".." + Hex.u2(end));
}
suppressDump = true;
}
Aggregations