hotspot/src/share/vm/memory/guardedMemory.hpp
author david
Tue, 29 Sep 2015 11:02:08 +0200
changeset 33105 294e48b4f704
parent 30578 8b6c44532ca2
child 42018 921e8769926b
permissions -rw-r--r--
8080775: Better argument formatting for assert() and friends Reviewed-by: kbarrett, pliden
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25056
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     1
/*
30578
8b6c44532ca2 8079148: Fix incorrect include guards
pliden
parents: 25058
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
25056
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     4
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     8
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    13
 * accompanied this code).
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    14
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    18
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    21
 * questions.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    22
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    23
 */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    24
30578
8b6c44532ca2 8079148: Fix incorrect include guards
pliden
parents: 25058
diff changeset
    25
#ifndef SHARE_VM_MEMORY_GUARDEDMEMORY_HPP
8b6c44532ca2 8079148: Fix incorrect include guards
pliden
parents: 25058
diff changeset
    26
#define SHARE_VM_MEMORY_GUARDEDMEMORY_HPP
25056
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    27
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    28
#include "memory/allocation.hpp"
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    30
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    31
/**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    32
 * Guarded memory for detecting buffer overrun.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    33
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    34
 * Allows allocations to be wrapped with padded bytes of a known byte pattern,
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    35
 * that is a "guard". Guard patterns may be verified to detect buffer overruns.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    36
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    37
 * Primarily used by "debug malloc" and "checked JNI".
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    38
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    39
 * Memory layout:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    40
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    41
 * |Offset             | Content              | Description    |
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    42
 * |------------------------------------------------------------
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    43
 * |base_addr          | 0xABABABABABABABAB   | Head guard     |
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    44
 * |+16                | <size_t:user_size>   | User data size |
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    45
 * |+sizeof(uintptr_t) | <tag>                | Tag word       |
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    46
 * |+sizeof(void*)     | 0xF1 <user_data> (   | User data      |
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    47
 * |+user_size         | 0xABABABABABABABAB   | Tail guard     |
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    48
 * -------------------------------------------------------------
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    49
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    50
 * Where:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    51
 *  - guard padding uses "badResourceValue" (0xAB)
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    52
 *  - tag word is general purpose
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    53
 *  - user data
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    54
 *    -- initially padded with "uninitBlockPad" (0xF1),
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    55
 *    -- to "freeBlockPad" (0xBA), when freed
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    56
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    57
 * Usage:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    58
 *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    59
 * * Allocations: one may wrap allocations with guard memory:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    60
 * <code>
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    61
 *   Thing* alloc_thing() {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    62
 *     void* mem = user_alloc_fn(GuardedMemory::get_total_size(sizeof(thing)));
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    63
 *     GuardedMemory guarded(mem, sizeof(thing));
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    64
 *     return (Thing*) guarded.get_user_ptr();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    65
 *   }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    66
 * </code>
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    67
 * * Verify: memory guards are still in tact
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    68
 * <code>
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    69
 *   bool verify_thing(Thing* thing) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    70
 *     GuardedMemory guarded((void*)thing);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    71
 *     return guarded.verify_guards();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    72
 *   }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    73
 * </code>
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    74
 * * Free: one may mark bytes as freed (further debugging support)
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    75
 * <code>
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    76
 *   void free_thing(Thing* thing) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    77
 *    GuardedMemory guarded((void*)thing);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    78
 *    assert(guarded.verify_guards(), "Corrupt thing");
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    79
 *    user_free_fn(guards.release_for_freeing();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    80
 *   }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    81
 * </code>
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    82
 */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    83
