Path: blob/master/src/hotspot/share/memory/metaspace/allocationGuard.hpp
40957 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef SHARE_MEMORY_METASPACE_ALLOCATIONGUARD_HPP26#define SHARE_MEMORY_METASPACE_ALLOCATIONGUARD_HPP2728#include "memory/allocation.hpp"29#include "memory/metaspace/chunklevel.hpp"30#include "utilities/globalDefinitions.hpp"3132// In Debug builds, Metadata in Metaspace can be optionally guarded - enclosed in canaries -33// to detect memory overwriters.34//35// These canaries are periodically checked, e.g. when the Metaspace is purged in a context36// of a GC.3738// The canaries precede any allocated block...39//40// +---------------+41// | 'METAMETA' |42// +---------------+43// | block size |44// +---------------+45// | block... |46// . .47// . .48// . .49// | |50// +---------------+51// . <padding> .52// +---------------+53// | 'METAMETA' |54// +---------------+55// | block size |56// +---------------+57// | block... |5859// ... and since the blocks are allocated via pointer bump and closely follow each other,60// one block's prefix is its predecessor's suffix, so apart from the last block all61// blocks have an overwriter canary on both ends.62//6364// Note: this feature is only available in debug, and is activated using65// -XX:+MetaspaceGuardAllocations. When active, it disables deallocation handling - since66// freeblock handling in the freeblock lists would get too complex - so one may run leaks67// in deallocation-heavy scenarios (e.g. lots of class redefinitions).68//6970namespace metaspace {7172#ifdef ASSERT7374struct Prefix {75static const uintx EyeCatcher =76NOT_LP64(0x77698465) LP64_ONLY(0x7769846577698465ULL); // "META" resp "METAMETA"7778const uintx _mark;79const size_t _word_size; // raw word size including prefix80// MetaWord payload [0]; // varsized (but unfortunately not all our compilers understand that)8182Prefix(size_t word_size) :83_mark(EyeCatcher),84_word_size(word_size)85{}8687MetaWord* payload() const {88return (MetaWord*)(this + 1);89}9091bool is_valid() const {92return _mark == EyeCatcher && _word_size > 0 && _word_size < chunklevel::MAX_CHUNK_WORD_SIZE;93}9495};9697// The prefix structure must be aligned to MetaWord size.98STATIC_ASSERT((sizeof(Prefix) & WordAlignmentMask) == 0);99100inline size_t prefix_size() {101return sizeof(Prefix);102}103104// Given a pointer to a memory area, establish the prefix at the start of that area and105// return the starting pointer to the payload.106inline MetaWord* establish_prefix(MetaWord* p_raw, size_t raw_word_size) {107const Prefix* pp = new(p_raw)Prefix(raw_word_size);108return pp->payload();109}110111#endif112113} // namespace metaspace114115#endif // SHARE_MEMORY_METASPACE_ALLOCATIONGUARD_HPP116117118