Java Code Examples for org.objectweb.asm.Opcodes#ARRAYLENGTH

The following examples show how to use org.objectweb.asm.Opcodes#ARRAYLENGTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrayLengthExpression.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
public static int tryReduce(List<Instruction> instructions, int index) {
    if (index < 1) {
        return -1;
    }
    Instruction instr = instructions.get(index);
    Instruction prev = instructions.get(index-1);
    if (instr.getOpcode() == Opcodes.ARRAYLENGTH && prev instanceof AssignableExpression) {
        ArrayLengthExpression out = new ArrayLengthExpression();
        out.target = prev;
        out.arrayLenInstruction = instr;
        
        instructions.remove(index-1);
        instructions.remove(index-1);
        instructions.add(index-1, out);
        
        return index-1;
        
    }
    return -1;
}
 
Example 2
Source File: BeforeFieldAccess.java    From Mixin with MIT License 6 votes vote down vote up
/**
 * Searches for an array access instruction in the supplied instruction list
 * which is within <tt>searchRange</tt> instructions of the supplied field
 * instruction. Searching halts if the search range is exhausted, if an
 * {@link Opcodes#ARRAYLENGTH} opcode is encountered immediately after the
 * specified access, if a matching field access is found, or if the end of 
 * the method is reached.
 * 
 * @param insns Instruction list to search
 * @param fieldNode Field instruction to search from
 * @param opcode array access opcode to search for
 * @param searchRange search range
 * @return matching opcode or <tt>null</tt> if not matched
 */
public static AbstractInsnNode findArrayNode(InsnList insns, FieldInsnNode fieldNode, int opcode, int searchRange) {
    int pos = 0;
    for (Iterator<AbstractInsnNode> iter = insns.iterator(insns.indexOf(fieldNode) + 1); iter.hasNext();) {
        AbstractInsnNode insn = iter.next();
        if (insn.getOpcode() == opcode) {
            return insn;
        } else if (insn.getOpcode() == Opcodes.ARRAYLENGTH && pos == 0) {
            return null;
        } else if (insn instanceof FieldInsnNode) {
            FieldInsnNode field = (FieldInsnNode) insn;
            if (field.desc.equals(fieldNode.desc) && field.name.equals(fieldNode.name) && field.owner.equals(fieldNode.owner)) {
                return null;
            }
        }
        if (pos++ > searchRange) {
            return null;
        }
    }
    return null;
}
 
Example 3
Source File: BeforeFieldAccess.java    From Mixin with MIT License 5 votes vote down vote up
public BeforeFieldAccess(InjectionPointData data) {
    super(data);
    this.opcode = data.getOpcode(-1, Opcodes.GETFIELD, Opcodes.PUTFIELD, Opcodes.GETSTATIC, Opcodes.PUTSTATIC, -1);
    
    String array = data.get("array", "");
    this.arrOpcode = BeforeFieldAccess.ARRAY_GET.equalsIgnoreCase(array) ? Opcodes.IALOAD
            : BeforeFieldAccess.ARRAY_SET.equalsIgnoreCase(array) ? Opcodes.IASTORE
            : BeforeFieldAccess.ARRAY_LENGTH.equalsIgnoreCase(array) ? Opcodes.ARRAYLENGTH : 0;
    this.fuzzFactor = Math.min(Math.max(data.get("fuzz", BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT), 1), 32);
}
 
Example 4
Source File: RedirectInjector.java    From Mixin with MIT License 5 votes vote down vote up
/**
 * Array element read (xALOAD) or array.length (ARRAYLENGTH)
 */
private void injectAtGetArray(RedirectedFieldData field, AbstractInsnNode varNode) {
    field.description = "array getter";
    field.elementType = field.type.getElementType();
    
    if (varNode != null && varNode.getOpcode() == Opcodes.ARRAYLENGTH) {
        field.elementType = Type.INT_TYPE;
        field.extraDimensions = 0;
    }
    
    this.validateParams(field, field.elementType, field.getArrayArgs());
    this.injectArrayRedirect(field, varNode, "array getter");
}
 
Example 5
Source File: ArrayLengthFrame.java    From deobfuscator with Apache License 2.0 4 votes vote down vote up
public ArrayLengthFrame(Frame array) {
    super(Opcodes.ARRAYLENGTH);
    this.array = array;
    this.array.children.add(this);
    this.parents.add(array);
}
 
