Example usage for org.objectweb.asm.tree FieldInsnNode getOpcode

List of usage examples for org.objectweb.asm.tree FieldInsnNode getOpcode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree FieldInsnNode getOpcode.

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

From source file:com.android.tools.lint.checks.CordovaVersionDetector.java

License:Apache License

private void checkInstructionInternal(ClassContext context, ClassNode classNode, AbstractInsnNode instruction) {

    FieldInsnNode node = (FieldInsnNode) instruction;
    if (node.getOpcode() == Opcodes.PUTSTATIC && node.owner.equals(FQN_CORDOVA_DEVICE)
            && node.name.equals(FIELD_NAME_CORDOVA_VERSION)) {
        AbstractInsnNode prevInstruction = LintUtils.getPrevInstruction(node);
        if (prevInstruction == null || prevInstruction.getOpcode() != Opcodes.LDC) {
            return;
        }/*from   ww w . jav  a2 s.c  o  m*/
        LdcInsnNode ldcInsnNode = (LdcInsnNode) prevInstruction;
        if (ldcInsnNode.cst instanceof String) {
            mCordovaVersion = createVersion((String) ldcInsnNode.cst);
            if (mCordovaVersion != null) {
                validateCordovaVersion(context, mCordovaVersion, context.getLocation(classNode));
            }
        }
    }
}

From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.TracingMethodInstrumenter.java

License:Open Source License

private void transformFieldInsn(final FieldInsnNode insn) {
    int objectTraceSeqIndex = -1;

    switch (insn.getOpcode()) {
    case PUTSTATIC:
    case GETSTATIC:
        // nothing is traced
        break;//from w w w  .  ja v a2 s. c  o  m

    case GETFIELD:
        // do not trace assignments or usages of "this$..." and "val$..." fields
        if (!insn.name.contains("$")) { // TODO can we lift this?
            // top item on stack is the object reference: duplicate it
            // (add instruction *before* the current one
            this.instructionIterator.previous();
            this.instructionIterator.add(new InsnNode(DUP));
            objectTraceSeqIndex = this.tracer.newLongTraceSequence();
            ++TracingMethodInstrumenter.statsGetField;
            //System.out.println("seq " + index + ": getField " + name + " in method " + readMethod.getReadClass().getClassName() + "." + readMethod.getName());
        }
        break;

    case PUTFIELD:
        // do not trace assignments or usages of "this$..." and "val$..." fields
        if (!insn.name.contains("$")) { // TODO can we lift this?
            // the second item on the stack is the object reference
            // (add instruction *before* the current one
            this.instructionIterator.previous();
            final int size = Type.getType(insn.desc).getSize(); // either 1 or 2
            if (size == 1) {
                this.instructionIterator.add(new InsnNode(DUP2));
                this.instructionIterator.add(new InsnNode(POP));
            } else {
                this.instructionIterator.add(new InsnNode(DUP2_X1));
                this.instructionIterator.add(new InsnNode(POP2));
                this.instructionIterator.add(new InsnNode(DUP_X2));
            }
            objectTraceSeqIndex = this.tracer.newLongTraceSequence();
            ++TracingMethodInstrumenter.statsPutField;
            //System.out.println("seq " + index + ": putField " + name + " in method " + readMethod.getReadClass().getClassName() + "." + readMethod.getName());
        }
        break;

    default:
        break;
    }

    if (objectTraceSeqIndex != -1) {
        this.instructionIterator.add(new VarInsnNode(ALOAD, this.tracerLocalVarIndex));
        this.instructionIterator.add(new InsnNode(SWAP));
        this.instructionIterator.add(getIntConstInsn(objectTraceSeqIndex));
        this.instructionIterator.add(new MethodInsnNode(INVOKEINTERFACE,
                Type.getInternalName(ThreadTracer.class), "traceObject", "(Ljava/lang/Object;I)V", true));
        // and move to the position where it was before entering this method
        this.instructionIterator.next();
    }

    registerInstruction(new FieldInstruction(this.readMethod, insn.getOpcode(), this.currentLine, insn.owner,
            insn.name, insn.desc, objectTraceSeqIndex), InstructionType.UNSAFE);
}

From source file:edu.mit.streamjit.impl.common.MessageConstraint.java

License:Open Source License

/**
 * Parse the given getHandle() call instruction and preceding instructions
 * into a WorkerData.  This is a rather brittle pattern-matching job and
 * will fail on obfuscated bytecodes./*from   w  ww.j a va  2 s . c o  m*/
 * @param call
 * @return
 */
