Ejemplo n.º 1
0
        // Create or return HeapRef corresponding to this string
        public HeapReference createString(ClassFile stringType, ConstantPoolInfo_String stringConst)
        {
            // the reason this method exists is because I suck and can't seem
            // to properly output unicode characters without breaking everything
            string stringVal = stringConst.getValue();
            if (stringMap[stringVal] == null){

                HeapReference stringRef = newInstance(stringType);

                StackFrame stringFrame = new StackFrame(stringType.getConstantPool());

                // Attempt to initialize using the character array
                MethodInfo stringInit = stringType.getMethod("<init>","([C)V");

                if (stringInit == null){
                    throw new Exception("Unable to find init method for string");
                }
                stringFrame.setMethod(stringType,stringInit);
                stringFrame.getLocalVariables()[0] = stringRef;
                stringFrame.getLocalVariables()[1] = stringConst;

                string printableChars = "";

                //stringRef.isUnicode = true; // I suck
                char[] chars = stringVal.ToCharArray();
                for (int i = 0; i < chars.Length; i++){
                    if (chars[i] > 13 && chars[i] < 127){
                        printableChars += chars[i];
                    }
                }

                if (log.IsDebugEnabled) log.DebugFormat("stringval1:{0}",printableChars);
                //if (log.IsDebugEnabled) log.DebugFormat(stringVal);
            //	if (log.IsDebugEnabled) log.DebugFormat("U:" + stringRef.isUnicode);
                stringType.execute("<init>","([C)V",stringFrame);
                stringMap.Add(stringVal,stringRef);
                reverseStringMap.Add(stringRef,stringVal);
            }
            return (HeapReference) stringMap[stringVal];
        }
Ejemplo n.º 2
0
        /**
         * int write(int fd,ByteBuffer src)
         */
        public static void write(StackFrame frame)
        {
            int fd = (int)frame.getLocalVariables()[1];
            Heap.HeapReference byteBufRef = (Heap.HeapReference) frame.getLocalVariables()[2];

            if (fd != 2){
                throw new ToyVMException("Can only handle stdout(2), have " + fd, frame);
            }

            // need to get the actual byte array
            StackFrame frame2 = new StackFrame(frame);

            ClassFile byteBufferClass = byteBufRef.type;
            string getterType = "()[B";
            string getterName = "array";
            MethodInfo method = byteBufferClass.getMethod(getterName,getterType);

            if (method == null){
                foreach (MethodInfo m in byteBufferClass.getMethods()){
                    if (log.IsDebugEnabled) log.DebugFormat(m.ToString());
                }
                throw new ToyVMException("Unable to find " + getterName + getterType,frame);
            }

            frame2.setMethod(byteBufferClass,method);

            frame2.getLocalVariables()[0] = byteBufRef;
            byteBufferClass.execute(getterName,getterType,frame2);

            Heap.HeapReference byteArrRef = (Heap.HeapReference) frame.popOperand();
            byte[] bytes = (byte[]) byteArrRef.obj;

            for (int i = 0; i < bytes.Length; i++){
                Console.Write((char)bytes[i]);
            }

            frame.getPrev().pushOperand(bytes.Length);
        }
Ejemplo n.º 3
0
        public HeapReference createString(ClassFile stringType, string stringVal)
        {
            if (stringMap[stringVal] == null){

                HeapReference stringRef = newInstance(stringType);

                StackFrame stringFrame = new StackFrame(stringType.getConstantPool());

                // Attempt to initialize using the character array
                MethodInfo stringInit = stringType.getMethod("<init>","([C)V");

                if (stringInit == null){
                    throw new Exception("Unable to find init method for string");
                }
                stringFrame.setMethod(stringType,stringInit);
                stringFrame.getLocalVariables()[0] = stringRef;
                stringFrame.getLocalVariables()[1] = stringVal;
            //	stringRef.isUnicode = containsUnicode(stringVal);
                char[] chars = stringVal.ToCharArray();
                    string printableChars = "";

                //stringRef.isUnicode = true; // I suck

                for (int i = 0; i < chars.Length; i++){
                    if (chars[i] > 13 && chars[i] < 127){
                        printableChars += chars[i];
                    }
                }

                if (log.IsDebugEnabled) log.DebugFormat("stringval2:{0}",printableChars);

             	stringType.execute("<init>","([C)V",stringFrame);
                stringMap.Add(stringVal,stringRef);
                reverseStringMap.Add(stringRef,stringVal);
            }
            return (HeapReference) stringMap[stringVal];
        }
