]> source.dussan.org Git - aspectj.git/commitdiff
175806: allow for crumby bytecode incoming
authoraclement <aclement>
Wed, 28 Feb 2007 13:10:34 +0000 (13:10 +0000)
committeraclement <aclement>
Wed, 28 Feb 2007 13:10:34 +0000 (13:10 +0000)
tests/bugs160/pr175806/A.java [new file with mode: 0644]
tests/src/org/aspectj/systemtest/ajc160/Ajc160Tests.java
tests/src/org/aspectj/systemtest/ajc160/ajc160.xml

diff --git a/tests/bugs160/pr175806/A.java b/tests/bugs160/pr175806/A.java
new file mode 100644 (file)
index 0000000..562276c
--- /dev/null
@@ -0,0 +1,12 @@
+public class A {
+
+  public static void main(String []argv) {
+    int i = 5;
+    try {
+      String s = "3";
+      System.out.println(s);
+    } catch (Exception e) {
+      System.out.println(i);
+    }
+  }
+}
index 0456efa115cec11f80a8eba0c4710966a5cb6b70..fc816e0b902f443e74b4836c3bd99a42886c4c23 100644 (file)
@@ -12,6 +12,14 @@ package org.aspectj.systemtest.ajc160;
 
 import java.io.File;
 
+import org.aspectj.apache.bcel.classfile.ConstantPool;
+import org.aspectj.apache.bcel.classfile.JavaClass;
+import org.aspectj.apache.bcel.classfile.LineNumberTable;
+import org.aspectj.apache.bcel.classfile.Method;
+import org.aspectj.apache.bcel.generic.ConstantPoolGen;
+import org.aspectj.apache.bcel.generic.MethodGen;
+import org.aspectj.apache.bcel.util.ClassPath;
+import org.aspectj.apache.bcel.util.SyntheticRepository;
 import org.aspectj.testing.XMLBasedAjcTestCase;
 
 import junit.framework.Test;
@@ -21,6 +29,96 @@ import junit.framework.Test;
  */
 public class Ajc160Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
        
