8008678: JSR 292: constant pool reconstitution must support pseudo strings
authorsspitsyn
Thu, 29 Jan 2015 03:11:01 -0800
changeset 28741 1f10b1bd612d
parent 28740 3ebd3353fda5
child 28742 e6ec6ed5be00
child 28817 09a44329d68d
8008678: JSR 292: constant pool reconstitution must support pseudo strings Summary: Keep orig idx from pseudo-string to UTF8, use 2nd lsb CPSlot to mark pseudo-string. Reviewed-by: coleenp, jrose Contributed-by: serguei.spitsyn@oracle.com
hotspot/src/share/vm/oops/constantPool.cpp
hotspot/src/share/vm/oops/constantPool.hpp
hotspot/src/share/vm/prims/methodComparator.cpp
hotspot/test/serviceability/jvmti/TestLambdaFormRetransformation.java
--- a/hotspot/src/share/vm/oops/constantPool.cpp	Thu Jan 29 03:54:44 2015 +0000
+++ b/hotspot/src/share/vm/oops/constantPool.cpp	Thu Jan 29 03:11:01 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -493,12 +493,7 @@
 }
 
 char* ConstantPool::string_at_noresolve(int which) {
-  Symbol* s = unresolved_string_at(which);
-  if (s == NULL) {
-    return (char*)"<pseudo-string>";
-  } else {
-    return unresolved_string_at(which)->as_C_string();
-  }
+  return unresolved_string_at(which)->as_C_string();
 }
 
 BasicType ConstantPool::basic_type_for_signature_at(int which) {
@@ -1828,7 +1823,7 @@
       // explicitly, because it may require scavenging.
       int obj_index = cp_to_object_index(index);
       pseudo_string_at_put(index, obj_index, patch());
-      DEBUG_ONLY(cp_patches->at_put(index, Handle());)
+     DEBUG_ONLY(cp_patches->at_put(index, Handle());)
     }
   }
 #ifdef ASSERT
