如何隐藏变量不被Java的JDI检测到?

huangapple go评论74阅读模式
英文:

How to hide variables from Java's JDI?

问题

我正在为一些类进行仪器化,并引入了一些新的局部变量。现在,当用户在代码中设置断点并停止执行时,新引入的局部变量可以在Intellij IDEA的调试器窗口中看到。如何隐藏它们?

更新:我将不得不以某种方式从仪器化的代码中删除调试信息,但不确定如何做。

更新2:我正在使用ASM库进行仪器化。

public void visitCode() {
    this.mv.visitLdcInsn(stringToPass);
    this.mv.visitMethodInsn(Opcodes.INVOKESTATIC, "MyAgentClass", "loadData", "(Ljava/lang/String;)LDataClass;", false);
    this.mv.visitVarInsn(Opcodes.ASTORE, this.getDataIndex());
}

public void visitMaxs(int maxStack, int maxLocals) {
    if (this.myStartLabel != null && this.myEndLabel != null) {
        this.mv.visitLocalVariable("__my__data__", "Ljava/lang/Object;", (String) null, this.myStartLabel, this.myEndLabel, this.getDataIndex());
    }

    super.visitMaxs(maxStack, maxLocals);
}

__my__data__ 在Intellij IDEA中可见。

英文:

I am instrumenting some classes and introducing some new local variables. Now, when the user places a breakpoint in the code, and execution is stopped, the newly introduced local variables can be seen inside Intellij IDEA's debugger window. How can I hide them?

UPDATE: I will have to somehow remove debug info from the instrumented code, but not sure how to do it.

UPDATE 2: I am using the ASM library for instrumentation.

 public void visitCode() {
       this.mv.visitLdcInsn(stringToPass);
       this.mv.visitMethodInsn(Opcodes.INVOKESTATIC, "MyAgentClass", "loadData", "(Ljava/lang/String;)LDataClass;", false);
       this.mv.visitVarInsn(Opcodes.ASTORE, this.getDataIndex());
}


public void visitMaxs(int maxStack, int maxLocals) {
     if (this.myStartLabel != null && this.myEndLabel != null) {
        this.mv.visitLocalVariable("__my__data__", "Ljava/lang/Object;", (String) null, this.myStartLabel, this.myEndLabel, this.getDataIndex());
     }

       super.visitMaxs(maxStack, maxLocals);
 }

__my__data__ is shown inside Intellij IDEA.

答案1

得分: 1

根据JVM规范,您可以从LocalVariableTable中删除您的局部变量。 Javassist会在生成的代码中自动执行此操作,我在运行期间无法看到变量secretCode

反编译结果类可以显示它没有LocalVariableTable条目:

public void run();
  descriptor: ()V
  flags: ACC_PUBLIC
  Code:
    stack=2, locals=3, args_size=1
       0: ldc           #39                 // int -889275714
       2: istore_1
       3: iload_1
       4: invokestatic  #43                 // Method org/example/App.test:(I)Ljava/lang/Integer;
       7: astore_2
       8: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      11: aload_2
      12: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      15: new           #2                  // class SecreFoo
      18: dup
      19: invokespecial #3                  // Method "<init>":()V
      22: astore_1
      23: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      26: aload_1
      27: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      30: return
    LineNumberTable:
      line 14: 15
      line 15: 23
      line 16: 30
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      31     0  this   LSecreFoo;
         23       8     1 fCopy   LSecreFoo;

因此,当您对类进行插桩时,请从表中删除所有局部变量(或不要添加它们)。

英文:

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.13

Based on the JVM specs, you can remove your local variables from LocalVariableTable. Javassist does that automatically in generated code and I cannot see the variable secretCode during the run:

如何隐藏变量不被Java的JDI检测到?

Decompiling the result class can show that there are no LocalVariableTable entries for it:


  public void run();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=1
         0: ldc           #39                 // int -889275714
         2: istore_1
         3: iload_1
         4: invokestatic  #43                 // Method org/example/App.test:(I)Ljava/lang/Integer;
         7: astore_2
         8: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        11: aload_2
        12: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
        15: new           #2                  // class SecreFoo
        18: dup
        19: invokespecial #3                  // Method "<init>":()V
        22: astore_1
        23: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        26: aload_1
        27: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
        30: return
      LineNumberTable:
        line 14: 15
        line 15: 23
        line 16: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      31     0  this   LSecreFoo;
           23       8     1 fCopy   LSecreFoo;

So when you are instrumenting your class, drop all your local variables from the table (or do not add them).

huangapple
  • 本文由 发表于 2020年8月9日 19:51:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63325980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定