Ejemplo n.º 4
0
        public void start(string className)
        {
            try
            {
                EventLogger logger = new EventLogger("/home/jdewald/ToyVMEvents.log");
                CountingHandler counter = new CountingHandler("/home/jdewald/ToyVMCounter");

                ToyVMClassLoader.ClassLoadedEvent += logger.handleClassLoaded;
                MethodInfo.EnterEvent += counter.HandleMethodInfoEnterEvent;
                MethodInfo.ByteCodeEnterEvent += counter.HandleByteCodeEnterEvent;

                ClassFile mainClass = ToyVMClassLoader.loadClass(className);
                if (log.IsDebugEnabled) log.DebugFormat("Loaded");
                if (log.IsDebugEnabled) log.DebugFormat(mainClass.GetName() + " was loaded");
                //classCache[mainClass.GetName()] = mainClass;

                if (log.IsDebugEnabled) log.DebugFormat(mainClass.GetName() + " stored in cache");
                // initialize from the root of the class tree
                string superClassName = mainClass.GetSuperClassName();

                if (log.IsDebugEnabled) log.DebugFormat("Super class is {0}",superClassName);
                ClassFile superClass = ToyVMClassLoader.loadClass(superClassName);
                if (superClass == null)
                {
                    throw new Exception("ClassLoader did not load super class!");
                    //superClass = loadClass(superClassName);
                }

                // Execute main(String[])
                //MethodInfo info = mainClass.getMethod("main","[Ljava/lang/String;V");
                StackFrame frame = new StackFrame(mainClass.getConstantPool());
                //frame.pushOperand(mainClass);
            //				info.execute(frame);
                frame.setMethod(mainClass,mainClass.getMethod("main","([Ljava/lang/String;)V"));
                mainClass.execute("main","([Ljava/lang/String;)V",frame);
                /*if (info == null) {
                    throw new Exception("Unable to find 'main' method of class " + className);
                }
                else {
                    StackFrame frame = new StackFrame(mainClass.getConstantPool());
                    frame.pushOperand(mainClass);
                    info.execute(frame);
                }
                */
                counter.Complete();

            }
            catch (ToyVMException te){
                if (te.InnerException == null){
                    Console.WriteLine("{0}\n{1}",te.Message,te.getStackFrame());
                }
                else {
                    Console.WriteLine("{0}:{1}\n{2}",te.InnerException,te.Message,te.getStackFrame());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + ":" + e.StackTrace);
                //throw e;
            }
        }
Ejemplo n.º 5
0
        /**
         * Sets the core default system properties
         */
        public static void preInit(StackFrame sf)
        {
            Heap.HeapReference propsObj = (Heap.HeapReference) sf.getLocalVariables()[0];
            ClassFile stringType = ToyVMClassLoader.loadClass("java/lang/String");
            ClassFile propsClass = (ClassFile)(propsObj.type);
            string setterType = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
            string setterName = "setProperty";
            MethodInfo method = propsClass.getMethod(setterName,setterType);

            if (method == null){
                foreach (MethodInfo m in propsClass.getMethods()){
                    if (log.IsDebugEnabled) log.DebugFormat(m.ToString());
                }
                throw new ToyVMException("Unable to find " + setterName + setterType,sf);
            }
            StackFrame frame = new StackFrame(sf);
            frame.setMethod(propsClass,method);

            Heap.HeapReference prop = Heap.GetInstance().createString(stringType,"java.version");
            Heap.HeapReference val = Heap.GetInstance().createString(stringType,"5");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.boot.class.path");
            val = Heap.GetInstance().createString(stringType,"/home/jdewald/ToyVM/ToyVM/openjdk");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vendor");
            val = Heap.GetInstance().createString(stringType,"jdewald");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vendor.url");
            val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.vendor");
            val = Heap.GetInstance().createString(stringType,"jdewald");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.name");
            val = Heap.GetInstance().createString(stringType,"ToyVM");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.vendor.url");
            val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.specification.version");
            val = Heap.GetInstance().createString(stringType,"2");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.specification.vendor");
            val = Heap.GetInstance().createString(stringType,"Sun");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.specification.url");
            val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.vm.specification.version");
            val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.class.version");
            val = Heap.GetInstance().createString(stringType,"49");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.home");
            val = Heap.GetInstance().createString(stringType,"/home/jdewald/ToyVM/ToyVM");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.class.path");
            val = Heap.GetInstance().createString(stringType,"/home/jdewald/ToyVM/ToyVM/openjdk");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.library.path");
            val = Heap.GetInstance().createString(stringType,"/home/jdewald/nativelibs");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"java.io.tmpdir");
            val = Heap.GetInstance().createString(stringType,"/tmp");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"os.name");
            val = Heap.GetInstance().createString(stringType,"Linux");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"os.arch");
            val = Heap.GetInstance().createString(stringType,"i386");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"file.separator");
            val = Heap.GetInstance().createString(stringType,"/");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"path.separator");
            val = Heap.GetInstance().createString(stringType,":");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"line.separator");
            val = Heap.GetInstance().createString(stringType,"\n");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"user.name");
            val = Heap.GetInstance().createString(stringType,"jdewald");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"user.home");
            val = Heap.GetInstance().createString(stringType,"/home/jdewald");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"user.dir");
            val = Heap.GetInstance().createString(stringType,Environment.CurrentDirectory);
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);

            prop = Heap.GetInstance().createString(stringType,"gnu.cpu.endian");
            val = Heap.GetInstance().createString(stringType,"little");
            frame.getLocalVariables()[0] = propsObj;
            frame.getLocalVariables()[1] = prop;
            frame.getLocalVariables()[2] = val;
            propsClass.execute(setterName,setterType,frame);
        }