--- a/hotspot/src/share/vm/oops/constantPool.hpp	Thu Jan 29 03:54:44 2015 +0000
+++ b/hotspot/src/share/vm/oops/constantPool.hpp	Thu Jan 29 03:11:01 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,17 +48,21 @@
 class CPSlot VALUE_OBJ_CLASS_SPEC {
   intptr_t _ptr;
  public:
+  enum TagBits  { _resolved_value = 0, _symbol_bit = 1, _pseudo_bit = 2, _symbol_mask = 3 };
+
   CPSlot(intptr_t ptr): _ptr(ptr) {}
   CPSlot(Klass* ptr): _ptr((intptr_t)ptr) {}
-  CPSlot(Symbol* ptr): _ptr((intptr_t)ptr | 1) {}
+  CPSlot(Symbol* ptr): _ptr((intptr_t)ptr | _symbol_bit) {}
+  CPSlot(Symbol* ptr, int tag_bits): _ptr((intptr_t)ptr | tag_bits) {}
 
   intptr_t value()   { return _ptr; }
-  bool is_resolved()   { return (_ptr & 1) == 0; }
-  bool is_unresolved() { return (_ptr & 1) == 1; }
+  bool is_resolved()      { return (_ptr & _symbol_bit ) == _resolved_value; }
+  bool is_unresolved()    { return (_ptr & _symbol_bit ) != _resolved_value; }
+  bool is_pseudo_string() { return (_ptr & _symbol_mask) == _symbol_bit + _pseudo_bit; }
 
   Symbol* get_symbol() {
     assert(is_unresolved(), "bad call");
-    return (Symbol*)(_ptr & ~1);
+    return (Symbol*)(_ptr & ~_symbol_mask);
   }
   Klass* get_klass() {
     assert(is_resolved(), "bad call");
@@ -261,7 +265,7 @@
 
   void unresolved_string_at_put(int which, Symbol* s) {
     release_tag_at_put(which, JVM_CONSTANT_String);
-    *symbol_at_addr(which) = s;
+    slot_at_put(which, CPSlot(s, CPSlot::_symbol_bit));
   }
 
   void int_at_put(int which, jint i) {
@@ -405,20 +409,18 @@
   // use pseudo-strings to link themselves to related metaobjects.
 
   bool is_pseudo_string_at(int which) {
-    // A pseudo string is a string that doesn't have a symbol in the cpSlot
-    return unresolved_string_at(which) == NULL;
+    assert(tag_at(which).is_string(), "Corrupted constant pool");
+    return slot_at(which).is_pseudo_string();
   }
 
   oop pseudo_string_at(int which, int obj_index) {
-    assert(tag_at(which).is_string(), "Corrupted constant pool");
-    assert(unresolved_string_at(which) == NULL, "shouldn't have symbol");
+    assert(is_pseudo_string_at(which), "must be a pseudo-string");
     oop s = resolved_references()->obj_at(obj_index);
     return s;
   }
 
   oop pseudo_string_at(int which) {
-    assert(tag_at(which).is_string(), "Corrupted constant pool");
-    assert(unresolved_string_at(which) == NULL, "shouldn't have symbol");
+    assert(is_pseudo_string_at(which), "must be a pseudo-string");
     int obj_index = cp_to_object_index(which);
     oop s = resolved_references()->obj_at(obj_index);
     return s;
@@ -426,7 +428,8 @@
 
   void pseudo_string_at_put(int which, int obj_index, oop x) {
     assert(tag_at(which).is_string(), "Corrupted constant pool");
-    unresolved_string_at_put(which, NULL); // indicates patched string
+    Symbol* sym = unresolved_string_at(which);
+    slot_at_put(which, CPSlot(sym, (CPSlot::_symbol_bit | CPSlot::_pseudo_bit)));
     string_at_put(which, obj_index, x);    // this works just fine
   }
 
@@ -443,15 +446,14 @@
 
   Symbol* unresolved_string_at(int which) {
     assert(tag_at(which).is_string(), "Corrupted constant pool");
-    Symbol* s = *symbol_at_addr(which);
-    return s;
+    Symbol* sym = slot_at(which).get_symbol();
+    return sym;
   }
 
   // Returns an UTF8 for a CONSTANT_String entry at a given index.
   // UTF8 char* representation was chosen to avoid conversion of
   // java_lang_Strings at resolved entries into Symbol*s
   // or vice versa.
-  // Caller is responsible for checking for pseudo-strings.
   char* string_at_noresolve(int which);
 
   jint name_and_type_at(int which) {
--- a/hotspot/src/share/vm/prims/methodComparator.cpp	Thu Jan 29 03:54:44 2015 +0000
+++ b/hotspot/src/share/vm/prims/methodComparator.cpp	Thu Jan 29 03:11:01 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -405,6 +405,8 @@
     if (strcmp(_old_cp->string_at_noresolve(cpi_old),
                _new_cp->string_at_noresolve(cpi_new)) != 0)
       return false;
+    if (_old_cp->is_pseudo_string_at(cpi_old) || _new_cp->is_pseudo_string_at(cpi_new))
+      return (_old_cp->is_pseudo_string_at(cpi_old) == _new_cp->is_pseudo_string_at(cpi_new));
   } else if (tag_old.is_klass() || tag_old.is_unresolved_klass()) {
     // tag_old should be klass - 4881222
     if (! (tag_new.is_unresolved_klass() || tag_new.is_klass()))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/jvmti/TestLambdaFormRetransformation.java	Thu Jan 29 03:11:01 2015 -0800
@@ -0,0 +1,138 @@
+
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8008678
+ * @summary JSR 292: constant pool reconstitution must support pseudo strings
+ * @library /testlibrary
+ * @compile -XDignore.symbol.file TestLambdaFormRetransformation.java
+ * @run main TestLambdaFormRetransformation
+ */
+
+import java.io.IOException;
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
+import java.lang.instrument.Instrumentation;
+import java.lang.instrument.UnmodifiableClassException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.ProtectionDomain;
+import java.util.Arrays;
+
+import com.oracle.java.testlibrary.ExitCode;
+import com.oracle.java.testlibrary.OutputAnalyzer;
+import com.oracle.java.testlibrary.ProcessTools;
+
+public class TestLambdaFormRetransformation {
+    private static String MANIFEST = String.format("Manifest-Version: 1.0\n" +
+                                                   "Premain-Class: %s\n" +
+                                                   "Can-Retransform-Classes: true\n",
+                                                   Agent.class.getName());
+
+    private static String CP = System.getProperty("test.classes");
+
+    public static void main(String args[]) throws Throwable {
+        Path agent = TestLambdaFormRetransformation.buildAgent();
+        OutputAnalyzer oa = ProcessTools.executeTestJvm("-javaagent:" +
+                                agent.toAbsolutePath().toString(), "-version");
+        oa.shouldHaveExitValue(ExitCode.OK.value);
+    }
+
+    private static Path buildAgent() throws IOException {
+        Path manifest = TestLambdaFormRetransformation.createManifest();
+        Path jar = Files.createTempFile(Paths.get("."), null, ".jar");
+
+        String[] args = new String[] {
+            "-cfm",
+            jar.toAbsolutePath().toString(),
+            manifest.toAbsolutePath().toString(),
+            "-C",
+            TestLambdaFormRetransformation.CP,
+            Agent.class.getName() + ".class"
+        };
+
+        sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
+
+        if (!jarTool.run(args)) {
+            throw new Error("jar failed: args=" + Arrays.toString(args));
+        }
+        return jar;
+    }
+
+    private static Path createManifest() throws IOException {
+        Path manifest = Files.createTempFile(Paths.get("."), null, ".mf");
+        byte[] manifestBytes = TestLambdaFormRetransformation.MANIFEST.getBytes();
+        Files.write(manifest, manifestBytes);
+        return manifest;
+    }
+}
+
+class Agent implements ClassFileTransformer {
+    private static Runnable lambda = () -> {
+        System.out.println("I'll crash you!");
+    };
+
+    public static void premain(String args, Instrumentation instrumentation) {
+        if (!instrumentation.isRetransformClassesSupported()) {
+            System.out.println("Class retransformation is not supported.");
+            return;
+        }
+        System.out.println("Calling lambda to ensure that lambda forms were created");
+
+        Agent.lambda.run();
+
+        System.out.println("Registering class file transformer");
+
+        instrumentation.addTransformer(new Agent());
+
+        for (Class c : instrumentation.getAllLoadedClasses()) {
+            if (c.getName().contains("LambdaForm") &&
+                instrumentation.isModifiableClass(c)) {
+                System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
+                try {
+                    instrumentation.retransformClasses(c);
+                } catch (UnmodifiableClassException e) {
+                    throw new AssertionError("Modification of modifiable class " +
+                                             "caused UnmodifiableClassException", e);
+                }
+            }
+        }
+    }
+
+    public static void main(String args[]) {
+    }
+
+    @Override
+    public byte[] transform(ClassLoader loader,
+                            String className,
+                            Class<?> classBeingRedefined,
+                            ProtectionDomain protectionDomain,
+                            byte[] classfileBuffer
+                           ) throws IllegalClassFormatException {
+        System.out.println("Transforming " + className);
+        return classfileBuffer.clone();
+    }
+}