8206977: Minor improvements of runtime code.
authorgoetz
Wed, 11 Jul 2018 16:11:10 +0200
changeset 51070 2f4c3cac8556
parent 51069 d5d5f6658b12
child 51071 040880bdd0d4
8206977: Minor improvements of runtime code. Reviewed-by: coleenp, lfoltan
src/hotspot/cpu/x86/vm_version_ext_x86.cpp
src/hotspot/cpu/x86/vm_version_ext_x86.hpp
src/hotspot/os/linux/os_linux.cpp
src/hotspot/os/linux/perfMemory_linux.cpp
src/hotspot/share/classfile/moduleEntry.cpp
src/hotspot/share/classfile/systemDictionary.cpp
src/hotspot/share/classfile/systemDictionaryShared.cpp
src/hotspot/share/classfile/verifier.cpp
src/hotspot/share/logging/logOutput.cpp
src/hotspot/share/memory/filemap.cpp
src/hotspot/share/memory/metaspace.cpp
src/hotspot/share/memory/virtualspace.cpp
src/hotspot/share/prims/jvmtiEnvBase.cpp
src/hotspot/share/runtime/flags/jvmFlag.cpp
src/hotspot/share/services/writeableFlags.cpp
src/hotspot/share/utilities/ostream.cpp
test/hotspot/gtest/logging/logTestUtils.inline.hpp
test/hotspot/gtest/memory/test_metachunk.cpp
--- a/src/hotspot/cpu/x86/vm_version_ext_x86.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/cpu/x86/vm_version_ext_x86.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -470,8 +470,8 @@
 }
 
 size_t VM_Version_Ext::cpu_write_support_string(char* const buf, size_t buf_len) {
-  assert(buf != NULL, "buffer is NULL!");
-  assert(buf_len > 0, "buffer len not enough!");
+  guarantee(buf != NULL, "buffer is NULL!");
+  guarantee(buf_len > 0, "buffer len not enough!");
 
   unsigned int flag = 0;
   unsigned int fi = 0;
@@ -481,8 +481,7 @@
 #define WRITE_TO_BUF(string)                                                          \
   {                                                                                   \
     int res = jio_snprintf(&buf[written], buf_len - written, "%s%s", prefix, string); \
-    if (res < 0 || (size_t) res >= buf_len - 1) {                                     \
-      buf[buf_len-1] = '\0';                                                          \
+    if (res < 0) {                                                                    \
       return buf_len - 1;                                                             \
     }                                                                                 \
     written += res;                                                                   \
@@ -592,7 +591,7 @@
     _cpuid_info.ext_cpuid1_edx);
 
   if (outputLen < 0 || (size_t) outputLen >= buf_len - 1) {
-    buf[buf_len-1] = '\0';
+    if (buf_len > 0) { buf[buf_len-1] = '\0'; }
     return OS_ERR;
   }
 
--- a/src/hotspot/cpu/x86/vm_version_ext_x86.hpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/cpu/x86/vm_version_ext_x86.hpp	Wed Jul 11 16:11:10 2018 +0200
@@ -63,6 +63,7 @@
   static bool cpu_is_em64t(void);
   static bool is_netburst(void);
 
+  // Returns bytes written excluding termninating null byte.
   static size_t cpu_write_support_string(char* const buf, size_t buf_len);
   static void resolve_cpu_information_details(void);
   static jlong max_qualified_cpu_freq_from_brand_string(void);
--- a/src/hotspot/os/linux/os_linux.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/os/linux/os_linux.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -2108,7 +2108,9 @@
   // special case for debian
   if (file_exists("/etc/debian_version")) {
     strncpy(buf, "Debian ", buflen);
-    parse_os_info(&buf[7], buflen-7, "/etc/debian_version");
+    if (buflen > 7) {
+      parse_os_info(&buf[7], buflen-7, "/etc/debian_version");
+    }
   } else {
     strncpy(buf, "Linux", buflen);
   }
@@ -2820,8 +2822,8 @@
 }
 
 int os::Linux::get_existing_num_nodes() {
-  size_t node;
-  size_t highest_node_number = Linux::numa_max_node();
+  int node;
+  int highest_node_number = Linux::numa_max_node();
   int num_nodes = 0;
 
   // Get the total number of nodes in the system including nodes without memory.
@@ -2834,14 +2836,14 @@
 }
 
 size_t os::numa_get_leaf_groups(int *ids, size_t size) {
-  size_t highest_node_number = Linux::numa_max_node();
+  int highest_node_number = Linux::numa_max_node();
   size_t i = 0;
 
-  // Map all node ids in which is possible to allocate memory. Also nodes are
+  // Map all node ids in which it is possible to allocate memory. Also nodes are
   // not always consecutively available, i.e. available from 0 to the highest
   // node number.
-  for (size_t node = 0; node <= highest_node_number; node++) {
-    if (Linux::isnode_in_configured_nodes(node)) {
+  for (int node = 0; node <= highest_node_number; node++) {
+    if (Linux::isnode_in_configured_nodes((unsigned int)node)) {
       ids[i++] = node;
     }
   }
--- a/src/hotspot/os/linux/perfMemory_linux.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/os/linux/perfMemory_linux.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -534,15 +534,14 @@
   // directory search
   char* oldest_user = NULL;
   time_t oldest_ctime = 0;
-  char buffer[TMP_BUFFER_LEN];
+  char buffer[MAXPATHLEN + 1];
   int searchpid;
   char* tmpdirname = (char *)os::get_temp_directory();
   assert(strlen(tmpdirname) == 4, "No longer using /tmp - update buffer size");
 
   if (nspid == -1) {
     searchpid = vmid;
-  }
-  else {
+  } else {
     jio_snprintf(buffer, MAXPATHLEN, "/proc/%d/root%s", vmid, tmpdirname);
     tmpdirname = buffer;
     searchpid = nspid;
--- a/src/hotspot/share/classfile/moduleEntry.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/classfile/moduleEntry.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -387,7 +387,8 @@
     entry->set_is_patched();
     if (log_is_enabled(Trace, module, patch)) {
       ResourceMark rm;
-      log_trace(module, patch)("Marked module %s as patched from --patch-module", name->as_C_string());
+      log_trace(module, patch)("Marked module %s as patched from --patch-module",
+                               name != NULL ? name->as_C_string() : UNNAMED_MODULE);
     }
   }
 
--- a/src/hotspot/share/classfile/systemDictionary.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/classfile/systemDictionary.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -1364,18 +1364,18 @@
 
     // notify a class loaded from shared object
     ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
-  }
-
-  ik->set_has_passed_fingerprint_check(false);
-  if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
-    uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
-    uint64_t cds_fp = ik->get_stored_fingerprint();
-    if (aot_fp != 0 && aot_fp == cds_fp) {
-      // This class matches with a class saved in an AOT library
-      ik->set_has_passed_fingerprint_check(true);
-    } else {
-      ResourceMark rm;
-      log_info(class, fingerprint)("%s :  expected = " PTR64_FORMAT " actual = " PTR64_FORMAT, ik->external_name(), aot_fp, cds_fp);
+
+    ik->set_has_passed_fingerprint_check(false);
+    if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
+      uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
+      uint64_t cds_fp = ik->get_stored_fingerprint();
+      if (aot_fp != 0 && aot_fp == cds_fp) {
+        // This class matches with a class saved in an AOT library
+        ik->set_has_passed_fingerprint_check(true);
+      } else {
+        ResourceMark rm;
+        log_info(class, fingerprint)("%s :  expected = " PTR64_FORMAT " actual = " PTR64_FORMAT, ik->external_name(), aot_fp, cds_fp);
+      }
     }
   }
   return ik;
--- a/src/hotspot/share/classfile/systemDictionaryShared.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/classfile/systemDictionaryShared.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -766,10 +766,11 @@
   SharedDictionaryEntry* entry = ((SharedDictionary*)(k->class_loader_data()->dictionary()))->find_entry_for(k);
   ResourceMark rm;
   // Lambda classes are not archived and will be regenerated at runtime.
-  if (entry == NULL && strstr(k->name()->as_C_string(), "Lambda$") != NULL) {
+  if (entry == NULL) {
+    guarantee(strstr(k->name()->as_C_string(), "Lambda$") != NULL,
+              "class should be in dictionary before being verified");
     return true;
   }
-  assert(entry != NULL, "class should be in dictionary before being verified");
   entry->add_verification_constraint(name, from_name, from_field_is_protected,
                                      from_is_array, from_is_object);
   if (entry->is_builtin()) {
--- a/src/hotspot/share/classfile/verifier.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/classfile/verifier.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -719,7 +719,8 @@
         ResourceMark rm(THREAD);
         LogStream ls(lt);
         current_frame.print_on(&ls);
-        lt.print("offset = %d,  opcode = %s", bci, Bytecodes::name(opcode));
+        lt.print("offset = %d,  opcode = %s", bci,
+                 opcode == Bytecodes::_illegal ? "illegal" : Bytecodes::name(opcode));
       }
 
       // Make sure wide instruction is in correct format
--- a/src/hotspot/share/logging/logOutput.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/logging/logOutput.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -262,7 +262,9 @@
   while (n_deviates > 0) {
     size_t prev_deviates = n_deviates;
     int max_score = 0;
-    const LogSelection* best_selection = NULL;
+
+    guarantee(n_selections > 0, "Cannot find maximal selection.");
+    const LogSelection* best_selection = &selections[0];
     for (size_t i = 0; i < n_selections; i++) {
 
       // Give the selection a score based on how many deviating tag sets it selects (with correct level)
@@ -287,13 +289,12 @@
 
       // Pick the selection with the best score, or in the case of a tie, the one with fewest tags
       if (score > max_score ||
-          (score == max_score && best_selection != NULL && selections[i].ntags() < best_selection->ntags())) {
+          (score == max_score && selections[i].ntags() < best_selection->ntags())) {
         max_score = score;
         best_selection = &selections[i];
       }
     }
 
-    assert(best_selection != NULL, "must always find a maximal selection");
     add_to_config_string(*best_selection);
 
     // Remove all deviates that this selection covered
--- a/src/hotspot/share/memory/filemap.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/memory/filemap.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -631,7 +631,9 @@
   si->_read_only = read_only;
   si->_allow_exec = allow_exec;
   si->_crc = ClassLoader::crc32(0, base, (jint)size);
-  write_bytes_aligned(base, (int)size);
+  if (base != NULL) {
+    write_bytes_aligned(base, (int)size);
+  }
 }
 
 // Write out the given archive heap memory regions.  GC code combines multiple
--- a/src/hotspot/share/memory/metaspace.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/memory/metaspace.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -1260,7 +1260,9 @@
       tty->print_cr("Please increase MaxMetaspaceSize (currently " SIZE_FORMAT " bytes).", MaxMetaspaceSize);
       vm_exit(1);
     }
-    report_metadata_oome(loader_data, word_size, type, mdtype, CHECK_NULL);
+    report_metadata_oome(loader_data, word_size, type, mdtype, THREAD);
+    assert(HAS_PENDING_EXCEPTION, "sanity");
+    return NULL;
   }
 
   // Zero initialize.
--- a/src/hotspot/share/memory/virtualspace.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/memory/virtualspace.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -70,6 +70,18 @@
   initialize(size, alignment, large, NULL, executable);
 }
 
