soot-oss / soot

Soot - A Java optimization framework
GNU Lesser General Public License v2.1
2.87k stars 706 forks source link

How to retain "LocalVariableTable" and "MethodParameters" attributes in .class after instrumentation? #2065

Open XYHyouKa opened 6 months ago

XYHyouKa commented 6 months ago

I found LocalVariableTable and MethodParameters attributes in .class are lost after instrumentation? I am not sure whether it's bug because I use soot my own way, so commit a feature request. Here's the detailed descriptions below:

I am using Soot to develop a Javaagent, thus requiring a workflow from byte[] to Jimple to byte[], rather than class files. So it would be better If soot can run continuely (that's what I do now) instead of executing only once (reads all class files and then outputs all class files), and supports byte[] workflow.

To do this, I added a byte stream ClassProvider so that I can use Scene.v().loadClassAndSupport(String) directly to read project classes. The output implemented a subclass of soot.baf.BafASMBackend to obtain byte array data of classes. Core implementation:

public class SubClass extends BafASMBackend {
    /* ... */
    public byte[] generateClassBytes() {
        ClassWriter cw = new SootASMClassWriter(ClassWriter.COMPUTE_FRAMES);
        cv = cw;
        generateByteCode();
        return cw.toByteArray();
    }
}

However, the classes obtained this way lost useful attributes for the program, such as LocalVariableTable and MethodParameters. Is there any wrong in my usage? How to handle this process more properly? Thank you for your reply!