From 10fcad70b3894023d65716b42dc67c1a2bda9c03 Mon Sep 17 00:00:00 2001 From: David Holmes Date: Fri, 19 Jul 2024 06:23:11 +0000 Subject: [PATCH] 8325945: Error reporting should limit the number of String characters printed Reviewed-by: thartmann, stuefe --- src/hotspot/share/classfile/javaClasses.cpp | 23 +++- src/hotspot/share/classfile/javaClasses.hpp | 2 +- src/hotspot/share/prims/whitebox.cpp | 9 ++ src/hotspot/share/runtime/globals.hpp | 6 + .../runtime/PrintingTests/StringPrinting.java | 122 ++++++++++++++++++ test/lib/jdk/test/whitebox/WhiteBox.java | 7 +- 6 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 test/hotspot/jtreg/runtime/PrintingTests/StringPrinting.java diff --git a/src/hotspot/share/classfile/javaClasses.cpp b/src/hotspot/share/classfile/javaClasses.cpp index c0a413f1868..a0b5b73c6d4 100644 --- a/src/hotspot/share/classfile/javaClasses.cpp +++ b/src/hotspot/share/classfile/javaClasses.cpp @@ -751,8 +751,16 @@ bool java_lang_String::equals(oop str1, oop str2) { return value_equals(value1, value2); } -void java_lang_String::print(oop java_string, outputStream* st) { +// Print the given string to the given outputStream, limiting the output to +// at most max_length of the string's characters. If the length exceeds the +// limit we print an abridged version of the string with the "middle" elided +// and replaced by " ... (N characters ommitted) ... ". If max_length is odd +// it is treated as max_length-1. +void java_lang_String::print(oop java_string, outputStream* st, int max_length) { assert(java_string->klass() == vmClasses::String_klass(), "must be java_string"); + // We need at least two characters to print A ... B + assert(max_length > 1, "invalid max_length: %d", max_length); + typeArrayOop value = java_lang_String::value_no_keepalive(java_string); if (value == nullptr) { @@ -765,8 +773,17 @@ void java_lang_String::print(oop java_string, outputStream* st) { int length = java_lang_String::length(java_string, value); bool is_latin1 = java_lang_String::is_latin1(java_string); + bool abridge = length > max_length; + st->print("\""); for (int index = 0; index < length; index++) { + // If we need to abridge and we've printed half the allowed characters + // then jump to the tail of the string. + if (abridge && index >= max_length / 2) { + st->print(" ... (%d characters ommitted) ... ", length - 2 * (max_length / 2)); + index = length - (max_length / 2); + abridge = false; // only do this once + } jchar c = (!is_latin1) ? value->char_at(index) : ((jchar) value->byte_at(index)) & 0xff; if (c < ' ') { @@ -776,6 +793,10 @@ void java_lang_String::print(oop java_string, outputStream* st) { } } st->print("\""); + + if (length > max_length) { + st->print(" (abridged) "); + } } // java_lang_Class diff --git a/src/hotspot/share/classfile/javaClasses.hpp b/src/hotspot/share/classfile/javaClasses.hpp index 973f2f8f524..57914e189b7 100644 --- a/src/hotspot/share/classfile/javaClasses.hpp +++ b/src/hotspot/share/classfile/javaClasses.hpp @@ -194,7 +194,7 @@ class java_lang_String : AllStatic { static inline bool is_instance(oop obj); // Debugging - static void print(oop java_string, outputStream* st); + static void print(oop java_string, outputStream* st, int max_length = MaxStringPrintSize); friend class JavaClasses; friend class StringTable; }; diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index a799d5b5de4..43e47409c4c 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -179,6 +179,14 @@ WB_ENTRY(jlong, WB_GetVMLargePageSize(JNIEnv* env, jobject o)) return os::large_page_size(); WB_END +WB_ENTRY(jstring, WB_PrintString(JNIEnv* env, jobject wb, jstring str, jint max_length)) + ResourceMark rm(THREAD); + stringStream sb; + java_lang_String::print(JNIHandles::resolve(str), &sb, max_length); + oop result = java_lang_String::create_oop_from_str(sb.as_string(), THREAD); + return (jstring) JNIHandles::make_local(THREAD, result); +WB_END + class WBIsKlassAliveClosure : public LockedClassesDo { Symbol* _name; int _count; @@ -2959,6 +2967,7 @@ static JNINativeMethod methods[] = { {CC"preTouchMemory", CC"(JJ)V", (void*)&WB_PreTouchMemory}, {CC"cleanMetaspaces", CC"()V", (void*)&WB_CleanMetaspaces}, {CC"rss", CC"()J", (void*)&WB_Rss}, + {CC"printString", CC"(Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_PrintString}, }; diff --git a/src/hotspot/share/runtime/globals.hpp b/src/hotspot/share/runtime/globals.hpp index cbd161d86eb..7a7a3769ac9 100644 --- a/src/hotspot/share/runtime/globals.hpp +++ b/src/hotspot/share/runtime/globals.hpp @@ -1304,6 +1304,12 @@ const int ObjectAlignmentInBytes = 8; develop(int, MaxElementPrintSize, 256, \ "maximum number of elements to print") \ \ + develop(int, MaxStringPrintSize, 256, \ + "maximum number of characters to print for a java.lang.String " \ + "in the VM. If exceeded, an abridged version of the string is " \ + "printed with the middle of the string elided.") \ + range(2, O_BUFLEN) \ + \ develop(intx, MaxSubklassPrintSize, 4, \ "maximum number of subklasses to print when printing klass") \ \ diff --git a/test/hotspot/jtreg/runtime/PrintingTests/StringPrinting.java b/test/hotspot/jtreg/runtime/PrintingTests/StringPrinting.java new file mode 100644 index 00000000000..fffcdd72d15 --- /dev/null +++ b/test/hotspot/jtreg/runtime/PrintingTests/StringPrinting.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024, 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 8325945 + * @summary Test abridged VM String printing + * @library /test/lib + * @build jdk.test.whitebox.WhiteBox + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+WhiteBoxAPI StringPrinting + */ + +import jdk.test.whitebox.WhiteBox; + +public class StringPrinting { + + private static final WhiteBox WB = WhiteBox.getWhiteBox(); + + static void checkEqual(String s1, String s2) { + if (!s1.equals(s2)) { + throw new RuntimeException("Different strings: " + s1 + " vs " + s2); + } + } + + static void checkEqual(int len1, int len2) { + if (len1 != len2) { + throw new RuntimeException("Different lengths: " + len1 + " vs " + len2); + } + } + + public static void main(String[] args) { + // Modified string format is "xxx ... (N characters ommitted) ... xxx" (abridged) + final String elipse = " ... "; + final String text = " characters ommitted)"; + final String abridged = "\" (abridged) "; + + // Define a set of maxLengths for ease of inspection in the test outout + // Note: maxLength must be >= 2 + int[] maxLengths = new int[] { 2, 3, 16, 256, 512 }; + for (int maxLength : maxLengths) { + // Test string lengths around maxLength and "much" bigger + // than maxLength + int[] strLengths = new int[] { maxLength - 1, + maxLength, + maxLength + 1, + 2 * maxLength + }; + for (int length : strLengths) { + System.out.println("Testing string length " + length + " with maxLength " + maxLength); + String s = "x".repeat(length); + String r = WB.printString(s, maxLength); + if (length <= maxLength) { + // Strip off the double-quotes that surround the string + if (r.charAt(0) == '\"' && r.charAt(r.length() - 1) == '\"') { + r = r.substring(1, r.length() - 1); + } else { + throw new RuntimeException("String was not quoted as expected: " + r); + } + checkEqual(s, r); + } else { + // Strip off leading double-quote + if (r.charAt(0) == '\"') { + r = r.substring(1, r.length()); + } else { + throw new RuntimeException("String was not quoted as expected: " + r); + } + + // Strip off abridged + if (r.endsWith(abridged)) { + r = r.substring(0, r.length() - abridged.length()); + } else { + throw new RuntimeException("String was not marked abridged as expected: " + r); + } + + // Now extract the two "halves" + int elipseStart = r.indexOf(elipse); + String firstHalf = r.substring(0, elipseStart); + int secondElipseStart = r.lastIndexOf(elipse); + String secondHalf = r.substring(secondElipseStart + elipse.length(), r.length()); + + System.out.println("S1: >" + firstHalf + "<"); + System.out.println("S2: >" + secondHalf + "<"); + + checkEqual(firstHalf.length(), maxLength / 2); + checkEqual(secondHalf.length(), maxLength /2); + + // Now check number of characters ommitted + String tail = r.substring(r.indexOf("("), r.length()); + int numberEnd = tail.indexOf(" "); + String nChars = tail.substring(1, numberEnd); + System.out.println("N: >" + nChars + "<"); + + // Now add all the bits back together to get the expected full length + int fullLength = maxLength / 2 + elipse.length() + 1 /* for ( */ + + nChars.length() + text.length() + elipse.length() + maxLength / 2; + checkEqual(r.length(), fullLength); + } + } + } + } +} diff --git a/test/lib/jdk/test/whitebox/WhiteBox.java b/test/lib/jdk/test/whitebox/WhiteBox.java index 1bc309e2aec..607e9552945 100644 --- a/test/lib/jdk/test/whitebox/WhiteBox.java +++ b/test/lib/jdk/test/whitebox/WhiteBox.java @@ -99,8 +99,13 @@ public class WhiteBox { } // Runtime - // Make sure class name is in the correct format + + // Returns the potentially abridged form of `str` as it would be + // printed by the VM. + public native String printString(String str, int maxLength); + public int countAliveClasses(String name) { + // Make sure class name is in the correct format return countAliveClasses0(name.replace('.', '/')); } private native int countAliveClasses0(String name);