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;
020:
021: /**
022: * MJI NativePeer class for java.lang.System library abstraction
023: */
024: public class JPF_java_lang_System {
025: public static void setErr0(MJIEnv env, int clsObjRef, int streamRef) {
026: env.setStaticIntField("java.lang.System", "err", streamRef);
027: }
028:
029: public static void setIn0(MJIEnv env, int clsObjRef, int streamRef) {
030: env.setStaticIntField("java.lang.System", "in", streamRef);
031: }
032:
033: public static void setOut0(MJIEnv env, int clsObjRef, int streamRef) {
034: env.setStaticIntField("java.lang.System", "out", streamRef);
035: }
036:
037: public static void $clinit(MJIEnv env, int clsObjRef) {
038: env.setStaticReferenceField("java.lang.System", "out", env
039: .newObject("java.io.PrintStream"));
040: env.setStaticReferenceField("java.lang.System", "err", env
041: .newObject("java.io.PrintStream"));
042: }
043:
044: // <2do> - now this baby really needs to be fixed. Imagine what it
045: // does to an app that works with large vectors
046: public static void arraycopy(MJIEnv env, int clsObjRef,
047: int srcArrayRef, int srcIdx, int dstArrayRef, int dstIdx,
048: int length) {
049: int i;
050:
051: if ((srcArrayRef == -1) || (dstArrayRef == -1)) {
052: env.throwException("java.lang.NullPointerException");
053:
054: return;
055: }
056:
057: if (!env.isArray(srcArrayRef) || !env.isArray(dstArrayRef)) {
058: env.throwException("java.lang.ArrayStoreException");
059:
060: return;
061: }
062:
063: int sts = env.getArrayTypeSize(srcArrayRef);
064: int dts = env.getArrayTypeSize(dstArrayRef);
065:
066: if (sts != dts) {
067: // not enough, real thing checks types !!
068: env.throwException("java.lang.ArrayStoreException");
069:
070: return;
071: }
072:
073: // ARGHH <2do> pcm - at some point, we should REALLY do this with a block
074: // operation (saves lots of state, speed if arraycopy is really used)
075: // we could also use the native checks in that case
076: int sl = env.getArrayLength(srcArrayRef);
077: int dl = env.getArrayLength(dstArrayRef);
078:
079: // <2do> - we need detail messages!
080: if ((srcIdx < 0) || ((srcIdx + length) > sl)) {
081: env
082: .throwException("java.lang.ArrayIndexOutOfBoundsException");
083:
084: return;
085: }
086:
087: if ((dstIdx < 0) || ((dstIdx + length) > dl)) {
088: env
089: .throwException("java.lang.ArrayIndexOutOfBoundsException");
090:
091: return;
092: }
093:
094: if (sts == 1) {
095: for (i = 0; i < length; i++, srcIdx++, dstIdx++) {
096: int v = env.getIntArrayElement(srcArrayRef, srcIdx);
097: env.setIntArrayElement(dstArrayRef, dstIdx, v);
098: }
099: } else {
100: for (i = 0; i < length; i++, srcIdx++, dstIdx++) {
101: long v = env.getLongArrayElement(srcArrayRef, srcIdx);
102: env.setLongArrayElement(dstArrayRef, dstIdx, v);
103: }
104: }
105: }
106:
107: // <2do> - this break every app which uses time delta thresholds
108: // (sort of "if ((t2 - t1) > d)"). Ok, we can't deal with
109: // real time, but we could at least give some SystemState dependent
110: // increment
111: public static long currentTimeMillis(MJIEnv env, int clsObjRef) {
112: return 1L;
113: }
114:
115: // <2do> - now this implementation is dangerous if it's called from
116: // a context where we have subsequent unchecked stack frame access
117: // (the bytecodes). We better choose a 'exit' flag instead of
118: // this stack suicide
119: public static void exit(MJIEnv env, int clsObjRef, int ret) {
120: KernelState ks = env.getKernelState();
121: int length = ks.tl.length();
122:
123: for (int i = 0; i < length; i++) {
124: ThreadInfo ti = ks.tl.get(i);
125:
126: while (ti.countStackFrames() > 0) {
127: ti.popFrame();
128: }
129: }
130: }
131:
132: public static void gc(MJIEnv env, int clsObjRef) {
133: env.getSystemState().activateGC();
134: }
135:
136: public static int identityHashCode(MJIEnv env, int clsObjRef,
137: int objref) {
138: return (objref ^ 0xABCD);
139: }
140:
141: public static void registerNatives(MJIEnv env, int clsObjRef) {
142: // ignore
143: }
144:
145: /**
146: * <2do> pcm - replace this with a fixed set of system properties
147: */
148: public static int getProperty__Ljava_lang_String_2(MJIEnv env,
149: int clsObjRef, int keyRef) {
150: int r = MJIEnv.NULL;
151:
152: if (keyRef != MJIEnv.NULL) {
153: String k = env.getStringObject(keyRef);
154: String v = System.getProperty(k);
155: if (v != null) {
156: r = env.newString(v);
157: }
158: }
159:
160: return r;
161: }
162: }
|