8166563: Convert GuardedMemory_test to Gtest
authorkzhaldyb
Wed, 28 Sep 2016 17:18:45 +0300
changeset 42018 921e8769926b
parent 42017 ed85071e7d9d
child 42019 3755a8d3b514
8166563: Convert GuardedMemory_test to Gtest Reviewed-by: iignatyev
hotspot/src/share/vm/memory/guardedMemory.cpp
hotspot/src/share/vm/memory/guardedMemory.hpp
hotspot/src/share/vm/utilities/internalVMTests.cpp
hotspot/test/native/memory/test_guardedMemory.cpp
--- a/hotspot/src/share/vm/memory/guardedMemory.cpp	Wed Sep 28 17:26:43 2016 +0300
+++ b/hotspot/src/share/vm/memory/guardedMemory.cpp	Wed Sep 28 17:18:45 2016 +0300
@@ -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	Wed Sep 28 17:26:43 2016 +0300
+++ b/hotspot/src/share/vm/memory/guardedMemory.hpp	Wed Sep 28 17:18:45 2016 +0300
@@ -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/utilities/internalVMTests.cpp	Wed Sep 28 17:26:43 2016 +0300
+++ b/hotspot/src/share/vm/utilities/internalVMTests.cpp	Wed Sep 28 17:18:45 2016 +0300
@@ -53,7 +53,6 @@
   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 Sep 28 17:18:45 2016 +0300
@@ -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";
+}