Stand-alone Bytecode Verifier

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:18:55

As with any project involving JVM bytecode, I would first check to see whether the BCEL has anything that might be useful for you. Also, perhaps FindBugs may help - though I'm not sure whether it assumes verifiable bytecode to start with or not.

ASM CheckClassAdaptor.verify() gives great feedback: http://asm.ow2.org/

I was also looking for something that would report potential verify errors, but especially IncompatibleClassChangeErrors. I wrote a little test project with one API class and another client class calling API methods, plus a main class to run a verifier; then changed the API, recompiling it but not the client, and checked to see what could be caught. Used -target 7 though no special JDK 7 features for now.

First and most obviously, Class.forName can find certain errors in the client class's signature, but it does not seem to check method bodies for calls to nonexistent API methods and the like, even if you call getDeclaredMethods; the errors are reported by the VM only when the problematic line of code is actually run.

JustIce in BCEL 5.2 seems to be easiest;

org.apache.bcel.verifier.Verifier.main(new String[] {clazz});

does the job:

Pass 3a, method number 1 ['public void m()']:
VERIFIED_REJECTED
Instruction invokestatic[184](3) 4 constraint violated:
  Referenced method 'x' with expected signature '()V' not found in class 'API'.
  ....

I tried ASM 4.0, but

org.objectweb.asm.util.CheckClassAdapter.main(new String[] {clazz});

does not work; perhaps it checks the format of methods, but not linkage. Inlining main and passing checkDataFlow=true does not help.

Searching, I also found https://kenai.com/hg/maxine~maxine/file/8429d3ebc036/com.oracle.max.vm/test/test/com/sun/max/vm/verifier/CommandLineVerifier.java but I could not find any way to make this work; the accompanying unit test throws a ClassNotFoundException when run.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!