loading...

Decompile and dissasemble JVM bytecode compiled from Kotlin source code

yuyabu profile image yuyabu ・2 min read

This is an experiment to try using javap (disassembler) and jad (decompiler) for this Kotlin code.

fun main (args: Array <String>) {
    println ("Hello, World!")
}

Code source
https://kotlinlang.org/docs/tutorials/command-line.html

Like other JVM languages, Kotlin is compiled into a class file executable as Java.
We can verify it because javap can display the mnemonic of JVM and decompile as java source code with jad.

Compile this.

kotlinc hello.kt -include-runtime -d hello.jar

Extract jar and use javap, jad for hello.class

jar xf hello.jar

Environment

macOS High Sierra 10

javap (Disassembler)

Preparation

It is unnecessary since it is in the JDK from the beginning

Execution

javap - c HelloKt.class

Result

Compiled from "hello.kt"
public final class HelloKt {
  public static final void main (java.lang.String []);
    Code:
       0: aload_ 0
       1: ldc # 9 // String args
       3: invokestatic # 15 // Method kotlin / jvm / internal / Intrinsics.checkParameterIsNotNull: (Ljava / lang / Object; Ljava / lang / String;) V
       6: ldc # 17 // String Hello, World!
       8: astore_1
       9: getstatic # 23 // Field java / lang / System.out: Ljava / io / PrintStream;
      12: aload_1
      13: invokevirtual # 29 // Method java / io / PrintStream.println: (Ljava / lang / Object;) V
      16: return
}

jad (decompiler)

Preparation

How to introduce jad

brew tap caskroom / cask
brew install caskroom / cask / jad

Execution

 You can display the mnemonic as a comment with the -a option.

Command

$ jad -a HelloKt.class
$ cat HelloKt.jad
import java.io.PrintStream;
import kotlin.jvm.internal.Intrinsics;

public final class HelloKt
{

    public static final void main (String args [])
    {
        Intrinsics.checkParameterIsNotNull (args, "args");
    // 0 0: aload_ 0
    // 1 1: ldc 1 # 9 <String "args">
    // 2 3: invokestatic # 15 <Method void Intrinsics.checkParameterIsNotNull (Object, String)>
        String s = "Hello, World!";
    // 3 6: ldc 1 # 17 <String "Hello, World!">
    // 4 8: astore_ 1
        System.out.println (s);
    // 5 9: getstatic # 23 <Field PrintStream System.out>
    // 6 12: aload - 1
    // 7 13: invokevirtual # 29 <Method void PrintStream.println (Object)>
    // 8 16: return
    }
}

It was easy to introduce and analyze.
I will check how Kotlin specific functions are represented and converted as Java in the future.

Posted on by:

yuyabu profile

yuyabu

@yuyabu

Japanese Programer. work for:Database R&D,Batch Framework on Distributed System(like Hadoop). I am not good at English, so please tell me my strange usage.

Discussion

pic
Editor guide