--- old/make/test/JtregNativeHotspot.gmk 2017-09-29 13:16:22.063506402 +0900 +++ new/make/test/JtregNativeHotspot.gmk 2017-09-29 13:16:21.899502904 +0900 @@ -74,6 +74,7 @@ $(TOPDIR)/test/hotspot/jtreg/serviceability/jvmti/ModuleAwareAgents/ClassLoadPrepare \ $(TOPDIR)/test/hotspot/jtreg/serviceability/jvmti/ModuleAwareAgents/ThreadStart \ $(TOPDIR)/test/hotspot/jtreg/serviceability/jvmti/StartPhase/AllowedFunctions \ + $(TOPDIR)/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed \ # # Add conditional directories here when needed. @@ -103,6 +104,9 @@ BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libMAAClassLoadPrepare := -lc BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libMAAThreadStart := -lc BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libAllowedFunctions := -lc + BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libException := -lc + BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libHasNoEntryPoint := -lc + BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libReturnError := -lc endif ifeq ($(OPENJDK_TARGET_OS), linux) --- old/src/hotspot/share/prims/jvmtiExport.cpp 2017-09-29 13:16:22.435514335 +0900 +++ new/src/hotspot/share/prims/jvmtiExport.cpp 2017-09-29 13:16:22.278510987 +0900 @@ -2475,7 +2475,7 @@ jint JvmtiExport::load_agent_library(const char *agent, const char *absParam, const char *options, outputStream* st) { - char ebuf[1024]; + char ebuf[1024] = {0}; char buffer[JVM_MAXPATHLEN]; void* library = NULL; jint result = JNI_ERR; @@ -2525,6 +2525,8 @@ if (!agent_lib->is_static_lib()) { os::dll_unload(library); } + st->print_cr("%s is not available in %s", + on_attach_symbols[0], agent_lib->name()); delete agent_lib; } else { // Invoke the Agent_OnAttach function @@ -2539,6 +2541,8 @@ // Agent_OnAttach may have used JNI if (HAS_PENDING_EXCEPTION) { + java_lang_Throwable::print(PENDING_EXCEPTION, st); + st->cr(); CLEAR_PENDING_EXCEPTION; } @@ -2550,11 +2554,16 @@ delete agent_lib; } - // Agent_OnAttach executed so completion status is JNI_OK - st->print_cr("%d", result); + st->print_cr("return code: %d", result); result = JNI_OK; } + } else { + st->print_cr("%s was not loaded.", agent); + if (*ebuf != '\0') { + st->print_cr("%s", ebuf); + } } + return result; } --- old/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java 2017-09-29 13:16:22.838522930 +0900 +++ new/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java 2017-09-29 13:16:22.678519518 +0900 @@ -39,6 +39,8 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; /* @@ -86,18 +88,26 @@ private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) throws AgentLoadException, AgentInitializationException, IOException { - InputStream in = execute("load", - agentLibrary, - isAbsolute ? "true" : "false", - options); - try { - int result = readInt(in); - if (result != 0) { - throw new AgentInitializationException("Agent_OnAttach failed", result); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader( + execute("load", agentLibrary, + Boolean.toString(isAbsolute), options)))) { + String result = reader.readLine(); + if (result == null) { + throw new AgentLoadException("Target VM did not respond"); + } else { + Matcher matcher = Pattern.compile("^return code: (\\d+)$") + .matcher(result); + if (matcher.matches()) { + int retCode = Integer.parseInt(matcher.group(1)); + if (retCode != 0) { + throw new AgentInitializationException( + "Agent_OnAttach failed", retCode); + } + } else { + throw new AgentLoadException(result); + } } - } finally { - in.close(); - } } --- old/test/jdk/com/sun/tools/attach/StartManagementAgent.java 2017-09-29 13:16:23.224531163 +0900 +++ new/test/jdk/com/sun/tools/attach/StartManagementAgent.java 2017-09-29 13:16:23.061527686 +0900 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2017, 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 @@ -99,7 +99,7 @@ } catch(AttachOperationFailedException ex) { // We expect parsing of "apa" above to fail, but if the file path // can't be read we get a different exception message - if (!ex.getMessage().contains("Invalid com.sun.management.jmxremote.port number")) { + if (!ex.getMessage().contains("For input string: \"apa\"")) { throw ex; } ex.printStackTrace(System.err); --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/AttachException.java 2017-09-29 13:16:23.440535769 +0900 @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017, 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. + */ +import java.nio.file.Paths; +import jdk.test.lib.dcmd.*; +import jdk.test.lib.Platform; +import jdk.test.lib.process.OutputAnalyzer; +import org.testng.annotations.Test; + +/* + * @test + * @bug 8165736 + * @library /test/lib + * @run testng AttachException + */ +public class AttachException { + + public void run(CommandExecutor executor) { + try{ + String libpath = Paths.get(System.getProperty("test.nativepath"), + "lib", + sharedObjectName("Exception")) + .toAbsolutePath() + .toString(); + OutputAnalyzer output = null; + + output = executor.execute("JVMTI.agent_load " + libpath); + output.shouldContain("Exception in Agent_OnAttach"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Build name of shared object according to platform rules + */ + public static String sharedObjectName(String name) { + if (Platform.isWindows()) { + return name + ".dll"; + } + if (Platform.isOSX()) { + return "lib" + name + ".dylib"; + } + return "lib" + name + ".so"; + } + + @Test + public void jmx() throws Throwable { + run(new JMXExecutor()); + } + + @Test + public void cli() throws Throwable { + run(new PidJcmdExecutor()); + } +} --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/AttachIncorrectLibrary.java 2017-09-29 13:16:23.831544108 +0900 @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017, 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. + */ +import jdk.test.lib.dcmd.*; +import jdk.test.lib.Platform; +import jdk.test.lib.process.OutputAnalyzer; +import org.testng.annotations.Test; + +/* + * @test + * @bug 8165736 + * @library /test/lib + * @run testng AttachIncorrectLibrary + */ +public class AttachIncorrectLibrary { + + public void run(CommandExecutor executor) { + try{ + OutputAnalyzer output = executor.execute("JVMTI.agent_load " + + sharedObjectName("SilverBullet")); + output.shouldContain(" was not loaded"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Build name of shared object according to platform rules + */ + public static String sharedObjectName(String name) { + if (Platform.isWindows()) { + return name + ".dll"; + } + if (Platform.isOSX()) { + return "lib" + name + ".dylib"; + } + return "lib" + name + ".so"; + } + + @Test + public void jmx() throws Throwable { + run(new JMXExecutor()); + } + + @Test + public void cli() throws Throwable { + run(new PidJcmdExecutor()); + } +} --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/AttachNoEntry.java 2017-09-29 13:16:24.235552725 +0900 @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017, 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. + */ +import java.nio.file.Paths; +import jdk.test.lib.dcmd.*; +import jdk.test.lib.Platform; +import jdk.test.lib.process.OutputAnalyzer; +import org.testng.annotations.Test; + +/* + * @test + * @bug 8165736 + * @library /test/lib + * @run testng AttachNoEntry + */ +public class AttachNoEntry { + + public void run(CommandExecutor executor) { + try{ + String libpath = Paths.get(System.getProperty("test.nativepath"), + "lib", + sharedObjectName("HasNoEntryPoint")) + .toAbsolutePath() + .toString(); + OutputAnalyzer output = null; + + output = executor.execute("JVMTI.agent_load " + libpath); + output.shouldContain("Agent_OnAttach is not available"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Build name of shared object according to platform rules + */ + public static String sharedObjectName(String name) { + if (Platform.isWindows()) { + return name + ".dll"; + } + if (Platform.isOSX()) { + return "lib" + name + ".dylib"; + } + return "lib" + name + ".so"; + } + + @Test + public void jmx() throws Throwable { + run(new JMXExecutor()); + } + + @Test + public void cli() throws Throwable { + run(new PidJcmdExecutor()); + } +} --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/AttachReturnError.java 2017-09-29 13:16:24.626561064 +0900 @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017, 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. + */ +import java.nio.file.Paths; +import jdk.test.lib.dcmd.*; +import jdk.test.lib.Platform; +import jdk.test.lib.process.OutputAnalyzer; +import org.testng.annotations.Test; + +/* + * @test + * @bug 8165736 + * @library /test/lib + * @run testng AttachReturnError + */ +public class AttachReturnError { + + public void run(CommandExecutor executor) { + try{ + String libpath = Paths.get(System.getProperty("test.nativepath"), + "lib", + sharedObjectName("ReturnError")) + .toAbsolutePath() + .toString(); + OutputAnalyzer output = null; + + output = executor.execute("JVMTI.agent_load " + libpath); + output.shouldContain("return code: -1"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Build name of shared object according to platform rules + */ + public static String sharedObjectName(String name) { + if (Platform.isWindows()) { + return name + ".dll"; + } + if (Platform.isOSX()) { + return "lib" + name + ".dylib"; + } + return "lib" + name + ".so"; + } + + @Test + public void jmx() throws Throwable { + run(new JMXExecutor()); + } + + @Test + public void cli() throws Throwable { + run(new PidJcmdExecutor()); + } +} --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/libException.c 2017-09-29 13:16:25.024569552 +0900 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2017, 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. + */ + +#include +#include + +JNIEXPORT +jint JNICALL Agent_OnAttach(JavaVM *vm, char *options, void *reserved) { + JNIEnv *env; + jclass runtimeExCls; + + (*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_9); + runtimeExCls = (*env)->FindClass(env, "java/lang/RuntimeException"); + (*env)->ThrowNew(env, runtimeExCls, "Exception in Agent_OnAttach"); + + return JNI_ERR; +} --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/libHasNoEntryPoint.c 2017-09-29 13:16:25.423578061 +0900 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2017, 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. + */ + +extern int dummy() { + return 0; +} --- /dev/null 2017-09-29 09:19:26.228777542 +0900 +++ new/test/hotspot/jtreg/serviceability/dcmd/jvmti/AttachFailed/libReturnError.c 2017-09-29 13:16:25.822586571 +0900 @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017, 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. + */ + +#include +#include + +JNIEXPORT +jint JNICALL Agent_OnAttach(JavaVM *vm, char *options, void *reserved) { + return JNI_ERR; +}