+ReservedSpace::ReservedSpace(char* base, size_t size, size_t alignment,
+                             bool special, bool executable) : _fd_for_heap(-1) {
+  assert((size % os::vm_allocation_granularity()) == 0,
+         "size not allocation aligned");
+  _base = base;
+  _size = size;
+  _alignment = alignment;
+  _noaccess_prefix = 0;
+  _special = special;
+  _executable = executable;
+}
+
 // Helper method
 static void unmap_or_release_memory(char* base, size_t size, bool is_file_mapped) {
   if (is_file_mapped) {
@@ -218,20 +230,6 @@
   }
 }
 
-
-ReservedSpace::ReservedSpace(char* base, size_t size, size_t alignment,
-                             bool special, bool executable) {
-  assert((size % os::vm_allocation_granularity()) == 0,
-         "size not allocation aligned");
-  _base = base;
-  _size = size;
-  _alignment = alignment;
-  _noaccess_prefix = 0;
-  _special = special;
-  _executable = executable;
-}
-
-
 ReservedSpace ReservedSpace::first_part(size_t partition_size, size_t alignment,
                                         bool split, bool realloc) {
   assert(partition_size <= size(), "partition failed");
--- a/src/hotspot/share/prims/jvmtiEnvBase.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/prims/jvmtiEnvBase.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -1219,7 +1219,7 @@
   }
   infop->state = state;
 
