changeset 17:e3e13e71f94d

CODETOOLS-7902063 Circular references through ConstantDynamic should be caught in the proper way, contributed by: leonid.kuskov@oracle.com
author afedorch
date Tue, 07 Nov 2017 15:28:57 -0800
parents 559c6d8df174
children 51618da7aecc
files src/org/openjdk/asmtools/jdis/ClassData.java src/org/openjdk/asmtools/jdis/ConstantPool.java
diffstat 2 files changed, 45 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/openjdk/asmtools/jdis/ClassData.java	Tue Nov 07 15:26:12 2017 -0800
+++ b/src/org/openjdk/asmtools/jdis/ClassData.java	Tue Nov 07 15:28:57 2017 -0800
@@ -28,7 +28,9 @@
 
 import java.io.*;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import static org.openjdk.asmtools.jasm.RuntimeConstants.*;
 import static org.openjdk.asmtools.jasm.Tables.*;
@@ -493,11 +495,22 @@
                 out.print(" @" + moduleData.getModuleVersion());
             out.println();
         }
+
+        List<IOException> issues = getIssues();
+        if( !issues.isEmpty() ) {
+
+            throw issues.get(0);
+        }
     } // end ClassData.print()
 
     // Gets the type of processed binary
     private boolean isModuleUnit() {
         return moduleData != null;
     }
+
+    private List<IOException> getIssues() {
+        return this.pool.pool.stream().filter(c->c!=null).filter(c->c.getIssue() != null).map(c->c.getIssue()).collect(Collectors.toList());
+    }
+
 }// end class ClassData
 
--- a/src/org/openjdk/asmtools/jdis/ConstantPool.java	Tue Nov 07 15:26:12 2017 -0800
+++ b/src/org/openjdk/asmtools/jdis/ConstantPool.java	Tue Nov 07 15:28:57 2017 -0800
@@ -29,6 +29,7 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Hashtable;
+import java.util.IllegalFormatException;
 
 /**
  *
@@ -42,6 +43,8 @@
     private static final Hashtable<Byte, TAG> taghash = new Hashtable<>();
     private static final Hashtable<Byte, SUBTAG> subtaghash = new Hashtable<>();
 
+    private Throwable foundProblem;
+
     /*-------------------------------------------------------- */
     /* ConstantPool Inner Classes */
     /**
@@ -216,6 +219,17 @@
         public String toString() {
             return "<CONSTANT " + tag.toString() + " " + stringVal() + ">";
         }
+
+        private IOException issue;
+
+        public IOException getIssue() {
+            return issue;
+        }
+
+        public void setIssue(IOException value) {
+            issue = value;
+        }
+
     }
 
     /* -------------------------------------------------------- */
@@ -527,8 +541,7 @@
                     } catch (NullPointerException npe) {
                         return "<Missing BootstrapMethods attribute>";
                     } catch (IndexOutOfBoundsException ioob) {
-                        return "<Invalid bootstrap method index:"
-                                + bsm_attr_idx + ">";
+                        return "<Invalid bootstrap method index:" + bsm_attr_idx + ">";
                     }
 
                     int bsm_ref = bsmData.bsm_index;
@@ -537,16 +550,25 @@
                     int bsm_args_len = bsmData.bsm_args_indexes.size();
                     for (int i = 0; i < bsm_args_len; i++) {
                         int bsm_arg_idx = bsmData.bsm_args_indexes.get(i);
-                        bsm_args_str.append(" ")
-                                .append(ConstantStrValue(bsm_arg_idx));
-                        if (i + 1 < bsm_args_len) {
-                            bsm_args_str.append(",");
+                        Constant cnt = pool.get(bsm_arg_idx);
+                        if(cnt.equals(this)) {
+                            String s = "circular reference to " + cnt.tag.tagname() + " #" + bsm_arg_idx;
+                            bsm_args_str.append("  <")
+                                    .append(s)
+                                    .append(">");
+                            cnt.setIssue(new IOException(s));
+                        } else {
+                            bsm_args_str.append(" ")
+                                    .append(ConstantStrValue(bsm_arg_idx));
+                            if (i + 1 < bsm_args_len) {
+                                bsm_args_str.append(",");
+                            }
                         }
                     }
-
-                    str = StringValue(bsm_ref) + ":" + StringValue(nape_idx)
-                          + bsm_args_str.toString();
-                    break;
+                    str = StringValue(bsm_ref) +
+                            ":" +
+                            StringValue(nape_idx) +
+                            bsm_args_str.toString();
                 default:
                     break;
             }