changeset 59417:dd322637eaa0

8244946: fatal error: memory leak: allocating without ResourceMark with -XX:+Verbose -Xlog:methodhandles Summary: Fix crash due to a missing ResourceMark when logging methodhandles with Verbose. Reviewed-by: dcubed, dholmes, coleenp
author chagedorn
date Mon, 25 May 2020 11:34:11 +0200
parents 072255c78f53
children 8113059a83b2
files src/hotspot/cpu/arm/methodHandles_arm.cpp src/hotspot/cpu/ppc/methodHandles_ppc.cpp src/hotspot/cpu/x86/methodHandles_x86.cpp src/hotspot/share/oops/klass.cpp test/hotspot/jtreg/runtime/logging/TestMethodHandlesVerbose.java
diffstat 5 files changed, 70 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/cpu/arm/methodHandles_arm.cpp	Mon May 25 11:05:23 2020 +0200
+++ b/src/hotspot/cpu/arm/methodHandles_arm.cpp	Mon May 25 11:34:11 2020 +0200
@@ -488,6 +488,7 @@
   if (last_sp != saved_sp && last_sp != NULL)
     tty->print_cr("*** last_sp=" INTPTR_FORMAT, p2i(last_sp));
   if (Verbose) {
+    ResourceMark rm;
     tty->print(" reg dump: ");
     int i;
     for (i = 0; i < trace_mh_nregs; i++) {
@@ -497,44 +498,43 @@
       tty->print(" %s: " INTPTR_FORMAT, reg_name, p2i((void *)saved_regs[i]));
     }
     tty->cr();
-  }
 
-  if (Verbose) {
-    // dump last frame (from JavaThread::print_frame_layout)
+    {
+      // dump last frame (from JavaThread::print_frame_layout)
 
-    // Note: code is robust but the dumped informationm may not be
-    // 100% correct, particularly with respect to the dumped
-    // "unextended_sp". Getting it right for all trace_method_handle
-    // call paths is not worth the complexity/risk. The correct slot
-    // will be identified by *Rsender_sp anyway in the dump.
-    JavaThread* p = JavaThread::active();
+      // Note: code is robust but the dumped informationm may not be
+      // 100% correct, particularly with respect to the dumped
+      // "unextended_sp". Getting it right for all trace_method_handle
+      // call paths is not worth the complexity/risk. The correct slot
+      // will be identified by *Rsender_sp anyway in the dump.
+      JavaThread* p = JavaThread::active();
 
-    ResourceMark rm;
-    PRESERVE_EXCEPTION_MARK;
-    FrameValues values;
+      PRESERVE_EXCEPTION_MARK;
+      FrameValues values;
 
-    intptr_t* dump_fp = (intptr_t *) saved_bp;
-    address dump_pc = (address) saved_regs[trace_mh_nregs-2]; // LR (with LR,PC last in saved_regs)
-    frame dump_frame((intptr_t *)entry_sp, dump_fp, dump_pc);
+      intptr_t* dump_fp = (intptr_t *) saved_bp;
+      address dump_pc = (address) saved_regs[trace_mh_nregs-2]; // LR (with LR,PC last in saved_regs)
+      frame dump_frame((intptr_t *)entry_sp, dump_fp, dump_pc);
 
-    dump_frame.describe(values, 1);
-    // mark Rsender_sp if seems valid
-    if (has_mh) {
-      if ((saved_sp >= entry_sp - UNREASONABLE_STACK_MOVE) && (saved_sp < dump_fp)) {
-        values.describe(-1, saved_sp, "*Rsender_sp");
+      dump_frame.describe(values, 1);
+      // mark Rsender_sp if seems valid
+      if (has_mh) {
+        if ((saved_sp >= entry_sp - UNREASONABLE_STACK_MOVE) && (saved_sp < dump_fp)) {
+          values.describe(-1, saved_sp, "*Rsender_sp");
+        }
       }
+
+      // Note: the unextended_sp may not be correct
+      tty->print_cr("  stack layout:");
+      values.print(p);
     }
 
-    // Note: the unextended_sp may not be correct
-    tty->print_cr("  stack layout:");
-    values.print(p);
-  }
-  if (Verbose) {
     if (has_mh && oopDesc::is_oop(mh)) {
       mh->print();
       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
-        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0)
+        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0) {
           java_lang_invoke_MethodHandle::form(mh)->print();
+        }
       }
     }
   }