-  if (thr != NULL || (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
+  if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
     infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
     env()->get_stack_trace(thr, 0, max_frame_count(),
                            infop->frame_buffer, &(infop->frame_count));
--- a/src/hotspot/share/runtime/flags/jvmFlag.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/runtime/flags/jvmFlag.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -939,6 +939,10 @@
     }
   }
 
+  if (match == NULL) {
+    return NULL;
+  }
+
   if (!(match->is_unlocked() || match->is_unlocker())) {
     if (!allow_locked) {
       return NULL;
--- a/src/hotspot/share/services/writeableFlags.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/services/writeableFlags.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -79,7 +79,7 @@
     case JVMFlag::NON_WRITABLE:
       buffer_concat(buffer, "flag is not writeable."); break;
     case JVMFlag::OUT_OF_BOUNDS:
-      print_flag_error_message_bounds(name, buffer); break;
+      if (name != NULL) { print_flag_error_message_bounds(name, buffer); } break;
     case JVMFlag::VIOLATES_CONSTRAINT:
       buffer_concat(buffer, "value violates its flag's constraint."); break;
     case JVMFlag::INVALID_FLAG:
--- a/src/hotspot/share/utilities/ostream.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/src/hotspot/share/utilities/ostream.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -531,7 +531,8 @@
 long fileStream::fileSize() {
   long size = -1;
   if (_file != NULL) {
-    long pos  = ::ftell(_file);
+    long pos = ::ftell(_file);
+    if (pos < 0) return pos;
     if (::fseek(_file, 0, SEEK_END) == 0) {
       size = ::ftell(_file);
     }
--- a/test/hotspot/gtest/logging/logTestUtils.inline.hpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/test/hotspot/gtest/logging/logTestUtils.inline.hpp	Wed Jul 11 16:11:10 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, 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
@@ -95,6 +95,7 @@
   int buflen = 512;
   char* buf = NEW_RESOURCE_ARRAY(char, buflen);
   long pos = ftell(fp);
+  if (pos < 0) return NULL;
 
   char* ret = fgets(buf, buflen, fp);
   while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) {
--- a/test/hotspot/gtest/memory/test_metachunk.cpp	Thu Jul 12 16:30:47 2018 +0200
+++ b/test/hotspot/gtest/memory/test_metachunk.cpp	Wed Jul 11 16:11:10 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, 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
@@ -60,7 +60,7 @@
   // Check sizes
   EXPECT_EQ(metachunk->size(), metachunk->word_size());
   EXPECT_EQ(pointer_delta(metachunk->end(), metachunk->bottom(),
-                sizeof (MetaWord*)),
+                          sizeof (MetaWord)),
             metachunk->word_size());
 
   // Check usage