Exemple #1
0
    def init_default_thread(self):
        '''Create initial thread group and thread.
        Both are java's objects
        '''
        tg_klass = self.get_class("java/lang/ThreadGroup")
        t_klass = self.get_class("java/lang/Thread")
        tg = tg_klass.get_instance(self)
        t = t_klass.get_instance(self)

        tg.fields["name"] = self.make_heap_string("system")
        tg.fields["maxPriority"] = 10
        t.fields["priority"] = 5
        t.fields["name"] = self.make_heap_string("system-main")
        t.fields["blockerLock"] = self.add_to_heap(
            self.get_class("java/lang/Object").get_instance(self))

        tg_ref = self.add_to_heap(tg)
        t_ref = self.add_to_heap(t)
        t.fields["group"] = tg_ref

        # Add thread to threadgroup; call byte code of void add(Thread)
        pvm_thread = Thread(self, t_ref)
        pvm_thread.is_alive = True
        method = tg_klass.find_method("add", "(Ljava/lang/Thread;)V")
        args = [None]*method[1]
        args[0] = tg_ref
        args[1] = t_ref
        frame = Frame(pvm_thread, tg_klass, method, args, "system tg init")
        pvm_thread.frame_stack.append(frame)
        self.run_thread(pvm_thread)

        self.top_group = tg
        self.top_thread = t
        self.top_group_ref = tg_ref
        self.top_thread_ref = t_ref
Exemple #2
0
def java_lang_System_identityHashCode__Ljava_lang_Object__I(frame, args):
    ref = args[0]
    if ref is None:
        frame.stack.append(0)
        return
    assert type(ref) is tuple
    assert ref[0] == "ref"

    o = frame.vm.heap[ref[1]]
    klass = o.java_class
    method = klass.find_method("hashCode", "()I")

    if method[0] & 0x0100 > 0:
        # assuming native call to object's hashCode, get heap id
        frame.stack.append(ref[1])
        return

    pvm_thread = Thread(frame.vm, frame.vm.top_thread_ref)
    pvm_thread.is_alive = True
    m_args = [None] * method[1]
    m_args[0] = ref
    sub = Frame(pvm_thread, klass, method, m_args, "call get hashCode")
    pvm_thread.frame_stack.append(sub)
    frame.vm.run_thread(pvm_thread)
    assert sub.has_result
    frame.stack.append(sub.ret)
Exemple #3
0
def java_lang_System_identityHashCode__Ljava_lang_Object__I(frame, args):
    ref = args[0]
    if ref is None:
        frame.stack.append(0)
        return
    assert type(ref) is tuple
    assert ref[0] == "ref"

    o = frame.vm.heap[ref[1]]
    klass = o.java_class
    method = klass.find_method("hashCode", "()I")

    if method[0] & 0x0100 > 0:
        # assuming native call to object's hashCode, get heap id
        frame.stack.append(ref[1])
        return

    pvm_thread = Thread(frame.vm, frame.vm.top_thread_ref)
    pvm_thread.is_alive = True
    m_args = [None]*method[1]
    m_args[0] = ref
    sub = Frame(pvm_thread, klass, method, m_args,
                "call get hashCode")
    pvm_thread.frame_stack.append(sub)
    frame.vm.run_thread(pvm_thread)
    assert sub.has_result
    frame.stack.append(sub.ret)
Exemple #4
0
    def initialize_vm(self, main_klass, method, m_args):
        """
        Run initialized vm with specific method of a class.
        This is class entered from command line. Method is looked up
        void main(String args[]).
        For more details see methods.txt in docs.
        :param main_klass:
        :param method:
        :param m_args:
        :return:
        """
        t_klass = self.get_class("java/lang/Thread")
        t = t_klass.get_instance(self)
        t.fields["priority"] = 5
        t.fields["name"] = self.make_heap_string("main")
        t.fields["blockerLock"] = self.add_to_heap(
            self.get_class("java/lang/Object").get_instance(self))
        t_ref = self.add_to_heap(t)
        t.fields["group"] = self.top_group_ref

        pvm_thread = Thread(self, t_ref)
        pvm_thread.is_alive = True
        frame = Frame(pvm_thread, main_klass, method, m_args, "main")
        pvm_thread.frame_stack.append(frame)

        self.add_thread(pvm_thread)
        logger.debug("run thread pool")
