Merge
authorkzhaldyb
Wed, 19 Oct 2016 12:10:43 +0200
changeset 42019 3755a8d3b514
parent 42016 4ef693df9ff9 (current diff)
parent 42018 921e8769926b (diff)
child 42020 8eacd62bf4f7
child 42021 390db590f103
Merge
--- a/hotspot/src/share/vm/memory/guardedMemory.cpp	Thu Oct 06 16:32:46 2016 +0200
+++ b/hotspot/src/share/vm/memory/guardedMemory.cpp	Wed Oct 19 12:10:43 2016 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 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
@@ -79,87 +79,3 @@
     break;
   }
 }
-
-// test code...
-
-#ifndef PRODUCT
-
-static void guarded_memory_test_check(void* p, size_t sz, void* tag) {
-  assert(p != NULL, "NULL pointer given to check");
-  u_char* c = (u_char*) p;
-  GuardedMemory guarded(c);
-  assert(guarded.get_tag() == tag, "Tag is not the same as supplied");
-  assert(guarded.get_user_ptr() == c, "User pointer is not the same as supplied");
-  assert(guarded.get_user_size() == sz, "User size is not the same as supplied");
-  assert(guarded.verify_guards(), "Guard broken");
-}
-
-void GuardedMemory::test_guarded_memory() {
-  // Test the basic characteristics...
-  size_t total_sz = GuardedMemory::get_total_size(1);
-  assert(total_sz > 1 && total_sz >= (sizeof(GuardHeader) + 1 + sizeof(Guard)), "Unexpected size");
-  u_char* basep = (u_char*) os::malloc(total_sz, mtInternal);
-
-  GuardedMemory guarded(basep, 1, (void*)0xf000f000);
-
-  assert(*basep == badResourceValue, "Expected guard in the form of badResourceValue");
-  u_char* userp = guarded.get_user_ptr();
-  assert(*userp == uninitBlockPad, "Expected uninitialized data in the form of uninitBlockPad");
-  guarded_memory_test_check(userp, 1, (void*)0xf000f000);
-
-  void* freep = guarded.release_for_freeing();
-  assert((u_char*)freep == basep, "Expected the same pointer guard was ");
-  assert(*userp == freeBlockPad, "Expected user data to be free block padded");
-  assert(!guarded.verify_guards(), "Expected failed");
-  os::free(freep);
-
-  // Test a number of odd sizes...
-  size_t sz = 0;
-  do {
-    void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
-    void* up = guarded.wrap_with_guards(p, sz, (void*)1);
-    memset(up, 0, sz);
-    guarded_memory_test_check(up, sz, (void*)1);
-    os::free(guarded.release_for_freeing());
-    sz = (sz << 4) + 1;
-  } while (sz < (256 * 1024));
-
-  // Test buffer overrun into head...
-  basep = (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
-  guarded.wrap_with_guards(basep, 1);
-  *basep = 0;
-  assert(!guarded.verify_guards(), "Expected failure");
-  os::free(basep);
-
-  // Test buffer overrun into tail with a number of odd sizes...
-  sz = 1;
-  do {
-    void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
-    void* up = guarded.wrap_with_guards(p, sz, (void*)1);
-    memset(up, 0, sz + 1); // Buffer-overwrite (within guard)
-    assert(!guarded.verify_guards(), "Guard was not broken as expected");
-    os::free(guarded.release_for_freeing());
-    sz = (sz << 4) + 1;
-  } while (sz < (256 * 1024));
-
-  // Test wrap_copy/wrap_free...
-  assert(GuardedMemory::free_copy(NULL), "Expected free NULL to be OK");
-
-  const char* str = "Check my bounds out";
-  size_t str_sz = strlen(str) + 1;
-  char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz);
-  guarded_memory_test_check(str_copy, str_sz, NULL);
-  assert(strcmp(str, str_copy) == 0, "Not identical copy");
-  assert(GuardedMemory::free_copy(str_copy), "Free copy failed to verify");
-
-  void* no_data = NULL;
-  void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0);
-  assert(GuardedMemory::free_copy(no_data_copy), "Expected valid guards even for no data copy");
-}
-
-void GuardedMemory_test() {
-  GuardedMemory::test_guarded_memory();
-}
-
-#endif // !PRODUCT
-
--- a/hotspot/src/share/vm/memory/guardedMemory.hpp	Thu Oct 06 16:32:46 2016 +0200
+++ b/hotspot/src/share/vm/memory/guardedMemory.hpp	Wed Oct 19 12:10:43 2016 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 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
@@ -82,6 +82,7 @@
  */
 class GuardedMemory : StackObj { // Wrapper on stack
 
+  friend class GuardedMemoryTest;
   // Private inner classes for memory layout...
 
 protected:
@@ -317,10 +318,6 @@
    */
   static bool free_copy(void* p);
 
-  // Testing...
-#ifndef PRODUCT
-  static void test_guarded_memory(void);
-#endif
 }; // GuardedMemory
 
 #endif // SHARE_VM_MEMORY_GUARDEDMEMORY_HPP
