Re: Errors caused by already instrumented classes

[email protected]
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <775134349.466701316855720408.JavaMail.root@zimbra7-e1.priv.proxad.net>
with the attachment :-)

> Lukas Marek wrote:
> > Hello,
> >
> > we are instrumenting classes using two our own tools build on top of
> > the
> > ASM. Both tools are used in a chain where classes are passed as
> bytes
> >
> > between the tools. Both tools should produce valid class as their
> > output.
> >
> > We encountered some errors in ASM when passing instrumented class
> from
> >
> > the first tool to the second one. In the first tool, the class is
> > properly (with no errors) instrumented and validated using
> > CheckClassAdapter. They are also successfully loaded with jvm if the
> > second instrumentation tool is skipped.
> >
> > There are two cases where the ASM fails to load the classes produced
> > by
> > the first tool.
> >
> > I've created byte dumps of the class produced by the first tool and
> > simple asm test cases where it fails in the second one.
> >
> > In the first scenario called "example1", if the "CheckClassAdapter"
> is
> >
> > removed, everything works fine. Otherwise, it produces
> > ArrayIndexOutOfBoundsException in MethodWriter class.
> >
> > Second scenario (example2) uses ClassVisitor and AdviceAdapter. If
> the
> >
> > AdviceAdapter is replaced with MethodVisitor, everything works fine.
> >
> > EditA and A sources should be another example of the second scenario
> > done by my colleague.
> >
> > We are using ASM 4.0 RC2.
> 
> the first example did not work because you can't visit the same class
> node instance twice (labels must be reset between visits, see
> InsnList.resetLabels). For convenience, I propose to automatically
> reset the labels when MethodNode.accept is called several times (see
> attached patch)
> 
> the second example did not work because of a bug in AdviceAdapter
> (fixed in attached patch).
> 
> Eric
asm.patch (application/octet-stream, 2.8 KB)
Index: src/org/objectweb/asm/commons/AdviceAdapter.java
===================================================================
--- src/org/objectweb/asm/commons/AdviceAdapter.java	(revision 1571)
+++ src/org/objectweb/asm/commons/AdviceAdapter.java	(working copy)
@@ -1,6 +1,7 @@
 /***
  * ASM: a very small and fast Java bytecode manipulation framework
  * Copyright (c) 2000-2011 INRIA, France Telecom
+ * Copyright (c) 2011 Google
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -396,7 +397,7 @@
     @Override
     public void visitIntInsn(final int opcode, final int operand) {
         mv.visitIntInsn(opcode, operand);
-        if (constructor && opcode!=NEWARRAY) {
+        if (constructor && opcode != NEWARRAY) {
             pushValue(OTHER);
         }
     }
@@ -569,6 +570,21 @@
         }
     }
 
+    @Override
+    public void visitTryCatchBlock(
+        Label start,
+        Label end,
+        Label handler,
+        String type)
+    {
+        super.visitTryCatchBlock(start, end, handler, type);
+        if (constructor && !branches.containsKey(handler)) {
+            List<Object> stackFrame = new ArrayList<Object>();
+            stackFrame.add(OTHER);
+            branches.put(handler, stackFrame);
+        }
+    }
+
     private void addBranches(final Label dflt, final Label[] labels) {
         addBranch(dflt);
         for (int i = 0; i < labels.length; i++) {
Index: src/org/objectweb/asm/tree/MethodNode.java
===================================================================
--- src/org/objectweb/asm/tree/MethodNode.java	(revision 1571)
+++ src/org/objectweb/asm/tree/MethodNode.java	(working copy)
@@ -1,6 +1,7 @@
 /***
  * ASM: a very small and fast Java bytecode manipulation framework
  * Copyright (c) 2000-2011 INRIA, France Telecom
+ * Copyright (c) 2011 Google
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -167,6 +168,11 @@
     public List<LocalVariableNode> localVariables;
 
     /**
+     * If the accept method has been called on this object.
+     */
+    private boolean visited;
+    
+    /**
      * Constructs an uninitialized {@link MethodNode}. <i>Subclasses must not
      * use this constructor</i>. Instead, they must use the
      * {@link #MethodNode(int)} version.
@@ -609,6 +615,9 @@
                 an.accept(mv.visitParameterAnnotation(i, an.desc, false));
             }
         }
+        if (visited) {
+            instructions.resetLabels();
+        }
         n = attrs == null ? 0 : attrs.size();
         for (i = 0; i < n; ++i) {
             mv.visitAttribute(attrs.get(i));
@@ -630,6 +639,7 @@
             }
             // visits maxs
             mv.visitMaxs(maxStack, maxLocals);
+            visited = true;
         }
         mv.visitEnd();
     }
message-footer.txt (text/plain, 238 B)
-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.