You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Max Spring <ms...@cisco.com> on 2002/09/23 20:39:47 UTC
Verifies chokes
I'm trying to write a small tool which adds 'exc.printStackTrace()' to each
exception handler which doesn't has one. Here's what I got so far:
import org.apache.bcel.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
public class AddExceptionStacktrace{
static void process(String fn) throws Exception{
JavaClass jc = new ClassParser(fn).parse();
ConstantPool cp = jc.getConstantPool();
ConstantPoolGen cpg = new ConstantPoolGen(cp);
InstructionFactory factory = new InstructionFactory(cpg);
Method[] methods = jc.getMethods();
for (int i=0; i<methods.length; i++){
Method method = methods[i];
if (!method.isAbstract() && !method.isNative()){
System.out.println(method);
MethodGen mg = new MethodGen(method,jc.getClassName(),cpg);
InstructionList il = mg.getInstructionList();
boolean mChanged = false;
CodeExceptionGen[] cegs = mg.getExceptionHandlers();
for (int j=0; j<cegs.length; j++){
CodeExceptionGen ceg = cegs[j];
System.out.println(" "+ceg.getCatchType());
InstructionHandle ih = ceg.getHandlerPC();
Instruction instr = ih.getInstruction();
if (instr instanceof ASTORE){
InstructionList ilNew = new InstructionList();
ilNew.append(new ALOAD(((ASTORE)instr).getIndex()));
ilNew.append(factory.createInvoke("java.lang.Throwable"
,"printStackTrace"
,Type.VOID
,Type.NO_ARGS
,Constants.INVOKESPECIAL));
il.append(ih,ilNew);
mChanged = true;
System.out.println(ih);
}//if
}//for
if (mChanged){
methods[i] = mg.getMethod();
break;
}//if
}//if
}//for
jc.setConstantPool(cpg.getFinalConstantPool());
jc.dump(fn+".mod");
}//process
public static void main(String[] args){
try{
for (int i=0; i<args.length; i++){
process(args[i]);
}//for
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}//catch
}//main
}//class AddExceptionStacktrace
It works so far, but when I apply it against something like
public class Target{
static void m1(){
try{
System.out.println("test1");
}catch(Exception ex){
System.out.println("def");
}//catch
System.out.println("abc");
}//m1
public static void main(String[] args){
m1();
}//main
}//class Target
and I try to execute Target, I get a message
Exception in thread "main" java.lang.VerifyError: (class: com/cisco/nm/tools/transposer/Target, method: m1 signature: ()V) Illegal use of nonvirtual function call
The Javap for the Target class says
Compiled from Target.java
public class com.cisco.nm.tools.transposer.Target extends java.lang.Object {
public com.cisco.nm.tools.transposer.Target();
static void m1();
public static void main(java.lang.String[]);
}
Method com.cisco.nm.tools.transposer.Target()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 return
Method void m1()
0 getstatic #2 <Field java.io.PrintStream out>
3 ldc #3 <String "test1">
5 invokevirtual #4 <Method void println(java.lang.String)>
8 goto 24
11 astore_0
12 aload_0
13 invokespecial #49 <Method void printStackTrace()>
16 getstatic #2 <Field java.io.PrintStream out>
19 ldc #6 <String "def">
21 invokevirtual #4 <Method void println(java.lang.String)>
24 getstatic #2 <Field java.io.PrintStream out>
27 ldc #7 <String "abc">
29 invokevirtual #4 <Method void println(java.lang.String)>
32 return
Exception table:
from to target type
0 8 11 <Class java.lang.Exception>
Method void main(java.lang.String[])
0 invokestatic #8 <Method void m1()>
3 return
I'm clearly missing something -- but what is it?
Regards,
-Max
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Verifies chokes
Posted by Laurent Martelli <la...@bearteam.org>.
>>>>> "Peter" == Peter Schneider <sc...@lathanda.de> writes:
[...]
Peter> insert before the first instruction of any exception handler
Peter> DUP INVOKEVIRTUAL Throwable.printStackTrace and everything
Peter> should be fine if you need to avoid double stack traces you
Peter> are in trouble. I'm not sure if it's even possible.
Everything is possible, it's just a matter of time :-)
A trivial approach would be to scan the instructions and see if
there's already an "INVOKEVIRTUAL Throwable.printStackTrace". But you
won't know what exception this is called on. If you want to know this,
you should do a little data-flow analysis. It's not that hard. You
should (well, that's an approach, maybe there are others) simulate the
effect of instructions on the state of the stack. This way, you can
determine what's on the top of the stack when INVOKEVIRTUAL is
executed.
I think I gave some pointers to the CVS repository where you can find
my code a few days a ago on the list.
Regards,
Laurent
--
Laurent Martelli http://jac.aopsys.com/
laurent@bearteam.org http://www.bearteam.org/~laurent/
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Verifies chokes
Posted by Max Spring <ms...@cisco.com>.
> Mailing-List: contact bcel-user-help@jakarta.apache.org; run by ezmlm
> List-Unsubscribe: <ma...@jakarta.apache.org>
> List-Subscribe: <ma...@jakarta.apache.org>
> List-Help: <ma...@jakarta.apache.org>
> List-Post: <ma...@jakarta.apache.org>
> List-Id: "BCEL Users List" <bcel-user.jakarta.apache.org>
> X-Antivirus: nagoya (v4218 created Aug 14 2002)
> From: "Peter Schneider" <sc...@lathanda.de>
> Date: Mon, 23 Sep 2002 21:12:59 +0200
> X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N
>
> Hi
>
> [...]
> Some code i don't like to read
> [...]
> > and I try to execute Target, I get a message
> >
> > Exception in thread "main" java.lang.VerifyError: (class:
> > com/cisco/nm/tools/transposer/Target, method: m1 signature:
> > ()V) Illegal use of nonvirtual function call
>
> [...]
> > 11 astore_0
> > 12 aload_0
> > 13 invokespecial #49 <Method void printStackTrace()>
> > 16 getstatic #2 <Field java.io.PrintStream out>
> > 19 ldc #6 <String "def">
> > 21 invokevirtual #4 <Method void println(java.lang.String)>
> > 24 getstatic #2 <Field java.io.PrintStream out>
> > 27 ldc #7 <String "abc">
> > 29 invokevirtual #4 <Method void println(java.lang.String)>
> > 32 return
> > Exception table:
> > from to target type
> > 0 8 11 <Class java.lang.Exception>
> >
> > Method void main(java.lang.String[])
> > 0 invokestatic #8 <Method void m1()>
> > 3 return
> >
> > I'm clearly missing something -- but what is it?
> You can't use invokespecial for printStackTrace,
> it's only legit for static final or private methods.
> btw it's no good idea to use local 0 to store the exception better use
> dup
I didn't "generate" the usage of local 0. That was the Java compiler.
>
> insert before the first instruction of any exception handler
> DUP
Initially, I tried that. But then I was unable to make the handlerPC of the
corresponding exception point to this new pre-prended instruction.
So, I went with the pattern
astore X
INS> aload X
INS> invokevirtual printStackTrace
...
observing that any catch statement seems to start with an astore.
> INVOKEVIRTUAL Throwable.printStackTrace
> and everything should be fine
> if you need to avoid double stack traces you are in trouble.
Double stacktrace wouldn't be a problem. I'm just doing a throw-away tool.
-Max
> I'm not sure if it's even possible.
>
>
>
>
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Verifies chokes
Posted by Peter Schneider <sc...@lathanda.de>.
Hi
[...]
Some code i don't like to read
[...]
> and I try to execute Target, I get a message
>
> Exception in thread "main" java.lang.VerifyError: (class:
> com/cisco/nm/tools/transposer/Target, method: m1 signature:
> ()V) Illegal use of nonvirtual function call
[...]
> 11 astore_0
> 12 aload_0
> 13 invokespecial #49 <Method void printStackTrace()>
> 16 getstatic #2 <Field java.io.PrintStream out>
> 19 ldc #6 <String "def">
> 21 invokevirtual #4 <Method void println(java.lang.String)>
> 24 getstatic #2 <Field java.io.PrintStream out>
> 27 ldc #7 <String "abc">
> 29 invokevirtual #4 <Method void println(java.lang.String)>
> 32 return
> Exception table:
> from to target type
> 0 8 11 <Class java.lang.Exception>
>
> Method void main(java.lang.String[])
> 0 invokestatic #8 <Method void m1()>
> 3 return
>
> I'm clearly missing something -- but what is it?
You can't use invokespecial for printStackTrace,
it's only legit for static final or private methods.
btw it's no good idea to use local 0 to store the exception better use
dup
insert before the first instruction of any exception handler
DUP
INVOKEVIRTUAL Throwable.printStackTrace
and everything should be fine
if you need to avoid double stack traces you are in trouble.
I'm not sure if it's even possible.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>