+       /** Complex test that attempts to damage a class like a badly behaved bytecode transformer would and checks if AspectJ can cope. */
+        public void testCopingWithGarbage_pr175806_1() throws ClassNotFoundException { 
+                
+               // Compile the program we are going to mess with
+           runTest("coping with bad tables");
+           
+           // Load up the class and the method 'main' we are interested in
+           JavaClass jc = getClassFrom(ajc.getSandboxDirectory(),"A");
+           Method[] meths = jc.getMethods();
+           Method oneWeWant = null;
+           for (int i = 0; i < meths.length && oneWeWant==null; i++) {
+                       Method method = meths[i];
+                       if (method.getName().equals("main")) oneWeWant = meths[i];
+           }
+           
+           /**
+            * For the main method:
+                 Stack=2, Locals=3, Args_size=1
+                 0:   iconst_5
+                 1:   istore_1
+                 2:   ldc     #18; //String 3
+                 4:   astore_2
+                 5:   getstatic       #24; //Field java/lang/System.out:Ljava/io/PrintStream;
+                 8:   aload_2
+                 9:   invokevirtual   #30; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
+                 12:  goto    23
+                 15:  pop
+                 16:  getstatic       #24; //Field java/lang/System.out:Ljava/io/PrintStream;
+                 19:  iload_1
+                 20:  invokevirtual   #33; //Method java/io/PrintStream.println:(I)V
+                 23:  return
+                Exception table:
+                 from   to  target type
+                   2    15    15   Class java/lang/Exception
+               
+                LineNumberTable:
+                 line 4: 0
+                 line 6: 2
+                 line 7: 5
+                 line 8: 15
+                 line 9: 16
+                 line 11: 23
+                LocalVariableTable:
+                 Start  Length  Slot  Name   Signature
+                 0      24      0    argv       [Ljava/lang/String;
+                 2      22      1    i       I
+                 5      10      2    s       Ljava/lang/String;
+            */
+           
+           ConstantPool cp = oneWeWant.getConstantPool();
+           ConstantPoolGen cpg = new ConstantPoolGen(cp);
+           
+           // Damage the line number table, entry 2 (Line7:5) so it points to an invalid (not on an instruction boundary) position of 6
+           oneWeWant.getLineNumberTable().getLineNumberTable()[2].setStartPC(6);
+
+           // Should be 'rounded down' when transforming it into a MethodGen, new position will be '5'
+//         System.out.println("BEFORE\n"+oneWeWant.getLineNumberTable().toString());
+           MethodGen toTransform = new MethodGen(oneWeWant,"A",cpg,false);
+           LineNumberTable lnt = toTransform.getMethod().getLineNumberTable();
+           assertTrue("Should have been 'rounded down' to position 5 but is "+lnt.getLineNumberTable()[2].getStartPC(), lnt.getLineNumberTable()[2].getStartPC()==5);
+//         System.out.println("AFTER\n"+lnt.toString());    
+        }
+        
+        public void testCopingWithGarbage_pr175806_2() throws ClassNotFoundException { 
+        
+               // Compile the program we are going to mess with
+           runTest("coping with bad tables");
+           
+           // Load up the class and the method 'main' we are interested in
+           JavaClass jc = getClassFrom(ajc.getSandboxDirectory(),"A");
+           Method[] meths = jc.getMethods();
+           Method oneWeWant = null;
+           for (int i = 0; i < meths.length && oneWeWant==null; i++) {
+                       Method method = meths[i];
+                       if (method.getName().equals("main")) oneWeWant = meths[i];
+           }
+           // see previous test for dump of main method
+           
+           ConstantPool cp = oneWeWant.getConstantPool();
+           ConstantPoolGen cpg = new ConstantPoolGen(cp);
+           
+           // Damage the local variable table, entry 2 (" 2      22      1    i       I") so it points to an invalid start pc of 3
+           oneWeWant.getLocalVariableTable().getLocalVariable(1).setStartPC(3);
+
+           // Should be 'rounded down' when transforming it into a MethodGen, new position will be '2'             
+           // This next line will go BANG with an NPE if we don't correctly round the start pc down to 2
+           MethodGen toTransform = new MethodGen(oneWeWant,"A",cpg,true);
+        }
+         
+
   public void testGenericAspectGenericPointcut_pr174449() { runTest("problem with generic aspect and generic pointcut");}
   public void testGenericAspectGenericPointcut_noinline_pr174449() { runTest("problem with generic aspect and generic pointcut - noinline");}
   public void testGenericMethodsAndOrdering_ok_pr171953_2() { runTest("problem with generic methods and ordering - ok");}
@@ -37,7 +135,7 @@ public class Ajc160Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
   public void testIncorrectOptimizationOfIstore_pr166084() { runTest("incorrect optimization of istore"); }
   public void testDualParameterizationsNotAllowed_pr165631() { runTest("dual parameterizations not allowed"); }
        
-       public void testSuppressWarnings1_pr166238() {
+       public void testSuppressWarnings1_pr166238() {
                runTest("Suppress warnings1");
        }
 
@@ -53,6 +151,16 @@ public class Ajc160Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
   protected File getSpecFile() {
     return new File("../tests/src/org/aspectj/systemtest/ajc160/ajc160.xml");
   }
+  
+  public SyntheticRepository createRepos(File cpentry) {
+       ClassPath cp = new ClassPath(cpentry+File.pathSeparator+System.getProperty("java.class.path"));
+       return SyntheticRepository.getInstance(cp);
+  }
+  
+  protected JavaClass getClassFrom(File where,String clazzname) throws ClassNotFoundException {
+       SyntheticRepository repos = createRepos(where);
+       return repos.loadClass(clazzname);
+  }
 
   
 }
\ No newline at end of file
index 128e18a03703c294f3e8057c97668d33dc450363..229027f5f0bacd401026fb336107301565f22bf0 100644 (file)
@@ -5,6 +5,10 @@
 
  <!-- first section - dont need a 1.6 vm but fixed in the 1.6 branch of AspectJ -->
  
+   <ajc-test dir="bugs160/pr175806" title="coping with bad tables">
+     <compile options="-1.5" files="A.java"/>
+   </ajc-test>
+   
    <ajc-test dir="bugs160/pr174449" title="problem with generic aspect and generic pointcut">
      <compile options="-1.5" files="Foo.java"/>
      <run class="Foo">