Blame gstackbounds.patch

Jiri Vanek 4749afe
diff --git a/src/os/linux/vm/os_linux.cpp b/src/os/linux/vm/os_linux.cpp
Jiri Vanek 4749afe
--- openjdk/hotspot/src/os/linux/vm/os_linux.cpp
Jiri Vanek 4749afe
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp
Jiri Vanek 4749afe
@@ -2763,39 +2763,47 @@
Jiri Vanek 4749afe
 // writing thread stacks don't use growable mappings (i.e. those
Jiri Vanek 4749afe
 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
Jiri Vanek 4749afe
 // only applies to the main thread.
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-static
Jiri Vanek 4749afe
-bool get_stack_bounds(uintptr_t *bottom, uintptr_t *top) {
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  char buf[128];
Jiri Vanek 4749afe
-  int fd, sz;
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  if ((fd = ::open("/proc/self/maps", O_RDONLY)) < 0) {
Jiri Vanek 4749afe
+static bool
Jiri Vanek 4749afe
+get_stack_bounds(uintptr_t *bottom, uintptr_t *top)
Jiri Vanek 4749afe
+{
Jiri Vanek 4749afe
+  FILE *f = fopen("/proc/self/maps", "r");
Jiri Vanek 4749afe
+  if (f == NULL)
Jiri Vanek 4749afe
     return false;
Jiri Vanek 4749afe
-  }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  const char kw[] = "[stack]";
Jiri Vanek 4749afe
-  const int kwlen = sizeof(kw)-1;
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  // Address part of /proc/self/maps couldn't be more than 128 bytes
Jiri Vanek 4749afe
-  while ((sz = os::get_line_chars(fd, buf, sizeof(buf))) > 0) {
Jiri Vanek 4749afe
-     if (sz > kwlen && ::memcmp(buf+sz-kwlen, kw, kwlen) == 0) {
Jiri Vanek 4749afe
-        // Extract addresses
Jiri Vanek 4749afe
-        if (sscanf(buf, "%" SCNxPTR "-%" SCNxPTR, bottom, top) == 2) {
Jiri Vanek 4749afe
-           uintptr_t sp = (uintptr_t) __builtin_frame_address(0);
Jiri Vanek 4749afe
-           if (sp >= *bottom && sp <= *top) {
Jiri Vanek 4749afe
-              ::close(fd);
Jiri Vanek 4749afe
-              return true;
Jiri Vanek 4749afe
-           }
Jiri Vanek 4749afe
+
Jiri Vanek 4749afe
+  while (!feof(f)) {
Jiri Vanek 4749afe
+    size_t dummy;
Jiri Vanek 4749afe
+    char *str = NULL;
Jiri Vanek 4749afe
+    ssize_t len = getline(&str, &dummy, f);
Jiri Vanek 4749afe
+    if (len == -1) {
Jiri Vanek 4749afe
+      fclose(f);
Jiri Vanek 4749afe
+      if (str != NULL)
Jiri Vanek 4749afe
+	free(str);
Jiri Vanek 4749afe
+      return false;
Jiri Vanek 4749afe
+    }
Jiri Vanek 4749afe
+
Jiri Vanek 4749afe
+    if (len > 0 && str[len-1] == '\n') {
Jiri Vanek 4749afe
+      str[len-1] = 0;
Jiri Vanek 4749afe
+      len--;
Jiri Vanek 4749afe
+    }
Jiri Vanek 4749afe
+
Jiri Vanek 4749afe
+    static const char *stack_str = "[stack]";
Jiri Vanek 4749afe
+    if (len > (ssize_t)strlen(stack_str)
Jiri Vanek 4749afe
+       && (strcmp(str + len - strlen(stack_str), stack_str) == 0)) {
Jiri Vanek 4749afe
+      if (sscanf(str, "%" SCNxPTR "-%" SCNxPTR, bottom, top) == 2) {
Jiri Vanek 4749afe
+        uintptr_t sp = (uintptr_t)__builtin_frame_address(0);
Jiri Vanek 4749afe
+        if (sp >= *bottom && sp <= *top) {
Jiri Vanek 4749afe
+          free(str);
Jiri Vanek 4749afe
+          fclose(f);
Jiri Vanek 4749afe
+          return true;
Jiri Vanek 4749afe
         }
Jiri Vanek 4749afe
-     }
Jiri Vanek 4749afe
-  }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
- ::close(fd);
Jiri Vanek 4749afe
+      }
Jiri Vanek 4749afe
+    }
Jiri Vanek 4749afe
+    free(str);
Jiri Vanek 4749afe
+  }
Jiri Vanek 4749afe
+  fclose(f);
Jiri Vanek 4749afe
   return false;
Jiri Vanek 4749afe
 }
Jiri Vanek 4749afe
 
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
 // If the (growable) stack mapping already extends beyond the point
Jiri Vanek 4749afe
 // where we're going to put our guard pages, truncate the mapping at
Jiri Vanek 4749afe
 // that point by munmap()ping it.  This ensures that when we later
Jiri Vanek 4749afe
diff --git a/src/share/vm/runtime/os.cpp b/src/share/vm/runtime/os.cpp
Jiri Vanek 4749afe
--- openjdk/hotspot/src/share/vm/runtime/os.cpp
Jiri Vanek 4749afe
+++ openjdk/hotspot/src/share/vm/runtime/os.cpp
Jiri Vanek 4749afe
@@ -1331,41 +1331,3 @@
Jiri Vanek 4749afe
   }
Jiri Vanek 4749afe
   return result;
Jiri Vanek 4749afe
 }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-// Read file line by line, if line is longer than bsize,
Jiri Vanek 4749afe
-// skip rest of line.
Jiri Vanek 4749afe
-int os::get_line_chars(int fd, char* buf, const size_t bsize){
Jiri Vanek 4749afe
-  size_t sz, i = 0;
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  // read until EOF, EOL or buf is full
Jiri Vanek 4749afe
-  while ((sz = (int) read(fd, &buf[i], 1)) == 1 && i < (bsize-2) && buf[i] != '\n') {
Jiri Vanek 4749afe
-     ++i;
Jiri Vanek 4749afe
-  }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  if (buf[i] == '\n') {
Jiri Vanek 4749afe
-    // EOL reached so ignore EOL character and return
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-    buf[i] = 0;
Jiri Vanek 4749afe
-    return (int) i;
Jiri Vanek 4749afe
-  }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  buf[i+1] = 0;
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  if (sz != 1) {
Jiri Vanek 4749afe
-    // EOF reached. if we read chars before EOF return them and
Jiri Vanek 4749afe
-    // return EOF on next call otherwise return EOF
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-    return (i == 0) ? -1 : (int) i;
Jiri Vanek 4749afe
-  }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  // line is longer than size of buf, skip to EOL
Jiri Vanek 4749afe
-  char ch;
Jiri Vanek 4749afe
-  while (read(fd, &ch, 1) == 1 && ch != '\n') {
Jiri Vanek 4749afe
-    // Do nothing
Jiri Vanek 4749afe
-  }
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  // return initial part of line that fits in buf.
Jiri Vanek 4749afe
-  // If we reached EOF, it will be returned on next call.
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
-  return (int) i;
Jiri Vanek 4749afe
-}
Jiri Vanek 4749afe
diff --git a/src/share/vm/runtime/os.hpp b/src/share/vm/runtime/os.hpp
Jiri Vanek 4749afe
--- openjdk/hotspot/src/share/vm/runtime/os.hpp
Jiri Vanek 4749afe
+++ openjdk/hotspot/src/share/vm/runtime/os.hpp
Jiri Vanek 4749afe
@@ -672,10 +672,6 @@
Jiri Vanek 4749afe
   // Hook for os specific jvm options that we don't want to abort on seeing
Jiri Vanek 4749afe
   static bool obsolete_option(const JavaVMOption *option);
Jiri Vanek 4749afe
 
Jiri Vanek 4749afe
-  // Read file line by line. If line is longer than bsize,
Jiri Vanek 4749afe
-  // rest of line is skipped. Returns number of bytes read or -1 on EOF
Jiri Vanek 4749afe
-  static int get_line_chars(int fd, char *buf, const size_t bsize);
Jiri Vanek 4749afe
-
Jiri Vanek 4749afe
   // Extensions
Jiri Vanek 4749afe
 #include "runtime/os_ext.hpp"
Jiri Vanek 4749afe
 
Jiri Vanek 4749afe