8067744: XMM/SSE float register values corrupted by JNI_CreateVM call in JRE 8 (Windows)
Summary: save/restore registers in generate_get_cpu_info() code which checks interrupts.
Reviewed-by: kvn, mcberg
Contributed-by: dmitry.chuyko@oracle.com
--- a/hotspot/make/test/JtregNative.gmk Fri Oct 14 14:47:01 2016 -0700
+++ b/hotspot/make/test/JtregNative.gmk Fri Oct 28 12:28:46 2016 -0700
@@ -47,11 +47,13 @@
$(HOTSPOT_TOPDIR)/test/runtime/jni/checked \
$(HOTSPOT_TOPDIR)/test/runtime/jni/PrivateInterfaceMethods \
$(HOTSPOT_TOPDIR)/test/runtime/jni/ToStringInInterfaceTest \
+ $(HOTSPOT_TOPDIR)/test/runtime/jni/CalleeSavedRegisters \
$(HOTSPOT_TOPDIR)/test/runtime/modules/getModuleJNI \
$(HOTSPOT_TOPDIR)/test/runtime/SameObject \
$(HOTSPOT_TOPDIR)/test/runtime/BoolReturn \
$(HOTSPOT_TOPDIR)/test/compiler/floatingpoint/ \
$(HOTSPOT_TOPDIR)/test/compiler/calls \
+ $(HOTSPOT_TOPDIR)/test/compiler/native \
$(HOTSPOT_TOPDIR)/test/serviceability/jvmti/GetNamedModule \
$(HOTSPOT_TOPDIR)/test/testlibrary/jvmti \
$(HOTSPOT_TOPDIR)/test/compiler/jvmci/jdk.vm.ci.code.test \
@@ -89,6 +91,11 @@
BUILD_HOTSPOT_JTREG_LIBRARIES_LDFLAGS_libtest-rwx := -z execstack
BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exeinvoke := -ljvm -lpthread
BUILD_TEST_invoke_exeinvoke.c_OPTIMIZATION := NONE
+ BUILD_HOTSPOT_JTREG_EXECUTABLES_LDFLAGS_exeFPRegs := -ldl
+endif
+
+ifeq ($(OPENJDK_TARGET_OS), windows)
+ BUILD_HOTSPOT_JTREG_EXECUTABLES_CFLAGS_exeFPRegs := -MT
endif
BUILD_HOTSPOT_JTREG_OUTPUT_DIR := $(BUILD_OUTPUT)/support/test/hotspot/jtreg/native
--- a/hotspot/src/cpu/x86/vm/vm_version_x86.cpp Fri Oct 14 14:47:01 2016 -0700
+++ b/hotspot/src/cpu/x86/vm/vm_version_x86.cpp Fri Oct 28 12:28:46 2016 -0700
@@ -362,6 +362,19 @@
VM_Version::set_evex_cpuFeatures(); // Enable temporary to pass asserts
UseAVX = 3;
UseSSE = 2;
+#ifdef _WINDOWS
+ // xmm5-xmm15 are not preserved by caller on windows
+ // https://msdn.microsoft.com/en-us/library/9z1stfyw.aspx
+ __ subptr(rsp, 64);
+ __ evmovdqul(Address(rsp, 0), xmm7, Assembler::AVX_512bit);
+#ifdef _LP64
+ __ subptr(rsp, 64);
+ __ evmovdqul(Address(rsp, 0), xmm8, Assembler::AVX_512bit);
+ __ subptr(rsp, 64);
+ __ evmovdqul(Address(rsp, 0), xmm31, Assembler::AVX_512bit);
+#endif // _LP64
+#endif // _WINDOWS
+
// load value into all 64 bytes of zmm7 register
__ movl(rcx, VM_Version::ymm_test_value());
__ movdl(xmm0, rcx);
@@ -381,6 +394,17 @@
VM_Version::set_avx_cpuFeatures(); // Enable temporary to pass asserts
UseAVX = 1;
UseSSE = 2;
+#ifdef _WINDOWS
+ __ subptr(rsp, 32);
+ __ vmovdqu(Address(rsp, 0), xmm7);
+#ifdef _LP64
+ __ subptr(rsp, 32);
+ __ vmovdqu(Address(rsp, 0), xmm8);
+ __ subptr(rsp, 32);
+ __ vmovdqu(Address(rsp, 0), xmm15);
+#endif // _LP64
+#endif // _WINDOWS
+
// load value into all 32 bytes of ymm7 register
__ movl(rcx, VM_Version::ymm_test_value());
@@ -428,6 +452,17 @@
__ evmovdqul(Address(rsi, 128), xmm8, Assembler::AVX_512bit);
__ evmovdqul(Address(rsi, 192), xmm31, Assembler::AVX_512bit);
#endif
+
+#ifdef _WINDOWS
+#ifdef _LP64
+ __ evmovdqul(xmm31, Address(rsp, 0), Assembler::AVX_512bit);
+ __ addptr(rsp, 64);
+ __ evmovdqul(xmm8, Address(rsp, 0), Assembler::AVX_512bit);
+ __ addptr(rsp, 64);
+#endif // _LP64
+ __ evmovdqul(xmm7, Address(rsp, 0), Assembler::AVX_512bit);
+ __ addptr(rsp, 64);
+#endif // _WINDOWS
VM_Version::clean_cpuFeatures();
UseAVX = saved_useavx;
UseSSE = saved_usesse;
@@ -445,6 +480,17 @@
__ vmovdqu(Address(rsi, 64), xmm8);
__ vmovdqu(Address(rsi, 96), xmm15);
#endif
+
+#ifdef _WINDOWS
+#ifdef _LP64
+ __ vmovdqu(xmm15, Address(rsp, 0));
+ __ addptr(rsp, 32);
+ __ vmovdqu(xmm8, Address(rsp, 0));
+ __ addptr(rsp, 32);
+#endif // _LP64
+ __ vmovdqu(xmm7, Address(rsp, 0));
+ __ addptr(rsp, 32);
+#endif // _WINDOWS
VM_Version::clean_cpuFeatures();
UseAVX = saved_useavx;
UseSSE = saved_usesse;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/jni/CalleeSavedRegisters/FPRegs.java Fri Oct 28 12:28:46 2016 -0700
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2016, 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 8067744
+ * @library /test/lib
+ * @modules java.base/jdk.internal.misc
+ * @run main/native FPRegs
+ */
+
+import jdk.test.lib.Platform;
+import jdk.test.lib.Utils;
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.process.OutputAnalyzer;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Optional;
+
+public class FPRegs {
+ public static void main(String[] args) throws IOException {
+ Path launcher = Paths.get(System.getProperty("test.nativepath"), "FPRegs" + (Platform.isWindows() ? ".exe" : ""));
+ System.out.println("Launcher = " + launcher + (Files.exists(launcher) ? " (exists)" : " (not exists)"));
+ Path jvmLib = findJVM();
+ ProcessBuilder pb = new ProcessBuilder(launcher.toString(), jvmLib.toString());
+ // bin as working directory to let Windows load dll
+ pb.directory(jvmLib.getParent().getParent().toFile());
+ OutputAnalyzer outputf = new OutputAnalyzer(pb.start());
+ outputf.shouldHaveExitValue(0);
+ }
+
+ static Path findJVM() throws IOException {
+ String root = Utils.TEST_JDK;
+ String lib = System.mapLibraryName("jvm");
+ System.out.println("Root = " + root);
+ System.out.println("Library = " + lib);
+
+ Optional<Path> jvmLib = Files.find(new File(root).toPath(), 4, (p, attr) -> p.toFile().getName().equals(lib)).findFirst();
+ Path p = null;
+ if (jvmLib.isPresent()) {
+ p = jvmLib.get().toRealPath();
+ System.out.println("JVM = " + p);
+ } else {
+ System.out.println("TESTBUG: JVM not found in ");
+ Files.walk(new File(root).toPath(), 4).map(Path::toString).forEach(System.out::println);
+ }
+ return p;
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/jni/CalleeSavedRegisters/exeFPRegs.c Fri Oct 28 12:28:46 2016 -0700
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2016, 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 <jni.h>
+#include <stdlib.h>
+
+#ifdef WINDOWS
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif // WINDOWS
+
+#ifdef WINDOWS
+ HMODULE handle;
+#else
+ void* handle;
+#endif // WINDOWS
+
+jint(JNICALL *jni_create_java_vm)(JavaVM **, JNIEnv **, void *) = NULL;
+
+// method to perform dlclose on an open dynamic library handle
+void closeHandle() {
+#ifdef WINDOWS
+ if (!FreeLibrary(handle)) {
+ fprintf(stderr, "Error occurred while closing handle: 0x%02X\n", GetLastError());
+ }
+#else
+ if (dlclose(handle) != 0) {
+ fprintf(stderr, "Error occurred while closing handle: %s\n", dlerror());
+ }
+#endif // WINDOWS
+}
+
+void fail(int code) {
+ if (handle) {
+ closeHandle();
+ }
+ exit(code);
+}
+
+
+// method to load the dynamic library libjvm
+int loadJVM(const char* path) {
+#ifdef WINDOWS
+ UINT errorMode = GetErrorMode();
+ SetErrorMode(errorMode | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
+ handle = LoadLibraryA(path);
+#else
+ handle = dlopen(path, RTLD_LAZY);
+#endif // WINDOWS
+
+ if (handle) {
+ // find the address of function
+#ifdef WINDOWS
+ *(void **) (&jni_create_java_vm) = GetProcAddress(handle, "JNI_CreateJavaVM");
+#else
+ *(void **) (&jni_create_java_vm) = dlsym(handle, "JNI_CreateJavaVM");
+#endif // WINDOWS
+
+ if (jni_create_java_vm == NULL) {
+ fprintf(stderr, "ERROR: No JNI_CreateJavaVM found: '%s'\n", path);
+ return -1;
+ }
+ } else {
+#ifdef WINDOWS
+ fprintf(stderr, "ERROR: Can't load JVM library: 0x%02X\n", GetLastError());
+#else
+ fprintf(stderr, "ERROR: Can't load JVM library: %s\n", dlerror());
+#endif // WINDOWS
+ return -1;
+ }
+ return 0;
+}
+
+long long unsigned int d2l(double d) {
+ union {
+ double d;
+ long long unsigned int llu;
+ } dl;
+
+ dl.d = d;
+ return dl.llu;
+}
+
+#define print_reg(r) printf("%s = %f (0x%llX)\n", #r, r, d2l(r));
+
+int main(int argc, const char** argv) {
+ JavaVM* jvm;
+ JNIEnv* env;
+ JavaVMInitArgs vm_args;
+
+ // values to trick constant folding
+ long long unsigned int vd[32];
+ int i;
+ int bad_cnt = 0;
+
+ // values occupy fp registers
+ // note: suitable code shape is produced only on Windows,
+ // and even then registers are corrupted not on every machine
+ register double d00;
+ register double d01;
+ register double d02;
+ register double d03;
+ register double d04;
+ register double d05;
+ register double d06;
+ register double d07;
+ register double d08;
+ register double d09;
+ register double d10;
+ register double d11;
+ register double d12;
+ register double d13;
+ register double d14;
+ register double d15;
+
+ if (argc != 2) {
+ printf("Usage: FPRegs <jvm_path>");
+ fail(2);
+ }
+ printf("jvm_path = %s\n", argv[1]);
+
+ if (loadJVM(argv[1]) < 0) {
+ fail(3);
+ }
+
+ vm_args.version = JNI_VERSION_1_8;
+ vm_args.ignoreUnrecognized = JNI_FALSE;
+ vm_args.options = NULL;
+ vm_args.nOptions = 0;
+
+ for(i = 0; i < 16; i++) {
+ vd[i] = d2l(100 + i);
+ }
+
+ d00 = 100.0;
+ d01 = 101.0;
+ d02 = 102.0;
+ d03 = 103.0;
+ d04 = 104.0;
+ d05 = 105.0;
+ d06 = 106.0;
+ d07 = 107.0;
+ d08 = 108.0;
+ d09 = 109.0;
+ d10 = 110.0;
+ d11 = 111.0;
+ d12 = 112.0;
+ d13 = 113.0;
+ d14 = 114.0;
+ d15 = 115.0;
+
+ printf("BEFORE:\n");
+ print_reg(d00);
+ print_reg(d01);
+ print_reg(d02);
+ print_reg(d03);
+ print_reg(d04);
+ print_reg(d05);
+ print_reg(d06);
+ print_reg(d07);
+ print_reg(d08);
+ print_reg(d09);
+ print_reg(d10);
+ print_reg(d11);
+ print_reg(d12);
+ print_reg(d13);
+ print_reg(d14);
+ print_reg(d15);
+
+ if (jni_create_java_vm(&jvm, &env, &vm_args) < 0 ) {
+ fprintf(stderr, "ERROR: Can't create JavaVM\n");
+ fail(4);
+ }
+
+ if (d2l(d00) != vd[0]) bad_cnt++;
+ if (d2l(d01) != vd[1]) bad_cnt++;
+ if (d2l(d02) != vd[2]) bad_cnt++;
+ if (d2l(d03) != vd[3]) bad_cnt++;
+ if (d2l(d04) != vd[4]) bad_cnt++;
+ if (d2l(d05) != vd[5]) bad_cnt++;
+ if (d2l(d06) != vd[6]) bad_cnt++;
+ if (d2l(d07) != vd[7]) bad_cnt++;
+ if (d2l(d08) != vd[8]) bad_cnt++;
+ if (d2l(d09) != vd[9]) bad_cnt++;
+ if (d2l(d10) != vd[10]) bad_cnt++;
+ if (d2l(d11) != vd[11]) bad_cnt++;
+ if (d2l(d12) != vd[12]) bad_cnt++;
+ if (d2l(d13) != vd[13]) bad_cnt++;
+ if (d2l(d14) != vd[14]) bad_cnt++;
+ if (d2l(d15) != vd[15]) bad_cnt++;
+
+ printf("AFTER:\n");
+ print_reg(d00);
+ print_reg(d01);
+ print_reg(d02);
+ print_reg(d03);
+ print_reg(d04);
+ print_reg(d05);
+ print_reg(d06);
+ print_reg(d07);
+ print_reg(d08);
+ print_reg(d09);
+ print_reg(d10);
+ print_reg(d11);
+ print_reg(d12);
+ print_reg(d13);
+ print_reg(d14);
+ print_reg(d15);
+
+ printf("%d registers corrupted\n", bad_cnt);
+ if (bad_cnt > 0) {
+ printf("TEST FAILED");
+ fail(1);
+ }
+
+ printf("TEST PASSED");
+ closeHandle();
+ return 0;
+}
+