Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/memory/metaspace/allocationGuard.hpp
40957 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2020 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
#ifndef SHARE_MEMORY_METASPACE_ALLOCATIONGUARD_HPP
27
#define SHARE_MEMORY_METASPACE_ALLOCATIONGUARD_HPP
28
29
#include "memory/allocation.hpp"
30
#include "memory/metaspace/chunklevel.hpp"
31
#include "utilities/globalDefinitions.hpp"
32
33
// In Debug builds, Metadata in Metaspace can be optionally guarded - enclosed in canaries -
34
// to detect memory overwriters.
35
//
36
// These canaries are periodically checked, e.g. when the Metaspace is purged in a context
37
// of a GC.
38
39
// The canaries precede any allocated block...
40
//
41
// +---------------+
42
// | 'METAMETA' |
43
// +---------------+
44
// | block size |
45
// +---------------+
46
// | block... |
47
// . .
48
// . .
49
// . .
50
// | |
51
// +---------------+
52
// . <padding> .
53
// +---------------+
54
// | 'METAMETA' |
55
// +---------------+
56
// | block size |
57
// +---------------+
58
// | block... |
59
60
// ... and since the blocks are allocated via pointer bump and closely follow each other,
61
// one block's prefix is its predecessor's suffix, so apart from the last block all
62
// blocks have an overwriter canary on both ends.
63
//
64
65
// Note: this feature is only available in debug, and is activated using
66
// -XX:+MetaspaceGuardAllocations. When active, it disables deallocation handling - since
67
// freeblock handling in the freeblock lists would get too complex - so one may run leaks
68
// in deallocation-heavy scenarios (e.g. lots of class redefinitions).
69
//
70
71
namespace metaspace {
72
73
#ifdef ASSERT
74
75
struct Prefix {
76
static const uintx EyeCatcher =
77
NOT_LP64(0x77698465) LP64_ONLY(0x7769846577698465ULL); // "META" resp "METAMETA"
78
79
const uintx _mark;
80
const size_t _word_size; // raw word size including prefix
81
// MetaWord payload [0]; // varsized (but unfortunately not all our compilers understand that)
82
83
Prefix(size_t word_size) :
84
_mark(EyeCatcher),
85
_word_size(word_size)
86
{}
87
88
MetaWord* payload() const {
89
return (MetaWord*)(this + 1);
90
}
91
92
bool is_valid() const {
93
return _mark == EyeCatcher && _word_size > 0 && _word_size < chunklevel::MAX_CHUNK_WORD_SIZE;
94
}
95
96
};
97
98
// The prefix structure must be aligned to MetaWord size.
99
STATIC_ASSERT((sizeof(Prefix) & WordAlignmentMask) == 0);
100
101
inline size_t prefix_size() {
102
return sizeof(Prefix);
103
}
104
105
// Given a pointer to a memory area, establish the prefix at the start of that area and
106
// return the starting pointer to the payload.
107
inline MetaWord* establish_prefix(MetaWord* p_raw, size_t raw_word_size) {
108
const Prefix* pp = new(p_raw)Prefix(raw_word_size);
109
return pp->payload();
110
}
111
112
#endif
113
114
} // namespace metaspace
115
116
#endif // SHARE_MEMORY_METASPACE_ALLOCATIONGUARD_HPP
117
118