private static WorkerData dataFromCall(Class<?> klass, MethodInsnNode call) {
    //Latency is either an integer constant or a getfield on this.
    Field latencyField = null;
    int constantLatency = Integer.MIN_VALUE;
    AbstractInsnNode latencyInsn = call.getPrevious();
    if (latencyInsn instanceof FieldInsnNode) {
        FieldInsnNode fieldInsn = (FieldInsnNode) latencyInsn;
        if (fieldInsn.getOpcode() != Opcodes.GETFIELD)
            throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                    + ": latency field insn opcode " + fieldInsn.getOpcode());
        if (!fieldInsn.desc.equals(Type.INT_TYPE.getDescriptor()))
            throw new IllegalStreamGraphException(
                    "Unsupported getHandle() use in " + klass + ": latency field desc " + fieldInsn.desc);
        if (!fieldInsn.owner.equals(Type.getType(klass).getInternalName()))
            throw new IllegalStreamGraphException(
                    "Unsupported getHandle() use in " + klass + ": latency field owner " + fieldInsn.owner);

        //Move latencyInsn to sync up with the other else-if branches.
        latencyInsn = latencyInsn.getPrevious();
        //We must be loading from this.
        if (latencyInsn.getOpcode() != Opcodes.ALOAD)
            throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                    + ": getfield subject opcode " + latencyInsn.getOpcode());
        int varIdx = ((VarInsnNode) latencyInsn).var;
        if (varIdx != 0)
            throw new IllegalStreamGraphException(
                    "Unsupported getHandle() use in " + klass + ": getfield not from this but from " + varIdx);

        //Check the field we're loading from is constant (final).
        //A static field is okay here since it isn't a reference parameter.
        try {
            latencyField = klass.getDeclaredField(fieldInsn.name);
            if (!Modifier.isFinal(latencyField.getModifiers()))
                throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                        + ": latency field not final: " + latencyField.toGenericString());
        } catch (NoSuchFieldException ex) {
            throw new IllegalStreamGraphException(
                    "Unsupported getHandle() use in " + klass + ": getfield not from this but from " + varIdx);
        }
    } else if (latencyInsn instanceof LdcInsnNode) {
        Object constant = ((LdcInsnNode) latencyInsn).cst;
        if (!(constant instanceof Integer))
            throw new IllegalStreamGraphException(
                    "Unsupported getHandle() use in " + klass + ": ldc " + constant);
        constantLatency = ((Integer) constant);
    } else
        switch (latencyInsn.getOpcode()) {
        case Opcodes.ICONST_M1:
            constantLatency = -1;
            break;
        case Opcodes.ICONST_0:
            constantLatency = 0;
            break;
        case Opcodes.ICONST_1:
            constantLatency = 1;
            break;
        case Opcodes.ICONST_2:
            constantLatency = 2;
            break;
        case Opcodes.ICONST_3:
            constantLatency = 3;
            break;
        case Opcodes.ICONST_4:
            constantLatency = 4;
            break;
        case Opcodes.ICONST_5:
            constantLatency = 5;
            break;
        case Opcodes.BIPUSH:
        case Opcodes.SIPUSH:
            constantLatency = ((IntInsnNode) latencyInsn).operand;
            break;
        default:
            throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                    + ": latencyInsn opcode " + latencyInsn.getOpcode());
        }
    //Finally, we've parsed the latency parameter.

    //Next is an aload_0 for the sender parameter.
    AbstractInsnNode senderInsn = latencyInsn.getPrevious();
    if (senderInsn.getOpcode() != Opcodes.ALOAD || ((VarInsnNode) senderInsn).var != 0)
        throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": bad sender");

    //Finally, a getfield of this for a final Portal instance field.
    AbstractInsnNode portalInsn = senderInsn.getPrevious();
    if (!(portalInsn instanceof FieldInsnNode))
        throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                + ": portal getfield opcode " + portalInsn.getOpcode());
    FieldInsnNode fieldInsn = (FieldInsnNode) portalInsn;
    if (fieldInsn.getOpcode() != Opcodes.GETFIELD)
        throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                + ": portal field insn opcode " + fieldInsn.getOpcode());
    if (!fieldInsn.desc.equals(Type.getType(Portal.class).getDescriptor()))
        throw new IllegalStreamGraphException(
                "Unsupported getHandle() use in " + klass + ": portal field desc " + fieldInsn.desc);
    if (!fieldInsn.owner.equals(Type.getType(klass).getInternalName()))
        throw new IllegalStreamGraphException(
                "Unsupported getHandle() use in " + klass + ": portal field owner " + fieldInsn.owner);

    portalInsn = portalInsn.getPrevious();
    //We must be loading from this.
    if (portalInsn.getOpcode() != Opcodes.ALOAD)
        throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                + ": portal getfield subject opcode " + portalInsn.getOpcode());
    int varIdx = ((VarInsnNode) portalInsn).var;
    if (varIdx != 0)
        throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                + ": portal getfield not from this but from " + varIdx);

    //Check the field we're loading from is constant (final) and nonstatic.
    Field portalField;
    try {
        portalField = klass.getDeclaredField(fieldInsn.name);
        if (!Modifier.isFinal(portalField.getModifiers()))
            throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                    + ": portal field not final: " + portalField.toGenericString());
        if (Modifier.isStatic(portalField.getModifiers()))
            throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                    + ": portal field is static: " + portalField.toGenericString());
    } catch (NoSuchFieldException ex) {
        throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass
                + ": portal getfield not from this but from " + varIdx);
    }

    return latencyField != null ? new WorkerData(portalField, latencyField)
            : new WorkerData(portalField, constantLatency);
}

