src/share/vm/memory/guardedMemory.cpp

Fri, 22 Jun 2018 01:55:23 -0700

author
kevinw
date
Fri, 22 Jun 2018 01:55:23 -0700
changeset 9336
0fa4c2b668b9
parent 7032
fa62fb12cdca
permissions
-rw-r--r--

8198304: VS2017 (C4838, C4312) Various conversion issues with gtest tests
Summary: Introduce specific casts to fix multiple type cast conversion compilation errors.
Reviewed-by: coffeys, lfoltan, gtriantafill, hseigel

     1 /*
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    24 #include "precompiled.hpp"
    25 #include "memory/allocation.hpp"
    26 #include "memory/allocation.inline.hpp"
    27 #include "memory/guardedMemory.hpp"
    28 #include "runtime/os.hpp"
    30 void* GuardedMemory::wrap_copy(const void* ptr, const size_t len, const void* tag) {
    31   size_t total_sz = GuardedMemory::get_total_size(len);
    32   void* outerp = os::malloc(total_sz, mtInternal);
    33   if (outerp != NULL) {
    34     GuardedMemory guarded(outerp, len, tag);
    35     void* innerp = guarded.get_user_ptr();
    36     memcpy(innerp, ptr, len);
    37     return innerp;
    38   }
    39   return NULL; // OOM
    40 }
    42 bool GuardedMemory::free_copy(void* p) {
    43   if (p == NULL) {
    44     return true;
    45   }
    46   GuardedMemory guarded((u_char*)p);
    47   bool verify_ok = guarded.verify_guards();
    49   /* always attempt to free, pass problem on to any nested memchecker */
    50   os::free(guarded.release_for_freeing());
    52   return verify_ok;
    53 }
    55 void GuardedMemory::print_on(outputStream* st) const {
    56   if (_base_addr == NULL) {
    57     st->print_cr("GuardedMemory(" PTR_FORMAT ") not associated to any memory", p2i(this));
    58     return;
    59   }
    60   st->print_cr("GuardedMemory(" PTR_FORMAT ") base_addr=" PTR_FORMAT
    61       " tag=" PTR_FORMAT " user_size=" SIZE_FORMAT " user_data=" PTR_FORMAT,
    62       p2i(this), p2i(_base_addr), p2i(get_tag()), get_user_size(), p2i(get_user_ptr()));
    64   Guard* guard = get_head_guard();
    65   st->print_cr("  Header guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN"));
    66   guard = get_tail_guard();
    67   st->print_cr("  Trailer guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN"));
    69   u_char udata = *get_user_ptr();
    70   switch (udata) {
    71   case uninitBlockPad:
    72     st->print_cr("  User data appears unused");
    73     break;
    74   case freeBlockPad:
    75     st->print_cr("  User data appears to have been freed");
    76     break;
    77   default:
    78     st->print_cr("  User data appears to be in use");
    79     break;
    80   }
    81 }
    83 // test code...
    85 #ifndef PRODUCT
    87 #define GEN_PURPOSE_TAG ((void *) ((uintptr_t)0xf000f000))
    89 static void guarded_memory_test_check(void* p, size_t sz, void* tag) {
    90   assert(p != NULL, "NULL pointer given to check");
    91   u_char* c = (u_char*) p;
    92   GuardedMemory guarded(c);
    93   assert(guarded.get_tag() == tag, "Tag is not the same as supplied");
    94   assert(guarded.get_user_ptr() == c, "User pointer is not the same as supplied");
    95   assert(guarded.get_user_size() == sz, "User size is not the same as supplied");
    96   assert(guarded.verify_guards(), "Guard broken");
    97 }
    99 void GuardedMemory::test_guarded_memory() {
   100   // Test the basic characteristics...
   101   size_t total_sz = GuardedMemory::get_total_size(1);
   102   assert(total_sz > 1 && total_sz >= (sizeof(GuardHeader) + 1 + sizeof(Guard)), "Unexpected size");
   103   u_char* basep = (u_char*) os::malloc(total_sz, mtInternal);
   105   GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);
   107   assert(*basep == badResourceValue, "Expected guard in the form of badResourceValue");
   108   u_char* userp = guarded.get_user_ptr();
   109   assert(*userp == uninitBlockPad, "Expected uninitialized data in the form of uninitBlockPad");
   110   guarded_memory_test_check(userp, 1, GEN_PURPOSE_TAG);
   112   void* freep = guarded.release_for_freeing();
   113   assert((u_char*)freep == basep, "Expected the same pointer guard was ");
   114   assert(*userp == freeBlockPad, "Expected user data to be free block padded");
   115   assert(!guarded.verify_guards(), "Expected failed");
   116   os::free(freep);
   118   // Test a number of odd sizes...
   119   size_t sz = 0;
   120   do {
   121     void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
   122     void* up = guarded.wrap_with_guards(p, sz, (void*)1);
   123     memset(up, 0, sz);
   124     guarded_memory_test_check(up, sz, (void*)1);
   125     os::free(guarded.release_for_freeing());
   126     sz = (sz << 4) + 1;
   127   } while (sz < (256 * 1024));
   129   // Test buffer overrun into head...
   130   basep = (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
   131   guarded.wrap_with_guards(basep, 1);
   132   *basep = 0;
   133   assert(!guarded.verify_guards(), "Expected failure");
   134   os::free(basep);
   136   // Test buffer overrun into tail with a number of odd sizes...
   137   sz = 1;
   138   do {
   139     void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
   140     void* up = guarded.wrap_with_guards(p, sz, (void*)1);
   141     memset(up, 0, sz + 1); // Buffer-overwrite (within guard)
   142     assert(!guarded.verify_guards(), "Guard was not broken as expected");
   143     os::free(guarded.release_for_freeing());
   144     sz = (sz << 4) + 1;
   145   } while (sz < (256 * 1024));
   147   // Test wrap_copy/wrap_free...
   148   assert(GuardedMemory::free_copy(NULL), "Expected free NULL to be OK");
   150   const char* str = "Check my bounds out";
   151   size_t str_sz = strlen(str) + 1;
   152   char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz);
   153   guarded_memory_test_check(str_copy, str_sz, NULL);
   154   assert(strcmp(str, str_copy) == 0, "Not identical copy");
   155   assert(GuardedMemory::free_copy(str_copy), "Free copy failed to verify");
   157   void* no_data = NULL;
   158   void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0);
   159   assert(GuardedMemory::free_copy(no_data_copy), "Expected valid guards even for no data copy");
   160 }
   162 #endif // !PRODUCT

mercurial