Skip to content

Commit 4031cb4

Browse files
committed
8254189: Improve comments for StackOverFlow and fix in_xxx() functions
Reviewed-by: dholmes, gziemski
1 parent caec8d2 commit 4031cb4

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

src/hotspot/share/runtime/stackOverflow.hpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ class StackOverflow {
137137
return _stack_red_zone_size;
138138
}
139139

140+
// Returns base of red zone (one-beyond the highest red zone address, so
141+
// itself outside red zone and the highest address of the yellow zone).
140142
address stack_red_zone_base() const {
141143
return (address)(stack_end() + stack_red_zone_size());
142144
}
143145

146+
// Returns true if address points into the red zone.
144147
bool in_stack_red_zone(address a) const {
145-
return a <= stack_red_zone_base() && a >= stack_end();
148+
return a < stack_red_zone_base() && a >= stack_end();
146149
}
147150

148151
static size_t stack_yellow_zone_size() {
@@ -155,20 +158,25 @@ class StackOverflow {
155158
return _stack_reserved_zone_size;
156159
}
157160

161+
// Returns base of the reserved zone (one-beyond the highest reserved zone address).
158162
address stack_reserved_zone_base() const {
159163
return (address)(stack_end() +
160164
(stack_red_zone_size() + stack_yellow_zone_size() + stack_reserved_zone_size()));
161165
}
166+
167+
// Returns true if address points into the reserved zone.
162168
bool in_stack_reserved_zone(address a) const {
163-
return (a <= stack_reserved_zone_base()) &&
169+
return (a < stack_reserved_zone_base()) &&
164170
(a >= (address)((intptr_t)stack_reserved_zone_base() - stack_reserved_zone_size()));
165171
}
166172

167173
static size_t stack_yellow_reserved_zone_size() {
168174
return _stack_yellow_zone_size + _stack_reserved_zone_size;
169175
}
176+
177+
// Returns true if a points into either yellow or reserved zone.
170178
bool in_stack_yellow_reserved_zone(address a) const {
171-
return (a <= stack_reserved_zone_base()) && (a >= stack_red_zone_base());
179+
return (a < stack_reserved_zone_base()) && (a >= stack_red_zone_base());
172180
}
173181

174182
// Size of red + yellow + reserved zones.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2020, 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+
25+
#include "precompiled.hpp"
26+
#include "runtime/os.hpp"
27+
#include "runtime/globals.hpp"
28+
#include "runtime/stackOverflow.hpp"
29+
#include "utilities/align.hpp"
30+
#include "utilities/globalDefinitions.hpp"
31+
#include "utilities/ostream.hpp"
32+
#include "unittest.hpp"
33+
34+
35+
TEST_VM(StackOverflow, basics) {
36+
StackOverflow so;
37+
38+
// Make up a stack range. No need to allocate anything. Size has to be large enough
39+
// to fit sum of all zones into them.
40+
address base = (address) 0x40000000;
41+
const size_t size = os::vm_page_size() * 100;
42+
address end = base - size;
43+
so.initialize(base, end);
44+
45+
// Walking down the "stack" check for consistency of the three "in_stack_xxx" functions
46+
enum { normal_stack, reserved_or_yellow_zone, red_zone } where = normal_stack;
47+
for (address p = base - 1; p >= end; p -= os::vm_page_size()) {
48+
// tty->print_cr(PTR_FORMAT " %d %d %d", p2i(p),
49+
// (int)so.in_stack_reserved_zone(p),
50+
// (int)so.in_stack_yellow_reserved_zone(p),
51+
// (int)so.in_stack_red_zone(p));
52+
switch (where) {
53+
case normal_stack:
54+
ASSERT_FALSE(so.in_stack_red_zone(p));
55+
if (so.in_stack_yellow_reserved_zone(p)) {
56+
if (StackReservedPages > 0) {
57+
ASSERT_TRUE(so.in_stack_reserved_zone(p));
58+
} else {
59+
ASSERT_FALSE(so.in_stack_reserved_zone(p));
60+
}
61+
where = reserved_or_yellow_zone;
62+
} else {
63+
ASSERT_FALSE(so.in_stack_reserved_zone((p)));
64+
}
65+
break;
66+
case reserved_or_yellow_zone:
67+
if (so.in_stack_red_zone(p)) {
68+
ASSERT_FALSE(so.in_stack_yellow_reserved_zone(p));
69+
where = red_zone;
70+
} else {
71+
ASSERT_TRUE(so.in_stack_yellow_reserved_zone(p));
72+
}
73+
break;
74+
case red_zone:
75+
ASSERT_TRUE(so.in_stack_red_zone(p));
76+
ASSERT_FALSE(so.in_stack_yellow_reserved_zone(p));
77+
ASSERT_FALSE(so.in_stack_reserved_zone((p)));
78+
break;
79+
}
80+
}
81+
ASSERT_EQ(where, red_zone);
82+
83+
// Check bases.
84+
ASSERT_FALSE(so.in_stack_red_zone(so.stack_red_zone_base()));
85+
ASSERT_TRUE(so.in_stack_red_zone(so.stack_red_zone_base() - 1));
86+
ASSERT_TRUE(so.in_stack_yellow_reserved_zone(so.stack_red_zone_base()));
87+
ASSERT_FALSE(so.in_stack_reserved_zone(so.stack_reserved_zone_base()));
88+
if (so.stack_reserved_zone_size() > 0) {
89+
ASSERT_TRUE(so.in_stack_reserved_zone(so.stack_reserved_zone_base() - 1));
90+
}
91+
}

0 commit comments

Comments
 (0)