From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java

License:Open Source License

private void interpret(FieldInsnNode insn, FrameState frame, BBInfo block) {
    Klass k = getKlassByInternalName(insn.owner);
    Field f = k.getField(insn.name);
    switch (insn.getOpcode()) {
    case Opcodes.GETSTATIC:
        LoadInst li = new LoadInst(f);
        block.block.instructions().add(li);
        frame.stack.push(li);// ww  w .  ja  va  2 s.  c  o m
        break;
    case Opcodes.GETFIELD:
        LoadInst li2 = new LoadInst(f, frame.stack.pop());
        block.block.instructions().add(li2);
        frame.stack.push(li2);
        break;
    case Opcodes.PUTSTATIC:
        StoreInst si = new StoreInst(f, frame.stack.pop());
        block.block.instructions().add(si);
        break;
    case Opcodes.PUTFIELD:
        StoreInst si2 = new StoreInst(f, frame.stack.pop(), frame.stack.pop());
        block.block.instructions().add(si2);
        break;
    default:
        throw new UnsupportedOperationException("" + insn.getOpcode());
    }
}

From source file:jaspex.transactifier.ChangeClinitMethodVisitor.java

License:Open Source License

private static boolean clinitIsSafe(Type t) {
    try {/*from w w  w.j  av a2s . c  o m*/
        ClassReader cr = new ClassReader(t.commonName());
        ClassNode cNode = new ClassNode();
        cr.accept(cNode, 0);

        for (MethodNode method : cNode.methods) {
            if (!method.name.equals("<clinit>"))
                continue;
            // Examinar instruces
            Iterator<AbstractInsnNode> it = method.instructions.iterator();
            while (it.hasNext()) {
                AbstractInsnNode insn = it.next();
                switch (insn.getType()) {
                case AbstractInsnNode.FRAME:
                case AbstractInsnNode.INT_INSN:
                case AbstractInsnNode.JUMP_INSN:
                case AbstractInsnNode.LABEL:
                case AbstractInsnNode.LDC_INSN:
                case AbstractInsnNode.LINE:
                case AbstractInsnNode.LOOKUPSWITCH_INSN:
                case AbstractInsnNode.MULTIANEWARRAY_INSN:
                case AbstractInsnNode.TABLESWITCH_INSN:
                case AbstractInsnNode.TYPE_INSN:
                case AbstractInsnNode.VAR_INSN:
                    break;
                case AbstractInsnNode.FIELD_INSN:
                    FieldInsnNode fieldInsn = (FieldInsnNode) insn;
                    if (fieldInsn.getOpcode() != PUTSTATIC) {
                        // GETSTATIC, GETFIELD, PUTFIELD
                        return false;
                    }
                    break;
                case AbstractInsnNode.IINC_INSN:
                    return false;
                case AbstractInsnNode.INSN:
                    if (unsafeInsnBytecodes.contains(insn.getOpcode())) {
                        Log.debug(t.commonName() + ".<clinit>() is unsafe " + "because of bytecode "
                                + insn.getOpcode());
                        return false;
                    }
                    break;
                case AbstractInsnNode.METHOD_INSN:
                    MethodInsnNode methodInsn = (MethodInsnNode) insn;
                    if (!ClassFilter.isMethodWhitelisted(Type.fromAsm(methodInsn.owner), methodInsn.name,
                            methodInsn.desc)) {
                        Log.debug(t.commonName() + ".<clinit>() is unsafe " + "because it invokes "
                                + Type.fromAsm(methodInsn.owner).commonName() + "." + methodInsn.name);
                        return false;
                    }
                    break;
                default:
                    throw new Error("Unexpected bytecode " + insn);
                }
            }

            //Log.debug(t.commonName() + ".<clinit>() for " + t + " is safe");
            return true;
        }

        return false;
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:net.minecraftforge.fml.common.asm.transformers.FieldRedirectTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (!this.clsName.equals(transformedName))
        return basicClass;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);//from  w  w  w  .  j  a v a  2 s . c o  m

    FieldNode fieldRef = null;
    for (FieldNode f : classNode.fields) {
        if (this.TYPE.equals(f.desc) && fieldRef == null) {
            fieldRef = f;
        } else if (this.TYPE.equals(f.desc)) {
            throw new RuntimeException("Error processing " + clsName + " - found a duplicate holder field");
        }
    }
    if (fieldRef == null) {
        throw new RuntimeException("Error processing " + clsName
                + " - no holder field declared (is the code somehow obfuscated?)");
    }

    MethodNode getMethod = null;
    for (MethodNode m : classNode.methods) {
        if (m.name.equals(this.bypass))
            continue;
        if (this.DESC.equals(m.desc) && getMethod == null) {
            getMethod = m;
        } else if (this.DESC.equals(m.desc)) {
            throw new RuntimeException("Error processing " + clsName + " - duplicate get method found");
        }
    }
    if (getMethod == null) {
        throw new RuntimeException(
                "Error processing " + clsName + " - no get method found (is the code somehow obfuscated?)");
    }

    for (MethodNode m : classNode.methods) {
        if (m.name.equals(this.bypass))
            continue;
        for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext();) {
            AbstractInsnNode insnNode = it.next();
            if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode fi = (FieldInsnNode) insnNode;
                if (fieldRef.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) {
                    it.remove();
                    MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name,
                            getMethod.name, getMethod.desc, false);
                    it.add(replace);
                }
            }
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InsnListPrinter.java

