Skip to content

Commit

Permalink
@tstuefe comments
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinccheung committed Aug 3, 2021
1 parent 0ed841c commit 35eccdc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 77 deletions.
3 changes: 2 additions & 1 deletion src/hotspot/share/runtime/os.cpp
Expand Up @@ -989,7 +989,8 @@ void os::print_environment_variables(outputStream* st, const char** env_list) {
if (envvar != NULL) {
st->print("%s", env_list[i]);
st->print("=");
st->print_cr("%s", envvar);
st->print("%s", envvar);
st->cr();
}
}
}
Expand Down
25 changes: 1 addition & 24 deletions src/hotspot/share/utilities/ostream.cpp
Expand Up @@ -131,33 +131,10 @@ 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') {
// 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 = 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;
}
const char* str = do_vsnprintf(_scratch, _scratch_len, format, ap, add_cr, len);
write(str, len);
if (simple_format && add_cr) {
cr();
}
}

void outputStream::do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) {
Expand Down
1 change: 0 additions & 1 deletion src/hotspot/share/utilities/ostream.hpp
Expand Up @@ -67,7 +67,6 @@ 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
Expand Down
66 changes: 15 additions & 51 deletions test/hotspot/jtreg/runtime/ErrorHandling/ClassPathEnvVar.java
Expand Up @@ -26,94 +26,58 @@
* @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 {
public static void main(String[] args) {
Unsafe.getUnsafe().putInt(0L, 0);
}
}
private static final String classPathEnv = "CLASSPATH";
private static final String endPath = "end-path";

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

checkErrorLog(output);

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

// 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);
String cp = envMap.get(classPathEnv);
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);
cp += pathSep + endPath;
envMap.put(classPathEnv, 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;
}
}
}
String classPathLine = output.firstMatch("CLASSPATH=.*");

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

// 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);
if (!classPathLine.endsWith(endPath)) {
throw new RuntimeException("CLASSPATH was truncated in the hs err log.");
}
}
}

0 comments on commit 35eccdc

Please sign in to comment.