Check the validity of a .class file

Keith McGuigan Keith.McGuigan at Sun.COM
Thu Nov 20 14:14:51 PST 2008


Mario Mendez-Lojo wrote:
> You are right about the -verify option in the JVM. However, I'm asking 
> about a tool to *statically* check the validity of a .class file, 
> possibly in the context of the Hotspot JVM. In this way, I don't need to 
> run the program in order to  figure out if the instrumented bytecode is 
> valid.

Well, verification in the JVM occurs as part of the loading, so the 
bytecode actually is statically examined.  It doesn't have to run, the 
verifier is kicked off at link time, not run time.  This means you 
should be able to check a class by simply calling Class.forName() in a 
java program (ANY java program, not necessarily the one you're 
instrumenting for), and if you get an exception then the bytecode is 
malformed.

> It seems to me that verifying the correctness of the bytecode can be 
> done at compile time, but maybe I'm wrong about this and we can only do 
> the verification at runtime, within the VM.

So long as you have definitions for any referenced classes, you should 
be able to perform the correctness check outside the JVM with no 
problem.  Of course you might have to implement it yourself :)  Just 
check the JVM spec for the specific rules concerning the format and type 
checks that need to be performed.   These checks could be done at any 
time by preprocessing tools, there's no VM-magic there, really.

IMO, leveraging the built-in verifier in the JVM would be the way to go, 
if you can.  Just spawn a side process that does the Class.forName() 
trick (or any other way of getting the class loaded and linked) and 
check for VerifyErrors there.

--
- Keith



More information about the hotspot-dev mailing list