class GuardedMemory : StackObj { // Wrapper on stack
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    84
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    85
  // Private inner classes for memory layout...
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    86
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    87
protected:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    88
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    89
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    90
   * Guard class for header and trailer known pattern to test for overwrites.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    91
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    92
  class Guard { // Class for raw memory (no vtbl allowed)
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    93
    friend class GuardedMemory;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    94
   protected:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    95
    enum {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    96
      GUARD_SIZE = 16
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    97
    };
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    98
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
    99
    u_char _guard[GUARD_SIZE];
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   100
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   101
   public:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   102
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   103
    void build() {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   104
      u_char* c = _guard; // Possibly unaligned if tail guard
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   105
      u_char* end = c + GUARD_SIZE;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   106
      while (c < end) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   107
        *c = badResourceValue;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   108
        c++;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   109
      }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   110
    }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   111
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   112
    bool verify() const {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   113
      u_char* c = (u_char*) _guard;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   114
      u_char* end = c + GUARD_SIZE;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   115
      while (c < end) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   116
        if (*c != badResourceValue) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   117
          return false;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   118
        }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   119
        c++;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   120
      }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   121
      return true;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   122
    }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   123
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   124
  }; // GuardedMemory::Guard
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   125
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   126
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   127
   * Header guard and size
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   128
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   129
  class GuardHeader : Guard {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   130
    friend class GuardedMemory;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   131
   protected:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   132
    // Take care in modifying fields here, will effect alignment
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   133
    // e.g. x86 ABI 16 byte stack alignment
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   134
    union {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   135
      uintptr_t __unused_full_word1;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   136
      size_t _user_size;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   137
    };
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   138
    void* _tag;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   139
   public:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   140
    void set_user_size(const size_t usz) { _user_size = usz; }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   141
    size_t get_user_size() const { return _user_size; }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   142
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   143
    void set_tag(const void* tag) { _tag = (void*) tag; }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   144
    void* get_tag() const { return _tag; }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   145
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   146
  }; // GuardedMemory::GuardHeader
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   147
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   148
  // Guarded Memory...
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   149
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   150
 protected:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   151
  u_char* _base_addr;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   152
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   153
 public:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   154
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   155
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   156
   * Create new guarded memory.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   157
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   158
   * Wraps, starting at the given "base_ptr" with guards. Use "get_user_ptr()"
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   159
   * to return a pointer suitable for user data.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   160
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   161
   * @param base_ptr  allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   162
   * @param user_size the size of the user data to be wrapped.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   163
   * @param tag       optional general purpose tag.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   164
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   165
  GuardedMemory(void* base_ptr, const size_t user_size, const void* tag = NULL) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   166
    wrap_with_guards(base_ptr, user_size, tag);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   167
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   168
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   169
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   170
   * Wrap existing guarded memory.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   171
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   172
   * To use this constructor, one must have created guarded memory with
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   173
   * "GuardedMemory(void*, size_t, void*)" (or indirectly via helper, e.g. "wrap_copy()").
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   174
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   175
   * @param user_p  existing wrapped memory.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   176
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   177
  GuardedMemory(void* userp) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   178
    u_char* user_ptr = (u_char*) userp;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   179
    assert((uintptr_t)user_ptr > (sizeof(GuardHeader) + 0x1000), "Invalid pointer");
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   180
    _base_addr = (user_ptr - sizeof(GuardHeader));
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   181
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   182
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   183
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   184
   * Create new guarded memory.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   185
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   186
   * Wraps, starting at the given "base_ptr" with guards. Allows reuse of stack allocated helper.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   187
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   188
   * @param base_ptr  allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   189
   * @param user_size the size of the user data to be wrapped.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   190
   * @param tag       optional general purpose tag.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   191
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   192
   * @return user data pointer (inner pointer to supplied "base_ptr").
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   193
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   194
  void* wrap_with_guards(void* base_ptr, size_t user_size, const void* tag = NULL) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   195
    assert(base_ptr != NULL, "Attempt to wrap NULL with memory guard");
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   196
    _base_addr = (u_char*)base_ptr;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   197
    get_head_guard()->build();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   198
    get_head_guard()->set_user_size(user_size);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   199
    get_tail_guard()->build();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   200
    set_tag(tag);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   201
    set_user_bytes(uninitBlockPad);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   202
    assert(verify_guards(), "Expected valid memory guards");
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   203
    return get_user_ptr();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   204
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   205
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   206
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   207
   * Verify head and tail guards.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   208
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   209
   * @return true if guards are intact, false would indicate a buffer overrun.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   210
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   211
  bool verify_guards() const {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   212
    if (_base_addr != NULL) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   213
      return (get_head_guard()->verify() && get_tail_guard()->verify());
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   214
    }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   215
    return false;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   216
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   217
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   218
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   219
   * Set the general purpose tag.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   220
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   221
   * @param tag general purpose tag.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   222
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   223
  void set_tag(const void* tag) { get_head_guard()->set_tag(tag); }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   224
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   225
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   226
   * Return the general purpose tag.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   227
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   228
   * @return the general purpose tag, defaults to NULL.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   229
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   230
  void* get_tag() const { return get_head_guard()->get_tag(); }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   231
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   232
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   233
   * Return the size of the user data.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   234
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   235
   * @return the size of the user data.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   236
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   237
  size_t get_user_size() const {
25058
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 25056
diff changeset
   238
    assert(_base_addr != NULL, "Not wrapping any memory");
25056
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   239
    return get_head_guard()->get_user_size();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   240
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   241
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   242
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   243
   * Return the user data pointer.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   244
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   245
   * @return the user data pointer.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   246
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   247
  u_char* get_user_ptr() const {
25058
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 25056
diff changeset
   248
    assert(_base_addr != NULL, "Not wrapping any memory");
25056
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   249
    return _base_addr + sizeof(GuardHeader);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   250
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   251
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   252
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   253
   * Release the wrapped pointer for resource freeing.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   254
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   255
   * Pads the user data with "freeBlockPad", and dis-associates the helper.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   256
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   257
   * @return the original base pointer used to wrap the data.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   258
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   259
  void* release_for_freeing() {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   260
    set_user_bytes(freeBlockPad);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   261
    return release();
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   262
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   263
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   264
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   265
   * Dis-associate the help from the original base address.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   266
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   267
   * @return the original base pointer used to wrap the data.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   268
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   269
  void* release() {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   270
    void* p = (void*) _base_addr;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   271
    _base_addr = NULL;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   272
    return p;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   273
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   274
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   275
  virtual void print_on(outputStream* st) const;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   276
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   277
 protected:
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   278
  GuardHeader*  get_head_guard() const { return (GuardHeader*) _base_addr; }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   279
  Guard*        get_tail_guard() const { return (Guard*) (get_user_ptr() + get_user_size()); };
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   280
  void set_user_bytes(u_char ch) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   281
    memset(get_user_ptr(), ch, get_user_size());
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   282
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   283
25058
4542f853c2ac 8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents: 25056
diff changeset
   284
 public:
25056
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   285
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   286
   * Return the total size required for wrapping the given user size.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   287
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   288
   * @return the total size required for wrapping the given user size.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   289
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   290
  static size_t get_total_size(size_t user_size) {
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   291
    size_t total_size = sizeof(GuardHeader) + user_size + sizeof(Guard);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   292
    assert(total_size > user_size, "Unexpected wrap-around");
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   293
    return total_size;
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   294
  }
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   295
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   296
  // Helper functions...
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   297
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   298
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   299
   * Wrap a copy of size "len" of "ptr".
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   300
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   301
   * @param ptr the memory to be copied
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   302
   * @param len the length of the copy
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   303
   * @param tag optional general purpose tag (see GuardedMemory::get_tag())
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   304
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   305
   * @return guarded wrapped memory pointer to the user area, or NULL if OOM.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   306
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   307
  static void* wrap_copy(const void* p, const size_t len, const void* tag = NULL);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   308
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   309
  /**
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   310
   * Free wrapped copy.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   311
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   312
   * Frees memory copied with "wrap_copy()".
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   313
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   314
   * @param p memory returned by "wrap_copy()".
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   315
   *
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   316
   * @return true if guards were verified as intact. false indicates a buffer overrun.
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   317
   */
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   318
  static bool free_copy(void* p);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   319
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   320
  // Testing...
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   321
#ifndef PRODUCT
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   322
  static void test_guarded_memory(void);
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   323
#endif
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   324
}; // GuardedMemory
5ad92b0d1beb 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
dsimms
parents:
diff changeset
   325
30578
8b6c44532ca2 8079148: Fix incorrect include guards
pliden
parents: 25058
diff changeset
   326
#endif // SHARE_VM_MEMORY_GUARDEDMEMORY_HPP