Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4ae4c388 authored by Makoto Onuki's avatar Makoto Onuki Committed by Android (Google) Code Review
Browse files

Merge "[HostStubGen] Support non-static methods..." into main

parents 7d859fde 0c76c852
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -119,8 +119,12 @@ fun getDirectOuterClassName(className: String): String? {
 * Write bytecode to push all the method arguments to the stack.
 * The number of arguments and their type are taken from [methodDescriptor].
 */
fun writeByteCodeToPushArguments(methodDescriptor: String, writer: MethodVisitor) {
    var i = -1
fun writeByteCodeToPushArguments(
        methodDescriptor: String,
        writer: MethodVisitor,
        argOffset: Int = 0,
        ) {
    var i = argOffset - 1
    Type.getArgumentTypes(methodDescriptor).forEach { type ->
        i++

@@ -158,6 +162,18 @@ fun writeByteCodeToReturn(methodDescriptor: String, writer: MethodVisitor) {
    }
}

/**
 * Given a method descriptor, insert an [argType] as the first argument to it.
 */
fun prependArgTypeToMethodDescriptor(methodDescriptor: String, argType: Type): String {
    val returnType = Type.getReturnType(methodDescriptor)
    val argTypes = Type.getArgumentTypes(methodDescriptor).toMutableList()

    argTypes.add(0, argType)

    return Type.getMethodDescriptor(returnType, *argTypes.toTypedArray())
}

/**
 * Return the "visibility" modifier from an `access` integer.
 *
+25 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import com.android.hoststubgen.asm.CLASS_INITIALIZER_DESC
import com.android.hoststubgen.asm.CLASS_INITIALIZER_NAME
import com.android.hoststubgen.asm.ClassNodes
import com.android.hoststubgen.asm.isVisibilityPrivateOrPackagePrivate
import com.android.hoststubgen.asm.prependArgTypeToMethodDescriptor
import com.android.hoststubgen.asm.writeByteCodeToPushArguments
import com.android.hoststubgen.asm.writeByteCodeToReturn
import com.android.hoststubgen.filters.FilterPolicy
@@ -285,7 +286,7 @@ class ImplGeneratingAdapter(
     * class.
     */
    private inner class NativeSubstitutingMethodAdapter(
            access: Int,
            val access: Int,
            private val name: String,
            private val descriptor: String,
            signature: String?,
@@ -300,12 +301,33 @@ class ImplGeneratingAdapter(
        }

        override fun visitEnd() {
            writeByteCodeToPushArguments(descriptor, this)
            var targetDescriptor = descriptor
            var argOffset = 0

            // For non-static native method, we need to tweak it a bit.
            if ((access and Opcodes.ACC_STATIC) == 0) {
                // Push `this` as the first argument.
                this.visitVarInsn(Opcodes.ALOAD, 0)

                // Update the descriptor -- add this class's type as the first argument
                // to the method descriptor.
                val thisType = Type.getType("L" + currentClassName + ";")

                targetDescriptor = prependArgTypeToMethodDescriptor(
                        descriptor,
                        thisType,
                )

                // Shift the original arguments by one.
                argOffset = 1
            }

            writeByteCodeToPushArguments(descriptor, this, argOffset)

            visitMethodInsn(Opcodes.INVOKESTATIC,
                    nativeSubstitutionClass,
                    name,
                    descriptor,
                    targetDescriptor,
                    false)

            writeByteCodeToReturn(descriptor, this)
+57 −3
Original line number Diff line number Diff line
@@ -1718,7 +1718,11 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #x                         // com/android/hoststubgen/test/tinyframework/TinyFrameworkNative
  super_class: #x                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 5, attributes: 2
  interfaces: 0, fields: 1, methods: 8, attributes: 2
  int value;
    descriptor: I
    flags: (0x0000)

  public com.android.hoststubgen.test.tinyframework.TinyFrameworkNative();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
@@ -1767,6 +1771,40 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative
        Start  Length  Slot  Name   Signature
            0       6     0  arg1   J
            0       6     2  arg2   J

  public void setValue(int);
    descriptor: (I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         x: aload_0
         x: iload_1
         x: putfield      #x                 // Field value:I
         x: return
      LineNumberTable:
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;
            0       6     1     v   I

  public native int nativeNonStaticAddToValue(int);
    descriptor: (I)I
    flags: (0x0101) ACC_PUBLIC, ACC_NATIVE

  public int nativeNonStaticAddToValue_should_be_like_this(int);
    descriptor: (I)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         x: aload_0
         x: iload_1
         x: invokestatic  #x                 // Method com/android/hoststubgen/test/tinyframework/TinyFrameworkNative_host.nativeNonStaticAddToValue:(Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;I)I
         x: ireturn
      LineNumberTable:
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;
            0       6     1   arg   I
}
SourceFile: "TinyFrameworkNative.java"
RuntimeInvisibleAnnotations:
@@ -1784,7 +1822,7 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative_host
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #x                         // com/android/hoststubgen/test/tinyframework/TinyFrameworkNative_host
  super_class: #x                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 3, attributes: 2
  interfaces: 0, fields: 0, methods: 4, attributes: 2
  public com.android.hoststubgen.test.tinyframework.TinyFrameworkNative_host();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
@@ -1826,6 +1864,22 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative_host
        Start  Length  Slot  Name   Signature
            0       4     0  arg1   J
            0       4     2  arg2   J

  public static int nativeNonStaticAddToValue(com.android.hoststubgen.test.tinyframework.TinyFrameworkNative, int);
    descriptor: (Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;I)I
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=2
         x: aload_0
         x: getfield      #x                  // Field com/android/hoststubgen/test/tinyframework/TinyFrameworkNative.value:I
         x: iload_1
         x: iadd
         x: ireturn
      LineNumberTable:
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       7     0 source   Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;
            0       7     1   arg   I
}
SourceFile: "TinyFrameworkNative_host.java"
RuntimeInvisibleAnnotations:
+31 −1
Original line number Diff line number Diff line
@@ -1039,7 +1039,11 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #x                          // com/android/hoststubgen/test/tinyframework/TinyFrameworkNative
  super_class: #x                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 5, attributes: 3
  interfaces: 0, fields: 1, methods: 8, attributes: 3
  int value;
    descriptor: I
    flags: (0x0000)

  public com.android.hoststubgen.test.tinyframework.TinyFrameworkNative();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
@@ -1080,6 +1084,32 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative
         x: ldc           #x                 // String Stub!
         x: invokespecial #x                 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
         x: athrow

  public void setValue(int);
    descriptor: (I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         x: new           #x                 // class java/lang/RuntimeException
         x: dup
         x: ldc           #x                 // String Stub!
         x: invokespecial #x                 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
         x: athrow

  public native int nativeNonStaticAddToValue(int);
    descriptor: (I)I
    flags: (0x0101) ACC_PUBLIC, ACC_NATIVE

  public int nativeNonStaticAddToValue_should_be_like_this(int);
    descriptor: (I)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         x: new           #x                 // class java/lang/RuntimeException
         x: dup
         x: ldc           #x                 // String Stub!
         x: invokespecial #x                 // Method java/lang/RuntimeException."<init>":(Ljava/lang/String;)V
         x: athrow
}
SourceFile: "TinyFrameworkNative.java"
RuntimeVisibleAnnotations:
+68 −2
Original line number Diff line number Diff line
@@ -1650,7 +1650,11 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #x                          // com/android/hoststubgen/test/tinyframework/TinyFrameworkNative
  super_class: #x                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 5, attributes: 3
  interfaces: 0, fields: 1, methods: 8, attributes: 3
  int value;
    descriptor: I
    flags: (0x0000)

  public com.android.hoststubgen.test.tinyframework.TinyFrameworkNative();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
@@ -1710,6 +1714,46 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative
        Start  Length  Slot  Name   Signature
            0       6     0  arg1   J
            0       6     2  arg2   J

  public void setValue(int);
    descriptor: (I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         x: aload_0
         x: iload_1
         x: putfield      #x                 // Field value:I
         x: return
      LineNumberTable:
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;
            0       6     1     v   I

  public int nativeNonStaticAddToValue(int);
    descriptor: (I)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         x: aload_0
         x: iload_1
         x: invokestatic  #x                 // Method com/android/hoststubgen/test/tinyframework/TinyFrameworkNative_host.nativeNonStaticAddToValue:(Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;I)I
         x: ireturn

  public int nativeNonStaticAddToValue_should_be_like_this(int);
    descriptor: (I)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         x: aload_0
         x: iload_1
         x: invokestatic  #x                 // Method com/android/hoststubgen/test/tinyframework/TinyFrameworkNative_host.nativeNonStaticAddToValue:(Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;I)I
         x: ireturn
      LineNumberTable:
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;
            0       6     1   arg   I
}
SourceFile: "TinyFrameworkNative.java"
RuntimeVisibleAnnotations:
@@ -1732,7 +1776,7 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative_host
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #x                          // com/android/hoststubgen/test/tinyframework/TinyFrameworkNative_host
  super_class: #x                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 3, attributes: 3
  interfaces: 0, fields: 0, methods: 4, attributes: 3
  public com.android.hoststubgen.test.tinyframework.TinyFrameworkNative_host();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
@@ -1792,6 +1836,28 @@ public class com.android.hoststubgen.test.tinyframework.TinyFrameworkNative_host
        Start  Length  Slot  Name   Signature
           15       4     0  arg1   J
           15       4     2  arg2   J

  public static int nativeNonStaticAddToValue(com.android.hoststubgen.test.tinyframework.TinyFrameworkNative, int);
    descriptor: (Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;I)I
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=2
         x: ldc           #x                 // String com/android/hoststubgen/test/tinyframework/TinyFrameworkNative_host
         x: ldc           #x                 // String nativeNonStaticAddToValue
         x: ldc           #x                 // String (Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;I)I
         x: invokestatic  #x                 // Method com/android/hoststubgen/hosthelper/HostTestUtils.getStackWalker:()Ljava/lang/StackWalker;
         x: invokevirtual #x                 // Method java/lang/StackWalker.getCallerClass:()Ljava/lang/Class;
        x: invokestatic  #x                 // Method com/android/hoststubgen/hosthelper/HostTestUtils.onNonStubMethodCalled:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Class;)V
        x: aload_0
        x: getfield      #x                 // Field com/android/hoststubgen/test/tinyframework/TinyFrameworkNative.value:I
        x: iload_1
        x: iadd
        x: ireturn
      LineNumberTable:
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
           15       7     0 source   Lcom/android/hoststubgen/test/tinyframework/TinyFrameworkNative;
           15       7     1   arg   I
}
SourceFile: "TinyFrameworkNative_host.java"
RuntimeVisibleAnnotations:
Loading