--- a/hotspot/src/share/vm/memory/metachunk.cpp	Thu Oct 06 16:32:46 2016 +0200
+++ b/hotspot/src/share/vm/memory/metachunk.cpp	Wed Oct 19 12:10:43 2016 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 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
@@ -111,61 +111,3 @@
   return;
 }
 
-/////////////// Unit tests ///////////////
-
-#ifndef PRODUCT
-
-class TestMetachunk {
- public:
-  static void test() {
-    size_t size = 2 * 1024 * 1024;
-    void* memory = malloc(size);
-    assert(memory != NULL, "Failed to malloc 2MB");
-
-    Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
-
-    assert(metachunk->bottom() == (MetaWord*)metachunk, "assert");
-    assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert");
-
-    // Check sizes
-    assert(metachunk->size() == metachunk->word_size(), "assert");
-    assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(),
-        sizeof(MetaWord*)), "assert");
-
-    // Check usage
-    assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
-    assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
-    assert(metachunk->top() == metachunk->initial_top(), "assert");
-    assert(metachunk->is_empty(), "assert");
-
-    // Allocate
-    size_t alloc_size = 64; // Words
-    assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert");
-
-    MetaWord* mem = metachunk->allocate(alloc_size);
-
-    // Check post alloc
-    assert(mem == metachunk->initial_top(), "assert");
-    assert(mem + alloc_size == metachunk->top(), "assert");
-    assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert");
-    assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
-    assert(!metachunk->is_empty(), "assert");
-
-    // Clear chunk
-    metachunk->reset_empty();
-
-    // Check post clear
-    assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
-    assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
-    assert(metachunk->top() == metachunk->initial_top(), "assert");
-    assert(metachunk->is_empty(), "assert");
-
-    free(memory);
-  }
-};
-
-void TestMetachunk_test() {
-  TestMetachunk::test();
-}
-
-#endif
--- a/hotspot/src/share/vm/memory/metachunk.hpp	Thu Oct 06 16:32:46 2016 +0200
+++ b/hotspot/src/share/vm/memory/metachunk.hpp	Wed Oct 19 12:10:43 2016 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 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
@@ -95,7 +95,7 @@
 //            +--------------+ <- bottom --+       --+
 
 class Metachunk : public Metabase<Metachunk> {
-  friend class TestMetachunk;
+  friend class MetachunkTest;
   // The VirtualSpaceNode containing this chunk.
   VirtualSpaceNode* _container;
 
--- a/hotspot/src/share/vm/utilities/internalVMTests.cpp	Thu Oct 06 16:32:46 2016 +0200
+++ b/hotspot/src/share/vm/utilities/internalVMTests.cpp	Wed Oct 19 12:10:43 2016 +0200
@@ -48,13 +48,11 @@
   run_unit_test(TestReserveMemorySpecial_test);
   run_unit_test(TestVirtualSpace_test);
   run_unit_test(TestMetaspaceAux_test);
-  run_unit_test(TestMetachunk_test);
   run_unit_test(TestVirtualSpaceNode_test);
   run_unit_test(TestGlobalDefinitions_test);
   run_unit_test(GCTimer_test);
   run_unit_test(CollectedHeap_test);
   run_unit_test(QuickSort_test);
-  run_unit_test(GuardedMemory_test);
   run_unit_test(TestNewSize_test);
   run_unit_test(TestOldSize_test);
   run_unit_test(TestBitMap_test);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/native/memory/test_guardedMemory.cpp	Wed Oct 19 12:10:43 2016 +0200
@@ -0,0 +1,148 @@
+/*
+ * 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 "precompiled.hpp"
+#include "memory/allocation.hpp"
+#include "memory/allocation.inline.hpp"
+#include "memory/guardedMemory.hpp"
+#include "runtime/os.hpp"
+#include "unittest.hpp"
+
+static void guarded_memory_test_check(void* p, size_t sz, void* tag) {
+  ASSERT_TRUE(p != NULL) << "NULL pointer given to check";
+  u_char* c = (u_char*) p;
+  GuardedMemory guarded(c);
+  EXPECT_EQ(guarded.get_tag(), tag) << "Tag is not the same as supplied";
+  EXPECT_EQ(guarded.get_user_ptr(), c) << "User pointer is not the same as supplied";
+  EXPECT_EQ(guarded.get_user_size(), sz) << "User size is not the same as supplied";
+  EXPECT_TRUE(guarded.verify_guards()) << "Guard broken";
+}
+
+class GuardedMemoryTest {
+ public:
+  static size_t get_guard_header_size() {
+    return sizeof (GuardedMemory::GuardHeader);
+  }
+  static size_t get_guard_size() {
+    return sizeof (GuardedMemory::Guard);
+  }
+};
+
+// Test GuardedMemory size
+TEST(GuardedMemory, size) {
+  size_t total_sz = GuardedMemory::get_total_size(1);
+  ASSERT_GT(total_sz, (size_t) 1) << "Unexpected size";
+  ASSERT_GE(total_sz, GuardedMemoryTest::get_guard_header_size() + 1
+          + GuardedMemoryTest::get_guard_size()) << "Unexpected size";
+}
+
+// Test the basic characteristics
+TEST(GuardedMemory, basic) {
+  u_char* basep =
+          (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
+  GuardedMemory guarded(basep, 1, (void*) 0xf000f000);
+
+  EXPECT_EQ(badResourceValue, *basep)
+          << "Expected guard in the form of badResourceValue";
+
+  u_char* userp = guarded.get_user_ptr();
+  EXPECT_EQ(uninitBlockPad, *userp)
+          << "Expected uninitialized data in the form of uninitBlockPad";
+  guarded_memory_test_check(userp, 1, (void*) 0xf000f000);
+
+  void* freep = guarded.release_for_freeing();
+  EXPECT_EQ((u_char*) freep, basep) << "Expected the same pointer guard was ";
+  EXPECT_EQ(freeBlockPad, *userp) << "Expected user data to be free block padded";
+  EXPECT_FALSE(guarded.verify_guards());
+  os::free(freep);
+}
+
+// Test a number of odd sizes
+TEST(GuardedMemory, odd_sizes) {
+  u_char* basep =
+          (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
+  GuardedMemory guarded(basep, 1, (void*) 0xf000f000);
+
+  size_t sz = 0;
+  do {
+    void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
+    void* up = guarded.wrap_with_guards(p, sz, (void*) 1);
+    memset(up, 0, sz);
+    guarded_memory_test_check(up, sz, (void*) 1);
+    if (HasFatalFailure()) {
+      return;
+    }
+
+    os::free(guarded.release_for_freeing());
+    sz = (sz << 4) + 1;
+  } while (sz < (256 * 1024));
+}
+
+// Test buffer overrun into head...
+TEST(GuardedMemory, buffer_overrun_head) {
+  u_char* basep =
+          (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
+  GuardedMemory guarded(basep, 1, (void*) 0xf000f000);
+
+  guarded.wrap_with_guards(basep, 1);
+  *basep = 0;
+  EXPECT_FALSE(guarded.verify_guards());
+  os::free(basep);
+}
+
+// Test buffer overrun into tail with a number of odd sizes
+TEST(GuardedMemory, buffer_overrun_tail) {
+  u_char* basep =
+          (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
+  GuardedMemory guarded(basep, 1, (void*) 0xf000f000);
+
+  size_t sz = 1;
+  do {
+    void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
+    void* up = guarded.wrap_with_guards(p, sz, (void*) 1);
+    memset(up, 0, sz + 1); // Buffer-overwrite (within guard)
+    EXPECT_FALSE(guarded.verify_guards()) << "Guard was not broken as expected";
+    os::free(guarded.release_for_freeing());
+    sz = (sz << 4) + 1;
+  } while (sz < (256 * 1024));
+}
+
+// Test wrap_copy/wrap_free
+TEST(GuardedMemory, wrap) {
+  EXPECT_TRUE(GuardedMemory::free_copy(NULL)) << "Expected free NULL to be OK";
+
+  const char* str = "Check my bounds out";
+  size_t str_sz = strlen(str) + 1;
+  char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz);
+  guarded_memory_test_check(str_copy, str_sz, NULL);
+  if (HasFatalFailure()) {
+    return;
+  }
+  EXPECT_EQ(0, strcmp(str, str_copy)) << "Not identical copy";
+  EXPECT_TRUE(GuardedMemory::free_copy(str_copy)) << "Free copy failed to verify";
+
+  void* no_data = NULL;
+  void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0);
+  EXPECT_TRUE(GuardedMemory::free_copy(no_data_copy))
+          << "Expected valid guards even for no data copy";
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/native/memory/test_metachunk.cpp	Wed Oct 19 12:10:43 2016 +0200
@@ -0,0 +1,90 @@
+/*
+ * 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 "precompiled.hpp"
+#include "memory/allocation.hpp"
+#include "memory/metachunk.hpp"
+#include "unittest.hpp"
+#include "utilities/copy.hpp"
+#include "utilities/debug.hpp"
+
+class MetachunkTest {
+ public:
+  static MetaWord* initial_top(Metachunk* metachunk) {
+    return metachunk->initial_top();
+  }
+  static MetaWord* top(Metachunk* metachunk) {
+    return metachunk->top();
+  }
+
+};
+
+TEST(Metachunk, basic) {
+  size_t size = 2 * 1024 * 1024;
+  void* memory = malloc(size);
+  ASSERT_TRUE(NULL != memory) << "Failed to malloc 2MB";
+
+  Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
+
+  EXPECT_EQ((MetaWord*) metachunk, metachunk->bottom());
+  EXPECT_EQ((uintptr_t*) metachunk + metachunk->size(), metachunk->end());
+
+  // Check sizes
+  EXPECT_EQ(metachunk->size(), metachunk->word_size());
+  EXPECT_EQ(pointer_delta(metachunk->end(), metachunk->bottom(),
+                sizeof (MetaWord*)),
+            metachunk->word_size());
+
+  // Check usage
+  EXPECT_EQ(metachunk->used_word_size(), metachunk->overhead());
+  EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(),
+            metachunk->free_word_size());
+  EXPECT_EQ(MetachunkTest::top(metachunk), MetachunkTest::initial_top(metachunk));
+  EXPECT_TRUE(metachunk->is_empty());
+
+  // Allocate
+  size_t alloc_size = 64; // Words
+  EXPECT_TRUE(is_size_aligned(alloc_size, Metachunk::object_alignment()));
+
+  MetaWord* mem = metachunk->allocate(alloc_size);
+
+  // Check post alloc
+  EXPECT_EQ(MetachunkTest::initial_top(metachunk), mem);
+  EXPECT_EQ(MetachunkTest::top(metachunk), mem + alloc_size);
+  EXPECT_EQ(metachunk->overhead() + alloc_size, metachunk->used_word_size());
+  EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(),
+            metachunk->free_word_size());
+  EXPECT_FALSE(metachunk->is_empty());
+
+  // Clear chunk
+  metachunk->reset_empty();
+
+  // Check post clear
+  EXPECT_EQ(metachunk->used_word_size(), metachunk->overhead());
+  EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(),
+            metachunk->free_word_size());
+  EXPECT_EQ(MetachunkTest::top(metachunk), MetachunkTest::initial_top(metachunk));
+  EXPECT_TRUE(metachunk->is_empty());
+
+  free(memory);
+}