Path: blob/master/src/hotspot/share/memory/guardedMemory.hpp
40949 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_MEMORY_GUARDEDMEMORY_HPP25#define SHARE_MEMORY_GUARDEDMEMORY_HPP2627#include "memory/allocation.hpp"28#include "utilities/globalDefinitions.hpp"2930/**31* Guarded memory for detecting buffer overrun.32*33* Allows allocations to be wrapped with padded bytes of a known byte pattern,34* that is a "guard". Guard patterns may be verified to detect buffer overruns.35*36* Primarily used by "debug malloc" and "checked JNI".37*38* Memory layout:39*40* |Offset | Content | Description |41* |------------------------------------------------------------42* |base_addr | 0xABABABABABABABAB | Head guard |43* |+16 | <size_t:user_size> | User data size |44* |+sizeof(uintptr_t) | <tag> | Tag word |45* |+sizeof(void*) | 0xF1 <user_data> ( | User data |46* |+user_size | 0xABABABABABABABAB | Tail guard |47* -------------------------------------------------------------48*49* Where:50* - guard padding uses "badResourceValue" (0xAB)51* - tag word is general purpose52* - user data53* -- initially padded with "uninitBlockPad" (0xF1),54* -- to "freeBlockPad" (0xBA), when freed55*56* Usage:57*58* * Allocations: one may wrap allocations with guard memory:59* <code>60* Thing* alloc_thing() {61* void* mem = user_alloc_fn(GuardedMemory::get_total_size(sizeof(thing)));62* GuardedMemory guarded(mem, sizeof(thing));63* return (Thing*) guarded.get_user_ptr();64* }65* </code>66* * Verify: memory guards are still in tact67* <code>68* bool verify_thing(Thing* thing) {69* GuardedMemory guarded((void*)thing);70* return guarded.verify_guards();71* }72* </code>73* * Free: one may mark bytes as freed (further debugging support)74* <code>75* void free_thing(Thing* thing) {76* GuardedMemory guarded((void*)thing);77* assert(guarded.verify_guards(), "Corrupt thing");78* user_free_fn(guards.release_for_freeing();79* }80* </code>81*/82class GuardedMemory : StackObj { // Wrapper on stack8384friend class GuardedMemoryTest;85// Private inner classes for memory layout...8687protected:8889/**90* Guard class for header and trailer known pattern to test for overwrites.91*/92class Guard { // Class for raw memory (no vtbl allowed)93friend class GuardedMemory;94protected:95enum {96GUARD_SIZE = 1697};9899u_char _guard[GUARD_SIZE];100101public:102103void build() {104u_char* c = _guard; // Possibly unaligned if tail guard105u_char* end = c + GUARD_SIZE;106while (c < end) {107*c = badResourceValue;108c++;109}110}111112bool verify() const {113u_char* c = (u_char*) _guard;114u_char* end = c + GUARD_SIZE;115while (c < end) {116if (*c != badResourceValue) {117return false;118}119c++;120}121return true;122}123124}; // GuardedMemory::Guard125126/**127* Header guard and size128*/129class GuardHeader : Guard {130friend class GuardedMemory;131protected:132// Take care in modifying fields here, will effect alignment133// e.g. x86 ABI 16 byte stack alignment134union {135uintptr_t __unused_full_word1;136size_t _user_size;137};138void* _tag;139public:140void set_user_size(const size_t usz) { _user_size = usz; }141size_t get_user_size() const { return _user_size; }142143void set_tag(const void* tag) { _tag = (void*) tag; }144void* get_tag() const { return _tag; }145146}; // GuardedMemory::GuardHeader147148// Guarded Memory...149150protected:151u_char* _base_addr;152153public:154155/**156* Create new guarded memory.157*158* Wraps, starting at the given "base_ptr" with guards. Use "get_user_ptr()"159* to return a pointer suitable for user data.160*161* @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.162* @param user_size the size of the user data to be wrapped.163* @param tag optional general purpose tag.164*/165GuardedMemory(void* base_ptr, const size_t user_size, const void* tag = NULL) {166wrap_with_guards(base_ptr, user_size, tag);167}168169/**170* Wrap existing guarded memory.171*172* To use this constructor, one must have created guarded memory with173* "GuardedMemory(void*, size_t, void*)" (or indirectly via helper, e.g. "wrap_copy()").174*175* @param user_p existing wrapped memory.176*/177GuardedMemory(void* userp) {178u_char* user_ptr = (u_char*) userp;179assert((uintptr_t)user_ptr > (sizeof(GuardHeader) + 0x1000), "Invalid pointer");180_base_addr = (user_ptr - sizeof(GuardHeader));181}182183/**184* Create new guarded memory.185*186* Wraps, starting at the given "base_ptr" with guards. Allows reuse of stack allocated helper.187*188* @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.189* @param user_size the size of the user data to be wrapped.190* @param tag optional general purpose tag.191*192* @return user data pointer (inner pointer to supplied "base_ptr").193*/194void* wrap_with_guards(void* base_ptr, size_t user_size, const void* tag = NULL) {195assert(base_ptr != NULL, "Attempt to wrap NULL with memory guard");196_base_addr = (u_char*)base_ptr;197get_head_guard()->build();198get_head_guard()->set_user_size(user_size);199get_tail_guard()->build();200set_tag(tag);201set_user_bytes(uninitBlockPad);202assert(verify_guards(), "Expected valid memory guards");203return get_user_ptr();204}205206/**207* Verify head and tail guards.208*209* @return true if guards are intact, false would indicate a buffer overrun.210*/211bool verify_guards() const {212if (_base_addr != NULL) {213return (get_head_guard()->verify() && get_tail_guard()->verify());214}215return false;216}217218/**219* Set the general purpose tag.220*221* @param tag general purpose tag.222*/223void set_tag(const void* tag) { get_head_guard()->set_tag(tag); }224225/**226* Return the general purpose tag.227*228* @return the general purpose tag, defaults to NULL.229*/230void* get_tag() const { return get_head_guard()->get_tag(); }231232/**233* Return the size of the user data.234*235* @return the size of the user data.236*/237size_t get_user_size() const {238assert(_base_addr != NULL, "Not wrapping any memory");239return get_head_guard()->get_user_size();240}241242/**243* Return the user data pointer.244*245* @return the user data pointer.246*/247u_char* get_user_ptr() const {248assert(_base_addr != NULL, "Not wrapping any memory");249return _base_addr + sizeof(GuardHeader);250}251252/**253* Release the wrapped pointer for resource freeing.254*255* Pads the user data with "freeBlockPad", and dis-associates the helper.256*257* @return the original base pointer used to wrap the data.258*/259void* release_for_freeing() {260set_user_bytes(freeBlockPad);261return release();262}263264/**265* Dis-associate the help from the original base address.266*267* @return the original base pointer used to wrap the data.268*/269void* release() {270void* p = (void*) _base_addr;271_base_addr = NULL;272return p;273}274275virtual void print_on(outputStream* st) const;276277protected:278GuardHeader* get_head_guard() const { return (GuardHeader*) _base_addr; }279Guard* get_tail_guard() const { return (Guard*) (get_user_ptr() + get_user_size()); };280void set_user_bytes(u_char ch) {281memset(get_user_ptr(), ch, get_user_size());282}283284public:285/**286* Return the total size required for wrapping the given user size.287*288* @return the total size required for wrapping the given user size.289*/290static size_t get_total_size(size_t user_size) {291size_t total_size = sizeof(GuardHeader) + user_size + sizeof(Guard);292assert(total_size > user_size, "Unexpected wrap-around");293return total_size;294}295296// Helper functions...297298/**299* Wrap a copy of size "len" of "ptr".300*301* @param ptr the memory to be copied302* @param len the length of the copy303* @param tag optional general purpose tag (see GuardedMemory::get_tag())304*305* @return guarded wrapped memory pointer to the user area, or NULL if OOM.306*/307static void* wrap_copy(const void* p, const size_t len, const void* tag = NULL);308309/**310* Free wrapped copy.311*312* Frees memory copied with "wrap_copy()".313*314* @param p memory returned by "wrap_copy()".315*316* @return true if guards were verified as intact. false indicates a buffer overrun.317*/318static bool free_copy(void* p);319320}; // GuardedMemory321322#endif // SHARE_MEMORY_GUARDEDMEMORY_HPP323324325