Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8271003: hs_err improvement: handle CLASSPATH env setting longer than O_BUFLEN #4947

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -131,10 +131,33 @@ void outputStream::do_vsnprintf_and_write_with_automatic_buffer(const char* form
write(str, len);
}

const char* outputStream::handle_simple_format(const char* format, va_list ap, size_t& len) {
const char* str = nullptr;
if (strchr(format, '%') == nullptr) {
// constant format string
str = format;
len = strlen(str);
} else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') {
Copy link
Member

@dholmes-ora dholmes-ora Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you assuming the %s must be at the beginning rather than "blah blah %s blah" ?

Copy link
Member

@dholmes-ora dholmes-ora Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see what you are doing now - the code just special cases the two cases of a string with no format specifiers, and a string that is only "%s". Anything else needs full handling.

// trivial copy-through format string
str = va_arg(ap, const char*);
len = strlen(str);
}
return str;
}

void outputStream::do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) {
size_t len;
const char* str = do_vsnprintf(_scratch, _scratch_len, format, ap, add_cr, len);
const char* str = nullptr;
bool simple_format = true;
str = handle_simple_format(format, ap, len);
if (str == nullptr) {
str = do_vsnprintf(_scratch, _scratch_len, format, ap, add_cr, len);
simple_format = false;
}
write(str, len);
if (simple_format && add_cr) {
cr();
}
}

void outputStream::do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) {
@@ -67,6 +67,7 @@ class outputStream : public ResourceObj {
void do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
// calls do_vsnprintf, then writes output to stream.
void do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0);
const char* handle_simple_format(const char* format, va_list ap, size_t& len);

public:
// creation
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2021, 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 8271003
* @summary CLASSPATH env variable setting should not be truncated in a hs err log.
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.compiler
* java.management
* jdk.internal.jvmstat/sun.jvmstat.monitor
* @run driver ClassPathEnvVar
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.Platform;
import jdk.internal.misc.Unsafe;

public class ClassPathEnvVar {
private static final String pathSep = File.pathSeparator;
private static final String sep = File.separator;
private static final String cp_env = "CLASSPATH";
private static final String end_path = "end-path";

private static class Crasher {
Copy link
Member

@dholmes-ora dholmes-ora Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just use one of the existing -XX:ErrorHandlerTest=n values to trigger the crash?

Copy link
Member Author

@calvinccheung calvinccheung Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using -XX:ErrorHandlerTest=1 should trigger the crash. I was just reusing the Crasher from another test case.

public static void main(String[] args) {
Unsafe.getUnsafe().putInt(0L, 0);
}
}

public static void main(String[] args) throws Exception {
OutputAnalyzer output = runCrasher("-XX:-CreateCoredumpOnCrash").shouldContain("CreateCoredumpOnCrash turned off, no core file dumped")
.shouldNotHaveExitValue(0);

checkErrorLog(output);

}
private static OutputAnalyzer runCrasher(String option) throws Exception {
ProcessBuilder pb =
ProcessTools.createJavaProcessBuilder(
"-Xmx128m", "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED", option, Crasher.class.getName());

// Obtain the CLASSPATH setting and expand it to more than 2000 chars.
Map<String, String> envMap = pb.environment();
Set<String> keys = envMap.keySet();
String cp = envMap.get(cp_env);
if (cp == null) {
cp = "this" + sep + "is" + sep + "dummy" + sep + "path";
}
while (cp.length() < 2000) {
cp += pathSep + cp;
}
cp += pathSep + end_path;
envMap.put(cp_env, cp);

return new OutputAnalyzer(pb.start());
}

private static void checkErrorLog(OutputAnalyzer output) throws Exception {
String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
System.out.println(" hs_err_file " + hs_err_file);
File f = new File(hs_err_file);
String absPath = f.getAbsolutePath();
if (!f.exists()) {
throw new RuntimeException("hs err log missing at " + absPath);
}

String cp_line = null;
try (
// Locate the line begins with "CLASSPATH".
FileInputStream fis = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fis))) {
String line = null;
while ((line = br.readLine()) != null) {
if (line.startsWith(cp_env)) {
cp_line = line;
break;
}
}
}

if (cp_line == null) {
throw new RuntimeException("CLASSPATH setting not found in hs err log: " + absPath);
}

// Check if the CLASSPATH line has been truncated.
if (!cp_line.endsWith(end_path)) {
throw new RuntimeException("CLASSPATH was truncated in the hs err log: " + absPath);
}
}
}