License:Open Source License

private void _visitInsn(AbstractInsnNode insn) {
    switch (insn.getType()) {
    case 0://from w w w. ja  va2s .c o m
        visitInsn(insn.getOpcode());
        break;
    case 1:
        IntInsnNode iinsn = (IntInsnNode) insn;
        visitIntInsn(iinsn.getOpcode(), iinsn.operand);
        break;
    case 2:
        VarInsnNode vinsn = (VarInsnNode) insn;
        visitVarInsn(vinsn.getOpcode(), vinsn.var);
        break;
    case 3:
        TypeInsnNode tinsn = (TypeInsnNode) insn;
        visitTypeInsn(tinsn.getOpcode(), tinsn.desc);
        break;
    case 4:
        FieldInsnNode finsn = (FieldInsnNode) insn;
        visitFieldInsn(finsn.getOpcode(), finsn.owner, finsn.name, finsn.desc);
        break;
    case 5:
        MethodInsnNode minsn = (MethodInsnNode) insn;
        visitMethodInsn(minsn.getOpcode(), minsn.owner, minsn.name, minsn.desc);
        break;
    case 6:
        InvokeDynamicInsnNode idinsn = (InvokeDynamicInsnNode) insn;
        visitInvokeDynamicInsn(idinsn.name, idinsn.desc, idinsn.bsm, idinsn.bsmArgs);
        break;
    case 7:
        JumpInsnNode jinsn = (JumpInsnNode) insn;
        visitJumpInsn(jinsn.getOpcode(), jinsn.label.getLabel());
        break;
    case 8:
        LabelNode linsn = (LabelNode) insn;
        visitLabel(linsn.getLabel());
        break;
    case 9:
        LdcInsnNode ldcinsn = (LdcInsnNode) insn;
        visitLdcInsn(ldcinsn.cst);
        break;
    case 10:
        IincInsnNode iiinsn = (IincInsnNode) insn;
        visitIincInsn(iiinsn.var, iiinsn.incr);
        break;
    case 11:
        TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
        Label[] tslables = new Label[tsinsn.labels.size()];
        for (int i = 0; i < tslables.length; i++) {
            tslables[i] = tsinsn.labels.get(i).getLabel();
        }
        visitTableSwitchInsn(tsinsn.min, tsinsn.max, tsinsn.dflt.getLabel(), tslables);
        break;
    case 12:
        LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
        Label[] lslables = new Label[lsinsn.labels.size()];
        for (int i = 0; i < lslables.length; i++) {
            lslables[i] = lsinsn.labels.get(i).getLabel();
        }
        int[] lskeys = new int[lsinsn.keys.size()];
        for (int i = 0; i < lskeys.length; i++) {
            lskeys[i] = lsinsn.keys.get(i);
        }
        visitLookupSwitchInsn(lsinsn.dflt.getLabel(), lskeys, lslables);
        break;
    case 13:
        MultiANewArrayInsnNode ainsn = (MultiANewArrayInsnNode) insn;
        visitMultiANewArrayInsn(ainsn.desc, ainsn.dims);
        break;
    case 14:
        FrameNode fnode = (FrameNode) insn;
        switch (fnode.type) {
        case -1:
        case 0:
            visitFrame(fnode.type, fnode.local.size(), fnode.local.toArray(), fnode.stack.size(),
                    fnode.stack.toArray());
            break;
        case 1:
            visitFrame(fnode.type, fnode.local.size(), fnode.local.toArray(), 0, null);
            break;
        case 2:
            visitFrame(fnode.type, fnode.local.size(), null, 0, null);
            break;
        case 3:
            visitFrame(fnode.type, 0, null, 0, null);
            break;
        case 4:
            visitFrame(fnode.type, 0, null, 1, fnode.stack.toArray());
        }
        break;
    case 15:
        LineNumberNode lnode = (LineNumberNode) insn;
        visitLineNumber(lnode.line, lnode.start.getLabel());
        break;
    }
}

