Exemplo n.º 1
0
def doPrivileged(klass, vm, method, frame):
    action = frame.get_local(0)
    run_method_name = Class.method_name('run', '()Ljava/lang/Object;')
    method = action._klass.methods[run_method_name]
    # instance to call on
    result = vm.wrap_run_method(action, method)
    return result
Exemplo n.º 2
0
def invokevirtual_special(vm, frame, offset, bytecode):
    method_index = vm.constant_pool_index(bytecode, frame.pc)
    new_klass, method = vm.resolve_field(frame.klass, method_index)
    # fetch the instance and then extract the class from that.
    # we do this so that in the case of an abstract class we get
    # the implemented method
    if bytecode[frame.pc] == 182:
        instance = frame.stack[-len(method.parameters)-1]
        if instance is null:
            # remove all our method parameters
            for i in xrange(len(method.parameters)+1):
                frame.pop()
            raise Exception
            vm.throw_exception(frame, 'java/lang/NullPointerException')
            return
        assert new_klass.is_subclass(instance),\
            '%s not a subclass of %s' % (str(new_klass), str(instance))
        new_klass = instance._klass
    if bytecode[frame.pc] == 184:
        assert (method.access_flags & ACC_STATIC) != 0

    # find the superclass that contains this method (or we have it)
    method_name = Class.method_name(method.name, method.descriptor)
    while method_name not in new_klass.methods:
        new_klass = new_klass.super_class
        if new_klass.super_class == new_klass:
            break
    assert method_name in new_klass.methods

    new_method = new_klass.methods[method_name]
    vm.run_method(new_klass, new_method)
    frame.pc += 2
Exemplo n.º 3
0
def invokevirtual_special(vm, frame, offset, bytecode):
    method_index = vm.constant_pool_index(bytecode, frame.pc)
    new_klass, method = vm.resolve_field(frame.klass, method_index)
    # fetch the instance and then extract the class from that.
    # we do this so that in the case of an abstract class we get
    # the implemented method
    if bytecode[frame.pc] == 182:
        instance = frame.stack[-len(method.parameters) - 1]
        if instance is null:
            # remove all our method parameters
            for i in xrange(len(method.parameters) + 1):
                frame.pop()
            raise Exception
            vm.throw_exception(frame, 'java/lang/NullPointerException')
            return
        assert new_klass.is_subclass(instance),\
            '%s not a subclass of %s' % (str(new_klass), str(instance))
        new_klass = instance._klass
    if bytecode[frame.pc] == 184:
        assert (method.access_flags & ACC_STATIC) != 0

    # find the superclass that contains this method (or we have it)
    method_name = Class.method_name(method.name, method.descriptor)
    while method_name not in new_klass.methods:
        new_klass = new_klass.super_class
        if new_klass.super_class == new_klass:
            break
    assert method_name in new_klass.methods

    new_method = new_klass.methods[method_name]
    vm.run_method(new_klass, new_method)
    frame.pc += 2
Exemplo n.º 4
0
    def parse(self):
        # read the first magic bytes
        for magic in MAGIC:
            byte = self._read_byte()
            if byte != magic:
                raise MalformedClassException()

        klass = self.klass

        # class file version
        klass.minor_version = self._read_byte2()
        klass.major_version = self._read_byte2()

        constant_pool_length = self._read_byte2()
        klass.constant_pool = ConstantPool(constant_pool_length)
        while klass.constant_pool.size < constant_pool_length - 1:
            klass.constant_pool.add_pool(self.parse_constant_pool_item())

        klass.access_flags = self._read_byte2()
        klass.this_class = klass.constant_pool.get_class(self._read_byte2())
        super_class_index = self._read_byte2()
        klass.super_class = 'java/lang/Object'
        if super_class_index != 0:
            klass.super_class = klass.constant_pool.get_class(
                super_class_index)

        interfaces_count = self._read_byte2()
        for i in xrange(interfaces_count):
            klass.interfaces.append(
                klass.constant_pool.get_class(self._read_byte2()))

        field_length = self._read_byte2()
        for i in xrange(field_length):
            field = self.parse_field()
            klass.fields[field.name] = field

        method_count = self._read_byte2()
        for i in xrange(method_count):
            method = self.parse_method()
            klass.methods[Class.method_name(method)] = method

        klass.attributes = self.parse_attributes()
Exemplo n.º 5
0
    def parse(self):
        # read the first magic bytes
        for magic in MAGIC:
            byte = self._read_byte()
            if byte != magic:
                raise MalformedClassException()

        klass = self.klass

        # class file version
        klass.minor_version = self._read_byte2()
        klass.major_version = self._read_byte2()

        constant_pool_length = self._read_byte2()
        klass.constant_pool = ConstantPool(constant_pool_length)
        while klass.constant_pool.size < constant_pool_length-1:
            klass.constant_pool.add_pool(self.parse_constant_pool_item())

        klass.access_flags = self._read_byte2()
        klass.this_class = klass.constant_pool.get_class(self._read_byte2())
        super_class_index = self._read_byte2()
        klass.super_class = 'java/lang/Object'
        if super_class_index != 0:
            klass.super_class = klass.constant_pool.get_class(super_class_index)

        interfaces_count = self._read_byte2()
        for i in xrange(interfaces_count):
            klass.interfaces.append(klass.constant_pool.get_class(self._read_byte2()))


        field_length = self._read_byte2()
        for i in xrange(field_length):
            field = self.parse_field()
            klass.fields[field.name] = field

        method_count = self._read_byte2()
        for i in xrange(method_count):
            method = self.parse_method()
            klass.methods[Class.method_name(method)] = method

        klass.attributes = self.parse_attributes()