Blame SOURCES/jdk8197429-pr3546-rh1536622-increased_stack_guard_causes_segfaults_on_x86_32.patch

4ca1da
# HG changeset patch
4ca1da
# User aph
4ca1da
# Date 1530894306 -3600
4ca1da
#      Fri Jul 06 17:25:06 2018 +0100
4ca1da
# Node ID 1485461a0fd1ff977a6acb8f2ed1069aaaf3b07e
4ca1da
# Parent  d7bcbcfde5057ad066ad2fb55a87d19a5827ddee
4ca1da
8197429: Increased stack guard causes segfaults on x86-32
4ca1da
Reviewed-by: dholmes
4ca1da
4ca1da
diff --git openjdk.orig/hotspot/src/os/linux/vm/os_linux.cpp openjdk/hotspot/src/os/linux/vm/os_linux.cpp
4ca1da
--- openjdk.orig/hotspot/src/os/linux/vm/os_linux.cpp
4ca1da
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp
4ca1da
@@ -724,6 +724,10 @@
4ca1da
   }
4ca1da
 }
4ca1da
 
4ca1da
+void os::Linux::expand_stack_to(address bottom) {
4ca1da
+  _expand_stack_to(bottom);
4ca1da
+}
4ca1da
+
4ca1da
 bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
4ca1da
   assert(t!=NULL, "just checking");
4ca1da
   assert(t->osthread()->expanding_stack(), "expand should be set");
4ca1da
diff --git openjdk.orig/hotspot/src/os/linux/vm/os_linux.hpp openjdk/hotspot/src/os/linux/vm/os_linux.hpp
4ca1da
--- openjdk.orig/hotspot/src/os/linux/vm/os_linux.hpp
4ca1da
+++ openjdk/hotspot/src/os/linux/vm/os_linux.hpp
4ca1da
@@ -249,6 +249,8 @@
4ca1da
   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
4ca1da
 
4ca1da
 private:
4ca1da
+  static void expand_stack_to(address bottom);
4ca1da
+
4ca1da
   typedef int (*sched_getcpu_func_t)(void);
4ca1da
   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
4ca1da
   typedef int (*numa_max_node_func_t)(void);