Exemple #5
0
    def init_default_thread(self):
        '''Create initial thread group and thread.
        Both are java's objects
        '''
        tg_klass = self.get_class("java/lang/ThreadGroup")
        t_klass = self.get_class("java/lang/Thread")
        tg = tg_klass.get_instance(self)
        t = t_klass.get_instance(self)

        tg.fields["name"] = self.make_heap_string("system")
        tg.fields["maxPriority"] = 10
        t.fields["priority"] = 5
        t.fields["name"] = self.make_heap_string("system-main")
        t.fields["blockerLock"] = self.add_to_heap(
            self.get_class("java/lang/Object").get_instance(self))

        tg_ref = self.add_to_heap(tg)
        t_ref = self.add_to_heap(t)
        t.fields["group"] = tg_ref

        # Add thread to threadgroup; call byte code of void add(Thread)
        pvm_thread = Thread(self, t_ref)
        pvm_thread.is_alive = True
        method = tg_klass.find_method("add", "(Ljava/lang/Thread;)V")
        args = [None] * method[1]
        args[0] = tg_ref
        args[1] = t_ref
        frame = Frame(pvm_thread, tg_klass, method, args, "system tg init")
        pvm_thread.frame_stack.append(frame)
        self.run_thread(pvm_thread)

        self.top_group = tg
        self.top_thread = t
        self.top_group_ref = tg_ref
        self.top_thread_ref = t_ref
Exemple #6
0
def java_lang_Thread_start0___V(frame, args):
    '''Create new thread with one's void run()
    see thread.txt for details
    '''
    t_ref = args[0]
    o = frame.vm.heap[t_ref[1]]
    run = o.java_class.find_method("run", "()V")
    assert run is not None

    pvm_thread = Thread(frame.vm, t_ref)
    pvm_thread.is_alive = True
    m_args = [None] * run[1]
    m_args[0] = t_ref
    sub = Frame(pvm_thread, o.java_class, run, m_args, "Thread")
    pvm_thread.frame_stack.append(sub)
    frame.vm.add_thread(pvm_thread)
Exemple #7
0
    def __init__(self, _class_path="."):
        logger.debug("Creating VM")

        # Major memory structures
        self.perm_gen = {}
        self.heap = {}
        self.heap_next_id = 1
        # todo clean up self.cache_klass_klass = {}
        self.global_strings = {}

        # Handle for linked list of threads
        self.threads_queue = deque()
        self.threads = []
        self.non_daemons = 0

        self.top_group = None
        self.top_thread = None
        self.top_group_ref = None
        self.top_thread_ref = None

        self.class_path = read_class_path(_class_path)

        self.init_default_thread()

        # Load System and init major fields
        system_class = self.get_class("java/lang/System")

        # Set System.props to vm owned object
        system_class.static_fields["props"][1] = (
            "vm_ref", VM_OBJECTS["System.Properties"])

        # STDout initialization using vm owned object
        ps_class = self.get_class("java/io/PrintStream")
        ps_object = ps_class.get_instance(self)
        ps_ref = self.add_to_heap(ps_object)
        method = ps_class.find_method("<init>", "(Ljava/io/OutputStream;)V")
        std_out_ref = ("vm_ref", VM_OBJECTS["Stdout.OutputStream"])
        thread = Thread(self, None)
        frame = Frame(thread, ps_class, method, [ps_ref, std_out_ref],
                      "PrintStream init")
        thread.frame_stack.append(frame)

        logger.debug("Run PrintStream init")
        self.run_thread(thread)  # Run exclusive thread
        # <--thread added to the VM at this point
        system_class.static_fields["out"][1] = ps_ref

        system_class.static_fields["in"][1] = \
            ("vm_ref", VM_OBJECTS["Stdin.InputputStream"])

        # Additional parameters
        system_class.static_fields["lineSeparator"][1] = \
            self.make_heap_string("\n")

        # Load additional classes to speed up booting
        self.touch_classes()

        self.initialized = True

        logger.debug("VM created")
Exemple #8
0
    def run_static_constructor(self, java_class):
        '''Static constructor is run for every class loaded by class loader.
        It is executed in thread exclusive mode.
        '''
        logger.debug("Running static constructor for %s", java_class.this_name)
        method = java_class.static_contructor()
        if method is None:
            logger.debug("No static constructor for %s", java_class.this_name)
            return
        pvm_thread = Thread(self, self.top_thread_ref)
        pvm_thread.is_alive = True
        frame = Frame(pvm_thread, java_class, method, [None] * method[1],
                      "<clinit:{0}>".format(java_class.this_name))
        pvm_thread.frame_stack.append(frame)
        self.run_thread(pvm_thread)

        logger.debug("Finished with static constructor for %s",
                     java_class.this_name)
