用 c 手撸一个 Java 的 decompiler

169 天前
 neocanable

失业在家重新学习 c 语言,本来想写一个 arm 的 hook 框架,结果心血来潮,手撸一个 java 的 decompiler

已经支持

还在开发

没有支持的还有一大堆,大体的框子已经写完了,剩下的大部分是体力活了。

收获

希望大佬帮忙

哪位大佬有类似编译原理或者编译器开发的交流群或者社区,需要理论指导的时候,十分抓狂。 龙书/虎书/鲸书理论看起来太枯燥。

成果

源码

import java.nio.channels.NotYetBoundException;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

public class TryCatchNestedInCatch {

    public static void demo(String name, String ye) {
        try {
            int mmm = 1000;
        } catch (Exception e) {
            // String e3 = "Exception";
            try {
                int mmm1 = 1000;
                //demo2();
            } catch (NotYetBoundException e100) {
                String e1 = "NotYetBoundException";
                // System.out.println(e1);
            } catch(Exception e1) {
                String e2 = "Exception";
                // System.out.println(e2);
            } finally {
                int mmm2 = 2000;
                // System.out.println(mmm2);
            }
            // System.out.println(e3);
        } finally {
            int ccc = 2000;
            // System.out.println(ccc);
        }

    }
}

反编译结果:

import java.nio.channels.NotYetBoundException;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

public class TryCatchNestedInCatch  {

	public static void demo(String var_0_0, String var_1_1) {
		exception { // block_id: 1  range: 0 - 35  parent_id: 0
			try { // block_id: 2  range: 0 - 4  parent_id: 1
				int var_2_2(1) = 1000;
				goto 74;
			}
			catch { // block_id: 3  range: 5 - 30  parent_id: 1
				exception { // block_id: 5  range: 6 - 27  parent_id: 3
					try { // block_id: 6  range: 6 - 10  parent_id: 5
						int var_3_4(1) = 1000;
						goto 57;
					}
					catch { // block_id: 7  range: 11 - 16  parent_id: 5
						String var_4_9(1) = "NotYetBoundException";
						goto 57;
					}
					catch { // block_id: 8  range: 17 - 22  parent_id: 5
						String var_4_12(1) = "Exception";
						goto 57;
					}
					finally { // block_id: 9  range: 23 - 27  parent_id: 5
						int var_6_15(1) = 2000;
						athrow(var_5_14);
					}
				}
				goto 74;
			}
			finally { // block_id: 4  range: 31 - 35  parent_id: 1
				int var_8_7(1) = 2000;
			}
		}
	}

}

源码:

import java.util.Random;

public class Or {
    public static int a1;
    public static int a2;
    public static int a3;

    public int b1;
    public int b2;
    public int b3;
    public static void main(String[] args)
    {
        Random r = new Random();
        boolean q = 100 < r.nextInt() || 20 > r.nextInt() && r.nextInt() > 0; 
        int m = 200000;
        boolean s = r.nextInt() > 20 && r.nextInt() < 100;
    }

    public static void st()
    {
        Random r = new Random();

        boolean q = 100 < r.nextInt() || (Or.a1 = Or.a2 = Or.a3 = 20) > r.nextInt() && r.nextInt() > 0;
    }

    public void field()
    {
        Random r = new Random();
         int[] d = {0, 0, 2, 3};
        boolean q = 100 < r.nextInt() || (this.b1 = this.b2 = this.b3 = 20) > r.nextInt() && r.nextInt() > 0;

        boolean cd = 100 < r.nextInt() || (d[0] = d[2] = this.b3 = 20) > r.nextInt() && r.nextInt() > 0;
    }


   public boolean define_or()
   {
       int m = 0;
       int k = 0;
       int n = 0;
       int z = 0;
       Random r = new Random();

      boolean dddd =
           (m = 20) > 100 || (
                   30 > ((r.nextInt() > 100 && r.nextInt() != 1000) ? (100 & r.nextInt()) : 20) ?
                   (Or.a1 = this.b1 = m = k = n = 100 > r.nextInt() ? r.nextInt() : 0) :
                   // (100 > r.nextInt() ? r.nextInt() : 0) :
                   (r.nextInt() > 2000 ? 100 : 20)
                   ) <
           (z = r.nextInt() > 1000 ? 0 : 1);


      System.out.println(m);
      System.out.println(k);
      System.out.println(n);

      return true;

   }




}

反编译结果:

import java.util.Random;
import java.io.PrintStream;

public class Or  {

  public static int a1;

  public static int a2;

  public static int a3;

  public int b1;

  public int b2;

  public int b3;


  public static void main(String[] var_0_0) {
    Random var_1_1(1) = new Random();
    int var_2_6(1) = 100 < var_1_1.nextInt() || 20 > var_1_1.nextInt() && var_1_1.nextInt() > 0 ? 0 : 1;
    int var_3_3(1) = 200000;
    int var_4_5(1) = var_1_1.nextInt() > 20 && var_1_1.nextInt() < 100 ? 0 : 1;
  }

  public static void st() {
    Random var_0_0(1) = new Random();
    int var_1_2(1) = 100 < var_0_0.nextInt() || Or.a1 = Or.a2 = Or.a3 = 20 > var_0_0.nextInt() && var_0_0.nextInt() > 0 ? 0 : 1;
  }

  public void field() {
    Random var_1_1(1) = new Random();
    int[] var_2_2(1) = new int[4]{0, 0, 2, 3};
    int var_3_6(1) = 100 < var_1_1.nextInt() || this.b1 = this.b2 = this.b3 = 20 > var_1_1.nextInt() && var_1_1.nextInt() > 0 ? 0 : 1;
    int var_4_5(1) = 100 < var_1_1.nextInt() || var_2_2[0] = var_2_2[2] = this.b3 = 20 > var_1_1.nextInt() && var_1_1.nextInt() > 0 ? 0 : 1;
  }

  public boolean define_or() {
    int var_1_1(1) = 0;
    int var_2_2(1) = 0;
    int var_3_3(1) = 0;
    int var_4_4(1) = 0;
    Random var_5_5(1) = new Random();
    int var_6_7(1) = var_1_1 = 20 > 100 || 30 > var_5_5.nextInt() > 100 && var_5_5.nextInt() != 1000 ? 20 : 100 & var_5_5.nextInt() ? var_5_5.nextInt() > 2000 ? 20 : 100 : 100 > var_5_5.nextInt() ? Or.a1 = this.b1 = var_1_1 = var_2_2 = var_3_3 = 0 : Or.a1 = this.b1 = var_1_1 = var_2_2 = var_3_3 = var_5_5.nextInt() < var_5_5.nextInt() > 1000 ? var_4_4 = 1 : var_4_4 = 0 ? 0 : 1;
    System.out.println(var_1_1);
    System.out.println(var_2_2);
    System.out.println(var_3_3);
    return true;
  }

}
1566 次点击
所在节点    分享创造
4 条回复
enchilada2020
169 天前
秀啊 屠龙技不懂 试试这个?
https://www.craftinginterpreters.com/
lisongeee
169 天前
感觉 java 的 decompiler 挺常见的,有考虑手撸 flutter/dart 的 decompiler 吗?
neocanable
168 天前
@enchilada2020 这本书太棒了
neocanable
168 天前
@lisongeee 不一样的东西吧,flutter 是包了一层 api 的 app

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1055381

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX