view patches/hotspot/aarch32/8076475-pr3696.patch @ 2967:7e0780d57319

Bump shenandoah to aarch64-shenandoah-jdk8u272-b04. 2020-10-20 Andrew John Hughes <gnu_andrew@member.fsf.org> Bump shenandoah to aarch64-shenandoah-jdk8u272-b04. * NEWS: Updated. * hotspot.map.in: Bump shenandoah to aarch64-shenandoah-jdk8u272-b04. * patches/hotspot/aarch32/8076475-pr3696.patch: Take a copy of the Shenandoah patch, so it can be changed without breaking AArch32 builds. * patches/hotspot/shenandoah/8076475-pr3696.patch: Update to apply after the version of JDK-8076475 which was included upstream, but didn't include changes to the code introduced by JDK-8059847, which is not upstream, or the strdup_check_oom calls, as JDK-6424123 is not in 8u yet.
author Andrew John Hughes <gnu_andrew@member.fsf.org>
date Wed, 21 Oct 2020 19:16:00 +0100
parents patches/hotspot/shenandoah/8076475-pr3696.patch@14e56f140ab0
children 9c238b325a8f
line wrap: on
line source

# HG changeset patch
# User stuefe
# Date 1549941248 0
#      Tue Feb 12 03:14:08 2019 +0000
# Node ID 9bbf02572dc114e85829673a7cdaffa482115f42
# Parent  cfb34db6589e222d34e5b736b99ed4f69c261c4f
8076475, PR3696: Misuses of strncpy/strncat
Summary: Various small fixes around strncpy and strncat
Reviewed-by: dsamersoff, coleenp

diff -Nru openjdk.orig/hotspot/agent/src/os/bsd/libproc_impl.c openjdk/hotspot/agent/src/os/bsd/libproc_impl.c
--- openjdk.orig/hotspot/agent/src/os/bsd/libproc_impl.c	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/agent/src/os/bsd/libproc_impl.c	2019-04-16 16:05:31.441511929 +0100
@@ -215,7 +215,12 @@
     return NULL;
   }
 
-  strncpy(newlib->name, libname, sizeof(newlib->name));
+  if (strlen(libname) >= sizeof(newlib->name)) {
+    print_debug("libname %s too long\n", libname);
+    return NULL;
+  }
+  strcpy(newlib->name, libname);
+
   newlib->base = base;
 
   if (fd == -1) {
diff -Nru openjdk.orig/hotspot/agent/src/os/linux/libproc_impl.c openjdk/hotspot/agent/src/os/linux/libproc_impl.c
--- openjdk.orig/hotspot/agent/src/os/linux/libproc_impl.c	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/agent/src/os/linux/libproc_impl.c	2019-04-16 16:05:31.441511929 +0100
@@ -162,7 +162,12 @@
       return NULL;
    }
 
-   strncpy(newlib->name, libname, sizeof(newlib->name));
+   if (strlen(libname) >= sizeof(newlib->name)) {
+     print_debug("libname %s too long\n", libname);
+     return NULL;
+   }
+   strcpy(newlib->name, libname);
+
    newlib->base = base;
 
    if (fd == -1) {
diff -Nru openjdk.orig/hotspot/src/os/bsd/dtrace/libjvm_db.c openjdk/hotspot/src/os/bsd/dtrace/libjvm_db.c
--- openjdk.orig/hotspot/src/os/bsd/dtrace/libjvm_db.c	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/os/bsd/dtrace/libjvm_db.c	2019-04-16 16:05:31.441511929 +0100
@@ -543,13 +543,14 @@
   CHECK_FAIL(err);
 
   result[0] = '\0';
-  strncat(result, klassString, size);
-  size -= strlen(klassString);
-  strncat(result, ".", size);
-  size -= 1;
-  strncat(result, nameString, size);
-  size -= strlen(nameString);
-  strncat(result, signatureString, size);
+  if (snprintf(result, size,
+    "%s.%s%s",
+    klassString,
+    nameString,
+    signatureString) >= size) {
+    // truncation
+    goto fail;
+  }
 
   if (nameString != NULL) free(nameString);
   if (klassString != NULL) free(klassString);
@@ -1056,9 +1057,9 @@
       CHECK_FAIL(err);
   }
   if (deoptimized) {
-    strncat(result + 1, " [deoptimized frame]; ", size-1);
+    strncat(result, " [deoptimized frame]; ", size - strlen(result) - 1);
   } else {
-    strncat(result + 1, " [compiled] ", size-1);
+    strncat(result, " [compiled] ", size - strlen(result) - 1);
   }
   if (debug)
       fprintf(stderr, "name_for_nmethod: END: method name: %s, vf_cnt: %d\n\n",
diff -Nru openjdk.orig/hotspot/src/os/bsd/vm/decoder_machO.cpp openjdk/hotspot/src/os/bsd/vm/decoder_machO.cpp
--- openjdk.orig/hotspot/src/os/bsd/vm/decoder_machO.cpp	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/os/bsd/vm/decoder_machO.cpp	2019-04-16 16:05:31.441511929 +0100
@@ -97,6 +97,7 @@
   char * symname = mach_find_in_stringtable((char*) ((uintptr_t)mach_base + stroff), strsize, found_strx);
   if (symname) {
       strncpy(buf, symname, buflen);
+      buf[buflen - 1] = '\0';
       return true;
   }
   DEBUG_ONLY(tty->print_cr("no string or null string found."));
diff -Nru openjdk.orig/hotspot/src/os/solaris/dtrace/libjvm_db.c openjdk/hotspot/src/os/solaris/dtrace/libjvm_db.c
--- openjdk.orig/hotspot/src/os/solaris/dtrace/libjvm_db.c	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/os/solaris/dtrace/libjvm_db.c	2019-04-16 16:05:31.441511929 +0100
@@ -543,13 +543,14 @@
   CHECK_FAIL(err);
 
   result[0] = '\0';
-  strncat(result, klassString, size);
-  size -= strlen(klassString);
-  strncat(result, ".", size);
-  size -= 1;
-  strncat(result, nameString, size);
-  size -= strlen(nameString);
-  strncat(result, signatureString, size);
+  if (snprintf(result, size,
+    "%s.%s%s",
+    klassString,
+    nameString,
+    signatureString) >= size) {
+    // truncation
+    goto fail;
+  }
 
   if (nameString != NULL) free(nameString);
   if (klassString != NULL) free(klassString);
@@ -1056,9 +1057,9 @@
       CHECK_FAIL(err);
   }
   if (deoptimized) {
-    strncat(result + 1, " [deoptimized frame]; ", size-1);
+    strncat(result, " [deoptimized frame]; ", size - strlen(result) - 1);
   } else {
-    strncat(result + 1, " [compiled] ", size-1);
+    strncat(result, " [compiled] ", size - strlen(result) - 1);
   }
   if (debug)
       fprintf(stderr, "name_for_nmethod: END: method name: %s, vf_cnt: %d\n\n",
diff -Nru openjdk.orig/hotspot/src/share/tools/hsdis/hsdis.c openjdk/hotspot/src/share/tools/hsdis/hsdis.c
--- openjdk.orig/hotspot/src/share/tools/hsdis/hsdis.c	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/share/tools/hsdis/hsdis.c	2019-04-16 16:05:31.445511866 +0100
@@ -438,6 +438,7 @@
     }
     p = q;
   }
+  *iop = '\0';
 }
 
 static void print_help(struct hsdis_app_data* app_data,
diff -Nru openjdk.orig/hotspot/src/share/vm/compiler/compileBroker.hpp openjdk/hotspot/src/share/vm/compiler/compileBroker.hpp
--- openjdk.orig/hotspot/src/share/vm/compiler/compileBroker.hpp	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/share/vm/compiler/compileBroker.hpp	2019-04-16 16:05:31.445511866 +0100
@@ -173,7 +173,8 @@
     // these methods should be called in a thread safe context
 
     void set_current_method(const char* method) {
-      strncpy(_current_method, method, (size_t)cmname_buffer_length);
+      strncpy(_current_method, method, (size_t)cmname_buffer_length-1);
+      _current_method[cmname_buffer_length-1] = '\0';
       if (UsePerfData) _perf_current_method->set_value(method);
     }
 
diff -Nru openjdk.orig/hotspot/src/share/vm/compiler/compilerOracle.cpp openjdk/hotspot/src/share/vm/compiler/compilerOracle.cpp
--- openjdk.orig/hotspot/src/share/vm/compiler/compilerOracle.cpp	2019-04-16 16:03:42.787215846 +0100
+++ openjdk/hotspot/src/share/vm/compiler/compilerOracle.cpp	2019-04-16 16:05:31.445511866 +0100
@@ -647,9 +647,7 @@
       // so read integer and fraction part of double value separately.
       if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
         char value[512] = "";
-        strncat(value, buffer[0], 255);
-        strcat(value, ".");
-        strncat(value, buffer[1], 255);
+        jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);
         total_bytes_read += bytes_read;
         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, atof(value));
       } else {
diff -Nru openjdk.orig/hotspot/src/share/vm/compiler/disassembler.cpp openjdk/hotspot/src/share/vm/compiler/disassembler.cpp
--- openjdk.orig/hotspot/src/share/vm/compiler/disassembler.cpp	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/share/vm/compiler/disassembler.cpp	2019-04-16 16:05:31.445511866 +0100
@@ -298,6 +298,7 @@
         strlen((const char*)arg) > sizeof(buffer) - 1) {
       // Only print this when the mach changes
       strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
+      buffer[sizeof(buffer) - 1] = '\0';
       output()->print_cr("[Disassembling for mach='%s']", arg);
     }
   } else if (match(event, "format bytes-per-line")) {
diff -Nru openjdk.orig/hotspot/src/share/vm/runtime/arguments.cpp openjdk/hotspot/src/share/vm/runtime/arguments.cpp
--- openjdk.orig/hotspot/src/share/vm/runtime/arguments.cpp	2019-04-16 16:03:42.763216221 +0100
+++ openjdk/hotspot/src/share/vm/runtime/arguments.cpp	2019-04-16 16:05:31.445511866 +0100
@@ -3629,8 +3629,7 @@
       src ++;
     }
 
-    char* copy = AllocateHeap(strlen(src) + 1, mtInternal);
-    strncpy(copy, src, strlen(src) + 1);
+    char* copy = os::strdup_check_oom(src, mtInternal);
 
     // trim all trailing empty paths
     for (char* tail = copy + strlen(copy) - 1; tail >= copy && *tail == separator; tail--) {
@@ -4013,18 +4012,14 @@
     if (end != NULL) *end = '\0';
     size_t jvm_path_len = strlen(jvm_path);
     size_t file_sep_len = strlen(os::file_separator());
-    shared_archive_path = NEW_C_HEAP_ARRAY(char, jvm_path_len +
-        file_sep_len + 20, mtInternal);
+    const size_t len = jvm_path_len + file_sep_len + 20;
+    shared_archive_path = NEW_C_HEAP_ARRAY(char, len, mtInternal);
     if (shared_archive_path != NULL) {
-      strncpy(shared_archive_path, jvm_path, jvm_path_len + 1);
-      strncat(shared_archive_path, os::file_separator(), file_sep_len);
-      strncat(shared_archive_path, "classes.jsa", 11);
+      jio_snprintf(shared_archive_path, len, "%s%sclasses.jsa",
+        jvm_path, os::file_separator());
     }
   } else {
-    shared_archive_path = NEW_C_HEAP_ARRAY(char, strlen(SharedArchiveFile) + 1, mtInternal);
-    if (shared_archive_path != NULL) {
-      strncpy(shared_archive_path, SharedArchiveFile, strlen(SharedArchiveFile) + 1);
-    }
+    shared_archive_path = os::strdup_check_oom(SharedArchiveFile, mtInternal);
   }
   return shared_archive_path;
 }
diff -Nru openjdk.orig/hotspot/src/share/vm/utilities/ostream.cpp openjdk/hotspot/src/share/vm/utilities/ostream.cpp
--- openjdk.orig/hotspot/src/share/vm/utilities/ostream.cpp	2019-04-05 19:21:24.000000000 +0100
+++ openjdk/hotspot/src/share/vm/utilities/ostream.cpp	2019-04-16 16:06:46.896328647 +0100
@@ -342,15 +342,19 @@
       assert(rm == NULL || Thread::current()->current_resource_mark() == rm,
              "stringStream is re-allocated with a different ResourceMark");
       buffer = NEW_RESOURCE_ARRAY(char, end);
-      strncpy(buffer, oldbuf, buffer_pos);
+      if (buffer_pos > 0) {
+        memcpy(buffer, oldbuf, buffer_pos);
+      }
       buffer_length = end;
     }
   }
   // invariant: buffer is always null-terminated
   guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
-  buffer[buffer_pos + write_len] = 0;
-  strncpy(buffer + buffer_pos, s, write_len);
-  buffer_pos += write_len;
+  if (write_len > 0) {
+    buffer[buffer_pos + write_len] = 0;
+    memcpy(buffer + buffer_pos, s, write_len);
+    buffer_pos += write_len;
+  }
 
   // Note that the following does not depend on write_len.
   // This means that position and count get updated
diff -Nru openjdk.orig/hotspot/src/share/vm/utilities/vmError.cpp openjdk/hotspot/src/share/vm/utilities/vmError.cpp
--- openjdk.orig/hotspot/src/share/vm/utilities/vmError.cpp	2019-04-16 16:03:40.295254924 +0100
+++ openjdk/hotspot/src/share/vm/utilities/vmError.cpp	2019-04-16 16:05:31.445511866 +0100
@@ -451,14 +451,7 @@
 #else
          const char *file = _filename;
 #endif
-         size_t len = strlen(file);
-         size_t buflen = sizeof(buf);
-
-         strncpy(buf, file, buflen);
-         if (len + 10 < buflen) {
-           sprintf(buf + len, ":%d", _lineno);
-         }
-         st->print(" (%s)", buf);
+         st->print(" (%s:%d)", file, _lineno);
        } else {
          st->print(" (0x%x)", _id);
        }