Exemple #9
0
    def run_static_constructor(self, java_class):
        '''Static constructor is run for every class loaded by class loader.
        It is executed in thread exclusive mode.
        '''
        logger.debug("Running static constructor for %s",
                     java_class.this_name)
        method = java_class.static_contructor()
        if method is None:
            logger.debug("No static constructor for %s",
                         java_class.this_name)
            return
        pvm_thread = Thread(self, self.top_thread_ref)
        pvm_thread.is_alive = True
        frame = Frame(pvm_thread, java_class, method, [None]*method[1],
                      "<clinit:{0}>".format(java_class.this_name))
        pvm_thread.frame_stack.append(frame)
        self.run_thread(pvm_thread)

        logger.debug("Finished with static constructor for %s",
                     java_class.this_name)
Exemple #10
0
    def raise_exception(self, frame, name):
        '''Util method to raise an exception based on name.
        e.g. java.lang.NullPointerException

        Exception is created on heap and throw op is called
        '''
        ex_klass = self.get_class(name)
        ex = ex_klass.get_instance(self)
        ref = self.add_to_heap(ex)

        method = ex_klass.find_method("<init>", "()V")
        m_args = [None]*method[1]
        m_args[0] = ref

        pvm_thread = Thread(self, None)
        pvm_thread.is_alive = True
        sub = Frame(pvm_thread, ex_klass, method, m_args, "exinit")
        pvm_thread.frame_stack.append(sub)
        self.run_thread(pvm_thread)

        frame.stack.append(ref)
        op_0xbf(frame)
Exemple #11
0
    def raise_exception(self, frame, name):
        '''Util method to raise an exception based on name.
        e.g. java.lang.NullPointerException

        Exception is created on heap and throw op is called
        '''
        ex_klass = self.get_class(name)
        ex = ex_klass.get_instance(self)
        ref = self.add_to_heap(ex)

        method = ex_klass.find_method("<init>", "()V")
        m_args = [None] * method[1]
        m_args[0] = ref

        pvm_thread = Thread(self, None)
        pvm_thread.is_alive = True
        sub = Frame(pvm_thread, ex_klass, method, m_args, "exinit")
        pvm_thread.frame_stack.append(sub)
        self.run_thread(pvm_thread)

        frame.stack.append(ref)
        get_operation('0xbf')(frame)
Exemple #12
0
    def run_vm(self, main_klass, method, m_args):
        '''Run initialized vm with specific method of a class.
        This is class entered from command line. Method is looked up
        void main(String args[]).
        For more details see methods.txt in docs.
        '''
        t_klass = self.get_class("java/lang/Thread")
        t = t_klass.get_instance(self)
        t.fields["priority"] = 5
        t.fields["name"] = self.make_heap_string("main")
        t.fields["blockerLock"] = self.add_to_heap(
            self.get_class("java/lang/Object").get_instance(self))
        t_ref = self.add_to_heap(t)
        t.fields["group"] = self.top_group_ref

        pvm_thread = Thread(self, t_ref)
        pvm_thread.is_alive = True
        frame = Frame(pvm_thread, main_klass, method, m_args, "main")
        pvm_thread.frame_stack.append(frame)

        self.add_thread(pvm_thread)
        logger.debug("run thread pool")
        self.run_thread_pool()
def sun_reflect_NativeConstructorAccessorImpl_newInstance0__Ljava_lang_reflect_Constructor__Ljava_lang_Object__Ljava_lang_Object_(frame, args):
    '''Create instance of a class, with constructor call'''
    ref = args[0]
    params = args[1]
    assert type(ref) is tuple and ref[0] == "ref"
    assert params is None or len(params) == 0
    o = frame.vm.heap[ref[1]]
    klass_klass = frame.vm.heap[o.fields["clazz"][1]]
    clazz = frame.vm.get_class(klass_klass.fields["@CLASS_NAME"])
    signature = str_to_string(frame.vm, o.fields["signature"])
    assert signature == "()V"
    instance = clazz.get_instance(frame.vm)
    iref = frame.vm.add_to_heap(instance)
    frame.stack.append(iref)
    method = clazz.find_method("<init>", signature)

    # actully running constructor in exclusive mode
    pvm_thread = Thread(frame.vm, frame.vm.top_thread_ref)
    pvm_thread.is_alive = True
    m_args = [None]*method[1]
    m_args[0] = iref
    sub = Frame(pvm_thread, clazz, method, m_args, "nativ instance0")
    pvm_thread.frame_stack.append(sub)
    frame.vm.run_thread(pvm_thread)