001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.verifier.structurals;
020
021import java.util.Objects;
022
023import org.apache.bcel.Const;
024import org.apache.bcel.Repository;
025import org.apache.bcel.classfile.Constant;
026import org.apache.bcel.classfile.ConstantClass;
027import org.apache.bcel.classfile.ConstantDouble;
028import org.apache.bcel.classfile.ConstantDynamic;
029import org.apache.bcel.classfile.ConstantFieldref;
030import org.apache.bcel.classfile.ConstantFloat;
031import org.apache.bcel.classfile.ConstantInteger;
032import org.apache.bcel.classfile.ConstantLong;
033import org.apache.bcel.classfile.ConstantString;
034import org.apache.bcel.classfile.Field;
035import org.apache.bcel.classfile.JavaClass;
036//CHECKSTYLE:OFF (there are lots of references!)
037import org.apache.bcel.generic.AALOAD;
038import org.apache.bcel.generic.AASTORE;
039import org.apache.bcel.generic.ACONST_NULL;
040import org.apache.bcel.generic.ALOAD;
041import org.apache.bcel.generic.ANEWARRAY;
042import org.apache.bcel.generic.ARETURN;
043import org.apache.bcel.generic.ARRAYLENGTH;
044import org.apache.bcel.generic.ASTORE;
045import org.apache.bcel.generic.ATHROW;
046import org.apache.bcel.generic.ArrayType;
047import org.apache.bcel.generic.BALOAD;
048import org.apache.bcel.generic.BASTORE;
049import org.apache.bcel.generic.BIPUSH;
050import org.apache.bcel.generic.BREAKPOINT;
051import org.apache.bcel.generic.BasicType;
052import org.apache.bcel.generic.CALOAD;
053import org.apache.bcel.generic.CASTORE;
054import org.apache.bcel.generic.CHECKCAST;
055import org.apache.bcel.generic.CPInstruction;
056import org.apache.bcel.generic.ConstantPoolGen;
057import org.apache.bcel.generic.D2F;
058import org.apache.bcel.generic.D2I;
059import org.apache.bcel.generic.D2L;
060import org.apache.bcel.generic.DADD;
061import org.apache.bcel.generic.DALOAD;
062import org.apache.bcel.generic.DASTORE;
063import org.apache.bcel.generic.DCMPG;
064import org.apache.bcel.generic.DCMPL;
065import org.apache.bcel.generic.DCONST;
066import org.apache.bcel.generic.DDIV;
067import org.apache.bcel.generic.DLOAD;
068import org.apache.bcel.generic.DMUL;
069import org.apache.bcel.generic.DNEG;
070import org.apache.bcel.generic.DREM;
071import org.apache.bcel.generic.DRETURN;
072import org.apache.bcel.generic.DSTORE;
073import org.apache.bcel.generic.DSUB;
074import org.apache.bcel.generic.DUP;
075import org.apache.bcel.generic.DUP2;
076import org.apache.bcel.generic.DUP2_X1;
077import org.apache.bcel.generic.DUP2_X2;
078import org.apache.bcel.generic.DUP_X1;
079import org.apache.bcel.generic.DUP_X2;
080import org.apache.bcel.generic.EmptyVisitor;
081import org.apache.bcel.generic.F2D;
082import org.apache.bcel.generic.F2I;
083import org.apache.bcel.generic.F2L;
084import org.apache.bcel.generic.FADD;
085import org.apache.bcel.generic.FALOAD;
086import org.apache.bcel.generic.FASTORE;
087import org.apache.bcel.generic.FCMPG;
088import org.apache.bcel.generic.FCMPL;
089import org.apache.bcel.generic.FCONST;
090import org.apache.bcel.generic.FDIV;
091import org.apache.bcel.generic.FLOAD;
092import org.apache.bcel.generic.FMUL;
093import org.apache.bcel.generic.FNEG;
094import org.apache.bcel.generic.FREM;
095import org.apache.bcel.generic.FRETURN;
096import org.apache.bcel.generic.FSTORE;
097import org.apache.bcel.generic.FSUB;
098import org.apache.bcel.generic.FieldInstruction;
099import org.apache.bcel.generic.GETFIELD;
100import org.apache.bcel.generic.GETSTATIC;
101import org.apache.bcel.generic.GOTO;
102import org.apache.bcel.generic.GOTO_W;
103import org.apache.bcel.generic.I2B;
104import org.apache.bcel.generic.I2C;
105import org.apache.bcel.generic.I2D;
106import org.apache.bcel.generic.I2F;
107import org.apache.bcel.generic.I2L;
108import org.apache.bcel.generic.I2S;
109import org.apache.bcel.generic.IADD;
110import org.apache.bcel.generic.IALOAD;
111import org.apache.bcel.generic.IAND;
112import org.apache.bcel.generic.IASTORE;
113import org.apache.bcel.generic.ICONST;
114import org.apache.bcel.generic.IDIV;
115import org.apache.bcel.generic.IFEQ;
116import org.apache.bcel.generic.IFGE;
117import org.apache.bcel.generic.IFGT;
118import org.apache.bcel.generic.IFLE;
119import org.apache.bcel.generic.IFLT;
120import org.apache.bcel.generic.IFNE;
121import org.apache.bcel.generic.IFNONNULL;
122import org.apache.bcel.generic.IFNULL;
123import org.apache.bcel.generic.IF_ACMPEQ;
124import org.apache.bcel.generic.IF_ACMPNE;
125import org.apache.bcel.generic.IF_ICMPEQ;
126import org.apache.bcel.generic.IF_ICMPGE;
127import org.apache.bcel.generic.IF_ICMPGT;
128import org.apache.bcel.generic.IF_ICMPLE;
129import org.apache.bcel.generic.IF_ICMPLT;
130import org.apache.bcel.generic.IF_ICMPNE;
131import org.apache.bcel.generic.IINC;
132import org.apache.bcel.generic.ILOAD;
133import org.apache.bcel.generic.IMPDEP1;
134import org.apache.bcel.generic.IMPDEP2;
135import org.apache.bcel.generic.IMUL;
136import org.apache.bcel.generic.INEG;
137import org.apache.bcel.generic.INSTANCEOF;
138import org.apache.bcel.generic.INVOKEDYNAMIC;
139import org.apache.bcel.generic.INVOKEINTERFACE;
140import org.apache.bcel.generic.INVOKESPECIAL;
141import org.apache.bcel.generic.INVOKESTATIC;
142import org.apache.bcel.generic.INVOKEVIRTUAL;
143import org.apache.bcel.generic.IOR;
144import org.apache.bcel.generic.IREM;
145import org.apache.bcel.generic.IRETURN;
146import org.apache.bcel.generic.ISHL;
147import org.apache.bcel.generic.ISHR;
148import org.apache.bcel.generic.ISTORE;
149import org.apache.bcel.generic.ISUB;
150import org.apache.bcel.generic.IUSHR;
151import org.apache.bcel.generic.IXOR;
152import org.apache.bcel.generic.Instruction;
153import org.apache.bcel.generic.InvokeInstruction;
154import org.apache.bcel.generic.JSR;
155import org.apache.bcel.generic.JSR_W;
156import org.apache.bcel.generic.L2D;
157import org.apache.bcel.generic.L2F;
158import org.apache.bcel.generic.L2I;
159import org.apache.bcel.generic.LADD;
160import org.apache.bcel.generic.LALOAD;
161import org.apache.bcel.generic.LAND;
162import org.apache.bcel.generic.LASTORE;
163import org.apache.bcel.generic.LCMP;
164import org.apache.bcel.generic.LCONST;
165import org.apache.bcel.generic.LDC;
166import org.apache.bcel.generic.LDC2_W;
167import org.apache.bcel.generic.LDC_W;
168import org.apache.bcel.generic.LDIV;
169import org.apache.bcel.generic.LLOAD;
170import org.apache.bcel.generic.LMUL;
171import org.apache.bcel.generic.LNEG;
172import org.apache.bcel.generic.LOOKUPSWITCH;
173import org.apache.bcel.generic.LOR;
174import org.apache.bcel.generic.LREM;
175import org.apache.bcel.generic.LRETURN;
176import org.apache.bcel.generic.LSHL;
177import org.apache.bcel.generic.LSHR;
178import org.apache.bcel.generic.LSTORE;
179import org.apache.bcel.generic.LSUB;
180import org.apache.bcel.generic.LUSHR;
181import org.apache.bcel.generic.LXOR;
182import org.apache.bcel.generic.LoadClass;
183import org.apache.bcel.generic.LoadInstruction;
184import org.apache.bcel.generic.LocalVariableInstruction;
185import org.apache.bcel.generic.MONITORENTER;
186import org.apache.bcel.generic.MONITOREXIT;
187import org.apache.bcel.generic.MULTIANEWARRAY;
188import org.apache.bcel.generic.MethodGen;
189import org.apache.bcel.generic.NEW;
190import org.apache.bcel.generic.NEWARRAY;
191import org.apache.bcel.generic.NOP;
192import org.apache.bcel.generic.ObjectType;
193import org.apache.bcel.generic.POP;
194import org.apache.bcel.generic.POP2;
195import org.apache.bcel.generic.PUTFIELD;
196import org.apache.bcel.generic.PUTSTATIC;
197import org.apache.bcel.generic.RET;
198import org.apache.bcel.generic.RETURN;
199import org.apache.bcel.generic.ReferenceType;
200import org.apache.bcel.generic.ReturnInstruction;
201import org.apache.bcel.generic.ReturnaddressType;
202import org.apache.bcel.generic.SALOAD;
203import org.apache.bcel.generic.SASTORE;
204import org.apache.bcel.generic.SIPUSH;
205import org.apache.bcel.generic.SWAP;
206import org.apache.bcel.generic.StackConsumer;
207import org.apache.bcel.generic.StackInstruction;
208import org.apache.bcel.generic.StackProducer;
209import org.apache.bcel.generic.StoreInstruction;
210import org.apache.bcel.generic.TABLESWITCH;
211import org.apache.bcel.generic.Type;
212import org.apache.bcel.verifier.VerificationResult;
213import org.apache.bcel.verifier.Verifier;
214import org.apache.bcel.verifier.VerifierFactory;
215import org.apache.bcel.verifier.exc.AssertionViolatedException;
216import org.apache.bcel.verifier.exc.StructuralCodeConstraintException;
217//CHECKSTYLE:ON
218
219/**
220 * A Visitor class testing for valid preconditions of JVM instructions. The instance of this class will throw a
221 * StructuralCodeConstraintException instance if an instruction is visitXXX()ed which has preconditions that are not
222 * satisfied. TODO: Currently, the JVM's behavior concerning monitors (MONITORENTER, MONITOREXIT) is not modeled in
223 * JustIce.
224 *
225 * @see StructuralCodeConstraintException
226 */
227public class InstConstraintVisitor extends EmptyVisitor {
228
229    private static final ObjectType GENERIC_ARRAY = ObjectType.getInstance(GenericArray.class.getName());
230
231    /**
232     * The Execution Frame we're working on.
233     *
234     * @see #setFrame(Frame f)
235     * @see #locals()
236     * @see #stack()
237     */
238    private Frame frame;
239
240    /**
241     * The ConstantPoolGen we're working on.
242     *
243     * @see #setConstantPoolGen(ConstantPoolGen cpg)
244     */
245    private ConstantPoolGen cpg;
246
247    /**
248     * The MethodGen we're working on.
249     *
250     * @see #setMethodGen(MethodGen mg)
251     */
252    private MethodGen mg;
253
254    /**
255     * The constructor. Constructs a new instance of this class.
256     */
257    public InstConstraintVisitor() {
258    }
259
260    /**
261     * Assures arrayref is of ArrayType or NULL; returns true if and only if arrayref is non-NULL.
262     *
263     * @throws StructuralCodeConstraintException Thrown if the above constraint is violated.
264     */
265    private boolean arrayrefOfArrayType(final Instruction o, final Type arrayref) {
266        if (!(arrayref instanceof ArrayType || arrayref.equals(Type.NULL))) {
267            constraintViolated(o, "The 'arrayref' does not refer to an array but is of type " + arrayref + ".");
268        }
269        return arrayref instanceof ArrayType;
270    }
271
272    private void checkArrayElementType(final BasicType expectedElementType, final Instruction o, final Type peek) {
273        if (Objects.equals(peek, Type.NULL)) {
274            return;
275        }
276        if (!(peek instanceof ArrayType)) {
277            constraintViolated(o, "Stack next-to-next-to-top must be of type " + expectedElementType + " but is '" + peek + "'.");
278        }
279        if (!((ArrayType) peek).isElementType(expectedElementType)) {
280            constraintViolated(o, "Stack next-to-next-to-top must be of type " + expectedElementType + " but is '" + peek + "'.");
281        }
282    }
283
284    private void checkTypeConstraint(final Type expected, final Instruction o, final int depth) {
285        final Type peek = stack().peek(depth);
286        if (!Objects.equals(expected, peek)) {
287            constraintViolated(o, "The value at the stack " + depth + " from the top is not of type '" + expected + "', but of type '" + peek + "'.");
288        }
289    }
290
291    private void checkTypeDoubleConstraint(final Instruction o, final int depth) {
292        checkTypeConstraint(Type.DOUBLE, o, depth);
293    }
294
295    private void checkTypeFloatConstraint(final Instruction o, final int depth) {
296        checkTypeConstraint(Type.FLOAT, o, depth);
297    }
298
299    private void checkTypeIntConstraint(final Instruction o, final int depth) {
300        checkTypeConstraint(Type.INT, o, depth);
301    }
302
303    private void checkTypeLongConstraint(final Instruction o, final int depth) {
304        checkTypeConstraint(Type.LONG, o, depth);
305    }
306
307    /**
308     * Always throws {@link StructuralCodeConstraintException} because the instruction violates a structural constraint.
309     *
310     * @throws StructuralCodeConstraintException Thrown unconditionally.
311     */
312    private void constraintViolated(final Instruction violator, final String description) {
313        final String fqClassName = violator.getClass().getName();
314        throw new StructuralCodeConstraintException(
315            "Instruction " + fqClassName.substring(fqClassName.lastIndexOf('.') + 1) + " constraint violated: " + description);
316    }
317
318    private ObjectType getObjectType(final FieldInstruction o) {
319        final ReferenceType rt = o.getReferenceType(cpg);
320        if (rt instanceof ObjectType) {
321            return (ObjectType) rt;
322        }
323        constraintViolated(o, "expecting ObjectType but got " + rt);
324        return null;
325    }
326
327    /**
328     * Assures index is of type INT.
329     *
330     * @throws StructuralCodeConstraintException Thrown if the above constraint is not satisfied.
331     */
332    private void indexOfInt(final Instruction o, final Type index) {
333        if (!index.equals(Type.INT)) {
334            constraintViolated(o, "The 'index' is not of type int but of type " + index + ".");
335        }
336    }
337
338    /**
339     * The LocalVariables we're working on.
340     *
341     * @see #setFrame(Frame f)
342     */
343    private LocalVariables locals() {
344        return frame.getLocals();
345    }
346
347    /**
348     * Ensures an initialized object reference is assignment compatible with the class named by a GETFIELD or PUTFIELD field reference.
349     *
350     * <p>
351     * The null type is permitted because dereferencing null fails at execution time, not during verification. The caller checks for disallowed reference types.
352     * </p>
353     */
354    private void objectrefIsInstanceOfFieldClass(final FieldInstruction o, final Type objectref) throws ClassNotFoundException {
355        if (objectref instanceof ObjectType) {
356            final String theClass = getObjectType(o).getClassName();
357            if (!Repository.instanceOf(((ObjectType) objectref).getClassName(), theClass)) {
358                constraintViolated(o, "The 'objectref' item '" + objectref + "' is not an instance of '" + theClass + "' as expected.");
359            }
360        }
361    }
362
363    /**
364     * Assures the ReferenceType r is initialized (or Type.NULL). Formally, this means (!(r instanceof
365     * UninitializedObjectType)), because there are no uninitialized array types.
366     *
367     * @throws StructuralCodeConstraintException Thrown if the above constraint is not satisfied.
368     */
369    private void referenceTypeIsInitialized(final Instruction o, final ReferenceType r) {
370        if (r instanceof UninitializedObjectType) {
371            constraintViolated(o, "Working on an uninitialized object '" + r + "'.");
372        }
373    }
374
375    /**
376     * Sets the ConstantPoolGen instance needed for constraint checking prior to execution.
377     *
378     * @param cpg The constant pool generator.
379     */
380    public void setConstantPoolGen(final ConstantPoolGen cpg) { // TODO could be package-protected?
381        this.cpg = cpg;
382    }
383
384    /**
385     * Sets the execution frame. Configure the constant pool before using this visitor.
386     *
387     * @param f The frame to set.
388     * @see #setConstantPoolGen(ConstantPoolGen cpg)
389     * @see #setMethodGen(MethodGen mg)
390     */
391    public void setFrame(final Frame f) { // TODO could be package-protected?
392        this.frame = f;
393        // if (singleInstance.mg == null || singleInstance.cpg == null)
394        // throw new AssertionViolatedException("Forgot to set important values first.");
395    }
396
397    /*
398     * "generic"visitXXXX methods where XXXX is an interface therefore, we don't know the order of visiting; but we know these methods are called before the
399     * visitYYYY methods below
400     */
401
402    /**
403     * Sets the MethodGen instance needed for constraint checking prior to execution.
404     *
405     * @param mg The method generator.
406     */
407    public void setMethodGen(final MethodGen mg) {
408        this.mg = mg;
409    }
410
411    /**
412     * The OperandStack we're working on.
413     *
414     * @see #setFrame(Frame f)
415     */
416    private OperandStack stack() {
417        return frame.getStack();
418    }
419
420    /** Assures value is of type INT. */
421    private void valueOfInt(final Instruction o, final Type value) {
422        if (!value.equals(Type.INT)) {
423            constraintViolated(o, "The 'value' is not of type int but of type " + value + ".");
424        }
425    }
426
427    /**
428     * Ensures the specific preconditions of the said instruction.
429     */
430    @Override
431    public void visitAALOAD(final AALOAD o) {
432        final Type arrayref = stack().peek(1);
433        final Type index = stack().peek(0);
434
435        indexOfInt(o, index);
436        if (arrayrefOfArrayType(o, arrayref) && !(((ArrayType) arrayref).getElementType() instanceof ReferenceType)) {
437            constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a ReferenceType but to an array of "
438                + ((ArrayType) arrayref).getElementType() + ".");
439        }
440        // referenceTypeIsInitialized(o, (ReferenceType) (((ArrayType) arrayref).getElementType()));
441    }
442
443    /**
444     * Ensures the specific preconditions of the said instruction.
445     */
446    @Override
447    public void visitAASTORE(final AASTORE o) {
448        final Type arrayref = stack().peek(2);
449        final Type index = stack().peek(1);
450        final Type value = stack().peek(0);
451        indexOfInt(o, index);
452        if (!(value instanceof ReferenceType)) {
453            constraintViolated(o, "The 'value' is not of a ReferenceType but of type " + value + ".");
454        }
455        // } else {
456            // referenceTypeIsInitialized(o, (ReferenceType) value);
457        // }
458        //
459        // Don't bother further with "referenceTypeIsInitialized()", there are no arrays
460        // of an uninitialized object type.
461        if (arrayrefOfArrayType(o, arrayref) && !(((ArrayType) arrayref).getElementType() instanceof ReferenceType)) {
462            constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a ReferenceType but to an array of "
463                + ((ArrayType) arrayref).getElementType() + ".");
464        }
465        // No check for array element assignment compatibility. This is done at runtime.
466    }
467
468    /**
469     * Ensures the specific preconditions of the said instruction.
470     */
471    @Override
472    public void visitACONST_NULL(final ACONST_NULL o) {
473        // Nothing needs to be done here.
474    }
475
476    /**
477     * Ensures the specific preconditions of the said instruction.
478     */
479    @Override
480    public void visitALOAD(final ALOAD o) {
481        // visitLoadInstruction(LoadInstruction) is called before.
482
483        // Nothing else needs to be done here.
484    }
485
486    /**
487     * Ensures the specific preconditions of the said instruction.
488     */
489    @Override
490    public void visitANEWARRAY(final ANEWARRAY o) {
491        final Type peek = stack().peek();
492        if (!peek.equals(Type.INT)) {
493            constraintViolated(o, "The 'count' at the stack top is not of type '" + Type.INT + "' but of type '" + peek + "'.");
494            // The runtime constant pool item at that index must be a symbolic reference to a class,
495            // array, or interface type. See Pass 3a.
496        }
497    }
498
499    /**
500     * Ensures the specific preconditions of the said instruction.
501     */
502    @Override
503    public void visitARETURN(final ARETURN o) {
504        final Type peek = stack().peek();
505        if (!(peek instanceof ReferenceType)) {
506            constraintViolated(o, "The 'objectref' at the stack top is not of a ReferenceType but of type '" + peek + "'.");
507        }
508        referenceTypeIsInitialized(o, (ReferenceType) peek);
509
510        // The check below should already done via visitReturnInstruction(ReturnInstruction), see there.
511        // It cannot be done using Staerk-et-al's "set of object types" instead of a
512        // "wider cast object type", anyway.
513        // if (! objectref.isAssignmentCompatibleWith(mg.getReturnType() )) {
514        // constraintViolated(o, "The 'objectref' type "+objectref+
515        // " at the stack top is not assignment compatible with the return type '"+mg.getReturnType()+"' of the method.");
516        // }
517    }
518
519    /**
520     * Ensures the specific preconditions of the said instruction.
521     */
522    @Override
523    public void visitARRAYLENGTH(final ARRAYLENGTH o) {
524        arrayrefOfArrayType(o, stack().peek(0));
525    }
526
527    /**
528     * Ensures the specific preconditions of the said instruction.
529     */
530    @Override
531    public void visitASTORE(final ASTORE o) {
532        final Type peek = stack().peek();
533        if (!(peek instanceof ReferenceType || peek instanceof ReturnaddressType)) {
534            constraintViolated(o, "The 'objectref' is not of a ReferenceType or of ReturnaddressType but of " + peek + ".");
535        }
536        // if (stack().peek() instanceof ReferenceType) {
537        // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
538        // }
539    }
540
541    /*
542    /* "special"visitXXXX methods for one type of instruction each.
543     */
544
545    /**
546     * Ensures the specific preconditions of the said instruction.
547     */
548    @Override
549    public void visitATHROW(final ATHROW o) {
550        try {
551            // It's stated that 'objectref' must be of a ReferenceType --- but since Throwable is
552            // not derived from an ArrayType, it follows that 'objectref' must be of an ObjectType or Type.NULL.
553            final Type peek = stack().peek();
554            if (!(peek instanceof ObjectType || peek.equals(Type.NULL))) {
555                constraintViolated(o, "The 'objectref' is not of an (initialized) ObjectType but of type " + peek + ".");
556            }
557            // NULL is a subclass of every class, so to speak.
558            if (peek.equals(Type.NULL)) {
559                return;
560            }
561            final ObjectType exc = (ObjectType) peek;
562            final ObjectType throwable = (ObjectType) Type.getType("Ljava/lang/Throwable;");
563            if (!exc.subclassOf(throwable) && !exc.equals(throwable)) {
564                constraintViolated(o, "The 'objectref' is not of class Throwable or of a subclass of Throwable, but of '" + peek + "'.");
565            }
566        } catch (final ClassNotFoundException e) {
567            // FIXME: maybe not the best way to handle this
568            throw new AssertionViolatedException("Missing class: " + e, e);
569        }
570    }
571
572    /**
573     * Ensures the specific preconditions of the said instruction.
574     */
575    @Override
576    public void visitBALOAD(final BALOAD o) {
577        final Type arrayref = stack().peek(1);
578        final Type index = stack().peek(0);
579        indexOfInt(o, index);
580        if (arrayrefOfArrayType(o, arrayref)
581            && !(((ArrayType) arrayref).isElementType(Type.BOOLEAN) || ((ArrayType) arrayref).isElementType(Type.BYTE))) {
582            constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a Type.BYTE or Type.BOOLEAN but to an array of '"
583                + ((ArrayType) arrayref).getElementType() + "'.");
584        }
585    }
586
587    /**
588     * Ensures the specific preconditions of the said instruction.
589     */
590    @Override
591    public void visitBASTORE(final BASTORE o) {
592        final Type arrayref = stack().peek(2);
593        final Type index = stack().peek(1);
594        final Type value = stack().peek(0);
595        indexOfInt(o, index);
596        valueOfInt(o, value);
597        if (arrayrefOfArrayType(o, arrayref)
598            && !(((ArrayType) arrayref).isElementType(Type.BOOLEAN) || ((ArrayType) arrayref).isElementType(Type.BYTE))) {
599            constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a Type.BYTE or Type.BOOLEAN but to an array of '"
600                + ((ArrayType) arrayref).getElementType() + "'.");
601        }
602    }
603
604    /**
605     * Ensures the specific preconditions of the said instruction.
606     */
607    @Override
608    public void visitBIPUSH(final BIPUSH o) {
609        // Nothing to do...
610    }
611
612    /**
613     * Always throws {@link AssertionViolatedException} because this instruction is illegal in this verification pass.
614     *
615     * @throws AssertionViolatedException Thrown unconditionally.
616     */
617    @Override
618    public void visitBREAKPOINT(final BREAKPOINT o) {
619        throw new AssertionViolatedException("This JustIce verification pass should not encounter an illegal instruction such as BREAKPOINT.");
620    }
621
622    /*
623     * "generic" visitYYYY methods where YYYY is a superclass.
624     * therefore, we know the order of visiting; we know
625     * these methods are called after the visitXXXX methods above.
626     */
627
628    /**
629     * Ensures the specific preconditions of the said instruction.
630     */
631    @Override
632    public void visitCALOAD(final CALOAD o) {
633        final Type arrayref = stack().peek(1);
634        final Type index = stack().peek(0);
635        indexOfInt(o, index);
636        if (arrayrefOfArrayType(o, arrayref) && !((ArrayType) arrayref).isElementType(Type.CHAR)) {
637            constraintViolated(o, "The 'arrayref' does not refer to an array with elements of type char but to an array of type "
638                + ((ArrayType) arrayref).getElementType() + ".");
639        }
640    }
641
642    /**
643     * Ensures the specific preconditions of the said instruction.
644     */
645    @Override
646    public void visitCASTORE(final CASTORE o) {
647        final Type arrayref = stack().peek(2);
648        final Type index = stack().peek(1);
649        final Type value = stack().peek(0);
650        indexOfInt(o, index);
651        valueOfInt(o, value);
652        if (arrayrefOfArrayType(o, arrayref) && !((ArrayType) arrayref).isElementType(Type.CHAR)) {
653            constraintViolated(o, "The 'arrayref' does not refer to an array with elements of type char but to an array of type "
654                + ((ArrayType) arrayref).getElementType() + ".");
655        }
656    }
657
658    /**
659     * Ensures the specific preconditions of the said instruction.
660     */
661    @Override
662    public void visitCHECKCAST(final CHECKCAST o) {
663        // The objectref must be of type reference.
664        final Type objectref = stack().peek(0);
665        if (!(objectref instanceof ReferenceType)) {
666            constraintViolated(o, "The 'objectref' is not of a ReferenceType but of type " + objectref + ".");
667        }
668        // else{
669        // referenceTypeIsInitialized(o, (ReferenceType) objectref);
670        // }
671        // The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the
672        // current class (�3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant
673        // pool item at the index must be a symbolic reference to a class, array, or interface type.
674        final Constant c = cpg.getConstant(o.getIndex());
675        if (!(c instanceof ConstantClass)) {
676            constraintViolated(o, "The Constant at 'index' is not a ConstantClass, but '" + c + "'.");
677        }
678    }
679
680    /**
681     * Ensures the general preconditions of a CPInstruction instance.
682     */
683    @Override
684    public void visitCPInstruction(final CPInstruction o) {
685        final int idx = o.getIndex();
686        if (idx < 0 || idx >= cpg.getSize()) {
687            throw new AssertionViolatedException("Huh?! Constant pool index of instruction '" + o + "' illegal? Pass 3a should have checked this.");
688        }
689    }
690
691    /**
692     * Ensures the specific preconditions of the said instruction.
693     */
694    @Override
695    public void visitD2F(final D2F o) {
696        checkTypeDoubleConstraint(o, 0);
697    }
698
699    /**
700     * Ensures the specific preconditions of the said instruction.
701     */
702    @Override
703    public void visitD2I(final D2I o) {
704        checkTypeDoubleConstraint(o, 0);
705    }
706
707    /**
708     * Ensures the specific preconditions of the said instruction.
709     */
710    @Override
711    public void visitD2L(final D2L o) {
712        checkTypeDoubleConstraint(o, 0);
713    }
714
715    /**
716     * Ensures the specific preconditions of the said instruction.
717     */
718    @Override
719    public void visitDADD(final DADD o) {
720        checkTypeDoubleConstraint(o, 0);
721        checkTypeDoubleConstraint(o, 1);
722    }
723
724    /**
725     * Ensures the specific preconditions of the said instruction.
726     */
727    @Override
728    public void visitDALOAD(final DALOAD o) {
729        indexOfInt(o, stack().peek());
730        checkArrayElementType(Type.DOUBLE, o, stack().peek(1));
731    }
732
733    /**
734     * Ensures the specific preconditions of the said instruction.
735     */
736    @Override
737    public void visitDASTORE(final DASTORE o) {
738        checkTypeDoubleConstraint(o, 0);
739        indexOfInt(o, stack().peek(1));
740        checkArrayElementType(Type.DOUBLE, o, stack().peek(2));
741    }
742
743    /**
744     * Ensures the specific preconditions of the said instruction.
745     */
746    @Override
747    public void visitDCMPG(final DCMPG o) {
748        checkTypeDoubleConstraint(o, 0);
749        checkTypeDoubleConstraint(o, 1);
750    }
751
752    /**
753     * Ensures the specific preconditions of the said instruction.
754     */
755    @Override
756    public void visitDCMPL(final DCMPL o) {
757        checkTypeDoubleConstraint(o, 0);
758        checkTypeDoubleConstraint(o, 1);
759    }
760
761    /**
762     * Ensures the specific preconditions of the said instruction.
763     */
764    @Override
765    public void visitDCONST(final DCONST o) {
766        // There's nothing to be done here.
767    }
768
769    /**
770     * Ensures the specific preconditions of the said instruction.
771     */
772    @Override
773    public void visitDDIV(final DDIV o) {
774        checkTypeDoubleConstraint(o, 0);
775        checkTypeDoubleConstraint(o, 1);
776    }
777
778    /**
779     * Ensures the specific preconditions of the said instruction.
780     */
781    @Override
782    public void visitDLOAD(final DLOAD o) {
783        // visitLoadInstruction(LoadInstruction) is called before.
784
785        // Nothing else needs to be done here.
786    }
787
788    /**
789     * Ensures the specific preconditions of the said instruction.
790     */
791    @Override
792    public void visitDMUL(final DMUL o) {
793        checkTypeDoubleConstraint(o, 0);
794        checkTypeDoubleConstraint(o, 1);
795    }
796
797    /**
798     * Ensures the specific preconditions of the said instruction.
799     */
800    @Override
801    public void visitDNEG(final DNEG o) {
802        checkTypeDoubleConstraint(o, 0);
803    }
804
805    /**
806     * Ensures the specific preconditions of the said instruction.
807     */
808    @Override
809    public void visitDREM(final DREM o) {
810        checkTypeDoubleConstraint(o, 0);
811        checkTypeDoubleConstraint(o, 1);
812    }
813
814    /**
815     * Ensures the specific preconditions of the said instruction.
816     */
817    @Override
818    public void visitDRETURN(final DRETURN o) {
819        checkTypeDoubleConstraint(o, 0);
820    }
821
822    /**
823     * Ensures the specific preconditions of the said instruction.
824     */
825    @Override
826    public void visitDSTORE(final DSTORE o) {
827        // visitStoreInstruction(StoreInstruction) is called before.
828
829        // Nothing else needs to be done here.
830    }
831
832    /**
833     * Ensures the specific preconditions of the said instruction.
834     */
835    @Override
836    public void visitDSUB(final DSUB o) {
837        checkTypeDoubleConstraint(o, 0);
838        checkTypeDoubleConstraint(o, 1);
839    }
840
841    /**
842     * Ensures the specific preconditions of the said instruction.
843     */
844    @Override
845    public void visitDUP(final DUP o) {
846        final Type peek = stack().peek();
847        if (peek.getSize() != 1) {
848            constraintViolated(o,
849                "Won't DUP type on stack top '" + peek + "' because it must occupy exactly one slot, not '" + peek.getSize() + "'.");
850        }
851    }
852
853    /**
854     * Ensures the specific preconditions of the said instruction.
855     */
856    @Override
857    public void visitDUP_X1(final DUP_X1 o) {
858        final Type peek = stack().peek();
859        if (peek.getSize() != 1) {
860            constraintViolated(o, "Type on stack top '" + peek + "' should occupy exactly one slot, not '" + peek.getSize() + "'.");
861        }
862        final Type peek1 = stack().peek(1);
863        if (peek1.getSize() != 1) {
864            constraintViolated(o,
865                "Type on stack next-to-top '" + peek1 + "' should occupy exactly one slot, not '" + peek1.getSize() + "'.");
866        }
867    }
868
869    /**
870     * Ensures the specific preconditions of the said instruction.
871     */
872    @Override
873    public void visitDUP_X2(final DUP_X2 o) {
874        final Type peek = stack().peek();
875        if (peek.getSize() != 1) {
876            constraintViolated(o, "Stack top type must be of size 1, but is '" + peek + "' of size '" + peek.getSize() + "'.");
877        }
878        final Type peek1 = stack().peek(1);
879        if (peek1.getSize() == 2) {
880            return; // Form 2, okay.
881        }
882        // stack().peek(1).getSize == 1.
883        final Type peek2 = stack().peek(2);
884        if (peek2.getSize() != 1) {
885            constraintViolated(o, "If stack top's size is 1 and stack next-to-top's size is 1, stack next-to-next-to-top's size must also be 1, but is: '"
886                + peek2 + "' of size '" + peek2.getSize() + "'.");
887        }
888    }
889
890    /**
891     * Ensures the specific preconditions of the said instruction.
892     */
893    @Override
894    public void visitDUP2(final DUP2 o) {
895        final Type peek = stack().peek();
896        if (peek.getSize() == 2) {
897            return; // Form 2, okay.
898        }
899        // stack().peek().getSize() == 1.
900        final Type peek1 = stack().peek(1);
901        if (peek1.getSize() != 1) {
902            constraintViolated(o, "If stack top's size is 1, then stack next-to-top's size must also be 1. But it is '" + peek1 + "' of size '"
903                + peek1.getSize() + "'.");
904        }
905    }
906
907    /**
908     * Ensures the specific preconditions of the said instruction.
909     */
910    @Override
911    public void visitDUP2_X1(final DUP2_X1 o) {
912        final Type peek = stack().peek();
913        final Type peek1 = stack().peek(1);
914        if (peek.getSize() == 2) {
915            if (peek1.getSize() != 1) {
916                constraintViolated(o, "If stack top's size is 2, then stack next-to-top's size must be 1. But it is '" + peek1 + "' of size '"
917                    + peek1.getSize() + "'.");
918            }
919        } else { // stack top is of size 1
920            if (peek1.getSize() != 1) {
921                constraintViolated(o, "If stack top's size is 1, then stack next-to-top's size must also be 1. But it is '" + peek1 + "' of size '"
922                    + peek1.getSize() + "'.");
923            }
924            final Type peek2 = stack().peek(2);
925            if (peek2.getSize() != 1) {
926                constraintViolated(o, "If stack top's size is 1, then stack next-to-next-to-top's size must also be 1. But it is '" + peek2
927                    + "' of size '" + peek2.getSize() + "'.");
928            }
929        }
930    }
931
932    /**
933     * Ensures the specific preconditions of the said instruction.
934     */
935    @Override
936    public void visitDUP2_X2(final DUP2_X2 o) {
937        final Type peek = stack().peek(0);
938        final Type peek1 = stack().peek(1);
939        final Type peek2 = stack().peek(2);
940        if (peek.getSize() == 2) {
941            // stack top size is 2, next-to-top's size is 1
942            if (peek1.getSize() == 2 || peek2.getSize() == 1) {
943                return; // Form 2
944            }
945            constraintViolated(o, "If stack top's size is 2 and stack-next-to-top's size is 1,"
946                + " then stack next-to-next-to-top's size must also be 1. But it is '" + peek2 + "' of size '" + peek2.getSize() + "'.");
947        } else if (peek1.getSize() == 1 && (peek2.getSize() == 2 || stack().peek(3).getSize() == 1)) {
948            return; // Form 1
949        }
950        constraintViolated(o, "The operand sizes on the stack do not match any of the four forms of usage of this instruction.");
951    }
952
953    /**
954     * Ensures the specific preconditions of the said instruction.
955     */
956    @Override
957    public void visitF2D(final F2D o) {
958        checkTypeFloatConstraint(o, 0);
959    }
960
961    /**
962     * Ensures the specific preconditions of the said instruction.
963     */
964    @Override
965    public void visitF2I(final F2I o) {
966        checkTypeFloatConstraint(o, 0);
967    }
968
969    /**
970     * Ensures the specific preconditions of the said instruction.
971     */
972    @Override
973    public void visitF2L(final F2L o) {
974        checkTypeFloatConstraint(o, 0);
975    }
976
977    /**
978     * Ensures the specific preconditions of the said instruction.
979     */
980    @Override
981    public void visitFADD(final FADD o) {
982        checkTypeFloatConstraint(o, 0);
983        checkTypeFloatConstraint(o, 1);
984    }
985
986    /**
987     * Ensures the specific preconditions of the said instruction.
988     */
989    @Override
990    public void visitFALOAD(final FALOAD o) {
991        indexOfInt(o, stack().peek());
992        checkArrayElementType(Type.FLOAT, o, stack().peek(1));
993    }
994
995    /**
996     * Ensures the specific preconditions of the said instruction.
997     */
998    @Override
999    public void visitFASTORE(final FASTORE o) {
1000        checkTypeFloatConstraint(o, 0);
1001        indexOfInt(o, stack().peek(1));
1002        checkArrayElementType(Type.FLOAT, o, stack().peek(2));
1003    }
1004
1005    /**
1006     * Ensures the specific preconditions of the said instruction.
1007     */
1008    @Override
1009    public void visitFCMPG(final FCMPG o) {
1010        checkTypeFloatConstraint(o, 0);
1011        checkTypeFloatConstraint(o, 1);
1012    }
1013
1014    /**
1015     * Ensures the specific preconditions of the said instruction.
1016     */
1017    @Override
1018    public void visitFCMPL(final FCMPL o) {
1019        checkTypeFloatConstraint(o, 0);
1020        checkTypeFloatConstraint(o, 1);
1021    }
1022
1023    /**
1024     * Ensures the specific preconditions of the said instruction.
1025     */
1026    @Override
1027    public void visitFCONST(final FCONST o) {
1028        // nothing to do here.
1029    }
1030
1031    /**
1032     * Ensures the specific preconditions of the said instruction.
1033     */
1034    @Override
1035    public void visitFDIV(final FDIV o) {
1036        checkTypeFloatConstraint(o, 0);
1037        checkTypeFloatConstraint(o, 1);
1038    }
1039
1040    /**
1041     * Ensures the general preconditions of a FieldInstruction instance.
1042     */
1043    @Override
1044    public void visitFieldInstruction(final FieldInstruction o) {
1045        // visitLoadClass(o) has been called before: Every FieldOrMethod
1046        // implements LoadClass.
1047        // visitCPInstruction(o) has been called before.
1048        // A FieldInstruction may be: GETFIELD, GETSTATIC, PUTFIELD, PUTSTATIC
1049        final Constant c = cpg.getConstant(o.getIndex());
1050        if (!(c instanceof ConstantFieldref)) {
1051            constraintViolated(o, "Index '" + o.getIndex() + "' should refer to a CONSTANT_Fieldref_info structure, but refers to '" + c + "'.");
1052        }
1053        // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
1054        final Type t = o.getType(cpg);
1055        if (t instanceof ObjectType) {
1056            final String name = ((ObjectType) t).getClassName();
1057            final Verifier v = VerifierFactory.getVerifier(name);
1058            final VerificationResult vr = v.doPass2();
1059            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
1060                constraintViolated(o, "Class '" + name + "' is referenced, but cannot be loaded and resolved: '" + vr + "'.");
1061            }
1062        }
1063    }
1064
1065    private Field visitFieldInstructionInternals(final FieldInstruction o) throws ClassNotFoundException {
1066        final String fieldName = o.getFieldName(cpg);
1067        final JavaClass jc = Repository.lookupClass(getObjectType(o).getClassName());
1068        final Field f = jc.findField(fieldName, o.getType(cpg));
1069        if (f == null) {
1070            throw new AssertionViolatedException("Field '" + fieldName + "' not found in " + jc.getClassName());
1071        }
1072        final Type value = stack().peek();
1073        final Type t = Type.getType(f.getSignature());
1074        Type shouldBe = t;
1075        if (shouldBe == Type.BOOLEAN || shouldBe == Type.BYTE || shouldBe == Type.CHAR || shouldBe == Type.SHORT) {
1076            shouldBe = Type.INT;
1077        }
1078        if (t instanceof ReferenceType) {
1079            if (value instanceof ReferenceType) {
1080                final ReferenceType rValue = (ReferenceType) value;
1081                referenceTypeIsInitialized(o, rValue);
1082                // TODO: This can possibly only be checked using Staerk-et-al's "set-of-object types", not
1083                // using "wider cast object types" created during verification.
1084                // Comment it out if you encounter problems. See also the analogon at visitPUTFIELD|visitPUTSTATIC.
1085                if (!rValue.isAssignmentCompatibleWith(shouldBe)) {
1086                    constraintViolated(o, "The stack top type '" + value + "' is not assignment compatible with '" + shouldBe + "'.");
1087                }
1088            } else {
1089                constraintViolated(o, "The stack top type '" + value + "' is not of a reference type as expected.");
1090            }
1091        } else if (shouldBe != value) {
1092            constraintViolated(o, "The stack top type '" + value + "' is not of type '" + shouldBe + "' as expected.");
1093        }
1094        return f;
1095    }
1096
1097    /**
1098     * Ensures the specific preconditions of the said instruction.
1099     */
1100    @Override
1101    public void visitFLOAD(final FLOAD o) {
1102        // visitLoadInstruction(LoadInstruction) is called before.
1103
1104        // Nothing else needs to be done here.
1105    }
1106
1107    /**
1108     * Ensures the specific preconditions of the said instruction.
1109     */
1110    @Override
1111    public void visitFMUL(final FMUL o) {
1112        checkTypeFloatConstraint(o, 0);
1113        checkTypeFloatConstraint(o, 1);
1114    }
1115
1116    /**
1117     * Ensures the specific preconditions of the said instruction.
1118     */
1119    @Override
1120    public void visitFNEG(final FNEG o) {
1121        checkTypeFloatConstraint(o, 0);
1122    }
1123
1124    /**
1125     * Ensures the specific preconditions of the said instruction.
1126     */
1127    @Override
1128    public void visitFREM(final FREM o) {
1129        checkTypeFloatConstraint(o, 0);
1130        checkTypeFloatConstraint(o, 1);
1131    }
1132
1133    /**
1134     * Ensures the specific preconditions of the said instruction.
1135     */
1136    @Override
1137    public void visitFRETURN(final FRETURN o) {
1138        checkTypeFloatConstraint(o, 0);
1139    }
1140
1141    /**
1142     * Ensures the specific preconditions of the said instruction.
1143     */
1144    @Override
1145    public void visitFSTORE(final FSTORE o) {
1146        // visitStoreInstruction(StoreInstruction) is called before.
1147
1148        // Nothing else needs to be done here.
1149    }
1150
1151    /**
1152     * Ensures the specific preconditions of the said instruction.
1153     */
1154    @Override
1155    public void visitFSUB(final FSUB o) {
1156        checkTypeFloatConstraint(o, 0);
1157        checkTypeFloatConstraint(o, 1);
1158    }
1159
1160    /**
1161     * Ensures the specific preconditions of the said instruction.
1162     */
1163    @Override
1164    public void visitGETFIELD(final GETFIELD o) {
1165        try {
1166            final Type objectref = stack().peek();
1167            if (!(objectref instanceof ObjectType || objectref == Type.NULL)) {
1168                constraintViolated(o, "Stack top should be an object reference that's not an array reference, but is '" + objectref + "'.");
1169            }
1170            objectrefIsInstanceOfFieldClass(o, objectref);
1171            final String fieldName = o.getFieldName(cpg);
1172
1173            final JavaClass jc = Repository.lookupClass(getObjectType(o).getClassName());
1174            final Field f = jc.findField(fieldName, o.getType(cpg));
1175            if (f == null) {
1176                throw new AssertionViolatedException("Field '" + fieldName + "' not found in " + jc.getClassName());
1177            }
1178            if (f.isProtected()) {
1179                final ObjectType classtype = getObjectType(o);
1180                final ObjectType curr = ObjectType.getInstance(mg.getClassName());
1181                if (classtype.equals(curr) || curr.subclassOf(classtype)) {
1182                    final Type t = stack().peek();
1183                    if (t == Type.NULL) {
1184                        return;
1185                    }
1186                    if (!(t instanceof ObjectType)) {
1187                        constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '" + t + "'.");
1188                    }
1189                    // final ObjectType objreftype = (ObjectType) t;
1190                    // if (!(objreftype.equals(curr) || objreftype.subclassOf(curr))) {
1191                        // TODO: One day move to Staerk-et-al's "Set of object types" instead of "wider" object types
1192                        // created during the verification.
1193                        // "Wider" object types don't allow us to check for things like that below.
1194                        // constraintViolated(o, "The referenced field has the ACC_PROTECTED modifier, "+
1195                        // "and it's a member of the current class or a superclass of the current class."+
1196                        // " However, the referenced object type '"+stack().peek()+
1197                        // "' is not the current class or a subclass of the current class.");
1198                    //}
1199                }
1200            }
1201            // TODO: Could go into Pass 3a.
1202            if (f.isStatic()) {
1203                constraintViolated(o, "Referenced field '" + f + "' is static which it shouldn't be.");
1204            }
1205        } catch (final ClassNotFoundException e) {
1206            // FIXME: maybe not the best way to handle this
1207            throw new AssertionViolatedException("Missing class: " + e, e);
1208        }
1209    }
1210
1211    /**
1212     * Ensures the specific preconditions of the said instruction.
1213     */
1214    @Override
1215    public void visitGETSTATIC(final GETSTATIC o) {
1216        // Field must be static: see Pass 3a.
1217    }
1218
1219    /**
1220     * Ensures the specific preconditions of the said instruction.
1221     */
1222    @Override
1223    public void visitGOTO(final GOTO o) {
1224        // nothing to do here.
1225    }
1226
1227    /**
1228     * Ensures the specific preconditions of the said instruction.
1229     */
1230    @Override
1231    public void visitGOTO_W(final GOTO_W o) {
1232        // nothing to do here.
1233    }
1234
1235    /**
1236     * Ensures the specific preconditions of the said instruction.
1237     */
1238    @Override
1239    public void visitI2B(final I2B o) {
1240        checkTypeIntConstraint(o, 0);
1241    }
1242
1243    /**
1244     * Ensures the specific preconditions of the said instruction.
1245     */
1246    @Override
1247    public void visitI2C(final I2C o) {
1248        checkTypeIntConstraint(o, 0);
1249    }
1250
1251    /**
1252     * Ensures the specific preconditions of the said instruction.
1253     */
1254    @Override
1255    public void visitI2D(final I2D o) {
1256        checkTypeIntConstraint(o, 0);
1257    }
1258
1259    /**
1260     * Ensures the specific preconditions of the said instruction.
1261     */
1262    @Override
1263    public void visitI2F(final I2F o) {
1264        checkTypeIntConstraint(o, 0);
1265    }
1266
1267    /**
1268     * Ensures the specific preconditions of the said instruction.
1269     */
1270    @Override
1271    public void visitI2L(final I2L o) {
1272        checkTypeIntConstraint(o, 0);
1273    }
1274
1275    /**
1276     * Ensures the specific preconditions of the said instruction.
1277     */
1278    @Override
1279    public void visitI2S(final I2S o) {
1280        checkTypeIntConstraint(o, 0);
1281    }
1282
1283    /**
1284     * Ensures the specific preconditions of the said instruction.
1285     */
1286    @Override
1287    public void visitIADD(final IADD o) {
1288        checkTypeIntConstraint(o, 0);
1289        checkTypeIntConstraint(o, 1);
1290    }
1291
1292    /**
1293     * Ensures the specific preconditions of the said instruction.
1294     */
1295    @Override
1296    public void visitIALOAD(final IALOAD o) {
1297        indexOfInt(o, stack().peek());
1298        checkArrayElementType(Type.INT, o, stack().peek(1));
1299    }
1300
1301    /**
1302     * Ensures the specific preconditions of the said instruction.
1303     */
1304    @Override
1305    public void visitIAND(final IAND o) {
1306        checkTypeIntConstraint(o, 0);
1307        checkTypeIntConstraint(o, 1);
1308    }
1309
1310    /**
1311     * Ensures the specific preconditions of the said instruction.
1312     */
1313    @Override
1314    public void visitIASTORE(final IASTORE o) {
1315        checkTypeIntConstraint(o, 0);
1316        indexOfInt(o, stack().peek(1));
1317        checkArrayElementType(Type.INT, o, stack().peek(2));
1318    }
1319
1320    /**
1321     * Ensures the specific preconditions of the said instruction.
1322     */
1323    @Override
1324    public void visitICONST(final ICONST o) {
1325        // nothing to do here.
1326    }
1327
1328    /**
1329     * Ensures the specific preconditions of the said instruction.
1330     */
1331    @Override
1332    public void visitIDIV(final IDIV o) {
1333        checkTypeIntConstraint(o, 0);
1334        checkTypeIntConstraint(o, 1);
1335    }
1336
1337    /**
1338     * Ensures the specific preconditions of the said instruction.
1339     */
1340    @Override
1341    public void visitIF_ACMPEQ(final IF_ACMPEQ o) {
1342        if (!(stack().peek() instanceof ReferenceType)) {
1343            constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '" + stack().peek() + "'.");
1344        }
1345        // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
1346        if (!(stack().peek(1) instanceof ReferenceType)) {
1347            constraintViolated(o, "The value at the stack next-to-top is not of a ReferenceType, but of type '" + stack().peek(1) + "'.");
1348        }
1349        // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek(1)) );
1350    }
1351
1352    /**
1353     * Ensures the specific preconditions of the said instruction.
1354     */
1355    @Override
1356    public void visitIF_ACMPNE(final IF_ACMPNE o) {
1357        if (!(stack().peek() instanceof ReferenceType)) {
1358            constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '" + stack().peek() + "'.");
1359            // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
1360        }
1361        if (!(stack().peek(1) instanceof ReferenceType)) {
1362            constraintViolated(o, "The value at the stack next-to-top is not of a ReferenceType, but of type '" + stack().peek(1) + "'.");
1363            // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek(1)) );
1364        }
1365    }
1366
1367    /**
1368     * Ensures the specific preconditions of the said instruction.
1369     */
1370    @Override
1371    public void visitIF_ICMPEQ(final IF_ICMPEQ o) {
1372        checkTypeIntConstraint(o, 0);
1373        checkTypeIntConstraint(o, 1);
1374    }
1375
1376    /**
1377     * Ensures the specific preconditions of the said instruction.
1378     */
1379    @Override
1380    public void visitIF_ICMPGE(final IF_ICMPGE o) {
1381        checkTypeIntConstraint(o, 0);
1382        checkTypeIntConstraint(o, 1);
1383    }
1384
1385    /**
1386     * Ensures the specific preconditions of the said instruction.
1387     */
1388    @Override
1389    public void visitIF_ICMPGT(final IF_ICMPGT o) {
1390        checkTypeIntConstraint(o, 0);
1391        checkTypeIntConstraint(o, 1);
1392    }
1393
1394    /**
1395     * Ensures the specific preconditions of the said instruction.
1396     */
1397    @Override
1398    public void visitIF_ICMPLE(final IF_ICMPLE o) {
1399        checkTypeIntConstraint(o, 0);
1400        checkTypeIntConstraint(o, 1);
1401    }
1402
1403    /**
1404     * Ensures the specific preconditions of the said instruction.
1405     */
1406    @Override
1407    public void visitIF_ICMPLT(final IF_ICMPLT o) {
1408        checkTypeIntConstraint(o, 0);
1409        checkTypeIntConstraint(o, 1);
1410    }
1411
1412    /**
1413     * Ensures the specific preconditions of the said instruction.
1414     */
1415    @Override
1416    public void visitIF_ICMPNE(final IF_ICMPNE o) {
1417        checkTypeIntConstraint(o, 0);
1418        checkTypeIntConstraint(o, 1);
1419    }
1420
1421    /**
1422     * Ensures the specific preconditions of the said instruction.
1423     */
1424    @Override
1425    public void visitIFEQ(final IFEQ o) {
1426        checkTypeIntConstraint(o, 0);
1427    }
1428
1429    /**
1430     * Ensures the specific preconditions of the said instruction.
1431     */
1432    @Override
1433    public void visitIFGE(final IFGE o) {
1434        checkTypeIntConstraint(o, 0);
1435    }
1436
1437    /**
1438     * Ensures the specific preconditions of the said instruction.
1439     */
1440    @Override
1441    public void visitIFGT(final IFGT o) {
1442        checkTypeIntConstraint(o, 0);
1443    }
1444
1445    /**
1446     * Ensures the specific preconditions of the said instruction.
1447     */
1448    @Override
1449    public void visitIFLE(final IFLE o) {
1450        checkTypeIntConstraint(o, 0);
1451    }
1452
1453    /**
1454     * Ensures the specific preconditions of the said instruction.
1455     */
1456    @Override
1457    public void visitIFLT(final IFLT o) {
1458        checkTypeIntConstraint(o, 0);
1459    }
1460
1461    /**
1462     * Ensures the specific preconditions of the said instruction.
1463     */
1464    @Override
1465    public void visitIFNE(final IFNE o) {
1466        checkTypeIntConstraint(o, 0);
1467    }
1468
1469    /**
1470     * Ensures the specific preconditions of the said instruction.
1471     */
1472    @Override
1473    public void visitIFNONNULL(final IFNONNULL o) {
1474        if (!(stack().peek() instanceof ReferenceType)) {
1475            constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '" + stack().peek() + "'.");
1476        }
1477        referenceTypeIsInitialized(o, (ReferenceType) stack().peek());
1478    }
1479
1480    /**
1481     * Ensures the specific preconditions of the said instruction.
1482     */
1483    @Override
1484    public void visitIFNULL(final IFNULL o) {
1485        if (!(stack().peek() instanceof ReferenceType)) {
1486            constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '" + stack().peek() + "'.");
1487        }
1488        referenceTypeIsInitialized(o, (ReferenceType) stack().peek());
1489    }
1490
1491    /**
1492     * Ensures the specific preconditions of the said instruction.
1493     */
1494    @Override
1495    public void visitIINC(final IINC o) {
1496        // Mhhh. In BCEL, at this time "IINC" is not a LocalVariableInstruction.
1497        if (locals().maxLocals() <= (o.getType(cpg).getSize() == 1 ? o.getIndex() : o.getIndex() + 1)) {
1498            constraintViolated(o, "The 'index' is not a valid index into the local variable array.");
1499        }
1500        indexOfInt(o, locals().get(o.getIndex()));
1501    }
1502
1503    /**
1504     * Ensures the specific preconditions of the said instruction.
1505     */
1506    @Override
1507    public void visitILOAD(final ILOAD o) {
1508        // All done by visitLocalVariableInstruction(), visitLoadInstruction()
1509    }
1510
1511    /**
1512     * Always throws {@link AssertionViolatedException} because this instruction is illegal in this verification pass.
1513     *
1514     * @throws AssertionViolatedException Thrown unconditionally.
1515     */
1516    @Override
1517    public void visitIMPDEP1(final IMPDEP1 o) {
1518        throw new AssertionViolatedException("This JustIce verification pass should not encounter an illegal instruction such as IMPDEP1.");
1519    }
1520
1521    /**
1522     * Always throws {@link AssertionViolatedException} because this instruction is illegal in this verification pass.
1523     *
1524     * @throws AssertionViolatedException Thrown unconditionally.
1525     */
1526    @Override
1527    public void visitIMPDEP2(final IMPDEP2 o) {
1528        throw new AssertionViolatedException("This JustIce verification pass should not encounter an illegal instruction such as IMPDEP2.");
1529    }
1530
1531    /**
1532     * Ensures the specific preconditions of the said instruction.
1533     */
1534    @Override
1535    public void visitIMUL(final IMUL o) {
1536        checkTypeIntConstraint(o, 0);
1537        checkTypeIntConstraint(o, 1);
1538    }
1539
1540    /**
1541     * Ensures the specific preconditions of the said instruction.
1542     */
1543    @Override
1544    public void visitINEG(final INEG o) {
1545        checkTypeIntConstraint(o, 0);
1546    }
1547
1548    /**
1549     * Ensures the specific preconditions of the said instruction.
1550     */
1551    @Override
1552    public void visitINSTANCEOF(final INSTANCEOF o) {
1553        // The objectref must be of type reference.
1554        final Type objectref = stack().peek(0);
1555        if (!(objectref instanceof ReferenceType)) {
1556            constraintViolated(o, "The 'objectref' is not of a ReferenceType but of type " + objectref + ".");
1557        }
1558        // else{
1559        // referenceTypeIsInitialized(o, (ReferenceType) objectref);
1560        // }
1561        // The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the
1562        // current class (�3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant
1563        // pool item at the index must be a symbolic reference to a class, array, or interface type.
1564        final Constant c = cpg.getConstant(o.getIndex());
1565        if (!(c instanceof ConstantClass)) {
1566            constraintViolated(o, "The Constant at 'index' is not a ConstantClass, but '" + c + "'.");
1567        }
1568    }
1569
1570    /**
1571     * Always throws {@link UnsupportedOperationException} because this instruction is not supported.
1572     *
1573     * @throws UnsupportedOperationException Thrown unconditionally.
1574     * @since 6.0
1575     */
1576    @Override
1577    public void visitINVOKEDYNAMIC(final INVOKEDYNAMIC o) {
1578        throw new UnsupportedOperationException("INVOKEDYNAMIC instruction is not supported at this time");
1579    }
1580
1581    /**
1582     * Ensures the general preconditions of an InvokeInstruction instance.
1583     */
1584    @Override
1585    public void visitInvokeInstruction(final InvokeInstruction o) {
1586        // visitLoadClass(o) has been called before: Every FieldOrMethod
1587        // implements LoadClass.
1588        // visitCPInstruction(o) has been called before.
1589        // TODO
1590    }
1591
1592    /**
1593     * Ensures the specific preconditions of the said instruction.
1594     */
1595    @Override
1596    public void visitINVOKEINTERFACE(final INVOKEINTERFACE o) {
1597        // Method is not native, otherwise pass 3 would not happen.
1598        final int count = o.getCount();
1599        if (count == 0) {
1600            constraintViolated(o, "The 'count' argument must not be 0.");
1601        }
1602        // It is a ConstantInterfaceMethodref, Pass 3a made it sure.
1603        // TODO: Do we want to do anything with it?
1604        // ConstantInterfaceMethodref cimr = (ConstantInterfaceMethodref) (cpg.getConstant(o.getIndex()));
1605        // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
1606        final Type t = o.getType(cpg);
1607        if (t instanceof ObjectType) {
1608            final String name = ((ObjectType) t).getClassName();
1609            final Verifier v = VerifierFactory.getVerifier(name);
1610            final VerificationResult vr = v.doPass2();
1611            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
1612                constraintViolated(o, "Class '" + name + "' is referenced, but cannot be loaded and resolved: '" + vr + "'.");
1613            }
1614        }
1615        final Type[] argTypes = o.getArgumentTypes(cpg);
1616        final int argCount = argTypes.length;
1617        for (int i = argCount - 1; i >= 0; i--) {
1618            final Type fromStack = stack().peek(argCount - 1 - i); // 0 to argCount - 1
1619            Type fromDesc = argTypes[i];
1620            if (fromDesc == Type.BOOLEAN || fromDesc == Type.BYTE || fromDesc == Type.CHAR || fromDesc == Type.SHORT) {
1621                fromDesc = Type.INT;
1622            }
1623            if (!fromStack.equals(fromDesc)) {
1624                if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType) {
1625                    final ReferenceType rFromStack = (ReferenceType) fromStack;
1626                    // ReferenceType rFromDesc = (ReferenceType) fromDesc;
1627                    // TODO: This can only be checked when using Staerk-et-al's "set of object types"
1628                    // instead of a "wider cast object type" created during verification.
1629                    // if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ) {
1630                    // constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+
1631                    // "' on the stack (which is not assignment compatible).");
1632                    // }
1633                    referenceTypeIsInitialized(o, rFromStack);
1634                } else {
1635                    constraintViolated(o, "Expecting a '" + fromDesc + "' but found a '" + fromStack + "' on the stack.");
1636                }
1637            }
1638        }
1639        Type objRef = stack().peek(argCount);
1640        if (objRef == Type.NULL) {
1641            return;
1642        }
1643        if (!(objRef instanceof ReferenceType)) {
1644            constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '" + objRef + "'.");
1645        }
1646        referenceTypeIsInitialized(o, (ReferenceType) objRef);
1647        if (!(objRef instanceof ObjectType)) {
1648            if (!(objRef instanceof ArrayType)) { // could be a ReturnaddressType
1649                constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '" + objRef + "'.");
1650            } else {
1651                objRef = GENERIC_ARRAY;
1652            }
1653        }
1654        // String objRefClassName = ((ObjectType) objRef).getClassName();
1655        // String theInterface = o.getClassName(cpg);
1656        // TODO: This can only be checked if we're using Staerk-et-al's "set of object types"
1657        // instead of "wider cast object types" generated during verification.
1658        // if ( ! Repository.implementationOf(objRefClassName, theInterface) ) {
1659        // constraintViolated(o, "The 'objRef' item '" + objRef + "' does not implement '" + theInterface + "' as expected.");
1660        // }
1661        int countedCount = 1; // 1 for the objectref
1662        for (int i = 0; i < argCount; i++) {
1663            countedCount += argTypes[i].getSize();
1664        }
1665        if (count != countedCount) {
1666            constraintViolated(o, "The 'count' argument should probably read '" + countedCount + "' but is '" + count + "'.");
1667        }
1668    }
1669
1670    private int visitInvokeInternals(final InvokeInstruction o) throws ClassNotFoundException {
1671        final Type t = o.getType(cpg);
1672        if (t instanceof ObjectType) {
1673            final String name = ((ObjectType) t).getClassName();
1674            final Verifier v = VerifierFactory.getVerifier(name);
1675            final VerificationResult vr = v.doPass2();
1676            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
1677                constraintViolated(o, "Class '" + name + "' is referenced, but cannot be loaded and resolved: '" + vr + "'.");
1678            }
1679        }
1680        final Type[] argtypes = o.getArgumentTypes(cpg);
1681        final int nargs = argtypes.length;
1682
1683        for (int i = nargs - 1; i >= 0; i--) {
1684            final Type fromStack = stack().peek(nargs - 1 - i); // 0 to nargs-1
1685            Type fromDesc = argtypes[i];
1686            if (fromDesc == Type.BOOLEAN || fromDesc == Type.BYTE || fromDesc == Type.CHAR || fromDesc == Type.SHORT) {
1687                fromDesc = Type.INT;
1688            }
1689            if (!fromStack.equals(fromDesc)) {
1690                if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType) {
1691                    final ReferenceType rFromStack = (ReferenceType) fromStack;
1692                    final ReferenceType rFromDesc = (ReferenceType) fromDesc;
1693                    // TODO: This can possibly only be checked when using Staerk-et-al's "set of object types" instead
1694                    // of a single "wider cast object type" created during verification.
1695                    if (!rFromStack.isAssignmentCompatibleWith(rFromDesc)) {
1696                        constraintViolated(o,
1697                            "Expecting a '" + fromDesc + "' but found a '" + fromStack + "' on the stack (which is not assignment compatible).");
1698                    }
1699                    referenceTypeIsInitialized(o, rFromStack);
1700                } else {
1701                    constraintViolated(o, "Expecting a '" + fromDesc + "' but found a '" + fromStack + "' on the stack.");
1702                }
1703            }
1704        }
1705        return nargs;
1706    }
1707
1708    /**
1709     * Ensures the specific preconditions of the said instruction.
1710     */
1711    @Override
1712    public void visitINVOKESPECIAL(final INVOKESPECIAL o) {
1713        try {
1714            // Don't init an object twice.
1715            if (o.getMethodName(cpg).equals(Const.CONSTRUCTOR_NAME) && !(stack().peek(o.getArgumentTypes(cpg).length) instanceof UninitializedObjectType)) {
1716                constraintViolated(o,
1717                    "Possibly initializing an object twice."
1718                        + " A valid instruction sequence must not have an uninitialized object on the operand stack or in a local variable"
1719                        + " during a backward branch, or in a local variable in code protected by an exception handler."
1720                        + " Please see The Java Virtual Machine Specification, Second Edition, 4.9.4 (pages 147 and 148) for details.");
1721            }
1722            // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
1723            final int nargs = visitInvokeInternals(o);
1724            Type objref = stack().peek(nargs);
1725            if (objref == Type.NULL) {
1726                return;
1727            }
1728            if (!(objref instanceof ReferenceType)) {
1729                constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '" + objref + "'.");
1730            }
1731            String objRefClassName = null;
1732            if (!o.getMethodName(cpg).equals(Const.CONSTRUCTOR_NAME)) {
1733                referenceTypeIsInitialized(o, (ReferenceType) objref);
1734                if (!(objref instanceof ObjectType)) {
1735                    if (!(objref instanceof ArrayType)) { // could be a ReturnaddressType
1736                        constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '" + objref + "'.");
1737                    } else {
1738                        objref = GENERIC_ARRAY;
1739                    }
1740                }
1741
1742                objRefClassName = ((ObjectType) objref).getClassName();
1743            } else {
1744                if (!(objref instanceof UninitializedObjectType)) {
1745                    constraintViolated(o, "Expecting an UninitializedObjectType as 'objectref' on the stack, not a '" + objref
1746                        + "'. Otherwise, you couldn't invoke a method since an array has no methods (not to speak of a return address).");
1747                }
1748                objRefClassName = ((UninitializedObjectType) objref).getInitialized().getClassName();
1749            }
1750
1751            final String theClass = o.getClassName(cpg);
1752            if (!Repository.instanceOf(objRefClassName, theClass)) {
1753                constraintViolated(o, "The 'objref' item '" + objref + "' does not implement '" + theClass + "' as expected.");
1754            }
1755
1756        } catch (final ClassNotFoundException e) {
1757            // FIXME: maybe not the best way to handle this
1758            throw new AssertionViolatedException("Missing class: " + e, e);
1759        }
1760    }
1761
1762    /**
1763     * Ensures the specific preconditions of the said instruction.
1764     */
1765    @Override
1766    public void visitINVOKESTATIC(final INVOKESTATIC o) {
1767        try {
1768            // Method is not native, otherwise pass 3 would not happen.
1769            visitInvokeInternals(o);
1770        } catch (final ClassNotFoundException e) {
1771            // FIXME: maybe not the best way to handle this
1772            throw new AssertionViolatedException("Missing class: " + e, e);
1773        }
1774    }
1775
1776    /**
1777     * Ensures the specific preconditions of the said instruction.
1778     */
1779    @Override
1780    public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) {
1781        try {
1782            // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
1783
1784            final int nargs = visitInvokeInternals(o);
1785            Type objref = stack().peek(nargs);
1786            if (objref == Type.NULL) {
1787                return;
1788            }
1789            if (!(objref instanceof ReferenceType)) {
1790                constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '" + objref + "'.");
1791            }
1792            referenceTypeIsInitialized(o, (ReferenceType) objref);
1793            if (!(objref instanceof ObjectType)) {
1794                if (!(objref instanceof ArrayType)) { // could be a ReturnaddressType
1795                    constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '" + objref + "'.");
1796                } else {
1797                    objref = GENERIC_ARRAY;
1798                }
1799            }
1800            final String objRefClassName = ((ObjectType) objref).getClassName();
1801            final String theClass = o.getClassName(cpg);
1802            if (objref != GENERIC_ARRAY && !Repository.instanceOf(objRefClassName, theClass)) {
1803                constraintViolated(o, "The 'objref' item '" + objref + "' does not implement '" + theClass + "' as expected.");
1804            }
1805        } catch (final ClassNotFoundException e) {
1806            // FIXME: maybe not the best way to handle this
1807            throw new AssertionViolatedException("Missing class: " + e, e);
1808        }
1809    }
1810
1811    /**
1812     * Ensures the specific preconditions of the said instruction.
1813     */
1814    @Override
1815    public void visitIOR(final IOR o) {
1816        checkTypeIntConstraint(o, 0);
1817        checkTypeIntConstraint(o, 1);
1818    }
1819
1820    /**
1821     * Ensures the specific preconditions of the said instruction.
1822     */
1823    @Override
1824    public void visitIREM(final IREM o) {
1825        checkTypeIntConstraint(o, 0);
1826        checkTypeIntConstraint(o, 1);
1827    }
1828
1829    /**
1830     * Ensures the specific preconditions of the said instruction.
1831     */
1832    @Override
1833    public void visitIRETURN(final IRETURN o) {
1834        checkTypeIntConstraint(o, 0);
1835    }
1836
1837    /**
1838     * Ensures the specific preconditions of the said instruction.
1839     */
1840    @Override
1841    public void visitISHL(final ISHL o) {
1842        checkTypeIntConstraint(o, 0);
1843        checkTypeIntConstraint(o, 1);
1844    }
1845
1846    /**
1847     * Ensures the specific preconditions of the said instruction.
1848     */
1849    @Override
1850    public void visitISHR(final ISHR o) {
1851        checkTypeIntConstraint(o, 0);
1852        checkTypeIntConstraint(o, 1);
1853    }
1854
1855    /**
1856     * Ensures the specific preconditions of the said instruction.
1857     */
1858    @Override
1859    public void visitISTORE(final ISTORE o) {
1860        // visitStoreInstruction(StoreInstruction) is called before.
1861        // Nothing else needs to be done here.
1862    }
1863
1864    /**
1865     * Ensures the specific preconditions of the said instruction.
1866     */
1867    @Override
1868    public void visitISUB(final ISUB o) {
1869        checkTypeIntConstraint(o, 0);
1870        checkTypeIntConstraint(o, 1);
1871    }
1872
1873    /**
1874     * Ensures the specific preconditions of the said instruction.
1875     */
1876    @Override
1877    public void visitIUSHR(final IUSHR o) {
1878        checkTypeIntConstraint(o, 0);
1879        checkTypeIntConstraint(o, 1);
1880    }
1881
1882    /**
1883     * Ensures the specific preconditions of the said instruction.
1884     */
1885    @Override
1886    public void visitIXOR(final IXOR o) {
1887        checkTypeIntConstraint(o, 0);
1888        checkTypeIntConstraint(o, 1);
1889    }
1890
1891    /**
1892     * Ensures the specific preconditions of the said instruction.
1893     */
1894    @Override
1895    public void visitJSR(final JSR o) {
1896        // nothing to do here.
1897    }
1898
1899    /**
1900     * Ensures the specific preconditions of the said instruction.
1901     */
1902    @Override
1903    public void visitJSR_W(final JSR_W o) {
1904        // nothing to do here.
1905    }
1906
1907    /**
1908     * Ensures the specific preconditions of the said instruction.
1909     */
1910    @Override
1911    public void visitL2D(final L2D o) {
1912        checkTypeLongConstraint(o, 0);
1913    }
1914
1915    /**
1916     * Ensures the specific preconditions of the said instruction.
1917     */
1918    @Override
1919    public void visitL2F(final L2F o) {
1920        checkTypeLongConstraint(o, 0);
1921    }
1922
1923    /**
1924     * Ensures the specific preconditions of the said instruction.
1925     */
1926    @Override
1927    public void visitL2I(final L2I o) {
1928        checkTypeLongConstraint(o, 0);
1929    }
1930
1931    /**
1932     * Ensures the specific preconditions of the said instruction.
1933     */
1934    @Override
1935    public void visitLADD(final LADD o) {
1936        checkTypeLongConstraint(o, 0);
1937        checkTypeLongConstraint(o, 1);
1938    }
1939
1940    /**
1941     * Ensures the specific preconditions of the said instruction.
1942     */
1943    @Override
1944    public void visitLALOAD(final LALOAD o) {
1945        indexOfInt(o, stack().peek());
1946        checkArrayElementType(Type.LONG, o, stack().peek(1));
1947    }
1948
1949    /**
1950     * Ensures the specific preconditions of the said instruction.
1951     */
1952    @Override
1953    public void visitLAND(final LAND o) {
1954        checkTypeLongConstraint(o, 0);
1955        checkTypeLongConstraint(o, 1);
1956    }
1957
1958    /**
1959     * Ensures the specific preconditions of the said instruction.
1960     */
1961    @Override
1962    public void visitLASTORE(final LASTORE o) {
1963        checkTypeLongConstraint(o, 0);
1964        indexOfInt(o, stack().peek(1));
1965        checkArrayElementType(Type.LONG, o, stack().peek(2));
1966    }
1967
1968    /**
1969     * Ensures the specific preconditions of the said instruction.
1970     */
1971    @Override
1972    public void visitLCMP(final LCMP o) {
1973        checkTypeLongConstraint(o, 0);
1974        checkTypeLongConstraint(o, 1);
1975    }
1976
1977    /**
1978     * Ensures the specific preconditions of the said instruction.
1979     */
1980    @Override
1981    public void visitLCONST(final LCONST o) {
1982        // Nothing to do here.
1983    }
1984
1985    /**
1986     * Ensures the specific preconditions of the said instruction.
1987     */
1988    @Override
1989    public void visitLDC(final LDC o) {
1990        // visitCPInstruction is called first.
1991
1992        final Constant c = cpg.getConstant(o.getIndex());
1993        if (!(c instanceof ConstantInteger
1994                || c instanceof ConstantFloat
1995                || c instanceof ConstantString
1996                || c instanceof ConstantClass
1997                || c instanceof ConstantDynamic)) {
1998            constraintViolated(o,
1999                "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String, a CONSTANT_Class, or a CONSTANT_Dynamic but is '"
2000                        + c + "'.");
2001        }
2002    }
2003
2004    /**
2005     * Ensures the specific preconditions of the said instruction.
2006     *
2007     * @param o The instruction to visit.
2008     */
2009    public void visitLDC_W(final LDC_W o) {
2010        // visitCPInstruction is called first.
2011
2012        final Constant c = cpg.getConstant(o.getIndex());
2013        if (!(c instanceof ConstantInteger || c instanceof ConstantFloat || c instanceof ConstantString || c instanceof ConstantClass)) {
2014            constraintViolated(o,
2015                "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '" + c + "'.");
2016        }
2017    }
2018
2019    /**
2020     * Ensures the specific preconditions of the said instruction.
2021     */
2022    @Override
2023    public void visitLDC2_W(final LDC2_W o) {
2024        // visitCPInstruction is called first.
2025
2026        final Constant c = cpg.getConstant(o.getIndex());
2027        if (!(c instanceof ConstantLong || c instanceof ConstantDouble)) {
2028            constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '" + c + "'.");
2029        }
2030    }
2031
2032    /**
2033     * Ensures the specific preconditions of the said instruction.
2034     */
2035    @Override
2036    public void visitLDIV(final LDIV o) {
2037        checkTypeLongConstraint(o, 0);
2038        checkTypeLongConstraint(o, 1);
2039    }
2040
2041    /**
2042     * Ensures the specific preconditions of the said instruction.
2043     */
2044    @Override
2045    public void visitLLOAD(final LLOAD o) {
2046        // visitLoadInstruction(LoadInstruction) is called before.
2047
2048        // Nothing else needs to be done here.
2049    }
2050
2051    /**
2052     * Ensures the specific preconditions of the said instruction.
2053     */
2054    @Override
2055    public void visitLMUL(final LMUL o) {
2056        checkTypeLongConstraint(o, 0);
2057        checkTypeLongConstraint(o, 1);
2058    }
2059
2060    /**
2061     * Ensures the specific preconditions of the said instruction.
2062     */
2063    @Override
2064    public void visitLNEG(final LNEG o) {
2065        checkTypeLongConstraint(o, 0);
2066    }
2067
2068    /**
2069     * Assures the generic preconditions of a LoadClass instance. The referenced class is loaded and pass2-verified.
2070     */
2071    @Override
2072    public void visitLoadClass(final LoadClass o) {
2073        final ObjectType t = o.getLoadClassType(cpg);
2074        if (t != null) { // null means "no class is loaded"
2075            final Verifier v = VerifierFactory.getVerifier(t.getClassName());
2076            final VerificationResult vr = v.doPass2();
2077            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
2078                constraintViolated((Instruction) o,
2079                    "Class '" + o.getLoadClassType(cpg).getClassName() + "' is referenced, but cannot be loaded and resolved: '" + vr + "'.");
2080            }
2081        }
2082    }
2083
2084    /**
2085     * Assures the generic preconditions of a LoadInstruction instance.
2086     */
2087    @Override
2088    public void visitLoadInstruction(final LoadInstruction o) {
2089        // visitLocalVariableInstruction(o) is called before, because it is more generic.
2090
2091        // LOAD instructions must not read Type.UNKNOWN
2092        if (locals().get(o.getIndex()) == Type.UNKNOWN) {
2093            constraintViolated(o, "Read-Access on local variable " + o.getIndex() + " with unknown content.");
2094        }
2095
2096        // LOAD instructions, two-slot-values at index N must have Type.UNKNOWN
2097        // as a symbol for the higher halve at index N+1
2098        // [suppose some instruction put an int at N+1--- our double at N is defective]
2099        if (o.getType(cpg).getSize() == 2 && locals().get(o.getIndex() + 1) != Type.UNKNOWN) {
2100            constraintViolated(o,
2101                "Reading a two-slot value from local variables " + o.getIndex() + " and " + (o.getIndex() + 1) + " where the latter one is destroyed.");
2102        }
2103
2104        // LOAD instructions must read the correct type.
2105        if (!(o instanceof ALOAD)) {
2106            if (locals().get(o.getIndex()) != o.getType(cpg)) {
2107                constraintViolated(o, "Local Variable type and load instruction type mismatch: Local Variable: '" + locals().get(o.getIndex())
2108                    + "'; Instruction type: '" + o.getType(cpg) + "'.");
2109            }
2110        } else if (!(locals().get(o.getIndex()) instanceof ReferenceType)) {
2111            constraintViolated(o, "Local Variable type and load instruction type mismatch: Local Variable: '" + locals().get(o.getIndex())
2112                + "'; Instruction expects a ReferenceType.");
2113        }
2114        // ALOAD __IS ALLOWED__ to put uninitialized objects onto the stack!
2115        // referenceTypeIsInitialized(o, (ReferenceType) (locals().get(o.getIndex())));
2116
2117        // LOAD instructions must have enough free stack slots.
2118        if (stack().maxStack() - stack().slotsUsed() < o.getType(cpg).getSize()) {
2119            constraintViolated(o, "Not enough free stack slots to load a '" + o.getType(cpg) + "' onto the OperandStack.");
2120        }
2121    }
2122
2123    /**
2124     * Assures the generic preconditions of a LocalVariableInstruction instance. That is, the index of the local variable
2125     * must be valid.
2126     */
2127    @Override
2128    public void visitLocalVariableInstruction(final LocalVariableInstruction o) {
2129        if (locals().maxLocals() <= (o.getType(cpg).getSize() == 1 ? o.getIndex() : o.getIndex() + 1)) {
2130            constraintViolated(o, "The 'index' is not a valid index into the local variable array.");
2131        }
2132    }
2133
2134    /**
2135     * Ensures the specific preconditions of the said instruction.
2136     */
2137    @Override
2138    public void visitLOOKUPSWITCH(final LOOKUPSWITCH o) {
2139        checkTypeIntConstraint(o, 0);
2140        // See also pass 3a.
2141    }
2142
2143    /**
2144     * Ensures the specific preconditions of the said instruction.
2145     */
2146    @Override
2147    public void visitLOR(final LOR o) {
2148        checkTypeLongConstraint(o, 0);
2149        checkTypeLongConstraint(o, 1);
2150    }
2151
2152    /**
2153     * Ensures the specific preconditions of the said instruction.
2154     */
2155    @Override
2156    public void visitLREM(final LREM o) {
2157        checkTypeLongConstraint(o, 0);
2158        checkTypeLongConstraint(o, 1);
2159    }
2160
2161    /**
2162     * Ensures the specific preconditions of the said instruction.
2163     */
2164    @Override
2165    public void visitLRETURN(final LRETURN o) {
2166        checkTypeLongConstraint(o, 0);
2167    }
2168
2169    /**
2170     * Ensures the specific preconditions of the said instruction.
2171     */
2172    @Override
2173    public void visitLSHL(final LSHL o) {
2174        checkTypeIntConstraint(o, 0);
2175        checkTypeLongConstraint(o, 1);
2176    }
2177
2178    /**
2179     * Ensures the specific preconditions of the said instruction.
2180     */
2181    @Override
2182    public void visitLSHR(final LSHR o) {
2183        checkTypeIntConstraint(o, 0);
2184        checkTypeLongConstraint(o, 1);
2185    }
2186
2187    /**
2188     * Ensures the specific preconditions of the said instruction.
2189     */
2190    @Override
2191    public void visitLSTORE(final LSTORE o) {
2192        // visitStoreInstruction(StoreInstruction) is called before.
2193
2194        // Nothing else needs to be done here.
2195    }
2196
2197    /**
2198     * Ensures the specific preconditions of the said instruction.
2199     */
2200    @Override
2201    public void visitLSUB(final LSUB o) {
2202        checkTypeLongConstraint(o, 0);
2203        checkTypeLongConstraint(o, 1);
2204    }
2205
2206    /**
2207     * Ensures the specific preconditions of the said instruction.
2208     */
2209    @Override
2210    public void visitLUSHR(final LUSHR o) {
2211        checkTypeIntConstraint(o, 0);
2212        checkTypeLongConstraint(o, 1);
2213    }
2214
2215    /**
2216     * Ensures the specific preconditions of the said instruction.
2217     */
2218    @Override
2219    public void visitLXOR(final LXOR o) {
2220        checkTypeLongConstraint(o, 0);
2221        checkTypeLongConstraint(o, 1);
2222    }
2223
2224    /**
2225     * Ensures the specific preconditions of the said instruction.
2226     */
2227    @Override
2228    public void visitMONITORENTER(final MONITORENTER o) {
2229        if (!(stack().peek() instanceof ReferenceType)) {
2230            constraintViolated(o, "The stack top should be of a ReferenceType, but is '" + stack().peek() + "'.");
2231        }
2232        // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
2233    }
2234
2235    /**
2236     * Ensures the specific preconditions of the said instruction.
2237     */
2238    @Override
2239    public void visitMONITOREXIT(final MONITOREXIT o) {
2240        if (!(stack().peek() instanceof ReferenceType)) {
2241            constraintViolated(o, "The stack top should be of a ReferenceType, but is '" + stack().peek() + "'.");
2242        }
2243        // referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
2244    }
2245
2246    /**
2247     * Ensures the specific preconditions of the said instruction.
2248     */
2249    @Override
2250    public void visitMULTIANEWARRAY(final MULTIANEWARRAY o) {
2251        final int dimensions = o.getDimensions();
2252        // Dimensions argument is okay: see Pass 3a.
2253        for (int i = 0; i < dimensions; i++) {
2254            if (stack().peek(i) != Type.INT) {
2255                constraintViolated(o, "The '" + dimensions + "' upper stack types should be 'int' but aren't.");
2256            }
2257        }
2258        // The runtime constant pool item at that index must be a symbolic reference to a class,
2259        // array, or interface type. See Pass 3a.
2260    }
2261
2262    /**
2263     * Ensures the specific preconditions of the said instruction.
2264     */
2265    @Override
2266    public void visitNEW(final NEW o) {
2267        // visitCPInstruction(CPInstruction) has been called before.
2268        // visitLoadClass(LoadClass) has been called before.
2269
2270        final Type t = o.getType(cpg);
2271        if (!(t instanceof ReferenceType)) {
2272            throw new AssertionViolatedException("NEW.getType() returning a non-reference type?");
2273        }
2274        if (!(t instanceof ObjectType)) {
2275            constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '" + t + "'.");
2276        }
2277        final ObjectType obj = (ObjectType) t;
2278
2279        // for example: Don't instantiate interfaces
2280        try {
2281            if (!obj.referencesClassExact()) {
2282                constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '" + obj + "'.");
2283            }
2284        } catch (final ClassNotFoundException e) {
2285            constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '" + obj + "', which threw " + e);
2286        }
2287    }
2288
2289    /**
2290     * Ensures the specific preconditions of the said instruction.
2291     */
2292    @Override
2293    public void visitNEWARRAY(final NEWARRAY o) {
2294        checkTypeIntConstraint(o, 0);
2295    }
2296
2297    /**
2298     * Ensures the specific preconditions of the said instruction.
2299     */
2300    @Override
2301    public void visitNOP(final NOP o) {
2302        // nothing is to be done here.
2303    }
2304
2305    /**
2306     * Ensures the specific preconditions of the said instruction.
2307     */
2308    @Override
2309    public void visitPOP(final POP o) {
2310        if (stack().peek().getSize() != 1) {
2311            constraintViolated(o, "Stack top size should be 1 but stack top is '" + stack().peek() + "' of size '" + stack().peek().getSize() + "'.");
2312        }
2313    }
2314
2315    /**
2316     * Ensures the specific preconditions of the said instruction.
2317     */
2318    @Override
2319    public void visitPOP2(final POP2 o) {
2320        if (stack().peek().getSize() != 2) {
2321            constraintViolated(o, "Stack top size should be 2 but stack top is '" + stack().peek() + "' of size '" + stack().peek().getSize() + "'.");
2322        }
2323    }
2324
2325    /**
2326     * Ensures the specific preconditions of the said instruction.
2327     */
2328    @Override
2329    public void visitPUTFIELD(final PUTFIELD o) {
2330        try {
2331
2332            final Type objectref = stack().peek(1);
2333            if (!(objectref instanceof ObjectType || objectref == Type.NULL)) {
2334                constraintViolated(o, "Stack next-to-top should be an object reference that's not an array reference, but is '" + objectref + "'.");
2335            }
2336            objectrefIsInstanceOfFieldClass(o, objectref);
2337
2338            final Field f = visitFieldInstructionInternals(o);
2339
2340            if (f.isProtected()) {
2341                final ObjectType classtype = getObjectType(o);
2342                final ObjectType curr = ObjectType.getInstance(mg.getClassName());
2343
2344                if (classtype.equals(curr) || curr.subclassOf(classtype)) {
2345                    final Type tp = stack().peek(1);
2346                    if (tp == Type.NULL) {
2347                        return;
2348                    }
2349                    if (!(tp instanceof ObjectType)) {
2350                        constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '" + tp + "'.");
2351                    }
2352                    final ObjectType objreftype = (ObjectType) tp;
2353                    if (!(objreftype.equals(curr) || objreftype.subclassOf(curr))) {
2354                        constraintViolated(o,
2355                            "The referenced field has the ACC_PROTECTED modifier, and it's a member of the current class or"
2356                                + " a superclass of the current class. However, the referenced object type '" + stack().peek()
2357                                + "' is not the current class or a subclass of the current class.");
2358                    }
2359                }
2360            }
2361
2362            // TODO: Could go into Pass 3a.
2363            if (f.isStatic()) {
2364                constraintViolated(o, "Referenced field '" + f + "' is static which it shouldn't be.");
2365            }
2366
2367        } catch (final ClassNotFoundException e) {
2368            // FIXME: maybe not the best way to handle this
2369            throw new AssertionViolatedException("Missing class: " + e, e);
2370        }
2371    }
2372
2373    /**
2374     * Ensures the specific preconditions of the said instruction.
2375     */
2376    @Override
2377    public void visitPUTSTATIC(final PUTSTATIC o) {
2378        try {
2379            visitFieldInstructionInternals(o);
2380        } catch (final ClassNotFoundException e) {
2381            // FIXME: maybe not the best way to handle this
2382            throw new AssertionViolatedException("Missing class: " + e, e);
2383        }
2384    }
2385
2386    /**
2387     * Ensures the specific preconditions of the said instruction.
2388     */
2389    @Override
2390    public void visitRET(final RET o) {
2391        if (!(locals().get(o.getIndex()) instanceof ReturnaddressType)) {
2392            constraintViolated(o, "Expecting a ReturnaddressType in local variable " + o.getIndex() + ".");
2393        }
2394        if (locals().get(o.getIndex()) == ReturnaddressType.NO_TARGET) {
2395            throw new AssertionViolatedException("RET expecting a target.");
2396        }
2397        // Other constraints such as non-allowed overlapping subroutines are enforced
2398        // while building the Subroutines data structure.
2399    }
2400
2401    /**
2402     * Ensures the specific preconditions of the said instruction.
2403     */
2404    @Override
2405    public void visitRETURN(final RETURN o) {
2406        if (mg.getName().equals(Const.CONSTRUCTOR_NAME) && Frame.getThis() != null && !mg.getClassName().equals(Type.OBJECT.getClassName())) {
2407            constraintViolated(o, "Leaving a constructor that itself did not call a constructor.");
2408        }
2409    }
2410
2411    /**
2412     * Assures the generic preconditions of a ReturnInstruction instance.
2413     */
2414    @Override
2415    public void visitReturnInstruction(final ReturnInstruction o) {
2416        Type methodType = mg.getType();
2417        if (methodType == Type.BOOLEAN || methodType == Type.BYTE || methodType == Type.SHORT || methodType == Type.CHAR) {
2418            methodType = Type.INT;
2419        }
2420
2421        if (o instanceof RETURN) {
2422            if (methodType == Type.VOID) {
2423                return;
2424            }
2425            constraintViolated(o, "RETURN instruction in non-void method.");
2426        }
2427        if (o instanceof ARETURN) {
2428            if (methodType == Type.VOID) {
2429                constraintViolated(o, "ARETURN instruction in void method.");
2430            }
2431            if (stack().peek() == Type.NULL) {
2432                return;
2433            }
2434            if (!(stack().peek() instanceof ReferenceType)) {
2435                constraintViolated(o, "Reference type expected on top of stack, but is: '" + stack().peek() + "'.");
2436            }
2437            referenceTypeIsInitialized(o, (ReferenceType) stack().peek());
2438            // ReferenceType objectref = (ReferenceType) (stack().peek());
2439            // TODO: This can only be checked if using Staerk-et-al's "set of object types" instead of a
2440            // "wider cast object type" created during verification.
2441            // if (! (objectref.isAssignmentCompatibleWith(mg.getType())) ) {
2442            // constraintViolated(o, "Type on stack top which should be returned is a '"+stack().peek()+
2443            // "' which is not assignment compatible with the return type of this method, '"+mg.getType()+"'.");
2444            // }
2445        } else if (!methodType.equals(stack().peek())) {
2446            constraintViolated(o, "Current method has return type of '" + mg.getType() + "' expecting a '" + methodType
2447                + "' on top of the stack. But stack top is a '" + stack().peek() + "'.");
2448        }
2449    }
2450
2451    /**
2452     * Ensures the specific preconditions of the said instruction.
2453     */
2454    @Override
2455    public void visitSALOAD(final SALOAD o) {
2456        indexOfInt(o, stack().peek());
2457        checkArrayElementType(Type.SHORT, o, stack().peek(1));
2458    }
2459
2460    /**
2461     * Ensures the specific preconditions of the said instruction.
2462     */
2463    @Override
2464    public void visitSASTORE(final SASTORE o) {
2465        checkTypeIntConstraint(o, 0);
2466        indexOfInt(o, stack().peek(1));
2467        checkArrayElementType(Type.SHORT, o, stack().peek(2));
2468    }
2469
2470    /**
2471     * Ensures the specific preconditions of the said instruction.
2472     */
2473    @Override
2474    public void visitSIPUSH(final SIPUSH o) {
2475        // nothing to do here. Generic visitXXX() methods did the trick before.
2476    }
2477
2478    /**
2479     * Ensures the general preconditions of an instruction that accesses the stack. This method is here because BCEL has no
2480     * such superinterface for the stack accessing instructions; and there are funny unexpected exceptions in the semantics
2481     * of the superinterfaces and superclasses provided. For example, SWAP is a StackConsumer, but DUP_X1 is not a StackProducer.
2482     * Therefore, this method is called by all StackProducer, StackConsumer, and StackInstruction instances via their
2483     * visitXXX() method. Unfortunately, as the superclasses and superinterfaces overlap, some instructions cause this
2484     * method to be called two or three times. [TODO: Fix this.]
2485     *
2486     * @see #visitStackConsumer(StackConsumer o)
2487     * @see #visitStackProducer(StackProducer o)
2488     * @see #visitStackInstruction(StackInstruction o)
2489     */
2490    private void visitStackAccessor(final Instruction o) {
2491        final int consume = o.consumeStack(cpg); // Stack values are always consumed first; then produced.
2492        if (consume > stack().slotsUsed()) {
2493            constraintViolated(o, "Cannot consume " + consume + " stack slots: only " + stack().slotsUsed() + " slot(s) left on stack!\nStack:\n" + stack());
2494        }
2495
2496        final int produce = o.produceStack(cpg) - o.consumeStack(cpg); // Stack values are always consumed first; then produced.
2497        if (produce + stack().slotsUsed() > stack().maxStack()) {
2498            constraintViolated(o, "Cannot produce " + produce + " stack slots: only " + (stack().maxStack() - stack().slotsUsed())
2499                + " free stack slot(s) left.\nStack:\n" + stack());
2500        }
2501    }
2502
2503    /**
2504     * Ensures the general preconditions of a StackConsumer instance.
2505     */
2506    @Override
2507    public void visitStackConsumer(final StackConsumer o) {
2508        visitStackAccessor((Instruction) o);
2509    }
2510
2511    /**
2512     * Ensures the general preconditions of a StackInstruction instance.
2513     */
2514    @Override
2515    public void visitStackInstruction(final StackInstruction o) {
2516        visitStackAccessor(o);
2517    }
2518
2519    /**
2520     * Ensures the general preconditions of a StackProducer instance.
2521     */
2522    @Override
2523    public void visitStackProducer(final StackProducer o) {
2524        visitStackAccessor((Instruction) o);
2525    }
2526
2527    /**
2528     * Assures the generic preconditions of a StoreInstruction instance.
2529     */
2530    @Override
2531    public void visitStoreInstruction(final StoreInstruction o) {
2532        // visitLocalVariableInstruction(o) is called before, because it is more generic.
2533
2534        if (stack().isEmpty()) { // Don't bother about 1 or 2 stack slots used. This check is implicitly done below while type checking.
2535            constraintViolated(o, "Cannot STORE: Stack to read from is empty.");
2536        }
2537
2538        if (!(o instanceof ASTORE)) {
2539            if (!(stack().peek() == o.getType(cpg))) { // the other xSTORE types are singletons in BCEL.
2540                constraintViolated(o,
2541                    "Stack top type and store instruction type mismatch: Stack top: '" + stack().peek() + "'; Instruction type: '" + o.getType(cpg) + "'.");
2542            }
2543        } else { // we deal with ASTORE
2544            final Type stacktop = stack().peek();
2545            if (!(stacktop instanceof ReferenceType) && !(stacktop instanceof ReturnaddressType)) {
2546                constraintViolated(o, "Stack top type and store instruction type mismatch: Stack top: '" + stack().peek()
2547                    + "'; Instruction expects a ReferenceType or a ReturnaddressType.");
2548            }
2549            // if (stacktop instanceof ReferenceType) {
2550            // referenceTypeIsInitialized(o, (ReferenceType) stacktop);
2551            // }
2552        }
2553    }
2554
2555    /**
2556     * Ensures the specific preconditions of the said instruction.
2557     */
2558    @Override
2559    public void visitSWAP(final SWAP o) {
2560        if (stack().peek().getSize() != 1) {
2561            constraintViolated(o, "The value at the stack top is not of size '1', but of size '" + stack().peek().getSize() + "'.");
2562        }
2563        if (stack().peek(1).getSize() != 1) {
2564            constraintViolated(o, "The value at the stack next-to-top is not of size '1', but of size '" + stack().peek(1).getSize() + "'.");
2565        }
2566    }
2567
2568    /**
2569     * Ensures the specific preconditions of the said instruction.
2570     */
2571    @Override
2572    public void visitTABLESWITCH(final TABLESWITCH o) {
2573        indexOfInt(o, stack().peek());
2574        // See Pass 3a.
2575    }
2576
2577}