From source file:org.diorite.inject.controller.TransformerInjectTracker.java

License:Open Source License

private TransformerInjectTracker(Transformer transformer, FieldInsnNode fieldInsnNode, InsnList insnList) {
    this.transformer = transformer;
    this.fieldInsnNode = fieldInsnNode;
    this.isStatic = fieldInsnNode.getOpcode() == PUTSTATIC;
    this.resultNodeList = insnList;
    this.initNodeList = insnList;
}

From source file:org.evosuite.instrumentation.mutation.DeleteField.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from  w  ww .ja  v a2 s.c om*/
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {
    List<Mutation> mutations = new LinkedList<Mutation>();

    FieldInsnNode node = (FieldInsnNode) instruction.getASMNode();
    Type fieldType = Type.getType(node.desc);

    // insert mutation into bytecode with conditional
    InsnList mutation = new InsnList();
    logger.debug("Mutation deletefield for statement " + node.name + node.desc);
    if (node.getOpcode() == Opcodes.GETFIELD) {
        logger.debug("Deleting source of type " + node.owner);
        mutation.add(new InsnNode(Opcodes.POP));
    }
    mutation.add(getDefault(fieldType));
    // insert mutation into pool
    Mutation mutationObject = MutationPool.addMutation(className, methodName,
            NAME + " " + node.name + node.desc, instruction, mutation, getInfectionDistance(node, mutation));

    mutations.add(mutationObject);
    return mutations;
}

From source file:org.evosuite.instrumentation.mutation.DeleteField.java

License:Open Source License

/**
 * <p>/*from  w ww  .  j  a  v  a 2  s.c  o m*/
 * getInfectionDistance
 * </p>
 * 
 * @param original
 *            a {@link org.objectweb.asm.tree.FieldInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(FieldInsnNode original, InsnList mutant) {
    InsnList distance = new InsnList();

    if (original.getOpcode() == Opcodes.GETFIELD)
        distance.add(new InsnNode(Opcodes.DUP)); //make sure to re-load this for GETFIELD

    distance.add(new FieldInsnNode(original.getOpcode(), original.owner, original.name, original.desc));
    Type type = Type.getType(original.desc);

    if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("[")) {
        ReplaceVariable.addReferenceDistanceCheck(distance, type, mutant);
    } else {
        ReplaceVariable.addPrimitiveDistanceCheck(distance, type, mutant);
    }

    return distance;
}