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)
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
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)
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")
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
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)
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)
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)
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)
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)
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)