001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm.bytecode;
020:
021: import gov.nasa.jpf.Config;
022: import gov.nasa.jpf.JPFException;
023: import gov.nasa.jpf.jvm.ElementInfo;
024: import gov.nasa.jpf.jvm.ThreadInfo;
025: import gov.nasa.jpf.jvm.ClassInfo;
026:
027: import org.apache.bcel.classfile.ConstantPool;
028: import org.apache.bcel.generic.ConstantPoolGen;
029: import org.apache.bcel.generic.Type;
030: import org.apache.bcel.generic.ReferenceType;
031: import gov.nasa.jpf.jvm.FieldInfo;
032: import gov.nasa.jpf.jvm.FieldLockInfo;
033:
034: /**
035: * parent class for PUT/GET FIELD/STATIC insns
036: *
037: * <2do> there is a inheritance level missing to deal with instance/static
038: * fields - w/o the instance/static helper methods we would have to duplicate
039: * code in the getters/setters
040: */
041: public abstract class FieldInstruction extends Instruction implements
042: VariableAccessor {
043: static FieldLockInfo prototype; //optimization measure
044:
045: protected String fname;
046: protected String className;
047: protected String varId;
048:
049: protected FieldInfo fi; // lazy eval, hence not public
050:
051: protected int size;
052: protected boolean isReferenceField;
053:
054: public static void init(Config config) throws Config.Exception {
055: if (config.getBoolean("vm.por")
056: && config.getBoolean("vm.por.sync_detection")) {
057: prototype = (FieldLockInfo) config.getEssentialInstance(
058: "vm.por.fieldlockinfo.class", FieldLockInfo.class);
059: }
060: }
061:
062: public void setPeer(org.apache.bcel.generic.Instruction i,
063: ConstantPool cp) {
064: org.apache.bcel.generic.FieldInstruction fi;
065: ConstantPoolGen cpg;
066:
067: fi = (org.apache.bcel.generic.FieldInstruction) i;
068: cpg = ClassInfo.getConstantPoolGen(cp);
069:
070: fname = fi.getFieldName(cpg);
071: className = fi.getClassName(cpg);
072:
073: Type ft = fi.getFieldType(cpg);
074: if (ft instanceof ReferenceType) {
075: isReferenceField = true;
076: }
077:
078: size = ft.getSize();
079: }
080:
081: public abstract FieldInfo getFieldInfo();
082:
083: public boolean isReferenceField() {
084: return isReferenceField;
085: }
086:
087: public String getId(ElementInfo ei) {
088: // <2do> - OUTCH, should be optimized
089: return (ei.toString() + '.' + fname);
090: }
091:
092: public String getVariableId() {
093: if (varId == null) {
094: varId = className + '.' + fname;
095: }
096: return varId;
097: }
098:
099: /**
100: * is this field supposed to be protected by a lock?
101: * this only gets called if on-the-fly POR is in effect
102: */
103: protected boolean isLockProtected(ElementInfo ei, ThreadInfo ti) {
104: String id = getId(ei);
105: FieldLockInfo flInfo = ei.getFieldLockInfo(id);
106: FieldLockInfo flInfoNext;
107:
108: FieldInfo fi = getFieldInfo(); // so that we make sure it's computed
109:
110: if (flInfo == null) {
111: try {
112: flInfoNext = (FieldLockInfo) prototype.clone();
113: } catch (Exception x) {
114: throw new JPFException(x); // pretty lame error handling here
115: }
116: } else {
117: flInfoNext = flInfo.checkProtection(ei, fi, ti);
118: }
119:
120: if (flInfoNext != flInfo) {
121: ei.setFieldLockInfo(id, flInfoNext);
122: }
123:
124: return flInfoNext.isProtected();
125: }
126: }
|