4ca1da
diff --git openjdk.orig/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp openjdk/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
4ca1da
--- openjdk.orig/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
4ca1da
+++ openjdk/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
4ca1da
@@ -892,6 +892,27 @@
4ca1da
 void os::workaround_expand_exec_shield_cs_limit() {
4ca1da
 #if defined(IA32)
4ca1da
   size_t page_size = os::vm_page_size();
4ca1da
+
4ca1da
+  /*
4ca1da
+   * JDK-8197429
4ca1da
+   *
4ca1da
+   * Expand the stack mapping to the end of the initial stack before
4ca1da
+   * attempting to install the codebuf.  This is needed because newer
4ca1da
+   * Linux kernels impose a distance of a megabyte between stack
4ca1da
+   * memory and other memory regions.  If we try to install the
4ca1da
+   * codebuf before expanding the stack the installation will appear
4ca1da
+   * to succeed but we'll get a segfault later if we expand the stack
4ca1da
+   * in Java code.
4ca1da
+   *
4ca1da
+   */
4ca1da
+  if (os::is_primordial_thread()) {
4ca1da
+    address limit = Linux::initial_thread_stack_bottom();
4ca1da
+    if (! DisablePrimordialThreadGuardPages) {
4ca1da
+      limit += (StackYellowPages + StackRedPages) * page_size;
4ca1da
+    }
4ca1da
+    os::Linux::expand_stack_to(limit);
4ca1da
+  }
4ca1da
+
4ca1da
   /*
4ca1da
    * Take the highest VA the OS will give us and exec
4ca1da
    *
4ca1da
@@ -910,6 +931,16 @@
4ca1da
   char* hint = (char*) (Linux::initial_thread_stack_bottom() -
4ca1da
                         ((StackYellowPages + StackRedPages + 1) * page_size));
4ca1da
   char* codebuf = os::attempt_reserve_memory_at(page_size, hint);
4ca1da
+
4ca1da
+  if (codebuf == NULL) {
4ca1da
+    // JDK-8197429: There may be a stack gap of one megabyte between
4ca1da
+    // the limit of the stack and the nearest memory region: this is a
4ca1da
+    // Linux kernel workaround for CVE-2017-1000364.  If we failed to
4ca1da
+    // map our codebuf, try again at an address one megabyte lower.
4ca1da
+    hint -= 1 * M;
4ca1da
+    codebuf = os::attempt_reserve_memory_at(page_size, hint);
4ca1da
+  }
4ca1da
+
4ca1da
   if ( (codebuf == NULL) || (!os::commit_memory(codebuf, page_size, true)) ) {
4ca1da
     return; // No matter, we tried, best effort.
4ca1da
   }
4ca1da
diff --git openjdk.orig/hotspot/test/runtime/StackGap/T.java openjdk/hotspot/test/runtime/StackGap/T.java
4ca1da
new file mode 100644
4ca1da
--- /dev/null
4ca1da
+++ openjdk/hotspot/test/runtime/StackGap/T.java
4ca1da
@@ -0,0 +1,33 @@
4ca1da
+/*
4ca1da
+ * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
4ca1da
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ca1da
+ *
4ca1da
+ * This code is free software; you can redistribute it and/or modify it
4ca1da
+ * under the terms of the GNU General Public License version 2 only, as
4ca1da
+ * published by the Free Software Foundation.
4ca1da
+ *
4ca1da
+ * This code is distributed in the hope that it will be useful, but WITHOUT
4ca1da
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ca1da
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ca1da
+ * version 2 for more details (a copy is included in the LICENSE file that
4ca1da
+ * accompanied this code).
4ca1da
+ *
4ca1da
+ * You should have received a copy of the GNU General Public License version
4ca1da
+ * 2 along with this work; if not, write to the Free Software Foundation,
4ca1da
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ca1da
+ *
4ca1da
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ca1da
+ * or visit www.oracle.com if you need additional information or have any
4ca1da
+ * questions.
4ca1da
+ */
4ca1da
+
4ca1da
+public class T {
4ca1da
+
4ca1da
+  public static void test(int n) {
4ca1da
+    if (n == 0) return;
4ca1da
+    System.out.println (n);
4ca1da
+    test (n - 1);
4ca1da
+
4ca1da
+  }
4ca1da
+
4ca1da
+}
4ca1da
diff --git openjdk.orig/hotspot/test/runtime/StackGap/exestack-gap.c openjdk/hotspot/test/runtime/StackGap/exestack-gap.c
4ca1da
new file mode 100644
4ca1da
--- /dev/null
4ca1da
+++ openjdk/hotspot/test/runtime/StackGap/exestack-gap.c
4ca1da
@@ -0,0 +1,82 @@
4ca1da
+/*
4ca1da
+ * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
4ca1da
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ca1da
+ *
4ca1da
+ * This code is free software; you can redistribute it and/or modify it
4ca1da
+ * under the terms of the GNU General Public License version 2 only, as
4ca1da
+ * published by the Free Software Foundation.
4ca1da
+ *
4ca1da
+ * This code is distributed in the hope that it will be useful, but WITHOUT
4ca1da
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ca1da
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ca1da
+ * version 2 for more details (a copy is included in the LICENSE file that
4ca1da
+ * accompanied this code).
4ca1da
+ *
4ca1da
+ * You should have received a copy of the GNU General Public License version
4ca1da
+ * 2 along with this work; if not, write to the Free Software Foundation,
4ca1da
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ca1da
+ *
4ca1da
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ca1da
+ * or visit www.oracle.com if you need additional information or have any
4ca1da
+ * questions.
4ca1da
+ */
4ca1da
+
4ca1da
+#include <jni.h>
4ca1da
+#include <stdio.h>
4ca1da
+#include <stdlib.h>
4ca1da
+
4ca1da
+JNIEnv* create_vm(JavaVM **jvm, char *extra_option)
4ca1da
+{
4ca1da
+    JNIEnv* env;
4ca1da
+    JavaVMInitArgs args;
4ca1da
+    JavaVMOption options[4];
4ca1da
+    args.version = JNI_VERSION_1_8;
4ca1da
+    args.nOptions = 3 + (extra_option != NULL);
4ca1da
+    options[0].optionString = "-Xss2048k";
4ca1da
+    char classpath[4096];
4ca1da
+    snprintf(classpath, sizeof classpath,
4ca1da
+             "-Djava.class.path=%s", getenv("CLASSPATH"));
4ca1da
+    options[1].optionString = classpath;
4ca1da
+    options[2].optionString = "-XX:+UnlockExperimentalVMOptions";
4ca1da
+    if (extra_option) {
4ca1da
+      options[3].optionString = extra_option;
4ca1da
+    }
4ca1da
+    args.options = &options[0];
4ca1da
+    args.ignoreUnrecognized = 0;
4ca1da
+    int rv;
4ca1da
+    rv = JNI_CreateJavaVM(jvm, (void**)&env, &args);
4ca1da
+    if (rv < 0) return NULL;
4ca1da
+    return env;
4ca1da
+}
4ca1da
+
4ca1da
+void run(char *extra_arg) {
4ca1da
+  JavaVM *jvm;
4ca1da
+  jclass T_class;
4ca1da
+  jmethodID test_method;
4ca1da
+  JNIEnv *env = create_vm(&jvm, extra_arg);
4ca1da
+  if (env == NULL)
4ca1da
+    exit(1);
4ca1da
+  T_class = (*env)->FindClass(env, "T");
4ca1da
+  if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
4ca1da
+    (*env)->ExceptionDescribe(env);
4ca1da
+    exit(1);
4ca1da
+  }
4ca1da
+  test_method = (*env)->GetStaticMethodID(env, T_class, "test", "(I)V");
4ca1da
+  if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
4ca1da
+    (*env)->ExceptionDescribe(env);
4ca1da
+    exit(1);
4ca1da
+  }
4ca1da
+  (*env)->CallStaticVoidMethod(env, T_class, test_method, 1000);
4ca1da
+}
4ca1da
+
4ca1da
+
4ca1da
+int main(int argc, char **argv)
4ca1da
+{
4ca1da
+  if (argc > 1) {
4ca1da
+    run(argv[1]);
4ca1da
+  } else {
4ca1da
+    run(NULL);
4ca1da
+  }
4ca1da
+
4ca1da
+  return 0;
4ca1da
+}
4ca1da
diff --git openjdk.orig/hotspot/test/runtime/StackGap/testme.sh openjdk/hotspot/test/runtime/StackGap/testme.sh
4ca1da
new file mode 100644
4ca1da
--- /dev/null
4ca1da
+++ openjdk/hotspot/test/runtime/StackGap/testme.sh
4ca1da
@@ -0,0 +1,73 @@
4ca1da
+# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
4ca1da
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ca1da
+#
4ca1da
+# This code is free software; you can redistribute it and/or modify it
4ca1da
+# under the terms of the GNU General Public License version 2 only, as
4ca1da
+# published by the Free Software Foundation.
4ca1da
+#
4ca1da
+# This code is distributed in the hope that it will be useful, but WITHOUT
4ca1da
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ca1da
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ca1da
+# version 2 for more details (a copy is included in the LICENSE file that
4ca1da
+# accompanied this code).
4ca1da
+#
4ca1da
+# You should have received a copy of the GNU General Public License version
4ca1da
+# 2 along with this work; if not, write to the Free Software Foundation,
4ca1da
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ca1da
+#
4ca1da
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ca1da
+# or visit www.oracle.com if you need additional information or have any
4ca1da
+# questions.
4ca1da
+#!/bin/sh
4ca1da
+
4ca1da
+#
4ca1da
+# @test testme.sh
4ca1da
+# @bug 8197429
4ca1da
+# @summary Linux kernel stack guard should not cause segfaults on x86-32
4ca1da
+# @compile T.java
4ca1da
+# @run shell testme.sh
4ca1da
+#
4ca1da
+
4ca1da
+if [ "${TESTSRC}" = "" ]
4ca1da
+then
4ca1da
+  TESTSRC=${PWD}
4ca1da
+  echo "TESTSRC not set.  Using "${TESTSRC}" as default"
4ca1da
+fi
4ca1da
+echo "TESTSRC=${TESTSRC}"
4ca1da
+## Adding common setup Variables for running shell tests.
4ca1da
+. ${TESTSRC}/../../test_env.sh
4ca1da
+
4ca1da
+if [ "${VM_OS}" != "linux" ]
4ca1da
+then
4ca1da
+  echo "Test only valid for Linux"
4ca1da
+  exit 0
4ca1da
+fi
4ca1da
+
4ca1da
+gcc_cmd=`which gcc`
4ca1da
+if [ "x$gcc_cmd" = "x" ]; then
4ca1da
+    echo "WARNING: gcc not found. Cannot execute test." 2>&1
4ca1da
+    exit 0;
4ca1da
+fi
4ca1da
+
4ca1da
+CFLAGS="-m${VM_BITS}"
4ca1da
+
4ca1da
+LD_LIBRARY_PATH=.:${COMPILEJAVA}/jre/lib/${VM_CPU}/${VM_TYPE}:/usr/lib:$LD_LIBRARY_PATH
4ca1da
+export LD_LIBRARY_PATH
4ca1da
+
4ca1da
+cp ${TESTSRC}${FS}exestack-gap.c .
4ca1da
+
4ca1da
+# Copy the result of our @compile action:
4ca1da
+cp ${TESTCLASSES}${FS}T.class .
4ca1da
+
4ca1da
+echo "Compilation flag: ${COMP_FLAG}"
4ca1da
+# Note pthread may not be found thus invoke creation will fail to be created.
4ca1da
+# Check to ensure you have a /usr/lib/libpthread.so if you don't please look
4ca1da
+# for /usr/lib/`uname -m`-linux-gnu version ensure to add that path to below compilation.
4ca1da
+
4ca1da
+$gcc_cmd -DLINUX ${CFLAGS} -o stack-gap \
4ca1da
+    -I${COMPILEJAVA}/include -I${COMPILEJAVA}/include/linux \
4ca1da
+    -L${COMPILEJAVA}/jre/lib/${VM_CPU}/${VM_TYPE} \
4ca1da
+    -ljvm -lpthread exestack-gap.c
4ca1da
+
4ca1da
+./stack-gap || exit $?
4ca1da
+./stack-gap -XX:+DisablePrimordialThreadGuardPages || exit $?