--- a/src/hotspot/cpu/ppc/methodHandles_ppc.cpp	Mon May 25 11:05:23 2020 +0200
+++ b/src/hotspot/cpu/ppc/methodHandles_ppc.cpp	Mon May 25 11:34:11 2020 +0200
@@ -483,6 +483,7 @@
                 adaptername, mh_reg_name, p2i(mh), p2i(entry_sp));
 
   if (Verbose) {
+    ResourceMark rm;
     tty->print_cr("Registers:");
     const int abi_offset = frame::abi_reg_args_size / 8;
     for (int i = R3->encoding(); i <= R12->encoding(); i++) {
@@ -503,7 +504,6 @@
 
       JavaThread* p = JavaThread::active();
 
-      ResourceMark rm;
       PRESERVE_EXCEPTION_MARK; // may not be needed by safer and unexpensive here
       FrameValues values;
 
@@ -538,8 +538,9 @@
     if (has_mh && oopDesc::is_oop(mh)) {
       mh->print();
       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
-        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0)
+        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0) {
           java_lang_invoke_MethodHandle::form(mh)->print();
+        }
       }
     }
   }
--- a/src/hotspot/cpu/x86/methodHandles_x86.cpp	Mon May 25 11:05:23 2020 +0200
+++ b/src/hotspot/cpu/x86/methodHandles_x86.cpp	Mon May 25 11:34:11 2020 +0200
@@ -502,6 +502,7 @@
                 p2i(mh), p2i(entry_sp));
 
   if (Verbose) {
+    ResourceMark rm;
     tty->print_cr("Registers:");
     const int saved_regs_count = RegisterImpl::number_of_registers;
     for (int i = 0; i < saved_regs_count; i++) {
@@ -527,12 +528,11 @@
     tty->cr();
 
     {
-     // dumping last frame with frame::describe
+      // dumping last frame with frame::describe
 
       JavaThread* p = JavaThread::active();
 
-      ResourceMark rm;
-      PRESERVE_EXCEPTION_MARK; // may not be needed by safer and unexpensive here
+      PRESERVE_EXCEPTION_MARK; // may not be needed but safer and inexpensive here
       FrameValues values;
 
       // Note: We want to allow trace_method_handle from any call site.
@@ -581,8 +581,9 @@
     if (has_mh && oopDesc::is_oop(mh)) {
       mh->print();
       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
-        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0)
+        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0) {
           java_lang_invoke_MethodHandle::form(mh)->print();
+        }
       }
     }
   }
--- a/src/hotspot/share/oops/klass.cpp	Mon May 25 11:05:23 2020 +0200
+++ b/src/hotspot/share/oops/klass.cpp	Mon May 25 11:34:11 2020 +0200
@@ -774,6 +774,7 @@
 
 #define BULLET  " - "
 
+// Caller needs ResourceMark
 void Klass::oop_print_on(oop obj, outputStream* st) {
   // print title
   st->print_cr("%s ", internal_name());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/logging/TestMethodHandlesVerbose.java	Mon May 25 11:34:11 2020 +0200
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2020, 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 8244946
+ * @summary Run simple test with -XX:+Verbose and -Xlog:methodhandles.
+ *
+ * @run main/othervm -XX:+Verbose -Xlog:methodhandles TestMethodHandlesVerbose
+ */
+
+public class TestMethodHandlesVerbose {
+    public static void main(String[] args) {
+    }
+}