Example 6
Source File: AllocationMethodAdapter.java    From allocation-instrumenter with Apache License 2.0 4 votes vote down vote up
private void pushProductOfIntArrayOnStack() {
  Label beginScopeLabel = new Label();
  Label endScopeLabel = new Label();

  int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
  int counterIndex = newLocal("I", beginScopeLabel, endScopeLabel);
  int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
  Label loopLabel = new Label();
  Label endLabel = new Label();

  super.visitLabel(beginScopeLabel);

  // stack: ... intArray
  super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
  // -> stack: ...

  // counter = 0
  super.visitInsn(Opcodes.ICONST_0);
  super.visitVarInsn(Opcodes.ISTORE, counterIndex);
  // product = 1
  super.visitInsn(Opcodes.ICONST_1);
  super.visitVarInsn(Opcodes.ISTORE, productIndex);
  // loop:
  super.visitLabel(loopLabel);
  // if index >= arraylength goto end:
  super.visitVarInsn(Opcodes.ILOAD, counterIndex);
  super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
  super.visitInsn(Opcodes.ARRAYLENGTH);
  super.visitJumpInsn(Opcodes.IF_ICMPGE, endLabel);
  // product = product * max(array[counter],1)
  super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
  super.visitVarInsn(Opcodes.ILOAD, counterIndex);
  super.visitInsn(Opcodes.IALOAD);
  super.visitInsn(Opcodes.DUP);
  Label nonZeroDimension = new Label();
  super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
  super.visitInsn(Opcodes.POP);
  super.visitInsn(Opcodes.ICONST_1);
  super.visitLabel(nonZeroDimension);
  super.visitVarInsn(Opcodes.ILOAD, productIndex);
  super.visitInsn(Opcodes.IMUL); // if overflow happens it happens.
  super.visitVarInsn(Opcodes.ISTORE, productIndex);
  // iinc counter 1
  super.visitIincInsn(counterIndex, 1);
  // goto loop
  super.visitJumpInsn(Opcodes.GOTO, loopLabel);
  // end:
  super.visitLabel(endLabel);
  // re-push dimensions array
  super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
  // push product
  super.visitVarInsn(Opcodes.ILOAD, productIndex);

  super.visitLabel(endScopeLabel);
}
 
Example 7
Source File: AllocationMethodAdapter.java    From allocation-instrumenter with Apache License 2.0 4 votes vote down vote up
void calculateArrayLengthAndDispatch(String typeName, int dimCount) {
  // Since the dimensions of the array are not known at instrumentation
  // time, we take the created multi-dimensional array and peel off nesting
  // levels from the left.  For each nesting layer we probe the array length
  // and accumulate a partial product which we can then feed the recording
  // function.

  // below we note the partial product of dimensions 1 to X-1 as productToX
  // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
  // array reference at the current nesting level (the containing aref's [0]
  // element).  If we hit a level whose arraylength is 0 or whose
  // reference is null, there's no point continuing, so we shortcut
  // out.

  // This approach works pretty well when you create a new array with the
  // newarray bytecodes.  You can also create a new array by cloning an
  // existing array; an existing multidimensional array might have had some
  // of its [0] elements nulled out.  We currently deal with this by bailing
  // out, but arguably we should do something more principled (like calculate
  // the size of the multidimensional array from scratch if you are using
  // clone()).
  // TODO(java-platform-team): Do something about modified multidimensional
  // arrays and clone().
  Label zeroDimension = new Label();
  super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
  super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
  for (int i = 0; i < dimCount; ++i) {
    // pre: stack: ... origaref aref0 productToI
    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
    super.visitInsn(Opcodes.DUP);

    Label nonNullDimension = new Label();
    // -> stack: ... origaref productToI aref aref
    super.visitJumpInsn(Opcodes.IFNONNULL, nonNullDimension);
    // -> stack: ... origaref productToI aref
    super.visitInsn(Opcodes.SWAP);
    // -> stack: ... origaref aref productToI
    super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
    super.visitLabel(nonNullDimension);

    // -> stack: ... origaref productToI aref
    super.visitInsn(Opcodes.DUP_X1);
    // -> stack: ... origaref aref0 productToI aref
    super.visitInsn(Opcodes.ARRAYLENGTH);
    // -> stack: ... origaref aref0 productToI dimI

    Label nonZeroDimension = new Label();
    super.visitInsn(Opcodes.DUP);
    // -> stack: ... origaref aref0 productToI dimI dimI
    super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
    // -> stack: ... origaref aref0 productToI dimI
    super.visitInsn(Opcodes.POP);
    // -> stack: ... origaref aref0 productToI
    super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
    super.visitLabel(nonZeroDimension);
    // -> stack: ... origaref aref0 productToI max(dimI,1)

    super.visitInsn(Opcodes.IMUL);
    // -> stack: ... origaref aref0 productTo{I+1}
    if (i < dimCount - 1) {
      super.visitInsn(Opcodes.SWAP);
      // -> stack: ... origaref productTo{I+1} aref0
      super.visitInsn(Opcodes.ICONST_0);
      // -> stack: ... origaref productTo{I+1} aref0 0
      super.visitInsn(Opcodes.AALOAD);
      // -> stack: ... origaref productTo{I+1} aref0'
      super.visitInsn(Opcodes.SWAP);
    }
    // post: stack: ... origaref aref0 productTo{I+1}
  }
  super.visitLabel(zeroDimension);

  super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
  super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
  super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
  invokeRecordAllocation(typeName);
}
 
Example 8
Source File: BeforeFieldAccess.java    From Mixin with MIT License 4 votes vote down vote up
private int getArrayOpcode(String desc) {
    if (this.arrOpcode != Opcodes.ARRAYLENGTH) {
        return Type.getType(desc).getElementType().getOpcode(this.arrOpcode); 
    }
    return this.arrOpcode;
}